github.com/EgonCoin/EgonChain@v1.10.16/p2p/netutil/error_test.go (about) 1 // Copyright 2016 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 netutil 18 19 import ( 20 "net" 21 "testing" 22 "time" 23 ) 24 25 // This test checks that isPacketTooBig correctly identifies 26 // errors that result from receiving a UDP packet larger 27 // than the supplied receive buffer. 28 func TestIsPacketTooBig(t *testing.T) { 29 listener, err := net.ListenPacket("udp", "127.0.0.1:0") 30 if err != nil { 31 t.Fatal(err) 32 } 33 defer listener.Close() 34 sender, err := net.Dial("udp", listener.LocalAddr().String()) 35 if err != nil { 36 t.Fatal(err) 37 } 38 defer sender.Close() 39 40 sendN := 1800 41 recvN := 300 42 for i := 0; i < 20; i++ { 43 go func() { 44 buf := make([]byte, sendN) 45 for i := range buf { 46 buf[i] = byte(i) 47 } 48 sender.Write(buf) 49 }() 50 51 buf := make([]byte, recvN) 52 listener.SetDeadline(time.Now().Add(1 * time.Second)) 53 n, _, err := listener.ReadFrom(buf) 54 if err != nil { 55 if nerr, ok := err.(net.Error); ok && nerr.Timeout() { 56 continue 57 } 58 if !isPacketTooBig(err) { 59 t.Fatalf("unexpected read error: %v", err) 60 } 61 continue 62 } 63 if n != recvN { 64 t.Fatalf("short read: %d, want %d", n, recvN) 65 } 66 for i := range buf { 67 if buf[i] != byte(i) { 68 t.Fatalf("error in pattern") 69 break 70 } 71 } 72 } 73 }