OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / sys / unix / syscall_linux_test.go
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         "io/ioutil"
11         "os"
12         "testing"
13         "time"
14
15         "golang.org/x/sys/unix"
16 )
17
18 func TestFchmodat(t *testing.T) {
19         defer chtmpdir(t)()
20
21         touch(t, "file1")
22         os.Symlink("file1", "symlink1")
23
24         err := unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, 0)
25         if err != nil {
26                 t.Fatalf("Fchmodat: unexpected error: %v", err)
27         }
28
29         fi, err := os.Stat("file1")
30         if err != nil {
31                 t.Fatal(err)
32         }
33
34         if fi.Mode() != 0444 {
35                 t.Errorf("Fchmodat: failed to change mode: expected %v, got %v", 0444, fi.Mode())
36         }
37
38         err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, unix.AT_SYMLINK_NOFOLLOW)
39         if err != unix.EOPNOTSUPP {
40                 t.Fatalf("Fchmodat: unexpected error: %v, expected EOPNOTSUPP", err)
41         }
42 }
43
44 func TestIoctlGetInt(t *testing.T) {
45         f, err := os.Open("/dev/random")
46         if err != nil {
47                 t.Fatalf("failed to open device: %v", err)
48         }
49         defer f.Close()
50
51         v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
52         if err != nil {
53                 t.Fatalf("failed to perform ioctl: %v", err)
54         }
55
56         t.Logf("%d bits of entropy available", v)
57 }
58
59 func TestPoll(t *testing.T) {
60         f, cleanup := mktmpfifo(t)
61         defer cleanup()
62
63         const timeout = 100
64
65         ok := make(chan bool, 1)
66         go func() {
67                 select {
68                 case <-time.After(10 * timeout * time.Millisecond):
69                         t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
70                 case <-ok:
71                 }
72         }()
73
74         fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
75         n, err := unix.Poll(fds, timeout)
76         ok <- true
77         if err != nil {
78                 t.Errorf("Poll: unexpected error: %v", err)
79                 return
80         }
81         if n != 0 {
82                 t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
83                 return
84         }
85 }
86
87 func TestPpoll(t *testing.T) {
88         f, cleanup := mktmpfifo(t)
89         defer cleanup()
90
91         const timeout = 100 * time.Millisecond
92
93         ok := make(chan bool, 1)
94         go func() {
95                 select {
96                 case <-time.After(10 * timeout):
97                         t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
98                 case <-ok:
99                 }
100         }()
101
102         fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
103         timeoutTs := unix.NsecToTimespec(int64(timeout))
104         n, err := unix.Ppoll(fds, &timeoutTs, nil)
105         ok <- true
106         if err != nil {
107                 t.Errorf("Ppoll: unexpected error: %v", err)
108                 return
109         }
110         if n != 0 {
111                 t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
112                 return
113         }
114 }
115
116 // mktmpfifo creates a temporary FIFO and provides a cleanup function.
117 func mktmpfifo(t *testing.T) (*os.File, func()) {
118         err := unix.Mkfifo("fifo", 0666)
119         if err != nil {
120                 t.Fatalf("mktmpfifo: failed to create FIFO: %v", err)
121         }
122
123         f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
124         if err != nil {
125                 os.Remove("fifo")
126                 t.Fatalf("mktmpfifo: failed to open FIFO: %v", err)
127         }
128
129         return f, func() {
130                 f.Close()
131                 os.Remove("fifo")
132         }
133 }
134
135 func TestTime(t *testing.T) {
136         var ut unix.Time_t
137         ut2, err := unix.Time(&ut)
138         if err != nil {
139                 t.Fatalf("Time: %v", err)
140         }
141         if ut != ut2 {
142                 t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
143         }
144
145         var now time.Time
146
147         for i := 0; i < 10; i++ {
148                 ut, err = unix.Time(nil)
149                 if err != nil {
150                         t.Fatalf("Time: %v", err)
151                 }
152
153                 now = time.Now()
154
155                 if int64(ut) == now.Unix() {
156                         return
157                 }
158         }
159
160         t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
161 }
162
163 func TestUtime(t *testing.T) {
164         defer chtmpdir(t)()
165
166         touch(t, "file1")
167
168         buf := &unix.Utimbuf{
169                 Modtime: 12345,
170         }
171
172         err := unix.Utime("file1", buf)
173         if err != nil {
174                 t.Fatalf("Utime: %v", err)
175         }
176
177         fi, err := os.Stat("file1")
178         if err != nil {
179                 t.Fatal(err)
180         }
181
182         if fi.ModTime().Unix() != 12345 {
183                 t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
184         }
185 }
186
187 func TestGetrlimit(t *testing.T) {
188         var rlim unix.Rlimit
189         err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
190         if err != nil {
191                 t.Fatalf("Getrlimit: %v", err)
192         }
193 }
194
195 // utilities taken from os/os_test.go
196
197 func touch(t *testing.T, name string) {
198         f, err := os.Create(name)
199         if err != nil {
200                 t.Fatal(err)
201         }
202         if err := f.Close(); err != nil {
203                 t.Fatal(err)
204         }
205 }
206
207 // chtmpdir changes the working directory to a new temporary directory and
208 // provides a cleanup function. Used when PWD is read-only.
209 func chtmpdir(t *testing.T) func() {
210         oldwd, err := os.Getwd()
211         if err != nil {
212                 t.Fatalf("chtmpdir: %v", err)
213         }
214         d, err := ioutil.TempDir("", "test")
215         if err != nil {
216                 t.Fatalf("chtmpdir: %v", err)
217         }
218         if err := os.Chdir(d); err != nil {
219                 t.Fatalf("chtmpdir: %v", err)
220         }
221         return func() {
222                 if err := os.Chdir(oldwd); err != nil {
223                         t.Fatalf("chtmpdir: %v", err)
224                 }
225                 os.RemoveAll(d)
226         }
227 }