github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/p2p/discover/node_test.go (about) 1 // Copyright 2015 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 discover 18 19 import ( 20 "math/big" 21 "math/rand" 22 "net" 23 "reflect" 24 "testing" 25 "testing/quick" 26 "time" 27 28 "github.com/ethereum/go-ethereum/common" 29 "github.com/ethereum/go-ethereum/crypto" 30 ) 31 32 var parseNodeTests = []struct { 33 rawurl string 34 wantError string 35 wantResult *Node 36 }{ 37 { 38 rawurl: "http://foobar", 39 wantError: `invalid URL scheme, want "enode"`, 40 }, 41 { 42 rawurl: "enode://foobar", 43 wantError: `does not contain node ID`, 44 }, 45 { 46 rawurl: "enode://01010101@123.124.125.126:3", 47 wantError: `invalid node ID (wrong length, need 64 hex bytes)`, 48 }, 49 { 50 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@hostname:3", 51 wantError: `invalid IP address`, 52 }, 53 { 54 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo", 55 wantError: `invalid port`, 56 }, 57 { 58 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:3?discport=foo", 59 wantError: `invalid discport in query`, 60 }, 61 { 62 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150", 63 wantResult: newNode( 64 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 65 net.IP{0x7f, 0x0, 0x0, 0x1}, 66 52150, 67 52150, 68 ), 69 }, 70 { 71 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[::]:52150", 72 wantResult: newNode( 73 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 74 net.ParseIP("::"), 75 52150, 76 52150, 77 ), 78 }, 79 { 80 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[2001:db8:3c4d:15::abcd:ef12]:52150", 81 wantResult: newNode( 82 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 83 net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 84 52150, 85 52150, 86 ), 87 }, 88 { 89 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150?discport=22334", 90 wantResult: newNode( 91 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 92 net.IP{0x7f, 0x0, 0x0, 0x1}, 93 22334, 94 52150, 95 ), 96 }, 97 } 98 99 func TestParseNode(t *testing.T) { 100 for i, test := range parseNodeTests { 101 n, err := ParseNode(test.rawurl) 102 if test.wantError != "" { 103 if err == nil { 104 t.Errorf("test %d: got nil error, expected %#q", i, test.wantError) 105 continue 106 } else if err.Error() != test.wantError { 107 t.Errorf("test %d: got error %#q, expected %#q", i, err.Error(), test.wantError) 108 continue 109 } 110 } else { 111 if err != nil { 112 t.Errorf("test %d: unexpected error: %v", i, err) 113 continue 114 } 115 if !reflect.DeepEqual(n, test.wantResult) { 116 t.Errorf("test %d: result mismatch:\ngot: %#v, want: %#v", i, n, test.wantResult) 117 } 118 } 119 } 120 } 121 122 func TestNodeString(t *testing.T) { 123 for i, test := range parseNodeTests { 124 if test.wantError != "" { 125 continue 126 } 127 str := test.wantResult.String() 128 if str != test.rawurl { 129 t.Errorf("test %d: Node.String() mismatch:\ngot: %s\nwant: %s", i, str, test.rawurl) 130 } 131 } 132 } 133 134 func TestHexID(t *testing.T) { 135 ref := NodeID{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 106, 217, 182, 31, 165, 174, 1, 67, 7, 235, 220, 150, 66, 83, 173, 205, 159, 44, 10, 57, 42, 161, 26, 188} 136 id1 := MustHexID("0x000000000000000000000000000000000000000000000000000000000000000000000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc") 137 id2 := MustHexID("000000000000000000000000000000000000000000000000000000000000000000000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc") 138 139 if id1 != ref { 140 t.Errorf("wrong id1\ngot %v\nwant %v", id1[:], ref[:]) 141 } 142 if id2 != ref { 143 t.Errorf("wrong id2\ngot %v\nwant %v", id2[:], ref[:]) 144 } 145 } 146 147 func TestNodeID_recover(t *testing.T) { 148 prv := newkey() 149 hash := make([]byte, 32) 150 sig, err := crypto.Sign(hash, prv) 151 if err != nil { 152 t.Fatalf("signing error: %v", err) 153 } 154 155 pub := PubkeyID(&prv.PublicKey) 156 recpub, err := recoverNodeID(hash, sig) 157 if err != nil { 158 t.Fatalf("recovery error: %v", err) 159 } 160 if pub != recpub { 161 t.Errorf("recovered wrong pubkey:\ngot: %v\nwant: %v", recpub, pub) 162 } 163 164 ecdsa, err := pub.Pubkey() 165 if err != nil { 166 t.Errorf("Pubkey error: %v", err) 167 } 168 if !reflect.DeepEqual(ecdsa, &prv.PublicKey) { 169 t.Errorf("Pubkey mismatch:\n got: %#v\n want: %#v", ecdsa, &prv.PublicKey) 170 } 171 } 172 173 func TestNodeID_pubkeyBad(t *testing.T) { 174 ecdsa, err := NodeID{}.Pubkey() 175 if err == nil { 176 t.Error("expected error for zero ID") 177 } 178 if ecdsa != nil { 179 t.Error("expected nil result") 180 } 181 } 182 183 func TestNodeID_distcmp(t *testing.T) { 184 distcmpBig := func(target, a, b common.Hash) int { 185 tbig := new(big.Int).SetBytes(target[:]) 186 abig := new(big.Int).SetBytes(a[:]) 187 bbig := new(big.Int).SetBytes(b[:]) 188 return new(big.Int).Xor(tbig, abig).Cmp(new(big.Int).Xor(tbig, bbig)) 189 } 190 if err := quick.CheckEqual(distcmp, distcmpBig, quickcfg()); err != nil { 191 t.Error(err) 192 } 193 } 194 195 // the random tests is likely to miss the case where they're equal. 196 func TestNodeID_distcmpEqual(t *testing.T) { 197 base := common.Hash{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 198 x := common.Hash{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0} 199 if distcmp(base, x, x) != 0 { 200 t.Errorf("distcmp(base, x, x) != 0") 201 } 202 } 203 204 func TestNodeID_logdist(t *testing.T) { 205 logdistBig := func(a, b common.Hash) int { 206 abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:]) 207 return new(big.Int).Xor(abig, bbig).BitLen() 208 } 209 if err := quick.CheckEqual(logdist, logdistBig, quickcfg()); err != nil { 210 t.Error(err) 211 } 212 } 213 214 // the random tests is likely to miss the case where they're equal. 215 func TestNodeID_logdistEqual(t *testing.T) { 216 x := common.Hash{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 217 if logdist(x, x) != 0 { 218 t.Errorf("logdist(x, x) != 0") 219 } 220 } 221 222 func TestNodeID_hashAtDistance(t *testing.T) { 223 // we don't use quick.Check here because its output isn't 224 // very helpful when the test fails. 225 cfg := quickcfg() 226 for i := 0; i < cfg.MaxCount; i++ { 227 a := gen(common.Hash{}, cfg.Rand).(common.Hash) 228 dist := cfg.Rand.Intn(len(common.Hash{}) * 8) 229 result := hashAtDistance(a, dist) 230 actualdist := logdist(result, a) 231 232 if dist != actualdist { 233 t.Log("a: ", a) 234 t.Log("result:", result) 235 t.Fatalf("#%d: distance of result is %d, want %d", i, actualdist, dist) 236 } 237 } 238 } 239 240 func quickcfg() *quick.Config { 241 return &quick.Config{ 242 MaxCount: 5000, 243 Rand: rand.New(rand.NewSource(time.Now().Unix())), 244 } 245 } 246 247 // TODO: The Generate method can be dropped when we require Go >= 1.5 248 // because testing/quick learned to generate arrays in 1.5. 249 250 func (NodeID) Generate(rand *rand.Rand, size int) reflect.Value { 251 var id NodeID 252 m := rand.Intn(len(id)) 253 for i := len(id) - 1; i > m; i-- { 254 id[i] = byte(rand.Uint32()) 255 } 256 return reflect.ValueOf(id) 257 }