syscall_unix_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. // Copyright 2013 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 darwin dragonfly freebsd linux netbsd openbsd solaris
  5. package unix_test
  6. import (
  7. "flag"
  8. "fmt"
  9. "io/ioutil"
  10. "net"
  11. "os"
  12. "os/exec"
  13. "path/filepath"
  14. "runtime"
  15. "syscall"
  16. "testing"
  17. "time"
  18. "golang.org/x/sys/unix"
  19. )
  20. // Tests that below functions, structures and constants are consistent
  21. // on all Unix-like systems.
  22. func _() {
  23. // program scheduling priority functions and constants
  24. var (
  25. _ func(int, int, int) error = unix.Setpriority
  26. _ func(int, int) (int, error) = unix.Getpriority
  27. )
  28. const (
  29. _ int = unix.PRIO_USER
  30. _ int = unix.PRIO_PROCESS
  31. _ int = unix.PRIO_PGRP
  32. )
  33. // termios constants
  34. const (
  35. _ int = unix.TCIFLUSH
  36. _ int = unix.TCIOFLUSH
  37. _ int = unix.TCOFLUSH
  38. )
  39. // fcntl file locking structure and constants
  40. var (
  41. _ = unix.Flock_t{
  42. Type: int16(0),
  43. Whence: int16(0),
  44. Start: int64(0),
  45. Len: int64(0),
  46. Pid: int32(0),
  47. }
  48. )
  49. const (
  50. _ = unix.F_GETLK
  51. _ = unix.F_SETLK
  52. _ = unix.F_SETLKW
  53. )
  54. }
  55. func TestErrnoSignalName(t *testing.T) {
  56. testErrors := []struct {
  57. num syscall.Errno
  58. name string
  59. }{
  60. {syscall.EPERM, "EPERM"},
  61. {syscall.EINVAL, "EINVAL"},
  62. {syscall.ENOENT, "ENOENT"},
  63. }
  64. for _, te := range testErrors {
  65. t.Run(fmt.Sprintf("%d/%s", te.num, te.name), func(t *testing.T) {
  66. e := unix.ErrnoName(te.num)
  67. if e != te.name {
  68. t.Errorf("ErrnoName(%d) returned %s, want %s", te.num, e, te.name)
  69. }
  70. })
  71. }
  72. testSignals := []struct {
  73. num syscall.Signal
  74. name string
  75. }{
  76. {syscall.SIGHUP, "SIGHUP"},
  77. {syscall.SIGPIPE, "SIGPIPE"},
  78. {syscall.SIGSEGV, "SIGSEGV"},
  79. }
  80. for _, ts := range testSignals {
  81. t.Run(fmt.Sprintf("%d/%s", ts.num, ts.name), func(t *testing.T) {
  82. s := unix.SignalName(ts.num)
  83. if s != ts.name {
  84. t.Errorf("SignalName(%d) returned %s, want %s", ts.num, s, ts.name)
  85. }
  86. })
  87. }
  88. }
  89. func TestFcntlInt(t *testing.T) {
  90. t.Parallel()
  91. file, err := ioutil.TempFile("", "TestFnctlInt")
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. defer os.Remove(file.Name())
  96. defer file.Close()
  97. f := file.Fd()
  98. flags, err := unix.FcntlInt(f, unix.F_GETFD, 0)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. if flags&unix.FD_CLOEXEC == 0 {
  103. t.Errorf("flags %#x do not include FD_CLOEXEC", flags)
  104. }
  105. }
  106. // TestFcntlFlock tests whether the file locking structure matches
  107. // the calling convention of each kernel.
  108. func TestFcntlFlock(t *testing.T) {
  109. name := filepath.Join(os.TempDir(), "TestFcntlFlock")
  110. fd, err := unix.Open(name, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0)
  111. if err != nil {
  112. t.Fatalf("Open failed: %v", err)
  113. }
  114. defer unix.Unlink(name)
  115. defer unix.Close(fd)
  116. flock := unix.Flock_t{
  117. Type: unix.F_RDLCK,
  118. Start: 0, Len: 0, Whence: 1,
  119. }
  120. if err := unix.FcntlFlock(uintptr(fd), unix.F_GETLK, &flock); err != nil {
  121. t.Fatalf("FcntlFlock failed: %v", err)
  122. }
  123. }
  124. // TestPassFD tests passing a file descriptor over a Unix socket.
  125. //
  126. // This test involved both a parent and child process. The parent
  127. // process is invoked as a normal test, with "go test", which then
  128. // runs the child process by running the current test binary with args
  129. // "-test.run=^TestPassFD$" and an environment variable used to signal
  130. // that the test should become the child process instead.
  131. func TestPassFD(t *testing.T) {
  132. if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
  133. t.Skip("cannot exec subprocess on iOS, skipping test")
  134. }
  135. if runtime.GOOS == "aix" {
  136. t.Skip("getsockname issue on AIX 7.2 tl1, skipping test")
  137. }
  138. if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
  139. passFDChild()
  140. return
  141. }
  142. tempDir, err := ioutil.TempDir("", "TestPassFD")
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. defer os.RemoveAll(tempDir)
  147. fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0)
  148. if err != nil {
  149. t.Fatalf("Socketpair: %v", err)
  150. }
  151. defer unix.Close(fds[0])
  152. defer unix.Close(fds[1])
  153. writeFile := os.NewFile(uintptr(fds[0]), "child-writes")
  154. readFile := os.NewFile(uintptr(fds[1]), "parent-reads")
  155. defer writeFile.Close()
  156. defer readFile.Close()
  157. cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir)
  158. cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
  159. if lp := os.Getenv("LD_LIBRARY_PATH"); lp != "" {
  160. cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+lp)
  161. }
  162. cmd.ExtraFiles = []*os.File{writeFile}
  163. out, err := cmd.CombinedOutput()
  164. if len(out) > 0 || err != nil {
  165. t.Fatalf("child process: %q, %v", out, err)
  166. }
  167. c, err := net.FileConn(readFile)
  168. if err != nil {
  169. t.Fatalf("FileConn: %v", err)
  170. }
  171. defer c.Close()
  172. uc, ok := c.(*net.UnixConn)
  173. if !ok {
  174. t.Fatalf("unexpected FileConn type; expected UnixConn, got %T", c)
  175. }
  176. buf := make([]byte, 32) // expect 1 byte
  177. oob := make([]byte, 32) // expect 24 bytes
  178. closeUnix := time.AfterFunc(5*time.Second, func() {
  179. t.Logf("timeout reading from unix socket")
  180. uc.Close()
  181. })
  182. _, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
  183. if err != nil {
  184. t.Fatalf("ReadMsgUnix: %v", err)
  185. }
  186. closeUnix.Stop()
  187. scms, err := unix.ParseSocketControlMessage(oob[:oobn])
  188. if err != nil {
  189. t.Fatalf("ParseSocketControlMessage: %v", err)
  190. }
  191. if len(scms) != 1 {
  192. t.Fatalf("expected 1 SocketControlMessage; got scms = %#v", scms)
  193. }
  194. scm := scms[0]
  195. gotFds, err := unix.ParseUnixRights(&scm)
  196. if err != nil {
  197. t.Fatalf("unix.ParseUnixRights: %v", err)
  198. }
  199. if len(gotFds) != 1 {
  200. t.Fatalf("wanted 1 fd; got %#v", gotFds)
  201. }
  202. f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
  203. defer f.Close()
  204. got, err := ioutil.ReadAll(f)
  205. want := "Hello from child process!\n"
  206. if string(got) != want {
  207. t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)
  208. }
  209. }
  210. // passFDChild is the child process used by TestPassFD.
  211. func passFDChild() {
  212. defer os.Exit(0)
  213. // Look for our fd. It should be fd 3, but we work around an fd leak
  214. // bug here (http://golang.org/issue/2603) to let it be elsewhere.
  215. var uc *net.UnixConn
  216. for fd := uintptr(3); fd <= 10; fd++ {
  217. f := os.NewFile(fd, "unix-conn")
  218. var ok bool
  219. netc, _ := net.FileConn(f)
  220. uc, ok = netc.(*net.UnixConn)
  221. if ok {
  222. break
  223. }
  224. }
  225. if uc == nil {
  226. fmt.Println("failed to find unix fd")
  227. return
  228. }
  229. // Make a file f to send to our parent process on uc.
  230. // We make it in tempDir, which our parent will clean up.
  231. flag.Parse()
  232. tempDir := flag.Arg(0)
  233. f, err := ioutil.TempFile(tempDir, "")
  234. if err != nil {
  235. fmt.Printf("TempFile: %v", err)
  236. return
  237. }
  238. f.Write([]byte("Hello from child process!\n"))
  239. f.Seek(0, 0)
  240. rights := unix.UnixRights(int(f.Fd()))
  241. dummyByte := []byte("x")
  242. n, oobn, err := uc.WriteMsgUnix(dummyByte, rights, nil)
  243. if err != nil {
  244. fmt.Printf("WriteMsgUnix: %v", err)
  245. return
  246. }
  247. if n != 1 || oobn != len(rights) {
  248. fmt.Printf("WriteMsgUnix = %d, %d; want 1, %d", n, oobn, len(rights))
  249. return
  250. }
  251. }
  252. // TestUnixRightsRoundtrip tests that UnixRights, ParseSocketControlMessage,
  253. // and ParseUnixRights are able to successfully round-trip lists of file descriptors.
  254. func TestUnixRightsRoundtrip(t *testing.T) {
  255. testCases := [...][][]int{
  256. {{42}},
  257. {{1, 2}},
  258. {{3, 4, 5}},
  259. {{}},
  260. {{1, 2}, {3, 4, 5}, {}, {7}},
  261. }
  262. for _, testCase := range testCases {
  263. b := []byte{}
  264. var n int
  265. for _, fds := range testCase {
  266. // Last assignment to n wins
  267. n = len(b) + unix.CmsgLen(4*len(fds))
  268. b = append(b, unix.UnixRights(fds...)...)
  269. }
  270. // Truncate b
  271. b = b[:n]
  272. scms, err := unix.ParseSocketControlMessage(b)
  273. if err != nil {
  274. t.Fatalf("ParseSocketControlMessage: %v", err)
  275. }
  276. if len(scms) != len(testCase) {
  277. t.Fatalf("expected %v SocketControlMessage; got scms = %#v", len(testCase), scms)
  278. }
  279. for i, scm := range scms {
  280. gotFds, err := unix.ParseUnixRights(&scm)
  281. if err != nil {
  282. t.Fatalf("ParseUnixRights: %v", err)
  283. }
  284. wantFds := testCase[i]
  285. if len(gotFds) != len(wantFds) {
  286. t.Fatalf("expected %v fds, got %#v", len(wantFds), gotFds)
  287. }
  288. for j, fd := range gotFds {
  289. if fd != wantFds[j] {
  290. t.Fatalf("expected fd %v, got %v", wantFds[j], fd)
  291. }
  292. }
  293. }
  294. }
  295. }
  296. func TestRlimit(t *testing.T) {
  297. var rlimit, zero unix.Rlimit
  298. err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit)
  299. if err != nil {
  300. t.Fatalf("Getrlimit: save failed: %v", err)
  301. }
  302. if zero == rlimit {
  303. t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit)
  304. }
  305. set := rlimit
  306. set.Cur = set.Max - 1
  307. if runtime.GOOS == "darwin" && set.Cur > 10240 {
  308. // The max file limit is 10240, even though
  309. // the max returned by Getrlimit is 1<<63-1.
  310. // This is OPEN_MAX in sys/syslimits.h.
  311. set.Cur = 10240
  312. }
  313. err = unix.Setrlimit(unix.RLIMIT_NOFILE, &set)
  314. if err != nil {
  315. t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
  316. }
  317. var get unix.Rlimit
  318. err = unix.Getrlimit(unix.RLIMIT_NOFILE, &get)
  319. if err != nil {
  320. t.Fatalf("Getrlimit: get failed: %v", err)
  321. }
  322. set = rlimit
  323. set.Cur = set.Max - 1
  324. if set != get {
  325. // Seems like Darwin requires some privilege to
  326. // increase the soft limit of rlimit sandbox, though
  327. // Setrlimit never reports an error.
  328. switch runtime.GOOS {
  329. case "darwin":
  330. default:
  331. t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get)
  332. }
  333. }
  334. err = unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
  335. if err != nil {
  336. t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err)
  337. }
  338. }
  339. func TestSeekFailure(t *testing.T) {
  340. _, err := unix.Seek(-1, 0, 0)
  341. if err == nil {
  342. t.Fatalf("Seek(-1, 0, 0) did not fail")
  343. }
  344. str := err.Error() // used to crash on Linux
  345. t.Logf("Seek: %v", str)
  346. if str == "" {
  347. t.Fatalf("Seek(-1, 0, 0) return error with empty message")
  348. }
  349. }
  350. func TestDup(t *testing.T) {
  351. file, err := ioutil.TempFile("", "TestDup")
  352. if err != nil {
  353. t.Fatalf("Tempfile failed: %v", err)
  354. }
  355. defer os.Remove(file.Name())
  356. defer file.Close()
  357. f := int(file.Fd())
  358. newFd, err := unix.Dup(f)
  359. if err != nil {
  360. t.Fatalf("Dup: %v", err)
  361. }
  362. err = unix.Dup2(newFd, newFd+1)
  363. if err != nil {
  364. t.Fatalf("Dup2: %v", err)
  365. }
  366. b1 := []byte("Test123")
  367. b2 := make([]byte, 7)
  368. _, err = unix.Write(newFd+1, b1)
  369. if err != nil {
  370. t.Fatalf("Write to dup2 fd failed: %v", err)
  371. }
  372. _, err = unix.Seek(f, 0, 0)
  373. if err != nil {
  374. t.Fatalf("Seek failed: %v", err)
  375. }
  376. _, err = unix.Read(f, b2)
  377. if err != nil {
  378. t.Fatalf("Read back failed: %v", err)
  379. }
  380. if string(b1) != string(b2) {
  381. t.Errorf("Dup: stdout write not in file, expected %v, got %v", string(b1), string(b2))
  382. }
  383. }
  384. func TestPoll(t *testing.T) {
  385. if runtime.GOOS == "android" ||
  386. (runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")) {
  387. t.Skip("mkfifo syscall is not available on android and iOS, skipping test")
  388. }
  389. defer chtmpdir(t)()
  390. f, cleanup := mktmpfifo(t)
  391. defer cleanup()
  392. const timeout = 100
  393. ok := make(chan bool, 1)
  394. go func() {
  395. select {
  396. case <-time.After(10 * timeout * time.Millisecond):
  397. t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
  398. case <-ok:
  399. }
  400. }()
  401. fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
  402. n, err := unix.Poll(fds, timeout)
  403. ok <- true
  404. if err != nil {
  405. t.Errorf("Poll: unexpected error: %v", err)
  406. return
  407. }
  408. if n != 0 {
  409. t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
  410. return
  411. }
  412. }
  413. func TestGetwd(t *testing.T) {
  414. fd, err := os.Open(".")
  415. if err != nil {
  416. t.Fatalf("Open .: %s", err)
  417. }
  418. defer fd.Close()
  419. // Directory list for test. Do not worry if any are symlinks or do not
  420. // exist on some common unix desktop environments. That will be checked.
  421. dirs := []string{"/", "/usr/bin", "/etc", "/var", "/opt"}
  422. switch runtime.GOOS {
  423. case "android":
  424. dirs = []string{"/", "/system/bin"}
  425. case "darwin":
  426. switch runtime.GOARCH {
  427. case "arm", "arm64":
  428. d1, err := ioutil.TempDir("", "d1")
  429. if err != nil {
  430. t.Fatalf("TempDir: %v", err)
  431. }
  432. d2, err := ioutil.TempDir("", "d2")
  433. if err != nil {
  434. t.Fatalf("TempDir: %v", err)
  435. }
  436. dirs = []string{d1, d2}
  437. }
  438. }
  439. oldwd := os.Getenv("PWD")
  440. for _, d := range dirs {
  441. // Check whether d exists, is a dir and that d's path does not contain a symlink
  442. fi, err := os.Stat(d)
  443. if err != nil || !fi.IsDir() {
  444. t.Logf("Test dir %s stat error (%v) or not a directory, skipping", d, err)
  445. continue
  446. }
  447. check, err := filepath.EvalSymlinks(d)
  448. if err != nil || check != d {
  449. t.Logf("Test dir %s (%s) is symlink or other error (%v), skipping", d, check, err)
  450. continue
  451. }
  452. err = os.Chdir(d)
  453. if err != nil {
  454. t.Fatalf("Chdir: %v", err)
  455. }
  456. pwd, err := unix.Getwd()
  457. if err != nil {
  458. t.Fatalf("Getwd in %s: %s", d, err)
  459. }
  460. os.Setenv("PWD", oldwd)
  461. err = fd.Chdir()
  462. if err != nil {
  463. // We changed the current directory and cannot go back.
  464. // Don't let the tests continue; they'll scribble
  465. // all over some other directory.
  466. fmt.Fprintf(os.Stderr, "fchdir back to dot failed: %s\n", err)
  467. os.Exit(1)
  468. }
  469. if pwd != d {
  470. t.Fatalf("Getwd returned %q want %q", pwd, d)
  471. }
  472. }
  473. }
  474. func TestFstatat(t *testing.T) {
  475. defer chtmpdir(t)()
  476. touch(t, "file1")
  477. var st1 unix.Stat_t
  478. err := unix.Stat("file1", &st1)
  479. if err != nil {
  480. t.Fatalf("Stat: %v", err)
  481. }
  482. var st2 unix.Stat_t
  483. err = unix.Fstatat(unix.AT_FDCWD, "file1", &st2, 0)
  484. if err != nil {
  485. t.Fatalf("Fstatat: %v", err)
  486. }
  487. if st1 != st2 {
  488. t.Errorf("Fstatat: returned stat does not match Stat")
  489. }
  490. err = os.Symlink("file1", "symlink1")
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. err = unix.Lstat("symlink1", &st1)
  495. if err != nil {
  496. t.Fatalf("Lstat: %v", err)
  497. }
  498. err = unix.Fstatat(unix.AT_FDCWD, "symlink1", &st2, unix.AT_SYMLINK_NOFOLLOW)
  499. if err != nil {
  500. t.Fatalf("Fstatat: %v", err)
  501. }
  502. if st1 != st2 {
  503. t.Errorf("Fstatat: returned stat does not match Lstat")
  504. }
  505. }
  506. func TestFchmodat(t *testing.T) {
  507. defer chtmpdir(t)()
  508. touch(t, "file1")
  509. err := os.Symlink("file1", "symlink1")
  510. if err != nil {
  511. t.Fatal(err)
  512. }
  513. mode := os.FileMode(0444)
  514. err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", uint32(mode), 0)
  515. if err != nil {
  516. t.Fatalf("Fchmodat: unexpected error: %v", err)
  517. }
  518. fi, err := os.Stat("file1")
  519. if err != nil {
  520. t.Fatal(err)
  521. }
  522. if fi.Mode() != mode {
  523. t.Errorf("Fchmodat: failed to change file mode: expected %v, got %v", mode, fi.Mode())
  524. }
  525. mode = os.FileMode(0644)
  526. didChmodSymlink := true
  527. err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", uint32(mode), unix.AT_SYMLINK_NOFOLLOW)
  528. if err != nil {
  529. if (runtime.GOOS == "android" || runtime.GOOS == "linux" || runtime.GOOS == "solaris") && err == unix.EOPNOTSUPP {
  530. // Linux and Illumos don't support flags != 0
  531. didChmodSymlink = false
  532. } else {
  533. t.Fatalf("Fchmodat: unexpected error: %v", err)
  534. }
  535. }
  536. if !didChmodSymlink {
  537. // Didn't change mode of the symlink. On Linux, the permissions
  538. // of a symbolic link are always 0777 according to symlink(7)
  539. mode = os.FileMode(0777)
  540. }
  541. var st unix.Stat_t
  542. err = unix.Lstat("symlink1", &st)
  543. if err != nil {
  544. t.Fatal(err)
  545. }
  546. got := os.FileMode(st.Mode & 0777)
  547. if got != mode {
  548. t.Errorf("Fchmodat: failed to change symlink mode: expected %v, got %v", mode, got)
  549. }
  550. }
  551. func TestMkdev(t *testing.T) {
  552. major := uint32(42)
  553. minor := uint32(7)
  554. dev := unix.Mkdev(major, minor)
  555. if unix.Major(dev) != major {
  556. t.Errorf("Major(%#x) == %d, want %d", dev, unix.Major(dev), major)
  557. }
  558. if unix.Minor(dev) != minor {
  559. t.Errorf("Minor(%#x) == %d, want %d", dev, unix.Minor(dev), minor)
  560. }
  561. }
  562. func TestRenameat(t *testing.T) {
  563. defer chtmpdir(t)()
  564. from, to := "renamefrom", "renameto"
  565. touch(t, from)
  566. err := unix.Renameat(unix.AT_FDCWD, from, unix.AT_FDCWD, to)
  567. if err != nil {
  568. t.Fatalf("Renameat: unexpected error: %v", err)
  569. }
  570. _, err = os.Stat(to)
  571. if err != nil {
  572. t.Error(err)
  573. }
  574. _, err = os.Stat(from)
  575. if err == nil {
  576. t.Errorf("Renameat: stat of renamed file %q unexpectedly succeeded", from)
  577. }
  578. }
  579. // mktmpfifo creates a temporary FIFO and provides a cleanup function.
  580. func mktmpfifo(t *testing.T) (*os.File, func()) {
  581. err := unix.Mkfifo("fifo", 0666)
  582. if err != nil {
  583. t.Fatalf("mktmpfifo: failed to create FIFO: %v", err)
  584. }
  585. f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
  586. if err != nil {
  587. os.Remove("fifo")
  588. t.Fatalf("mktmpfifo: failed to open FIFO: %v", err)
  589. }
  590. return f, func() {
  591. f.Close()
  592. os.Remove("fifo")
  593. }
  594. }
  595. // utilities taken from os/os_test.go
  596. func touch(t *testing.T, name string) {
  597. f, err := os.Create(name)
  598. if err != nil {
  599. t.Fatal(err)
  600. }
  601. if err := f.Close(); err != nil {
  602. t.Fatal(err)
  603. }
  604. }
  605. // chtmpdir changes the working directory to a new temporary directory and
  606. // provides a cleanup function. Used when PWD is read-only.
  607. func chtmpdir(t *testing.T) func() {
  608. oldwd, err := os.Getwd()
  609. if err != nil {
  610. t.Fatalf("chtmpdir: %v", err)
  611. }
  612. d, err := ioutil.TempDir("", "test")
  613. if err != nil {
  614. t.Fatalf("chtmpdir: %v", err)
  615. }
  616. if err := os.Chdir(d); err != nil {
  617. t.Fatalf("chtmpdir: %v", err)
  618. }
  619. return func() {
  620. if err := os.Chdir(oldwd); err != nil {
  621. t.Fatalf("chtmpdir: %v", err)
  622. }
  623. os.RemoveAll(d)
  624. }
  625. }