-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathpathHelper_test.go
More file actions
108 lines (95 loc) · 2.48 KB
/
Copy pathpathHelper_test.go
File metadata and controls
108 lines (95 loc) · 2.48 KB
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
package geoblock_test
import (
"os"
"path/filepath"
"runtime"
"testing"
geoblock "github.com/PascalMinder/geoblock"
)
func TestValidatePersistencePath(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
okDir := filepath.Join(tmp, "ok")
if err := os.MkdirAll(okDir, 0o755); err != nil {
t.Fatalf("mkdir okDir: %v", err)
}
roDir := filepath.Join(tmp, "ro")
if err := os.MkdirAll(roDir, 0o755); err != nil {
t.Fatalf("mkdir roDir: %v", err)
}
// Make read-only for the writability probe.
if err := os.Chmod(roDir, 0o555); err != nil {
t.Fatalf("chmod roDir: %v", err)
}
// Restore permissions at the end (best-effort).
t.Cleanup(func() { _ = os.Chmod(roDir, 0o755) })
notDir := filepath.Join(tmp, "notdir")
if err := os.WriteFile(notDir, []byte("x"), 0o600); err != nil {
t.Fatalf("create non-dir file: %v", err)
}
type tc struct {
name string
in string
wantEnabled bool
wantErr bool
skipOnWindows bool
}
cases := []tc{
{
name: "empty string disables feature without error",
in: " ",
wantEnabled: false,
wantErr: false,
},
{
name: "ok new file under writable parent",
in: filepath.Join(okDir, "db.bin"),
wantEnabled: true,
wantErr: false,
},
{
name: "missing parent directory",
in: filepath.Join(tmp, "missing", "db.bin"),
wantErr: true,
},
{
name: "parent is a file (not a directory)",
in: filepath.Join(notDir, "x"),
wantErr: true,
},
{
name: "read-only parent directory (not writable)",
in: filepath.Join(roDir, "db.bin"),
wantErr: true,
skipOnWindows: true, // Windows ACLs can make this flaky
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
if c.skipOnWindows && runtime.GOOS == "windows" {
t.Skip("skipping on Windows due to permission semantics")
}
out, err := geoblock.ValidatePersistencePath(c.in)
if c.wantErr {
if err == nil {
t.Fatalf("expected error, got nil (enabled=%v, out=%q)", len(out) > 0, out)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(out) > 0 != c.wantEnabled {
t.Fatalf("enabled: got %v, want %v", len(out) > 0, c.wantEnabled)
}
if c.wantEnabled && out == "" {
t.Fatalf("expected non-empty output path when enabled")
}
if !c.wantEnabled && out != "" {
t.Fatalf("expected empty output path when disabled, got %q", out)
}
})
}
}