github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/unix/syscall_aix_test.go (about)

     1  // Copyright 2018 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
     6  
     7  package unix_test
     8  
     9  import (
    10  	"os"
    11  	"runtime"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/Andyfoo/golang/x/sys/unix"
    16  )
    17  
    18  func TestIoctlGetInt(t *testing.T) {
    19  	f, err := os.Open("/dev/random")
    20  	if err != nil {
    21  		t.Fatalf("failed to open device: %v", err)
    22  	}
    23  	defer f.Close()
    24  
    25  	v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
    26  	if err != nil {
    27  		t.Fatalf("failed to perform ioctl: %v", err)
    28  	}
    29  
    30  	t.Logf("%d bits of entropy available", v)
    31  }
    32  
    33  func TestTime(t *testing.T) {
    34  	var ut unix.Time_t
    35  	ut2, err := unix.Time(&ut)
    36  	if err != nil {
    37  		t.Fatalf("Time: %v", err)
    38  	}
    39  	if ut != ut2 {
    40  		t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
    41  	}
    42  
    43  	var now time.Time
    44  
    45  	for i := 0; i < 10; i++ {
    46  		ut, err = unix.Time(nil)
    47  		if err != nil {
    48  			t.Fatalf("Time: %v", err)
    49  		}
    50  
    51  		now = time.Now()
    52  
    53  		if int64(ut) == now.Unix() {
    54  			return
    55  		}
    56  	}
    57  
    58  	t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
    59  }
    60  
    61  func TestUtime(t *testing.T) {
    62  	defer chtmpdir(t)()
    63  
    64  	touch(t, "file1")
    65  
    66  	buf := &unix.Utimbuf{
    67  		Modtime: 12345,
    68  	}
    69  
    70  	err := unix.Utime("file1", buf)
    71  	if err != nil {
    72  		t.Fatalf("Utime: %v", err)
    73  	}
    74  
    75  	fi, err := os.Stat("file1")
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	if fi.ModTime().Unix() != 12345 {
    81  		t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
    82  	}
    83  }
    84  
    85  func TestUtimesNanoAt(t *testing.T) {
    86  	defer chtmpdir(t)()
    87  
    88  	symlink := "symlink1"
    89  	defer os.Remove(symlink)
    90  	err := os.Symlink("nonexisting", symlink)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	ts := []unix.Timespec{
    96  		{Sec: 1111, Nsec: 2222},
    97  		{Sec: 3333, Nsec: 4444},
    98  	}
    99  	err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
   100  	if err != nil {
   101  		t.Fatalf("UtimesNanoAt: %v", err)
   102  	}
   103  
   104  	var st unix.Stat_t
   105  	err = unix.Lstat(symlink, &st)
   106  	if err != nil {
   107  		t.Fatalf("Lstat: %v", err)
   108  	}
   109  	if runtime.GOARCH == "ppc64" {
   110  		if int64(st.Atim.Sec) != int64(ts[0].Sec) || st.Atim.Nsec != ts[0].Nsec {
   111  			t.Errorf("UtimesNanoAt: wrong atime: %v", st.Atim)
   112  		}
   113  		if int64(st.Mtim.Sec) != int64(ts[1].Sec) || st.Mtim.Nsec != ts[1].Nsec {
   114  			t.Errorf("UtimesNanoAt: wrong mtime: %v", st.Mtim)
   115  		}
   116  	} else {
   117  		if int32(st.Atim.Sec) != int32(ts[0].Sec) || int32(st.Atim.Nsec) != int32(ts[0].Nsec) {
   118  			t.Errorf("UtimesNanoAt: wrong atime: %v", st.Atim)
   119  		}
   120  		if int32(st.Mtim.Sec) != int32(ts[1].Sec) || int32(st.Mtim.Nsec) != int32(ts[1].Nsec) {
   121  			t.Errorf("UtimesNanoAt: wrong mtime: %v", st.Mtim)
   122  		}
   123  	}
   124  }
   125  
   126  func TestSelect(t *testing.T) {
   127  	_, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
   128  	if err != nil {
   129  		t.Fatalf("Select: %v", err)
   130  	}
   131  
   132  	dur := 150 * time.Millisecond
   133  	tv := unix.NsecToTimeval(int64(dur))
   134  	start := time.Now()
   135  	_, err = unix.Select(0, nil, nil, nil, &tv)
   136  	took := time.Since(start)
   137  	if err != nil {
   138  		t.Fatalf("Select: %v", err)
   139  	}
   140  
   141  	if took < dur {
   142  		t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
   143  	}
   144  }
   145  
   146  func TestPselect(t *testing.T) {
   147  	if runtime.GOARCH == "ppc64" {
   148  		t.Skip("pselect issue with structure timespec on AIX 7.2 tl0, skipping test")
   149  	}
   150  
   151  	_, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
   152  	if err != nil {
   153  		t.Fatalf("Pselect: %v", err)
   154  	}
   155  
   156  	dur := 2500 * time.Microsecond
   157  	ts := unix.NsecToTimespec(int64(dur))
   158  	start := time.Now()
   159  	_, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
   160  	took := time.Since(start)
   161  	if err != nil {
   162  		t.Fatalf("Pselect: %v", err)
   163  	}
   164  
   165  	if took < dur {
   166  		t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
   167  	}
   168  }