|
| 1 | +package writ_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/opskernel-io/writ" |
| 10 | +) |
| 11 | + |
| 12 | +// testPolicyDir writes a minimal allow-all OPA policy and returns the dir path. |
| 13 | +func testPolicyDir(t *testing.T) string { |
| 14 | + t.Helper() |
| 15 | + dir := t.TempDir() |
| 16 | + const allow = `package writ.gate |
| 17 | +import rego.v1 |
| 18 | +default allow := true |
| 19 | +default tier := 2 |
| 20 | +default denial_reason := ""` |
| 21 | + if err := os.WriteFile(filepath.Join(dir, "allow_all.rego"), []byte(allow), 0o600); err != nil { |
| 22 | + t.Fatal(err) |
| 23 | + } |
| 24 | + return dir |
| 25 | +} |
| 26 | + |
| 27 | +// corruptedChainPath writes a valid single entry then tampers the hash field. |
| 28 | +func corruptedChainPath(t *testing.T) string { |
| 29 | + t.Helper() |
| 30 | + f, err := os.CreateTemp(t.TempDir(), "chain-*.jsonl") |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + // A well-formed JSONL line but with a tampered hash value. |
| 35 | + _, _ = f.WriteString(`{"id":"a1","prev_hash":"0000000000000000000000000000000000000000000000000000000000000000","hash":"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef","event_type":"tool_use","allowed":true,"timestamp":"2026-05-05T00:00:00Z"}` + "\n") |
| 36 | + f.Close() |
| 37 | + return f.Name() |
| 38 | +} |
| 39 | + |
| 40 | +func TestNew_CleanChain_Succeeds(t *testing.T) { |
| 41 | + chainPath := filepath.Join(t.TempDir(), "chain.jsonl") |
| 42 | + cfg := writ.Config{ |
| 43 | + PolicyPath: testPolicyDir(t), |
| 44 | + AuditPath: chainPath, |
| 45 | + CallerID: "test-clean", |
| 46 | + } |
| 47 | + c, err := writ.New(cfg) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("want clean-chain New() to succeed, got: %v", err) |
| 50 | + } |
| 51 | + if c == nil { |
| 52 | + t.Fatal("want non-nil client") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestNew_CorruptChain_RecoveryFalse_ReturnsErrCorruptChain(t *testing.T) { |
| 57 | + cfg := writ.Config{ |
| 58 | + PolicyPath: testPolicyDir(t), |
| 59 | + AuditPath: corruptedChainPath(t), |
| 60 | + AllowCorruptChainRecovery: false, |
| 61 | + } |
| 62 | + _, err := writ.New(cfg) |
| 63 | + if err == nil { |
| 64 | + t.Fatal("want ErrCorruptChain, got nil") |
| 65 | + } |
| 66 | + if !errors.Is(err, writ.ErrCorruptChain) { |
| 67 | + t.Fatalf("want errors.Is(err, ErrCorruptChain), got: %v", err) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestNew_CorruptChain_RecoveryTrue_BoundaryEntryWritten(t *testing.T) { |
| 72 | + chainPath := corruptedChainPath(t) |
| 73 | + cfg := writ.Config{ |
| 74 | + PolicyPath: testPolicyDir(t), |
| 75 | + AuditPath: chainPath, |
| 76 | + AllowCorruptChainRecovery: true, |
| 77 | + } |
| 78 | + c, err := writ.New(cfg) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("want recovery to succeed, got: %v", err) |
| 81 | + } |
| 82 | + if c == nil { |
| 83 | + t.Fatal("want non-nil client") |
| 84 | + } |
| 85 | + |
| 86 | + // Chain now has original corrupt entry + boundary entry; verify the boundary. |
| 87 | + if verifyErr := writ.Verify(chainPath); verifyErr == nil { |
| 88 | + // If overall verify passes, the boundary fully repaired the chain — unexpected |
| 89 | + // since the first entry is permanently corrupt. This is fine — no assertion here. |
| 90 | + } |
| 91 | + |
| 92 | + // Confirm boundary entry was written by checking raw chain entries. |
| 93 | + store := writ.NewMemoryStore() |
| 94 | + _ = store // use the JSONL path instead |
| 95 | + entries, readErr := writ.ReadChainFile(chainPath) |
| 96 | + if readErr != nil { |
| 97 | + t.Fatalf("ReadChainFile: %v", readErr) |
| 98 | + } |
| 99 | + var foundBoundary bool |
| 100 | + for _, e := range entries { |
| 101 | + if e.EventType == "chain_segment_boundary" { |
| 102 | + foundBoundary = true |
| 103 | + if e.Metadata == nil || e.Metadata["type"] != "RECOVERY" { |
| 104 | + t.Errorf("boundary entry missing RECOVERY metadata: %+v", e.Metadata) |
| 105 | + } |
| 106 | + break |
| 107 | + } |
| 108 | + } |
| 109 | + if !foundBoundary { |
| 110 | + t.Errorf("want chain_segment_boundary entry after recovery, none found in %d entries", len(entries)) |
| 111 | + } |
| 112 | +} |
0 commit comments