Test scripts (#57)

Added test scripts for user challenge tests.
This commit is contained in:
Ravin Perera
2019-11-13 10:54:10 +05:30
committed by GitHub
parent 198fe16359
commit e742034e4e
6 changed files with 189 additions and 0 deletions

1
test/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules/**

51
test/global.js Normal file
View File

@@ -0,0 +1,51 @@
const ws_api = require('ws');
module.exports = {
init: function(endpoint) {
wsurl = endpoint;
},
testcontext: function (methodname) {
let ws = new ws_api(wsurl, { rejectUnauthorized: false });
let timer = setTimeout(() => {
ws.removeAllListeners();
ws.close();
fail(methodname, "Timeout");
}, 1000);
let ctx = {
ws,
pass: function (message) {
clearTimeout(timer);
ws.removeAllListeners();
ws.close();
pass(methodname, message);
},
fail: function (message) {
clearTimeout(timer);
ws.removeAllListeners();
ws.close();
fail(methodname, message);
}
}
ws.onerror = function () {
ctx.fail('Connection error');
};
return ctx;
}
}
let wsurl = '';
function pass(methodname, message) {
let log = "PASS: " + methodname + (message ? ": " + message : "");
console.log('\x1b[32m%s\x1b[0m', log)
}
function fail(methodname, message) {
let log = "FAIL: " + methodname + (message ? ": " + message : "");
console.log('\x1b[31m%s\x1b[0m', log);
}

32
test/package-lock.json generated Normal file
View File

@@ -0,0 +1,32 @@
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"async-limiter": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz",
"integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ=="
},
"libsodium": {
"version": "0.7.5",
"resolved": "https://registry.npmjs.org/libsodium/-/libsodium-0.7.5.tgz",
"integrity": "sha512-0YVU2QJc5sDR5HHkGCaliYImS7pGeXi11fiOfm4DirBd96PJVZIn3LJa06ZOFjLNsWkL3UbNjYhLRUOABPL9vw=="
},
"libsodium-wrappers": {
"version": "0.7.5",
"resolved": "https://registry.npmjs.org/libsodium-wrappers/-/libsodium-wrappers-0.7.5.tgz",
"integrity": "sha512-QE9Q+FxLLGdJRiJTuC2GB3LEHZeHX/VcbMQeNPdAixEKo86JPy6bOWND1XmMLu0tjWUu0xIY0YpJYQApxIZwbQ==",
"requires": {
"libsodium": "0.7.5"
}
},
"ws": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.1.2.tgz",
"integrity": "sha512-gftXq3XI81cJCgkUiAVixA0raD9IVmXqsylCrjRygw4+UOOGzPoxnQ6r/CnVL9i+mDncJo94tSkyrtuuQVBmrg==",
"requires": {
"async-limiter": "^1.0.0"
}
}
}
}

6
test/package.json Normal file
View File

@@ -0,0 +1,6 @@
{
"dependencies": {
"libsodium-wrappers": "0.7.5",
"ws": "7.1.2"
}
}

9
test/test.js Normal file
View File

@@ -0,0 +1,9 @@
const global = require('./global')
const challenge_tests = require('./test_challenge')
function runtests() {
challenge_tests.run();
}
global.init('wss://localhost:8081');
runtests();

90
test/test_challenge.js Normal file
View File

@@ -0,0 +1,90 @@
const sodium = require('libsodium-wrappers')
const global = require('./global')
module.exports = {
run: function () {
sodium.ready.then(() =>
testmethods.forEach(f => f()));
}
}
let testmethods = [
() => {
let ctx = global.testcontext("challenge: format_is_correct");
ctx.ws.on('message', (m) => {
let obj = {};
try {
obj = JSON.parse(m);
} catch {
ctx.fail('JSON parse failed');
return;
}
if (obj.version && obj.type == 'public_challenge' && obj.challenge)
ctx.pass();
else
ctx.fail('Invalid fields');
});
},
() => {
let ctx = global.testcontext("challenge: accepts_connection_for_valid_signature");
ctx.ws.on('message', (m) => {
let obj = JSON.parse(m);
var keys = sodium.crypto_sign_keypair();
// sign the challenge and send back the response
var sigbytes = sodium.crypto_sign_detached(obj.challenge, keys.privateKey);
ctx.ws.send(JSON.stringify({
type: 'challenge_resp',
challenge: obj.challenge,
sig: Buffer.from(sigbytes).toString('hex'),
pubkey: 'ed' + Buffer.from(keys.publicKey).toString('hex')
}));
let timer = setTimeout(() => {
// Wait 500ms and pass. That means we haven't been disconnected thus far.
ctx.pass();
}, 500);
ctx.ws.on('close', () => {
// Clear the pass timer and fail test if we get disconnected.
clearTimeout(timer);
ctx.fail('Got disconnected');
});
});
},
() => {
let ctx = global.testcontext("challenge: rejects_connection_for_empty_response");
ctx.ws.on('message', (m) => {
ctx.ws.send(JSON.stringify({}))
ctx.ws.on('close', () => {
ctx.pass();
})
});
},
() => {
let ctx = global.testcontext("challenge: rejects_connection_for_invalid_type");
ctx.ws.on('message', (m) => {
ctx.ws.send(JSON.stringify({ type: 'dummy_type' }))
ctx.ws.on('close', () => {
ctx.pass();
})
});
}
]