github.com/ie310mu/ie310go/forks/golang.org/x/sys@v0.0.0-20190821095322-9a46783d4de5/unix/syscall_unix_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package unix_test
     8  
     9  import (
    10  	"flag"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"net"
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  	"runtime"
    18  	"strconv"
    19  	"syscall"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/ie310mu/ie310go/forks/golang.org/x/sys/unix"
    24  )
    25  
    26  // Tests that below functions, structures and constants are consistent
    27  // on all Unix-like systems.
    28  func _() {
    29  	// program scheduling priority functions and constants
    30  	var (
    31  		_ func(int, int, int) error   = unix.Setpriority
    32  		_ func(int, int) (int, error) = unix.Getpriority
    33  	)
    34  	const (
    35  		_ int = unix.PRIO_USER
    36  		_ int = unix.PRIO_PROCESS
    37  		_ int = unix.PRIO_PGRP
    38  	)
    39  
    40  	// termios constants
    41  	const (
    42  		_ int = unix.TCIFLUSH
    43  		_ int = unix.TCIOFLUSH
    44  		_ int = unix.TCOFLUSH
    45  	)
    46  
    47  	// fcntl file locking structure and constants
    48  	var (
    49  		_ = unix.Flock_t{
    50  			Type:   int16(0),
    51  			Whence: int16(0),
    52  			Start:  int64(0),
    53  			Len:    int64(0),
    54  			Pid:    int32(0),
    55  		}
    56  	)
    57  	const (
    58  		_ = unix.F_GETLK
    59  		_ = unix.F_SETLK
    60  		_ = unix.F_SETLKW
    61  	)
    62  }
    63  
    64  func TestErrnoSignalName(t *testing.T) {
    65  	testErrors := []struct {
    66  		num  syscall.Errno
    67  		name string
    68  	}{
    69  		{syscall.EPERM, "EPERM"},
    70  		{syscall.EINVAL, "EINVAL"},
    71  		{syscall.ENOENT, "ENOENT"},
    72  	}
    73  
    74  	for _, te := range testErrors {
    75  		t.Run(fmt.Sprintf("%d/%s", te.num, te.name), func(t *testing.T) {
    76  			e := unix.ErrnoName(te.num)
    77  			if e != te.name {
    78  				t.Errorf("ErrnoName(%d) returned %s, want %s", te.num, e, te.name)
    79  			}
    80  		})
    81  	}
    82  
    83  	testSignals := []struct {
    84  		num  syscall.Signal
    85  		name string
    86  	}{
    87  		{syscall.SIGHUP, "SIGHUP"},
    88  		{syscall.SIGPIPE, "SIGPIPE"},
    89  		{syscall.SIGSEGV, "SIGSEGV"},
    90  	}
    91  
    92  	for _, ts := range testSignals {
    93  		t.Run(fmt.Sprintf("%d/%s", ts.num, ts.name), func(t *testing.T) {
    94  			s := unix.SignalName(ts.num)
    95  			if s != ts.name {
    96  				t.Errorf("SignalName(%d) returned %s, want %s", ts.num, s, ts.name)
    97  			}
    98  		})
    99  	}
   100  }
   101  
   102  func TestSignalNum(t *testing.T) {
   103  	testSignals := []struct {
   104  		name string
   105  		want syscall.Signal
   106  	}{
   107  		{"SIGHUP", syscall.SIGHUP},
   108  		{"SIGPIPE", syscall.SIGPIPE},
   109  		{"SIGSEGV", syscall.SIGSEGV},
   110  		{"NONEXISTS", 0},
   111  	}
   112  	for _, ts := range testSignals {
   113  		t.Run(fmt.Sprintf("%s/%d", ts.name, ts.want), func(t *testing.T) {
   114  			got := unix.SignalNum(ts.name)
   115  			if got != ts.want {
   116  				t.Errorf("SignalNum(%s) returned %d, want %d", ts.name, got, ts.want)
   117  			}
   118  		})
   119  
   120  	}
   121  }
   122  
   123  func TestFcntlInt(t *testing.T) {
   124  	t.Parallel()
   125  	file, err := ioutil.TempFile("", "TestFnctlInt")
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	defer os.Remove(file.Name())
   130  	defer file.Close()
   131  	f := file.Fd()
   132  	flags, err := unix.FcntlInt(f, unix.F_GETFD, 0)
   133  	if err != nil {
   134  		t.Fatal(err)
   135  	}
   136  	if flags&unix.FD_CLOEXEC == 0 {
   137  		t.Errorf("flags %#x do not include FD_CLOEXEC", flags)
   138  	}
   139  }
   140  
   141  // TestFcntlFlock tests whether the file locking structure matches
   142  // the calling convention of each kernel.
   143  func TestFcntlFlock(t *testing.T) {
   144  	name := filepath.Join(os.TempDir(), "TestFcntlFlock")
   145  	fd, err := unix.Open(name, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0)
   146  	if err != nil {
   147  		t.Fatalf("Open failed: %v", err)
   148  	}
   149  	defer unix.Unlink(name)
   150  	defer unix.Close(fd)
   151  	flock := unix.Flock_t{
   152  		Type:  unix.F_RDLCK,
   153  		Start: 0, Len: 0, Whence: 1,
   154  	}
   155  	if err := unix.FcntlFlock(uintptr(fd), unix.F_GETLK, &flock); err != nil {
   156  		t.Fatalf("FcntlFlock failed: %v", err)
   157  	}
   158  }
   159  
   160  // TestPassFD tests passing a file descriptor over a Unix socket.
   161  //
   162  // This test involved both a parent and child process. The parent
   163  // process is invoked as a normal test, with "go test", which then
   164  // runs the child process by running the current test binary with args
   165  // "-test.run=^TestPassFD$" and an environment variable used to signal
   166  // that the test should become the child process instead.
   167  func TestPassFD(t *testing.T) {
   168  	if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
   169  		t.Skip("cannot exec subprocess on iOS, skipping test")
   170  	}
   171  
   172  	if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
   173  		passFDChild()
   174  		return
   175  	}
   176  
   177  	if runtime.GOOS == "aix" {
   178  		// Unix network isn't properly working on AIX
   179  		// 7.2 with Technical Level < 2
   180  		out, err := exec.Command("oslevel", "-s").Output()
   181  		if err != nil {
   182  			t.Skipf("skipping on AIX because oslevel -s failed: %v", err)
   183  		}
   184  
   185  		if len(out) < len("7200-XX-ZZ-YYMM") { // AIX 7.2, Tech Level XX, Service Pack ZZ, date YYMM
   186  			t.Skip("skipping on AIX because oslevel -s hasn't the right length")
   187  		}
   188  		aixVer := string(out[:4])
   189  		tl, err := strconv.Atoi(string(out[5:7]))
   190  		if err != nil {
   191  			t.Skipf("skipping on AIX because oslevel -s output cannot be parsed: %v", err)
   192  		}
   193  		if aixVer < "7200" || (aixVer == "7200" && tl < 2) {
   194  			t.Skip("skipped on AIX versions previous to 7.2 TL 2")
   195  		}
   196  	}
   197  
   198  	tempDir, err := ioutil.TempDir("", "TestPassFD")
   199  	if err != nil {
   200  		t.Fatal(err)
   201  	}
   202  	defer os.RemoveAll(tempDir)
   203  
   204  	fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0)
   205  	if err != nil {
   206  		t.Fatalf("Socketpair: %v", err)
   207  	}
   208  	defer unix.Close(fds[0])
   209  	defer unix.Close(fds[1])
   210  	writeFile := os.NewFile(uintptr(fds[0]), "child-writes")
   211  	readFile := os.NewFile(uintptr(fds[1]), "parent-reads")
   212  	defer writeFile.Close()
   213  	defer readFile.Close()
   214  
   215  	cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir)
   216  	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
   217  	if lp := os.Getenv("LD_LIBRARY_PATH"); lp != "" {
   218  		cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+lp)
   219  	}
   220  	cmd.ExtraFiles = []*os.File{writeFile}
   221  
   222  	out, err := cmd.CombinedOutput()
   223  	if len(out) > 0 || err != nil {
   224  		t.Fatalf("child process: %q, %v", out, err)
   225  	}
   226  
   227  	c, err := net.FileConn(readFile)
   228  	if err != nil {
   229  		t.Fatalf("FileConn: %v", err)
   230  	}
   231  	defer c.Close()
   232  
   233  	uc, ok := c.(*net.UnixConn)
   234  	if !ok {
   235  		t.Fatalf("unexpected FileConn type; expected UnixConn, got %T", c)
   236  	}
   237  
   238  	buf := make([]byte, 32) // expect 1 byte
   239  	oob := make([]byte, 32) // expect 24 bytes
   240  	closeUnix := time.AfterFunc(5*time.Second, func() {
   241  		t.Logf("timeout reading from unix socket")
   242  		uc.Close()
   243  	})
   244  	_, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
   245  	if err != nil {
   246  		t.Fatalf("ReadMsgUnix: %v", err)
   247  	}
   248  	closeUnix.Stop()
   249  
   250  	scms, err := unix.ParseSocketControlMessage(oob[:oobn])
   251  	if err != nil {
   252  		t.Fatalf("ParseSocketControlMessage: %v", err)
   253  	}
   254  	if len(scms) != 1 {
   255  		t.Fatalf("expected 1 SocketControlMessage; got scms = %#v", scms)
   256  	}
   257  	scm := scms[0]
   258  	gotFds, err := unix.ParseUnixRights(&scm)
   259  	if err != nil {
   260  		t.Fatalf("unix.ParseUnixRights: %v", err)
   261  	}
   262  	if len(gotFds) != 1 {
   263  		t.Fatalf("wanted 1 fd; got %#v", gotFds)
   264  	}
   265  
   266  	f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
   267  	defer f.Close()
   268  
   269  	got, err := ioutil.ReadAll(f)
   270  	want := "Hello from child process!\n"
   271  	if string(got) != want {
   272  		t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)
   273  	}
   274  }
   275  
   276  // passFDChild is the child process used by TestPassFD.
   277  func passFDChild() {
   278  	defer os.Exit(0)
   279  
   280  	// Look for our fd. It should be fd 3, but we work around an fd leak
   281  	// bug here (http://golang.org/issue/2603) to let it be elsewhere.
   282  	var uc *net.UnixConn
   283  	for fd := uintptr(3); fd <= 10; fd++ {
   284  		f := os.NewFile(fd, "unix-conn")
   285  		var ok bool
   286  		netc, _ := net.FileConn(f)
   287  		uc, ok = netc.(*net.UnixConn)
   288  		if ok {
   289  			break
   290  		}
   291  	}
   292  	if uc == nil {
   293  		fmt.Println("failed to find unix fd")
   294  		return
   295  	}
   296  
   297  	// Make a file f to send to our parent process on uc.
   298  	// We make it in tempDir, which our parent will clean up.
   299  	flag.Parse()
   300  	tempDir := flag.Arg(0)
   301  	f, err := ioutil.TempFile(tempDir, "")
   302  	if err != nil {
   303  		fmt.Printf("TempFile: %v", err)
   304  		return
   305  	}
   306  
   307  	f.Write([]byte("Hello from child process!\n"))
   308  	f.Seek(0, 0)
   309  
   310  	rights := unix.UnixRights(int(f.Fd()))
   311  	dummyByte := []byte("x")
   312  	n, oobn, err := uc.WriteMsgUnix(dummyByte, rights, nil)
   313  	if err != nil {
   314  		fmt.Printf("WriteMsgUnix: %v", err)
   315  		return
   316  	}
   317  	if n != 1 || oobn != len(rights) {
   318  		fmt.Printf("WriteMsgUnix = %d, %d; want 1, %d", n, oobn, len(rights))
   319  		return
   320  	}
   321  }
   322  
   323  // TestUnixRightsRoundtrip tests that UnixRights, ParseSocketControlMessage,
   324  // and ParseUnixRights are able to successfully round-trip lists of file descriptors.
   325  func TestUnixRightsRoundtrip(t *testing.T) {
   326  	testCases := [...][][]int{
   327  		{{42}},
   328  		{{1, 2}},
   329  		{{3, 4, 5}},
   330  		{{}},
   331  		{{1, 2}, {3, 4, 5}, {}, {7}},
   332  	}
   333  	for _, testCase := range testCases {
   334  		b := []byte{}
   335  		var n int
   336  		for _, fds := range testCase {
   337  			// Last assignment to n wins
   338  			n = len(b) + unix.CmsgLen(4*len(fds))
   339  			b = append(b, unix.UnixRights(fds...)...)
   340  		}
   341  		// Truncate b
   342  		b = b[:n]
   343  
   344  		scms, err := unix.ParseSocketControlMessage(b)
   345  		if err != nil {
   346  			t.Fatalf("ParseSocketControlMessage: %v", err)
   347  		}
   348  		if len(scms) != len(testCase) {
   349  			t.Fatalf("expected %v SocketControlMessage; got scms = %#v", len(testCase), scms)
   350  		}
   351  		for i, scm := range scms {
   352  			gotFds, err := unix.ParseUnixRights(&scm)
   353  			if err != nil {
   354  				t.Fatalf("ParseUnixRights: %v", err)
   355  			}
   356  			wantFds := testCase[i]
   357  			if len(gotFds) != len(wantFds) {
   358  				t.Fatalf("expected %v fds, got %#v", len(wantFds), gotFds)
   359  			}
   360  			for j, fd := range gotFds {
   361  				if fd != wantFds[j] {
   362  					t.Fatalf("expected fd %v, got %v", wantFds[j], fd)
   363  				}
   364  			}
   365  		}
   366  	}
   367  }
   368  
   369  func TestRlimit(t *testing.T) {
   370  	var rlimit, zero unix.Rlimit
   371  	err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit)
   372  	if err != nil {
   373  		t.Fatalf("Getrlimit: save failed: %v", err)
   374  	}
   375  	if zero == rlimit {
   376  		t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit)
   377  	}
   378  	set := rlimit
   379  	set.Cur = set.Max - 1
   380  	if runtime.GOOS == "darwin" && set.Cur > 10240 {
   381  		// The max file limit is 10240, even though
   382  		// the max returned by Getrlimit is 1<<63-1.
   383  		// This is OPEN_MAX in sys/syslimits.h.
   384  		set.Cur = 10240
   385  	}
   386  	err = unix.Setrlimit(unix.RLIMIT_NOFILE, &set)
   387  	if err != nil {
   388  		t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
   389  	}
   390  	var get unix.Rlimit
   391  	err = unix.Getrlimit(unix.RLIMIT_NOFILE, &get)
   392  	if err != nil {
   393  		t.Fatalf("Getrlimit: get failed: %v", err)
   394  	}
   395  	set = rlimit
   396  	set.Cur = set.Max - 1
   397  	if set != get {
   398  		// Seems like Darwin requires some privilege to
   399  		// increase the soft limit of rlimit sandbox, though
   400  		// Setrlimit never reports an error.
   401  		switch runtime.GOOS {
   402  		case "darwin":
   403  		default:
   404  			t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get)
   405  		}
   406  	}
   407  	err = unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
   408  	if err != nil {
   409  		t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err)
   410  	}
   411  }
   412  
   413  func TestSeekFailure(t *testing.T) {
   414  	_, err := unix.Seek(-1, 0, 0)
   415  	if err == nil {
   416  		t.Fatalf("Seek(-1, 0, 0) did not fail")
   417  	}
   418  	str := err.Error() // used to crash on Linux
   419  	t.Logf("Seek: %v", str)
   420  	if str == "" {
   421  		t.Fatalf("Seek(-1, 0, 0) return error with empty message")
   422  	}
   423  }
   424  
   425  func TestSetsockoptString(t *testing.T) {
   426  	// should not panic on empty string, see issue #31277
   427  	err := unix.SetsockoptString(-1, 0, 0, "")
   428  	if err == nil {
   429  		t.Fatalf("SetsockoptString: did not fail")
   430  	}
   431  }
   432  
   433  func TestDup(t *testing.T) {
   434  	file, err := ioutil.TempFile("", "TestDup")
   435  	if err != nil {
   436  		t.Fatalf("Tempfile failed: %v", err)
   437  	}
   438  	defer os.Remove(file.Name())
   439  	defer file.Close()
   440  	f := int(file.Fd())
   441  
   442  	newFd, err := unix.Dup(f)
   443  	if err != nil {
   444  		t.Fatalf("Dup: %v", err)
   445  	}
   446  
   447  	// Create and reserve a file descriptor.
   448  	// Dup2 automatically closes it before reusing it.
   449  	nullFile, err := os.Open("/dev/null")
   450  	if err != nil {
   451  		t.Fatal(err)
   452  	}
   453  	dupFd := int(file.Fd())
   454  	err = unix.Dup2(newFd, dupFd)
   455  	if err != nil {
   456  		t.Fatalf("Dup2: %v", err)
   457  	}
   458  	// Keep the dummy file open long enough to not be closed in
   459  	// its finalizer.
   460  	runtime.KeepAlive(nullFile)
   461  
   462  	b1 := []byte("Test123")
   463  	b2 := make([]byte, 7)
   464  	_, err = unix.Write(dupFd, b1)
   465  	if err != nil {
   466  		t.Fatalf("Write to dup2 fd failed: %v", err)
   467  	}
   468  	_, err = unix.Seek(f, 0, 0)
   469  	if err != nil {
   470  		t.Fatalf("Seek failed: %v", err)
   471  	}
   472  	_, err = unix.Read(f, b2)
   473  	if err != nil {
   474  		t.Fatalf("Read back failed: %v", err)
   475  	}
   476  	if string(b1) != string(b2) {
   477  		t.Errorf("Dup: stdout write not in file, expected %v, got %v", string(b1), string(b2))
   478  	}
   479  }
   480  
   481  func TestPoll(t *testing.T) {
   482  	if runtime.GOOS == "android" ||
   483  		(runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")) {
   484  		t.Skip("mkfifo syscall is not available on android and iOS, skipping test")
   485  	}
   486  
   487  	defer chtmpdir(t)()
   488  	f, cleanup := mktmpfifo(t)
   489  	defer cleanup()
   490  
   491  	const timeout = 100
   492  
   493  	ok := make(chan bool, 1)
   494  	go func() {
   495  		select {
   496  		case <-time.After(10 * timeout * time.Millisecond):
   497  			t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
   498  		case <-ok:
   499  		}
   500  	}()
   501  
   502  	fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
   503  	n, err := unix.Poll(fds, timeout)
   504  	ok <- true
   505  	if err != nil {
   506  		t.Errorf("Poll: unexpected error: %v", err)
   507  		return
   508  	}
   509  	if n != 0 {
   510  		t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
   511  		return
   512  	}
   513  }
   514  
   515  func TestGetwd(t *testing.T) {
   516  	fd, err := os.Open(".")
   517  	if err != nil {
   518  		t.Fatalf("Open .: %s", err)
   519  	}
   520  	defer fd.Close()
   521  	// Directory list for test. Do not worry if any are symlinks or do not
   522  	// exist on some common unix desktop environments. That will be checked.
   523  	dirs := []string{"/", "/usr/bin", "/etc", "/var", "/opt"}
   524  	switch runtime.GOOS {
   525  	case "android":
   526  		dirs = []string{"/", "/system/bin"}
   527  	case "darwin":
   528  		switch runtime.GOARCH {
   529  		case "arm", "arm64":
   530  			d1, err := ioutil.TempDir("", "d1")
   531  			if err != nil {
   532  				t.Fatalf("TempDir: %v", err)
   533  			}
   534  			d2, err := ioutil.TempDir("", "d2")
   535  			if err != nil {
   536  				t.Fatalf("TempDir: %v", err)
   537  			}
   538  			dirs = []string{d1, d2}
   539  		}
   540  	}
   541  	oldwd := os.Getenv("PWD")
   542  	for _, d := range dirs {
   543  		// Check whether d exists, is a dir and that d's path does not contain a symlink
   544  		fi, err := os.Stat(d)
   545  		if err != nil || !fi.IsDir() {
   546  			t.Logf("Test dir %s stat error (%v) or not a directory, skipping", d, err)
   547  			continue
   548  		}
   549  		check, err := filepath.EvalSymlinks(d)
   550  		if err != nil || check != d {
   551  			t.Logf("Test dir %s (%s) is symlink or other error (%v), skipping", d, check, err)
   552  			continue
   553  		}
   554  		err = os.Chdir(d)
   555  		if err != nil {
   556  			t.Fatalf("Chdir: %v", err)
   557  		}
   558  		pwd, err := unix.Getwd()
   559  		if err != nil {
   560  			t.Fatalf("Getwd in %s: %s", d, err)
   561  		}
   562  		os.Setenv("PWD", oldwd)
   563  		err = fd.Chdir()
   564  		if err != nil {
   565  			// We changed the current directory and cannot go back.
   566  			// Don't let the tests continue; they'll scribble
   567  			// all over some other directory.
   568  			fmt.Fprintf(os.Stderr, "fchdir back to dot failed: %s\n", err)
   569  			os.Exit(1)
   570  		}
   571  		if pwd != d {
   572  			t.Fatalf("Getwd returned %q want %q", pwd, d)
   573  		}
   574  	}
   575  }
   576  
   577  func TestFstatat(t *testing.T) {
   578  	defer chtmpdir(t)()
   579  
   580  	touch(t, "file1")
   581  
   582  	var st1 unix.Stat_t
   583  	err := unix.Stat("file1", &st1)
   584  	if err != nil {
   585  		t.Fatalf("Stat: %v", err)
   586  	}
   587  
   588  	var st2 unix.Stat_t
   589  	err = unix.Fstatat(unix.AT_FDCWD, "file1", &st2, 0)
   590  	if err != nil {
   591  		t.Fatalf("Fstatat: %v", err)
   592  	}
   593  
   594  	if st1 != st2 {
   595  		t.Errorf("Fstatat: returned stat does not match Stat")
   596  	}
   597  
   598  	err = os.Symlink("file1", "symlink1")
   599  	if err != nil {
   600  		t.Fatal(err)
   601  	}
   602  
   603  	err = unix.Lstat("symlink1", &st1)
   604  	if err != nil {
   605  		t.Fatalf("Lstat: %v", err)
   606  	}
   607  
   608  	err = unix.Fstatat(unix.AT_FDCWD, "symlink1", &st2, unix.AT_SYMLINK_NOFOLLOW)
   609  	if err != nil {
   610  		t.Fatalf("Fstatat: %v", err)
   611  	}
   612  
   613  	if st1 != st2 {
   614  		t.Errorf("Fstatat: returned stat does not match Lstat")
   615  	}
   616  }
   617  
   618  func TestFchmodat(t *testing.T) {
   619  	defer chtmpdir(t)()
   620  
   621  	touch(t, "file1")
   622  	err := os.Symlink("file1", "symlink1")
   623  	if err != nil {
   624  		t.Fatal(err)
   625  	}
   626  
   627  	mode := os.FileMode(0444)
   628  	err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", uint32(mode), 0)
   629  	if err != nil {
   630  		t.Fatalf("Fchmodat: unexpected error: %v", err)
   631  	}
   632  
   633  	fi, err := os.Stat("file1")
   634  	if err != nil {
   635  		t.Fatal(err)
   636  	}
   637  
   638  	if fi.Mode() != mode {
   639  		t.Errorf("Fchmodat: failed to change file mode: expected %v, got %v", mode, fi.Mode())
   640  	}
   641  
   642  	mode = os.FileMode(0644)
   643  	didChmodSymlink := true
   644  	err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", uint32(mode), unix.AT_SYMLINK_NOFOLLOW)
   645  	if err != nil {
   646  		if (runtime.GOOS == "android" || runtime.GOOS == "linux" ||
   647  			runtime.GOOS == "solaris" || runtime.GOOS == "illumos") && err == unix.EOPNOTSUPP {
   648  			// Linux and Illumos don't support flags != 0
   649  			didChmodSymlink = false
   650  		} else {
   651  			t.Fatalf("Fchmodat: unexpected error: %v", err)
   652  		}
   653  	}
   654  
   655  	if !didChmodSymlink {
   656  		// Didn't change mode of the symlink. On Linux, the permissions
   657  		// of a symbolic link are always 0777 according to symlink(7)
   658  		mode = os.FileMode(0777)
   659  	}
   660  
   661  	var st unix.Stat_t
   662  	err = unix.Lstat("symlink1", &st)
   663  	if err != nil {
   664  		t.Fatal(err)
   665  	}
   666  
   667  	got := os.FileMode(st.Mode & 0777)
   668  	if got != mode {
   669  		t.Errorf("Fchmodat: failed to change symlink mode: expected %v, got %v", mode, got)
   670  	}
   671  }
   672  
   673  func TestMkdev(t *testing.T) {
   674  	major := uint32(42)
   675  	minor := uint32(7)
   676  	dev := unix.Mkdev(major, minor)
   677  
   678  	if unix.Major(dev) != major {
   679  		t.Errorf("Major(%#x) == %d, want %d", dev, unix.Major(dev), major)
   680  	}
   681  	if unix.Minor(dev) != minor {
   682  		t.Errorf("Minor(%#x) == %d, want %d", dev, unix.Minor(dev), minor)
   683  	}
   684  }
   685  
   686  func TestRenameat(t *testing.T) {
   687  	defer chtmpdir(t)()
   688  
   689  	from, to := "renamefrom", "renameto"
   690  
   691  	touch(t, from)
   692  
   693  	err := unix.Renameat(unix.AT_FDCWD, from, unix.AT_FDCWD, to)
   694  	if err != nil {
   695  		t.Fatalf("Renameat: unexpected error: %v", err)
   696  	}
   697  
   698  	_, err = os.Stat(to)
   699  	if err != nil {
   700  		t.Error(err)
   701  	}
   702  
   703  	_, err = os.Stat(from)
   704  	if err == nil {
   705  		t.Errorf("Renameat: stat of renamed file %q unexpectedly succeeded", from)
   706  	}
   707  }
   708  
   709  // mktmpfifo creates a temporary FIFO and provides a cleanup function.
   710  func mktmpfifo(t *testing.T) (*os.File, func()) {
   711  	err := unix.Mkfifo("fifo", 0666)
   712  	if err != nil {
   713  		t.Fatalf("mktmpfifo: failed to create FIFO: %v", err)
   714  	}
   715  
   716  	f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
   717  	if err != nil {
   718  		os.Remove("fifo")
   719  		t.Fatalf("mktmpfifo: failed to open FIFO: %v", err)
   720  	}
   721  
   722  	return f, func() {
   723  		f.Close()
   724  		os.Remove("fifo")
   725  	}
   726  }
   727  
   728  // utilities taken from os/os_test.go
   729  
   730  func touch(t *testing.T, name string) {
   731  	f, err := os.Create(name)
   732  	if err != nil {
   733  		t.Fatal(err)
   734  	}
   735  	if err := f.Close(); err != nil {
   736  		t.Fatal(err)
   737  	}
   738  }
   739  
   740  // chtmpdir changes the working directory to a new temporary directory and
   741  // provides a cleanup function. Used when PWD is read-only.
   742  func chtmpdir(t *testing.T) func() {
   743  	oldwd, err := os.Getwd()
   744  	if err != nil {
   745  		t.Fatalf("chtmpdir: %v", err)
   746  	}
   747  	d, err := ioutil.TempDir("", "test")
   748  	if err != nil {
   749  		t.Fatalf("chtmpdir: %v", err)
   750  	}
   751  	if err := os.Chdir(d); err != nil {
   752  		t.Fatalf("chtmpdir: %v", err)
   753  	}
   754  	return func() {
   755  		if err := os.Chdir(oldwd); err != nil {
   756  			t.Fatalf("chtmpdir: %v", err)
   757  		}
   758  		os.RemoveAll(d)
   759  	}
   760  }