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