gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/icmpv6_param_problem_test.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package icmpv6_param_problem_test
    16  
    17  import (
    18  	"flag"
    19  	"testing"
    20  	"time"
    21  
    22  	"gvisor.dev/gvisor/pkg/tcpip/header"
    23  	"gvisor.dev/gvisor/test/packetimpact/testbench"
    24  )
    25  
    26  func init() {
    27  	testbench.Initialize(flag.CommandLine)
    28  }
    29  
    30  // TestICMPv6ParamProblemTest sends a packet with a bad next header. The DUT
    31  // should respond with an ICMPv6 Parameter Problem message.
    32  func TestICMPv6ParamProblemTest(t *testing.T) {
    33  	dut := testbench.NewDUT(t)
    34  	conn := dut.Net.NewIPv6Conn(t, testbench.IPv6{}, testbench.IPv6{})
    35  	defer conn.Close(t)
    36  	ipv6 := testbench.IPv6{
    37  		// 254 is reserved and used for experimentation and testing. This should
    38  		// cause an error.
    39  		NextHeader: testbench.Uint8(254),
    40  	}
    41  	icmpv6 := testbench.ICMPv6{
    42  		Type:    testbench.ICMPv6Type(header.ICMPv6EchoRequest),
    43  		Payload: []byte("hello world"),
    44  	}
    45  
    46  	toSend := conn.CreateFrame(t, testbench.Layers{&ipv6}, &icmpv6)
    47  	conn.SendFrame(t, toSend)
    48  
    49  	// Build the expected ICMPv6 payload, which includes an index to the
    50  	// problematic byte and also the problematic packet as described in
    51  	// https://tools.ietf.org/html/rfc4443#page-12 .
    52  	ipv6Sent := toSend[1:]
    53  	expectedPayload, err := ipv6Sent.ToBytes()
    54  	if err != nil {
    55  		t.Fatalf("can't convert %s to bytes: %s", ipv6Sent, err)
    56  	}
    57  
    58  	expectedICMPv6 := testbench.ICMPv6{
    59  		Type:    testbench.ICMPv6Type(header.ICMPv6ParamProblem),
    60  		Payload: expectedPayload,
    61  		// The problematic field is the NextHeader.
    62  		Pointer: testbench.Uint32(header.IPv6NextHeaderOffset),
    63  	}
    64  
    65  	paramProblem := testbench.Layers{
    66  		&testbench.Ether{},
    67  		&testbench.IPv6{},
    68  		&expectedICMPv6,
    69  	}
    70  	timeout := time.Second
    71  	if _, err := conn.ExpectFrame(t, paramProblem, timeout); err != nil {
    72  		t.Errorf("expected %s within %s but got none: %s", paramProblem, timeout, err)
    73  	}
    74  }