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