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//go:build aix
6// +build aix
7
8package unix_test
9
10import (
11	"os"
12	"runtime"
13	"testing"
14	"time"
15
16	"golang.org/x/sys/unix"
17)
18
19func 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
34func TestTime(t *testing.T) {
35	var ut unix.Time_t
36	ut2, err := unix.Time(&ut)
37	if err != nil {
38		t.Fatalf("Time: %v", err)
39	}
40	if ut != ut2 {
41		t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
42	}
43
44	var now time.Time
45
46	for i := 0; i < 10; i++ {
47		ut, err = unix.Time(nil)
48		if err != nil {
49			t.Fatalf("Time: %v", err)
50		}
51
52		now = time.Now()
53		diff := int64(ut) - now.Unix()
54		if -1 <= diff && diff <= 1 {
55			return
56		}
57	}
58
59	t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v±1", ut, now.Unix())
60}
61
62func TestUtime(t *testing.T) {
63	defer chtmpdir(t)()
64
65	touch(t, "file1")
66
67	buf := &unix.Utimbuf{
68		Modtime: 12345,
69	}
70
71	err := unix.Utime("file1", buf)
72	if err != nil {
73		t.Fatalf("Utime: %v", err)
74	}
75
76	fi, err := os.Stat("file1")
77	if err != nil {
78		t.Fatal(err)
79	}
80
81	if fi.ModTime().Unix() != 12345 {
82		t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
83	}
84}
85
86func TestPselect(t *testing.T) {
87	if runtime.GOARCH == "ppc64" {
88		t.Skip("pselect issue with structure timespec on AIX 7.2 tl0, skipping test")
89	}
90
91	_, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
92	if err != nil {
93		t.Fatalf("Pselect: %v", err)
94	}
95
96	dur := 2500 * time.Microsecond
97	ts := unix.NsecToTimespec(int64(dur))
98	start := time.Now()
99	_, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
100	took := time.Since(start)
101	if err != nil {
102		t.Fatalf("Pselect: %v", err)
103	}
104
105	if took < dur {
106		t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
107	}
108}
109