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