|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +package sandbox |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +// ONLY ABSENCE IS NOTHING TO UNDO. |
| 14 | +// |
| 15 | +// removeCreatedRuntimeDirBound returned success on any failure to identify the |
| 16 | +// directory, so one that was still there, but could not be looked at, was |
| 17 | +// reported as removed. Here the parent loses search permission, which makes the |
| 18 | +// directory exist and be unidentifiable at the same time. |
| 19 | +func TestRemovingAnUnidentifiableCreatedDirectoryIsReported(t *testing.T) { |
| 20 | + if os.Geteuid() == 0 { |
| 21 | + t.Skip("root bypasses directory search permission, so the lookup cannot be made to fail") |
| 22 | + } |
| 23 | + parent := filepath.Join(t.TempDir(), "zero") |
| 24 | + created := filepath.Join(parent, "runtime") |
| 25 | + if err := os.MkdirAll(created, 0o700); err != nil { |
| 26 | + t.Fatalf("create the directory: %v", err) |
| 27 | + } |
| 28 | + identity, ok := runtimeDirIdentity(created) |
| 29 | + if !ok { |
| 30 | + t.Fatal("identify the created directory") |
| 31 | + } |
| 32 | + if err := os.Chmod(parent, 0o600); err != nil { |
| 33 | + t.Fatalf("withdraw search permission: %v", err) |
| 34 | + } |
| 35 | + t.Cleanup(func() { _ = os.Chmod(parent, 0o700) }) |
| 36 | + if _, err := os.Lstat(created); err == nil || errors.Is(err, os.ErrNotExist) { |
| 37 | + t.Fatalf("SETUP INVALID: the directory should exist and be unidentifiable, Lstat said %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + err := removeCreatedRuntimeDirBound(created, identity) |
| 41 | + if err == nil { |
| 42 | + t.Fatal("a created directory that could not be identified was reported as removed") |
| 43 | + } |
| 44 | + if !strings.Contains(err.Error(), "could not be identified") { |
| 45 | + t.Errorf("the error does not say why the directory was left: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + // And it is still there, which is why the error is owed. |
| 49 | + if err := os.Chmod(parent, 0o700); err != nil { |
| 50 | + t.Fatalf("restore search permission: %v", err) |
| 51 | + } |
| 52 | + if _, err := os.Lstat(created); err != nil { |
| 53 | + t.Fatalf("the directory was gone after all, so the error was not owed: %v", err) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Absence stays a clean undo, or the test above would be satisfied by a function |
| 58 | +// that reports every lookup failure. |
| 59 | +func TestRemovingACreatedDirectoryThatIsAlreadyGoneSucceeds(t *testing.T) { |
| 60 | + created := filepath.Join(t.TempDir(), "runtime") |
| 61 | + if err := os.Mkdir(created, 0o700); err != nil { |
| 62 | + t.Fatalf("create the directory: %v", err) |
| 63 | + } |
| 64 | + identity, ok := runtimeDirIdentity(created) |
| 65 | + if !ok { |
| 66 | + t.Fatal("identify the created directory") |
| 67 | + } |
| 68 | + if err := os.Remove(created); err != nil { |
| 69 | + t.Fatalf("remove the directory: %v", err) |
| 70 | + } |
| 71 | + if err := removeCreatedRuntimeDirBound(created, identity); err != nil { |
| 72 | + t.Fatalf("a created directory that is already gone was reported as a failed undo: %v", err) |
| 73 | + } |
| 74 | +} |
0 commit comments