| index 939b671e3..d0cbc331f 100644 |
| --- a/ship_check_prepare_test.go |
| +++ b/ship_check_prepare_test.go |
| @@ -4,9 +4,11 @@ package main |
| |
| import ( |
| "errors" |
| + "fmt" |
| "os" |
| "os/exec" |
| "path/filepath" |
| + "slices" |
| "strconv" |
| "strings" |
| "syscall" |
| @@ -74,6 +76,60 @@ func TestPrepareCheckDir(t *testing.T) { |
| } |
| } |
| |
| +// prepareCheckDir sets the mode, then the owner, with exactly the |
| +// arguments the check needs. The order is the property: in the prepare |
| +// container root holds CAP_CHOWN alone, so once the directory is 65534's it |
| +// could no longer chmod it (that needs CAP_FOWNER). CI has no root to give |
| +// a directory away, so this pins the calls through the checkChmod and |
| +// checkLchown seams; the capabilities themselves are witnessed by the |
| +// rootful Podman run on a dev box (rootfs Plan 4 Task 11, 2026-10-29). |
| +func TestPrepareCheckDirSetsModeThenOwner(t *testing.T) { |
| + var calls []string |
| + oldChmod, oldLchown := checkChmod, checkLchown |
| + t.Cleanup(func() { checkChmod, checkLchown = oldChmod, oldLchown }) |
| + checkChmod = func(name string, mode os.FileMode) error { |
| + calls = append(calls, fmt.Sprintf("chmod %s %#o", name, mode)) |
| + return nil |
| + } |
| + checkLchown = func(name string, uid, gid int) error { |
| + calls = append(calls, fmt.Sprintf("lchown %s %d:%d", name, uid, gid)) |
| + return nil |
| + } |
| + |
| + dir := filepath.Join(t.TempDir(), "check") |
| + if err := os.Mkdir(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := prepareCheckDir(dir, 65534, 65534); err != nil { |
| + t.Fatalf("prepareCheckDir: %v", err) |
| + } |
| + want := []string{"chmod " + dir + " 0700", "lchown " + dir + " 65534:65534"} |
| + if !slices.Equal(calls, want) { |
| + t.Errorf("calls %q, want %q", calls, want) |
| + } |
| + |
| + // The verb gives --uid as the group too. |
| + calls = nil |
| + if got := runRootfsCheckPrepare([]string{"--dir", dir, "--uid", "65534"}); got != 0 || !slices.Equal(calls, want) { |
| + t.Errorf("the verb: exit %d, calls %q, want 0 and %q", got, calls, want) |
| + } |
| + |
| + // A refused path is never changed; a chmod that fails stops the chown. |
| + link := filepath.Join(t.TempDir(), "link") |
| + if err := os.Symlink(dir, link); err != nil { |
| + t.Fatal(err) |
| + } |
| + calls = nil |
| + if err := prepareCheckDir(link, 65534, 65534); err == nil || len(calls) != 0 { |
| + t.Errorf("a symlink: %v, calls %q", err, calls) |
| + } |
| + checkChmod = func(string, os.FileMode) error { return errors.New("operation not permitted") } |
| + calls = nil |
| + if err := prepareCheckDir(dir, 65534, 65534); err == nil || len(calls) != 0 { |
| + t.Errorf("a failed chmod: %v, calls %q", err, calls) |
| + } |
| +} |
| + |
| // The verb's exit statuses: 0 done, 1 refused, 2 a flag error. |
| func TestRunRootfsCheckPrepareExits(t *testing.T) { |
| dir := filepath.Join(t.TempDir(), "check") |