Files
ssw-mvp/scard/code/server/policy/policy_test.go
T
admin 3247e9fd15 init: ssw-mvp initial commit
- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
2026-08-05 18:29:12 +08:00

387 lines
8.8 KiB
Go

package policy
import (
"testing"
"security-connector/common/types"
)
func TestNewPolicyEngine(t *testing.T) {
pe := NewPolicyEngine()
if pe == nil {
t.Fatal("PolicyEngine should not be nil")
}
if pe.policies == nil {
t.Error("Policies map should be initialized")
}
if pe.deviceRoles == nil {
t.Error("DeviceRoles map should be initialized")
}
}
func TestAddPolicy(t *testing.T) {
pe := NewPolicyEngine()
policy := &types.Policy{
PolicyID: "test_policy",
Name: "Test Policy",
Effect: types.PolicyAllow,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"read"},
Priority: 50,
Enabled: true,
}
if err := pe.AddPolicy(policy); err != nil {
t.Fatalf("AddPolicy failed: %v", err)
}
// Adding same ID replaces existing policy (no error in current impl)
policy2 := &types.Policy{
PolicyID: "test_policy",
Name: "Test Policy 2",
Effect: types.PolicyDeny,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"write"},
Priority: 50,
Enabled: true,
}
pe.AddPolicy(policy2) // Should not error, just replace
// Verify the policy was replaced
policies := pe.GetPoliciesForSubject("DEV_test_001")
if len(policies) != 1 {
t.Errorf("Should have 1 policy, got %d", len(policies))
}
}
func TestAddPolicyEmptyID(t *testing.T) {
pe := NewPolicyEngine()
policy := &types.Policy{
PolicyID: "",
Name: "Test Policy",
Effect: types.PolicyAllow,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"read"},
Priority: 50,
Enabled: true,
}
if err := pe.AddPolicy(policy); err == nil {
t.Error("AddPolicy should fail for empty policy ID")
}
}
func TestRemovePolicy(t *testing.T) {
pe := NewPolicyEngine()
policy := &types.Policy{
PolicyID: "remove_test",
Name: "Remove Test",
Effect: types.PolicyAllow,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"read"},
Priority: 50,
Enabled: true,
}
pe.AddPolicy(policy)
if err := pe.RemovePolicy("remove_test"); err != nil {
t.Fatalf("RemovePolicy failed: %v", err)
}
// Remove non-existent
if err := pe.RemovePolicy("nonexistent"); err == nil {
t.Error("RemovePolicy should fail for nonexistent policy")
}
}
func TestEvaluateAllow(t *testing.T) {
pe := NewPolicyEngine()
pe.AddPolicy(&types.Policy{
PolicyID: "allow_read",
Name: "Allow Read",
Effect: types.PolicyAllow,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"read"},
Priority: 50,
Enabled: true,
})
allowed, err := pe.Evaluate("DEV_test_001", "read", "resource_1", nil)
if err != nil {
t.Fatalf("Evaluate failed: %v", err)
}
if !allowed {
t.Error("Should be allowed")
}
}
func TestEvaluateDeny(t *testing.T) {
pe := NewPolicyEngine()
pe.AddPolicy(&types.Policy{
PolicyID: "deny_write",
Name: "Deny Write",
Effect: types.PolicyDeny,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"write"},
Priority: 50,
Enabled: true,
})
allowed, err := pe.Evaluate("DEV_test_001", "write", "resource_1", nil)
if err != nil {
t.Fatalf("Evaluate failed: %v", err)
}
if allowed {
t.Error("Should be denied")
}
}
func TestEvaluateNoMatchingPolicy(t *testing.T) {
pe := NewPolicyEngine()
// No policies added - default deny
allowed, err := pe.Evaluate("DEV_test_001", "read", "resource_1", nil)
if err != nil {
t.Fatalf("Evaluate failed: %v", err)
}
if allowed {
t.Error("Should be denied when no policy matches")
}
}
func TestEvaluatePriority(t *testing.T) {
pe := NewPolicyEngine()
// Lower priority allow
pe.AddPolicy(&types.Policy{
PolicyID: "allow_all",
Name: "Allow All",
Effect: types.PolicyAllow,
Subjects: []string{"*"},
Resources: []string{"*"},
Actions: []string{"*"},
Priority: 10,
Enabled: true,
})
// Higher priority deny
pe.AddPolicy(&types.Policy{
PolicyID: "deny_write",
Name: "Deny Write",
Effect: types.PolicyDeny,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"write"},
Priority: 50,
Enabled: true,
})
// Deny should take precedence due to higher priority
allowed, err := pe.Evaluate("DEV_test_001", "write", "resource_1", nil)
if err != nil {
t.Fatalf("Evaluate failed: %v", err)
}
if allowed {
t.Error("Higher priority deny should take precedence")
}
}
func TestEvaluateDisabledPolicy(t *testing.T) {
pe := NewPolicyEngine()
pe.AddPolicy(&types.Policy{
PolicyID: "disabled_policy",
Name: "Disabled Policy",
Effect: types.PolicyAllow,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"read"},
Priority: 50,
Enabled: false, // Disabled
})
// No other policies - default deny
allowed, err := pe.Evaluate("DEV_test_001", "read", "resource_1", nil)
if err != nil {
t.Fatalf("Evaluate failed: %v", err)
}
if allowed {
t.Error("Disabled policy should not match")
}
}
func TestSubjectMatches(t *testing.T) {
pe := NewPolicyEngine()
tests := []struct {
subject string
subjects []string
expected bool
}{
{"DEV_test_001", []string{"DEV_test_001"}, true},
{"DEV_test_001", []string{"DEV_test_002"}, false},
{"DEV_test_001", []string{"*"}, true},
}
for _, tt := range tests {
result := pe.subjectMatches(tt.subject, tt.subjects)
if result != tt.expected {
t.Errorf("subjectMatches(%s, %v) = %v, want %v", tt.subject, tt.subjects, result, tt.expected)
}
}
}
func TestResourceMatches(t *testing.T) {
pe := NewPolicyEngine()
tests := []struct {
resource string
resources []string
expected bool
}{
{"data_1", []string{"data_1"}, true},
{"data_1", []string{"data_2"}, false},
{"data_1", []string{"*"}, true},
{"data_123", []string{"data_*"}, true},
{"other_123", []string{"data_*"}, false},
}
for _, tt := range tests {
result := pe.resourceMatches(tt.resource, tt.resources)
if result != tt.expected {
t.Errorf("resourceMatches(%s, %v) = %v, want %v", tt.resource, tt.resources, result, tt.expected)
}
}
}
func TestActionMatches(t *testing.T) {
pe := NewPolicyEngine()
tests := []struct {
action string
actions []string
expected bool
}{
{"read", []string{"read"}, true},
{"read", []string{"write"}, false},
{"read", []string{"*"}, true},
}
for _, tt := range tests {
result := pe.actionMatches(tt.action, tt.actions)
if result != tt.expected {
t.Errorf("actionMatches(%s, %v) = %v, want %v", tt.action, tt.actions, result, tt.expected)
}
}
}
func TestAssignRole(t *testing.T) {
pe := NewPolicyEngine()
pe.AssignRole("DEV_test_001", "admin")
pe.AssignRole("DEV_test_001", "user")
roles, exists := pe.deviceRoles["DEV_test_001"]
if !exists {
t.Fatal("Device roles should exist")
}
if len(roles) != 2 {
t.Errorf("Should have 2 roles, got %d", len(roles))
}
}
func TestRemoveRole(t *testing.T) {
pe := NewPolicyEngine()
pe.AssignRole("DEV_test_001", "admin")
pe.AssignRole("DEV_test_001", "user")
pe.RemoveRole("DEV_test_001", "admin")
roles, exists := pe.deviceRoles["DEV_test_001"]
if !exists {
t.Fatal("Device roles should exist")
}
if len(roles) != 1 {
t.Errorf("Should have 1 role, got %d", len(roles))
}
}
func TestGetPoliciesForSubject(t *testing.T) {
pe := NewPolicyEngine()
pe.AddPolicy(&types.Policy{
PolicyID: "policy1",
Name: "Policy 1",
Effect: types.PolicyAllow,
Subjects: []string{"DEV_test_001"},
Resources: []string{"*"},
Actions: []string{"read"},
Priority: 50,
Enabled: true,
})
pe.AddPolicy(&types.Policy{
PolicyID: "policy2",
Name: "Policy 2",
Effect: types.PolicyAllow,
Subjects: []string{"DEV_test_002"},
Resources: []string{"*"},
Actions: []string{"write"},
Priority: 50,
Enabled: true,
})
policies := pe.GetPoliciesForSubject("DEV_test_001")
if len(policies) != 1 {
t.Errorf("Should have 1 policy, got %d", len(policies))
}
if policies[0].PolicyID != "policy1" {
t.Errorf("PolicyID should be policy1, got %s", policies[0].PolicyID)
}
}
func TestDefaultPolicies(t *testing.T) {
policies := DefaultPolicies()
if len(policies) == 0 {
t.Error("DefaultPolicies should return some policies")
}
// Check admin policy exists
var found bool
for _, p := range policies {
if p.PolicyID == "admin_all" {
found = true
break
}
}
if !found {
t.Error("admin_all policy should exist in defaults")
}
}
func TestSetAuditLogger(t *testing.T) {
pe := NewPolicyEngine()
pe.SetAuditLogger(func(e *types.AuditEvent) {
// Mock audit logger - just validates the function is callable
_ = e
})
// The audit logger would be called during policy evaluation if configured
// This just tests that it can be set without panic
if pe.auditLog == nil {
t.Error("Audit logger should be set")
}
}