syscall_aix_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2018 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 aix
  5. package unix_test
  6. import (
  7. "os"
  8. "runtime"
  9. "testing"
  10. "time"
  11. "golang.org/x/sys/unix"
  12. )
  13. func TestIoctlGetInt(t *testing.T) {
  14. f, err := os.Open("/dev/random")
  15. if err != nil {
  16. t.Fatalf("failed to open device: %v", err)
  17. }
  18. defer f.Close()
  19. v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
  20. if err != nil {
  21. t.Fatalf("failed to perform ioctl: %v", err)
  22. }
  23. t.Logf("%d bits of entropy available", v)
  24. }
  25. func TestTime(t *testing.T) {
  26. var ut unix.Time_t
  27. ut2, err := unix.Time(&ut)
  28. if err != nil {
  29. t.Fatalf("Time: %v", err)
  30. }
  31. if ut != ut2 {
  32. t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
  33. }
  34. var now time.Time
  35. for i := 0; i < 10; i++ {
  36. ut, err = unix.Time(nil)
  37. if err != nil {
  38. t.Fatalf("Time: %v", err)
  39. }
  40. now = time.Now()
  41. if int64(ut) == now.Unix() {
  42. return
  43. }
  44. }
  45. t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
  46. }
  47. func TestUtime(t *testing.T) {
  48. defer chtmpdir(t)()
  49. touch(t, "file1")
  50. buf := &unix.Utimbuf{
  51. Modtime: 12345,
  52. }
  53. err := unix.Utime("file1", buf)
  54. if err != nil {
  55. t.Fatalf("Utime: %v", err)
  56. }
  57. fi, err := os.Stat("file1")
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if fi.ModTime().Unix() != 12345 {
  62. t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
  63. }
  64. }
  65. func TestUtimesNanoAt(t *testing.T) {
  66. defer chtmpdir(t)()
  67. symlink := "symlink1"
  68. defer os.Remove(symlink)
  69. err := os.Symlink("nonexisting", symlink)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. ts := []unix.Timespec{
  74. {Sec: 1111, Nsec: 2222},
  75. {Sec: 3333, Nsec: 4444},
  76. }
  77. err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
  78. if err != nil {
  79. t.Fatalf("UtimesNanoAt: %v", err)
  80. }
  81. var st unix.Stat_t
  82. err = unix.Lstat(symlink, &st)
  83. if err != nil {
  84. t.Fatalf("Lstat: %v", err)
  85. }
  86. if runtime.GOARCH == "ppc64" {
  87. if int64(st.Atim.Sec) != int64(ts[0].Sec) || st.Atim.Nsec != ts[0].Nsec {
  88. t.Errorf("UtimesNanoAt: wrong atime: %v", st.Atim)
  89. }
  90. if int64(st.Mtim.Sec) != int64(ts[1].Sec) || st.Mtim.Nsec != ts[1].Nsec {
  91. t.Errorf("UtimesNanoAt: wrong mtime: %v", st.Mtim)
  92. }
  93. } else {
  94. if int32(st.Atim.Sec) != int32(ts[0].Sec) || int32(st.Atim.Nsec) != int32(ts[0].Nsec) {
  95. t.Errorf("UtimesNanoAt: wrong atime: %v", st.Atim)
  96. }
  97. if int32(st.Mtim.Sec) != int32(ts[1].Sec) || int32(st.Mtim.Nsec) != int32(ts[1].Nsec) {
  98. t.Errorf("UtimesNanoAt: wrong mtime: %v", st.Mtim)
  99. }
  100. }
  101. }
  102. func TestSelect(t *testing.T) {
  103. _, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
  104. if err != nil {
  105. t.Fatalf("Select: %v", err)
  106. }
  107. dur := 150 * time.Millisecond
  108. tv := unix.NsecToTimeval(int64(dur))
  109. start := time.Now()
  110. _, err = unix.Select(0, nil, nil, nil, &tv)
  111. took := time.Since(start)
  112. if err != nil {
  113. t.Fatalf("Select: %v", err)
  114. }
  115. if took < dur {
  116. t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
  117. }
  118. }
  119. func TestPselect(t *testing.T) {
  120. if runtime.GOARCH == "ppc64" {
  121. t.Skip("pselect issue with structure timespec on AIX 7.2 tl0, skipping test")
  122. }
  123. _, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
  124. if err != nil {
  125. t.Fatalf("Pselect: %v", err)
  126. }
  127. dur := 2500 * time.Microsecond
  128. ts := unix.NsecToTimespec(int64(dur))
  129. start := time.Now()
  130. _, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
  131. took := time.Since(start)
  132. if err != nil {
  133. t.Fatalf("Pselect: %v", err)
  134. }
  135. if took < dur {
  136. t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
  137. }
  138. }