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,30 +134,56 @@ 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,
},
} { } {
buf := make([]byte, binary.MaxVarintLen64) t.Run(v.name, func(t *testing.T) {
for _, vv := range v.headerBytes { buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutUvarint(buf, uint64(vv)) for _, vv := range v.headerBytes {
bb = append(bb, buf[:n]...) n := binary.PutUvarint(buf, uint64(vv))
} bb = append(bb, buf[:n]...)
}
h := common.HexToHash("29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f") h := common.HexToHash("29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")
bb = append(bb, h[:]...) bb = append(bb, h[:]...)
str := hex.EncodeToString(bb) str := hex.EncodeToString(bb)
fmt.Println(str) fmt.Println(str)
decodedHash, e := manualDecode(bb) decodedHash, e := manualDecode(bb)
if e != nil { switch v.fails {
t.Fatal(e) case true:
} if e == nil {
t.Fatal("the decode should fail")
if !bytes.Equal(decodedHash[:], h[:]) { }
t.Fatal("hashes not equal") case false:
} if e != nil {
t.Fatal("the deccode shouldnt fail")
}
}
if e != nil {
t.Fatal(e)
}
if !bytes.Equal(decodedHash[:], h[:]) {
t.Fatal("hashes not equal")
}
})
} }
/* from the EIP documentation /* from the EIP documentation