native host tests

This commit is contained in:
antelle 2021-04-10 18:38:27 +02:00
parent ef6e5ebcbe
commit 1e6034ce37
No known key found for this signature in database
GPG Key ID: 63C9777AAB7C563C
3 changed files with 92 additions and 18 deletions

View File

@ -11,3 +11,6 @@ format:
run:
echo -n 020000007b7d | xxd -r -p | build/keeweb-native-messaging-host chrome-extension://enjifmdnhaddmajefhfaoglcfdobkcpj
tests:
../../node_modules/.bin/mocha test/native-host-test.mjs

View File

@ -1,18 +0,0 @@
const os = require('os');
const net = require('net');
const path = require('path');
const fs = require('fs');
const sockPath = path.join(os.tmpdir(), 'keeweb-example.sock');
try {
fs.unlinkSync(sockPath);
} catch {}
const server = net.createServer((socket) => {
socket.on('data', (data) => {
socket.write(data);
});
socket.on('end', () => {});
});
server.listen(sockPath);

View File

@ -0,0 +1,89 @@
import os from 'os';
import net from 'net';
import path from 'path';
import fs from 'fs';
import childProcess from 'child_process';
import { expect } from 'chai';
describe('KeeWeb extension native module host', function () {
const sockPath = path.join(os.tmpdir(), 'keeweb.sock');
const hostPath = 'build/keeweb-native-messaging-host';
const extensionOrigin = 'chrome-extension://enjifmdnhaddmajefhfaoglcfdobkcpj';
let server;
let serverConnection = undefined;
this.timeout(5000);
afterEach((done) => {
serverConnection = undefined;
if (server) {
server.close(done);
server = null;
} else {
done();
}
});
it('exits without arguments', (done) => {
const process = childProcess.spawn(hostPath);
process.on('exit', (code) => {
expect(code).to.eql(1);
done();
});
});
it('exits with bad origin', (done) => {
const process = childProcess.spawn(hostPath, ['something']);
process.on('exit', (code) => {
expect(code).to.eql(1);
done();
});
});
it('exits on host exit', (done) => {
startServer();
const process = childProcess.spawn(hostPath, [extensionOrigin]);
process.on('exit', (code) => {
expect(code).to.eql(0);
done();
});
setTimeout(() => {
expect(serverConnection).to.be.ok;
server.close();
server = null;
serverConnection.end();
}, 500);
});
it('sends messages between stdio and socket', (done) => {
startServer();
const process = childProcess.spawn(hostPath, [extensionOrigin]);
process.on('exit', (code) => {
expect(code).to.eql(0);
done();
});
process.stdin.write('ping');
process.stderr.on('data', (data) => console.log(data.toString()));
process.stdout.on('data', (data) => {
expect(data.toString()).to.eql('ping response');
server.close();
server = null;
serverConnection.end();
});
});
function startServer() {
try {
fs.unlinkSync(sockPath);
} catch {}
server = net.createServer((socket) => {
serverConnection = socket;
socket.on('data', (data) => {
socket.write(Buffer.concat([data, Buffer.from(' response')]));
});
});
server.listen(sockPath);
}
});