...
1
2
3
4
5
6
7 package unix_test
8
9 import (
10 "os"
11 "runtime"
12 "testing"
13 "time"
14
15 "golang.org/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 diff := int64(ut) - now.Unix()
53 if -1 <= diff && diff <= 1 {
54 return
55 }
56 }
57
58 t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v±1", ut, now.Unix())
59 }
60
61 func TestUtime(t *testing.T) {
62 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 TestPselect(t *testing.T) {
86 if runtime.GOARCH == "ppc64" {
87 t.Skip("pselect issue with structure timespec on AIX 7.2 tl0, skipping test")
88 }
89
90 _, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
91 if err != nil {
92 t.Fatalf("Pselect: %v", err)
93 }
94
95 dur := 2500 * time.Microsecond
96 ts := unix.NsecToTimespec(int64(dur))
97 start := time.Now()
98 _, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
99 took := time.Since(start)
100 if err != nil {
101 t.Fatalf("Pselect: %v", err)
102 }
103
104 if took < dur {
105 t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
106 }
107 }
108
View as plain text