Repository navigation
Expand file tree
/
Copy pathtestdata_false_positives.go
More file actions
85 lines (77 loc) · 3.58 KB
/
Copy pathtestdata_false_positives.go
File metadata and controls
85 lines (77 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package redact
import (
_ "embed"
"fmt"
"gopkg.in/yaml.v3"
)
//go:embed testdata/false_positives.yaml
var falsePositiveFixtureData []byte
// FalsePositiveCase is a single fixture entry in testdata/false_positives.yaml.
// Each case specifies an input string, the rule under test, the expected outcome
// ("match" or "no_match"), a human-readable reason, and the provenance of the case.
type FalsePositiveCase struct {
// Name is a unique slug used as the t.Run subtest name.
Name string `yaml:"name"`
// Input is the text passed to Detect.
Input string `yaml:"input"`
// Rule is the rule ID to look for in the returned matches (e.g. "aws_secret_key").
Rule string `yaml:"rule"`
// Expect is either "match" or "no_match".
// "match" — the rule must fire on the input (true positive).
// "no_match" — the rule must NOT fire on the input (false positive guard).
Expect string `yaml:"expect"`
// Level is the redaction level at which to run Detect. Empty means Standard.
Level RedactionLevel `yaml:"level"`
// Reason documents why the case behaves as expected.
Reason string `yaml:"reason"`
// Source records where the pattern was observed:
// "real_transcript" — from an actual agent session
// "detect_secrets" — from the detect-secrets reference implementation
// "synthetic" — hand-crafted for coverage
Source string `yaml:"source"`
}
type falsePositiveFixtures struct {
// RequiredCaseNames is the reviewed manifest of the rows. The tests assert
// that it and the rows agree exactly, so a deleted row fails.
RequiredCaseNames []string `yaml:"requiredCaseNames"`
Cases []FalsePositiveCase `yaml:"cases"`
}
// LoadFalsePositiveFixtures parses the embedded YAML fixture file and returns
// all test cases. Returns an error if the YAML is malformed.
func LoadFalsePositiveFixtures() ([]FalsePositiveCase, error) {
return loadFalsePositiveFixtures(falsePositiveFixtureData)
}
// loadFalsePositiveFixtures keeps the embedded public loader small while
// allowing malformed and semantically invalid fixture bytes to exercise the
// same production parsing path in tests.
func loadFalsePositiveFixtures(data []byte) ([]FalsePositiveCase, error) {
f, err := parseFalsePositiveFixtures(data)
return f.Cases, err
}
func parseFalsePositiveFixtures(data []byte) (falsePositiveFixtures, error) {
var f falsePositiveFixtures
if err := yaml.Unmarshal(data, &f); err != nil {
return falsePositiveFixtures{}, &actionableError{
what: "the false-positive fixture YAML could not be parsed",
why: err.Error(),
where: "testdata/false_positives.yaml via redact.LoadFalsePositiveFixtures",
when: "loading embedded redaction fixtures before running the false-positive corpus",
means: "the corpus cannot run, so rule false-positive coverage is unavailable",
fix: "fix the YAML syntax in testdata/false_positives.yaml",
cause: err,
}
}
for i, fixture := range f.Cases {
if fixture.Level != "" && !fixture.Level.IsValid() {
return falsePositiveFixtures{}, &actionableError{
what: fmt.Sprintf("fixture case %q at cases[%d] has invalid redaction level %q", fixture.Name, i, fixture.Level),
why: "fixture levels must be minimal, standard, maximum, or empty for the Standard default",
where: "testdata/false_positives.yaml via redact.LoadFalsePositiveFixtures",
when: "validating embedded fixtures before selecting a redactor level",
means: "the test cannot safely choose a level and the false-positive corpus cannot run",
fix: "set the fixture level to minimal, standard, maximum, or remove it to use Standard",
}
}
}
return f, nil
}