carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package release
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "testing"
8 "time"
9)
10
11func TestProofPolicyAbsentIsOff(t *testing.T) {
12 p, err := LoadProofPolicy(filepath.Join(t.TempDir(), "proof-policy.json"))
13 if p != nil || err != nil {
14 t.Fatalf("absent = %+v, %v; want nil, nil (D3 off)", p, err)
15 }
16 if p.Requires("console") != nil || p.Proves("console") || p.ProveAfter() != DefaultProveAfter {
17 t.Fatal("a nil policy is not inert")
18 }
19}
20
21func TestProofPolicyParses(t *testing.T) {
22 _, _, pubB64 := proverKey(t)
23 path := filepath.Join(t.TempDir(), "proof-policy.json")
24 body := fmt.Sprintf(`{"schema":1,
25 "prove":{"apps":["console"],"prover":"dev","after":"20m","key_file":"/etc/carlos/proving.key"},
26 "require":[{"app":"console","prover":"dev","key":%q}]}`, pubB64)
27 if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
28 t.Fatal(err)
29 }
30 p, err := LoadProofPolicy(path)
31 if err != nil {
32 t.Fatal(err)
33 }
34 if !p.Proves("console") || p.Proves("other") || p.ProveAfter() != 20*time.Minute {
35 t.Fatalf("prove block = %+v", p.Prove)
36 }
37 req := p.Requires("console")
38 if len(req) != 1 || req[0].Prover != "dev" || len(req[0].PublicKey()) == 0 {
39 t.Fatalf("require = %+v", req)
40 }
41 if p.Requires("other") != nil {
42 t.Fatal("an unlisted app is required")
43 }
44}
45
46func TestProofPolicyRequireOnlyUsesTheDefaultAfter(t *testing.T) {
47 _, _, pubB64 := proverKey(t)
48 p, err := ParseProofPolicy([]byte(fmt.Sprintf(`{"schema":1,"require":[{"app":"console","prover":"dev","key":%q}]}`, pubB64)))
49 if err != nil || p.ProveAfter() != DefaultProveAfter {
50 t.Fatalf("p = %+v, %v", p, err)
51 }
52}
53
54// A typo must never open the gate: every one of these is an error, which the
55// gate turns into "on and waiting".
56func TestProofPolicyUnparseableIsAnError(t *testing.T) {
57 _, _, pubB64 := proverKey(t)
58 for name, body := range map[string]string{
59 "empty": ``,
60 "torn": `{"schema":1,"require":[`,
61 "unknown key": fmt.Sprintf(`{"schema":1,"requires":[{"app":"console","prover":"dev","key":%q}]}`, pubB64),
62 "schema 2": `{"schema":2}`,
63 "bad key": `{"schema":1,"require":[{"app":"console","prover":"dev","key":"nope"}]}`,
64 "bad prover": fmt.Sprintf(`{"schema":1,"require":[{"app":"console","prover":"Dev","key":%q}]}`, pubB64),
65 "bad after": `{"schema":1,"prove":{"apps":["console"],"prover":"dev","after":"soon","key_file":"/k"}}`,
66 "relative key": `{"schema":1,"prove":{"apps":["console"],"prover":"dev","after":"15m","key_file":"proving.key"}}`,
67 "no apps": `{"schema":1,"prove":{"apps":[],"prover":"dev","after":"15m","key_file":"/k"}}`,
68 "trailing junk": `{"schema":1} {}`,
69 "trailing }": `{"schema":1}}`,
70 "trailing ]": `{"schema":1}]`,
71 "trailing word": `{"schema":1} x`,
72 } {
73 if _, err := ParseProofPolicy([]byte(body)); err == nil {
74 t.Fatalf("%s: parsed, want an error", name)
75 }
76 }
77}
78
79// Trailing whitespace is still the end of input.
80func TestProofPolicyTrailingWhitespaceParses(t *testing.T) {
81 if _, err := ParseProofPolicy([]byte("{\"schema\":1}\n\n \t")); err != nil {
82 t.Fatal(err)
83 }
84}
85
86// Absent means nothing is at the path. A dangling symlink or a directory
87// there is a broken config, and a broken config is closed, not off.
88func TestProofPolicyBrokenPathIsAnError(t *testing.T) {
89 dir := t.TempDir()
90 dangling := filepath.Join(dir, "dangling.json")
91 if err := os.Symlink(filepath.Join(dir, "gone.json"), dangling); err != nil {
92 t.Fatal(err)
93 }
94 if p, err := LoadProofPolicy(dangling); err == nil {
95 t.Fatalf("dangling symlink = %+v, nil; want an error (closed), not off", p)
96 }
97 asDir := filepath.Join(dir, "dir.json")
98 if err := os.Mkdir(asDir, 0o755); err != nil {
99 t.Fatal(err)
100 }
101 if p, err := LoadProofPolicy(asDir); err == nil {
102 t.Fatalf("directory = %+v, nil; want an error (closed), not off", p)
103 }
104}
105