package release import ( "fmt" "os" "path/filepath" "testing" "time" ) func TestProofPolicyAbsentIsOff(t *testing.T) { p, err := LoadProofPolicy(filepath.Join(t.TempDir(), "proof-policy.json")) if p != nil || err != nil { t.Fatalf("absent = %+v, %v; want nil, nil (D3 off)", p, err) } if p.Requires("console") != nil || p.Proves("console") || p.ProveAfter() != DefaultProveAfter { t.Fatal("a nil policy is not inert") } } func TestProofPolicyParses(t *testing.T) { _, _, pubB64 := proverKey(t) path := filepath.Join(t.TempDir(), "proof-policy.json") body := fmt.Sprintf(`{"schema":1, "prove":{"apps":["console"],"prover":"dev","after":"20m","key_file":"/etc/carlos/proving.key"}, "require":[{"app":"console","prover":"dev","key":%q}]}`, pubB64) if err := os.WriteFile(path, []byte(body), 0o644); err != nil { t.Fatal(err) } p, err := LoadProofPolicy(path) if err != nil { t.Fatal(err) } if !p.Proves("console") || p.Proves("other") || p.ProveAfter() != 20*time.Minute { t.Fatalf("prove block = %+v", p.Prove) } req := p.Requires("console") if len(req) != 1 || req[0].Prover != "dev" || len(req[0].PublicKey()) == 0 { t.Fatalf("require = %+v", req) } if p.Requires("other") != nil { t.Fatal("an unlisted app is required") } } func TestProofPolicyRequireOnlyUsesTheDefaultAfter(t *testing.T) { _, _, pubB64 := proverKey(t) p, err := ParseProofPolicy([]byte(fmt.Sprintf(`{"schema":1,"require":[{"app":"console","prover":"dev","key":%q}]}`, pubB64))) if err != nil || p.ProveAfter() != DefaultProveAfter { t.Fatalf("p = %+v, %v", p, err) } } // A typo must never open the gate: every one of these is an error, which the // gate turns into "on and waiting". func TestProofPolicyUnparseableIsAnError(t *testing.T) { _, _, pubB64 := proverKey(t) for name, body := range map[string]string{ "empty": ``, "torn": `{"schema":1,"require":[`, "unknown key": fmt.Sprintf(`{"schema":1,"requires":[{"app":"console","prover":"dev","key":%q}]}`, pubB64), "schema 2": `{"schema":2}`, "bad key": `{"schema":1,"require":[{"app":"console","prover":"dev","key":"nope"}]}`, "bad prover": fmt.Sprintf(`{"schema":1,"require":[{"app":"console","prover":"Dev","key":%q}]}`, pubB64), "bad after": `{"schema":1,"prove":{"apps":["console"],"prover":"dev","after":"soon","key_file":"/k"}}`, "relative key": `{"schema":1,"prove":{"apps":["console"],"prover":"dev","after":"15m","key_file":"proving.key"}}`, "no apps": `{"schema":1,"prove":{"apps":[],"prover":"dev","after":"15m","key_file":"/k"}}`, "trailing junk": `{"schema":1} {}`, "trailing }": `{"schema":1}}`, "trailing ]": `{"schema":1}]`, "trailing word": `{"schema":1} x`, } { if _, err := ParseProofPolicy([]byte(body)); err == nil { t.Fatalf("%s: parsed, want an error", name) } } } // Trailing whitespace is still the end of input. func TestProofPolicyTrailingWhitespaceParses(t *testing.T) { if _, err := ParseProofPolicy([]byte("{\"schema\":1}\n\n \t")); err != nil { t.Fatal(err) } } // Absent means nothing is at the path. A dangling symlink or a directory // there is a broken config, and a broken config is closed, not off. func TestProofPolicyBrokenPathIsAnError(t *testing.T) { dir := t.TempDir() dangling := filepath.Join(dir, "dangling.json") if err := os.Symlink(filepath.Join(dir, "gone.json"), dangling); err != nil { t.Fatal(err) } if p, err := LoadProofPolicy(dangling); err == nil { t.Fatalf("dangling symlink = %+v, nil; want an error (closed), not off", p) } asDir := filepath.Join(dir, "dir.json") if err := os.Mkdir(asDir, 0o755); err != nil { t.Fatal(err) } if p, err := LoadProofPolicy(asDir); err == nil { t.Fatalf("directory = %+v, nil; want an error (closed), not off", p) } }