You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
56 lines
1.7 KiB
|
2 weeks ago
|
package capsule_test
|
||
|
|
|
||
|
|
import (
|
||
|
|
"crypto/sha256"
|
||
|
|
"encoding/hex"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"g.activething.com/go/DateKeys/internal/testkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
// The differential corpus of the pre-unlock checks,
|
||
|
|
// testdata/vectors/inspect_differential.json, replays: every mutation of an
|
||
|
|
// official fixture gets exactly the recorded verdict of Inspect, ok or an
|
||
|
|
// error code at a step. genfixtures regenerates the corpus, so that a change
|
||
|
|
// of verdict also shows as a change of the file.
|
||
|
|
func TestInspectDifferentialCorpus(t *testing.T) {
|
||
|
|
var f testkit.DifferentialFile
|
||
|
|
if err := testkit.ReadJSON("../testdata/vectors/inspect_differential.json", &f); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(f.Mutations) < 1500 {
|
||
|
|
t.Fatalf("%d mutations, want at least 1500", len(f.Mutations))
|
||
|
|
}
|
||
|
|
bases := make([][]byte, len(f.Bases))
|
||
|
|
for i, b := range f.Bases {
|
||
|
|
dkc, err := os.ReadFile(filepath.Join(fixtureDir, b.File))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if sum := sha256.Sum256(dkc); hex.EncodeToString(sum[:]) != b.SHA256 {
|
||
|
|
t.Fatalf("%s changed", b.File)
|
||
|
|
}
|
||
|
|
bases[i] = dkc
|
||
|
|
}
|
||
|
|
kinds := map[string]int{}
|
||
|
|
for i, m := range f.Mutations {
|
||
|
|
dkc, err := testkit.ApplyEdits(bases[m.Base], m.Edits)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("mutation %d: %v", i, err)
|
||
|
|
}
|
||
|
|
result, step := testkit.InspectVerdict(dkc)
|
||
|
|
if result != m.Result || step != m.Step {
|
||
|
|
t.Errorf("mutation %d (%s of %s, edits %v): got %s at step %d, want %s at step %d",
|
||
|
|
i, m.Kind, f.Bases[m.Base].File, m.Edits, result, step, m.Result, m.Step)
|
||
|
|
}
|
||
|
|
kinds[m.Kind]++
|
||
|
|
}
|
||
|
|
for _, k := range []string{"flip", "byte", "truncate", "insert", "delete", "length", "header", "datekey", "age"} {
|
||
|
|
if kinds[k] == 0 {
|
||
|
|
t.Errorf("no mutation of kind %s", k)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|