gunk-dev/the-valley

policy_test.go

source30fe2c053de0main's published tip history
1.3 KiB
1package main
2
3import (
4 "io"
5 "os"
6 "path/filepath"
7 "testing"
8)
9
10// The authorization counter reads no clock and survives the process: each
11// publisher over one state directory allocates a sequence greater than
12// every one allocated before, a floor from the receiver raises it, and a
13// counter that cannot be read is an error, never a fresh start at one.
14func TestTheAuthorizationSequenceIsDurable(t *testing.T) {
15 state := t.TempDir()
16 next := func(floor int64) (int64, error) {
17 p := &publisher{cfg: config{state: state}, stdout: io.Discard, stderr: io.Discard}
18 return p.nextSequence(floor)
19 }
20 for i, c := range []struct{ floor, want int64 }{{0, 1}, {0, 2}, {10, 11}, {3, 12}, {0, 13}} {
21 got, err := next(c.floor)
22 if err != nil || got != c.want {
23 t.Fatalf("allocation %d with floor %d gave %d, %v; want %d", i+1, c.floor, got, err, c.want)
24 }
25 }
26 data, err := os.ReadFile(filepath.Join(state, authorizationSequenceFile))
27 if err != nil || string(data) != "13\n" {
28 t.Errorf("the counter holds %q, %v; want 13", data, err)
29 }
30 for _, bad := range []string{"", "x\n", "-4\n"} {
31 if err := os.WriteFile(filepath.Join(state, authorizationSequenceFile), []byte(bad), 0o600); err != nil {
32 t.Fatal(err)
33 }
34 if got, err := next(0); err == nil {
35 t.Errorf("a counter holding %q gave sequence %d", bad, got)
36 }
37 }
38}