github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/contracts/ens/cid_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package ens 18 19 import ( 20 "bytes" 21 "encoding/binary" 22 "encoding/hex" 23 "fmt" 24 "testing" 25 26 "github.com/ethereum/go-ethereum/common" 27 ) 28 29 // Tests for the decoding of the example ENS 30 func TestEIPSpecCidDecode(t *testing.T) { 31 const ( 32 eipSpecHash = "e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f" 33 eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f" 34 dagPb = 0x70 35 sha2256 = 0x12 36 ) 37 b, err := hex.DecodeString(eipSpecHash) 38 if err != nil { 39 t.Fatal(err) 40 } 41 hashBytes, err := hex.DecodeString(eipHash) 42 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 storageNs, contentType, hashType, hashLength, decodedHashBytes, err := decodeEIP1577ContentHash(b) 48 49 if err != nil { 50 t.Fatal(err) 51 } 52 if storageNs != nsIpfs { 53 t.Fatal("wrong ns") 54 } 55 if contentType != dagPb { 56 t.Fatal("should be ipfs typecode") 57 } 58 if hashType != sha2256 { 59 t.Fatal("should be sha2-256") 60 } 61 if hashLength != 32 { 62 t.Fatal("should be 32") 63 } 64 if !bytes.Equal(hashBytes, decodedHashBytes) { 65 t.Fatal("should be equal") 66 } 67 68 } 69 func TestManualCidDecode(t *testing.T) { 70 // call cid encode method with hash. expect byte slice returned, compare according to spec 71 72 for _, v := range []struct { 73 name string 74 headerBytes []byte 75 wantErr bool 76 }{ 77 { 78 name: "values correct, should not fail", 79 headerBytes: []byte{0xe4, 0x01, 0xfa, 0x1b, 0x20}, 80 wantErr: false, 81 }, 82 { 83 name: "cid version wrong, should fail", 84 headerBytes: []byte{0xe4, 0x00, 0xfa, 0x1b, 0x20}, 85 wantErr: true, 86 }, 87 { 88 name: "hash length wrong, should fail", 89 headerBytes: []byte{0xe4, 0x01, 0xfa, 0x1b, 0x1f}, 90 wantErr: true, 91 }, 92 { 93 name: "values correct for ipfs, should fail", 94 headerBytes: []byte{0xe3, 0x01, 0x70, 0x12, 0x20}, 95 wantErr: true, 96 }, 97 { 98 name: "loose values for swarm, todo remove, should not fail", 99 headerBytes: []byte{0xe4, 0x01, 0x70, 0x12, 0x20}, 100 wantErr: false, 101 }, 102 { 103 name: "loose values for swarm, todo remove, should not fail", 104 headerBytes: []byte{0xe4, 0x01, 0x99, 0x99, 0x20}, 105 wantErr: false, 106 }, 107 } { 108 t.Run(v.name, func(t *testing.T) { 109 const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f" 110 111 var bb []byte 112 buf := make([]byte, binary.MaxVarintLen64) 113 for _, vv := range v.headerBytes { 114 n := binary.PutUvarint(buf, uint64(vv)) 115 bb = append(bb, buf[:n]...) 116 } 117 118 h := common.HexToHash(eipHash) 119 bb = append(bb, h[:]...) 120 str := hex.EncodeToString(bb) 121 fmt.Println(str) 122 decodedHash, e := extractContentHash(bb) 123 switch v.wantErr { 124 case true: 125 if e == nil { 126 t.Fatal("the decode should fail") 127 } 128 case false: 129 if e != nil { 130 t.Fatalf("the deccode shouldnt fail: %v", e) 131 } 132 if !bytes.Equal(decodedHash[:], h[:]) { 133 t.Fatal("hashes not equal") 134 } 135 } 136 }) 137 } 138 } 139 140 func TestManuelCidEncode(t *testing.T) { 141 // call cid encode method with hash. expect byte slice returned, compare according to spec 142 const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f" 143 cidBytes, err := EncodeSwarmHash(common.HexToHash(eipHash)) 144 if err != nil { 145 t.Fatal(err) 146 } 147 148 // logic in extractContentHash is unit tested thoroughly 149 // hence we just check that the returned hash is equal 150 h, err := extractContentHash(cidBytes) 151 if err != nil { 152 t.Fatal(err) 153 } 154 155 if bytes.Equal(h[:], cidBytes) { 156 t.Fatal("should be equal") 157 } 158 }