oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1// Command mutate proves the security gates can fail (spec §11: gates 1,
2// 4 and 8 each need a mutation test). For each mutation it breaks the
3// code on purpose, through `go test -overlay` so the tree is never
4// touched, runs the gate, and fails unless the gate fails.
5//
6// go run ./scripts/mutate # or: make mutate
7//
8// A mutation whose text no longer matches the source fails too: a
9// mutation that silently stops applying is a gate nobody is testing.
10package main
11
12import (
13 "encoding/json"
14 "fmt"
15 "os"
16 "os/exec"
17 "path/filepath"
18 "strings"
19)
20
21// edit replaces one exact, unique text in one file.
22type edit struct {
23 file, from, to string
24}
25
26// mutation is one way to break a gate, and the test that must notice.
27type mutation struct {
28 gate, name, test string
29 edits []edit
30 // pkg is the package whose test is the gate; ./internal/mapatest/
31 // when empty.
32 pkg string
33 // copy runs the gate in a mutated copy of the repository instead of
34 // through an overlay: for a gate that reads the source from disk
35 // (I8's egress scan, I17's callers), which an overlay cannot reach.
36 copy bool
37}
38
39// every is each mutation: the base gates', the images', the Notion
40// mapper's (mutations_mapper.go), the Notion import's
41// (mutations_import.go) and the File menu's (mutations_menus.go).
42func every() []mutation {
43 all := append([]mutation{}, mutations...)
44 all = append(all, imageMutations...)
45 all = append(all, lookMutations...)
46 all = append(all, insideMutations...)
47 all = append(all, columnMutations...)
48 all = append(all, mapperMutations...)
49 all = append(all, importMutations...)
50 all = append(all, importLookMutations...)
51 return append(all, menuMutations...)
52}
53
54func main() {
55 failed := 0
56 all := chosen(every())
57 for _, broken := range all {
58 caught, output, err := run(broken)
59 switch {
60 case err != nil:
61 fmt.Printf("ERROR gate %s: %s: %v\n", broken.gate,
62 broken.name, err)
63 failed++
64 case !caught:
65 fmt.Printf("MISSED gate %s: %s\n%s\n", broken.gate, broken.name,
66 output)
67 failed++
68 default:
69 fmt.Printf("caught gate %s: %s\n", broken.gate, broken.name)
70 }
71 }
72 if failed > 0 {
73 fmt.Printf("%d of %d mutations were not caught\n", failed,
74 len(all))
75 os.Exit(1)
76 }
77 fmt.Printf("all %d mutations caught\n", len(all))
78}
79
80// run applies a mutation through an overlay and runs its gate. It
81// reports whether the gate failed.
82func run(broken mutation) (bool, string, error) {
83 dir, err := os.MkdirTemp("", "mapa-mutate-")
84 if err != nil {
85 return false, "", err
86 }
87 defer os.RemoveAll(dir)
88 command, err := gateCommand(dir, broken)
89 if err != nil {
90 return false, "", err
91 }
92 command.Env = append(os.Environ(), "CGO_ENABLED=0")
93 output, err := command.CombinedOutput()
94 if strings.Contains(string(output), "[build failed]") ||
95 strings.Contains(string(output), "[setup failed]") {
96 return false, string(output), fmt.Errorf("the mutant did not build")
97 }
98 return err != nil, string(output), nil
99}
100
101// gateCommand is the go test that runs broken's gate, mutated: through
102// an overlay, or in a copy of the repository under dir.
103func gateCommand(dir string, broken mutation) (*exec.Cmd, error) {
104 pkg := firstOf(broken.pkg, "./internal/mapatest/")
105 if !broken.copy {
106 overlay, err := writeOverlay(dir, broken.edits)
107 if err != nil {
108 return nil, err
109 }
110 return exec.Command("go", "test", "-overlay", overlay, "-count=1",
111 "-run", broken.test, pkg), nil
112 }
113 if err := copyRepository(".", dir); err != nil {
114 return nil, err
115 }
116 for _, change := range broken.edits {
117 if err := mutateFile(filepath.Join(dir, change.file),
118 change); err != nil {
119 return nil, err
120 }
121 }
122 command := exec.Command("go", "test", "-count=1", "-run", broken.test,
123 pkg)
124 command.Dir = dir
125 return command, nil
126}
127
128// writeOverlay writes each mutated file into dir, and the overlay that
129// puts them in place of the originals.
130func writeOverlay(dir string, edits []edit) (string, error) {
131 replace := map[string]string{}
132 for number, change := range edits {
133 path, err := filepath.Abs(change.file)
134 if err != nil {
135 return "", err
136 }
137 source, err := os.ReadFile(firstOf(replace[path], path))
138 if err != nil {
139 return "", err
140 }
141 if count := strings.Count(string(source), change.from); count != 1 {
142 return "", fmt.Errorf("%s: the text to break occurs %d "+
143 "times, not once", change.file, count)
144 }
145 mutated := strings.Replace(string(source), change.from, change.to, 1)
146 target := filepath.Join(dir, fmt.Sprintf("%d.go", number))
147 if err := os.WriteFile(target, []byte(mutated), 0o600); err != nil {
148 return "", err
149 }
150 replace[path] = target
151 }
152 overlay := filepath.Join(dir, "overlay.json")
153 plan, _ := json.Marshal(map[string]any{"Replace": replace})
154 return overlay, os.WriteFile(overlay, plan, 0o600)
155}
156
157func firstOf(values ...string) string {
158 for _, value := range values {
159 if value != "" {
160 return value
161 }
162 }
163 return ""
164}
165
166// chosen is every mutation, or with MUTATE_GATES=IMG1,IMG7 only those
167// gates': for running one spec's mutants while working on it.
168func chosen(all []mutation) []mutation {
169 only := os.Getenv("MUTATE_GATES")
170 if only == "" {
171 return all
172 }
173 var out []mutation
174 for _, broken := range all {
175 for _, gate := range strings.Split(only, ",") {
176 if broken.gate == gate {
177 out = append(out, broken)
178 }
179 }
180 }
181 return out
182}
183