gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/fsimpl/proc/tasks_sys_test.go (about)

     1  // Copyright 2019 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  	"bytes"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"gvisor.dev/gvisor/pkg/abi/linux"
    23  	"gvisor.dev/gvisor/pkg/context"
    24  	"gvisor.dev/gvisor/pkg/sentry/contexttest"
    25  	"gvisor.dev/gvisor/pkg/sentry/inet"
    26  	"gvisor.dev/gvisor/pkg/usermem"
    27  )
    28  
    29  func newIPv6TestStack() *inet.TestStack {
    30  	s := inet.NewTestStack()
    31  	s.SupportsIPv6Flag = true
    32  	return s
    33  }
    34  
    35  func TestIfinet6NoAddresses(t *testing.T) {
    36  	n := &ifinet6{stack: newIPv6TestStack()}
    37  	var buf bytes.Buffer
    38  	n.Generate(contexttest.Context(t), &buf)
    39  	if buf.Len() > 0 {
    40  		t.Errorf("n.Generate() generated = %v, want = %v", buf.Bytes(), []byte{})
    41  	}
    42  }
    43  
    44  func TestIfinet6(t *testing.T) {
    45  	s := newIPv6TestStack()
    46  	s.InterfacesMap[1] = inet.Interface{Name: "eth0"}
    47  	s.InterfaceAddrsMap[1] = []inet.InterfaceAddr{
    48  		{
    49  			Family:    linux.AF_INET6,
    50  			PrefixLen: 128,
    51  			Addr:      []byte("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
    52  		},
    53  	}
    54  	s.InterfacesMap[2] = inet.Interface{Name: "eth1"}
    55  	s.InterfaceAddrsMap[2] = []inet.InterfaceAddr{
    56  		{
    57  			Family:    linux.AF_INET6,
    58  			PrefixLen: 128,
    59  			Addr:      []byte("\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"),
    60  		},
    61  	}
    62  	want := map[string]struct{}{
    63  		"000102030405060708090a0b0c0d0e0f 01 80 00 00     eth0\n": {},
    64  		"101112131415161718191a1b1c1d1e1f 02 80 00 00     eth1\n": {},
    65  	}
    66  
    67  	n := &ifinet6{stack: s}
    68  	contents := n.contents()
    69  	if len(contents) != len(want) {
    70  		t.Errorf("Got len(n.contents()) = %d, want = %d", len(contents), len(want))
    71  	}
    72  	got := map[string]struct{}{}
    73  	for _, l := range contents {
    74  		got[l] = struct{}{}
    75  	}
    76  
    77  	if !reflect.DeepEqual(got, want) {
    78  		t.Errorf("Got n.contents() = %v, want = %v", got, want)
    79  	}
    80  }
    81  
    82  // TestIPForwarding tests the implementation of
    83  // /proc/sys/net/ipv4/ip_forwarding
    84  func TestConfigureIPForwarding(t *testing.T) {
    85  	ctx := context.Background()
    86  	s := inet.NewTestStack()
    87  
    88  	var cases = []struct {
    89  		comment string
    90  		initial bool
    91  		str     string
    92  		final   bool
    93  	}{
    94  		{
    95  			comment: `Forwarding is disabled; write 1 and enable forwarding`,
    96  			initial: false,
    97  			str:     "1",
    98  			final:   true,
    99  		},
   100  		{
   101  			comment: `Forwarding is disabled; write 0 and disable forwarding`,
   102  			initial: false,
   103  			str:     "0",
   104  			final:   false,
   105  		},
   106  		{
   107  			comment: `Forwarding is enabled; write 1 and enable forwarding`,
   108  			initial: true,
   109  			str:     "1",
   110  			final:   true,
   111  		},
   112  		{
   113  			comment: `Forwarding is enabled; write 0 and disable forwarding`,
   114  			initial: true,
   115  			str:     "0",
   116  			final:   false,
   117  		},
   118  		{
   119  			comment: `Forwarding is disabled; write 2404 and enable forwarding`,
   120  			initial: false,
   121  			str:     "2404",
   122  			final:   true,
   123  		},
   124  		{
   125  			comment: `Forwarding is enabled; write 2404 and enable forwarding`,
   126  			initial: true,
   127  			str:     "2404",
   128  			final:   true,
   129  		},
   130  	}
   131  	for _, c := range cases {
   132  		t.Run(c.comment, func(t *testing.T) {
   133  			s.IPForwarding = c.initial
   134  
   135  			file := &ipForwarding{stack: s, enabled: c.initial}
   136  
   137  			// Write the values.
   138  			src := usermem.BytesIOSequence([]byte(c.str))
   139  			if n, err := file.Write(ctx, nil, src, 0); n != int64(len(c.str)) || err != nil {
   140  				t.Errorf("file.Write(ctx, nil, %q, 0) = (%d, %v); want (%d, nil)", c.str, n, err, len(c.str))
   141  			}
   142  
   143  			// Read the values from the stack and check them.
   144  			if got, want := s.IPForwarding, c.final; got != want {
   145  				t.Errorf("s.IPForwarding incorrect; got: %v, want: %v", got, want)
   146  			}
   147  		})
   148  	}
   149  }