fix unit tests after merge from master, issues due to newly async tests that cause environment changes across test scripts

This commit is contained in:
El RIDO 2018-09-02 11:33:27 +02:00
parent b191e2c437
commit b791157717
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
3 changed files with 20 additions and 12 deletions

View File

@ -56,7 +56,7 @@ var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
mimeLine = '';
// redirect console messages to log file
console.info = console.warn = function () {
console.info = console.warn = console.error = function () {
logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
};

View File

@ -1,7 +1,5 @@
'use strict';
require('../common');
jsdom();
window.crypto = new WebCrypto();
describe('CryptTool', function () {
describe('cipher & decipher', function () {
@ -12,6 +10,8 @@ describe('CryptTool', function () {
'string',
'string',
function (key, password, message) {
var clean = jsdom();
window.crypto = new WebCrypto();
message = message.trim();
return $.PrivateBin.CryptTool.cipher(
key, password, message
@ -19,7 +19,7 @@ describe('CryptTool', function () {
return $.PrivateBin.CryptTool.decipher(
key, password, ciphertext
).then(function(plaintext) {
if (message !== plaintext) console.log([message, plaintext]);
clean();
return message === plaintext;
});
});
@ -33,8 +33,8 @@ describe('CryptTool', function () {
'supports PrivateBin v1 ciphertext (SJCL & browser atob)',
async function () {
delete global.Base64;
// make btoa available
global.btoa = window.btoa;
var clean = jsdom();
window.crypto = new WebCrypto();
// Of course you can easily decipher the following texts, if you like.
// Bonus points for finding their sources and hidden meanings.
@ -96,6 +96,7 @@ describe('CryptTool', function () {
'99gjHX7wphJ6umKMM+fn6PcbYJkhDh2GlJL5COXjXfm/5aj/vuyaRRWZ' +
'MZtmnYpGAtAPg7AUG"}'
);
clean();
assert.ok(
paste1.includes('securely packed in iron') &&
@ -108,6 +109,8 @@ describe('CryptTool', function () {
'supports ZeroBin ciphertext (SJCL & Base64 1.7)',
async function () {
global.Base64 = require('../base64-1.7').Base64;
var clean = jsdom();
window.crypto = new WebCrypto();
// Of course you can easily decipher the following texts, if you like.
// Bonus points for finding their sources and hidden meanings.
@ -154,6 +157,7 @@ describe('CryptTool', function () {
'7NnhqVk5A6vIBbu4AC5PZf76l6yep4xsoy/QtdDxCMocCXeAML9MQ9uP' +
'QbuspOKrBvMfN5igA1kBqasnxI472KBNXsdZnaDddSVUuvhTcETM="}'
);
clean();
delete global.Base64;
assert.ok(
@ -165,6 +169,7 @@ describe('CryptTool', function () {
});
describe('getSymmetricKey', function () {
this.timeout(30000);
var keys = [];
// the parameter is used to ensure the test is run more then one time
@ -172,25 +177,31 @@ describe('CryptTool', function () {
'returns random, non-empty keys',
'integer',
function(counter) {
var clean = jsdom();
window.crypto = new WebCrypto();
var key = $.PrivateBin.CryptTool.getSymmetricKey(),
result = (key !== '' && keys.indexOf(key) === -1);
keys.push(key);
clean();
return result;
}
);
});
describe('SJCL.js vs abab.js', function () {
this.timeout(30000);
jsc.property(
'these all return the same base64 string',
'string',
function(string) {
var clean = jsdom();
// not comparing Base64.js v1.7 encode/decode, that has known issues
var Base64 = require('../base64-1.7').Base64,
sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)),
abab = window.btoa(Base64.utob(string)),
lcjs = global.sjcl.codec.utf8String.fromBits(global.sjcl.codec.base64.toBits(abab)),
baba = Base64.btou(window.atob(sjcl));
clean();
return sjcl === abab && string === lcjs && lcjs === baba;
}
);

View File

@ -230,6 +230,7 @@ describe('Helper', function () {
});
var clean = jsdom('', {cookie: cookieArray}),
result = $.PrivateBin.Helper.getCookie(selectedKey);
$.PrivateBin.Helper.reset();
clean();
return result === selectedValue;
}
@ -238,21 +239,17 @@ describe('Helper', function () {
describe('baseUri', function () {
this.timeout(30000);
before(function () {
$.PrivateBin.Helper.reset();
});
jsc.property(
'returns the URL without query & fragment',
common.jscSchemas(),
jsc.elements(['http', 'https']),
jsc.nearray(common.jscA2zString()),
jsc.array(common.jscQueryString()),
'string',
function (schema, address, query, fragment) {
$.PrivateBin.Helper.reset();
var expected = schema + '://' + address.join('') + '/',
clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
result = $.PrivateBin.Helper.baseUri();
$.PrivateBin.Helper.reset();
clean();
return expected === result;
}