// Command mutate proves the security gates can fail (spec ยง11: gates 1, // 4 and 8 each need a mutation test). For each mutation it breaks the // code on purpose, through `go test -overlay` so the tree is never // touched, runs the gate, and fails unless the gate fails. // // go run ./scripts/mutate # or: make mutate // // A mutation whose text no longer matches the source fails too: a // mutation that silently stops applying is a gate nobody is testing. package main import ( "encoding/json" "fmt" "os" "os/exec" "path/filepath" "strings" ) // edit replaces one exact, unique text in one file. type edit struct { file, from, to string } // mutation is one way to break a gate, and the test that must notice. type mutation struct { gate, name, test string edits []edit // pkg is the package whose test is the gate; ./internal/mapatest/ // when empty. pkg string // copy runs the gate in a mutated copy of the repository instead of // through an overlay: for a gate that reads the source from disk // (I8's egress scan, I17's callers), which an overlay cannot reach. copy bool } // every is each mutation: the base gates', the images', the Notion // mapper's (mutations_mapper.go), the Notion import's // (mutations_import.go) and the File menu's (mutations_menus.go). func every() []mutation { all := append([]mutation{}, mutations...) all = append(all, imageMutations...) all = append(all, lookMutations...) all = append(all, insideMutations...) all = append(all, columnMutations...) all = append(all, mapperMutations...) all = append(all, importMutations...) all = append(all, importLookMutations...) return append(all, menuMutations...) } func main() { failed := 0 all := chosen(every()) for _, broken := range all { caught, output, err := run(broken) switch { case err != nil: fmt.Printf("ERROR gate %s: %s: %v\n", broken.gate, broken.name, err) failed++ case !caught: fmt.Printf("MISSED gate %s: %s\n%s\n", broken.gate, broken.name, output) failed++ default: fmt.Printf("caught gate %s: %s\n", broken.gate, broken.name) } } if failed > 0 { fmt.Printf("%d of %d mutations were not caught\n", failed, len(all)) os.Exit(1) } fmt.Printf("all %d mutations caught\n", len(all)) } // run applies a mutation through an overlay and runs its gate. It // reports whether the gate failed. func run(broken mutation) (bool, string, error) { dir, err := os.MkdirTemp("", "mapa-mutate-") if err != nil { return false, "", err } defer os.RemoveAll(dir) command, err := gateCommand(dir, broken) if err != nil { return false, "", err } command.Env = append(os.Environ(), "CGO_ENABLED=0") output, err := command.CombinedOutput() if strings.Contains(string(output), "[build failed]") || strings.Contains(string(output), "[setup failed]") { return false, string(output), fmt.Errorf("the mutant did not build") } return err != nil, string(output), nil } // gateCommand is the go test that runs broken's gate, mutated: through // an overlay, or in a copy of the repository under dir. func gateCommand(dir string, broken mutation) (*exec.Cmd, error) { pkg := firstOf(broken.pkg, "./internal/mapatest/") if !broken.copy { overlay, err := writeOverlay(dir, broken.edits) if err != nil { return nil, err } return exec.Command("go", "test", "-overlay", overlay, "-count=1", "-run", broken.test, pkg), nil } if err := copyRepository(".", dir); err != nil { return nil, err } for _, change := range broken.edits { if err := mutateFile(filepath.Join(dir, change.file), change); err != nil { return nil, err } } command := exec.Command("go", "test", "-count=1", "-run", broken.test, pkg) command.Dir = dir return command, nil } // writeOverlay writes each mutated file into dir, and the overlay that // puts them in place of the originals. func writeOverlay(dir string, edits []edit) (string, error) { replace := map[string]string{} for number, change := range edits { path, err := filepath.Abs(change.file) if err != nil { return "", err } source, err := os.ReadFile(firstOf(replace[path], path)) if err != nil { return "", err } if count := strings.Count(string(source), change.from); count != 1 { return "", fmt.Errorf("%s: the text to break occurs %d "+ "times, not once", change.file, count) } mutated := strings.Replace(string(source), change.from, change.to, 1) target := filepath.Join(dir, fmt.Sprintf("%d.go", number)) if err := os.WriteFile(target, []byte(mutated), 0o600); err != nil { return "", err } replace[path] = target } overlay := filepath.Join(dir, "overlay.json") plan, _ := json.Marshal(map[string]any{"Replace": replace}) return overlay, os.WriteFile(overlay, plan, 0o600) } func firstOf(values ...string) string { for _, value := range values { if value != "" { return value } } return "" } // chosen is every mutation, or with MUTATE_GATES=IMG1,IMG7 only those // gates': for running one spec's mutants while working on it. func chosen(all []mutation) []mutation { only := os.Getenv("MUTATE_GATES") if only == "" { return all } var out []mutation for _, broken := range all { for _, gate := range strings.Split(only, ",") { if broken.gate == gate { out = append(out, broken) } } } return out }