(t *testing.T)
| 690 | } |
| 691 | |
| 692 | func TestAnalyzeFirewallPolicy(t *testing.T) { |
| 693 | t.Run("full enrichment", func(t *testing.T) { |
| 694 | dir := t.TempDir() |
| 695 | auditDir := filepath.Join(dir, "sandbox", "firewall", "audit") |
| 696 | require.NoError(t, os.MkdirAll(auditDir, 0755)) |
| 697 | |
| 698 | // Write policy manifest |
| 699 | manifest := PolicyManifest{ |
| 700 | Version: 1, |
| 701 | GeneratedAt: "2026-01-01T00:00:00Z", |
| 702 | Rules: []PolicyRule{ |
| 703 | {ID: "allow-github", Order: 1, Action: "allow", ACLName: "allowed_domains", Protocol: "both", Domains: []string{".github.com"}, Description: "Allow GitHub"}, |
| 704 | {ID: "deny-all", Order: 2, Action: "deny", ACLName: "all", Protocol: "both", Domains: []string{}, Description: "Block all other traffic"}, |
| 705 | }, |
| 706 | } |
| 707 | manifestData, err := json.Marshal(manifest) |
| 708 | require.NoError(t, err) |
| 709 | require.NoError(t, os.WriteFile(filepath.Join(auditDir, "policy-manifest.json"), manifestData, 0644)) |
| 710 | |
| 711 | // Write audit JSONL |
| 712 | jsonl := `{"ts":1.0,"host":"api.github.com:443","method":"CONNECT","status":200,"decision":"TCP_TUNNEL"} |
| 713 | {"ts":2.0,"host":"evil.com:443","method":"CONNECT","status":403,"decision":"NONE_NONE"} |
| 714 | ` |
| 715 | require.NoError(t, os.WriteFile(filepath.Join(auditDir, "audit.jsonl"), []byte(jsonl), 0644)) |
| 716 | |
| 717 | analysis, err := analyzeFirewallPolicy(dir, false) |
| 718 | require.NoError(t, err, "Should analyze without error") |
| 719 | require.NotNil(t, analysis, "Analysis should not be nil") |
| 720 | |
| 721 | assert.Equal(t, 2, analysis.TotalRequests, "Should have 2 total requests") |
| 722 | assert.Equal(t, 1, analysis.AllowedCount, "Should have 1 allowed request") |
| 723 | assert.Equal(t, 1, analysis.DeniedCount, "Should have 1 denied request") |
| 724 | require.Len(t, analysis.DeniedRequests, 1, "Should have 1 denied request detail") |
| 725 | assert.Equal(t, "deny-all", analysis.DeniedRequests[0].RuleID, "Denied request should be attributed to deny-all") |
| 726 | }) |
| 727 | |
| 728 | t.Run("manifest only - no audit.jsonl", func(t *testing.T) { |
| 729 | dir := t.TempDir() |
| 730 | auditDir := filepath.Join(dir, "sandbox", "firewall", "audit") |
| 731 | require.NoError(t, os.MkdirAll(auditDir, 0755)) |
| 732 | |
| 733 | manifest := PolicyManifest{ |
| 734 | Version: 1, |
| 735 | Rules: []PolicyRule{{ID: "r1", Order: 1, Action: "allow", Domains: []string{".example.com"}}}, |
| 736 | SSLBumpEnabled: true, |
| 737 | } |
| 738 | manifestData, err := json.Marshal(manifest) |
| 739 | require.NoError(t, err) |
| 740 | require.NoError(t, os.WriteFile(filepath.Join(auditDir, "policy-manifest.json"), manifestData, 0644)) |
| 741 | |
| 742 | analysis, err := analyzeFirewallPolicy(dir, false) |
| 743 | require.NoError(t, err, "Should not error with manifest only") |
| 744 | require.NotNil(t, analysis, "Should return analysis with manifest-only data") |
| 745 | assert.Contains(t, analysis.PolicySummary, "SSL Bump enabled", "Should reflect SSL Bump enabled") |
| 746 | }) |
| 747 | |
| 748 | t.Run("no artifacts returns nil", func(t *testing.T) { |
| 749 | dir := t.TempDir() |
nothing calls this directly
no test coverage detected