選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

422 行
9.8 KiB

  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. "os"
  8. "runtime"
  9. "runtime/debug"
  10. "testing"
  11. "time"
  12. "golang.org/x/sys/unix"
  13. )
  14. func TestIoctlGetInt(t *testing.T) {
  15. f, err := os.Open("/dev/random")
  16. if err != nil {
  17. t.Fatalf("failed to open device: %v", err)
  18. }
  19. defer f.Close()
  20. v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
  21. if err != nil {
  22. t.Fatalf("failed to perform ioctl: %v", err)
  23. }
  24. t.Logf("%d bits of entropy available", v)
  25. }
  26. func TestPpoll(t *testing.T) {
  27. if runtime.GOOS == "android" {
  28. t.Skip("mkfifo syscall is not available on android, skipping test")
  29. }
  30. f, cleanup := mktmpfifo(t)
  31. defer cleanup()
  32. const timeout = 100 * time.Millisecond
  33. ok := make(chan bool, 1)
  34. go func() {
  35. select {
  36. case <-time.After(10 * timeout):
  37. t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
  38. case <-ok:
  39. }
  40. }()
  41. fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
  42. timeoutTs := unix.NsecToTimespec(int64(timeout))
  43. n, err := unix.Ppoll(fds, &timeoutTs, nil)
  44. ok <- true
  45. if err != nil {
  46. t.Errorf("Ppoll: unexpected error: %v", err)
  47. return
  48. }
  49. if n != 0 {
  50. t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
  51. return
  52. }
  53. }
  54. func TestTime(t *testing.T) {
  55. var ut unix.Time_t
  56. ut2, err := unix.Time(&ut)
  57. if err != nil {
  58. t.Fatalf("Time: %v", err)
  59. }
  60. if ut != ut2 {
  61. t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
  62. }
  63. var now time.Time
  64. for i := 0; i < 10; i++ {
  65. ut, err = unix.Time(nil)
  66. if err != nil {
  67. t.Fatalf("Time: %v", err)
  68. }
  69. now = time.Now()
  70. if int64(ut) == now.Unix() {
  71. return
  72. }
  73. }
  74. t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
  75. }
  76. func TestUtime(t *testing.T) {
  77. defer chtmpdir(t)()
  78. touch(t, "file1")
  79. buf := &unix.Utimbuf{
  80. Modtime: 12345,
  81. }
  82. err := unix.Utime("file1", buf)
  83. if err != nil {
  84. t.Fatalf("Utime: %v", err)
  85. }
  86. fi, err := os.Stat("file1")
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. if fi.ModTime().Unix() != 12345 {
  91. t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
  92. }
  93. }
  94. func TestUtimesNanoAt(t *testing.T) {
  95. defer chtmpdir(t)()
  96. symlink := "symlink1"
  97. os.Remove(symlink)
  98. err := os.Symlink("nonexisting", symlink)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. ts := []unix.Timespec{
  103. {Sec: 1111, Nsec: 2222},
  104. {Sec: 3333, Nsec: 4444},
  105. }
  106. err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
  107. if err != nil {
  108. t.Fatalf("UtimesNanoAt: %v", err)
  109. }
  110. var st unix.Stat_t
  111. err = unix.Lstat(symlink, &st)
  112. if err != nil {
  113. t.Fatalf("Lstat: %v", err)
  114. }
  115. // Only check Mtim, Atim might not be supported by the underlying filesystem
  116. expected := ts[1]
  117. if st.Mtim.Nsec == 0 {
  118. // Some filesystems only support 1-second time stamp resolution
  119. // and will always set Nsec to 0.
  120. expected.Nsec = 0
  121. }
  122. if st.Mtim != expected {
  123. t.Errorf("UtimesNanoAt: wrong mtime: expected %v, got %v", expected, st.Mtim)
  124. }
  125. }
  126. func TestRlimitAs(t *testing.T) {
  127. // disable GC during to avoid flaky test
  128. defer debug.SetGCPercent(debug.SetGCPercent(-1))
  129. var rlim unix.Rlimit
  130. err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
  131. if err != nil {
  132. t.Fatalf("Getrlimit: %v", err)
  133. }
  134. var zero unix.Rlimit
  135. if zero == rlim {
  136. t.Fatalf("Getrlimit: got zero value %#v", rlim)
  137. }
  138. set := rlim
  139. set.Cur = uint64(unix.Getpagesize())
  140. err = unix.Setrlimit(unix.RLIMIT_AS, &set)
  141. if err != nil {
  142. t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
  143. }
  144. // RLIMIT_AS was set to the page size, so mmap()'ing twice the page size
  145. // should fail. See 'man 2 getrlimit'.
  146. _, err = unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
  147. if err == nil {
  148. t.Fatal("Mmap: unexpectedly suceeded after setting RLIMIT_AS")
  149. }
  150. err = unix.Setrlimit(unix.RLIMIT_AS, &rlim)
  151. if err != nil {
  152. t.Fatalf("Setrlimit: restore failed: %#v %v", rlim, err)
  153. }
  154. b, err := unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
  155. if err != nil {
  156. t.Fatalf("Mmap: %v", err)
  157. }
  158. err = unix.Munmap(b)
  159. if err != nil {
  160. t.Fatalf("Munmap: %v", err)
  161. }
  162. }
  163. func TestSelect(t *testing.T) {
  164. _, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
  165. if err != nil {
  166. t.Fatalf("Select: %v", err)
  167. }
  168. dur := 150 * time.Millisecond
  169. tv := unix.NsecToTimeval(int64(dur))
  170. start := time.Now()
  171. _, err = unix.Select(0, nil, nil, nil, &tv)
  172. took := time.Since(start)
  173. if err != nil {
  174. t.Fatalf("Select: %v", err)
  175. }
  176. if took < dur {
  177. t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
  178. }
  179. }
  180. func TestPselect(t *testing.T) {
  181. _, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
  182. if err != nil {
  183. t.Fatalf("Pselect: %v", err)
  184. }
  185. dur := 2500 * time.Microsecond
  186. ts := unix.NsecToTimespec(int64(dur))
  187. start := time.Now()
  188. _, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
  189. took := time.Since(start)
  190. if err != nil {
  191. t.Fatalf("Pselect: %v", err)
  192. }
  193. if took < dur {
  194. t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
  195. }
  196. }
  197. func TestSchedSetaffinity(t *testing.T) {
  198. runtime.LockOSThread()
  199. defer runtime.UnlockOSThread()
  200. var oldMask unix.CPUSet
  201. err := unix.SchedGetaffinity(0, &oldMask)
  202. if err != nil {
  203. t.Fatalf("SchedGetaffinity: %v", err)
  204. }
  205. var newMask unix.CPUSet
  206. newMask.Zero()
  207. if newMask.Count() != 0 {
  208. t.Errorf("CpuZero: didn't zero CPU set: %v", newMask)
  209. }
  210. cpu := 1
  211. newMask.Set(cpu)
  212. if newMask.Count() != 1 || !newMask.IsSet(cpu) {
  213. t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
  214. }
  215. cpu = 5
  216. newMask.Set(cpu)
  217. if newMask.Count() != 2 || !newMask.IsSet(cpu) {
  218. t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
  219. }
  220. newMask.Clear(cpu)
  221. if newMask.Count() != 1 || newMask.IsSet(cpu) {
  222. t.Errorf("CpuClr: didn't clear CPU %d in set: %v", cpu, newMask)
  223. }
  224. if runtime.NumCPU() < 2 {
  225. t.Skip("skipping setaffinity tests on single CPU system")
  226. }
  227. if runtime.GOOS == "android" {
  228. t.Skip("skipping setaffinity tests on android")
  229. }
  230. err = unix.SchedSetaffinity(0, &newMask)
  231. if err != nil {
  232. t.Fatalf("SchedSetaffinity: %v", err)
  233. }
  234. var gotMask unix.CPUSet
  235. err = unix.SchedGetaffinity(0, &gotMask)
  236. if err != nil {
  237. t.Fatalf("SchedGetaffinity: %v", err)
  238. }
  239. if gotMask != newMask {
  240. t.Errorf("SchedSetaffinity: returned affinity mask does not match set affinity mask")
  241. }
  242. // Restore old mask so it doesn't affect successive tests
  243. err = unix.SchedSetaffinity(0, &oldMask)
  244. if err != nil {
  245. t.Fatalf("SchedSetaffinity: %v", err)
  246. }
  247. }
  248. func TestStatx(t *testing.T) {
  249. var stx unix.Statx_t
  250. err := unix.Statx(unix.AT_FDCWD, ".", 0, 0, &stx)
  251. if err == unix.ENOSYS || err == unix.EPERM {
  252. t.Skip("statx syscall is not available, skipping test")
  253. } else if err != nil {
  254. t.Fatalf("Statx: %v", err)
  255. }
  256. defer chtmpdir(t)()
  257. touch(t, "file1")
  258. var st unix.Stat_t
  259. err = unix.Stat("file1", &st)
  260. if err != nil {
  261. t.Fatalf("Stat: %v", err)
  262. }
  263. flags := unix.AT_STATX_SYNC_AS_STAT
  264. err = unix.Statx(unix.AT_FDCWD, "file1", flags, unix.STATX_ALL, &stx)
  265. if err != nil {
  266. t.Fatalf("Statx: %v", err)
  267. }
  268. if uint32(stx.Mode) != st.Mode {
  269. t.Errorf("Statx: returned stat mode does not match Stat")
  270. }
  271. ctime := unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
  272. mtime := unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
  273. if stx.Ctime != ctime {
  274. t.Errorf("Statx: returned stat ctime does not match Stat")
  275. }
  276. if stx.Mtime != mtime {
  277. t.Errorf("Statx: returned stat mtime does not match Stat")
  278. }
  279. err = os.Symlink("file1", "symlink1")
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. err = unix.Lstat("symlink1", &st)
  284. if err != nil {
  285. t.Fatalf("Lstat: %v", err)
  286. }
  287. err = unix.Statx(unix.AT_FDCWD, "symlink1", flags, unix.STATX_BASIC_STATS, &stx)
  288. if err != nil {
  289. t.Fatalf("Statx: %v", err)
  290. }
  291. // follow symlink, expect a regulat file
  292. if stx.Mode&unix.S_IFREG == 0 {
  293. t.Errorf("Statx: didn't follow symlink")
  294. }
  295. err = unix.Statx(unix.AT_FDCWD, "symlink1", flags|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ALL, &stx)
  296. if err != nil {
  297. t.Fatalf("Statx: %v", err)
  298. }
  299. // follow symlink, expect a symlink
  300. if stx.Mode&unix.S_IFLNK == 0 {
  301. t.Errorf("Statx: unexpectedly followed symlink")
  302. }
  303. if uint32(stx.Mode) != st.Mode {
  304. t.Errorf("Statx: returned stat mode does not match Lstat")
  305. }
  306. ctime = unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
  307. mtime = unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
  308. if stx.Ctime != ctime {
  309. t.Errorf("Statx: returned stat ctime does not match Lstat")
  310. }
  311. if stx.Mtime != mtime {
  312. t.Errorf("Statx: returned stat mtime does not match Lstat")
  313. }
  314. }
  315. // stringsFromByteSlice converts a sequence of attributes to a []string.
  316. // On Linux, each entry is a NULL-terminated string.
  317. func stringsFromByteSlice(buf []byte) []string {
  318. var result []string
  319. off := 0
  320. for i, b := range buf {
  321. if b == 0 {
  322. result = append(result, string(buf[off:i]))
  323. off = i + 1
  324. }
  325. }
  326. return result
  327. }
  328. func TestFaccessat(t *testing.T) {
  329. defer chtmpdir(t)()
  330. touch(t, "file1")
  331. err := unix.Faccessat(unix.AT_FDCWD, "file1", unix.O_RDONLY, 0)
  332. if err != nil {
  333. t.Errorf("Faccessat: unexpected error: %v", err)
  334. }
  335. err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.O_RDONLY, 2)
  336. if err != unix.EINVAL {
  337. t.Errorf("Faccessat: unexpected error: %v, want EINVAL", err)
  338. }
  339. err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.O_RDONLY, unix.AT_EACCESS)
  340. if err != unix.EOPNOTSUPP {
  341. t.Errorf("Faccessat: unexpected error: %v, want EOPNOTSUPP", err)
  342. }
  343. err = os.Symlink("file1", "symlink1")
  344. if err != nil {
  345. t.Fatal(err)
  346. }
  347. err = unix.Faccessat(unix.AT_FDCWD, "symlink1", unix.O_RDONLY, unix.AT_SYMLINK_NOFOLLOW)
  348. if err != unix.EOPNOTSUPP {
  349. t.Errorf("Faccessat: unexpected error: %v, want EOPNOTSUPP", err)
  350. }
  351. }