executable_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. package os_test
  5. import (
  6. "fmt"
  7. "internal/testenv"
  8. "os"
  9. osexec "os/exec"
  10. "path/filepath"
  11. "runtime"
  12. "testing"
  13. )
  14. const executable_EnvVar = "OSTEST_OUTPUT_EXECPATH"
  15. func TestExecutable(t *testing.T) {
  16. testenv.MustHaveExec(t)
  17. ep, err := os.Executable()
  18. if err != nil {
  19. t.Fatalf("Executable failed: %v", err)
  20. }
  21. // we want fn to be of the form "dir/prog"
  22. dir := filepath.Dir(filepath.Dir(ep))
  23. fn, err := filepath.Rel(dir, ep)
  24. if err != nil {
  25. t.Fatalf("filepath.Rel: %v", err)
  26. }
  27. cmd := &osexec.Cmd{}
  28. // make child start with a relative program path
  29. cmd.Dir = dir
  30. cmd.Path = fn
  31. // forge argv[0] for child, so that we can verify we could correctly
  32. // get real path of the executable without influenced by argv[0].
  33. cmd.Args = []string{"-", "-test.run=XXXX"}
  34. if runtime.GOOS == "openbsd" || runtime.GOOS == "aix" {
  35. // OpenBSD and AIX rely on argv[0]
  36. cmd.Args[0] = fn
  37. }
  38. cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", executable_EnvVar))
  39. out, err := cmd.CombinedOutput()
  40. if err != nil {
  41. t.Fatalf("exec(self) failed: %v", err)
  42. }
  43. outs := string(out)
  44. if !filepath.IsAbs(outs) {
  45. t.Fatalf("Child returned %q, want an absolute path", out)
  46. }
  47. if !sameFile(outs, ep) {
  48. t.Fatalf("Child returned %q, not the same file as %q", out, ep)
  49. }
  50. }
  51. func sameFile(fn1, fn2 string) bool {
  52. fi1, err := os.Stat(fn1)
  53. if err != nil {
  54. return false
  55. }
  56. fi2, err := os.Stat(fn2)
  57. if err != nil {
  58. return false
  59. }
  60. return os.SameFile(fi1, fi2)
  61. }
  62. func init() {
  63. if e := os.Getenv(executable_EnvVar); e != "" {
  64. // first chdir to another path
  65. dir := "/"
  66. if runtime.GOOS == "windows" {
  67. cwd, err := os.Getwd()
  68. if err != nil {
  69. panic(err)
  70. }
  71. dir = filepath.VolumeName(cwd)
  72. }
  73. os.Chdir(dir)
  74. if ep, err := os.Executable(); err != nil {
  75. fmt.Fprint(os.Stderr, "ERROR: ", err)
  76. } else {
  77. fmt.Fprint(os.Stderr, ep)
  78. }
  79. os.Exit(0)
  80. }
  81. }
  82. func TestExecutableDeleted(t *testing.T) {
  83. testenv.MustHaveExec(t)
  84. switch runtime.GOOS {
  85. case "windows", "plan9":
  86. t.Skipf("%v does not support deleting running binary", runtime.GOOS)
  87. case "openbsd", "freebsd", "aix":
  88. t.Skipf("%v does not support reading deleted binary name", runtime.GOOS)
  89. }
  90. dir := t.TempDir()
  91. src := filepath.Join(dir, "testdel.go")
  92. exe := filepath.Join(dir, "testdel.exe")
  93. err := os.WriteFile(src, []byte(testExecutableDeletion), 0666)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. out, err := osexec.Command(testenv.GoToolPath(t), "build", "-o", exe, src).CombinedOutput()
  98. t.Logf("build output:\n%s", out)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. out, err = osexec.Command(exe).CombinedOutput()
  103. t.Logf("exec output:\n%s", out)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. }
  108. const testExecutableDeletion = `package main
  109. import (
  110. "fmt"
  111. "os"
  112. )
  113. func main() {
  114. before, err := os.Executable()
  115. if err != nil {
  116. fmt.Fprintf(os.Stderr, "failed to read executable name before deletion: %v\n", err)
  117. os.Exit(1)
  118. }
  119. err = os.Remove(before)
  120. if err != nil {
  121. fmt.Fprintf(os.Stderr, "failed to remove executable: %v\n", err)
  122. os.Exit(1)
  123. }
  124. after, err := os.Executable()
  125. if err != nil {
  126. fmt.Fprintf(os.Stderr, "failed to read executable name after deletion: %v\n", err)
  127. os.Exit(1)
  128. }
  129. if before != after {
  130. fmt.Fprintf(os.Stderr, "before and after do not match: %v != %v\n", before, after)
  131. os.Exit(1)
  132. }
  133. }
  134. `