A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

creds_test.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2012 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. "bytes"
  8. "net"
  9. "os"
  10. "syscall"
  11. "testing"
  12. "golang.org/x/sys/unix"
  13. )
  14. // TestSCMCredentials tests the sending and receiving of credentials
  15. // (PID, UID, GID) in an ancillary message between two UNIX
  16. // sockets. The SO_PASSCRED socket option is enabled on the sending
  17. // socket for this to work.
  18. func TestSCMCredentials(t *testing.T) {
  19. fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0)
  20. if err != nil {
  21. t.Fatalf("Socketpair: %v", err)
  22. }
  23. defer unix.Close(fds[0])
  24. defer unix.Close(fds[1])
  25. err = unix.SetsockoptInt(fds[0], unix.SOL_SOCKET, unix.SO_PASSCRED, 1)
  26. if err != nil {
  27. t.Fatalf("SetsockoptInt: %v", err)
  28. }
  29. srvFile := os.NewFile(uintptr(fds[0]), "server")
  30. defer srvFile.Close()
  31. srv, err := net.FileConn(srvFile)
  32. if err != nil {
  33. t.Errorf("FileConn: %v", err)
  34. return
  35. }
  36. defer srv.Close()
  37. cliFile := os.NewFile(uintptr(fds[1]), "client")
  38. defer cliFile.Close()
  39. cli, err := net.FileConn(cliFile)
  40. if err != nil {
  41. t.Errorf("FileConn: %v", err)
  42. return
  43. }
  44. defer cli.Close()
  45. var ucred unix.Ucred
  46. if os.Getuid() != 0 {
  47. ucred.Pid = int32(os.Getpid())
  48. ucred.Uid = 0
  49. ucred.Gid = 0
  50. oob := unix.UnixCredentials(&ucred)
  51. _, _, err := cli.(*net.UnixConn).WriteMsgUnix(nil, oob, nil)
  52. if op, ok := err.(*net.OpError); ok {
  53. err = op.Err
  54. }
  55. if sys, ok := err.(*os.SyscallError); ok {
  56. err = sys.Err
  57. }
  58. if err != syscall.EPERM {
  59. t.Fatalf("WriteMsgUnix failed with %v, want EPERM", err)
  60. }
  61. }
  62. ucred.Pid = int32(os.Getpid())
  63. ucred.Uid = uint32(os.Getuid())
  64. ucred.Gid = uint32(os.Getgid())
  65. oob := unix.UnixCredentials(&ucred)
  66. // this is going to send a dummy byte
  67. n, oobn, err := cli.(*net.UnixConn).WriteMsgUnix(nil, oob, nil)
  68. if err != nil {
  69. t.Fatalf("WriteMsgUnix: %v", err)
  70. }
  71. if n != 0 {
  72. t.Fatalf("WriteMsgUnix n = %d, want 0", n)
  73. }
  74. if oobn != len(oob) {
  75. t.Fatalf("WriteMsgUnix oobn = %d, want %d", oobn, len(oob))
  76. }
  77. oob2 := make([]byte, 10*len(oob))
  78. n, oobn2, flags, _, err := srv.(*net.UnixConn).ReadMsgUnix(nil, oob2)
  79. if err != nil {
  80. t.Fatalf("ReadMsgUnix: %v", err)
  81. }
  82. if flags != 0 {
  83. t.Fatalf("ReadMsgUnix flags = 0x%x, want 0", flags)
  84. }
  85. if n != 1 {
  86. t.Fatalf("ReadMsgUnix n = %d, want 1 (dummy byte)", n)
  87. }
  88. if oobn2 != oobn {
  89. // without SO_PASSCRED set on the socket, ReadMsgUnix will
  90. // return zero oob bytes
  91. t.Fatalf("ReadMsgUnix oobn = %d, want %d", oobn2, oobn)
  92. }
  93. oob2 = oob2[:oobn2]
  94. if !bytes.Equal(oob, oob2) {
  95. t.Fatal("ReadMsgUnix oob bytes don't match")
  96. }
  97. scm, err := unix.ParseSocketControlMessage(oob2)
  98. if err != nil {
  99. t.Fatalf("ParseSocketControlMessage: %v", err)
  100. }
  101. newUcred, err := unix.ParseUnixCredentials(&scm[0])
  102. if err != nil {
  103. t.Fatalf("ParseUnixCredentials: %v", err)
  104. }
  105. if *newUcred != ucred {
  106. t.Fatalf("ParseUnixCredentials = %+v, want %+v", newUcred, ucred)
  107. }
  108. }