github.com/simplechain-org/go-simplechain@v1.0.6/p2p/discv5/node_test.go (about) 1 // Copyright 2015 The go-simplechain Authors 2 // This file is part of the go-simplechain library. 3 // 4 // The go-simplechain 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-simplechain 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-simplechain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package discv5 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/simplechain-org/go-simplechain/common" 31 "github.com/simplechain-org/go-simplechain/crypto" 32 ) 33 34 func ExampleNewNode() { 35 id := MustHexID("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439") 36 37 // Complete nodes contain UDP and TCP endpoints: 38 n1 := NewNode(id, net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 52150, 30303) 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 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150", 83 wantResult: NewNode( 84 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 85 net.IP{0x7f, 0x0, 0x0, 0x1}, 86 52150, 87 52150, 88 ), 89 }, 90 { 91 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[::]:52150", 92 wantResult: NewNode( 93 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 94 net.ParseIP("::"), 95 52150, 96 52150, 97 ), 98 }, 99 { 100 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[2001:db8:3c4d:15::abcd:ef12]:52150", 101 wantResult: NewNode( 102 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 103 net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 104 52150, 105 52150, 106 ), 107 }, 108 { 109 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150?discport=22334", 110 wantResult: NewNode( 111 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 112 net.IP{0x7f, 0x0, 0x0, 0x1}, 113 22334, 114 52150, 115 ), 116 }, 117 // Incomplete nodes with no address. 118 { 119 rawurl: "1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439", 120 wantResult: NewNode( 121 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 122 nil, 0, 0, 123 ), 124 }, 125 { 126 rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439", 127 wantResult: NewNode( 128 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 129 nil, 0, 0, 130 ), 131 }, 132 // Invalid URLs 133 { 134 rawurl: "01010101", 135 wantError: `invalid node ID (wrong length, want 128 hex chars)`, 136 }, 137 { 138 rawurl: "enode://01010101", 139 wantError: `invalid node ID (wrong length, want 128 hex chars)`, 140 }, 141 { 142 // This test checks that errors from url.Parse are handled. 143 rawurl: "://foo", 144 wantError: `missing protocol scheme`, 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 }