You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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