Skip to content

Commit ee92f7f

Browse files
Improve tests for difc package
- Replace manual if/t.Errorf checks with idiomatic testify assertions - assert.True/False instead of if-blocks for IsAllowed() checks - assert.Equal instead of if-blocks for field comparison - assert.NotEmpty instead of assert.False(t, len(x) == 0, ...) - Add require import (was missing; needed for require.NotNil) - Add test cases for OperationReadWrite (previously untested): - denied when read check fails (missing secrecy) - denied when write check fails (missing integrity) - allowed when both read and write pass - Add TestNewEvaluatorWithMode with table-driven tests for all modes - Add TestEvaluatorSetMode to cover GetMode/SetMode API Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 20d3459 commit ee92f7f

1 file changed

Lines changed: 100 additions & 25 deletions

File tree

internal/difc/difc_test.go

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestLabelOperations(t *testing.T) {
@@ -68,8 +69,7 @@ func TestEvaluator(t *testing.T) {
6869
result := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationRead)
6970

7071
assert.False(t, result.IsAllowed(), "Expected access to be denied for read with insufficient secrecy")
71-
72-
assert.False(t, len(result.SecrecyToAdd) == 0, "Expected SecrecyToAdd to contain required tags")
72+
assert.NotEmpty(t, result.SecrecyToAdd, "Expected SecrecyToAdd to contain required tags")
7373
})
7474

7575
t.Run("Read operation - allowed with matching labels", func(t *testing.T) {
@@ -83,9 +83,7 @@ func TestEvaluator(t *testing.T) {
8383

8484
result := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationRead)
8585

86-
if !result.IsAllowed() {
87-
t.Errorf("Expected access to be allowed: %s", result.Reason)
88-
}
86+
assert.True(t, result.IsAllowed(), "Expected access to be allowed: %s", result.Reason)
8987
})
9088

9189
t.Run("Write operation - integrity check", func(t *testing.T) {
@@ -99,8 +97,7 @@ func TestEvaluator(t *testing.T) {
9997
result := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationWrite)
10098

10199
assert.False(t, result.IsAllowed(), "Expected access to be denied for write with insufficient integrity")
102-
103-
assert.False(t, len(result.IntegrityToDrop) == 0, "Expected IntegrityToDrop to contain required tags")
100+
assert.NotEmpty(t, result.IntegrityToDrop, "Expected IntegrityToDrop to contain required tags")
104101
})
105102

106103
t.Run("Write operation - allowed with matching integrity", func(t *testing.T) {
@@ -114,9 +111,7 @@ func TestEvaluator(t *testing.T) {
114111

115112
result := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationWrite)
116113

117-
if !result.IsAllowed() {
118-
t.Errorf("Expected access to be allowed: %s", result.Reason)
119-
}
114+
assert.True(t, result.IsAllowed(), "Expected access to be allowed: %s", result.Reason)
120115
})
121116

122117
t.Run("Empty resource allows all operations", func(t *testing.T) {
@@ -131,13 +126,99 @@ func TestEvaluator(t *testing.T) {
131126
readResult := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationRead)
132127
writeResult := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationWrite)
133128

134-
if !readResult.IsAllowed() {
135-
t.Errorf("Expected read to be allowed for empty resource: %s", readResult.Reason)
136-
}
137-
if !writeResult.IsAllowed() {
138-
t.Errorf("Expected write to be allowed for empty resource: %s", writeResult.Reason)
139-
}
129+
assert.True(t, readResult.IsAllowed(), "Expected read to be allowed for empty resource: %s", readResult.Reason)
130+
assert.True(t, writeResult.IsAllowed(), "Expected write to be allowed for empty resource: %s", writeResult.Reason)
140131
})
132+
133+
t.Run("ReadWrite operation - denied when read fails", func(t *testing.T) {
134+
// Agent without secrecy clearance; read-write should be denied at the read step
135+
agentSecrecy := NewSecrecyLabel()
136+
agentIntegrity := NewIntegrityLabel()
137+
agentIntegrity.Label.Add("trusted")
138+
139+
resource := NewLabeledResource("secret-rw-resource")
140+
resource.Secrecy.Label.Add("secret")
141+
resource.Integrity.Label.Add("trusted")
142+
143+
result := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationReadWrite)
144+
145+
assert.False(t, result.IsAllowed(), "Expected ReadWrite to be denied when read fails")
146+
assert.NotEmpty(t, result.SecrecyToAdd, "Expected SecrecyToAdd to be set on ReadWrite denial")
147+
})
148+
149+
t.Run("ReadWrite operation - denied when write fails", func(t *testing.T) {
150+
// Agent has secrecy clearance but missing integrity; write should fail
151+
agentSecrecy := NewSecrecyLabel()
152+
agentSecrecy.Label.Add("secret")
153+
agentIntegrity := NewIntegrityLabel()
154+
// Agent has no integrity tags, resource requires "trusted"
155+
156+
resource := NewLabeledResource("secret-rw-resource")
157+
resource.Secrecy.Label.Add("secret")
158+
resource.Integrity.Label.Add("trusted")
159+
160+
result := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationReadWrite)
161+
162+
assert.False(t, result.IsAllowed(), "Expected ReadWrite to be denied when write fails")
163+
})
164+
165+
t.Run("ReadWrite operation - allowed when both read and write pass", func(t *testing.T) {
166+
agentSecrecy := NewSecrecyLabel()
167+
agentSecrecy.Label.Add("secret")
168+
agentIntegrity := NewIntegrityLabel()
169+
agentIntegrity.Label.Add("trusted")
170+
171+
resource := NewLabeledResource("secret-rw-resource")
172+
resource.Secrecy.Label.Add("secret")
173+
resource.Integrity.Label.Add("trusted")
174+
175+
result := eval.Evaluate(agentSecrecy, agentIntegrity, resource, OperationReadWrite)
176+
177+
assert.True(t, result.IsAllowed(), "Expected ReadWrite to be allowed when both read and write pass: %s", result.Reason)
178+
})
179+
}
180+
181+
func TestNewEvaluatorWithMode(t *testing.T) {
182+
tests := []struct {
183+
name string
184+
mode EnforcementMode
185+
wantMode EnforcementMode
186+
}{
187+
{
188+
name: "strict mode",
189+
mode: EnforcementStrict,
190+
wantMode: EnforcementStrict,
191+
},
192+
{
193+
name: "filter mode",
194+
mode: EnforcementFilter,
195+
wantMode: EnforcementFilter,
196+
},
197+
{
198+
name: "propagate mode",
199+
mode: EnforcementPropagate,
200+
wantMode: EnforcementPropagate,
201+
},
202+
}
203+
204+
for _, tt := range tests {
205+
t.Run(tt.name, func(t *testing.T) {
206+
eval := NewEvaluatorWithMode(tt.mode)
207+
require.NotNil(t, eval)
208+
assert.Equal(t, tt.wantMode, eval.GetMode(), "Evaluator mode should match requested mode")
209+
})
210+
}
211+
}
212+
213+
func TestEvaluatorSetMode(t *testing.T) {
214+
eval := NewEvaluator()
215+
assert.Equal(t, EnforcementStrict, eval.GetMode(), "Default mode should be strict")
216+
217+
eval.SetMode(EnforcementFilter)
218+
assert.Equal(t, EnforcementFilter, eval.GetMode(), "Mode should be updated to filter")
219+
220+
eval.SetMode(EnforcementPropagate)
221+
assert.Equal(t, EnforcementPropagate, eval.GetMode(), "Mode should be updated to propagate")
141222
}
142223

143224
func TestFormatViolationError(t *testing.T) {
@@ -385,9 +466,7 @@ func TestAgentRegistry(t *testing.T) {
385466

386467
t.Run("GetOrCreate creates new agent", func(t *testing.T) {
387468
agent := registry.GetOrCreate("agent-1")
388-
if agent.AgentID != "agent-1" {
389-
t.Errorf("Expected agent ID to be 'agent-1', got %s", agent.AgentID)
390-
}
469+
assert.Equal(t, "agent-1", agent.AgentID, "Expected agent ID to be 'agent-1'")
391470

392471
// Should have empty labels initially
393472
assert.True(t, agent.Secrecy.Label.IsEmpty(), "Expected new agent to have empty secrecy labels")
@@ -476,11 +555,7 @@ func TestCollectionFiltering(t *testing.T) {
476555

477556
filtered := eval.FilterCollection(agentSecrecy, agentIntegrity, collection, OperationRead)
478557

479-
if filtered.GetAccessibleCount() != 1 {
480-
t.Errorf("Expected 1 accessible item, got %d", filtered.GetAccessibleCount())
481-
}
482-
if filtered.GetFilteredCount() != 1 {
483-
t.Errorf("Expected 1 filtered item, got %d", filtered.GetFilteredCount())
484-
}
558+
assert.Equal(t, 1, filtered.GetAccessibleCount(), "Expected 1 accessible item")
559+
assert.Equal(t, 1, filtered.GetFilteredCount(), "Expected 1 filtered item")
485560
})
486561
}

0 commit comments

Comments
 (0)