| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "go/ast" |
| 5 | "go/parser" |
| 6 | "go/token" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestPlan14ProductionProofConstructors(t *testing.T) { |
| 13 | t.Parallel() |
| 14 | entries, err := os.ReadDir(".") |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | found := map[string]int{} |
| 19 | healthyWriter := 0 |
| 20 | settledWriter := 0 |
| 21 | for _, entry := range entries { |
| 22 | name := entry.Name() |
| 23 | if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { |
| 24 | continue |
| 25 | } |
| 26 | file, err := parser.ParseFile(token.NewFileSet(), name, nil, 0) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | ast.Inspect(file, func(n ast.Node) bool { |
| 31 | if lit, ok := n.(*ast.CompositeLit); ok { |
| 32 | if typ, ok := lit.Type.(*ast.Ident); ok && typ.Name == "proofRoot" { |
| 33 | for _, elt := range lit.Elts { |
| 34 | kv, ok := elt.(*ast.KeyValueExpr) |
| 35 | if !ok { |
| 36 | continue |
| 37 | } |
| 38 | key, ok := kv.Key.(*ast.Ident) |
| 39 | if !ok || key.Name != "healthyDir" { |
| 40 | continue |
| 41 | } |
| 42 | if isRootHealthy(kv.Value) { |
| 43 | healthyWriter++ |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | if call, ok := n.(*ast.CallExpr); ok { |
| 49 | if fn, ok := call.Fun.(*ast.SelectorExpr); ok && fn.Sel.Name == "recordSettled" && len(call.Args) == 1 && isRootHealthy(call.Args[0]) { |
| 50 | settledWriter++ |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | sel, ok := n.(*ast.SelectorExpr) |
| 55 | if !ok { |
| 56 | return true |
| 57 | } |
| 58 | pkg, ok := sel.X.(*ast.Ident) |
| 59 | if !ok || pkg.Name != "release" { |
| 60 | return true |
| 61 | } |
| 62 | if sel.Sel.Name == "NewProofGate" { |
| 63 | t.Errorf("%s retains writable healthy authority", name) |
| 64 | } |
| 65 | if sel.Sel.Name == "NewRootProofGate" { |
| 66 | found[name]++ |
| 67 | } |
| 68 | return true |
| 69 | }) |
| 70 | } |
| 71 | for _, name := range []string{"agent.go", "proofask.go", "heldstage.go", "movespass.go"} { |
| 72 | if found[name] != 1 { |
| 73 | t.Errorf("%s: trusted constructors = %d", name, found[name]) |
| 74 | } |
| 75 | } |
| 76 | if healthyWriter != 1 || settledWriter != 1 { |
| 77 | t.Fatalf("root healthy writers: observation=%d settle=%d", healthyWriter, settledWriter) |
| 78 | } |
| 79 | if _, err := os.Stat("movesheldrelease_production.go"); err == nil && found["movesheldrelease_production.go"] != 1 { |
| 80 | t.Fatal("moves root constructor not wired") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func isRootHealthy(expr ast.Expr) bool { |
| 85 | sel, ok := expr.(*ast.SelectorExpr) |
| 86 | if !ok || sel.Sel.Name != "RootHealthyDir" { |
| 87 | return false |
| 88 | } |
| 89 | pkg, ok := sel.X.(*ast.Ident) |
| 90 | return ok && pkg.Name == "release" |
| 91 | } |
| 92 | |