Source file
src/os/getwd_unix_test.go
1
2
3
4
5
6
7 package os_test
8
9 import (
10 "errors"
11 . "os"
12 "path/filepath"
13 "runtime"
14 "strings"
15 "syscall"
16 "testing"
17 )
18
19 func TestGetwdDeep(t *testing.T) {
20 testGetwdDeep(t, false)
21 }
22
23 func TestGetwdDeepWithPWDSet(t *testing.T) {
24 testGetwdDeep(t, true)
25 }
26
27
28
29 func testGetwdDeep(t *testing.T, setPWD bool) {
30 tempDir := t.TempDir()
31
32 dir := tempDir
33 t.Chdir(dir)
34
35 if setPWD {
36 t.Setenv("PWD", dir)
37 } else {
38
39
40
41 t.Setenv("PWD", "")
42 }
43
44 name := strings.Repeat("a", 200)
45 for {
46 if err := Mkdir(name, 0o700); err != nil {
47 t.Fatal(err)
48 }
49 if err := Chdir(name); err != nil {
50 t.Fatal(err)
51 }
52 if setPWD {
53 dir += "/" + name
54 if err := Setenv("PWD", dir); err != nil {
55 t.Fatal(err)
56 }
57 t.Logf(" $PWD len: %d", len(dir))
58 }
59
60 wd, err := Getwd()
61 t.Logf("Getwd len: %d", len(wd))
62 if err != nil {
63
64
65 if errors.Is(err, syscall.EACCES) {
66 t.Logf("ignoring EACCES error: %v", err)
67 break
68 }
69 t.Fatal(err)
70 }
71 if setPWD && wd != dir {
72
73
74
75
76
77 if len(dir) > 1000 {
78 symDir, err := filepath.EvalSymlinks(tempDir)
79 if err == nil && symDir != tempDir {
80 t.Logf("EvalSymlinks(%q) = %q", tempDir, symDir)
81 if strings.Replace(dir, tempDir, symDir, 1) == wd {
82
83 break
84 }
85 }
86 }
87
88 t.Fatalf("Getwd: got %q, want same value as $PWD: %q", wd, dir)
89 }
90
91
92
93
94
95
96
97
98
99 if _, err := Stat(wd); err != nil || len(wd) > 4096 {
100 t.Logf("Done; len(wd)=%d", len(wd))
101
102
103 switch {
104 case err == nil:
105 case errors.Is(err, syscall.ENAMETOOLONG):
106 case runtime.GOOS == "dragonfly" && errors.Is(err, syscall.EFAULT):
107 default:
108 t.Fatalf("unexpected Stat error: %v", err)
109 }
110 break
111 }
112 }
113 }
114
View as plain text