|
| 1 | +package detect |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func soft(id string, conf float64) Signal { |
| 6 | + return Signal{CheckID: id, Tier: TierSoft, ThreatType: ThreatToolPoisoning, Confidence: conf, Detail: id} |
| 7 | +} |
| 8 | +func hard(id string, conf float64) Signal { |
| 9 | + return Signal{CheckID: id, Tier: TierHard, ThreatType: ThreatPromptInjection, Confidence: conf, Detail: id} |
| 10 | +} |
| 11 | + |
| 12 | +func TestAggregateNoSignals(t *testing.T) { |
| 13 | + if _, ok := aggregate(ToolView{Name: "x"}, nil, "s"); ok { |
| 14 | + t.Fatal("no signals must yield ok=false") |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestAggregateHardIsDangerous(t *testing.T) { |
| 19 | + tool := ToolView{Server: "srv", Name: "calc"} |
| 20 | + f, ok := aggregate(tool, []Signal{hard("unicode.hidden", 0.95)}, "tpa-descriptions") |
| 21 | + if !ok { |
| 22 | + t.Fatal("expected a finding") |
| 23 | + } |
| 24 | + if f.ThreatLevel != ThreatLevelDangerous { |
| 25 | + t.Errorf("ThreatLevel = %q, want dangerous", f.ThreatLevel) |
| 26 | + } |
| 27 | + if f.Severity != SeverityCritical { |
| 28 | + t.Errorf("Severity = %q, want critical (escalated hard)", f.Severity) |
| 29 | + } |
| 30 | + if f.ThreatType != ThreatPromptInjection { |
| 31 | + t.Errorf("ThreatType = %q, want prompt_injection", f.ThreatType) |
| 32 | + } |
| 33 | + if f.Scanner != "tpa-descriptions" { |
| 34 | + t.Errorf("Scanner = %q", f.Scanner) |
| 35 | + } |
| 36 | + if f.Location != "srv:calc" { |
| 37 | + t.Errorf("Location = %q, want srv:calc", f.Location) |
| 38 | + } |
| 39 | + if len(f.Signals) != 1 || f.Signals[0] != "unicode.hidden" { |
| 40 | + t.Errorf("Signals = %v", f.Signals) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestAggregateHardNonEscalatedIsHigh(t *testing.T) { |
| 45 | + f, _ := aggregate(ToolView{Name: "t"}, []Signal{hard("shadowing.cross_server", 0.6)}, "s") |
| 46 | + if f.Severity != SeverityHigh { |
| 47 | + t.Errorf("Severity = %q, want high (non-escalated hard)", f.Severity) |
| 48 | + } |
| 49 | + if f.ThreatLevel != ThreatLevelDangerous { |
| 50 | + t.Errorf("ThreatLevel = %q, want dangerous", f.ThreatLevel) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestAggregateSoftSeverityLadder(t *testing.T) { |
| 55 | + cases := []struct { |
| 56 | + name string |
| 57 | + sigs []Signal |
| 58 | + want string |
| 59 | + }{ |
| 60 | + {"one→low", []Signal{soft("a", 0.4)}, SeverityLow}, |
| 61 | + {"two→medium", []Signal{soft("a", 0.4), soft("b", 0.3)}, SeverityMedium}, |
| 62 | + {"three→high", []Signal{soft("a", 0.3), soft("b", 0.3), soft("c", 0.3)}, SeverityHigh}, |
| 63 | + {"dupes count once", []Signal{soft("a", 0.2), soft("a", 0.2)}, SeverityLow}, |
| 64 | + } |
| 65 | + for _, tc := range cases { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + f, ok := aggregate(ToolView{Name: "t"}, tc.sigs, "s") |
| 68 | + if !ok { |
| 69 | + t.Fatal("expected finding") |
| 70 | + } |
| 71 | + if f.Severity != tc.want { |
| 72 | + t.Errorf("Severity = %q, want %q", f.Severity, tc.want) |
| 73 | + } |
| 74 | + if f.ThreatLevel != ThreatLevelWarning { |
| 75 | + t.Errorf("soft-only ThreatLevel = %q, want warning", f.ThreatLevel) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestAggregateConsensusRaisesConfidence(t *testing.T) { |
| 82 | + single, _ := aggregate(ToolView{Name: "t"}, []Signal{soft("a", 0.5)}, "s") |
| 83 | + double, _ := aggregate(ToolView{Name: "t"}, []Signal{soft("a", 0.5), soft("b", 0.4)}, "s") |
| 84 | + if !(double.Confidence > single.Confidence) { |
| 85 | + t.Errorf("consensus confidence %v not greater than single %v", double.Confidence, single.Confidence) |
| 86 | + } |
| 87 | + if single.Confidence != 0.5 { |
| 88 | + t.Errorf("single confidence = %v, want 0.5", single.Confidence) |
| 89 | + } |
| 90 | + // Independent signals add, capped at 1.0. |
| 91 | + capped, _ := aggregate(ToolView{Name: "t"}, []Signal{soft("a", 0.7), soft("b", 0.8)}, "s") |
| 92 | + if capped.Confidence != 1.0 { |
| 93 | + t.Errorf("capped confidence = %v, want 1.0", capped.Confidence) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestAggregateDistinctSignalsList(t *testing.T) { |
| 98 | + f, _ := aggregate(ToolView{Name: "t"}, []Signal{soft("b", 0.2), soft("a", 0.2), soft("b", 0.2)}, "s") |
| 99 | + // First-seen order, deduped. |
| 100 | + if len(f.Signals) != 2 || f.Signals[0] != "b" || f.Signals[1] != "a" { |
| 101 | + t.Errorf("Signals = %v, want [b a]", f.Signals) |
| 102 | + } |
| 103 | +} |
0 commit comments