github.com/alexis81/domosgo@v0.0.0-20191016125037-5aee90a434af/Domos/src/golang.org/x/sys/unix/syscall_linux_test.go (about)

     1  // Copyright 2016 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 linux
     6  
     7  package unix_test
     8  
     9  import (
    10  	"os"
    11  	"runtime"
    12  	"runtime/debug"
    13  	"testing"
    14  	"time"
    15  
    16  	"golang.org/x/sys/unix"
    17  )
    18  
    19  func TestIoctlGetInt(t *testing.T) {
    20  	f, err := os.Open("/dev/random")
    21  	if err != nil {
    22  		t.Fatalf("failed to open device: %v", err)
    23  	}
    24  	defer f.Close()
    25  
    26  	v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
    27  	if err != nil {
    28  		t.Fatalf("failed to perform ioctl: %v", err)
    29  	}
    30  
    31  	t.Logf("%d bits of entropy available", v)
    32  }
    33  
    34  func TestPpoll(t *testing.T) {
    35  	f, cleanup := mktmpfifo(t)
    36  	defer cleanup()
    37  
    38  	const timeout = 100 * time.Millisecond
    39  
    40  	ok := make(chan bool, 1)
    41  	go func() {
    42  		select {
    43  		case <-time.After(10 * timeout):
    44  			t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
    45  		case <-ok:
    46  		}
    47  	}()
    48  
    49  	fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
    50  	timeoutTs := unix.NsecToTimespec(int64(timeout))
    51  	n, err := unix.Ppoll(fds, &timeoutTs, nil)
    52  	ok <- true
    53  	if err != nil {
    54  		t.Errorf("Ppoll: unexpected error: %v", err)
    55  		return
    56  	}
    57  	if n != 0 {
    58  		t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
    59  		return
    60  	}
    61  }
    62  
    63  func TestTime(t *testing.T) {
    64  	var ut unix.Time_t
    65  	ut2, err := unix.Time(&ut)
    66  	if err != nil {
    67  		t.Fatalf("Time: %v", err)
    68  	}
    69  	if ut != ut2 {
    70  		t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
    71  	}
    72  
    73  	var now time.Time
    74  
    75  	for i := 0; i < 10; i++ {
    76  		ut, err = unix.Time(nil)
    77  		if err != nil {
    78  			t.Fatalf("Time: %v", err)
    79  		}
    80  
    81  		now = time.Now()
    82  
    83  		if int64(ut) == now.Unix() {
    84  			return
    85  		}
    86  	}
    87  
    88  	t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
    89  }
    90  
    91  func TestUtime(t *testing.T) {
    92  	defer chtmpdir(t)()
    93  
    94  	touch(t, "file1")
    95  
    96  	buf := &unix.Utimbuf{
    97  		Modtime: 12345,
    98  	}
    99  
   100  	err := unix.Utime("file1", buf)
   101  	if err != nil {
   102  		t.Fatalf("Utime: %v", err)
   103  	}
   104  
   105  	fi, err := os.Stat("file1")
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	if fi.ModTime().Unix() != 12345 {
   111  		t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
   112  	}
   113  }
   114  
   115  func TestUtimesNanoAt(t *testing.T) {
   116  	defer chtmpdir(t)()
   117  
   118  	symlink := "symlink1"
   119  	os.Remove(symlink)
   120  	err := os.Symlink("nonexisting", symlink)
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  
   125  	ts := []unix.Timespec{
   126  		{Sec: 1111, Nsec: 2222},
   127  		{Sec: 3333, Nsec: 4444},
   128  	}
   129  	err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
   130  	if err != nil {
   131  		t.Fatalf("UtimesNanoAt: %v", err)
   132  	}
   133  
   134  	var st unix.Stat_t
   135  	err = unix.Lstat(symlink, &st)
   136  	if err != nil {
   137  		t.Fatalf("Lstat: %v", err)
   138  	}
   139  	if st.Atim != ts[0] {
   140  		t.Errorf("UtimesNanoAt: wrong atime: %v", st.Atim)
   141  	}
   142  	if st.Mtim != ts[1] {
   143  		t.Errorf("UtimesNanoAt: wrong mtime: %v", st.Mtim)
   144  	}
   145  }
   146  
   147  func TestRlimitAs(t *testing.T) {
   148  	// disable GC during to avoid flaky test
   149  	defer debug.SetGCPercent(debug.SetGCPercent(-1))
   150  
   151  	var rlim unix.Rlimit
   152  	err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
   153  	if err != nil {
   154  		t.Fatalf("Getrlimit: %v", err)
   155  	}
   156  	var zero unix.Rlimit
   157  	if zero == rlim {
   158  		t.Fatalf("Getrlimit: got zero value %#v", rlim)
   159  	}
   160  	set := rlim
   161  	set.Cur = uint64(unix.Getpagesize())
   162  	err = unix.Setrlimit(unix.RLIMIT_AS, &set)
   163  	if err != nil {
   164  		t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
   165  	}
   166  
   167  	// RLIMIT_AS was set to the page size, so mmap()'ing twice the page size
   168  	// should fail. See 'man 2 getrlimit'.
   169  	_, err = unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
   170  	if err == nil {
   171  		t.Fatal("Mmap: unexpectedly suceeded after setting RLIMIT_AS")
   172  	}
   173  
   174  	err = unix.Setrlimit(unix.RLIMIT_AS, &rlim)
   175  	if err != nil {
   176  		t.Fatalf("Setrlimit: restore failed: %#v %v", rlim, err)
   177  	}
   178  
   179  	b, err := unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
   180  	if err != nil {
   181  		t.Fatalf("Mmap: %v", err)
   182  	}
   183  	err = unix.Munmap(b)
   184  	if err != nil {
   185  		t.Fatalf("Munmap: %v", err)
   186  	}
   187  }
   188  
   189  func TestSelect(t *testing.T) {
   190  	_, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
   191  	if err != nil {
   192  		t.Fatalf("Select: %v", err)
   193  	}
   194  
   195  	dur := 150 * time.Millisecond
   196  	tv := unix.NsecToTimeval(int64(dur))
   197  	start := time.Now()
   198  	_, err = unix.Select(0, nil, nil, nil, &tv)
   199  	took := time.Since(start)
   200  	if err != nil {
   201  		t.Fatalf("Select: %v", err)
   202  	}
   203  
   204  	if took < dur {
   205  		t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
   206  	}
   207  }
   208  
   209  func TestPselect(t *testing.T) {
   210  	_, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
   211  	if err != nil {
   212  		t.Fatalf("Pselect: %v", err)
   213  	}
   214  
   215  	dur := 2500 * time.Microsecond
   216  	ts := unix.NsecToTimespec(int64(dur))
   217  	start := time.Now()
   218  	_, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
   219  	took := time.Since(start)
   220  	if err != nil {
   221  		t.Fatalf("Pselect: %v", err)
   222  	}
   223  
   224  	if took < dur {
   225  		t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
   226  	}
   227  }
   228  
   229  func TestSchedSetaffinity(t *testing.T) {
   230  	runtime.LockOSThread()
   231  	defer runtime.UnlockOSThread()
   232  
   233  	var oldMask unix.CPUSet
   234  	err := unix.SchedGetaffinity(0, &oldMask)
   235  	if err != nil {
   236  		t.Fatalf("SchedGetaffinity: %v", err)
   237  	}
   238  
   239  	var newMask unix.CPUSet
   240  	newMask.Zero()
   241  	if newMask.Count() != 0 {
   242  		t.Errorf("CpuZero: didn't zero CPU set: %v", newMask)
   243  	}
   244  	cpu := 1
   245  	newMask.Set(cpu)
   246  	if newMask.Count() != 1 || !newMask.IsSet(cpu) {
   247  		t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
   248  	}
   249  	cpu = 5
   250  	newMask.Set(cpu)
   251  	if newMask.Count() != 2 || !newMask.IsSet(cpu) {
   252  		t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
   253  	}
   254  	newMask.Clear(cpu)
   255  	if newMask.Count() != 1 || newMask.IsSet(cpu) {
   256  		t.Errorf("CpuClr: didn't clear CPU %d in set: %v", cpu, newMask)
   257  	}
   258  
   259  	if runtime.NumCPU() < 2 {
   260  		t.Skip("skipping setaffinity tests on single CPU system")
   261  	}
   262  
   263  	err = unix.SchedSetaffinity(0, &newMask)
   264  	if err != nil {
   265  		t.Fatalf("SchedSetaffinity: %v", err)
   266  	}
   267  
   268  	var gotMask unix.CPUSet
   269  	err = unix.SchedGetaffinity(0, &gotMask)
   270  	if err != nil {
   271  		t.Fatalf("SchedGetaffinity: %v", err)
   272  	}
   273  
   274  	if gotMask != newMask {
   275  		t.Errorf("SchedSetaffinity: returned affinity mask does not match set affinity mask")
   276  	}
   277  
   278  	// Restore old mask so it doesn't affect successive tests
   279  	err = unix.SchedSetaffinity(0, &oldMask)
   280  	if err != nil {
   281  		t.Fatalf("SchedSetaffinity: %v", err)
   282  	}
   283  }
   284  
   285  func TestStatx(t *testing.T) {
   286  	var stx unix.Statx_t
   287  	err := unix.Statx(unix.AT_FDCWD, ".", 0, 0, &stx)
   288  	if err == unix.ENOSYS {
   289  		t.Skip("statx syscall is not available, skipping test")
   290  	} else if err != nil {
   291  		t.Fatalf("Statx: %v", err)
   292  	}
   293  
   294  	defer chtmpdir(t)()
   295  	touch(t, "file1")
   296  
   297  	var st unix.Stat_t
   298  	err = unix.Stat("file1", &st)
   299  	if err != nil {
   300  		t.Fatalf("Stat: %v", err)
   301  	}
   302  
   303  	flags := unix.AT_STATX_SYNC_AS_STAT
   304  	err = unix.Statx(unix.AT_FDCWD, "file1", flags, unix.STATX_ALL, &stx)
   305  	if err != nil {
   306  		t.Fatalf("Statx: %v", err)
   307  	}
   308  
   309  	if uint32(stx.Mode) != st.Mode {
   310  		t.Errorf("Statx: returned stat mode does not match Stat")
   311  	}
   312  
   313  	atime := unix.StatxTimestamp{Sec: int64(st.Atim.Sec), Nsec: uint32(st.Atim.Nsec)}
   314  	ctime := unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
   315  	mtime := unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
   316  
   317  	if stx.Atime != atime {
   318  		t.Errorf("Statx: returned stat atime does not match Stat")
   319  	}
   320  	if stx.Ctime != ctime {
   321  		t.Errorf("Statx: returned stat ctime does not match Stat")
   322  	}
   323  	if stx.Mtime != mtime {
   324  		t.Errorf("Statx: returned stat mtime does not match Stat")
   325  	}
   326  
   327  	err = os.Symlink("file1", "symlink1")
   328  	if err != nil {
   329  		t.Fatal(err)
   330  	}
   331  
   332  	err = unix.Lstat("symlink1", &st)
   333  	if err != nil {
   334  		t.Fatalf("Lstat: %v", err)
   335  	}
   336  
   337  	err = unix.Statx(unix.AT_FDCWD, "symlink1", flags, unix.STATX_BASIC_STATS, &stx)
   338  	if err != nil {
   339  		t.Fatalf("Statx: %v", err)
   340  	}
   341  
   342  	// follow symlink, expect a regulat file
   343  	if stx.Mode&unix.S_IFREG == 0 {
   344  		t.Errorf("Statx: didn't follow symlink")
   345  	}
   346  
   347  	err = unix.Statx(unix.AT_FDCWD, "symlink1", flags|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ALL, &stx)
   348  	if err != nil {
   349  		t.Fatalf("Statx: %v", err)
   350  	}
   351  
   352  	// follow symlink, expect a symlink
   353  	if stx.Mode&unix.S_IFLNK == 0 {
   354  		t.Errorf("Statx: unexpectedly followed symlink")
   355  	}
   356  	if uint32(stx.Mode) != st.Mode {
   357  		t.Errorf("Statx: returned stat mode does not match Lstat")
   358  	}
   359  
   360  	atime = unix.StatxTimestamp{Sec: int64(st.Atim.Sec), Nsec: uint32(st.Atim.Nsec)}
   361  	ctime = unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
   362  	mtime = unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
   363  
   364  	if stx.Atime != atime {
   365  		t.Errorf("Statx: returned stat atime does not match Lstat")
   366  	}
   367  	if stx.Ctime != ctime {
   368  		t.Errorf("Statx: returned stat ctime does not match Lstat")
   369  	}
   370  	if stx.Mtime != mtime {
   371  		t.Errorf("Statx: returned stat mtime does not match Lstat")
   372  	}
   373  }