| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "testing" |
| 9 | |
| 10 | "lchat/internal/bot" |
| 11 | "lchat/internal/e2ee" |
| 12 | ) |
| 13 | |
| 14 | // mintAgentInviteForTest does what the admin console does when "This is an |
| 15 | // agent" is ticked: derive id + claim_hash from a fresh secret T client-side, |
| 16 | // then store them via the admin API. Returns T (the link #fragment). |
| 17 | func mintAgentInviteForTest(t *testing.T, tsURL string) string { |
| 18 | return mintInviteForTest(t, tsURL, "TestBot", true) |
| 19 | } |
| 20 | |
| 21 | // mintInviteForTest mints a labelled invite (agent or plain member). The label |
| 22 | // matters to the runtime tests: it becomes the roster label, which is what the |
| 23 | // bots-never-talk-to-bots rule matches payload names against. |
| 24 | func mintInviteForTest(t *testing.T, tsURL, label string, agent bool) string { |
| 25 | t.Helper() |
| 26 | secret, err := e2ee.RandomSecret() |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | inv, err := e2ee.DeriveInvite(secret) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | body, _ := json.Marshal(map[string]any{ |
| 35 | "label": label, |
| 36 | "id": inv.ID, |
| 37 | "claim_hash": inv.ClaimHash, |
| 38 | "agent": agent, |
| 39 | }) |
| 40 | req, _ := http.NewRequest("POST", tsURL+"/api/admin/key-invites", bytes.NewReader(body)) |
| 41 | req.Header.Set("Authorization", "Bearer admintok") |
| 42 | req.Header.Set("Content-Type", "application/json") |
| 43 | resp, err := http.DefaultClient.Do(req) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | defer resp.Body.Close() |
| 48 | var out struct { |
| 49 | OK bool `json:"ok"` |
| 50 | } |
| 51 | if err := json.NewDecoder(resp.Body).Decode(&out); err != nil || !out.OK { |
| 52 | t.Fatalf("mint invite: status %d ok=%v err=%v", resp.StatusCode, out.OK, err) |
| 53 | } |
| 54 | return secret |
| 55 | } |
| 56 | |
| 57 | func TestBotProvisionAgainstRealServer(t *testing.T) { |
| 58 | ts, s := newTestServer(t) |
| 59 | secret := mintAgentInviteForTest(t, ts.URL) |
| 60 | |
| 61 | sess, err := bot.Provision(ts.URL, secret, "TestBot") |
| 62 | if err != nil { |
| 63 | t.Fatalf("Provision: %v", err) |
| 64 | } |
| 65 | if sess.Token == "" || sess.UserPub == "" || sess.UserPriv == "" { |
| 66 | t.Fatal("session is missing fields") |
| 67 | } |
| 68 | |
| 69 | // The minted device token must authenticate like any member's. |
| 70 | if !s.tokenOK(sess.Token) { |
| 71 | t.Error("provisioned token doesn't authenticate") |
| 72 | } |
| 73 | uid, ok := s.db.deviceTokenUser(sess.Token) |
| 74 | if !ok { |
| 75 | t.Fatal("token maps to no user") |
| 76 | } |
| 77 | if !s.db.isAgent(uid) { |
| 78 | t.Error("provisioned user is not marked as an agent") |
| 79 | } |
| 80 | |
| 81 | // The registered public key must be the session's — and usable: wrap a |
| 82 | // thread key to it (as an inviter's browser would) and unwrap with the |
| 83 | // session's private key. |
| 84 | registered := s.db.userPublicKey(uid) |
| 85 | if registered != sess.UserPub { |
| 86 | t.Fatalf("roster pubkey %q != session pubkey %q", registered, sess.UserPub) |
| 87 | } |
| 88 | threadKey, err := e2ee.GenerateKey() |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | wrapped, err := e2ee.WrapThreadKey(e2ee.B64uEncode(threadKey), registered) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | back, err := e2ee.UnwrapThreadKey(wrapped, sess.UserPriv) |
| 97 | if err != nil { |
| 98 | t.Fatalf("unwrap with session key: %v", err) |
| 99 | } |
| 100 | if back != e2ee.B64uEncode(threadKey) { |
| 101 | t.Error("thread key wrapped to the roster pubkey doesn't unwrap with the session key") |
| 102 | } |
| 103 | |
| 104 | // Single-use: a second claim of the same invite must fail. |
| 105 | if _, err := bot.Provision(ts.URL, secret, "TestBot2"); err == nil { |
| 106 | t.Error("re-provisioning a used invite succeeded") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestBotSessionFileNodeCompat(t *testing.T) { |
| 111 | // The Go bot must read the Node sidecar's session.json as-is (same identity, |
| 112 | // no re-provisioning) and write files Node can read back. |
| 113 | path := t.TempDir() + "/session.json" |
| 114 | nodeJSON := `{ |
| 115 | "token": "tok123", |
| 116 | "name": "Eleven", |
| 117 | "userPub": "BPub", |
| 118 | "userPriv": "Priv" |
| 119 | }` |
| 120 | if err := os.WriteFile(path, []byte(nodeJSON), 0o600); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | s, err := bot.LoadSession(path) |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | if s == nil || s.Token != "tok123" || s.Name != "Eleven" || s.UserPub != "BPub" || s.UserPriv != "Priv" { |
| 128 | t.Fatalf("bad session: %+v", s) |
| 129 | } |
| 130 | |
| 131 | if err := s.Save(path); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | var round map[string]string |
| 135 | raw, _ := os.ReadFile(path) |
| 136 | if err := json.Unmarshal(raw, &round); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | for _, k := range []string{"token", "name", "userPub", "userPriv"} { |
| 140 | if round[k] == "" { |
| 141 | t.Errorf("saved session is missing Node-compat key %q", k) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | missing, err := bot.LoadSession(t.TempDir() + "/nope.json") |
| 146 | if err != nil || missing != nil { |
| 147 | t.Errorf("missing session file should be (nil, nil), got (%v, %v)", missing, err) |
| 148 | } |
| 149 | } |
| 150 | |