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
7package unix_test
8
9import (
10	"os"
11	"runtime"
12	"testing"
13	"time"
14
15	"golang.org/x/sys/unix"
16)
17
18func 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
33func 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
61func 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
85func 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 != int32(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 != int32(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
126func TestPselect(t *testing.T) {
127	if runtime.GOARCH == "ppc64" {
128		t.Skip("pselect issue with structure timespec on AIX 7.2 tl0, skipping test")
129	}
130
131	_, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
132	if err != nil {
133		t.Fatalf("Pselect: %v", err)
134	}
135
136	dur := 2500 * time.Microsecond
137	ts := unix.NsecToTimespec(int64(dur))
138	start := time.Now()
139	_, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
140	took := time.Since(start)
141	if err != nil {
142		t.Fatalf("Pselect: %v", err)
143	}
144
145	if took < dur {
146		t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
147	}
148}
149
150// stringsFromByteSlice converts a sequence of attributes to a []string.
151// On Linux, each entry is a NULL-terminated string.
152func stringsFromByteSlice(buf []byte) []string {
153	var result []string
154	off := 0
155	for i, b := range buf {
156		if b == 0 {
157			result = append(result, string(buf[off:i]))
158			off = i + 1
159		}
160	}
161	return result
162}
163