| 1 | package approval |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "the-valley/note" |
| 12 | ) |
| 13 | |
| 14 | const keysBelow = "# Compiled from the identity registry — do not edit.\nexample-operator+3649fd12+AT/3+vgQhY5cdQ4AqspQ1A2yW60nn4j1FOyx04WfuQjc\n" |
| 15 | |
| 16 | func midnight(t *testing.T, day string) time.Time { |
| 17 | t.Helper() |
| 18 | d, err := time.Parse(dayLayout, day) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | return d |
| 23 | } |
| 24 | |
| 25 | // A stamp is written in one form, and reads back as the Authority it was |
| 26 | // written from: with a SHA-1 or a SHA-256 commit, and with no provenance |
| 27 | // at all. The keys below it read as they do without it. |
| 28 | func TestAStampReadsBackAsWritten(t *testing.T) { |
| 29 | day := midnight(t, "2026-11-16") |
| 30 | for _, c := range []struct { |
| 31 | a Authority |
| 32 | line string |
| 33 | }{ |
| 34 | {Authority{Ref: "refs/heads/main", Commit: strings.Repeat("5f", 20), Day: day}, |
| 35 | "# valley-approval-authority-v1 ref=refs/heads/main commit=" + strings.Repeat("5f", 20) + " day=2026-11-16\n"}, |
| 36 | {Authority{Ref: "refs/heads/registry", Commit: strings.Repeat("0a", 32), Day: day}, |
| 37 | "# valley-approval-authority-v1 ref=refs/heads/registry commit=" + strings.Repeat("0a", 32) + " day=2026-11-16\n"}, |
| 38 | {Authority{Day: day}, "# valley-approval-authority-v1 ref=none commit=none day=2026-11-16\n"}, |
| 39 | } { |
| 40 | line, err := c.a.Stamp() |
| 41 | if err != nil { |
| 42 | t.Fatalf("%+v: %v", c.a, err) |
| 43 | } |
| 44 | if line != c.line { |
| 45 | t.Errorf("%+v stamps %q, want %q", c.a, line, c.line) |
| 46 | } |
| 47 | file := []byte(line + keysBelow) |
| 48 | got, err := ReadAuthority(bytes.NewReader(file)) |
| 49 | if err != nil || got != c.a { |
| 50 | t.Errorf("%q reads as %+v, %v", line, got, err) |
| 51 | } |
| 52 | if string(StripAuthority(file)) != keysBelow { |
| 53 | t.Errorf("stripping %q leaves %q", line, StripAuthority(file)) |
| 54 | } |
| 55 | keys, err := note.ParseVerifierKeys(file) |
| 56 | if err != nil || len(keys) != 1 || keys[0].Name != "example-operator" { |
| 57 | t.Errorf("the keys below %q read as %v, %v", line, keys, err) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Stamp writes nothing ReadAuthority would refuse. |
| 63 | func TestAStampThatWouldNotReadIsNotWritten(t *testing.T) { |
| 64 | day := midnight(t, "2026-11-16") |
| 65 | commit := strings.Repeat("5f", 20) |
| 66 | for what, a := range map[string]Authority{ |
| 67 | "a short ref": {Ref: "main", Commit: commit, Day: day}, |
| 68 | "a ref holding a space": {Ref: "refs/heads/a b", Commit: commit, Day: day}, |
| 69 | "a ref too long for a stamp": {Ref: "refs/heads/" + strings.Repeat("a", MaxStamp), Commit: commit, Day: day}, |
| 70 | "a ref with no commit": {Ref: "refs/heads/main", Day: day}, |
| 71 | "a commit with no ref": {Commit: commit, Day: day}, |
| 72 | "an abbreviated commit": {Ref: "refs/heads/main", Commit: commit[:12], Day: day}, |
| 73 | "an uppercase commit": {Ref: "refs/heads/main", Commit: strings.ToUpper(commit), Day: day}, |
| 74 | "a day that is not midnight": {Ref: "refs/heads/main", Commit: commit, Day: day.Add(time.Hour)}, |
| 75 | } { |
| 76 | if line, err := a.Stamp(); err == nil { |
| 77 | t.Errorf("%s was stamped %q", what, line) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // A file is refused unless its first line is one stamp of this version |
| 83 | // that reads, and no other line opens as a stamp. |
| 84 | func TestAStampThatDoesNotReadIsRefused(t *testing.T) { |
| 85 | commit := strings.Repeat("5f", 20) |
| 86 | good := "# valley-approval-authority-v1 ref=refs/heads/main commit=" + commit + " day=2026-11-16\n" |
| 87 | if _, err := ReadAuthority(strings.NewReader(good + keysBelow)); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | for what, file := range map[string]string{ |
| 91 | "no stamp": keysBelow, |
| 92 | "an empty file": "", |
| 93 | "the stamp after the header": "# Compiled\n" + good, |
| 94 | "a stamp of a later version": "# valley-approval-authority-v2 ref=refs/heads/main commit=" + commit + " day=2026-11-16\n", |
| 95 | "a stamp with no newline": strings.TrimSuffix(good, "\n"), |
| 96 | "a stamp closed by CRLF": strings.TrimSuffix(good, "\n") + "\r\n", |
| 97 | "the fields out of order": "# valley-approval-authority-v1 commit=" + commit + " ref=refs/heads/main day=2026-11-16\n", |
| 98 | "two spaces between fields": "# valley-approval-authority-v1 ref=refs/heads/main commit=" + commit + " day=2026-11-16\n", |
| 99 | "a trailing space": strings.TrimSuffix(good, "\n") + " \n", |
| 100 | "a field more": strings.TrimSuffix(good, "\n") + " by=operator\n", |
| 101 | "a field missing": "# valley-approval-authority-v1 ref=refs/heads/main commit=" + commit + "\n", |
| 102 | "a short ref": "# valley-approval-authority-v1 ref=main commit=" + commit + " day=2026-11-16\n", |
| 103 | "a ref ending in .lock": "# valley-approval-authority-v1 ref=refs/heads/main.lock commit=" + commit + " day=2026-11-16\n", |
| 104 | "an abbreviated commit": "# valley-approval-authority-v1 ref=refs/heads/main commit=5f5f5f5f day=2026-11-16\n", |
| 105 | "an uppercase commit": "# valley-approval-authority-v1 ref=refs/heads/main commit=" + strings.ToUpper(commit) + " day=2026-11-16\n", |
| 106 | "a ref named and no commit": "# valley-approval-authority-v1 ref=refs/heads/main commit=none day=2026-11-16\n", |
| 107 | "a commit named and no ref": "# valley-approval-authority-v1 ref=none commit=" + commit + " day=2026-11-16\n", |
| 108 | "a day that is not a day": "# valley-approval-authority-v1 ref=refs/heads/main commit=" + commit + " day=2026-02-30\n", |
| 109 | "a day written another way": "# valley-approval-authority-v1 ref=refs/heads/main commit=" + commit + " day=2026-11-16T00:00:00Z\n", |
| 110 | "a day with no leading zeros": "# valley-approval-authority-v1 ref=refs/heads/main commit=" + commit + " day=2026-1-6\n", |
| 111 | } { |
| 112 | if a, err := ReadAuthority(strings.NewReader(file)); err == nil { |
| 113 | t.Errorf("%s read as %+v", what, a) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // endless is a stream that never ends, of one byte over and over, after |
| 119 | // what it opens with. reads counts the bytes taken from it. |
| 120 | type endless struct { |
| 121 | opening []byte |
| 122 | fill byte |
| 123 | reads int |
| 124 | } |
| 125 | |
| 126 | func (e *endless) Read(p []byte) (int, error) { |
| 127 | n := copy(p, e.opening) |
| 128 | e.opening = e.opening[n:] |
| 129 | for i := n; i < len(p); i++ { |
| 130 | p[i] = e.fill |
| 131 | } |
| 132 | e.reads += len(p) |
| 133 | return len(p), nil |
| 134 | } |
| 135 | |
| 136 | // The stamp is read from the first MaxStamp bytes alone, however large the |
| 137 | // file. A stamp followed by a file that never ends reads, and a first line |
| 138 | // that never ends is refused, each after at most MaxStamp bytes. A second |
| 139 | // line opening like a stamp is not looked for: the first line is the |
| 140 | // stamp. |
| 141 | func TestTheStampIsReadFromTheFirstLineAlone(t *testing.T) { |
| 142 | good := "# valley-approval-authority-v1 ref=refs/heads/main commit=" + strings.Repeat("5f", 20) + " day=2026-11-16\n" |
| 143 | file := &endless{opening: []byte(good + keysBelow), fill: '\n'} |
| 144 | if a, err := ReadAuthority(file); err != nil || a.Ref != "refs/heads/main" { |
| 145 | t.Errorf("a stamped file that never ends reads as %+v, %v", a, err) |
| 146 | } |
| 147 | if file.reads > MaxStamp { |
| 148 | t.Errorf("reading the stamp took %d bytes", file.reads) |
| 149 | } |
| 150 | line := &endless{opening: []byte("# valley-approval-authority-v1 ref=refs/heads/"), fill: 'a'} |
| 151 | if a, err := ReadAuthority(line); err == nil || !strings.Contains(err.Error(), "longer than") { |
| 152 | t.Errorf("a first line that never ends reads as %+v, %v", a, err) |
| 153 | } |
| 154 | if line.reads > MaxStamp { |
| 155 | t.Errorf("refusing a long first line took %d bytes", line.reads) |
| 156 | } |
| 157 | if a, err := ReadAuthority(strings.NewReader(good + good)); err != nil || a.Commit != strings.Repeat("5f", 20) { |
| 158 | t.Errorf("a stamp with another below it reads as %+v, %v", a, err) |
| 159 | } |
| 160 | // Reading the stamp leaves the rest of the file to be read. |
| 161 | r := strings.NewReader(good + keysBelow) |
| 162 | if _, err := ReadAuthority(r); err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | if rest, err := io.ReadAll(r); err != nil || string(rest) != keysBelow { |
| 166 | t.Errorf("after the stamp, the file holds %q, %v", rest, err) |
| 167 | } |
| 168 | // A stream whose first line has arrived is not read further: a reader |
| 169 | // that fails on the next read, as a pipe whose writer stays open would |
| 170 | // block on it, still reads. |
| 171 | if a, err := ReadAuthority(&oneLine{line: []byte(good)}); err != nil || a.Ref != "refs/heads/main" { |
| 172 | t.Errorf("a stamp followed by a failing read reads as %+v, %v", a, err) |
| 173 | } |
| 174 | if _, err := ReadAuthority(&oneLine{line: []byte("# valley-approval-authority-v1 ref=refs/heads/main")}); err == nil || !strings.Contains(err.Error(), "the stream stalls") { |
| 175 | t.Errorf("a stream that fails before its first newline: %v", err) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // A stamp whose newline is byte MaxStamp reads. One a byte longer, whose |
| 180 | // newline would be byte MaxStamp+1, is refused after MaxStamp bytes, with |
| 181 | // nothing past them read. |
| 182 | func TestTheStampEndsWithinMaxStampBytes(t *testing.T) { |
| 183 | commit := strings.Repeat("5f", 20) |
| 184 | line := func(n int) string { |
| 185 | t.Helper() |
| 186 | short := "# valley-approval-authority-v1 ref=refs/heads/ commit=" + commit + " day=2026-11-16\n" |
| 187 | return strings.Replace(short, "refs/heads/", "refs/heads/"+strings.Repeat("a", n-len(short)), 1) |
| 188 | } |
| 189 | at := line(MaxStamp) |
| 190 | if len(at) != MaxStamp || at[MaxStamp-1] != '\n' { |
| 191 | t.Fatalf("the line is %d bytes, not %d ending in its newline", len(at), MaxStamp) |
| 192 | } |
| 193 | a, err := ReadAuthority(strings.NewReader(at + keysBelow)) |
| 194 | if err != nil { |
| 195 | t.Fatalf("a stamp of %d bytes: %v", MaxStamp, err) |
| 196 | } |
| 197 | if again, err := a.Stamp(); err != nil || again != at { |
| 198 | t.Errorf("a stamp of %d bytes is written back as %q, %v", MaxStamp, again, err) |
| 199 | } |
| 200 | past := &endless{opening: []byte(line(MaxStamp + 1)), fill: '\n'} |
| 201 | if a, err := ReadAuthority(past); err == nil || !strings.Contains(err.Error(), "longer than") { |
| 202 | t.Errorf("a stamp of %d bytes reads as %+v, %v", MaxStamp+1, a, err) |
| 203 | } |
| 204 | if past.reads != MaxStamp { |
| 205 | t.Errorf("refusing a stamp of %d bytes took %d bytes, not %d", MaxStamp+1, past.reads, MaxStamp) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // A reader that keeps giving nothing, and no error, is given up on. |
| 210 | func TestAReaderThatGivesNothingIsGivenUpOn(t *testing.T) { |
| 211 | if a, err := ReadAuthority(nothing{}); !errors.Is(err, io.ErrNoProgress) { |
| 212 | t.Errorf("a reader that gives nothing reads as %+v, %v", a, err) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // nothing gives no bytes and no error, every time. |
| 217 | type nothing struct{} |
| 218 | |
| 219 | func (nothing) Read([]byte) (int, error) { return 0, nil } |
| 220 | |
| 221 | // oneLine gives its line, and fails every read after it. |
| 222 | type oneLine struct { |
| 223 | line []byte |
| 224 | } |
| 225 | |
| 226 | func (o *oneLine) Read(p []byte) (int, error) { |
| 227 | if len(o.line) == 0 { |
| 228 | return 0, errors.New("the stream stalls") |
| 229 | } |
| 230 | n := copy(p, o.line) |
| 231 | o.line = o.line[n:] |
| 232 | return n, nil |
| 233 | } |
| 234 | |
| 235 | // Stripping drops the stamp line of this version and nothing else: a file |
| 236 | // that does not open with one is kept byte for byte. |
| 237 | func TestStrippingDropsTheStampLineAlone(t *testing.T) { |
| 238 | for _, file := range []string{ |
| 239 | keysBelow, |
| 240 | "", |
| 241 | "# Compiled\n# valley-approval-authority-v1 ref=none commit=none day=2026-11-16\n", |
| 242 | "# valley-approval-authority-v2 ref=none commit=none day=2026-11-16\n" + keysBelow, |
| 243 | } { |
| 244 | if got := string(StripAuthority([]byte(file))); got != file { |
| 245 | t.Errorf("stripping %q left %q", file, got) |
| 246 | } |
| 247 | } |
| 248 | if got := string(StripAuthority([]byte("# valley-approval-authority-v1 ref=none commit=none day=2026-11-16"))); got != "" { |
| 249 | t.Errorf("stripping a lone stamp left %q", got) |
| 250 | } |
| 251 | // A stamp line closed by CRLF is dropped whole, carriage return |
| 252 | // included, though it does not read as a stamp. |
| 253 | crlf := "# valley-approval-authority-v1 ref=none commit=none day=2026-11-16\r\n" |
| 254 | if got := string(StripAuthority([]byte(crlf + keysBelow))); got != keysBelow { |
| 255 | t.Errorf("stripping a CRLF stamp left %q", got) |
| 256 | } |
| 257 | // A first line longer than a stamp is no stamp line, and is kept. |
| 258 | long := "# valley-approval-authority-v1 ref=refs/heads/" + strings.Repeat("a", 2*MaxStamp) + "\n" + keysBelow |
| 259 | if got := string(StripAuthority([]byte(long))); got != long { |
| 260 | t.Errorf("stripping a first line of %d bytes left %d bytes", len(long), len(got)) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // A rendering is fresh while the stamped ref still holds the stamped |
| 265 | // commit and the day is the stamped day, at any hour of it. Another |
| 266 | // commit, the day before, the day after, and no provenance are stale. |
| 267 | func TestFreshnessIsTheCommitAndTheDay(t *testing.T) { |
| 268 | commit, other := strings.Repeat("5f", 20), strings.Repeat("6e", 20) |
| 269 | a := Authority{Ref: "refs/heads/main", Commit: commit, Day: midnight(t, "2026-11-16")} |
| 270 | at := func(s string) time.Time { |
| 271 | t.Helper() |
| 272 | tm, err := time.Parse(time.RFC3339, s) |
| 273 | if err != nil { |
| 274 | t.Fatal(err) |
| 275 | } |
| 276 | return tm |
| 277 | } |
| 278 | for _, now := range []string{"2026-11-16T00:00:00Z", "2026-11-16T23:59:59Z", "2026-11-16T20:00:00-03:00"} { |
| 279 | if err := a.Fresh(commit, at(now)); err != nil { |
| 280 | t.Errorf("at %s: %v", now, err) |
| 281 | } |
| 282 | } |
| 283 | for what, c := range map[string]struct { |
| 284 | a Authority |
| 285 | current string |
| 286 | now string |
| 287 | }{ |
| 288 | "another commit": {a, other, "2026-11-16T12:00:00Z"}, |
| 289 | "the day before": {a, commit, "2026-11-15T23:59:59Z"}, |
| 290 | "the day after": {a, commit, "2026-11-17T00:00:00Z"}, |
| 291 | "the day after, in UTC": {a, commit, "2026-11-16T22:00:00-03:00"}, |
| 292 | "no provenance": {Authority{Day: a.Day}, "", "2026-11-16T12:00:00Z"}, |
| 293 | } { |
| 294 | if err := c.a.Fresh(c.current, at(c.now)); err == nil { |
| 295 | t.Errorf("%s is fresh", what) |
| 296 | } |
| 297 | } |
| 298 | } |