|
| 1 | +package guards_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/jskswamy/aide/pkg/seatbelt" |
| 10 | + "github.com/jskswamy/aide/pkg/seatbelt/guards" |
| 11 | +) |
| 12 | + |
| 13 | +// nixStoreOverride sets TestNixStoreDir to a fake store dir for the duration |
| 14 | +// of the test and restores it on cleanup. |
| 15 | +func nixStoreOverride(t *testing.T, dir string) { |
| 16 | + t.Helper() |
| 17 | + orig := *guards.TestNixStoreDir |
| 18 | + *guards.TestNixStoreDir = dir |
| 19 | + t.Cleanup(func() { *guards.TestNixStoreDir = orig }) |
| 20 | +} |
| 21 | + |
| 22 | +// TestGuard_NixToolchain_PopulatesWritableAndReadableForLandlock pins the |
| 23 | +// cross-platform grant split: read+write nix user dirs must be in Writable, |
| 24 | +// and channel/config dirs must be in Readable (not Writable). Both must be |
| 25 | +// absent from the other slice. |
| 26 | +func TestGuard_NixToolchain_PopulatesWritableAndReadableForLandlock(t *testing.T) { |
| 27 | + nixStoreOverride(t, t.TempDir()) |
| 28 | + |
| 29 | + home := t.TempDir() |
| 30 | + g := guards.NixToolchainGuard() |
| 31 | + result := g.Rules(&seatbelt.Context{HomeDir: home}) |
| 32 | + |
| 33 | + mustBeWritable := []string{ |
| 34 | + filepath.Join(home, ".nix-profile"), |
| 35 | + filepath.Join(home, ".local", "state", "nix"), |
| 36 | + filepath.Join(home, ".cache", "nix"), |
| 37 | + } |
| 38 | + for _, p := range mustBeWritable { |
| 39 | + if !slices.Contains(result.Writable, p) { |
| 40 | + t.Errorf("expected %q in result.Writable; got %v", p, result.Writable) |
| 41 | + } |
| 42 | + if slices.Contains(result.Readable, p) { |
| 43 | + t.Errorf("writable path must not be in Readable: %q", p) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + mustBeReadable := []string{ |
| 48 | + filepath.Join(home, ".nix-defexpr"), |
| 49 | + filepath.Join(home, ".config", "nix"), |
| 50 | + } |
| 51 | + for _, p := range mustBeReadable { |
| 52 | + if !slices.Contains(result.Readable, p) { |
| 53 | + t.Errorf("expected %q in result.Readable; got %v", p, result.Readable) |
| 54 | + } |
| 55 | + if slices.Contains(result.Writable, p) { |
| 56 | + t.Errorf("read-only path must not be in Writable: %q", p) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// TestGuard_NixToolchain_SkippedWhenNixNotInstalled verifies the guard returns |
| 62 | +// a Skipped entry and no rules when the nix store is absent. |
| 63 | +func TestGuard_NixToolchain_SkippedWhenNixNotInstalled(t *testing.T) { |
| 64 | + nixStoreOverride(t, "/nonexistent/nix/store") |
| 65 | + |
| 66 | + g := guards.NixToolchainGuard() |
| 67 | + result := g.Rules(&seatbelt.Context{HomeDir: t.TempDir()}) |
| 68 | + |
| 69 | + if len(result.Rules) != 0 { |
| 70 | + t.Error("expected no Rules when nix store absent") |
| 71 | + } |
| 72 | + if len(result.Skipped) == 0 { |
| 73 | + t.Error("expected Skipped entry when nix store absent") |
| 74 | + } |
| 75 | + if len(result.Writable) != 0 || len(result.Readable) != 0 { |
| 76 | + t.Error("expected no Writable/Readable when nix not installed") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestGuard_NixToolchain_RulesContent verifies the Seatbelt rule content when |
| 81 | +// nix is present: daemon socket, read-write user paths, read-only config paths. |
| 82 | +func TestGuard_NixToolchain_RulesContent(t *testing.T) { |
| 83 | + nixStoreOverride(t, t.TempDir()) |
| 84 | + |
| 85 | + home := t.TempDir() |
| 86 | + g := guards.NixToolchainGuard() |
| 87 | + result := g.Rules(&seatbelt.Context{HomeDir: home}) |
| 88 | + |
| 89 | + if len(result.Rules) == 0 { |
| 90 | + t.Fatal("expected rules when nix store present") |
| 91 | + } |
| 92 | + output := renderTestRules(result.Rules) |
| 93 | + for _, want := range []string{ |
| 94 | + "network-outbound", |
| 95 | + "/nix/var/nix/daemon-socket/socket", |
| 96 | + ".nix-profile", |
| 97 | + ".local/state/nix", |
| 98 | + ".cache/nix", |
| 99 | + ".nix-defexpr", |
| 100 | + ".config/nix", |
| 101 | + "file-read* file-write*", |
| 102 | + } { |
| 103 | + if !strings.Contains(output, want) { |
| 104 | + t.Errorf("expected rules to contain %q", want) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments