142 lines
6.7 KiB
JavaScript
142 lines
6.7 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { randomBytes } from 'node:crypto'
|
|
import { mkdtemp, rm } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import test from 'node:test'
|
|
import { _electron, chromium } from 'playwright'
|
|
import electron from 'electron'
|
|
|
|
const base = process.env.XHCHAT_TEST_URL ?? 'http://127.0.0.1:8080'
|
|
const work = (page) => page.getByRole('navigation', { name: '工作区' })
|
|
const testUser = () => 'qa_' + randomBytes(6).toString('hex')
|
|
|
|
async function login(page, username, password, register) {
|
|
if (register) await page.getByRole('button', { name: '注册', exact: true }).click()
|
|
await page.getByLabel('用户名').fill(username)
|
|
await page.getByLabel('密码', { exact: true }).fill(password)
|
|
await page.locator('button.primary').click()
|
|
await page.getByText('实时连接已建立').waitFor()
|
|
}
|
|
|
|
async function enable(page) {
|
|
await page.getByRole('button', { name: '启用本机加密聊天' }).click()
|
|
await page.locator('.security-code').waitFor({ timeout: 10_000 })
|
|
return page.locator('.security-code').textContent()
|
|
}
|
|
|
|
async function trust(page, code) {
|
|
await page.getByLabel('对方安全码').fill(code)
|
|
await page.getByRole('button', { name: '核验设备' }).click()
|
|
await page.getByText('设备已验证').waitFor()
|
|
}
|
|
|
|
async function send(page, text) {
|
|
await page.getByRole('textbox', { name: '消息', exact: true }).fill(text)
|
|
await page.getByRole('button', { name: '发送', exact: true }).click()
|
|
try {
|
|
await page.getByText(text, { exact: true }).waitFor({ timeout: 10_000 })
|
|
} catch (error) {
|
|
throw new Error(JSON.stringify(await page.locator('[role=alert]').allTextContents()), { cause: error })
|
|
}
|
|
}
|
|
|
|
test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢复', { timeout: 90000 }, async () => {
|
|
const browser = await chromium.launch({ channel: process.env.XHCHAT_TEST_BROWSER ?? 'msedge' })
|
|
const profile = await mkdtemp(join(tmpdir(), 'xhchat-mls-test-'))
|
|
let desktop
|
|
try {
|
|
const web = await browser.newPage()
|
|
await web.goto(base)
|
|
desktop = await _electron.launch({
|
|
executablePath: electron,
|
|
args: [join(import.meta.dirname, '..', 'Desktop'), '--user-data-dir=' + profile],
|
|
env: { ...process.env, XHCHAT_SERVER_URL: base },
|
|
})
|
|
const windows = await desktop.firstWindow()
|
|
const clientErrors = []
|
|
for (const [label, page] of [['web', web], ['windows', windows]]) {
|
|
page.on('pageerror', (error) => clientErrors.push(label + ': ' + error.message))
|
|
page.on('console', (message) => { if (message.type() === 'error') clientErrors.push(label + ': ' + message.text()) })
|
|
}
|
|
const a = testUser(), b = testUser()
|
|
const password = randomBytes(24).toString('hex')
|
|
await login(web, a, password, true)
|
|
await login(windows, b, password, true)
|
|
await work(web).getByRole('link', { name: '联系人' }).click()
|
|
await work(windows).getByRole('link', { name: '联系人' }).click()
|
|
await web.getByLabel('按用户名查找').fill(b)
|
|
await web.getByRole('button', { name: '查找' }).click()
|
|
await web.getByRole('button', { name: '添加好友' }).click()
|
|
await windows.getByRole('button', { name: new RegExp(a) }).click()
|
|
await windows.getByRole('button', { name: '接受申请' }).click()
|
|
await web.getByRole('link', { name: '打开加密聊天' }).click()
|
|
await windows.getByRole('link', { name: '打开加密聊天' }).click()
|
|
const windowsChat = windows.url()
|
|
let webCode, desktopCode
|
|
try {
|
|
;[webCode, desktopCode] = await Promise.all([enable(web), enable(windows)])
|
|
} catch (error) {
|
|
throw new Error(JSON.stringify({ web: await web.locator('[role=alert]').allTextContents(), windows: await windows.locator('[role=alert]').allTextContents(), clientErrors }), { cause: error })
|
|
}
|
|
assert.match(webCode, /^[0-9a-f]{64}$/)
|
|
assert.match(desktopCode, /^[0-9a-f]{64}$/)
|
|
assert.notEqual(webCode, desktopCode)
|
|
await web.getByRole('button', { name: '刷新' }).click()
|
|
await windows.getByRole('button', { name: '刷新' }).click()
|
|
await web.getByLabel('对方安全码').fill('0'.repeat(64))
|
|
await web.getByRole('button', { name: '核验设备' }).click()
|
|
await web.getByText('安全码不匹配').waitFor()
|
|
assert.equal(await web.getByRole('button', { name: '发送', exact: true }).count(), 0)
|
|
await trust(web, desktopCode)
|
|
await trust(windows, webCode)
|
|
|
|
const text = 'Web 到 Windows ' + randomBytes(8).toString('hex')
|
|
const packets = []
|
|
web.on('request', (req) => {
|
|
if (req.method() === 'POST' && req.url().endsWith('/api/v1/inbox')) packets.push(req.postDataJSON())
|
|
})
|
|
await send(web, text)
|
|
const packet = packets.find((entry) => Buffer.from(entry.ciphertext, 'base64')[3] === 2)
|
|
assert.ok(packet)
|
|
assert.ok(!Buffer.from(packet.ciphertext, 'base64').includes(Buffer.from(text)))
|
|
assert.deepEqual(Object.keys(packet).sort(), ['ciphertext', 'messageId', 'recipientDevice'])
|
|
await windows.getByText(text, { exact: true }).waitFor()
|
|
const reply = 'Windows 到 Web ' + randomBytes(8).toString('hex')
|
|
await send(windows, reply)
|
|
await web.getByText(reply, { exact: true }).waitFor()
|
|
|
|
await windows.getByRole('button', { name: '退出登录' }).click()
|
|
await windows.getByRole('heading', { name: '登录 XHChat' }).waitFor()
|
|
const offline = '离线补拉 ' + randomBytes(8).toString('hex')
|
|
await send(web, offline)
|
|
await login(windows, b, password, false)
|
|
await windows.evaluate((url) => { location.hash = new URL(url).hash }, windowsChat)
|
|
assert.equal(await enable(windows), desktopCode)
|
|
await windows.getByText(offline, { exact: true }).waitFor()
|
|
const stored = await windows.evaluate(async () => {
|
|
const db = await new Promise((resolve, reject) => {
|
|
const request = indexedDB.open('xhchat-encrypted-chat', 1)
|
|
request.onsuccess = () => resolve(request.result)
|
|
request.onerror = () => reject(request.error)
|
|
})
|
|
try {
|
|
return await new Promise((resolve, reject) => {
|
|
const request = db.transaction('vaults').objectStore('vaults').getAll()
|
|
request.onsuccess = () => resolve(request.result.map((value) => ({
|
|
wrapped: typeof value.key === 'string',
|
|
encrypted: value.sealed instanceof ArrayBuffer && value.sealed.byteLength > 0,
|
|
fields: Object.keys(value).sort(),
|
|
})))
|
|
request.onerror = () => reject(request.error)
|
|
})
|
|
} finally { db.close() }
|
|
})
|
|
assert.deepEqual(stored, [{ wrapped: true, encrypted: true, fields: ['iv', 'key', 'sealed'] }])
|
|
} finally {
|
|
await desktop?.close()
|
|
await browser.close()
|
|
await rm(profile, { recursive: true, maxRetries: 5, retryDelay: 200, force: true })
|
|
}
|
|
})
|