github.com/maenmax/kairep@v0.0.0-20210218001208-55bf3df36788/src/golang.org/x/sys/unix/syscall_linux_test.go (about) 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 TestPoll(t *testing.T) { 19 f, cleanup := mktmpfifo(t) 20 defer cleanup() 21 22 const timeout = 100 23 24 ok := make(chan bool, 1) 25 go func() { 26 select { 27 case <-time.After(10 * timeout * time.Millisecond): 28 t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout) 29 case <-ok: 30 } 31 }() 32 33 fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}} 34 n, err := unix.Poll(fds, timeout) 35 ok <- true 36 if err != nil { 37 t.Errorf("Poll: unexpected error: %v", err) 38 return 39 } 40 if n != 0 { 41 t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0) 42 return 43 } 44 } 45 46 func TestPpoll(t *testing.T) { 47 f, cleanup := mktmpfifo(t) 48 defer cleanup() 49 50 const timeout = 100 * time.Millisecond 51 52 ok := make(chan bool, 1) 53 go func() { 54 select { 55 case <-time.After(10 * timeout): 56 t.Errorf("Ppoll: failed to timeout after %d", 10*timeout) 57 case <-ok: 58 } 59 }() 60 61 fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}} 62 timeoutTs := unix.NsecToTimespec(int64(timeout)) 63 n, err := unix.Ppoll(fds, &timeoutTs, nil) 64 ok <- true 65 if err != nil { 66 t.Errorf("Ppoll: unexpected error: %v", err) 67 return 68 } 69 if n != 0 { 70 t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0) 71 return 72 } 73 } 74 75 // mktmpfifo creates a temporary FIFO and provides a cleanup function. 76 func mktmpfifo(t *testing.T) (*os.File, func()) { 77 err := unix.Mkfifo("fifo", 0666) 78 if err != nil { 79 t.Fatalf("mktmpfifo: failed to create FIFO: %v", err) 80 } 81 82 f, err := os.OpenFile("fifo", os.O_RDWR, 0666) 83 if err != nil { 84 os.Remove("fifo") 85 t.Fatalf("mktmpfifo: failed to open FIFO: %v", err) 86 } 87 88 return f, func() { 89 f.Close() 90 os.Remove("fifo") 91 } 92 } 93 94 func TestTime(t *testing.T) { 95 var ut unix.Time_t 96 ut2, err := unix.Time(&ut) 97 if err != nil { 98 t.Fatalf("Time: %v", err) 99 } 100 if ut != ut2 { 101 t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut) 102 } 103 104 var now time.Time 105 106 for i := 0; i < 10; i++ { 107 ut, err = unix.Time(nil) 108 if err != nil { 109 t.Fatalf("Time: %v", err) 110 } 111 112 now = time.Now() 113 114 if int64(ut) == now.Unix() { 115 return 116 } 117 } 118 119 t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix()) 120 } 121 122 func TestUtime(t *testing.T) { 123 defer chtmpdir(t)() 124 125 touch(t, "file1") 126 127 buf := &unix.Utimbuf{ 128 Modtime: 12345, 129 } 130 131 err := unix.Utime("file1", buf) 132 if err != nil { 133 t.Fatalf("Utime: %v", err) 134 } 135 136 fi, err := os.Stat("file1") 137 if err != nil { 138 t.Fatal(err) 139 } 140 141 if fi.ModTime().Unix() != 12345 { 142 t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix()) 143 } 144 } 145 146 func TestGetrlimit(t *testing.T) { 147 var rlim unix.Rlimit 148 err := unix.Getrlimit(unix.RLIMIT_AS, &rlim) 149 if err != nil { 150 t.Fatalf("Getrlimit: %v", err) 151 } 152 } 153 154 // utilities taken from os/os_test.go 155 156 func touch(t *testing.T, name string) { 157 f, err := os.Create(name) 158 if err != nil { 159 t.Fatal(err) 160 } 161 if err := f.Close(); err != nil { 162 t.Fatal(err) 163 } 164 } 165 166 // chtmpdir changes the working directory to a new temporary directory and 167 // provides a cleanup function. Used when PWD is read-only. 168 func chtmpdir(t *testing.T) func() { 169 oldwd, err := os.Getwd() 170 if err != nil { 171 t.Fatalf("chtmpdir: %v", err) 172 } 173 d, err := ioutil.TempDir("", "test") 174 if err != nil { 175 t.Fatalf("chtmpdir: %v", err) 176 } 177 if err := os.Chdir(d); err != nil { 178 t.Fatalf("chtmpdir: %v", err) 179 } 180 return func() { 181 if err := os.Chdir(oldwd); err != nil { 182 t.Fatalf("chtmpdir: %v", err) 183 } 184 os.RemoveAll(d) 185 } 186 }