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