Files
bubble/test/relay.device.test.ts
T
trav 96c368dfd1 v0.5.0: bubble for Android 0.5.0 — text from the Rabbit's SIM on the desktop, and chat names and read status sync, through jai
A relay on jai (server/bubble_relay.py, stdlib + SQLite, tailscale address
only, bearer token) carries sealed blobs between the desktop and the Rabbit:
AES-256-GCM with a key the desktop makes and hands over USB (Sync to Rabbit),
so jai can neither read nor forge anything. It deletes what's been picked up,
and anything after 14 days.

- Desktop (src/main/relay.ts): "Rabbit SIM" is a number to text from, groups
  included; the SIM's history and new texts arrive; older photos are fetched
  when shown; names and read/unread/archived go both ways. Settings → Rabbit
  relay connects it.
- Rabbit (Relay.java, RelayJob): no timer of its own for the battery; it rides
  along with the background checks, plus one network job after a text, a send
  or a swipe. Each send from the desktop goes out at most once and not after a
  day. Plain http only to jai and localhost (network_security_config).
- scripts/deploy-jai.sh; tests: server unit tests, an end-to-end relay test,
  and DEVICE=1 relay.device.test.ts against the dev app.
2026-11-06 17:35:00 -08:00

89 lines
4.3 KiB
TypeScript

// The relay against the dev app on a plugged-in Rabbit (DEVICE=1 npm test --
// test/relay.device.test.ts). A throwaway local relay, reached from the Rabbit
// through adb reverse; the dev app's made-up data (tools/push_sandbox.py).
// Never the real app, never jai.
import { execFileSync, spawn, type ChildProcess } from 'node:child_process'
import fs from 'node:fs'
import net from 'node:net'
import os from 'node:os'
import path from 'node:path'
import { afterAll, describe, expect, it, vi } from 'vitest'
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'relay-device-'))
vi.mock('electron', () => ({
app: { getPath: () => tmp },
safeStorage: { isEncryptionAvailable: () => true, encryptString: (s: string) => Buffer.from(s), decryptString: (b: Buffer) => b.toString() },
nativeImage: { createFromBuffer: () => ({ getSize: () => ({ width: 0, height: 0 }) }) },
net: { fetch: () => Promise.reject(new Error('no network in tests')) }
}))
const { MessageDb } = await import('../src/main/db')
const { MediaStore } = await import('../src/main/media')
const { newKey, RelayEngine } = await import('../src/main/relay')
const PKG = 'me.teafry.voipmessages.dev'
const DEVICE_PORT = 18080
let server: ChildProcess | undefined
afterAll(() => {
server?.kill()
try {
execFileSync('adb', ['reverse', '--remove', `tcp:${DEVICE_PORT}`])
} catch {
/* gone already */
}
})
const sleep = (ms: number): Promise<void> => new Promise((r) => setTimeout(r, ms))
describe.skipIf(!process.env['DEVICE'])('relay with the dev app on the Rabbit', () => {
it('seals in Java and opens in Node, and back', { timeout: 240_000 }, async () => {
const port = await new Promise<number>((resolve) => {
const s = net.createServer().listen(0, '127.0.0.1', () => {
const p = (s.address() as net.AddressInfo).port
s.close(() => resolve(p))
})
})
const data = path.join(tmp, 'server')
server = spawn('python3', [path.join(__dirname, '../server/bubble_relay.py'), '--port', String(port), '--data', data], { stdio: 'ignore' })
const url = `http://127.0.0.1:${port}`
for (let i = 0; i < 50 && !(await fetch(`${url}/health`).then((r) => r.ok).catch(() => false)); i++) await sleep(100)
const token = fs.readFileSync(path.join(data, 'token'), 'utf8').trim()
const key = newKey()
execFileSync('adb', ['reverse', `tcp:${DEVICE_PORT}`, `tcp:${port}`])
execFileSync('python3', [path.join(__dirname, '../android/tools/push_sandbox.py'), '--relay', `http://127.0.0.1:${DEVICE_PORT}`], {
env: { ...process.env, RELAY_TOKEN: token, RELAY_KEY: key },
stdio: 'ignore'
})
const db = new MessageDb(path.join(tmp, 'messages.db'))
const media = new MediaStore(path.join(tmp, 'media'), db)
const engine = new RelayEngine(db, media, { url, token, key }, 30)
;(engine as unknown as { running: boolean }).running = true
db.setName('sim', '5415550150', 'Made-up SIM friend')
engine.stateChanged()
const tempId = db.insertPending({ did: 'sim', contact: '5415550150', direction: 'out', body: 'from the desk', ts: Date.now() })
engine.send(tempId, '5415550150', 'from the desk', [])
await engine.syncNow()
// Opening the app has it check the relay (a job a few seconds later).
execFileSync('adb', ['shell', 'am', 'start', '-n', `${PKG}/me.teafry.voipmessages.MainActivity`])
let simIds: number[] = []
for (let i = 0; i < 40; i++) {
await sleep(4000)
await engine.syncNow()
simIds = db.getConversationMessages('sim', '5415550150').map((m) => m.id)
if (simIds.includes(2 ** 51 + 1) && db.getMessage(tempId)?.status === 'failed') break
// The send's answer comes on the Rabbit's next round: nudge it.
if (i % 5 === 4) execFileSync('adb', ['shell', 'am', 'start', '-S', '-n', `${PKG}/me.teafry.voipmessages.MainActivity`])
}
// The made-up SIM history came over, decrypted.
expect(simIds).toEqual(expect.arrayContaining([2 ** 51 + 1, 2 ** 51 + 2]))
expect(db.getConversationMessages('sim', '5415550150').find((m) => m.id === 2 ** 51 + 1)?.body).toBe('Texting your SIM number now')
// The send was read and answered (the dev app isn't the default SMS app, so: a reason).
expect(db.getMessage(tempId)?.status).toBe('failed')
expect(db.getMessage(tempId)?.error).toMatch(/texting app/)
engine.stop()
db.close()
})
})