github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/proc/net_test.go (about)

     1  // Copyright 2018 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 proc
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/inet"
    23  )
    24  
    25  func newIPv6TestStack() *inet.TestStack {
    26  	s := inet.NewTestStack()
    27  	s.SupportsIPv6Flag = true
    28  	return s
    29  }
    30  
    31  func TestIfinet6NoAddresses(t *testing.T) {
    32  	n := &ifinet6{s: newIPv6TestStack()}
    33  	if got := n.contents(); got != nil {
    34  		t.Errorf("Got n.contents() = %v, want = %v", got, nil)
    35  	}
    36  }
    37  
    38  func TestIfinet6(t *testing.T) {
    39  	s := newIPv6TestStack()
    40  	s.InterfacesMap[1] = inet.Interface{Name: "eth0"}
    41  	s.InterfaceAddrsMap[1] = []inet.InterfaceAddr{
    42  		{
    43  			Family:    linux.AF_INET6,
    44  			PrefixLen: 128,
    45  			Addr:      []byte("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
    46  		},
    47  	}
    48  	s.InterfacesMap[2] = inet.Interface{Name: "eth1"}
    49  	s.InterfaceAddrsMap[2] = []inet.InterfaceAddr{
    50  		{
    51  			Family:    linux.AF_INET6,
    52  			PrefixLen: 128,
    53  			Addr:      []byte("\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"),
    54  		},
    55  	}
    56  	want := map[string]struct{}{
    57  		"000102030405060708090a0b0c0d0e0f 01 80 00 00     eth0\n": {},
    58  		"101112131415161718191a1b1c1d1e1f 02 80 00 00     eth1\n": {},
    59  	}
    60  
    61  	n := &ifinet6{s: s}
    62  	contents := n.contents()
    63  	if len(contents) != len(want) {
    64  		t.Errorf("Got len(n.contents()) = %d, want = %d", len(contents), len(want))
    65  	}
    66  	got := map[string]struct{}{}
    67  	for _, l := range contents {
    68  		got[l] = struct{}{}
    69  	}
    70  
    71  	if !reflect.DeepEqual(got, want) {
    72  		t.Errorf("Got n.contents() = %v, want = %v", got, want)
    73  	}
    74  }