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 = ''; mimeLine = '';
// redirect console messages to log file // 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'); logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
}; };

View File

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

View File

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