Skip to content
Permalink
Browse files

add test case, fix issuer error

  • Loading branch information...
Sneezry committed Mar 9, 2018
1 parent 6fb284f commit 9a4d68cb61544314573351fa1218e77cea4b63f1
Showing with 328 additions and 3 deletions.
  1. +303 −0 src/test/test.ts
  2. +4 −0 src/ui/entry.ts
  3. +20 −0 test.html
  4. +1 −3 tsconfig.json
@@ -0,0 +1,303 @@
interface TestCase {
name: string;
data: {
/* tslint:disable-next-line:no-any */
[hash: string]: {
/* tslint:disable-next-line:no-any */
[key: string]: any
};
};
}

const cases: TestCase[] = [
{
name: 'Missing fields',
data: {'7733be61632fa6af88d31218e6c4afb2': {'secret': 'abcd2345'}}
},
{
name: 'Bad hash in key',
data: {
'badhash': {
'account': 'test',
'counter': 0,
'encrypted': false,
'hash': '7733be61632fa6af88d31218e6c4afb2',
'index': 0,
'issuer': '',
'secret': 'abcd2345',
'type': 'totp'
}
}
},
{
name: 'Bad hash',
data: {
'badhash': {
'account': 'test',
'counter': 0,
'encrypted': false,
'hash': 'badhash',
'index': 0,
'issuer': '',
'secret': 'abcd2345',
'type': 'totp'
}
}
},
{
name: 'Bad type for HEX',
data: {
'e19d5cd5af0378da05f63f891c7467af': {
'account': 'test',
'counter': 0,
'encrypted': false,
'hash': 'e19d5cd5af0378da05f63f891c7467af',
'index': 0,
'issuer': '',
'secret': 'abcd1234',
'type': 'totp'
}
}
},
{
name: 'Unicode in issuer',
data: {
'7733be61632fa6af88d31218e6c4afb2': {
'account': 'test',
'counter': 0,
'encrypted': false,
'hash': '7733be61632fa6af88d31218e6c4afb2',
'index': 0,
'issuer': '✓ à la mode',
'secret': 'abcd2345',
'type': 'totp'
}
}
},
{
name: 'Battle migrate',
data: {
'95c869de1221960c7f7e6892f78d7062': {
'account': 'test',
'counter': 0,
'encrypted': false,
'hash': '95c869de1221960c7f7e6892f78d7062',
'index': 0,
'issuer': '',
'secret': 'blz-abcd2345',
'type': 'totp'
}
}
},
{
name: 'Steam migrate',
data: {
'95c869de1221960c7f7e6892f78d7062': {
'account': 'test',
'counter': 0,
'encrypted': false,
'hash': '95c869de1221960c7f7e6892f78d7062',
'index': 0,
'issuer': '',
'secret': 'stm-abcd2345',
'type': 'totp'
}
}
},
{
name: 'Missing field with HEX secret',
data: {'e19d5cd5af0378da05f63f891c7467af': {'secret': 'abcd1234'}}
},
{
name: 'Mess index',
data: {
'7733be61632fa6af88d31218e6c4afb2': {
'account': 'test',
'counter': 0,
'encrypted': false,
'hash': '7733be61632fa6af88d31218e6c4afb2',
'index': 6,
'issuer': '',
'secret': 'abcd2345',
'type': 'totp'
},
'770f51f23603ddae810e446630c2f673': {
'account': 'test',
'counter': 0,
'encrypted': false,
'hash': '770f51f23603ddae810e446630c2f673',
'index': 6,
'issuer': '',
'secret': 'abcd2346',
'type': 'totp'
}
}
}
];

let testCaseIndex = 0;
let testRes: Array<{pass: boolean, error: string}> = [];

function testStart() {
if (document.getElementById('lock')) {
const checkbox = document.getElementById('lock') as HTMLInputElement;
if (!checkbox.checked) {
return;
}
}
const startBtn = document.getElementById('start');
if (startBtn) {
startBtn.setAttribute('disabled', 'true');
}
testCaseIndex = 0;
testRes = [];
test();
}

function testFinished() {
clear();
console.log('Test finished.');
for (const res of testRes) {
if (!res.pass) {
alert('Test failed!');
return;
}
}
alert('Test passed!');
return;
}

async function clear() {
return new Promise((resolve: () => void, reject: (reason: Error) => void) => {
try {
chrome.storage.sync.clear(resolve);
} catch (error) {
reject(error);
}
});
}

async function get<T>() {
return new Promise(
(resolve: (items: {[key: string]: T}) => void,
reject: (reason: Error) => void) => {
try {
chrome.storage.sync.get(resolve);
} catch (error) {
reject(error);
}
});
}

async function set(items: {[key: string]: {}}) {
/* tslint:disable-next-line:no-any */
return new Promise((resolve: () => void, reject: (reason: Error) => void) => {
try {
chrome.storage.sync.set(items, resolve);
} catch (error) {
reject(error);
}
});
}

async function test() {
if (testCaseIndex === cases.length * 2) {
testFinished();
return;
}

console.log(
cases[Math.floor(testCaseIndex / 2)].name,
testCaseIndex % 2 ? 'Reopen' : '');

await clear();
if (testCaseIndex % 2 === 0) {
await set(cases[Math.floor(testCaseIndex / 2)].data);
}

if (document.getElementsByTagName('iframe') &&
document.getElementsByTagName('iframe')[0]) {
testRes[testCaseIndex] = {pass: true, error: ''};

document.getElementsByTagName('iframe')[0].src = 'popup.html';
document.getElementsByTagName('iframe')[0].onload = () => {
document.getElementsByTagName('iframe')[0].contentWindow.addEventListener(
'unhandledrejection', event => {
const rejectionEvent = event as PromiseRejectionEvent;
testRes[testCaseIndex] = {
pass: false,
error: rejectionEvent.reason
};
});

document.getElementsByTagName('iframe')[0].contentWindow.onerror =
error => {
testRes[testCaseIndex] = {pass: false, error};
};
};
}

setTimeout(async () => {
if (testRes[testCaseIndex].pass) {
const data = await get<{
/* tslint:disable-next-line:no-any */
[key: string]: any
}>();
for (const hash of Object.keys(data)) {
const item = data[hash];
const keys = [
'issuer', 'account', 'secret', 'hash', 'index', 'type', 'counter',
'encrypted'
];
for (const key of keys) {
if (item[key] === undefined) {
testRes[testCaseIndex] = {
pass: false,
error: `Missing key<${key}>: ${JSON.stringify(item)}`
};
break;
}
}
}
}

showTestResult();
testCaseIndex++;

if (document.getElementsByTagName('iframe') &&
document.getElementsByTagName('iframe')[0]) {
document.getElementsByTagName('iframe')[0].src = 'about:blank';
}

test();
}, 1000);
}

function showTestResult() {
const testResultContainer = document.getElementById('test');
if (!testResultContainer) {
return;
}

testResultContainer.innerHTML = '';
for (let i = 0; i < testRes.length; i++) {
const el = document.createElement('tr');
el.innerHTML = `<td style="vertical-align: text-top; width: 50px; color: ${
testRes[i].pass ? 'green' :
'red'}">[${testRes[i].pass ? 'Pass' : 'Fail'}]</td>`;
el.innerHTML +=
`<td><h3 style="margin: 0">${cases[Math.floor(i / 2)].name}${
i % 2 === 1 ? ' (Reopen)' : ''}</h3>${testRes[i].error}<br></td>`;

testResultContainer.appendChild(el);
}
}

const startBtn = document.getElementById('start');
if (startBtn) {
startBtn.onclick = testStart;
}

window.addEventListener('message', (message) => {
console.log(message);
}, false);
@@ -137,6 +137,10 @@ function hasMatchedEntry(siteName: Array<string|null>, entries: OTPEntry[]) {
}

function isMatchedEntry(siteName: Array<string|null>, entry: OTPEntry) {
if (!entry.issuer) {
return false;
}

const issuerHostMatches = entry.issuer.split('::');
const issuer = issuerHostMatches[0].replace(/[^0-9a-z]/ig, '').toLowerCase();

@@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<style>
iframe {
display: none;
}
</style>
</head>
<body>
<iframe src="about:black" frameborder="0"></iframe>
<button id="start">Test</button>
Test will destroy all data! <input type="checkbox" id="lock">
<hr>
<div>
<table id="test"></table>
</div>
<script src="build/test/test.js"></script>
</body>
</html>
@@ -9,9 +9,7 @@
},
"include": [
"src/*.ts",
"src/**/*.ts",
"test/*.ts",
"test/**/*.ts"
"src/**/*.ts"
],
"exclude": [
"node_modules"

0 comments on commit 9a4d68c

Please sign in to comment.
You can’t perform that action at this time.