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