|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//go:build darwin || linux |
| 5 | + |
| 6 | +package xattr |
| 7 | + |
| 8 | +import ( |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "golang.org/x/sys/unix" |
| 16 | +) |
| 17 | + |
| 18 | +func TestSetOverrideStatTree_NestedTree(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + root := t.TempDir() |
| 22 | + sub1 := filepath.Join(root, "a") |
| 23 | + sub2 := filepath.Join(root, "a", "b") |
| 24 | + require.NoError(t, os.MkdirAll(sub2, 0o755)) |
| 25 | + |
| 26 | + // Create a regular file — it should also get the xattr. |
| 27 | + filePath := filepath.Join(sub1, "file.txt") |
| 28 | + require.NoError(t, os.WriteFile(filePath, []byte("hi"), 0o644)) |
| 29 | + |
| 30 | + require.NoError(t, SetOverrideStatTree(root, 1000, 1000)) |
| 31 | + |
| 32 | + // All directories should have the xattr set. |
| 33 | + for _, dir := range []string{root, sub1, sub2} { |
| 34 | + val := readXattrOpt(t, dir) |
| 35 | + assert.Contains(t, val, "1000:1000:", "dir %s should have override xattr", dir) |
| 36 | + } |
| 37 | + |
| 38 | + // Regular files should also have the xattr set. |
| 39 | + val := readXattrOpt(t, filePath) |
| 40 | + assert.Contains(t, val, "1000:1000:", "file should have override xattr") |
| 41 | +} |
| 42 | + |
| 43 | +func TestSetOverrideStatTree_SymlinkToExternalDir(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + |
| 46 | + root := t.TempDir() |
| 47 | + external := t.TempDir() |
| 48 | + externalSub := filepath.Join(external, "secret") |
| 49 | + require.NoError(t, os.Mkdir(externalSub, 0o755)) |
| 50 | + |
| 51 | + // Create a symlink inside root pointing to an external directory. |
| 52 | + require.NoError(t, os.Symlink(external, filepath.Join(root, "escape"))) |
| 53 | + |
| 54 | + require.NoError(t, SetOverrideStatTree(root, 1000, 1000)) |
| 55 | + |
| 56 | + // The external directory must NOT have the xattr set. |
| 57 | + _, err := unix.Lgetxattr(external, overrideKey, make([]byte, 256)) |
| 58 | + assert.Error(t, err, "external dir should not have override xattr") |
| 59 | + _, err = unix.Lgetxattr(externalSub, overrideKey, make([]byte, 256)) |
| 60 | + assert.Error(t, err, "external subdir should not have override xattr") |
| 61 | +} |
| 62 | + |
| 63 | +func TestSetOverrideStatTree_SymlinkToFile(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + root := t.TempDir() |
| 67 | + target := filepath.Join(root, "real.txt") |
| 68 | + require.NoError(t, os.WriteFile(target, []byte("data"), 0o644)) |
| 69 | + require.NoError(t, os.Symlink(target, filepath.Join(root, "link.txt"))) |
| 70 | + |
| 71 | + require.NoError(t, SetOverrideStatTree(root, 1000, 1000)) |
| 72 | + |
| 73 | + // The real file gets the xattr (it's a regular file under root). |
| 74 | + val := readXattrOpt(t, target) |
| 75 | + assert.Contains(t, val, "1000:1000:", "real file should have override xattr") |
| 76 | + |
| 77 | + // The symlink itself should NOT have the xattr. |
| 78 | + link := filepath.Join(root, "link.txt") |
| 79 | + _, err := unix.Lgetxattr(link, overrideKey, make([]byte, 256)) |
| 80 | + assert.Error(t, err, "symlink should not have override xattr") |
| 81 | +} |
| 82 | + |
| 83 | +func TestSetOverrideStatTree_InaccessibleRoot(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + |
| 86 | + err := SetOverrideStatTree("/nonexistent/path/xattr-test", 1000, 1000) |
| 87 | + assert.Error(t, err, "should fail on inaccessible root") |
| 88 | +} |
| 89 | + |
| 90 | +func TestSetOverrideStatTree_EmptyDir(t *testing.T) { |
| 91 | + t.Parallel() |
| 92 | + |
| 93 | + root := t.TempDir() |
| 94 | + require.NoError(t, SetOverrideStatTree(root, 1000, 1000)) |
| 95 | + |
| 96 | + // Root dir itself should have the xattr. |
| 97 | + val := readXattrOpt(t, root) |
| 98 | + assert.Contains(t, val, "1000:1000:", "root dir should have override xattr") |
| 99 | +} |
| 100 | + |
| 101 | +func TestSetOverrideStatTree_RootIsSymlink(t *testing.T) { |
| 102 | + t.Parallel() |
| 103 | + |
| 104 | + real := t.TempDir() |
| 105 | + sub := filepath.Join(real, "child") |
| 106 | + require.NoError(t, os.Mkdir(sub, 0o755)) |
| 107 | + |
| 108 | + // Create a symlink that points to real. The walk should resolve it |
| 109 | + // and set xattrs on the real directory tree. |
| 110 | + link := filepath.Join(t.TempDir(), "link") |
| 111 | + require.NoError(t, os.Symlink(real, link)) |
| 112 | + |
| 113 | + require.NoError(t, SetOverrideStatTree(link, 1000, 1000)) |
| 114 | + |
| 115 | + val := readXattrOpt(t, real) |
| 116 | + assert.Contains(t, val, "1000:1000:", "resolved root should have override xattr") |
| 117 | + val = readXattrOpt(t, sub) |
| 118 | + assert.Contains(t, val, "1000:1000:", "child dir should have override xattr") |
| 119 | +} |
| 120 | + |
| 121 | +func TestSetOverrideStatTree_DifferentUIDGID(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + |
| 124 | + root := t.TempDir() |
| 125 | + filePath := filepath.Join(root, "file.txt") |
| 126 | + require.NoError(t, os.WriteFile(filePath, []byte("data"), 0o644)) |
| 127 | + |
| 128 | + // Use different UID and GID to verify both are written independently. |
| 129 | + require.NoError(t, SetOverrideStatTree(root, 1000, 2000)) |
| 130 | + |
| 131 | + val := readXattrOpt(t, root) |
| 132 | + assert.Contains(t, val, "1000:2000:", "dir should have uid=1000 gid=2000") |
| 133 | + |
| 134 | + val = readXattrOpt(t, filePath) |
| 135 | + assert.Contains(t, val, "1000:2000:", "file should have uid=1000 gid=2000") |
| 136 | +} |
| 137 | + |
| 138 | +// readXattrOpt reads the override_stat xattr and returns its value, or |
| 139 | +// empty string if the xattr is not set. |
| 140 | +func readXattrOpt(t *testing.T, path string) string { |
| 141 | + t.Helper() |
| 142 | + buf := make([]byte, 256) |
| 143 | + n, err := unix.Lgetxattr(path, overrideKey, buf) |
| 144 | + if err != nil { |
| 145 | + return "" |
| 146 | + } |
| 147 | + return string(buf[:n]) |
| 148 | +} |
0 commit comments