github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/icmp/message_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package icmp_test
     6  
     7  import (
     8  	"net"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"golang.org/x/net/icmp"
    13  	"golang.org/x/net/internal/iana"
    14  	"golang.org/x/net/ipv4"
    15  	"golang.org/x/net/ipv6"
    16  )
    17  
    18  var marshalAndParseMessageForIPv4Tests = []icmp.Message{
    19  	{
    20  		Type: ipv4.ICMPTypeDestinationUnreachable, Code: 15,
    21  		Body: &icmp.DstUnreach{
    22  			Data: []byte("ERROR-INVOKING-PACKET"),
    23  		},
    24  	},
    25  	{
    26  		Type: ipv4.ICMPTypeTimeExceeded, Code: 1,
    27  		Body: &icmp.TimeExceeded{
    28  			Data: []byte("ERROR-INVOKING-PACKET"),
    29  		},
    30  	},
    31  	{
    32  		Type: ipv4.ICMPTypeParameterProblem, Code: 2,
    33  		Body: &icmp.ParamProb{
    34  			Pointer: 8,
    35  			Data:    []byte("ERROR-INVOKING-PACKET"),
    36  		},
    37  	},
    38  	{
    39  		Type: ipv4.ICMPTypeEcho, Code: 0,
    40  		Body: &icmp.Echo{
    41  			ID: 1, Seq: 2,
    42  			Data: []byte("HELLO-R-U-THERE"),
    43  		},
    44  	},
    45  	{
    46  		Type: ipv4.ICMPTypePhoturis,
    47  		Body: &icmp.DefaultMessageBody{
    48  			Data: []byte{0x80, 0x40, 0x20, 0x10},
    49  		},
    50  	},
    51  }
    52  
    53  func TestMarshalAndParseMessageForIPv4(t *testing.T) {
    54  	for i, tt := range marshalAndParseMessageForIPv4Tests {
    55  		b, err := tt.Marshal(nil)
    56  		if err != nil {
    57  			t.Fatal(err)
    58  		}
    59  		m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
    60  		if err != nil {
    61  			t.Fatal(err)
    62  		}
    63  		if m.Type != tt.Type || m.Code != tt.Code {
    64  			t.Errorf("#%v: got %v; want %v", i, m, &tt)
    65  		}
    66  		if !reflect.DeepEqual(m.Body, tt.Body) {
    67  			t.Errorf("#%v: got %v; want %v", i, m.Body, tt.Body)
    68  		}
    69  	}
    70  }
    71  
    72  var marshalAndParseMessageForIPv6Tests = []icmp.Message{
    73  	{
    74  		Type: ipv6.ICMPTypeDestinationUnreachable, Code: 6,
    75  		Body: &icmp.DstUnreach{
    76  			Data: []byte("ERROR-INVOKING-PACKET"),
    77  		},
    78  	},
    79  	{
    80  		Type: ipv6.ICMPTypePacketTooBig, Code: 0,
    81  		Body: &icmp.PacketTooBig{
    82  			MTU:  1<<16 - 1,
    83  			Data: []byte("ERROR-INVOKING-PACKET"),
    84  		},
    85  	},
    86  	{
    87  		Type: ipv6.ICMPTypeTimeExceeded, Code: 1,
    88  		Body: &icmp.TimeExceeded{
    89  			Data: []byte("ERROR-INVOKING-PACKET"),
    90  		},
    91  	},
    92  	{
    93  		Type: ipv6.ICMPTypeParameterProblem, Code: 2,
    94  		Body: &icmp.ParamProb{
    95  			Pointer: 8,
    96  			Data:    []byte("ERROR-INVOKING-PACKET"),
    97  		},
    98  	},
    99  	{
   100  		Type: ipv6.ICMPTypeEchoRequest, Code: 0,
   101  		Body: &icmp.Echo{
   102  			ID: 1, Seq: 2,
   103  			Data: []byte("HELLO-R-U-THERE"),
   104  		},
   105  	},
   106  	{
   107  		Type: ipv6.ICMPTypeDuplicateAddressConfirmation,
   108  		Body: &icmp.DefaultMessageBody{
   109  			Data: []byte{0x80, 0x40, 0x20, 0x10},
   110  		},
   111  	},
   112  }
   113  
   114  func TestMarshalAndParseMessageForIPv6(t *testing.T) {
   115  	pshicmp := icmp.IPv6PseudoHeader(net.ParseIP("fe80::1"), net.ParseIP("ff02::1"))
   116  	for i, tt := range marshalAndParseMessageForIPv6Tests {
   117  		for _, psh := range [][]byte{pshicmp, nil} {
   118  			b, err := tt.Marshal(psh)
   119  			if err != nil {
   120  				t.Fatal(err)
   121  			}
   122  			m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, b)
   123  			if err != nil {
   124  				t.Fatal(err)
   125  			}
   126  			if m.Type != tt.Type || m.Code != tt.Code {
   127  				t.Errorf("#%v: got %v; want %v", i, m, &tt)
   128  			}
   129  			if !reflect.DeepEqual(m.Body, tt.Body) {
   130  				t.Errorf("#%v: got %v; want %v", i, m.Body, tt.Body)
   131  			}
   132  		}
   133  	}
   134  }