交付 Windows 与 Web 端到端加密附件预览
This commit is contained in:
1 parent
d1efdedc57
commit
c24960d22a
19 files changed
+421
-58
No files matched your search
@@ -116,3 +116,33 @@ test('设备密钥包验证签名并只领取一次', { timeout: 25000 }, async
|
||||
assert.equal((await call('/devices/' + owner.device.id, 'DELETE', owner.token)).status, 204)
|
||||
assert.deepEqual(await json(directory, 'GET', contactUser.token), [])
|
||||
})
|
||||
|
||||
test('附件访问按设备授权并支持相同密文重试', { timeout: 15000 }, async () => {
|
||||
const sender = await register()
|
||||
const recipient = await register()
|
||||
const stranger = await register()
|
||||
const path = '/api/v1/attachments/' + randomUUID()
|
||||
const bytes = randomBytes(256)
|
||||
const upload = (body) => fetch(base + path + '?recipient=' + recipient.device.id, {
|
||||
method: 'POST',
|
||||
headers: { Authorization: 'Bearer ' + sender.token, 'Content-Type': 'application/octet-stream' },
|
||||
body,
|
||||
signal: AbortSignal.timeout(10_000),
|
||||
})
|
||||
const download = (token) => fetch(base + path, {
|
||||
headers: { Authorization: 'Bearer ' + token },
|
||||
signal: AbortSignal.timeout(10_000),
|
||||
})
|
||||
assert.equal((await upload(bytes)).status, 404)
|
||||
await contact(sender, recipient, 'request')
|
||||
await contact(recipient, sender, 'accept')
|
||||
assert.equal((await upload(bytes)).status, 204)
|
||||
assert.equal((await upload(bytes)).status, 204)
|
||||
assert.equal((await upload(randomBytes(256))).status, 409)
|
||||
assert.equal((await download(stranger.token)).status, 404)
|
||||
const response = await download(recipient.token)
|
||||
assert.equal(response.status, 200)
|
||||
assert.deepEqual(Buffer.from(await response.arrayBuffer()), bytes)
|
||||
assert.equal((await call('/devices/' + recipient.device.id, 'DELETE', recipient.token)).status, 204)
|
||||
assert.equal((await download(recipient.token)).status, 401)
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { randomBytes } from 'node:crypto'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { mkdtemp, readFile, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import test from 'node:test'
|
||||
@@ -46,7 +46,7 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
|
||||
const profile = await mkdtemp(join(tmpdir(), 'xhchat-mls-test-'))
|
||||
let desktop
|
||||
try {
|
||||
const web = await browser.newPage()
|
||||
const web = await browser.newPage({ acceptDownloads: true })
|
||||
await web.goto(base)
|
||||
desktop = await _electron.launch({
|
||||
executablePath: electron,
|
||||
@@ -106,6 +106,33 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
|
||||
await send(windows, reply)
|
||||
await web.getByText(reply, { exact: true }).waitFor()
|
||||
|
||||
const contents = Buffer.from('端到端文件 ' + randomBytes(32).toString('hex'))
|
||||
const fileName = '跨端验收.txt'
|
||||
const upload = windows.waitForRequest((req) => req.method() === 'POST' && req.url().includes('/api/v1/attachments/'))
|
||||
await windows.locator('input[type=file]').setInputFiles({ name: fileName, mimeType: 'text/plain', buffer: contents })
|
||||
const fileRequest = await upload
|
||||
const uploaded = fileRequest.postDataBuffer()
|
||||
assert.ok(uploaded)
|
||||
assert.equal(uploaded.length, contents.length + 16)
|
||||
assert.ok(!uploaded.includes(contents))
|
||||
assert.ok(!fileRequest.url().includes(encodeURIComponent(fileName)))
|
||||
const fileButton = web.getByRole('button', { name: '下载 ' + fileName, exact: true })
|
||||
await fileButton.waitFor()
|
||||
const fileURL = new URL(fileRequest.url())
|
||||
await web.route(fileURL.origin + fileURL.pathname, async (route) => {
|
||||
const response = await route.fetch()
|
||||
const corrupted = await response.body()
|
||||
corrupted[0] ^= 1
|
||||
await route.fulfill({ response, body: corrupted })
|
||||
}, { times: 1 })
|
||||
await fileButton.click()
|
||||
await web.getByText('附件完整性校验失败').waitFor()
|
||||
const downloaded = web.waitForEvent('download')
|
||||
await fileButton.click()
|
||||
const file = await downloaded
|
||||
assert.equal(file.suggestedFilename(), fileName)
|
||||
assert.deepEqual(await readFile(await file.path()), contents)
|
||||
|
||||
await windows.getByRole('button', { name: '退出登录' }).click()
|
||||
await windows.getByRole('heading', { name: '登录 XHChat' }).waitFor()
|
||||
const offline = '离线补拉 ' + randomBytes(8).toString('hex')
|
||||
|
||||
Reference in new issue
Block a user