Skip to content

Commit 4e3e08c

Browse files
committed
test(filetxn): cover the Windows directory identity refusals
Address the fleet review on #386: - identity_windows_test.go covers a regular file, a junction, a directory replaced by a junction or by another directory after Lstat, and a path longer than MAX_PATH. - The stale-Lstat subtest now states the expected result per OS: a refusal on Windows, where the identity is read from the path, and the original identity on unix, where it is part of the Lstat result. A fresh Lstat is pinned first so a refusal cannot come from an unreadable path. - identity_other.go refuses on platforms without an identity source, so js/wasm and wasip1 compile and fail closed. Refs #370
1 parent 62ad16b commit 4e3e08c

4 files changed

Lines changed: 147 additions & 36 deletions

File tree

‎internal/filetxn/identity_other.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !unix && !windows
2+
3+
package filetxn
4+
5+
import "os"
6+
7+
// directoryIdentity refuses on platforms without a directory identity source,
8+
// so accepting or restarting a created directory fails closed there.
9+
func directoryIdentity(string, os.FileInfo) (device, inode uint64, ok bool) {
10+
return 0, 0, false
11+
}

‎internal/filetxn/identity_test.go‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package filetxn
33
import (
44
"os"
55
"path/filepath"
6+
"runtime"
67
"testing"
78
)
89

@@ -50,19 +51,32 @@ func TestDirectoryIdentity(t *testing.T) {
5051
t.Fatalf("replacement kept inode %d", before)
5152
}
5253
})
53-
t.Run("never the replacement identity for a stale lstat", func(t *testing.T) {
54+
t.Run("stale lstat never yields the replacement identity", func(t *testing.T) {
5455
root := t.TempDir()
55-
must(t, os.Mkdir(filepath.Join(root, "made"), 0o700))
56-
stale, err := os.Lstat(filepath.Join(root, "made"))
56+
path := filepath.Join(root, "made")
57+
must(t, os.Mkdir(path, 0o700))
58+
_, original := lstatDirectoryIdentity(t, path)
59+
stale, err := os.Lstat(path)
5760
must(t, err)
5861
// Pin the lazily loaded Windows identity, as callers do with os.SameFile.
5962
if !os.SameFile(stale, stale) {
6063
t.Fatal("stale directory identity is unavailable")
6164
}
6265
replaceDirectoryWithDistinctIdentity(t, root, "made")
63-
_, currentInode := lstatDirectoryIdentity(t, filepath.Join(root, "made"))
64-
if _, inode, ok := directoryIdentity(filepath.Join(root, "made"), stale); ok && inode == currentInode {
65-
t.Fatalf("stale lstat result reported the replacement identity %d", inode)
66+
// A fresh Lstat still resolves, so a refusal below can only come from
67+
// the stale result, not from an unreadable path.
68+
_, current := lstatDirectoryIdentity(t, path)
69+
_, inode, ok := directoryIdentity(path, stale)
70+
if runtime.GOOS == "windows" {
71+
// The identity is read from the path, which no longer names stale.
72+
if ok {
73+
t.Fatalf("stale lstat reported identity %d, want refusal", inode)
74+
}
75+
return
76+
}
77+
// On unix the identity is part of the Lstat result itself.
78+
if !ok || inode != original || inode == current {
79+
t.Fatalf("stale lstat identity = %d, %v; want original %d, distinct from %d", inode, ok, original, current)
6680
}
6781
})
6882
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//go:build windows
2+
3+
package filetxn
4+
5+
import (
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strings"
10+
"testing"
11+
)
12+
13+
func makeJunction(t *testing.T, link, target string) {
14+
t.Helper()
15+
if output, err := exec.Command("cmd", "/c", "mklink", "/J", link, target).CombinedOutput(); err != nil {
16+
t.Fatalf("mklink /J %s %s: %v: %s", link, target, err, output)
17+
}
18+
}
19+
20+
// pinnedLstat returns an Lstat result whose lazily loaded Windows identity is
21+
// already fixed, the way callers hold it after an os.SameFile comparison.
22+
func pinnedLstat(t *testing.T, path string) os.FileInfo {
23+
t.Helper()
24+
info, err := os.Lstat(path)
25+
must(t, err)
26+
if !os.SameFile(info, info) {
27+
t.Fatalf("identity is unavailable: %s", path)
28+
}
29+
return info
30+
}
31+
32+
func TestDirectoryIdentityWindowsRefusals(t *testing.T) {
33+
t.Run("regular file", func(t *testing.T) {
34+
path := filepath.Join(t.TempDir(), "file.txt")
35+
writeFile(t, path, []byte("x"), 0o600)
36+
if _, _, ok := directoryIdentity(path, pinnedLstat(t, path)); ok {
37+
t.Fatal("regular file reported a directory identity")
38+
}
39+
})
40+
t.Run("junction", func(t *testing.T) {
41+
root := t.TempDir()
42+
must(t, os.Mkdir(filepath.Join(root, "real"), 0o700))
43+
makeJunction(t, filepath.Join(root, "link"), filepath.Join(root, "real"))
44+
info, err := os.Lstat(filepath.Join(root, "link"))
45+
must(t, err)
46+
if _, _, ok := directoryIdentity(filepath.Join(root, "link"), info); ok {
47+
t.Fatal("junction reported a directory identity")
48+
}
49+
})
50+
t.Run("directory replaced by a junction after lstat", func(t *testing.T) {
51+
root := t.TempDir()
52+
must(t, os.Mkdir(filepath.Join(root, "made"), 0o700))
53+
must(t, os.Mkdir(filepath.Join(root, "elsewhere"), 0o700))
54+
stale := pinnedLstat(t, filepath.Join(root, "made"))
55+
must(t, os.Rename(filepath.Join(root, "made"), filepath.Join(root, "retired")))
56+
makeJunction(t, filepath.Join(root, "made"), filepath.Join(root, "elsewhere"))
57+
if _, _, ok := directoryIdentity(filepath.Join(root, "made"), stale); ok {
58+
t.Fatal("junction replacement reported the stale directory identity")
59+
}
60+
})
61+
t.Run("directory replaced by another directory after lstat", func(t *testing.T) {
62+
root := t.TempDir()
63+
path := filepath.Join(root, "made")
64+
must(t, os.Mkdir(path, 0o700))
65+
stale := pinnedLstat(t, path)
66+
replaceDirectoryWithDistinctIdentity(t, root, "made")
67+
if _, _, ok := directoryIdentity(path, pinnedLstat(t, path)); !ok {
68+
t.Fatal("fresh lstat of the replacement has no identity")
69+
}
70+
if _, _, ok := directoryIdentity(path, stale); ok {
71+
t.Fatal("stale lstat reported an identity for the replacement")
72+
}
73+
})
74+
t.Run("path longer than MAX_PATH", func(t *testing.T) {
75+
path := t.TempDir()
76+
for len(path) <= 300 {
77+
path = filepath.Join(path, strings.Repeat("d", 40))
78+
}
79+
must(t, os.MkdirAll(path, 0o700))
80+
if _, _, ok := directoryIdentity(path, pinnedLstat(t, path)); !ok {
81+
t.Fatalf("long path (%d chars) has no identity", len(path))
82+
}
83+
})
84+
}

‎tests/test_community_policy.py‎

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,11 @@
201201
"internal/filetxn/directories.go",
202202
"internal/filetxn/directories_external_test.go",
203203
"internal/filetxn/directories_test.go",
204+
"internal/filetxn/identity_other.go",
204205
"internal/filetxn/identity_test.go",
205206
"internal/filetxn/identity_unix.go",
206207
"internal/filetxn/identity_windows.go",
208+
"internal/filetxn/identity_windows_test.go",
207209
"internal/filetxn/restart.go",
208210
"internal/filetxn/restart_test.go",
209211
"internal/filetxn/snapshot.go",
@@ -463,36 +465,36 @@ def test_publication_inventory_is_exact_and_not_linked(self) -> None:
463465
or path.is_symlink()
464466
)
465467

466-
self.assertEqual(len(EXPECTED_FILES), 409)
467-
self.assertEqual(len(actual_files), 409)
468-
self.assertEqual(len(EXPECTED_FILES), 409)
469-
self.assertEqual(len(actual_files), 409)
470-
self.assertEqual(len(EXPECTED_FILES), 409)
471-
self.assertEqual(len(actual_files), 409)
472-
self.assertEqual(len(EXPECTED_FILES), 409)
473-
self.assertEqual(len(actual_files), 409)
474-
self.assertEqual(len(EXPECTED_FILES), 409)
475-
self.assertEqual(len(actual_files), 409)
476-
self.assertEqual(len(EXPECTED_FILES), 409)
477-
self.assertEqual(len(actual_files), 409)
478-
self.assertEqual(len(EXPECTED_FILES), 409)
479-
self.assertEqual(len(actual_files), 409)
480-
self.assertEqual(len(EXPECTED_FILES), 409)
481-
self.assertEqual(len(actual_files), 409)
482-
self.assertEqual(len(EXPECTED_FILES), 409)
483-
self.assertEqual(len(actual_files), 409)
484-
self.assertEqual(len(EXPECTED_FILES), 409)
485-
self.assertEqual(len(actual_files), 409)
486-
self.assertEqual(len(EXPECTED_FILES), 409)
487-
self.assertEqual(len(actual_files), 409)
488-
self.assertEqual(len(EXPECTED_FILES), 409)
489-
self.assertEqual(len(actual_files), 409)
490-
self.assertEqual(len(EXPECTED_FILES), 409)
491-
self.assertEqual(len(actual_files), 409)
492-
self.assertEqual(len(EXPECTED_FILES), 409)
493-
self.assertEqual(len(actual_files), 409)
494-
self.assertEqual(len(EXPECTED_FILES), 409)
495-
self.assertEqual(len(actual_files), 409)
468+
self.assertEqual(len(EXPECTED_FILES), 411)
469+
self.assertEqual(len(actual_files), 411)
470+
self.assertEqual(len(EXPECTED_FILES), 411)
471+
self.assertEqual(len(actual_files), 411)
472+
self.assertEqual(len(EXPECTED_FILES), 411)
473+
self.assertEqual(len(actual_files), 411)
474+
self.assertEqual(len(EXPECTED_FILES), 411)
475+
self.assertEqual(len(actual_files), 411)
476+
self.assertEqual(len(EXPECTED_FILES), 411)
477+
self.assertEqual(len(actual_files), 411)
478+
self.assertEqual(len(EXPECTED_FILES), 411)
479+
self.assertEqual(len(actual_files), 411)
480+
self.assertEqual(len(EXPECTED_FILES), 411)
481+
self.assertEqual(len(actual_files), 411)
482+
self.assertEqual(len(EXPECTED_FILES), 411)
483+
self.assertEqual(len(actual_files), 411)
484+
self.assertEqual(len(EXPECTED_FILES), 411)
485+
self.assertEqual(len(actual_files), 411)
486+
self.assertEqual(len(EXPECTED_FILES), 411)
487+
self.assertEqual(len(actual_files), 411)
488+
self.assertEqual(len(EXPECTED_FILES), 411)
489+
self.assertEqual(len(actual_files), 411)
490+
self.assertEqual(len(EXPECTED_FILES), 411)
491+
self.assertEqual(len(actual_files), 411)
492+
self.assertEqual(len(EXPECTED_FILES), 411)
493+
self.assertEqual(len(actual_files), 411)
494+
self.assertEqual(len(EXPECTED_FILES), 411)
495+
self.assertEqual(len(actual_files), 411)
496+
self.assertEqual(len(EXPECTED_FILES), 411)
497+
self.assertEqual(len(actual_files), 411)
496498
self.assertEqual(actual_files, EXPECTED_FILES)
497499

498500
gitignore_entries = set(read_text(".gitignore").splitlines())

0 commit comments

Comments
 (0)