github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/vntp2p/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 if _, err := sender.Write(buf); err != nil { 49 t.Error(err) 50 } 51 }() 52 53 buf := make([]byte, recvN) 54 listener.SetDeadline(time.Now().Add(1 * time.Second)) 55 n, _, err := listener.ReadFrom(buf) 56 if err != nil { 57 if nerr, ok := err.(net.Error); ok && nerr.Timeout() { 58 continue 59 } 60 if !isPacketTooBig(err) { 61 t.Fatalf("unexpected read error: %v", err) 62 } 63 continue 64 } 65 if n != recvN { 66 t.Fatalf("short read: %d, want %d", n, recvN) 67 } 68 for i := range buf { 69 if buf[i] != byte(i) { 70 t.Fatalf("error in pattern") 71 break 72 } 73 } 74 } 75 }