syscall_darwin_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. package unix_test
  5. import (
  6. "os"
  7. "testing"
  8. "golang.org/x/sys/unix"
  9. )
  10. // stringsFromByteSlice converts a sequence of attributes to a []string.
  11. // On Darwin, each entry is a NULL-terminated string.
  12. func stringsFromByteSlice(buf []byte) []string {
  13. var result []string
  14. off := 0
  15. for i, b := range buf {
  16. if b == 0 {
  17. result = append(result, string(buf[off:i]))
  18. off = i + 1
  19. }
  20. }
  21. return result
  22. }
  23. func TestUtimesNanoAt(t *testing.T) {
  24. defer chtmpdir(t)()
  25. symlink := "symlink1"
  26. os.Remove(symlink)
  27. err := os.Symlink("nonexisting", symlink)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. ts := []unix.Timespec{
  32. {Sec: 1111, Nsec: 2222},
  33. {Sec: 3333, Nsec: 4444},
  34. }
  35. err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
  36. if err != nil {
  37. t.Fatalf("UtimesNanoAt: %v", err)
  38. }
  39. var st unix.Stat_t
  40. err = unix.Lstat(symlink, &st)
  41. if err != nil {
  42. t.Fatalf("Lstat: %v", err)
  43. }
  44. // Only check Mtim, Atim might not be supported by the underlying filesystem
  45. expected := ts[1]
  46. if st.Mtim.Nsec == 0 {
  47. // Some filesystems only support 1-second time stamp resolution
  48. // and will always set Nsec to 0.
  49. expected.Nsec = 0
  50. }
  51. if st.Mtim != expected {
  52. t.Errorf("UtimesNanoAt: wrong mtime: got %v, expected %v", st.Mtim, expected)
  53. }
  54. }
  55. func TestSysctlClockinfo(t *testing.T) {
  56. ci, err := unix.SysctlClockinfo("kern.clockrate")
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. t.Logf("tick = %v, tickadj = %v, hz = %v, profhz = %v, stathz = %v",
  61. ci.Tick, ci.Tickadj, ci.Hz, ci.Profhz, ci.Stathz)
  62. }