syscall_linux_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 TestTime(t *testing.T) {
  14. var ut unix.Time_t
  15. ut2, err := unix.Time(&ut)
  16. if err != nil {
  17. t.Fatalf("Time: %v", err)
  18. }
  19. if ut != ut2 {
  20. t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
  21. }
  22. var now time.Time
  23. for i := 0; i < 10; i++ {
  24. ut, err = unix.Time(nil)
  25. if err != nil {
  26. t.Fatalf("Time: %v", err)
  27. }
  28. now = time.Now()
  29. if int64(ut) == now.Unix() {
  30. return
  31. }
  32. }
  33. t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
  34. }
  35. func TestUtime(t *testing.T) {
  36. defer chtmpdir(t)()
  37. touch(t, "file1")
  38. buf := &unix.Utimbuf{
  39. Modtime: 12345,
  40. }
  41. err := unix.Utime("file1", buf)
  42. if err != nil {
  43. t.Fatalf("Utime: %v", err)
  44. }
  45. fi, err := os.Stat("file1")
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. if fi.ModTime().Unix() != 12345 {
  50. t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
  51. }
  52. }
  53. func TestGetrlimit(t *testing.T) {
  54. var rlim unix.Rlimit
  55. err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
  56. if err != nil {
  57. t.Fatalf("Getrlimit: %v", err)
  58. }
  59. }
  60. // utilities taken from os/os_test.go
  61. func touch(t *testing.T, name string) {
  62. f, err := os.Create(name)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if err := f.Close(); err != nil {
  67. t.Fatal(err)
  68. }
  69. }
  70. // chtmpdir changes the working directory to a new temporary directory and
  71. // provides a cleanup function. Used when PWD is read-only.
  72. func chtmpdir(t *testing.T) func() {
  73. oldwd, err := os.Getwd()
  74. if err != nil {
  75. t.Fatalf("chtmpdir: %v", err)
  76. }
  77. d, err := ioutil.TempDir("", "test")
  78. if err != nil {
  79. t.Fatalf("chtmpdir: %v", err)
  80. }
  81. if err := os.Chdir(d); err != nil {
  82. t.Fatalf("chtmpdir: %v", err)
  83. }
  84. return func() {
  85. if err := os.Chdir(oldwd); err != nil {
  86. t.Fatalf("chtmpdir: %v", err)
  87. }
  88. os.RemoveAll(d)
  89. }
  90. }