syscall_linux_test.go 4.7 KB

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