syscall_bsd_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2014 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 darwin dragonfly freebsd openbsd
  5. package unix_test
  6. import (
  7. "os"
  8. "os/exec"
  9. "runtime"
  10. "testing"
  11. "time"
  12. "golang.org/x/sys/unix"
  13. )
  14. func TestGetfsstat(t *testing.T) {
  15. n, err := unix.Getfsstat(nil, unix.MNT_NOWAIT)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. data := make([]unix.Statfs_t, n)
  20. n2, err := unix.Getfsstat(data, unix.MNT_NOWAIT)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. if n != n2 {
  25. t.Errorf("Getfsstat(nil) = %d, but subsequent Getfsstat(slice) = %d", n, n2)
  26. }
  27. for i, stat := range data {
  28. if stat == (unix.Statfs_t{}) {
  29. t.Errorf("index %v is an empty Statfs_t struct", i)
  30. }
  31. }
  32. if t.Failed() {
  33. for i, stat := range data[:n2] {
  34. t.Logf("data[%v] = %+v", i, stat)
  35. }
  36. mount, err := exec.Command("mount").CombinedOutput()
  37. if err != nil {
  38. t.Logf("mount: %v\n%s", err, mount)
  39. } else {
  40. t.Logf("mount: %s", mount)
  41. }
  42. }
  43. }
  44. func TestSelect(t *testing.T) {
  45. n, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
  46. if err != nil {
  47. t.Fatalf("Select: %v", err)
  48. }
  49. if n != 0 {
  50. t.Fatalf("Select: expected 0 ready file descriptors, got %v", n)
  51. }
  52. dur := 250 * time.Millisecond
  53. tv := unix.NsecToTimeval(int64(dur))
  54. start := time.Now()
  55. n, err = unix.Select(0, nil, nil, nil, &tv)
  56. took := time.Since(start)
  57. if err != nil {
  58. t.Fatalf("Select: %v", err)
  59. }
  60. if n != 0 {
  61. t.Fatalf("Select: expected 0 ready file descriptors, got %v", n)
  62. }
  63. // On some BSDs the actual timeout might also be slightly less than the requested.
  64. // Add an acceptable margin to avoid flaky tests.
  65. if took < dur*2/3 {
  66. t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
  67. }
  68. rr, ww, err := os.Pipe()
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. defer rr.Close()
  73. defer ww.Close()
  74. if _, err := ww.Write([]byte("HELLO GOPHER")); err != nil {
  75. t.Fatal(err)
  76. }
  77. rFdSet := &unix.FdSet{}
  78. fd := rr.Fd()
  79. // FD_SET(fd, rFdSet)
  80. rFdSet.Bits[fd/unix.NFDBITS] |= (1 << (fd % unix.NFDBITS))
  81. n, err = unix.Select(int(fd+1), rFdSet, nil, nil, nil)
  82. if err != nil {
  83. t.Fatalf("Select: %v", err)
  84. }
  85. if n != 1 {
  86. t.Fatalf("Select: expected 1 ready file descriptors, got %v", n)
  87. }
  88. }
  89. func TestSysctlRaw(t *testing.T) {
  90. if runtime.GOOS == "openbsd" {
  91. t.Skip("kern.proc.pid does not exist on OpenBSD")
  92. }
  93. _, err := unix.SysctlRaw("kern.proc.pid", unix.Getpid())
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. }
  98. func TestSysctlUint32(t *testing.T) {
  99. maxproc, err := unix.SysctlUint32("kern.maxproc")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. t.Logf("kern.maxproc: %v", maxproc)
  104. }