2018-06-28 22:48:56 -04:00
|
|
|
package cid
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
mh "github.com/multiformats/go-multihash"
|
|
|
|
|
)
|
|
|
|
|
|
2018-08-09 23:55:45 -04:00
|
|
|
func TestV0Builder(t *testing.T) {
|
2018-06-28 22:48:56 -04:00
|
|
|
data := []byte("this is some test content")
|
|
|
|
|
|
|
|
|
|
// Construct c1
|
2018-08-09 23:55:45 -04:00
|
|
|
format := V0Builder{}
|
2018-06-28 22:48:56 -04:00
|
|
|
c1, err := format.Sum(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct c2
|
|
|
|
|
hash, err := mh.Sum(data, mh.SHA2_256, -1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2018-08-09 23:55:45 -04:00
|
|
|
c2 := NewCidV0(hash)
|
2018-06-28 22:48:56 -04:00
|
|
|
|
|
|
|
|
if !c1.Equals(c2) {
|
|
|
|
|
t.Fatal("cids mismatch")
|
|
|
|
|
}
|
|
|
|
|
if c1.Prefix() != c2.Prefix() {
|
|
|
|
|
t.Fatal("prefixes mismatch")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 23:55:45 -04:00
|
|
|
func TestV1Builder(t *testing.T) {
|
2018-06-28 22:48:56 -04:00
|
|
|
data := []byte("this is some test content")
|
|
|
|
|
|
|
|
|
|
// Construct c1
|
2018-08-10 00:13:49 -04:00
|
|
|
format := V1Builder{Codec: DagCBOR, MhType: mh.SHA2_256}
|
2018-06-28 22:48:56 -04:00
|
|
|
c1, err := format.Sum(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct c2
|
|
|
|
|
hash, err := mh.Sum(data, mh.SHA2_256, -1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2018-08-09 23:55:45 -04:00
|
|
|
c2 := NewCidV1(DagCBOR, hash)
|
2018-06-28 22:48:56 -04:00
|
|
|
|
|
|
|
|
if !c1.Equals(c2) {
|
|
|
|
|
t.Fatal("cids mismatch")
|
|
|
|
|
}
|
|
|
|
|
if c1.Prefix() != c2.Prefix() {
|
|
|
|
|
t.Fatal("prefixes mismatch")
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-09 23:55:45 -04:00
|
|
|
|
|
|
|
|
func TestCodecChange(t *testing.T) {
|
|
|
|
|
t.Run("Prefix-CidV0", func(t *testing.T) {
|
|
|
|
|
p := Prefix{Version: 0, Codec: DagProtobuf, MhType: mh.SHA2_256, MhLength: mh.DefaultLengths[mh.SHA2_256]}
|
|
|
|
|
testCodecChange(t, p)
|
|
|
|
|
})
|
|
|
|
|
t.Run("Prefix-CidV1", func(t *testing.T) {
|
|
|
|
|
p := Prefix{Version: 1, Codec: DagProtobuf, MhType: mh.SHA2_256, MhLength: mh.DefaultLengths[mh.SHA2_256]}
|
|
|
|
|
testCodecChange(t, p)
|
|
|
|
|
})
|
|
|
|
|
t.Run("V0Builder", func(t *testing.T) {
|
|
|
|
|
testCodecChange(t, V0Builder{})
|
|
|
|
|
})
|
|
|
|
|
t.Run("V1Builder", func(t *testing.T) {
|
2018-08-10 00:13:49 -04:00
|
|
|
testCodecChange(t, V1Builder{Codec: DagProtobuf, MhType: mh.SHA2_256})
|
2018-08-09 23:55:45 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testCodecChange(t *testing.T, b Builder) {
|
|
|
|
|
data := []byte("this is some test content")
|
|
|
|
|
|
|
|
|
|
if b.GetCodec() != DagProtobuf {
|
|
|
|
|
t.Fatal("original builder not using Protobuf codec")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b = b.WithCodec(Raw)
|
|
|
|
|
c, err := b.Sum(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.Type() != Raw {
|
|
|
|
|
t.Fatal("new cid codec did not change to Raw")
|
|
|
|
|
}
|
|
|
|
|
}
|