contracts/ens: added decode tests

This commit is contained in:
Elad 2019-03-08 17:07:28 +07:00
parent 3a14b32ab6
commit 46c2c1c623

View file

@ -124,7 +124,7 @@ func TestENS(t *testing.T) {
} }
t.Fatal("todo: try to set old contract with new multicodec stuff and assert fail, set new contract with multicodec stuff, encode, decode and assert returns correct hash") t.Fatal("todo: try to set old contract with new multicodec stuff and assert fail, set new contract with multicodec stuff, encode, decode and assert returns correct hash")
} }
func TestManuelCidDecode(t *testing.T) { func TestManualCidDecode(t *testing.T) {
// call cid encode method with hash. expect byte slice returned, compare according to spec // call cid encode method with hash. expect byte slice returned, compare according to spec
bb := []byte{} bb := []byte{}
@ -134,11 +134,27 @@ func TestManuelCidDecode(t *testing.T) {
fails bool fails bool
}{ }{
{ {
name: "w00t", name: "values correct, should not fail",
headerBytes: []byte{0xe4, 0x01, 0x99, 0x1b, 0x20}, headerBytes: []byte{0xe4, 0x01, 0x99, 0x1b, 0x20},
fails: false, fails: false,
}, },
{
name: "cid version wrong, should fail",
headerBytes: []byte{0xe4, 0x01, 0x99, 0x1b, 0x20},
fails: false,
},
{
name: "hash length wrong, should fail",
headerBytes: []byte{0xe4, 0x01, 0x99, 0x1b, 0x1F},
fails: false,
},
{
name: "values correct for ipfs, should fail",
headerBytes: []byte{0xe3, 0x01, 0x99, 0x1b, 0x20},
fails: true,
},
} { } {
t.Run(v.name, func(t *testing.T) {
buf := make([]byte, binary.MaxVarintLen64) buf := make([]byte, binary.MaxVarintLen64)
for _, vv := range v.headerBytes { for _, vv := range v.headerBytes {
n := binary.PutUvarint(buf, uint64(vv)) n := binary.PutUvarint(buf, uint64(vv))
@ -150,6 +166,16 @@ func TestManuelCidDecode(t *testing.T) {
str := hex.EncodeToString(bb) str := hex.EncodeToString(bb)
fmt.Println(str) fmt.Println(str)
decodedHash, e := manualDecode(bb) decodedHash, e := manualDecode(bb)
switch v.fails {
case true:
if e == nil {
t.Fatal("the decode should fail")
}
case false:
if e != nil {
t.Fatal("the deccode shouldnt fail")
}
}
if e != nil { if e != nil {
t.Fatal(e) t.Fatal(e)
} }
@ -157,7 +183,7 @@ func TestManuelCidDecode(t *testing.T) {
if !bytes.Equal(decodedHash[:], h[:]) { if !bytes.Equal(decodedHash[:], h[:]) {
t.Fatal("hashes not equal") t.Fatal("hashes not equal")
} }
})
} }
/* from the EIP documentation /* from the EIP documentation