github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/proc/sys_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  	"testing"
    19  
    20  	"github.com/SagerNet/gvisor/pkg/context"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/inet"
    22  	"github.com/SagerNet/gvisor/pkg/usermem"
    23  )
    24  
    25  func TestQuerySendBufferSize(t *testing.T) {
    26  	ctx := context.Background()
    27  	s := inet.NewTestStack()
    28  	s.TCPSendBufSize = inet.TCPBufferSize{100, 200, 300}
    29  	tmi := &tcpMemInode{s: s, dir: tcpWMem}
    30  	tmf := &tcpMemFile{tcpMemInode: tmi}
    31  
    32  	buf := make([]byte, 100)
    33  	dst := usermem.BytesIOSequence(buf)
    34  	n, err := tmf.Read(ctx, nil, dst, 0)
    35  	if err != nil {
    36  		t.Fatalf("Read failed: %v", err)
    37  	}
    38  
    39  	if got, want := string(buf[:n]), "100\t200\t300\n"; got != want {
    40  		t.Fatalf("Bad string: got %v, want %v", got, want)
    41  	}
    42  }
    43  
    44  func TestQueryRecvBufferSize(t *testing.T) {
    45  	ctx := context.Background()
    46  	s := inet.NewTestStack()
    47  	s.TCPRecvBufSize = inet.TCPBufferSize{100, 200, 300}
    48  	tmi := &tcpMemInode{s: s, dir: tcpRMem}
    49  	tmf := &tcpMemFile{tcpMemInode: tmi}
    50  
    51  	buf := make([]byte, 100)
    52  	dst := usermem.BytesIOSequence(buf)
    53  	n, err := tmf.Read(ctx, nil, dst, 0)
    54  	if err != nil {
    55  		t.Fatalf("Read failed: %v", err)
    56  	}
    57  
    58  	if got, want := string(buf[:n]), "100\t200\t300\n"; got != want {
    59  		t.Fatalf("Bad string: got %v, want %v", got, want)
    60  	}
    61  }
    62  
    63  var cases = []struct {
    64  	str     string
    65  	initial inet.TCPBufferSize
    66  	final   inet.TCPBufferSize
    67  }{
    68  	{
    69  		str:     "",
    70  		initial: inet.TCPBufferSize{1, 2, 3},
    71  		final:   inet.TCPBufferSize{1, 2, 3},
    72  	},
    73  	{
    74  		str:     "100\n",
    75  		initial: inet.TCPBufferSize{1, 100, 200},
    76  		final:   inet.TCPBufferSize{100, 100, 200},
    77  	},
    78  	{
    79  		str:     "100 200 300\n",
    80  		initial: inet.TCPBufferSize{1, 2, 3},
    81  		final:   inet.TCPBufferSize{100, 200, 300},
    82  	},
    83  }
    84  
    85  func TestConfigureSendBufferSize(t *testing.T) {
    86  	ctx := context.Background()
    87  	s := inet.NewTestStack()
    88  	for _, c := range cases {
    89  		s.TCPSendBufSize = c.initial
    90  		tmi := &tcpMemInode{s: s, dir: tcpWMem}
    91  		tmf := &tcpMemFile{tcpMemInode: tmi}
    92  
    93  		// Write the values.
    94  		src := usermem.BytesIOSequence([]byte(c.str))
    95  		if n, err := tmf.Write(ctx, nil, src, 0); n != int64(len(c.str)) || err != nil {
    96  			t.Errorf("Write, case = %q: got (%d, %v), wanted (%d, nil)", c.str, n, err, len(c.str))
    97  		}
    98  
    99  		// Read the values from the stack and check them.
   100  		if s.TCPSendBufSize != c.final {
   101  			t.Errorf("TCPSendBufferSize, case = %q: got %v, wanted %v", c.str, s.TCPSendBufSize, c.final)
   102  		}
   103  	}
   104  }
   105  
   106  func TestConfigureRecvBufferSize(t *testing.T) {
   107  	ctx := context.Background()
   108  	s := inet.NewTestStack()
   109  	for _, c := range cases {
   110  		s.TCPRecvBufSize = c.initial
   111  		tmi := &tcpMemInode{s: s, dir: tcpRMem}
   112  		tmf := &tcpMemFile{tcpMemInode: tmi}
   113  
   114  		// Write the values.
   115  		src := usermem.BytesIOSequence([]byte(c.str))
   116  		if n, err := tmf.Write(ctx, nil, src, 0); n != int64(len(c.str)) || err != nil {
   117  			t.Errorf("Write, case = %q: got (%d, %v), wanted (%d, nil)", c.str, n, err, len(c.str))
   118  		}
   119  
   120  		// Read the values from the stack and check them.
   121  		if s.TCPRecvBufSize != c.final {
   122  			t.Errorf("TCPRecvBufferSize, case = %q: got %v, wanted %v", c.str, s.TCPRecvBufSize, c.final)
   123  		}
   124  	}
   125  }
   126  
   127  // TestIPForwarding tests the implementation of
   128  // /proc/sys/net/ipv4/ip_forwarding
   129  func TestIPForwarding(t *testing.T) {
   130  	ctx := context.Background()
   131  	s := inet.NewTestStack()
   132  
   133  	var cases = []struct {
   134  		comment string
   135  		initial bool
   136  		str     string
   137  		final   bool
   138  	}{
   139  		{
   140  			comment: `Forwarding is disabled; write 1 and enable forwarding`,
   141  			initial: false,
   142  			str:     "1",
   143  			final:   true,
   144  		},
   145  		{
   146  			comment: `Forwarding is disabled; write 0 and disable forwarding`,
   147  			initial: false,
   148  			str:     "0",
   149  			final:   false,
   150  		},
   151  		{
   152  			comment: `Forwarding is enabled; write 1 and enable forwarding`,
   153  			initial: true,
   154  			str:     "1",
   155  			final:   true,
   156  		},
   157  		{
   158  			comment: `Forwarding is enabled; write 0 and disable forwarding`,
   159  			initial: true,
   160  			str:     "0",
   161  			final:   false,
   162  		},
   163  		{
   164  			comment: `Forwarding is disabled; write 2404 and enable forwarding`,
   165  			initial: false,
   166  			str:     "2404",
   167  			final:   true,
   168  		},
   169  		{
   170  			comment: `Forwarding is enabled; write 2404 and enable forwarding`,
   171  			initial: true,
   172  			str:     "2404",
   173  			final:   true,
   174  		},
   175  	}
   176  	for _, c := range cases {
   177  		t.Run(c.comment, func(t *testing.T) {
   178  			s.IPForwarding = c.initial
   179  			ipf := &ipForwarding{stack: s}
   180  			file := &ipForwardingFile{
   181  				stack: s,
   182  				ipf:   ipf,
   183  			}
   184  
   185  			// Write the values.
   186  			src := usermem.BytesIOSequence([]byte(c.str))
   187  			if n, err := file.Write(ctx, nil, src, 0); n != int64(len(c.str)) || err != nil {
   188  				t.Errorf("file.Write(ctx, nil, %q, 0) = (%d, %v); want (%d, nil)", c.str, n, err, len(c.str))
   189  			}
   190  
   191  			// Read the values from the stack and check them.
   192  			if got, want := s.IPForwarding, c.final; got != want {
   193  				t.Errorf("s.IPForwarding incorrect; got: %v, want: %v", got, want)
   194  			}
   195  
   196  		})
   197  	}
   198  }