| 1 | package main |
| 2 | |
| 3 | // 🤖 Live-wire test for the CLI vault path (docs/locked-vaults.md): two |
| 4 | // members against the real router, one emitting vault events through |
| 5 | // Client.SendEvent, the other folding them back through Client.Events — |
| 6 | // including a >1-page history, so the full-log pager is exercised where a |
| 7 | // silent truncation would fold a vault with no keyring. |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "encoding/json" |
| 12 | "fmt" |
| 13 | "net/http" |
| 14 | "testing" |
| 15 | |
| 16 | "lchat/internal/e2ee" |
| 17 | "lchat/internal/vault" |
| 18 | ) |
| 19 | |
| 20 | func TestElevenVaultEventsRoundTrip(t *testing.T) { |
| 21 | ts, srv := newTestServer(t) |
| 22 | // The message brake (burst 30, ~30/min) would turn a 200+-message history |
| 23 | // into a seven-minute test; the brake has its own tests, this one is about |
| 24 | // the pager. |
| 25 | srv.brakes.messages = newModLimiter(100000, 100000) |
| 26 | term := joinForTest(t, ts.URL, "Terminal") |
| 27 | friend := joinForTest(t, ts.URL, "Friend") |
| 28 | termUID, err := term.Me() |
| 29 | if err != nil { |
| 30 | t.Fatalf("Me: %v", err) |
| 31 | } |
| 32 | friendUID, err := friend.Me() |
| 33 | if err != nil { |
| 34 | t.Fatalf("Me: %v", err) |
| 35 | } |
| 36 | |
| 37 | // A shared group, the operator-vault shape: thread key wrapped to both. |
| 38 | tk, err := e2ee.GenerateKey() |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | tkB64 := e2ee.B64uEncode(tk) |
| 43 | wrapTo := func(pub string) string { |
| 44 | w, err := e2ee.WrapThreadKey(tkB64, pub) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | return w |
| 49 | } |
| 50 | nameBlob, err := e2ee.EncryptJSON(tk, map[string]string{"name": "ops"}) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | body, _ := json.Marshal(map[string]any{ |
| 55 | "token": friend.Session.Token, "kind": "group", "name_blob": nameBlob, |
| 56 | "members": []map[string]string{ |
| 57 | {"user_id": friendUID, "wrapped": wrapTo(friend.Session.UserPub)}, |
| 58 | {"user_id": termUID, "wrapped": wrapTo(term.Session.UserPub)}, |
| 59 | }, |
| 60 | }) |
| 61 | resp, err := http.Post(ts.URL+"/api/threads", "application/json", bytes.NewReader(body)) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | var created struct { |
| 66 | ThreadID string `json:"thread_id"` |
| 67 | } |
| 68 | if err := json.NewDecoder(resp.Body).Decode(&created); err != nil || created.ThreadID == "" { |
| 69 | t.Fatalf("create thread: %v (id %q)", err, created.ThreadID) |
| 70 | } |
| 71 | resp.Body.Close() |
| 72 | tid := created.ThreadID |
| 73 | |
| 74 | // Terminal writes a vault life: one plain add, the lock re-sealing it, and |
| 75 | // enough sealed adds to push the history past one /api/messages page (200) |
| 76 | // — the pager must fold the FIRST page's lock event or everything below is |
| 77 | // wrong. Blobs are opaque strings to the fold; the wire crypto (thread-key |
| 78 | // AES-GCM) is the real thing via SendEvent. |
| 79 | if err := term.SendEvent(tid, map[string]any{"type": "vault.add", "id": "s0", "aid": "a0", "label": "WiFi", "value": "hunter2"}); err != nil { |
| 80 | t.Fatalf("SendEvent add: %v", err) |
| 81 | } |
| 82 | if err := term.SendEvent(tid, map[string]any{ |
| 83 | "type": "vault.lock", "aid": "a-lock", "pub": "vault-pub", |
| 84 | "wrap": map[string]any{"id": "w1", "kind": "keychain", "label": "Terminal — CLI", "wrapped": "wrap-blob"}, |
| 85 | "sealed": map[string]any{"s0": "sealed-s0"}, |
| 86 | }); err != nil { |
| 87 | t.Fatalf("SendEvent lock: %v", err) |
| 88 | } |
| 89 | // A plain chat line in between must never fold into the vault. |
| 90 | if err := term.Send(tid, "deploying now"); err != nil { |
| 91 | t.Fatalf("Send: %v", err) |
| 92 | } |
| 93 | const extra = 205 |
| 94 | for i := 1; i <= extra; i++ { |
| 95 | ev := map[string]any{ |
| 96 | "type": "vault.add", "id": fmt.Sprintf("s%03d", i), "aid": fmt.Sprintf("a%03d", i), |
| 97 | "label": fmt.Sprintf("KEY_%03d", i), "sealed": fmt.Sprintf("sealed-%03d", i), |
| 98 | } |
| 99 | if err := term.SendEvent(tid, ev); err != nil { |
| 100 | t.Fatalf("SendEvent %d: %v", i, err) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Friend folds the whole thing back — 200-per-page means at least two pages. |
| 105 | events, err := friend.Events(tid, "vault.") |
| 106 | if err != nil { |
| 107 | t.Fatalf("Events: %v", err) |
| 108 | } |
| 109 | if len(events) != extra+2 { |
| 110 | t.Fatalf("folded %d vault events, want %d (partial page walk?)", len(events), extra+2) |
| 111 | } |
| 112 | s := vault.FoldAll(events) |
| 113 | if s.Keyring == nil || s.Keyring.Pub != "vault-pub" || len(s.Keyring.Wraps) != 1 { |
| 114 | t.Fatalf("keyring didn't survive the page walk: %+v", s.Keyring) |
| 115 | } |
| 116 | if len(s.Items) != extra+1 { |
| 117 | t.Fatalf("items %d, want %d", len(s.Items), extra+1) |
| 118 | } |
| 119 | if it := s.FindItem("WiFi"); it == nil || it.Sealed != "sealed-s0" || it.Value != "" { |
| 120 | t.Fatalf("the lock's re-seal was lost across pages: %+v", it) |
| 121 | } |
| 122 | if it := s.FindItem("KEY_205"); it == nil || it.Sealed != "sealed-205" { |
| 123 | t.Fatalf("last item wrong: %+v", it) |
| 124 | } |
| 125 | // The transport injected attribution: the emitter's uid and name, not the |
| 126 | // event author's claims. |
| 127 | if it := s.FindItem("KEY_001"); it == nil || it.By != termUID || it.ByName != "Terminal" { |
| 128 | t.Fatalf("attribution not injected: %+v", it) |
| 129 | } |
| 130 | } |
| 131 | |