| 1 | package readerstate |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/base64" |
| 7 | "encoding/hex" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "sync" |
| 14 | "syscall" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "the-valley/web/export" |
| 19 | ) |
| 20 | |
| 21 | // link is a read link the tests issue: its credential, and the binding an |
| 22 | // authorization gives it. |
| 23 | type link struct { |
| 24 | credential string |
| 25 | binding export.Binding |
| 26 | } |
| 27 | |
| 28 | func newLink(n int, principal string) link { |
| 29 | secret := make([]byte, 32) |
| 30 | secret[0] = byte(n) |
| 31 | sum := sha256.Sum256(secret) |
| 32 | id := fmt.Sprintf("lk-%016x", n) |
| 33 | return link{credential: id + "." + base64.RawURLEncoding.EncodeToString(secret), |
| 34 | binding: export.Binding{ID: id, Principal: principal, Verifier: hex.EncodeToString(sum[:])}} |
| 35 | } |
| 36 | |
| 37 | // authority is a stored authorization a test changes, and the Hold that |
| 38 | // reads it. |
| 39 | type authority struct { |
| 40 | mu sync.Mutex |
| 41 | a *export.Authorization |
| 42 | } |
| 43 | |
| 44 | func (au *authority) set(links ...link) { |
| 45 | au.mu.Lock() |
| 46 | defer au.mu.Unlock() |
| 47 | au.a = &export.Authorization{Instance: "valley-host", Destination: "example.org", Sequence: 1} |
| 48 | for _, l := range links { |
| 49 | au.a.Bindings = append(au.a.Bindings, l.binding) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (au *authority) hold(context.Context) (*export.Authorization, func(), error) { |
| 54 | au.mu.Lock() |
| 55 | defer au.mu.Unlock() |
| 56 | return au.a, func() {}, nil |
| 57 | } |
| 58 | |
| 59 | func (au *authority) current() *export.Authorization { |
| 60 | a, _, _ := au.hold(context.Background()) |
| 61 | return a |
| 62 | } |
| 63 | |
| 64 | // browserDir is a new browser-state directory, mode 0700. |
| 65 | func browserDir(t *testing.T) string { |
| 66 | t.Helper() |
| 67 | dir := filepath.Join(t.TempDir(), "browser") |
| 68 | if err := os.Mkdir(dir, 0o700); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | return dir |
| 72 | } |
| 73 | |
| 74 | func open(t *testing.T, dir string, o Options) *State { |
| 75 | t.Helper() |
| 76 | s, err := Open(dir, o) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | t.Cleanup(func() { s.Close() }) |
| 81 | return s |
| 82 | } |
| 83 | |
| 84 | // created is a state Initialize created under an authorization with no |
| 85 | // binding, and that authorization. |
| 86 | func created(t *testing.T, o Options) (*State, *authority, string) { |
| 87 | t.Helper() |
| 88 | dir := browserDir(t) |
| 89 | s := open(t, dir, o) |
| 90 | au := &authority{} |
| 91 | au.set() |
| 92 | if ok, err := s.Initialize(context.Background(), au.hold); !ok || err != nil { |
| 93 | t.Fatalf("Initialize under no binding: %v, %v", ok, err) |
| 94 | } |
| 95 | return s, au, dir |
| 96 | } |
| 97 | |
| 98 | // pending is Pending, with no deadline. |
| 99 | func (s *State) pending(t *testing.T) bool { |
| 100 | t.Helper() |
| 101 | p, err := s.Pending(context.Background()) |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | return p |
| 106 | } |
| 107 | |
| 108 | func (s *State) sessionOf(t *testing.T, token string, a *export.Authorization) (string, error) { |
| 109 | t.Helper() |
| 110 | r, err := s.Read(context.Background()) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | defer r.Done() |
| 115 | return r.Session(token, a) |
| 116 | } |
| 117 | |
| 118 | func TestOpenRefusesWhatItDoesNotOwn(t *testing.T) { |
| 119 | dir := browserDir(t) |
| 120 | open(t, dir, Options{}) |
| 121 | if _, err := Open(dir, Options{}); err == nil || !strings.Contains(err.Error(), "another process owns") { |
| 122 | t.Errorf("a second Open of one directory: %v", err) |
| 123 | } |
| 124 | wide := filepath.Join(t.TempDir(), "wide") |
| 125 | if err := os.Mkdir(wide, 0o750); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | os.Chmod(wide, 0o750) |
| 129 | if _, err := Open(wide, Options{}); err == nil || !strings.Contains(err.Error(), "mode 0750") { |
| 130 | t.Errorf("Open of a directory its group may read: %v", err) |
| 131 | } |
| 132 | link := filepath.Join(t.TempDir(), "link") |
| 133 | if err := os.Symlink(browserDir(t), link); err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | if _, err := Open(link, Options{}); err == nil || !strings.Contains(err.Error(), "not a directory") { |
| 137 | t.Errorf("Open of a symbolic link to a directory: %v", err) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestInitializeOnlyUnderNoBinding(t *testing.T) { |
| 142 | ctx := context.Background() |
| 143 | s := open(t, browserDir(t), Options{}) |
| 144 | au := &authority{} |
| 145 | if ok, err := s.Initialize(ctx, au.hold); ok || err != nil || !s.pending(t) { |
| 146 | t.Errorf("Initialize with no authorization: %v, %v, pending %v", ok, err, s.pending(t)) |
| 147 | } |
| 148 | au.set(newLink(1, "alice")) |
| 149 | if ok, err := s.Initialize(ctx, au.hold); ok || err != nil || !s.pending(t) { |
| 150 | t.Errorf("Initialize under a binding: %v, %v, pending %v", ok, err, s.pending(t)) |
| 151 | } |
| 152 | if _, err := s.Redeem(ctx, au.hold, newLink(1, "alice").credential, ""); !errors.Is(err, ErrUnavailable) { |
| 153 | t.Errorf("Redeem before the state exists: %v", err) |
| 154 | } |
| 155 | if _, err := s.sessionOf(t, "", au.current()); !errors.Is(err, ErrUnavailable) { |
| 156 | t.Errorf("a session before the state exists: %v", err) |
| 157 | } |
| 158 | au.set() |
| 159 | if ok, err := s.Initialize(ctx, au.hold); !ok || err != nil || s.pending(t) { |
| 160 | t.Errorf("Initialize under no binding: %v, %v, pending %v", ok, err, s.pending(t)) |
| 161 | } |
| 162 | if ok, err := s.Initialize(ctx, au.hold); ok || err != nil { |
| 163 | t.Errorf("a second Initialize: %v, %v", ok, err) |
| 164 | } |
| 165 | data, err := os.ReadFile(filepath.Join(s.dir, stateFile)) |
| 166 | if err != nil || string(data) != Type+"\n" { |
| 167 | t.Errorf("the created state holds %q, %v", data, err) |
| 168 | } |
| 169 | for _, name := range []string{stateFile, lockFile, ownerFile} { |
| 170 | if info, err := os.Lstat(filepath.Join(s.dir, name)); err != nil || info.Mode() != 0o600 { |
| 171 | t.Errorf("%s: %v, %v", name, info.Mode(), err) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestARedeemedLinkHoldsOneSession(t *testing.T) { |
| 177 | ctx := context.Background() |
| 178 | s, au, dir := created(t, Options{}) |
| 179 | alice, bob := newLink(1, "alice"), newLink(2, "bob") |
| 180 | au.set(alice, bob) |
| 181 | token, err := s.Redeem(ctx, au.hold, alice.credential, "") |
| 182 | if prefixed := strings.HasPrefix(token, alice.binding.ID+"."); err != nil || !prefixed { |
| 183 | t.Fatalf("Redeem: %v, and the token starts with the link's ID: %v", err, prefixed) |
| 184 | } |
| 185 | if p, err := s.sessionOf(t, token, au.current()); p != "alice" || err != nil { |
| 186 | t.Errorf("the session is %q, %v", p, err) |
| 187 | } |
| 188 | for name, cred := range map[string]string{ |
| 189 | "redeemed": alice.credential, |
| 190 | "malformed": "lk-0000000000000002", |
| 191 | "unknown": newLink(3, "bob").credential, |
| 192 | "wrong": bob.binding.ID + "." + strings.Split(alice.credential, ".")[1], |
| 193 | "padded": bob.credential + "=", |
| 194 | "uppercase": strings.ToUpper(bob.binding.ID[:3]) + bob.credential[3:], |
| 195 | "noncanonical": bob.credential[:len(bob.credential)-1] + "B", |
| 196 | } { |
| 197 | if _, err := s.Redeem(ctx, au.hold, cred, ""); !errors.Is(err, ErrRefused) { |
| 198 | t.Errorf("Redeem of a %s credential: %v", name, err) |
| 199 | } |
| 200 | } |
| 201 | if _, err := s.Redeem(ctx, au.hold, bob.credential, token); !errors.Is(err, ErrSignedIn) { |
| 202 | t.Errorf("Redeem with a valid session: %v", err) |
| 203 | } |
| 204 | for name, tok := range map[string]string{"unknown": newLink(9, "alice").credential, "malformed": "x", "empty": ""} { |
| 205 | if p, err := s.sessionOf(t, tok, au.current()); p != "" || err != nil { |
| 206 | t.Errorf("a session of an %s token: %q, %v", name, p, err) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // The state survives a restart, and logout keeps the link consumed. |
| 211 | s.Close() |
| 212 | s = open(t, dir, Options{}) |
| 213 | if p, _ := s.sessionOf(t, token, au.current()); p != "alice" { |
| 214 | t.Errorf("after a restart the session is %q", p) |
| 215 | } |
| 216 | if err := s.Logout(ctx, au.hold, token); err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | if err := s.Logout(ctx, au.hold, token); err != nil { |
| 220 | t.Errorf("a second logout: %v", err) |
| 221 | } |
| 222 | if p, _ := s.sessionOf(t, token, au.current()); p != "" { |
| 223 | t.Errorf("after logout the session is %q", p) |
| 224 | } |
| 225 | if _, err := s.Redeem(ctx, au.hold, alice.credential, ""); !errors.Is(err, ErrRefused) { |
| 226 | t.Errorf("Redeem after logout: %v", err) |
| 227 | } |
| 228 | if _, err := s.Redeem(ctx, au.hold, bob.credential, token); err != nil { |
| 229 | t.Errorf("Redeem with a signed-out token: %v", err) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // A session holds only while the authorization binds its link exactly as |
| 234 | // it was redeemed, and no authorization makes a consumed link redeemable. |
| 235 | func TestASessionNeedsItsExactBinding(t *testing.T) { |
| 236 | ctx := context.Background() |
| 237 | s, au, _ := created(t, Options{}) |
| 238 | alice := newLink(1, "alice") |
| 239 | au.set(alice) |
| 240 | token, err := s.Redeem(ctx, au.hold, alice.credential, "") |
| 241 | if err != nil { |
| 242 | t.Fatal(err) |
| 243 | } |
| 244 | mallory := alice |
| 245 | mallory.binding.Principal = "mallory" |
| 246 | other := alice |
| 247 | other.binding.Verifier = newLink(5, "alice").binding.Verifier |
| 248 | for name, l := range map[string]link{"another principal": mallory, "another verifier": other} { |
| 249 | au.set(l) |
| 250 | if p, err := s.sessionOf(t, token, au.current()); p != "" || err != nil { |
| 251 | t.Errorf("under %s the session is %q, %v", name, p, err) |
| 252 | } |
| 253 | if _, err := s.Redeem(ctx, au.hold, alice.credential, ""); !errors.Is(err, ErrRefused) { |
| 254 | t.Errorf("under %s the consumed link redeems: %v", name, err) |
| 255 | } |
| 256 | } |
| 257 | au.set() |
| 258 | if p, _ := s.sessionOf(t, token, au.current()); p != "" { |
| 259 | t.Errorf("revoked, the session is %q", p) |
| 260 | } |
| 261 | au.set(alice) |
| 262 | if p, _ := s.sessionOf(t, token, au.current()); p != "alice" { |
| 263 | t.Errorf("bound again as it was, the session is %q", p) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func TestConcurrentRedemptionsHaveOneWinner(t *testing.T) { |
| 268 | s, au, _ := created(t, Options{}) |
| 269 | alice := newLink(1, "alice") |
| 270 | au.set(alice) |
| 271 | var wg sync.WaitGroup |
| 272 | var won sync.Map |
| 273 | for i := range 16 { |
| 274 | wg.Add(1) |
| 275 | go func() { |
| 276 | defer wg.Done() |
| 277 | if token, err := s.Redeem(context.Background(), au.hold, alice.credential, ""); err == nil { |
| 278 | won.Store(i, token) |
| 279 | } |
| 280 | }() |
| 281 | } |
| 282 | wg.Wait() |
| 283 | n := 0 |
| 284 | won.Range(func(any, any) bool { n++; return true }) |
| 285 | if n != 1 { |
| 286 | t.Errorf("%d redemptions of one link succeeded", n) |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // A transaction whose context is done once it holds a lock changes |
| 291 | // nothing: not with every lock free, and not when the context ends while |
| 292 | // it waits for the serving lock. A wait for the snapshot's lock, to read |
| 293 | // or to write, ends at its deadline. |
| 294 | func TestADoneContextChangesNothing(t *testing.T) { |
| 295 | reached, slow := make(chan struct{}), make(chan struct{}) |
| 296 | var once sync.Once |
| 297 | s, au, _ := created(t, Options{Fault: func(op Op, step Step) error { |
| 298 | if op == Redeeming && step == Created { |
| 299 | once.Do(func() { close(reached); <-slow }) |
| 300 | } |
| 301 | return nil |
| 302 | }}) |
| 303 | alice, bob := newLink(1, "alice"), newLink(2, "bob") |
| 304 | au.set(alice, bob) |
| 305 | done, cancel := context.WithCancel(context.Background()) |
| 306 | cancel() |
| 307 | if _, err := s.Redeem(done, au.hold, alice.credential, ""); !errors.Is(err, context.Canceled) { |
| 308 | t.Errorf("Redeem under a done context: %v", err) |
| 309 | } |
| 310 | ending, end := context.WithCancel(context.Background()) |
| 311 | endsInHold := func(ctx context.Context) (*export.Authorization, func(), error) { |
| 312 | end() |
| 313 | return au.hold(ctx) |
| 314 | } |
| 315 | if _, err := s.Redeem(ending, endsInHold, alice.credential, ""); !errors.Is(err, context.Canceled) { |
| 316 | t.Errorf("Redeem whose context ended in its hold: %v", err) |
| 317 | } |
| 318 | if strings.Contains(string(s.snap.encode()), alice.binding.ID) { |
| 319 | t.Fatal("a redemption under a done context consumed its link") |
| 320 | } |
| 321 | |
| 322 | redeemed := make(chan error, 1) |
| 323 | go func() { |
| 324 | _, err := s.Redeem(context.Background(), au.hold, alice.credential, "") |
| 325 | redeemed <- err |
| 326 | }() |
| 327 | <-reached |
| 328 | short, stop := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 329 | defer stop() |
| 330 | if _, err := s.Redeem(short, au.hold, bob.credential, ""); !errors.Is(err, context.DeadlineExceeded) { |
| 331 | t.Errorf("Redeem behind a slow transaction: %v", err) |
| 332 | } |
| 333 | if _, err := s.Read(short); !errors.Is(err, context.DeadlineExceeded) { |
| 334 | t.Errorf("Read behind a slow transaction: %v", err) |
| 335 | } |
| 336 | close(slow) |
| 337 | if err := <-redeemed; err != nil { |
| 338 | t.Fatalf("the slow redemption: %v", err) |
| 339 | } |
| 340 | token, err := s.Redeem(context.Background(), au.hold, bob.credential, "") |
| 341 | if err != nil { |
| 342 | t.Fatalf("bob's link, after its redemption ran out of time: %v", err) |
| 343 | } |
| 344 | if err := s.Logout(done, au.hold, token); !errors.Is(err, context.Canceled) { |
| 345 | t.Errorf("Logout under a done context: %v", err) |
| 346 | } |
| 347 | if p, err := s.sessionOf(t, token, au.current()); p != "bob" || err != nil { |
| 348 | t.Errorf("a logout under a done context ended the session: %q, %v", p, err) |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // A transaction whose deadline passes after it holds every lock, while it |
| 353 | // encodes the next document, stops before it makes state.new: a |
| 354 | // redemption consumes nothing and a logout ends no session, and the state |
| 355 | // stays usable. |
| 356 | func TestADeadlineWhileEncodingChangesNothing(t *testing.T) { |
| 357 | // late is the context of the transaction the fault hook holds at its |
| 358 | // encoding until the context's deadline passes. |
| 359 | var late context.Context |
| 360 | s, au, dir := created(t, Options{Fault: func(op Op, step Step) error { |
| 361 | if step == Encoded && late != nil { |
| 362 | <-late.Done() |
| 363 | } |
| 364 | return nil |
| 365 | }}) |
| 366 | alice := newLink(1, "alice") |
| 367 | au.set(alice) |
| 368 | before, err := os.ReadFile(filepath.Join(dir, stateFile)) |
| 369 | if err != nil { |
| 370 | t.Fatal(err) |
| 371 | } |
| 372 | short := func() context.Context { |
| 373 | ctx, stop := context.WithTimeout(context.Background(), 50*time.Millisecond) |
| 374 | t.Cleanup(stop) |
| 375 | return ctx |
| 376 | } |
| 377 | late = short() |
| 378 | if _, err := s.Redeem(late, au.hold, alice.credential, ""); !errors.Is(err, context.DeadlineExceeded) { |
| 379 | t.Errorf("a redemption whose deadline passed while it encoded: %v", err) |
| 380 | } |
| 381 | late = nil |
| 382 | if after, err := os.ReadFile(filepath.Join(dir, stateFile)); err != nil || string(after) != string(before) { |
| 383 | t.Errorf("a redemption whose deadline passed while it encoded changed the state to %q, %v", after, err) |
| 384 | } |
| 385 | if _, err := os.Lstat(filepath.Join(dir, nextFile)); err == nil { |
| 386 | t.Error("a redemption whose deadline passed while it encoded made state.new") |
| 387 | } |
| 388 | token, err := s.Redeem(context.Background(), au.hold, alice.credential, "") |
| 389 | if err != nil { |
| 390 | t.Fatalf("the link, after its redemption ran out of time: %v", err) |
| 391 | } |
| 392 | late = short() |
| 393 | if err := s.Logout(late, au.hold, token); !errors.Is(err, context.DeadlineExceeded) { |
| 394 | t.Errorf("a logout whose deadline passed while it encoded: %v", err) |
| 395 | } |
| 396 | late = nil |
| 397 | if p, err := s.sessionOf(t, token, au.current()); p != "alice" || err != nil { |
| 398 | t.Errorf("a logout whose deadline passed while it encoded ended the session: %q, %v", p, err) |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | func TestAFullStateRefusesWithoutConsuming(t *testing.T) { |
| 403 | ctx := context.Background() |
| 404 | one := len(Type+"\n") + len("link lk-0000000000000001 alice ") + 64 + 1 + 64 + 1 |
| 405 | s, au, _ := created(t, Options{Max: one + 10}) |
| 406 | alice, bob := newLink(1, "alice"), newLink(2, "bob") |
| 407 | au.set(alice, bob) |
| 408 | token, err := s.Redeem(ctx, au.hold, alice.credential, "") |
| 409 | if err != nil { |
| 410 | t.Fatal(err) |
| 411 | } |
| 412 | if _, err := s.Redeem(ctx, au.hold, bob.credential, ""); !errors.Is(err, ErrFull) { |
| 413 | t.Errorf("Redeem past the bound: %v", err) |
| 414 | } |
| 415 | if err := s.Logout(ctx, au.hold, token); err != nil { |
| 416 | t.Errorf("logout of a full state: %v", err) |
| 417 | } |
| 418 | if strings.Contains(string(s.snap.encode()), bob.binding.ID) { |
| 419 | t.Error("the refused redemption consumed its link") |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // A transaction that fails at any step leaves the state unavailable until |
| 424 | // a restart. After it, the link is consumed when the rename happened, and |
| 425 | // redeems once when it did not. |
| 426 | func TestAFailedTransactionLeavesTheStateUnavailable(t *testing.T) { |
| 427 | ctx := context.Background() |
| 428 | for _, step := range Steps { |
| 429 | t.Run(step.String(), func(t *testing.T) { |
| 430 | fail := true |
| 431 | s, au, dir := created(t, Options{Fault: func(op Op, st Step) error { |
| 432 | if fail && op == Redeeming && st == step { |
| 433 | return errors.New("injected") |
| 434 | } |
| 435 | return nil |
| 436 | }}) |
| 437 | alice, bob := newLink(1, "alice"), newLink(2, "bob") |
| 438 | au.set(alice, bob) |
| 439 | if _, err := s.Redeem(ctx, au.hold, alice.credential, ""); !errors.Is(err, ErrUnavailable) { |
| 440 | t.Fatalf("a redemption that failed at %s: %v", step, err) |
| 441 | } |
| 442 | fail = false |
| 443 | if _, err := s.Redeem(ctx, au.hold, bob.credential, ""); !errors.Is(err, ErrUnavailable) { |
| 444 | t.Errorf("a redemption after the failure: %v", err) |
| 445 | } |
| 446 | if _, err := s.sessionOf(t, "", au.current()); !errors.Is(err, ErrUnavailable) { |
| 447 | t.Errorf("a session after the failure: %v", err) |
| 448 | } |
| 449 | if _, err := os.Lstat(filepath.Join(dir, nextFile)); step < Renamed && err == nil { |
| 450 | t.Errorf("state.new is left after a failure at %s", step) |
| 451 | } |
| 452 | s.Close() |
| 453 | s = open(t, dir, Options{}) |
| 454 | _, err := s.Redeem(ctx, au.hold, alice.credential, "") |
| 455 | if step < Renamed && err != nil || step >= Renamed && !errors.Is(err, ErrRefused) { |
| 456 | t.Errorf("after a restart, a redemption of the link that failed at %s: %v", step, err) |
| 457 | } |
| 458 | if _, err := s.Redeem(ctx, au.hold, bob.credential, ""); err != nil { |
| 459 | t.Errorf("after a restart, another link: %v", err) |
| 460 | } |
| 461 | }) |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | func TestAStateChangedUnderTheProcessIsUnavailable(t *testing.T) { |
| 466 | ctx := context.Background() |
| 467 | for name, change := range map[string]func(path string){ |
| 468 | "removed": func(path string) { os.Remove(path) }, |
| 469 | "replaced": func(path string) { |
| 470 | data, _ := os.ReadFile(path) |
| 471 | os.WriteFile(path+".x", data, 0o600) |
| 472 | os.Rename(path+".x", path) |
| 473 | }, |
| 474 | "appended": func(path string) { |
| 475 | f, _ := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0) |
| 476 | f.WriteString("x\n") |
| 477 | f.Close() |
| 478 | }, |
| 479 | } { |
| 480 | t.Run(name, func(t *testing.T) { |
| 481 | s, au, dir := created(t, Options{}) |
| 482 | alice := newLink(1, "alice") |
| 483 | au.set(alice) |
| 484 | token, err := s.Redeem(ctx, au.hold, alice.credential, "") |
| 485 | if err != nil { |
| 486 | t.Fatal(err) |
| 487 | } |
| 488 | change(filepath.Join(dir, stateFile)) |
| 489 | if _, err := s.sessionOf(t, token, au.current()); !errors.Is(err, ErrUnavailable) { |
| 490 | t.Errorf("a session: %v", err) |
| 491 | } |
| 492 | if err := s.Logout(ctx, au.hold, token); !errors.Is(err, ErrUnavailable) { |
| 493 | t.Errorf("a logout: %v", err) |
| 494 | } |
| 495 | }) |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | func TestOpenKeepsAStateItCannotRead(t *testing.T) { |
| 500 | for name, make := range map[string]func(path string){ |
| 501 | "garbage": func(path string) { os.WriteFile(path, []byte("garbage\n"), 0o600) }, |
| 502 | "unsorted": func(path string) { os.WriteFile(path, []byte(Type+"\n"+recordLine(2)+recordLine(1)), 0o600) }, |
| 503 | "open": func(path string) { os.WriteFile(path, []byte(Type+"\n"), 0o644); os.Chmod(path, 0o644) }, |
| 504 | "symlink": func(path string) { |
| 505 | os.WriteFile(path+".real", []byte(Type+"\n"), 0o600) |
| 506 | os.Symlink(path+".real", path) |
| 507 | }, |
| 508 | "fifo": func(path string) { syscall.Mkfifo(path, 0o600) }, |
| 509 | } { |
| 510 | t.Run(name, func(t *testing.T) { |
| 511 | dir := browserDir(t) |
| 512 | make(filepath.Join(dir, stateFile)) |
| 513 | s := open(t, dir, Options{}) |
| 514 | if s.Err() == nil || s.pending(t) { |
| 515 | t.Errorf("Open of a %s state: err %v, pending %v", name, s.Err(), s.pending(t)) |
| 516 | } |
| 517 | au := &authority{} |
| 518 | au.set() |
| 519 | if ok, err := s.Initialize(context.Background(), au.hold); ok || !errors.Is(err, ErrUnavailable) { |
| 520 | t.Errorf("Initialize over a %s state: %v, %v", name, ok, err) |
| 521 | } |
| 522 | }) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | func TestAnAbandonedNextStateIsNeverPromoted(t *testing.T) { |
| 527 | dir := browserDir(t) |
| 528 | next := Type + "\n" + recordLine(1) |
| 529 | if err := os.WriteFile(filepath.Join(dir, nextFile), []byte(next), 0o600); err != nil { |
| 530 | t.Fatal(err) |
| 531 | } |
| 532 | s := open(t, dir, Options{}) |
| 533 | if !s.pending(t) { |
| 534 | t.Error("an abandoned state.new made a state") |
| 535 | } |
| 536 | if _, err := os.Lstat(filepath.Join(dir, nextFile)); err == nil { |
| 537 | t.Error("Open kept the abandoned state.new") |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | func recordLine(n int) string { |
| 542 | l := newLink(n, "alice") |
| 543 | return fmt.Sprintf("link %s alice %s -\n", l.binding.ID, l.binding.Verifier) |
| 544 | } |