gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/p2p/discover/udp_test.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package discover 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "encoding/binary" 23 "encoding/hex" 24 "errors" 25 "fmt" 26 "io" 27 "math/rand" 28 "net" 29 "path/filepath" 30 "reflect" 31 "runtime" 32 "sync" 33 "testing" 34 "time" 35 36 "github.com/davecgh/go-spew/spew" 37 "gitlab.com/aquachain/aquachain/common" 38 "gitlab.com/aquachain/aquachain/crypto" 39 "gitlab.com/aquachain/aquachain/rlp" 40 ) 41 42 func init() { 43 spew.Config.DisableMethods = true 44 } 45 46 // shared test variables 47 var ( 48 futureExp = uint64(time.Now().Add(10 * time.Hour).Unix()) 49 testTarget = NodeID{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1} 50 testRemote = rpcEndpoint{IP: net.ParseIP("1.1.1.1").To4(), UDP: 1, TCP: 2} 51 testLocalAnnounced = rpcEndpoint{IP: net.ParseIP("2.2.2.2").To4(), UDP: 3, TCP: 4} 52 testLocal = rpcEndpoint{IP: net.ParseIP("3.3.3.3").To4(), UDP: 5, TCP: 6} 53 54 netcompat = false 55 pingPacket = aquapingPacket 56 pongPacket = aquapongPacket 57 findnodePacket = aquafindnodePacket 58 neighborsPacket = aquaneighborsPacket 59 ) 60 61 type udpTest struct { 62 t *testing.T 63 pipe *dgramPipe 64 table *Table 65 udp *udp 66 sent [][]byte 67 localkey, remotekey *ecdsa.PrivateKey 68 remoteaddr *net.UDPAddr 69 } 70 71 func newUDPTest(t *testing.T) *udpTest { 72 test := &udpTest{ 73 t: t, 74 pipe: newpipe(), 75 localkey: newkey(), 76 remotekey: newkey(), 77 remoteaddr: &net.UDPAddr{IP: net.IP{10, 0, 1, 99}, Port: 30303}, 78 } 79 test.table, test.udp, _ = newUDP(test.pipe, Config{PrivateKey: test.localkey, ChainId: rand.Uint64()}) 80 // Wait for initial refresh so the table doesn't send unexpected findnode. 81 <-test.table.initDone 82 return test 83 } 84 85 // handles a packet as if it had been sent to the transport. 86 func (test *udpTest) packetIn(wantError error, ptype byte, data packet) error { 87 enc, _, err := encodePacket(netcompat, test.remotekey, ptype, data) 88 if err != nil { 89 return test.errorf("packet (%d) encode error: %v", ptype, err) 90 } 91 test.sent = append(test.sent, enc) 92 if err = test.udp.handlePacket(test.remoteaddr, enc); err != wantError { 93 return test.errorf("error mismatch: got %q, want %q", err, wantError) 94 } 95 return nil 96 } 97 98 // waits for a packet to be sent by the transport. 99 // validate should have type func(*udpTest, X) error, where X is a packet type. 100 func (test *udpTest) waitPacketOut(validate interface{}) ([]byte, error) { 101 dgram := test.pipe.waitPacketOut() 102 p, _, hash, err := decodePacket(netcompat, dgram) 103 if err != nil { 104 return hash, test.errorf("sent packet decode error: %v", err) 105 } 106 fn := reflect.ValueOf(validate) 107 exptype := fn.Type().In(0) 108 if reflect.TypeOf(p) != exptype { 109 return hash, test.errorf("sent packet type mismatch, got: %v, want: %v", reflect.TypeOf(p), exptype) 110 } 111 fn.Call([]reflect.Value{reflect.ValueOf(p)}) 112 return hash, nil 113 } 114 115 func (test *udpTest) errorf(format string, args ...interface{}) error { 116 _, file, line, ok := runtime.Caller(2) // errorf + waitPacketOut 117 if ok { 118 file = filepath.Base(file) 119 } else { 120 file = "???" 121 line = 1 122 } 123 err := fmt.Errorf(format, args...) 124 fmt.Printf("\t%s:%d: %v\n", file, line, err) 125 test.t.Fail() 126 return err 127 } 128 129 func TestUDP_packetErrors(t *testing.T) { 130 test := newUDPTest(t) 131 defer test.table.Close() 132 133 test.packetIn(errExpired, pingPacket, &ping{From: testRemote, To: testLocalAnnounced, Version: Version}) 134 test.packetIn(errUnsolicitedReply, pongPacket, &pong{ReplyTok: []byte{}, Expiration: futureExp}) 135 test.packetIn(errUnknownNode, findnodePacket, &findnode{Expiration: futureExp}) 136 test.packetIn(errUnsolicitedReply, neighborsPacket, &neighbors{Expiration: futureExp}) 137 } 138 139 func TestUDP_pingTimeout(t *testing.T) { 140 t.Parallel() 141 test := newUDPTest(t) 142 defer test.table.Close() 143 144 toaddr := &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 2222} 145 toid := NodeID{1, 2, 3, 4} 146 if err := test.udp.ping(toid, toaddr); err != errTimeout { 147 t.Error("expected timeout error, got", err) 148 } 149 } 150 151 func TestUDP_responseTimeouts(t *testing.T) { 152 t.Parallel() 153 test := newUDPTest(t) 154 defer test.table.Close() 155 156 rand.Seed(time.Now().UnixNano()) 157 randomDuration := func(max time.Duration) time.Duration { 158 return time.Duration(rand.Int63n(int64(max))) 159 } 160 161 var ( 162 nReqs = 200 163 nTimeouts = 0 // number of requests with ptype > 128 164 nilErr = make(chan error, nReqs) // for requests that get a reply 165 timeoutErr = make(chan error, nReqs) // for requests that time out 166 ) 167 for i := 0; i < nReqs; i++ { 168 // Create a matcher for a random request in udp.loop. Requests 169 // with ptype <= 128 will not get a reply and should time out. 170 // For all other requests, a reply is scheduled to arrive 171 // within the timeout window. 172 p := &pending{ 173 ptype: byte(rand.Intn(255)), 174 callback: func(interface{}) bool { return true }, 175 } 176 binary.BigEndian.PutUint64(p.from[:], uint64(i)) 177 if p.ptype <= 128 { 178 p.errc = timeoutErr 179 test.udp.addpending <- p 180 nTimeouts++ 181 } else { 182 p.errc = nilErr 183 test.udp.addpending <- p 184 time.AfterFunc(randomDuration(60*time.Millisecond), func() { 185 if !test.udp.handleReply(p.from, p.ptype, nil) { 186 t.Logf("not matched: %v", p) 187 } 188 }) 189 } 190 time.Sleep(randomDuration(30 * time.Millisecond)) 191 } 192 193 // Check that all timeouts were delivered and that the rest got nil errors. 194 // The replies must be delivered. 195 var ( 196 recvDeadline = time.After(20 * time.Second) 197 nTimeoutsRecv, nNil = 0, 0 198 ) 199 for i := 0; i < nReqs; i++ { 200 select { 201 case err := <-timeoutErr: 202 if err != errTimeout { 203 t.Fatalf("got non-timeout error on timeoutErr %d: %v", i, err) 204 } 205 nTimeoutsRecv++ 206 case err := <-nilErr: 207 if err != nil { 208 t.Fatalf("got non-nil error on nilErr %d: %v", i, err) 209 } 210 nNil++ 211 case <-recvDeadline: 212 t.Fatalf("exceeded recv deadline") 213 } 214 } 215 if nTimeoutsRecv != nTimeouts { 216 t.Errorf("wrong number of timeout errors received: got %d, want %d", nTimeoutsRecv, nTimeouts) 217 } 218 if nNil != nReqs-nTimeouts { 219 t.Errorf("wrong number of successful replies: got %d, want %d", nNil, nReqs-nTimeouts) 220 } 221 } 222 223 func TestUDP_findnodeTimeout(t *testing.T) { 224 t.Parallel() 225 test := newUDPTest(t) 226 defer test.table.Close() 227 228 toaddr := &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 2222} 229 toid := NodeID{1, 2, 3, 4} 230 target := NodeID{4, 5, 6, 7} 231 result, err := test.udp.findnode(toid, toaddr, target) 232 if err != errTimeout { 233 t.Error("expected timeout error, got", err) 234 } 235 if len(result) > 0 { 236 t.Error("expected empty result, got", result) 237 } 238 } 239 240 func TestUDP_findnode(t *testing.T) { 241 test := newUDPTest(t) 242 defer test.table.Close() 243 244 // put a few nodes into the table. their exact 245 // distribution shouldn't matter much, although we need to 246 // take care not to overflow any bucket. 247 targetHash := crypto.Keccak256Hash(testTarget[:]) 248 nodes := &nodesByDistance{target: targetHash} 249 for i := 0; i < bucketSize; i++ { 250 nodes.push(nodeAtDistance(test.table.self.sha, i+2), bucketSize) 251 } 252 test.table.stuff(nodes.entries) 253 254 // ensure there's a bond with the test node, 255 // findnode won't be accepted otherwise. 256 test.table.db.updateBondTime(PubkeyID(&test.remotekey.PublicKey), time.Now()) 257 258 // check that closest neighbors are returned. 259 test.packetIn(nil, findnodePacket, &findnode{Target: testTarget, Expiration: futureExp}) 260 expected := test.table.closest(targetHash, bucketSize) 261 262 waitNeighbors := func(want []*Node) { 263 test.waitPacketOut(func(p *neighbors) { 264 if len(p.Nodes) != len(want) { 265 t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize) 266 } 267 for i := range p.Nodes { 268 if p.Nodes[i].ID != want[i].ID { 269 t.Errorf("result mismatch at %d:\n got: %v\n want: %v", i, p.Nodes[i], expected.entries[i]) 270 } 271 } 272 }) 273 } 274 waitNeighbors(expected.entries[:maxNeighbors]) 275 waitNeighbors(expected.entries[maxNeighbors:]) 276 } 277 278 func TestUDP_findnodeMultiReply(t *testing.T) { 279 test := newUDPTest(t) 280 defer test.table.Close() 281 282 // queue a pending findnode request 283 resultc, errc := make(chan []*Node), make(chan error) 284 go func() { 285 rid := PubkeyID(&test.remotekey.PublicKey) 286 ns, err := test.udp.findnode(rid, test.remoteaddr, testTarget) 287 if err != nil && len(ns) == 0 { 288 errc <- err 289 } else { 290 resultc <- ns 291 } 292 }() 293 294 // wait for the findnode to be sent. 295 // after it is sent, the transport is waiting for a reply 296 test.waitPacketOut(func(p *findnode) { 297 if p.Target != testTarget { 298 t.Errorf("wrong target: got %v, want %v", p.Target, testTarget) 299 } 300 }) 301 302 // send the reply as two packets. 303 list := []*Node{ 304 MustParseNode("enode://ba85011c70bcc5c04d8607d3a0ed29aa6179c092cbdda10d5d32684fb33ed01bd94f588ca8f91ac48318087dcb02eaf36773a7a453f0eedd6742af668097b29c@10.0.1.16:20303?discport=21304"), 305 MustParseNode("enode://81fa361d25f157cd421c60dcc28d8dac5ef6a89476633339c5df30287474520caca09627da18543d9079b5b288698b542d56167aa5c09111e55acdbbdf2ef799@10.0.1.16:20303"), 306 MustParseNode("enode://9bffefd833d53fac8e652415f4973bee289e8b1a5c6c4cbe70abf817ce8a64cee11b823b66a987f51aaa9fba0d6a91b3e6bf0d5a5d1042de8e9eeea057b217f8@10.0.1.36:20301?discport=17"), 307 MustParseNode("enode://1b5b4aa662d7cb44a7221bfba67302590b643028197a7d5214790f3bac7aaa4a3241be9e83c09cf1f6c69d007c634faae3dc1b1221793e8446c0b3a09de65960@10.0.1.16:20303"), 308 } 309 rpclist := make([]rpcNode, len(list)) 310 for i := range list { 311 rpclist[i] = nodeToRPC(list[i]) 312 } 313 test.packetIn(nil, neighborsPacket, &neighbors{Expiration: futureExp, Nodes: rpclist[:2]}) 314 test.packetIn(nil, neighborsPacket, &neighbors{Expiration: futureExp, Nodes: rpclist[2:]}) 315 316 // check that the sent neighbors are all returned by findnode 317 select { 318 case result := <-resultc: 319 want := append(list[:2], list[3:]...) 320 if !reflect.DeepEqual(result, want) { 321 t.Errorf("neighbors mismatch:\n got: %v\n want: %v", result, want) 322 } 323 case err := <-errc: 324 t.Errorf("findnode error: %v", err) 325 case <-time.After(5 * time.Second): 326 t.Error("findnode did not return within 5 seconds") 327 } 328 } 329 330 func TestUDP_successfulPing(t *testing.T) { 331 t.Skip() 332 test := newUDPTest(t) 333 added := make(chan *Node, 1) 334 test.table.nodeAddedHook = func(n *Node) { added <- n } 335 defer test.table.Close() 336 337 // The remote side sends a ping packet to initiate the exchange. 338 go test.packetIn(nil, pingPacket, &ping{From: testRemote, To: testLocalAnnounced, Version: Version, Expiration: futureExp}) 339 340 // the ping is replied to. 341 test.waitPacketOut(func(p *pong) { 342 pinghash := test.sent[0][:macSize] 343 if !bytes.Equal(p.ReplyTok, pinghash) { 344 t.Errorf("got pong.ReplyTok %x, want %x", p.ReplyTok, pinghash) 345 } 346 wantTo := rpcEndpoint{ 347 // The mirrored UDP address is the UDP packet sender 348 IP: test.remoteaddr.IP, UDP: uint16(test.remoteaddr.Port), 349 // The mirrored TCP port is the one from the ping packet 350 TCP: testRemote.TCP, 351 } 352 if !reflect.DeepEqual(p.To, wantTo) { 353 t.Errorf("got pong.To %v, want %v", p.To, wantTo) 354 } 355 }) 356 357 // remote is unknown, the table pings back. 358 hash, _ := test.waitPacketOut(func(p *ping) error { 359 if !reflect.DeepEqual(p.From, test.udp.ourEndpoint) { 360 t.Errorf("got ping.From %v, want %v", p.From, test.udp.ourEndpoint) 361 } 362 wantTo := rpcEndpoint{ 363 // The mirrored UDP address is the UDP packet sender. 364 IP: test.remoteaddr.IP, UDP: uint16(test.remoteaddr.Port), 365 TCP: 0, 366 } 367 if !reflect.DeepEqual(p.To, wantTo) { 368 t.Errorf("got ping.To %v, want %v", p.To, wantTo) 369 } 370 return nil 371 }) 372 test.packetIn(nil, pongPacket, &pong{ReplyTok: hash, Expiration: futureExp}) 373 374 // the node should be added to the table shortly after getting the 375 // pong packet. 376 select { 377 case n := <-added: 378 rid := PubkeyID(&test.remotekey.PublicKey) 379 if n.ID != rid { 380 t.Errorf("node has wrong ID: got %v, want %v", n.ID, rid) 381 } 382 if !n.IP.Equal(test.remoteaddr.IP) { 383 t.Errorf("node has wrong IP: got %v, want: %v", n.IP, test.remoteaddr.IP) 384 } 385 if int(n.UDP) != test.remoteaddr.Port { 386 t.Errorf("node has wrong UDP port: got %v, want: %v", n.UDP, test.remoteaddr.Port) 387 } 388 if n.TCP != testRemote.TCP { 389 t.Errorf("node has wrong TCP port: got %v, want: %v", n.TCP, testRemote.TCP) 390 } 391 case <-time.After(2 * time.Second): 392 t.Errorf("node was not added within 2 seconds") 393 } 394 } 395 396 var testPackets = []struct { 397 input string 398 wantPacket interface{} 399 }{ 400 { 401 input: "71dbda3a79554728d4f94411e42ee1f8b0d561c10e1e5f5893367948c6a7d70bb87b235fa28a77070271b6c164a2dce8c7e13a5739b53b5e96f2e5acb0e458a02902f5965d55ecbeb2ebb6cabb8b2b232896a36b737666c55265ad0a68412f250001ea04cb847f000001820cfa8215a8d790000000000000000000000000000000018208ae820d058443b9a355", 402 wantPacket: &ping{ 403 Version: 400, 404 From: rpcEndpoint{net.ParseIP("127.0.0.1").To4(), 3322, 5544}, 405 To: rpcEndpoint{net.ParseIP("::1"), 2222, 3333}, 406 Expiration: 1136239445, 407 Rest: []rlp.RawValue{}, 408 }, 409 }, 410 { 411 input: "e9614ccfd9fc3e74360018522d30e1419a143407ffcce748de3e22116b7e8dc92ff74788c0b6663aaa3d67d641936511c8f8d6ad8698b820a7cf9e1be7155e9a241f556658c55428ec0563514365799a4be2be5a685a80971ddcfa80cb422cdd0101ec04cb847f000001820cfa8215a8d790000000000000000000000000000000018208ae820d058443b9a3550102", 412 wantPacket: &ping{ 413 Version: 400, 414 From: rpcEndpoint{net.ParseIP("127.0.0.1").To4(), 3322, 5544}, 415 To: rpcEndpoint{net.ParseIP("::1"), 2222, 3333}, 416 Expiration: 1136239445, 417 Rest: []rlp.RawValue{{0x01}, {0x02}}, 418 }, 419 }, 420 { 421 input: "577be4349c4dd26768081f58de4c6f375a7a22f3f7adda654d1428637412c3d7fe917cadc56d4e5e7ffae1dbe3efffb9849feb71b262de37977e7c7a44e677295680e9e38ab26bee2fcbae207fba3ff3d74069a50b902a82c9903ed37cc993c50001f83e82022bd79020010db83c4d001500000000abcdef12820cfa8215a8d79020010db885a308d313198a2e037073488208ae82823a8443b9a355c5010203040531b9019afde696e582a78fa8d95ea13ce3297d4afb8ba6433e4154caa5ac6431af1b80ba76023fa4090c408f6b4bc3701562c031041d4702971d102c9ab7fa5eed4cd6bab8f7af956f7d565ee1917084a95398b6a21eac920fe3dd1345ec0a7ef39367ee69ddf092cbfe5b93e5e568ebc491983c09c76d922dc3", 422 wantPacket: &ping{ 423 Version: 555, 424 From: rpcEndpoint{net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 3322, 5544}, 425 To: rpcEndpoint{net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:7348"), 2222, 13338}, 426 Expiration: 1136239445, 427 Rest: []rlp.RawValue{{0xC5, 0x01, 0x02, 0x03, 0x04, 0x05}}, 428 }, 429 }, 430 { 431 input: "09b2428d83348d27cdf7064ad9024f526cebc19e4958f0fdad87c15eb598dd61d08423e0bf66b2069869e1724125f820d851c136684082774f870e614d95a2855d000f05d1648b2d5945470bc187c2d2216fbe870f43ed0909009882e176a46b0102f846d79020010db885a308d313198a2e037073488208ae82823aa0fbc914b16819237dcd8801d7e53f69e9719adecb3cc0e790c57e91ca4461c9548443b9a355c6010203c2040506a0c969a58f6f9095004c0177a6b47f451530cab38966a25cca5cb58f055542124e", 432 wantPacket: &pong{ 433 To: rpcEndpoint{net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:7348"), 2222, 13338}, 434 ReplyTok: common.Hex2Bytes("fbc914b16819237dcd8801d7e53f69e9719adecb3cc0e790c57e91ca4461c954"), 435 Expiration: 1136239445, 436 Rest: []rlp.RawValue{{0xC6, 0x01, 0x02, 0x03, 0xC2, 0x04, 0x05}, {0x06}}, 437 }, 438 }, 439 { 440 input: "c7c44041b9f7c7e41934417ebac9a8e1a4c6298f74553f2fcfdcae6ed6fe53163eb3d2b52e39fe91831b8a927bf4fc222c3902202027e5e9eb812195f95d20061ef5cd31d502e47ecb61183f74a504fe04c51e73df81f25c4d506b26db4517490103f84eb840ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f8443b9a35582999983999999280dc62cc8255c73471e0a61da0c89acdc0e035e260add7fc0c04ad9ebf3919644c91cb247affc82b69bd2ca235c71eab8e49737c937a2c396", 441 wantPacket: &findnode{ 442 Target: MustHexID("ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f"), 443 Expiration: 1136239445, 444 Rest: []rlp.RawValue{{0x82, 0x99, 0x99}, {0x83, 0x99, 0x99, 0x99}}, 445 }, 446 }, 447 { 448 input: "c679fc8fe0b8b12f06577f2e802d34f6fa257e6137a995f6f4cbfc9ee50ed3710faf6e66f932c4c8d81d64343f429651328758b47d3dbc02c4042f0fff6946a50f4a49037a72bb550f3a7872363a83e1b9ee6469856c24eb4ef80b7535bcf99c0004f9015bf90150f84d846321163782115c82115db8403155e1427f85f10a5c9a7755877748041af1bcd8d474ec065eb33df57a97babf54bfd2103575fa829115d224c523596b401065a97f74010610fce76382c0bf32f84984010203040101b840312c55512422cf9b8a4097e9a6ad79402e87a15ae909a4bfefa22398f03d20951933beea1e4dfa6f968212385e829f04c2d314fc2d4e255e0d3bc08792b069dbf8599020010db83c4d001500000000abcdef12820d05820d05b84038643200b172dcfef857492156971f0e6aa2c538d8b74010f8e140811d53b98c765dd2d96126051913f44582e8c199ad7c6d6819e9a56483f637feaac9448aacf8599020010db885a308d313198a2e037073488203e78203e8b8408dcab8618c3253b558d459da53bd8fa68935a719aff8b811197101a4b2b47dd2d47295286fc00cc081bb542d760717d1bdd6bec2c37cd72eca367d6dd3b9df738443b9a355010203b525a138aa34383fec3d2719a0", 449 wantPacket: &neighbors{ 450 Nodes: []rpcNode{ 451 { 452 ID: MustHexID("3155e1427f85f10a5c9a7755877748041af1bcd8d474ec065eb33df57a97babf54bfd2103575fa829115d224c523596b401065a97f74010610fce76382c0bf32"), 453 IP: net.ParseIP("99.33.22.55").To4(), 454 UDP: 4444, 455 TCP: 4445, 456 }, 457 { 458 ID: MustHexID("312c55512422cf9b8a4097e9a6ad79402e87a15ae909a4bfefa22398f03d20951933beea1e4dfa6f968212385e829f04c2d314fc2d4e255e0d3bc08792b069db"), 459 IP: net.ParseIP("1.2.3.4").To4(), 460 UDP: 1, 461 TCP: 1, 462 }, 463 { 464 ID: MustHexID("38643200b172dcfef857492156971f0e6aa2c538d8b74010f8e140811d53b98c765dd2d96126051913f44582e8c199ad7c6d6819e9a56483f637feaac9448aac"), 465 IP: net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 466 UDP: 3333, 467 TCP: 3333, 468 }, 469 { 470 ID: MustHexID("8dcab8618c3253b558d459da53bd8fa68935a719aff8b811197101a4b2b47dd2d47295286fc00cc081bb542d760717d1bdd6bec2c37cd72eca367d6dd3b9df73"), 471 IP: net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:7348"), 472 UDP: 999, 473 TCP: 1000, 474 }, 475 }, 476 Expiration: 1136239445, 477 Rest: []rlp.RawValue{{0x01}, {0x02}, {0x03}}, 478 }, 479 }, 480 } 481 482 func TestForwardCompatibility(t *testing.T) { 483 t.Skip() 484 testkey, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 485 wantNodeID := PubkeyID(&testkey.PublicKey) 486 487 for _, test := range testPackets { 488 input, err := hex.DecodeString(test.input) 489 if err != nil { 490 t.Fatalf("invalid hex: %s", test.input) 491 } 492 packet, nodeid, _, err := decodePacket(netcompat, input) 493 if err != nil { 494 t.Errorf("did not accept packet %s\n%v", test.input, err) 495 continue 496 } 497 if !reflect.DeepEqual(packet, test.wantPacket) { 498 t.Errorf("got %s\nwant %s", spew.Sdump(packet), spew.Sdump(test.wantPacket)) 499 } 500 if nodeid != wantNodeID { 501 t.Errorf("got id %v\nwant id %v", nodeid, wantNodeID) 502 } 503 } 504 } 505 506 // dgramPipe is a fake UDP socket. It queues all sent datagrams. 507 type dgramPipe struct { 508 mu *sync.Mutex 509 cond *sync.Cond 510 closing chan struct{} 511 closed bool 512 queue [][]byte 513 } 514 515 func newpipe() *dgramPipe { 516 mu := new(sync.Mutex) 517 return &dgramPipe{ 518 closing: make(chan struct{}), 519 cond: &sync.Cond{L: mu}, 520 mu: mu, 521 } 522 } 523 524 // WriteToUDP queues a datagram. 525 func (c *dgramPipe) WriteToUDP(b []byte, to *net.UDPAddr) (n int, err error) { 526 msg := make([]byte, len(b)) 527 copy(msg, b) 528 c.mu.Lock() 529 defer c.mu.Unlock() 530 if c.closed { 531 return 0, errors.New("closed") 532 } 533 c.queue = append(c.queue, msg) 534 c.cond.Signal() 535 return len(b), nil 536 } 537 538 // ReadFromUDP just hangs until the pipe is closed. 539 func (c *dgramPipe) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) { 540 <-c.closing 541 return 0, nil, io.EOF 542 } 543 544 func (c *dgramPipe) Close() error { 545 c.mu.Lock() 546 defer c.mu.Unlock() 547 if !c.closed { 548 close(c.closing) 549 c.closed = true 550 } 551 return nil 552 } 553 554 func (c *dgramPipe) LocalAddr() net.Addr { 555 return &net.UDPAddr{IP: testLocal.IP, Port: int(testLocal.UDP)} 556 } 557 558 func (c *dgramPipe) waitPacketOut() []byte { 559 c.mu.Lock() 560 defer c.mu.Unlock() 561 for len(c.queue) == 0 { 562 c.cond.Wait() 563 } 564 p := c.queue[0] 565 copy(c.queue, c.queue[1:]) 566 c.queue = c.queue[:len(c.queue)-1] 567 return p 568 }