gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/testutil/testutil_test.go (about)

     1  // Copyright 2021 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 testutil
    16  
    17  import (
    18  	"testing"
    19  
    20  	"gvisor.dev/gvisor/pkg/tcpip"
    21  )
    22  
    23  // Who tests the testutils?
    24  
    25  func TestMustParse4(t *testing.T) {
    26  	tcs := []struct {
    27  		str         string
    28  		addr        string
    29  		shouldPanic bool
    30  	}{
    31  		{
    32  			str:  "127.0.0.1",
    33  			addr: "\x7f\x00\x00\x01",
    34  		}, {
    35  			str:         "",
    36  			shouldPanic: true,
    37  		}, {
    38  			str:         "fe80::1",
    39  			shouldPanic: true,
    40  		}, {
    41  			// In an ideal world this panics too, but net.IP
    42  			// doesn't distinguish between IPv4 and IPv4-mapped
    43  			// addresses.
    44  			str:  "::ffff:0.0.0.1",
    45  			addr: "\x00\x00\x00\x01",
    46  		},
    47  	}
    48  
    49  	for _, tc := range tcs {
    50  		t.Run(tc.str, func(t *testing.T) {
    51  			if tc.shouldPanic {
    52  				defer func() {
    53  					if r := recover(); r == nil {
    54  						t.Errorf("panic expected, but did not occur")
    55  					}
    56  				}()
    57  			}
    58  			if got := MustParse4(tc.str); got != tcpip.AddrFromSlice([]byte(tc.addr)) {
    59  				t.Errorf("got MustParse4(%s) = %s, want = %s", tc.str, got, tc.addr)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestMustParse6(t *testing.T) {
    66  	tcs := []struct {
    67  		str         string
    68  		addr        string
    69  		shouldPanic bool
    70  	}{
    71  		{
    72  			// In an ideal world this panics too, but net.IP
    73  			// doesn't distinguish between IPv4 and IPv4-mapped
    74  			// addresses.
    75  			str:  "127.0.0.1",
    76  			addr: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\x01",
    77  		}, {
    78  			str:         "",
    79  			shouldPanic: true,
    80  		}, {
    81  			str:  "fe80::1",
    82  			addr: "\xfe\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
    83  		}, {
    84  			str:  "::ffff:0.0.0.1",
    85  			addr: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x01",
    86  		},
    87  	}
    88  
    89  	for _, tc := range tcs {
    90  		t.Run(tc.str, func(t *testing.T) {
    91  			if tc.shouldPanic {
    92  				defer func() {
    93  					if r := recover(); r == nil {
    94  						t.Errorf("panic expected, but did not occur")
    95  					}
    96  				}()
    97  			}
    98  			if got := MustParse6(tc.str); got != tcpip.AddrFromSlice([]byte(tc.addr)) {
    99  				t.Errorf("got MustParse6(%s) = %s, want = %s", tc.str, got, tc.addr)
   100  			}
   101  		})
   102  	}
   103  }