syscall_unix_test.go 17 KB

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