| 1 |
// Copyright 2022 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 |
|
| 5 |
package exec_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"internal/testenv" |
| 10 |
"os" |
| 11 |
. "os/exec" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
) |
| 17 |
|
| 18 |
var pathVar string = func() string { |
| 19 |
if runtime.GOOS == "plan9" { |
| 20 |
return "path" |
| 21 |
} |
| 22 |
return "PATH" |
| 23 |
}() |
| 24 |
|
| 25 |
func TestLookPath(t *testing.T) { |
| 26 |
testenv.MustHaveExec(t) |
| 27 |
// Not parallel: uses Chdir and Setenv. |
| 28 |
|
| 29 |
tmpDir := filepath.Join(t.TempDir(), "testdir") |
| 30 |
if err := os.Mkdir(tmpDir, 0777); err != nil { |
| 31 |
t.Fatal(err) |
| 32 |
} |
| 33 |
|
| 34 |
executable := "execabs-test" |
| 35 |
if runtime.GOOS == "windows" { |
| 36 |
executable += ".exe" |
| 37 |
} |
| 38 |
if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0777); err != nil { |
| 39 |
t.Fatal(err) |
| 40 |
} |
| 41 |
t.Chdir(tmpDir) |
| 42 |
t.Logf(". is %#q", tmpDir) |
| 43 |
|
| 44 |
origPath := os.Getenv(pathVar) |
| 45 |
|
| 46 |
// Add "." to PATH so that exec.LookPath looks in the current directory on all systems. |
| 47 |
// And try to trick it with "../testdir" too. |
| 48 |
for _, errdot := range []string{"1", "0"} { |
| 49 |
t.Run("GODEBUG=execerrdot="+errdot, func(t *testing.T) { |
| 50 |
t.Setenv("GODEBUG", "execerrdot="+errdot+",execwait=2") |
| 51 |
for _, dir := range []string{".", "../testdir"} { |
| 52 |
t.Run(pathVar+"="+dir, func(t *testing.T) { |
| 53 |
t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath) |
| 54 |
good := dir + "/execabs-test" |
| 55 |
if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { |
| 56 |
t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good) |
| 57 |
} |
| 58 |
if runtime.GOOS == "windows" { |
| 59 |
good = dir + `\execabs-test` |
| 60 |
if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { |
| 61 |
t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
_, err := LookPath("execabs-test") |
| 66 |
if errdot == "1" { |
| 67 |
if err == nil { |
| 68 |
t.Fatalf("LookPath didn't fail when finding a non-relative path") |
| 69 |
} else if !errors.Is(err, ErrDot) { |
| 70 |
t.Fatalf("LookPath returned unexpected error: want Is ErrDot, got %q", err) |
| 71 |
} |
| 72 |
} else { |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("LookPath failed unexpectedly: %v", err) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
cmd := Command("execabs-test") |
| 79 |
if errdot == "1" { |
| 80 |
if cmd.Err == nil { |
| 81 |
t.Fatalf("Command didn't fail when finding a non-relative path") |
| 82 |
} else if !errors.Is(cmd.Err, ErrDot) { |
| 83 |
t.Fatalf("Command returned unexpected error: want Is ErrDot, got %q", cmd.Err) |
| 84 |
} |
| 85 |
cmd.Err = nil |
| 86 |
} else { |
| 87 |
if cmd.Err != nil { |
| 88 |
t.Fatalf("Command failed unexpectedly: %v", err) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Clearing cmd.Err should let the execution proceed, |
| 93 |
// and it should fail because it's not a valid binary. |
| 94 |
if err := cmd.Run(); err == nil { |
| 95 |
t.Fatalf("Run did not fail: expected exec error") |
| 96 |
} else if errors.Is(err, ErrDot) { |
| 97 |
t.Fatalf("Run returned unexpected error ErrDot: want error like ENOEXEC: %q", err) |
| 98 |
} |
| 99 |
}) |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
// Test the behavior when the first entry in PATH is an absolute name for the |
| 105 |
// current directory. |
| 106 |
// |
| 107 |
// On Windows, "." may or may not be implicitly included before the explicit |
| 108 |
// %PATH%, depending on the process environment; |
| 109 |
// see https://go.dev/issue/4394. |
| 110 |
// |
| 111 |
// If the relative entry from "." resolves to the same executable as what |
| 112 |
// would be resolved from an absolute entry in %PATH% alone, LookPath should |
| 113 |
// return the absolute version of the path instead of ErrDot. |
| 114 |
// (See https://go.dev/issue/53536.) |
| 115 |
// |
| 116 |
// If PATH does not implicitly include "." (such as on Unix platforms, or on |
| 117 |
// Windows configured with NoDefaultCurrentDirectoryInExePath), then this |
| 118 |
// lookup should succeed regardless of the behavior for ".", so it may be |
| 119 |
// useful to run as a control case even on those platforms. |
| 120 |
t.Run(pathVar+"=$PWD", func(t *testing.T) { |
| 121 |
t.Setenv(pathVar, tmpDir+string(filepath.ListSeparator)+origPath) |
| 122 |
good := filepath.Join(tmpDir, "execabs-test") |
| 123 |
if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { |
| 124 |
t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, good, found, err, good) |
| 125 |
} |
| 126 |
|
| 127 |
if found, err := LookPath("execabs-test"); err != nil || !strings.HasPrefix(found, good) { |
| 128 |
t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, "execabs-test", found, err, good) |
| 129 |
} |
| 130 |
|
| 131 |
cmd := Command("execabs-test") |
| 132 |
if cmd.Err != nil { |
| 133 |
t.Fatalf("Command(%#q).Err = %v; want nil", "execabs-test", cmd.Err) |
| 134 |
} |
| 135 |
}) |
| 136 |
|
| 137 |
t.Run(pathVar+"=$OTHER", func(t *testing.T) { |
| 138 |
// Control case: if the lookup returns ErrDot when PATH is empty, then we |
| 139 |
// know that PATH implicitly includes ".". If it does not, then we don't |
| 140 |
// expect to see ErrDot at all in this test (because the path will be |
| 141 |
// unambiguously absolute). |
| 142 |
wantErrDot := false |
| 143 |
t.Setenv(pathVar, "") |
| 144 |
if found, err := LookPath("execabs-test"); errors.Is(err, ErrDot) { |
| 145 |
wantErrDot = true |
| 146 |
} else if err == nil { |
| 147 |
t.Fatalf(`with PATH='', LookPath(%#q) = %#q; want non-nil error`, "execabs-test", found) |
| 148 |
} |
| 149 |
|
| 150 |
// Set PATH to include an explicit directory that contains a completely |
| 151 |
// independent executable that happens to have the same name as an |
| 152 |
// executable in ".". If "." is included implicitly, looking up the |
| 153 |
// (unqualified) executable name will return ErrDot; otherwise, the |
| 154 |
// executable in "." should have no effect and the lookup should |
| 155 |
// unambiguously resolve to the directory in PATH. |
| 156 |
|
| 157 |
dir := t.TempDir() |
| 158 |
executable := "execabs-test" |
| 159 |
if runtime.GOOS == "windows" { |
| 160 |
executable += ".exe" |
| 161 |
} |
| 162 |
if err := os.WriteFile(filepath.Join(dir, executable), []byte{1, 2, 3}, 0777); err != nil { |
| 163 |
t.Fatal(err) |
| 164 |
} |
| 165 |
t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath) |
| 166 |
|
| 167 |
found, err := LookPath("execabs-test") |
| 168 |
if wantErrDot { |
| 169 |
wantFound := filepath.Join(".", executable) |
| 170 |
if found != wantFound || !errors.Is(err, ErrDot) { |
| 171 |
t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, Is ErrDot`, "execabs-test", found, err, wantFound) |
| 172 |
} |
| 173 |
} else { |
| 174 |
wantFound := filepath.Join(dir, executable) |
| 175 |
if found != wantFound || err != nil { |
| 176 |
t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, nil`, "execabs-test", found, err, wantFound) |
| 177 |
} |
| 178 |
} |
| 179 |
}) |
| 180 |
|
| 181 |
checker := func(test string) func(t *testing.T) { |
| 182 |
return func(t *testing.T) { |
| 183 |
t.Helper() |
| 184 |
t.Logf("PATH=%s", os.Getenv("PATH")) |
| 185 |
p, err := LookPath(test) |
| 186 |
if err == nil { |
| 187 |
t.Errorf("%q: error expected, got nil", test) |
| 188 |
} |
| 189 |
if p != "" { |
| 190 |
t.Errorf("%q: path returned should be \"\". Got %q", test, p) |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// Reference behavior for the next test |
| 196 |
t.Run(pathVar+"=$OTHER2", func(t *testing.T) { |
| 197 |
t.Run("empty", checker("")) |
| 198 |
t.Run("dot", checker(".")) |
| 199 |
t.Run("dotdot1", checker("abc/..")) |
| 200 |
t.Run("dotdot2", checker("..")) |
| 201 |
}) |
| 202 |
|
| 203 |
// Test the behavior when PATH contains an executable file which is not a directory |
| 204 |
t.Run(pathVar+"=exe", func(t *testing.T) { |
| 205 |
// Inject an executable file (not a directory) in PATH. |
| 206 |
// Use our own binary os.Args[0]. |
| 207 |
t.Setenv(pathVar, testenv.Executable(t)) |
| 208 |
t.Run("empty", checker("")) |
| 209 |
t.Run("dot", checker(".")) |
| 210 |
t.Run("dotdot1", checker("abc/..")) |
| 211 |
t.Run("dotdot2", checker("..")) |
| 212 |
}) |
| 213 |
|
| 214 |
// Test the behavior when PATH contains an executable file which is not a directory |
| 215 |
t.Run(pathVar+"=exe/xx", func(t *testing.T) { |
| 216 |
// Inject an executable file (not a directory) in PATH. |
| 217 |
// Use our own binary os.Args[0]. |
| 218 |
t.Setenv(pathVar, filepath.Join(testenv.Executable(t), "xx")) |
| 219 |
t.Run("empty", checker("")) |
| 220 |
t.Run("dot", checker(".")) |
| 221 |
t.Run("dotdot1", checker("abc/..")) |
| 222 |
t.Run("dotdot2", checker("..")) |
| 223 |
}) |
| 224 |
} |