github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/fd/fd_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 fd
    16  
    17  import (
    18  	"math"
    19  	"os"
    20  	"testing"
    21  
    22  	"golang.org/x/sys/unix"
    23  )
    24  
    25  func TestSetNegOne(t *testing.T) {
    26  	type entry struct {
    27  		name string
    28  		file *FD
    29  		fn   func() error
    30  	}
    31  	var tests []entry
    32  
    33  	fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
    34  	if err != nil {
    35  		t.Fatal("unix.Socket:", err)
    36  	}
    37  	f1 := New(fd)
    38  	tests = append(tests, entry{
    39  		"Release",
    40  		f1,
    41  		func() error {
    42  			return unix.Close(f1.Release())
    43  		},
    44  	})
    45  
    46  	fd, err = unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
    47  	if err != nil {
    48  		t.Fatal("unix.Socket:", err)
    49  	}
    50  	f2 := New(fd)
    51  	tests = append(tests, entry{
    52  		"Close",
    53  		f2,
    54  		f2.Close,
    55  	})
    56  
    57  	for _, test := range tests {
    58  		if err := test.fn(); err != nil {
    59  			t.Errorf("%s: %v", test.name, err)
    60  			continue
    61  		}
    62  		if fd := test.file.FD(); fd != -1 {
    63  			t.Errorf("%s: got FD() = %d, want = -1", test.name, fd)
    64  		}
    65  	}
    66  }
    67  
    68  func TestStartsNegOne(t *testing.T) {
    69  	type entry struct {
    70  		name string
    71  		file *FD
    72  	}
    73  
    74  	tests := []entry{
    75  		{"-1", New(-1)},
    76  		{"-2", New(-2)},
    77  		{"MinInt32", New(math.MinInt32)},
    78  		{"MinInt64", New(math.MinInt64)},
    79  	}
    80  
    81  	for _, test := range tests {
    82  		if fd := test.file.FD(); fd != -1 {
    83  			t.Errorf("%s: got FD() = %d, want = -1", test.name, fd)
    84  		}
    85  	}
    86  }
    87  
    88  func TestFileDotFile(t *testing.T) {
    89  	fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
    90  	if err != nil {
    91  		t.Fatal("unix.Socket:", err)
    92  	}
    93  
    94  	f := New(fd)
    95  	of, err := f.File()
    96  	if err != nil {
    97  		t.Fatalf("File got err %v want nil", err)
    98  	}
    99  
   100  	if ofd, nfd := int(of.Fd()), f.FD(); ofd == nfd || ofd == -1 {
   101  		// Try not to double close the FD.
   102  		f.Release()
   103  
   104  		t.Fatalf("got %#v.File().Fd() = %d, want new FD", f, ofd)
   105  	}
   106  
   107  	f.Close()
   108  	of.Close()
   109  }
   110  
   111  func TestFileDotFileError(t *testing.T) {
   112  	f := &FD{ReadWriter{-2}}
   113  
   114  	if of, err := f.File(); err == nil {
   115  		t.Errorf("File %v got nil err want non-nil", of)
   116  		of.Close()
   117  	}
   118  }
   119  
   120  func TestNewFromFile(t *testing.T) {
   121  	f, err := NewFromFile(os.Stdin)
   122  	if err != nil {
   123  		t.Fatalf("NewFromFile got err %v want nil", err)
   124  	}
   125  	if nfd, ofd := f.FD(), int(os.Stdin.Fd()); nfd == -1 || nfd == ofd {
   126  		t.Errorf("got FD() = %d, want = new FD (old FD was %d)", nfd, ofd)
   127  	}
   128  	f.Close()
   129  }
   130  
   131  func TestNewFromFileError(t *testing.T) {
   132  	f, err := NewFromFile(nil)
   133  	if err == nil {
   134  		t.Errorf("NewFromFile got %v with nil err want non-nil", f)
   135  		f.Close()
   136  	}
   137  }