mirror of https://github.com/keeweb/keeweb.git
native host tests
This commit is contained in:
parent
ef6e5ebcbe
commit
1e6034ce37
|
@ -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
|
||||
|
|
|
@ -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);
|
|
@ -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);
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue