syscall_linux_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 TestPoll(t *testing.T) {
  14. err := unix.Mkfifo("fifo", 0666)
  15. if err != nil {
  16. t.Errorf("Poll: failed to create FIFO: %v", err)
  17. return
  18. }
  19. defer os.Remove("fifo")
  20. f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
  21. if err != nil {
  22. t.Errorf("Poll: failed to open FIFO: %v", err)
  23. return
  24. }
  25. defer f.Close()
  26. const timeout = 100
  27. ok := make(chan bool, 1)
  28. go func() {
  29. select {
  30. case <-time.After(10 * timeout * time.Millisecond):
  31. t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
  32. case <-ok:
  33. }
  34. }()
  35. fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
  36. n, err := unix.Poll(fds, timeout)
  37. ok <- true
  38. if err != nil {
  39. t.Errorf("Poll: unexpected error: %v", err)
  40. return
  41. }
  42. if n != 0 {
  43. t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
  44. return
  45. }
  46. }
  47. func TestTime(t *testing.T) {
  48. var ut unix.Time_t
  49. ut2, err := unix.Time(&ut)
  50. if err != nil {
  51. t.Fatalf("Time: %v", err)
  52. }
  53. if ut != ut2 {
  54. t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
  55. }
  56. var now time.Time
  57. for i := 0; i < 10; i++ {
  58. ut, err = unix.Time(nil)
  59. if err != nil {
  60. t.Fatalf("Time: %v", err)
  61. }
  62. now = time.Now()
  63. if int64(ut) == now.Unix() {
  64. return
  65. }
  66. }
  67. t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
  68. }
  69. func TestUtime(t *testing.T) {
  70. defer chtmpdir(t)()
  71. touch(t, "file1")
  72. buf := &unix.Utimbuf{
  73. Modtime: 12345,
  74. }
  75. err := unix.Utime("file1", buf)
  76. if err != nil {
  77. t.Fatalf("Utime: %v", err)
  78. }
  79. fi, err := os.Stat("file1")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. if fi.ModTime().Unix() != 12345 {
  84. t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
  85. }
  86. }
  87. func TestGetrlimit(t *testing.T) {
  88. var rlim unix.Rlimit
  89. err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
  90. if err != nil {
  91. t.Fatalf("Getrlimit: %v", err)
  92. }
  93. }
  94. // utilities taken from os/os_test.go
  95. func touch(t *testing.T, name string) {
  96. f, err := os.Create(name)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if err := f.Close(); err != nil {
  101. t.Fatal(err)
  102. }
  103. }
  104. // chtmpdir changes the working directory to a new temporary directory and
  105. // provides a cleanup function. Used when PWD is read-only.
  106. func chtmpdir(t *testing.T) func() {
  107. oldwd, err := os.Getwd()
  108. if err != nil {
  109. t.Fatalf("chtmpdir: %v", err)
  110. }
  111. d, err := ioutil.TempDir("", "test")
  112. if err != nil {
  113. t.Fatalf("chtmpdir: %v", err)
  114. }
  115. if err := os.Chdir(d); err != nil {
  116. t.Fatalf("chtmpdir: %v", err)
  117. }
  118. return func() {
  119. if err := os.Chdir(oldwd); err != nil {
  120. t.Fatalf("chtmpdir: %v", err)
  121. }
  122. os.RemoveAll(d)
  123. }
  124. }