Repository navigation
Expand file tree
/
Copy pathengine_parity_test.go
More file actions
507 lines (480 loc) · 18.2 KB
/
Copy pathengine_parity_test.go
File metadata and controls
507 lines (480 loc) · 18.2 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//go:build !no_re2
package redact
import (
_ "embed"
"encoding/json"
"reflect"
"slices"
"testing"
"github.com/peasant-labs/schema"
re2 "github.com/wasilibs/go-re2"
"gopkg.in/yaml.v3"
)
//go:embed testdata/engine_parity.yaml
var engineParityData []byte
const engineParityTestFixturePath = "testdata/engine_parity.yaml"
type engineParityCase struct {
Name string `yaml:"name"`
Input string `yaml:"input"`
Output string `yaml:"output"`
Rules []string `yaml:"rules"`
Patterns []UserPattern `yaml:"patterns"`
Reports []engineExpectedReport `yaml:"reports"`
RequiredReportNames []string `yaml:"required_report_names"`
// Stable, when set, asks that RedactText return Output unchanged when it
// is given Output: a second pass changes nothing.
Stable bool `yaml:"stable"`
}
type engineExpectedReport struct {
Name string `yaml:"name"`
Total int `yaml:"total"`
Counts map[string]int `yaml:"counts"`
}
type engineParityFixtures struct {
RequiredNames []string `yaml:"required_names"`
Cases []engineParityCase `yaml:"cases"`
LiteralReplacement string `yaml:"literal_replacement"`
InvalidPattern string `yaml:"invalid_pattern"`
XDGDataHome string `yaml:"xdg_data_home"`
RoutingInput string `yaml:"routing_input"`
TitleRoutingInput string `yaml:"title_routing_input"`
RequiredOperations []string `yaml:"required_operations"`
Operations []string `yaml:"operations"`
}
func loadEngineParityFixtures(t *testing.T) engineParityFixtures {
t.Helper()
var fixtures engineParityFixtures
if err := yaml.Unmarshal(engineParityData, &fixtures); err != nil {
t.Fatal(err)
}
names := fixtureNames(fixtures.Cases, func(row engineParityCase) string { return row.Name })
if err := requireFixtureNames(engineParityTestFixturePath, "required_names", fixtures.RequiredNames, names); err != nil {
t.Fatal(err)
}
if err := requireFixtureNames(engineParityTestFixturePath, "required_operations", fixtures.RequiredOperations, fixtures.Operations); err != nil {
t.Fatal(err)
}
for _, row := range fixtures.Cases {
if err := requireFixtureNames(engineParityTestFixturePath, row.Name+"/required_report_names", row.RequiredReportNames, fixtureNames(row.Reports, func(report engineExpectedReport) string { return report.Name })); err != nil {
t.Fatal(err)
}
for _, report := range row.Reports {
total := 0
for _, count := range report.Counts {
if count <= 0 {
t.Fatalf("%s: expected per-rule counts must be positive", row.Name)
}
total += count
}
if report.Counts == nil || report.Total != total {
t.Fatalf("%s: expected report counts missing or do not sum to total", row.Name)
}
}
}
seen := make(map[string]bool)
for _, name := range names {
if seen[name] {
t.Fatalf("duplicate engine fixture %q; give each case a unique name", name)
}
seen[name] = true
}
if fixtures.LiteralReplacement == "" || fixtures.InvalidPattern == "" || fixtures.XDGDataHome == "" || fixtures.RoutingInput == "" || fixtures.TitleRoutingInput == "" {
t.Fatal("engine fixture settings missing; restore literal replacement, invalid pattern, and XDG data home")
}
return fixtures
}
// engineCorpus reuses reviewed synthetic inputs rather than copying the rule
// catalog into another fixture. Every production rule must match at least one
// input and miss at least one input; filter rules must both keep and suppress.
func engineCorpus(t *testing.T) []engineParityCase {
t.Helper()
corpus := loadEngineParityFixtures(t).Cases
behavior := loadRedactorBehaviorFixtures(t)
for _, family := range [][]textRedactionFixture{behavior.Secrets, behavior.PII, behavior.Paths} {
for _, row := range family {
corpus = append(corpus, engineParityCase{Name: "redactor/" + row.ID, Input: row.Input})
}
}
rules := mustLoadRuleFixtures(t)
for _, family := range rules.Rules {
for _, row := range family.Cases {
corpus = append(corpus, engineParityCase{Name: family.RuleID + "/" + row.Name, Input: row.Input})
}
}
project, err := loadProjectRuleFixtures()
if err != nil {
t.Fatal(err)
}
for _, family := range project.Rules {
for _, row := range family.Cases {
corpus = append(corpus, engineParityCase{Name: family.RuleID + "/" + row.Name, Input: row.Input})
}
}
for _, row := range rules.Residues {
corpus = append(corpus, engineParityCase{Name: "residue/" + row.Name, Input: row.Input})
}
for _, row := range project.Residues {
corpus = append(corpus, engineParityCase{Name: "residue/" + row.Name, Input: row.Input})
}
for _, row := range loadResidueFixtures(t).PositiveTriggers {
corpus = append(corpus, engineParityCase{Name: "residue/" + row.ID, Input: row.Input})
}
falsePositives, err := LoadFalsePositiveFixtures()
if err != nil {
t.Fatal(err)
}
for _, row := range falsePositives {
corpus = append(corpus, engineParityCase{Name: row.Name, Input: row.Input})
}
return corpus
}
func TestEngineRuleParity(t *testing.T) {
e := goRE2Engine{}
if e.Identity() != engineGoRE2 {
t.Fatal("go-re2 adapter reports the wrong engine identity")
}
fixtures := loadEngineParityFixtures(t)
corpus := engineCorpus(t)
for _, rule := range Rules {
t.Run(rule.ID, func(t *testing.T) {
pattern, err := (goRE2Engine{}).Compile(rule.Pattern.String())
if err != nil {
t.Fatal(err)
}
if pattern.String() != rule.Pattern.String() {
t.Fatal("adapter changed the pattern source")
}
var matched, missed, kept, suppressed bool
for _, row := range corpus {
want := rule.Pattern.FindAllStringIndex(row.Input, -1)
got := pattern.FindAllStringIndex(row.Input, -1)
if !reflect.DeepEqual(got, want) {
t.Fatalf("%s: spans = %v, stdlib = %v", row.Name, got, want)
}
matched = matched || len(want) > 0
missed = missed || len(want) == 0
if got, want := pattern.ReplaceAllString(row.Input, rule.Replacement), rule.Pattern.ReplaceAllString(row.Input, rule.Replacement); got != want {
t.Fatalf("%s: replacement = %q, stdlib = %q", row.Name, got, want)
}
if got, want := pattern.ReplaceAllLiteralString(row.Input, fixtures.LiteralReplacement), rule.Pattern.ReplaceAllLiteralString(row.Input, fixtures.LiteralReplacement); got != want {
t.Fatalf("%s: literal replacement = %q, stdlib = %q", row.Name, got, want)
}
for _, loc := range want {
if rule.FilterFn != nil {
accepted := rule.FilterFn(row.Input[loc[0]:loc[1]], row.Input, loc[0])
kept = kept || accepted
suppressed = suppressed || !accepted
}
}
}
if !matched || !missed || (rule.FilterFn != nil && (!kept || !suppressed)) {
t.Fatalf("incomplete corpus: match=%v miss=%v filter_keep=%v filter_suppress=%v; add the missing synthetic boundary", matched, missed, kept, suppressed)
}
})
}
if pattern, err := (goRE2Engine{}).Compile(fixtures.InvalidPattern); err == nil || pattern != nil {
t.Fatalf("invalid pattern: got %v, %v; compilation must refuse", pattern, err)
}
}
func newEngineTestRedactor(t *testing.T, e engine, patterns []UserPattern) Redactor {
t.Helper()
fixtures := loadEngineParityFixtures(t)
r, err := newRedactorWithEngine(e, Standard, patterns, XDGPaths{DataHome: fixtures.XDGDataHome})
if err != nil {
t.Fatal(err)
}
return r
}
func assertEngineReports(t *testing.T, actual, baseline RedactionReport) {
t.Helper()
// Legacy category insertion iterates a count map, so its order is not a
// contract. Preserve exact membership, counts, warnings, and last matches.
slices.Sort(actual.Categories)
slices.Sort(baseline.Categories)
if !reflect.DeepEqual(actual, baseline) {
t.Fatalf("report = %#v, stdlib = %#v", actual, baseline)
}
}
func TestEnginePublicTextParity(t *testing.T) {
baseline := newEngineTestRedactor(t, goRegexpEngine{}, nil)
actual := newEngineTestRedactor(t, goRE2Engine{}, nil)
for _, row := range engineCorpus(t) {
t.Run(row.Name, func(t *testing.T) {
wantMatches, gotMatches := baseline.Detect(row.Input), actual.Detect(row.Input)
if !reflect.DeepEqual(gotMatches, wantMatches) {
t.Fatalf("Detect = %v, stdlib = %v", gotMatches, wantMatches)
}
if got, want := actual.Redact(row.Input, gotMatches), baseline.Redact(row.Input, wantMatches); got != want {
t.Fatalf("Redact = %q, stdlib = %q", got, want)
}
if got, want := actual.RedactText(row.Input), baseline.RedactText(row.Input); got != want {
t.Fatalf("RedactText = %q, stdlib = %q", got, want)
}
assertEngineReports(t, actual.Report(), baseline.Report())
})
}
}
func TestEnginePublicStructuredParity(t *testing.T) {
for _, row := range loadEngineParityFixtures(t).Cases {
t.Run(row.Name, func(t *testing.T) {
actual := newEngineTestRedactor(t, goRE2Engine{}, row.Patterns)
baseline := newEngineTestRedactor(t, goRegexpEngine{}, row.Patterns)
matches := actual.Detect(row.Input)
ids := make([]string, 0, len(matches))
for _, match := range matches {
ids = append(ids, match.Rule)
}
if !slices.Equal(ids, row.Rules) {
t.Fatalf("rule order = %v, want %v", ids, row.Rules)
}
for call, expected := range row.Reports {
if got := actual.RedactText(row.Input); got != row.Output {
t.Fatalf("go-re2 call %d: RedactText = %q, want %q", call+1, got, row.Output)
}
assertExpectedEngineReport(t, engineGoRE2, call+1, actual.Report(), expected)
if got := baseline.RedactText(row.Input); got != row.Output {
t.Fatalf("stdlib call %d: RedactText = %q, want %q", call+1, got, row.Output)
}
assertExpectedEngineReport(t, engineGoRegexp, call+1, baseline.Report(), expected)
assertEngineReports(t, actual.Report(), baseline.Report())
}
if row.Stable {
for name, redactor := range map[engineName]Redactor{engineGoRE2: actual, engineGoRegexp: baseline} {
if again := redactor.RedactText(row.Output); again != row.Output {
t.Fatalf("%s: second RedactText pass over %q = %q, want it unchanged", name, row.Output, again)
}
}
}
// unstring-allow: go-json-body-map — dynamic JSON shape the redactor must traverse
value := []any{row.Input, map[string]any{"text": row.Input}, json.Number("9007199254740993")}
if got, want := actual.RedactJSON(value), baseline.RedactJSON(value); !reflect.DeepEqual(got, want) {
t.Fatalf("RedactJSON = %#v, stdlib = %#v", got, want)
}
data, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
if got, want := RedactJSONDocBytes(actual, data), RedactJSONDocBytes(baseline, data); string(got) != string(want) {
t.Fatalf("RedactJSONDocBytes = %s, stdlib = %s", got, want)
}
got, gotErr := RedactJSONLBytes(actual, append(data, '\n'))
want, wantErr := RedactJSONLBytes(baseline, append(data, '\n'))
if gotErr != nil || wantErr != nil || string(got) != string(want) {
t.Fatalf("RedactJSONLBytes = %s, %v; stdlib = %s, %v", got, gotErr, want, wantErr)
}
meta := schema.NewUnifiedMetadata()
meta.CWD, meta.Project.FilePath, meta.Source.FilePath = row.Input, row.Input, row.Input
meta.Git.Remote = &row.Input
before := meta
if got, want := actual.RedactMetadata(&meta), baseline.RedactMetadata(&meta); !reflect.DeepEqual(got, want) {
t.Fatalf("RedactMetadata = %#v, stdlib = %#v", got, want)
}
if !reflect.DeepEqual(meta, before) {
t.Fatal("RedactMetadata mutated its input")
}
if actual.Level() != baseline.Level() || actual.RuleSetVersion() != baseline.RuleSetVersion() {
t.Fatal("engine changed level or rule-set version")
}
})
}
}
func assertExpectedEngineReport(t *testing.T, name engineName, call int, got RedactionReport, want engineExpectedReport) {
t.Helper()
if got.TotalRedactions != want.Total || !reflect.DeepEqual(got.Counts, want.Counts) {
t.Fatalf("%s call %d: report total=%d counts=%v, want total=%d counts=%v", name, call, got.TotalRedactions, got.Counts, want.Total, want.Counts)
}
}
func TestEngineConfiguredAndXDGRulesCompiled(t *testing.T) {
for _, row := range loadEngineParityFixtures(t).Cases {
t.Run(row.Name, func(t *testing.T) {
r := newEngineTestRedactor(t, goRE2Engine{}, row.Patterns).(*DefaultRedactor)
if r.configuredRuleCount != len(row.Patterns) {
t.Fatal("configured rules missing from the constructor")
}
var xdgFound bool
for index, rule := range r.userRules {
// Inspect the actual map, not lookup: its stdlib fallback could
// otherwise conceal an uncompiled configured or generated rule.
pattern, exists := r.patterns[rule.Pattern]
if _, ok := pattern.(*re2.Regexp); !exists || !ok {
t.Fatalf("%s: engine map entry = %T, present=%v; want *re2.Regexp", rule.ID, pattern, exists)
}
if index >= r.configuredRuleCount && rule.ID == "xdg_data_home" {
xdgFound = true
}
}
if !xdgFound {
t.Fatal("generated XDG data-home rule missing; the routing assertion must not be vacuous")
}
})
}
}
func TestEngineConcurrentTextParity(t *testing.T) {
actual := newEngineTestRedactor(t, goRE2Engine{}, nil)
baseline := newEngineTestRedactor(t, goRegexpEngine{}, nil)
for _, row := range loadEngineParityFixtures(t).Cases {
t.Run(row.Name, func(t *testing.T) {
t.Parallel()
if got, want := actual.RedactText(row.Input), baseline.RedactText(row.Input); got != want {
t.Fatalf("concurrent RedactText = %q, stdlib = %q", got, want)
}
// Exercise report snapshots while other calls update the same
// redactor. Last-call match ordering is intentionally unspecified
// across concurrent calls, so only output parity is asserted.
actual.Report()
})
}
}
func TestEngineResidueParity(t *testing.T) {
patterns, err := compileRulePatterns(goRE2Engine{}, nil, nil, ResidueRules)
if err != nil {
t.Fatal(err)
}
actual := &DefaultResidueDetector{patterns: patterns}
// Keep the baseline explicitly on stdlib. NewResidueDetector follows the
// build default, so using it here would make the parity test compare go-re2
// with itself once the default changes.
baseline, err := newResidueDetector(goRegexpEngine{})
if err != nil {
t.Fatal(err)
}
seen := make(map[string]bool)
for _, row := range engineCorpus(t) {
got, want := actual.Scan(row.Input), baseline.Scan(row.Input)
if !reflect.DeepEqual(got, want) {
t.Fatalf("%s: residue = %v, stdlib = %v", row.Name, got, want)
}
for _, warning := range got {
seen[warning.RuleID] = true
}
}
for _, rule := range ResidueRules {
if !seen[rule.ID] {
t.Errorf("residue rule %s has no positive corpus input; add a synthetic case", rule.ID)
}
}
}
func TestEngineTitleParity(t *testing.T) {
fixtures, err := loadTitleFixtures()
if err != nil {
t.Fatal(err)
}
actual, err := newTitlePipelineWithEngine(goRE2Engine{})
if err != nil {
t.Fatal(err)
}
baseline, err := NewTitlePipeline()
if err != nil {
t.Fatal(err)
}
for _, row := range fixtures.Cases {
t.Run(row.Name, func(t *testing.T) {
context := TitleContext{Harness: row.Harness, ProjectPath: row.ProjectPath}
got, gotErr := actual.Generate(row.Input, context)
want, wantErr := baseline.Generate(row.Input, context)
if !reflect.DeepEqual(got, want) || engineErrorText(gotErr) != engineErrorText(wantErr) {
t.Fatalf("Generate = %#v, %v; stdlib = %#v, %v", got, gotErr, want, wantErr)
}
got, gotErr = actual.Sanitize(row.Input, context)
want, wantErr = baseline.Sanitize(row.Input, context)
if !reflect.DeepEqual(got, want) || engineErrorText(gotErr) != engineErrorText(wantErr) {
t.Fatalf("Sanitize = %#v, %v; stdlib = %#v, %v", got, gotErr, want, wantErr)
}
})
}
}
func engineErrorText(err error) string {
if err == nil {
return ""
}
return err.Error()
}
// trackingEngine delegates to the actual adapter. It detects wiring regressions
// that differential output alone cannot catch when both engines agree.
type trackingEngine struct {
engine
scans, replacements int
}
func (e *trackingEngine) Compile(pattern string) (compiled, error) {
p, err := e.engine.Compile(pattern)
if err != nil {
return nil, err
}
return trackingPattern{compiled: p, engine: e}, nil
}
type trackingPattern struct {
compiled
engine *trackingEngine
}
func (p trackingPattern) FindAllStringIndex(s string, n int) [][]int {
p.engine.scans++
return p.compiled.FindAllStringIndex(s, n)
}
func (p trackingPattern) ReplaceAllString(s, repl string) string {
p.engine.replacements++
return p.compiled.ReplaceAllString(s, repl)
}
func TestEnginePublicPathUsesAdapter(t *testing.T) {
fixtures := loadEngineParityFixtures(t)
for _, operation := range fixtures.Operations {
t.Run(operation, func(t *testing.T) {
e := &trackingEngine{engine: goRE2Engine{}}
r := newEngineTestRedactor(t, e, nil)
input := fixtures.RoutingInput
matches := r.Detect(input)
e.scans, e.replacements = 0, 0
switch operation {
case "Detect":
r.Detect(input)
case "Redact":
r.Redact(input, matches)
case "RedactText":
r.RedactText(input)
case "RedactJSON":
r.RedactJSON(input)
case "RedactMetadata":
meta := schema.NewUnifiedMetadata()
meta.Git.Remote = &input
r.RedactMetadata(&meta)
case "RedactJSONLBytes", "RedactJSONDocBytes":
data, err := json.Marshal(input)
if err != nil {
t.Fatal(err)
}
if operation == "RedactJSONLBytes" {
if _, err := RedactJSONLBytes(r, data); err != nil {
t.Fatal(err)
}
} else {
RedactJSONDocBytes(r, data)
}
case "Generate", "Sanitize":
p, err := newTitlePipelineWithEngine(e)
if err != nil {
t.Fatal(err)
}
// A non-path input survives title path normalization and reaches
// the canonical rule scanner through the selected engine.
if operation == "Generate" {
_, err = p.Generate(fixtures.TitleRoutingInput, TitleContext{})
} else {
_, err = p.Sanitize(fixtures.TitleRoutingInput, TitleContext{})
}
if err != nil {
t.Fatal(err)
}
case "Scan":
r.(*DefaultRedactor).residueDetector.Scan(input)
default:
t.Fatalf("unknown engine operation %q; implement the production-path assertion", operation)
}
if operation == "Redact" {
if e.scans != 0 || e.replacements == 0 {
t.Fatalf("Redact scans=%d replacements=%d; must use the adapter without rescanning", e.scans, e.replacements)
}
} else if e.scans == 0 {
t.Fatal("public operation bypassed the selected adapter")
}
})
}
}