syscall_unix_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. err = unix.Setrlimit(unix.RLIMIT_NOFILE, &set)
  308. if err != nil {
  309. t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
  310. }
  311. var get unix.Rlimit
  312. err = unix.Getrlimit(unix.RLIMIT_NOFILE, &get)
  313. if err != nil {
  314. t.Fatalf("Getrlimit: get failed: %v", err)
  315. }
  316. set = rlimit
  317. set.Cur = set.Max - 1
  318. if set != get {
  319. // Seems like Darwin requires some privilege to
  320. // increase the soft limit of rlimit sandbox, though
  321. // Setrlimit never reports an error.
  322. switch runtime.GOOS {
  323. case "darwin":
  324. default:
  325. t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get)
  326. }
  327. }
  328. err = unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
  329. if err != nil {
  330. t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err)
  331. }
  332. }
  333. func TestSeekFailure(t *testing.T) {
  334. _, err := unix.Seek(-1, 0, 0)
  335. if err == nil {
  336. t.Fatalf("Seek(-1, 0, 0) did not fail")
  337. }
  338. str := err.Error() // used to crash on Linux
  339. t.Logf("Seek: %v", str)
  340. if str == "" {
  341. t.Fatalf("Seek(-1, 0, 0) return error with empty message")
  342. }
  343. }
  344. func TestDup(t *testing.T) {
  345. file, err := ioutil.TempFile("", "TestDup")
  346. if err != nil {
  347. t.Fatalf("Tempfile failed: %v", err)
  348. }
  349. defer os.Remove(file.Name())
  350. defer file.Close()
  351. f := int(file.Fd())
  352. newFd, err := unix.Dup(f)
  353. if err != nil {
  354. t.Fatalf("Dup: %v", err)
  355. }
  356. err = unix.Dup2(newFd, newFd+1)
  357. if err != nil {
  358. t.Fatalf("Dup2: %v", err)
  359. }
  360. b1 := []byte("Test123")
  361. b2 := make([]byte, 7)
  362. _, err = unix.Write(newFd+1, b1)
  363. if err != nil {
  364. t.Fatalf("Write to dup2 fd failed: %v", err)
  365. }
  366. _, err = unix.Seek(f, 0, 0)
  367. if err != nil {
  368. t.Fatalf("Seek failed: %v", err)
  369. }
  370. _, err = unix.Read(f, b2)
  371. if err != nil {
  372. t.Fatalf("Read back failed: %v", err)
  373. }
  374. if string(b1) != string(b2) {
  375. t.Errorf("Dup: stdout write not in file, expected %v, got %v", string(b1), string(b2))
  376. }
  377. }
  378. func TestPoll(t *testing.T) {
  379. if runtime.GOOS == "android" ||
  380. (runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")) {
  381. t.Skip("mkfifo syscall is not available on android and iOS, skipping test")
  382. }
  383. f, cleanup := mktmpfifo(t)
  384. defer cleanup()
  385. const timeout = 100
  386. ok := make(chan bool, 1)
  387. go func() {
  388. select {
  389. case <-time.After(10 * timeout * time.Millisecond):
  390. t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
  391. case <-ok:
  392. }
  393. }()
  394. fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
  395. n, err := unix.Poll(fds, timeout)
  396. ok <- true
  397. if err != nil {
  398. t.Errorf("Poll: unexpected error: %v", err)
  399. return
  400. }
  401. if n != 0 {
  402. t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
  403. return
  404. }
  405. }
  406. func TestGetwd(t *testing.T) {
  407. fd, err := os.Open(".")
  408. if err != nil {
  409. t.Fatalf("Open .: %s", err)
  410. }
  411. defer fd.Close()
  412. // These are chosen carefully not to be symlinks on a Mac
  413. // (unlike, say, /var, /etc)
  414. dirs := []string{"/", "/usr/bin"}
  415. switch runtime.GOOS {
  416. case "android":
  417. dirs = []string{"/", "/system/bin"}
  418. case "darwin":
  419. switch runtime.GOARCH {
  420. case "arm", "arm64":
  421. d1, err := ioutil.TempDir("", "d1")
  422. if err != nil {
  423. t.Fatalf("TempDir: %v", err)
  424. }
  425. d2, err := ioutil.TempDir("", "d2")
  426. if err != nil {
  427. t.Fatalf("TempDir: %v", err)
  428. }
  429. dirs = []string{d1, d2}
  430. }
  431. }
  432. oldwd := os.Getenv("PWD")
  433. for _, d := range dirs {
  434. err = os.Chdir(d)
  435. if err != nil {
  436. t.Fatalf("Chdir: %v", err)
  437. }
  438. pwd, err := unix.Getwd()
  439. if err != nil {
  440. t.Fatalf("Getwd in %s: %s", d, err)
  441. }
  442. os.Setenv("PWD", oldwd)
  443. err = fd.Chdir()
  444. if err != nil {
  445. // We changed the current directory and cannot go back.
  446. // Don't let the tests continue; they'll scribble
  447. // all over some other directory.
  448. fmt.Fprintf(os.Stderr, "fchdir back to dot failed: %s\n", err)
  449. os.Exit(1)
  450. }
  451. if pwd != d {
  452. t.Fatalf("Getwd returned %q want %q", pwd, d)
  453. }
  454. }
  455. }
  456. func TestFstatat(t *testing.T) {
  457. defer chtmpdir(t)()
  458. touch(t, "file1")
  459. var st1 unix.Stat_t
  460. err := unix.Stat("file1", &st1)
  461. if err != nil {
  462. t.Fatalf("Stat: %v", err)
  463. }
  464. var st2 unix.Stat_t
  465. err = unix.Fstatat(unix.AT_FDCWD, "file1", &st2, 0)
  466. if err != nil {
  467. t.Fatalf("Fstatat: %v", err)
  468. }
  469. if st1 != st2 {
  470. t.Errorf("Fstatat: returned stat does not match Stat")
  471. }
  472. err = os.Symlink("file1", "symlink1")
  473. if err != nil {
  474. t.Fatal(err)
  475. }
  476. err = unix.Lstat("symlink1", &st1)
  477. if err != nil {
  478. t.Fatalf("Lstat: %v", err)
  479. }
  480. err = unix.Fstatat(unix.AT_FDCWD, "symlink1", &st2, unix.AT_SYMLINK_NOFOLLOW)
  481. if err != nil {
  482. t.Fatalf("Fstatat: %v", err)
  483. }
  484. if st1 != st2 {
  485. t.Errorf("Fstatat: returned stat does not match Lstat")
  486. }
  487. }
  488. func TestFchmodat(t *testing.T) {
  489. defer chtmpdir(t)()
  490. touch(t, "file1")
  491. err := os.Symlink("file1", "symlink1")
  492. if err != nil {
  493. t.Fatal(err)
  494. }
  495. mode := os.FileMode(0444)
  496. err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", uint32(mode), 0)
  497. if err != nil {
  498. t.Fatalf("Fchmodat: unexpected error: %v", err)
  499. }
  500. fi, err := os.Stat("file1")
  501. if err != nil {
  502. t.Fatal(err)
  503. }
  504. if fi.Mode() != mode {
  505. t.Errorf("Fchmodat: failed to change file mode: expected %v, got %v", mode, fi.Mode())
  506. }
  507. mode = os.FileMode(0644)
  508. didChmodSymlink := true
  509. err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", uint32(mode), unix.AT_SYMLINK_NOFOLLOW)
  510. if err != nil {
  511. if (runtime.GOOS == "android" || runtime.GOOS == "linux" || runtime.GOOS == "solaris") && err == unix.EOPNOTSUPP {
  512. // Linux and Illumos don't support flags != 0
  513. didChmodSymlink = false
  514. } else {
  515. t.Fatalf("Fchmodat: unexpected error: %v", err)
  516. }
  517. }
  518. if !didChmodSymlink {
  519. // Didn't change mode of the symlink. On Linux, the permissions
  520. // of a symbolic link are always 0777 according to symlink(7)
  521. mode = os.FileMode(0777)
  522. }
  523. var st unix.Stat_t
  524. err = unix.Lstat("symlink1", &st)
  525. if err != nil {
  526. t.Fatal(err)
  527. }
  528. got := os.FileMode(st.Mode & 0777)
  529. if got != mode {
  530. t.Errorf("Fchmodat: failed to change symlink mode: expected %v, got %v", mode, got)
  531. }
  532. }
  533. func TestMkdev(t *testing.T) {
  534. major := uint32(42)
  535. minor := uint32(7)
  536. dev := unix.Mkdev(major, minor)
  537. if unix.Major(dev) != major {
  538. t.Errorf("Major(%#x) == %d, want %d", dev, unix.Major(dev), major)
  539. }
  540. if unix.Minor(dev) != minor {
  541. t.Errorf("Minor(%#x) == %d, want %d", dev, unix.Minor(dev), minor)
  542. }
  543. }
  544. // mktmpfifo creates a temporary FIFO and provides a cleanup function.
  545. func mktmpfifo(t *testing.T) (*os.File, func()) {
  546. err := unix.Mkfifo("fifo", 0666)
  547. if err != nil {
  548. t.Fatalf("mktmpfifo: failed to create FIFO: %v", err)
  549. }
  550. f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
  551. if err != nil {
  552. os.Remove("fifo")
  553. t.Fatalf("mktmpfifo: failed to open FIFO: %v", err)
  554. }
  555. return f, func() {
  556. f.Close()
  557. os.Remove("fifo")
  558. }
  559. }
  560. // utilities taken from os/os_test.go
  561. func touch(t *testing.T, name string) {
  562. f, err := os.Create(name)
  563. if err != nil {
  564. t.Fatal(err)
  565. }
  566. if err := f.Close(); err != nil {
  567. t.Fatal(err)
  568. }
  569. }
  570. // chtmpdir changes the working directory to a new temporary directory and
  571. // provides a cleanup function. Used when PWD is read-only.
  572. func chtmpdir(t *testing.T) func() {
  573. oldwd, err := os.Getwd()
  574. if err != nil {
  575. t.Fatalf("chtmpdir: %v", err)
  576. }
  577. d, err := ioutil.TempDir("", "test")
  578. if err != nil {
  579. t.Fatalf("chtmpdir: %v", err)
  580. }
  581. if err := os.Chdir(d); err != nil {
  582. t.Fatalf("chtmpdir: %v", err)
  583. }
  584. return func() {
  585. if err := os.Chdir(oldwd); err != nil {
  586. t.Fatalf("chtmpdir: %v", err)
  587. }
  588. os.RemoveAll(d)
  589. }
  590. }