add missing ws.js and introduce save check for both sides
This commit is contained in:
1 parent
ff7b84af02
commit
1bfeb9843a
4 files changed
+189
-37
No files matched your search
+64
-6
@@ -136,6 +136,19 @@ function holdReads() {
|
||||
};
|
||||
}
|
||||
|
||||
// Init script for a sender: keeps its data channels in window.channels, so a test can send on them
|
||||
// like a peer that skips the security check.
|
||||
function recordChannels() {
|
||||
const page = window as unknown as { channels: RTCDataChannel[] };
|
||||
page.channels = [];
|
||||
const create = RTCPeerConnection.prototype.createDataChannel;
|
||||
RTCPeerConnection.prototype.createDataChannel = function (this: RTCPeerConnection, ...args) {
|
||||
const channel = create.apply(this, args);
|
||||
page.channels.push(channel);
|
||||
return channel;
|
||||
};
|
||||
}
|
||||
|
||||
const received = (page: Page) => page.evaluate(() => (window as unknown as { received: number }).received);
|
||||
const release = (page: Page) => page.evaluate(() => (window as unknown as { release: () => void }).release());
|
||||
|
||||
@@ -171,9 +184,8 @@ async function withLogs(sender: Page, receiver: Page, fn: () => Promise<void>) {
|
||||
}
|
||||
}
|
||||
|
||||
// Connects, verifies the security check and confirms it. Returns the download, which starts on its
|
||||
// own as soon as the file is announced, and the receiver log at that moment.
|
||||
async function start(mode: string, sender: Page, receiver: Page, source: string) {
|
||||
// Connects and verifies that both sides show the same security check, swapped.
|
||||
async function connect(mode: string, sender: Page, receiver: Page, source: string) {
|
||||
await sender.setInputFiles('input[type=file]', source);
|
||||
await MODES[mode]!(sender, receiver);
|
||||
|
||||
@@ -183,13 +195,26 @@ async function start(mode: string, sender: Page, receiver: Page, source: string)
|
||||
});
|
||||
await expect(receiver.locator('#check-remote')).toHaveText((await check(sender, 'local'))!);
|
||||
await expect(receiver.locator('#check-local')).toHaveText((await check(sender, 'remote'))!);
|
||||
await expect(sender.locator('#log')).toContainText('waiting for the security check');
|
||||
await expect(receiver.locator('#log')).not.toContainText('receiving');
|
||||
for (const page of [sender, receiver]) {
|
||||
await expect(page.locator('#log')).toContainText('waiting for the security check');
|
||||
}
|
||||
}
|
||||
|
||||
const started = receiver
|
||||
// Resolves with the download, which starts on its own as soon as the file is announced, and the
|
||||
// receiver log at that moment.
|
||||
const downloadStarts = (receiver: Page) =>
|
||||
receiver
|
||||
.waitForEvent('download', { timeout: test.info().timeout })
|
||||
.then(async (download) => ({ download, logAtStart: await log(receiver) }));
|
||||
|
||||
// Connects and confirms the security check on both sides.
|
||||
async function start(mode: string, sender: Page, receiver: Page, source: string) {
|
||||
await connect(mode, sender, receiver, source);
|
||||
const started = downloadStarts(receiver);
|
||||
await sender.click('#confirm');
|
||||
await expect(sender.locator('#log')).toContainText("waiting for the receiver's security check");
|
||||
await expect(receiver.locator('#log')).not.toContainText('receiving');
|
||||
await receiver.click('#confirm');
|
||||
return started;
|
||||
}
|
||||
|
||||
@@ -299,6 +324,39 @@ for (const name of Object.keys(BROWSERS)) {
|
||||
});
|
||||
});
|
||||
|
||||
test(`check: ${name} accepts nothing before the receiver confirmed`, async () => {
|
||||
const [sender, receiver] = await Promise.all([open(name, [], recordChannels), open(name, [])]);
|
||||
const source = test.info().outputPath('payload.bin');
|
||||
await writeFile(source, randomBytes(SIZE));
|
||||
|
||||
await withLogs(sender!, receiver!, async () => {
|
||||
let downloads = 0;
|
||||
receiver!.on('download', () => downloads++);
|
||||
await connect('offline', sender!, receiver!, source);
|
||||
// What a man in the middle could send without waiting for anyone's confirmation.
|
||||
await sender!.evaluate(() => {
|
||||
const [channel] = (window as unknown as { channels: RTCDataChannel[] }).channels;
|
||||
channel!.send(JSON.stringify({ name: 'injected.bin', size: 4 }));
|
||||
channel!.send(new Uint8Array(4));
|
||||
});
|
||||
await sender!.click('#confirm');
|
||||
await expect(sender!.locator('#log')).toContainText("waiting for the receiver's security check");
|
||||
// Proving that nothing starts needs a moment, a download would start within milliseconds.
|
||||
await receiver!.waitForTimeout(1000);
|
||||
expect(await log(receiver!)).not.toContain('receiving');
|
||||
expect(await log(sender!)).not.toContain('sending');
|
||||
expect(downloads).toBe(0);
|
||||
|
||||
const started = downloadStarts(receiver!);
|
||||
await receiver!.click('#confirm');
|
||||
const { download } = await started;
|
||||
expect(download.suggestedFilename()).toBe('payload.bin');
|
||||
expect(await hash(await download.path())).toBe(await hash(source));
|
||||
await expect(sender!.locator('#log')).toContainText('delivered');
|
||||
expect(downloads).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
test(`disconnect: ${name} fails the download when the sender vanishes`, async () => {
|
||||
const [sender, receiver] = await Promise.all([open(name, []), open(name, [])]);
|
||||
const source = test.info().outputPath('payload.bin');
|
||||
|
||||
Reference in new issue
Block a user