pause_test.gno
4.29 Kb · 120 lines
1package config
2
3import (
4 "testing"
5
6 "gno.land/p/moul/pausable/v0"
7 "gno.land/p/nt/uassert/v0"
8)
9
10func TestPauseDefaultRunning(t *testing.T) {
11 resetSettings()
12 uassert.False(t, IsPausedFor(otherRealm))
13 uassert.True(t, PauseFor(otherRealm).AllowsRead())
14 uassert.True(t, PauseFor(otherRealm).AllowsWrite())
15 AssertWritableFor(otherRealm)
16 AssertReadableFor(otherRealm)
17}
18
19func TestPauseGlobal(cur realm, t *testing.T) {
20 resetSettings()
21 testing.SetRealm(testing.NewUserRealm(originAddr))
22 Set(cross(cur), KeyPause, "readonly")
23
24 // Reaches every realm.
25 for _, path := range []string{otherRealm, "gno.land/r/moul/home", "gno.land/r/someone/else"} {
26 uassert.True(t, IsPausedFor(path), path+" should be held back")
27 uassert.True(t, PauseFor(path).AllowsRead(), path+" should still read")
28 uassert.False(t, PauseFor(path).AllowsWrite(), path+" should refuse writes")
29 }
30
31 uassert.PanicsWithMessage(t, cur, "writes are paused", func() {
32 AssertWritableFor(otherRealm)
33 })
34 AssertReadableFor(otherRealm)
35}
36
37func TestPauseScoped(cur realm, t *testing.T) {
38 resetSettings()
39 testing.SetRealm(testing.NewUserRealm(originAddr))
40 Set(cross(cur), KeyFor(KeyPause, otherRealm), "paused: incident")
41
42 uassert.True(t, IsPausedFor(otherRealm))
43 uassert.False(t, PauseFor(otherRealm).AllowsRead())
44 uassert.False(t, IsPausedFor("gno.land/r/moul/home"), "another realm is unaffected")
45
46 uassert.PanicsWithMessage(t, cur, "paused: incident", func() {
47 AssertReadableFor(otherRealm)
48 })
49}
50
51// TestGlobalPauseCannotBeDefeated is the reason the two combine with
52// Strictest. A per-realm entry left behind from last month must not re-open a
53// realm during a global halt.
54func TestGlobalPauseCannotBeDefeated(cur realm, t *testing.T) {
55 resetSettings()
56 testing.SetRealm(testing.NewUserRealm(originAddr))
57 Set(cross(cur), KeyFor(KeyPause, otherRealm), "running")
58 testing.SetRealm(testing.NewUserRealm(originAddr))
59 Set(cross(cur), KeyPause, "paused: chain incident")
60
61 uassert.False(t, PauseFor(otherRealm).AllowsRead(),
62 "an explicit per-realm 'running' does not survive a global pause")
63 uassert.Equal(t, "chain incident", PauseFor(otherRealm).Reason)
64
65 // The other direction works: a per-realm pause under a running global.
66 resetSettings()
67 testing.SetRealm(testing.NewUserRealm(originAddr))
68 Set(cross(cur), KeyFor(KeyPause, otherRealm), "paused: just this one")
69 uassert.False(t, PauseFor(otherRealm).AllowsRead())
70 uassert.True(t, PauseFor("gno.land/r/moul/home").AllowsRead())
71}
72
73// TestPauseValueValidatedOnWrite is the other half of failing closed. Because
74// pausable.MustParse turns an unreadable value into Paused, an unvalidated
75// typo here would take every realm offline at the next render; refusing the
76// write turns that into a failed transaction the writer sees at once.
77func TestPauseValueValidatedOnWrite(cur realm, t *testing.T) {
78 resetSettings()
79
80 for _, bad := range []string{"yes", "true", "1", "stop", "read-only"} {
81 testing.SetRealm(testing.NewUserRealm(originAddr))
82 uassert.AbortsContains(t, cur, "invalid pause value", func() {
83 Set(cross(cur), KeyPause, bad)
84 })
85 }
86 uassert.Equal(t, 0, Size(), "no bad value was stored")
87
88 // The scoped key is validated too: the check is on the key's NAME, so it
89 // cannot be sidestepped by scoping it.
90 testing.SetRealm(testing.NewUserRealm(originAddr))
91 uassert.AbortsContains(t, cur, "invalid pause value", func() {
92 Set(cross(cur), KeyFor(KeyPause, otherRealm), "yes")
93 })
94
95 // And every value the reader understands is accepted.
96 for _, good := range []string{"", "running", "readonly", "paused", "paused: why"} {
97 testing.SetRealm(testing.NewUserRealm(originAddr))
98 Set(cross(cur), KeyPause, good)
99 uassert.Equal(t, good, Get(KeyPause))
100 }
101
102 // A setting that is not pause is not subject to the grammar.
103 testing.SetRealm(testing.NewUserRealm(originAddr))
104 Set(cross(cur), "some.key", "yes")
105 uassert.Equal(t, "yes", Get("some.key"))
106}
107
108func TestPauseZeroArgFormsNameTheCaller(cur realm, t *testing.T) {
109 resetSettings()
110 testing.SetRealm(testing.NewUserRealm(originAddr))
111 Set(cross(cur), KeyFor(KeyPause, otherRealm), "paused")
112
113 testing.SetRealm(testing.NewCodeRealm(otherRealm))
114 uassert.True(t, IsPaused())
115 uassert.Equal(t, int(pausable.Paused), int(Pause().Level))
116
117 testing.SetRealm(testing.NewCodeRealm("gno.land/r/moul/home"))
118 uassert.False(t, IsPaused(), "another realm is unaffected")
119 AssertWritable()
120}