-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetwd_unix_test.go
113 lines (102 loc) · 2.91 KB
/
getwd_unix_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build unix
package os_test
import (
"errors"
. "os"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
)
func TestGetwdDeep(t *testing.T) {
testGetwdDeep(t, false)
}
func TestGetwdDeepWithPWDSet(t *testing.T) {
testGetwdDeep(t, true)
}
// testGetwdDeep checks that os.Getwd is able to return paths
// longer than syscall.PathMax (with or without PWD set).
func testGetwdDeep(t *testing.T, setPWD bool) {
tempDir := t.TempDir()
dir := tempDir
t.Chdir(dir)
if setPWD {
t.Setenv("PWD", dir)
} else {
// When testing os.Getwd, setting PWD to empty string
// is the same as unsetting it, but the latter would
// be more complicated since we don't have t.Unsetenv.
t.Setenv("PWD", "")
}
name := strings.Repeat("a", 200)
for {
if err := Mkdir(name, 0o700); err != nil {
t.Fatal(err)
}
if err := Chdir(name); err != nil {
t.Fatal(err)
}
if setPWD {
dir += "/" + name
if err := Setenv("PWD", dir); err != nil {
t.Fatal(err)
}
t.Logf(" $PWD len: %d", len(dir))
}
wd, err := Getwd()
t.Logf("Getwd len: %d", len(wd))
if err != nil {
// We can get an EACCES error if we can't read up
// to root, which happens on the Android builders.
if errors.Is(err, syscall.EACCES) {
t.Logf("ignoring EACCES error: %v", err)
break
}
t.Fatal(err)
}
if setPWD && wd != dir {
// It's possible for the stat of PWD to fail
// with ENAMETOOLONG, and for getwd to fail for
// the same reason, and it's possible for $TMPDIR
// to contain a symlink. In that case the fallback
// code will not return the same directory.
if len(dir) > 1000 {
symDir, err := filepath.EvalSymlinks(tempDir)
if err == nil && symDir != tempDir {
t.Logf("EvalSymlinks(%q) = %q", tempDir, symDir)
if strings.Replace(dir, tempDir, symDir, 1) == wd {
// Symlink confusion is OK.
break
}
}
}
t.Fatalf("Getwd: got %q, want same value as $PWD: %q", wd, dir)
}
// Ideally the success criterion should be len(wd) > syscall.PathMax,
// but the latter is not public for some platforms, so use Stat(wd).
// When it fails with ENAMETOOLONG, it means:
// - wd is longer than PathMax;
// - Getwd have used the slow fallback code.
//
// To avoid an endless loop here in case Stat keeps working,
// check if len(wd) is above the largest known PathMax among
// all Unix platforms (4096, on Linux).
if _, err := Stat(wd); err != nil || len(wd) > 4096 {
t.Logf("Done; len(wd)=%d", len(wd))
// Most systems return ENAMETOOLONG.
// Dragonfly returns EFAULT.
switch {
case err == nil:
case errors.Is(err, syscall.ENAMETOOLONG):
case runtime.GOOS == "dragonfly" && errors.Is(err, syscall.EFAULT):
default:
t.Fatalf("unexpected Stat error: %v", err)
}
break
}
}
}