2017-01-29 14:31:44 +01:00
|
|
|
'use strict';
|
2017-02-05 16:45:11 +01:00
|
|
|
var jsc = require('jsverify'),
|
|
|
|
jsdom = require('jsdom-global'),
|
2017-02-05 16:58:58 +01:00
|
|
|
cleanup = jsdom(),
|
2017-03-26 11:34:19 +02:00
|
|
|
base64lib = require('./base64-2.1.9'),
|
|
|
|
rawdeflatelib = require('./rawdeflate-0.5'),
|
|
|
|
rawinflatelib = require('./rawinflate-0.3'),
|
2017-02-05 16:58:58 +01:00
|
|
|
|
|
|
|
a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
|
|
|
|
'n','o','p','q','r','s','t','u','v','w','x','y','z'],
|
|
|
|
alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
|
2017-02-05 18:03:42 +01:00
|
|
|
queryString = alnumString.concat(['+','%','&','.','*','-','_']),
|
|
|
|
base64String = alnumString.concat(['+','/','=']).concat(
|
|
|
|
a2zString.map(function(c) {
|
|
|
|
return c.toUpperCase();
|
|
|
|
})
|
2017-03-06 19:48:07 +01:00
|
|
|
),
|
|
|
|
// schemas supported by the whatwg-url library
|
2017-03-26 09:24:42 +02:00
|
|
|
schemas = ['ftp','gopher','http','https','ws','wss'],
|
|
|
|
supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
|
|
|
|
logFile = require('fs').createWriteStream('test.log');
|
2017-02-05 16:58:58 +01:00
|
|
|
|
2017-02-05 16:45:11 +01:00
|
|
|
global.$ = global.jQuery = require('./jquery-3.1.1');
|
|
|
|
global.sjcl = require('./sjcl-1.0.6');
|
2017-03-26 11:34:19 +02:00
|
|
|
global.Base64 = base64lib.Base64;
|
|
|
|
global.RawDeflate = rawdeflatelib.RawDeflate;
|
|
|
|
global.RawDeflate.inflate = rawinflatelib.RawDeflate.inflate;
|
2017-02-05 16:45:11 +01:00
|
|
|
require('./privatebin');
|
2017-01-29 14:31:44 +01:00
|
|
|
|
2017-03-26 09:24:42 +02:00
|
|
|
// redirect console messages to log file
|
|
|
|
console.warn = console.error = function (msg) {
|
|
|
|
logFile.write(msg + '\n');
|
|
|
|
}
|
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('Helper', function () {
|
2017-01-29 14:31:44 +01:00
|
|
|
describe('secondsToHuman', function () {
|
2017-02-05 16:45:11 +01:00
|
|
|
after(function () {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
2017-01-29 14:31:44 +01:00
|
|
|
jsc.property('returns an array with a number and a word', 'integer', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
var result = $.PrivateBin.Helper.secondsToHuman(number);
|
2017-01-29 14:31:44 +01:00
|
|
|
return Array.isArray(result) &&
|
|
|
|
result.length === 2 &&
|
|
|
|
result[0] === parseInt(result[0], 10) &&
|
|
|
|
typeof result[1] === 'string';
|
|
|
|
});
|
|
|
|
jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
// max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
|
|
|
|
jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
});
|
2017-02-05 16:45:11 +01:00
|
|
|
|
2017-02-11 09:09:47 +01:00
|
|
|
// this test is not yet meaningful using jsdom, as it does not contain getSelection support.
|
|
|
|
// TODO: This needs to be tested using a browser.
|
|
|
|
describe('selectText', function () {
|
|
|
|
jsc.property(
|
|
|
|
'selection contains content of given ID',
|
2017-02-11 09:56:56 +01:00
|
|
|
jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
|
2017-02-11 09:09:47 +01:00
|
|
|
'nearray string',
|
|
|
|
function (ids, contents) {
|
|
|
|
var html = '',
|
|
|
|
result = true;
|
|
|
|
ids.forEach(function(item, i) {
|
2017-03-25 09:06:04 +01:00
|
|
|
html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
|
2017-02-11 09:09:47 +01:00
|
|
|
});
|
|
|
|
var clean = jsdom(html);
|
|
|
|
ids.forEach(function(item, i) {
|
2017-03-25 09:06:04 +01:00
|
|
|
$.PrivateBin.Helper.selectText(item.join(''));
|
2017-02-11 09:09:47 +01:00
|
|
|
// TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
|
|
|
|
// Once there is one, uncomment the line below to actually check the result.
|
|
|
|
//result *= (contents[i] || contents[0]) === window.getSelection().toString();
|
|
|
|
});
|
|
|
|
clean();
|
2017-02-11 09:56:56 +01:00
|
|
|
return Boolean(result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setElementText', function () {
|
2017-02-11 19:34:51 +01:00
|
|
|
after(function () {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
2017-02-11 09:56:56 +01:00
|
|
|
jsc.property(
|
|
|
|
'replaces the content of an element',
|
|
|
|
jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
|
|
|
|
'nearray string',
|
|
|
|
'string',
|
|
|
|
function (ids, contents, replacingContent) {
|
|
|
|
var html = '',
|
|
|
|
result = true;
|
|
|
|
ids.forEach(function(item, i) {
|
2017-03-25 09:06:04 +01:00
|
|
|
html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
|
2017-02-11 09:56:56 +01:00
|
|
|
});
|
2017-02-11 19:34:51 +01:00
|
|
|
var elements = $('<body />').html(html);
|
2017-02-11 09:56:56 +01:00
|
|
|
ids.forEach(function(item, i) {
|
|
|
|
var id = item.join(''),
|
2017-02-11 19:34:51 +01:00
|
|
|
element = elements.find('#' + id).first();
|
2017-03-25 09:06:04 +01:00
|
|
|
$.PrivateBin.Helper.setElementText(element, replacingContent);
|
2017-02-11 19:34:51 +01:00
|
|
|
result *= replacingContent === element.text();
|
2017-02-11 09:56:56 +01:00
|
|
|
});
|
|
|
|
return Boolean(result);
|
2017-02-11 09:09:47 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-02-11 16:02:24 +01:00
|
|
|
describe('urls2links', function () {
|
2017-02-11 19:34:51 +01:00
|
|
|
after(function () {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
2017-02-11 16:02:24 +01:00
|
|
|
jsc.property(
|
|
|
|
'ignores non-URL content',
|
|
|
|
'string',
|
|
|
|
function (content) {
|
|
|
|
var element = $('<div>' + content + '</div>'),
|
|
|
|
before = element.html();
|
2017-03-25 09:06:04 +01:00
|
|
|
$.PrivateBin.Helper.urls2links(element);
|
2017-02-11 16:02:24 +01:00
|
|
|
return before === element.html();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces URLs with anchors',
|
|
|
|
'string',
|
|
|
|
jsc.elements(['http', 'https', 'ftp']),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
|
|
|
'string',
|
|
|
|
function (prefix, schema, address, query, fragment, postfix) {
|
|
|
|
var query = query.join(''),
|
|
|
|
fragment = fragment.join(''),
|
|
|
|
url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
|
2017-03-25 09:06:04 +01:00
|
|
|
prefix = $.PrivateBin.Helper.htmlEntities(prefix),
|
|
|
|
postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix),
|
2017-02-11 16:02:24 +01:00
|
|
|
element = $('<div>' + prefix + url + postfix + '</div>');
|
|
|
|
|
|
|
|
// special cases: When the query string and fragment imply the beginning of an HTML entity, eg. � or &#x
|
|
|
|
if (
|
|
|
|
query.slice(-1) === '&' &&
|
|
|
|
(parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
|
|
|
|
postfix = '';
|
|
|
|
element = $('<div>' + prefix + url + '</div>');
|
|
|
|
}
|
|
|
|
|
2017-03-25 09:06:04 +01:00
|
|
|
$.PrivateBin.Helper.urls2links(element);
|
2017-02-11 16:02:24 +01:00
|
|
|
return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix + '</div>').html();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces magnet links with anchors',
|
|
|
|
'string',
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
|
|
|
'string',
|
|
|
|
function (prefix, query, postfix) {
|
|
|
|
var url = 'magnet:?' + query.join(''),
|
2017-03-25 09:06:04 +01:00
|
|
|
prefix = $.PrivateBin.Helper.htmlEntities(prefix),
|
|
|
|
postfix = $.PrivateBin.Helper.htmlEntities(postfix),
|
2017-02-11 16:02:24 +01:00
|
|
|
element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
|
2017-03-25 09:06:04 +01:00
|
|
|
$.PrivateBin.Helper.urls2links(element);
|
2017-02-11 16:02:24 +01:00
|
|
|
return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-02-11 19:34:51 +01:00
|
|
|
describe('sprintf', function () {
|
|
|
|
after(function () {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'replaces %s in strings with first given parameter',
|
|
|
|
'string',
|
|
|
|
'(small nearray) string',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
2017-03-25 10:47:12 +01:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
params[0] = params[0].replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var result = prefix + params[0] + postfix;
|
2017-02-11 19:34:51 +01:00
|
|
|
params.unshift(prefix + '%s' + postfix);
|
2017-03-25 09:06:04 +01:00
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
2017-02-11 19:34:51 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %d in strings with first given parameter',
|
|
|
|
'string',
|
|
|
|
'(small nearray) nat',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
2017-03-25 10:47:12 +01:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var result = prefix + params[0] + postfix;
|
2017-02-11 19:34:51 +01:00
|
|
|
params.unshift(prefix + '%d' + postfix);
|
2017-03-25 09:06:04 +01:00
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
2017-02-11 19:34:51 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %d in strings with 0 if first parameter is not a number',
|
|
|
|
'string',
|
|
|
|
'(small nearray) falsy',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
2017-03-25 10:47:12 +01:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var result = prefix + '0' + postfix;
|
2017-02-11 19:34:51 +01:00
|
|
|
params.unshift(prefix + '%d' + postfix);
|
2017-03-25 09:06:04 +01:00
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params)
|
2017-02-11 19:34:51 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %d and %s in strings in order',
|
|
|
|
'string',
|
|
|
|
'nat',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
function (prefix, uint, middle, string, postfix) {
|
2017-03-25 10:47:12 +01:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
middle = middle.replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
|
2017-02-11 19:34:51 +01:00
|
|
|
result = prefix + uint + middle + string + postfix;
|
2017-03-25 09:06:04 +01:00
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
2017-02-11 19:34:51 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %d and %s in strings in reverse order',
|
|
|
|
'string',
|
|
|
|
'nat',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
function (prefix, uint, middle, string, postfix) {
|
2017-03-25 10:47:12 +01:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
middle = middle.replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
|
2017-02-11 19:34:51 +01:00
|
|
|
result = prefix + string + middle + uint + postfix;
|
2017-03-25 09:06:04 +01:00
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
2017-02-11 19:34:51 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-02-12 17:11:21 +01:00
|
|
|
describe('getCookie', function () {
|
|
|
|
jsc.property(
|
|
|
|
'returns the requested cookie',
|
|
|
|
'nearray asciinestring',
|
|
|
|
'nearray asciistring',
|
|
|
|
function (labels, values) {
|
|
|
|
var selectedKey = '', selectedValue = '',
|
|
|
|
cookieArray = [],
|
|
|
|
count = 0;
|
|
|
|
labels.forEach(function(item, i) {
|
2017-03-25 09:41:24 +01:00
|
|
|
// deliberatly using a non-ascii key for replacing invalid characters
|
2017-03-25 10:18:28 +01:00
|
|
|
var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
|
2017-02-12 17:11:21 +01:00
|
|
|
value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
|
|
|
|
cookieArray.push(key + '=' + value);
|
2017-03-26 06:47:57 +02:00
|
|
|
if (Math.random() < 1 / i || selectedKey === key)
|
2017-02-12 17:11:21 +01:00
|
|
|
{
|
|
|
|
selectedKey = key;
|
|
|
|
selectedValue = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var clean = jsdom('', {cookie: cookieArray}),
|
2017-03-25 09:06:04 +01:00
|
|
|
result = $.PrivateBin.Helper.getCookie(selectedKey);
|
2017-02-12 17:11:21 +01:00
|
|
|
clean();
|
|
|
|
return result === selectedValue;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('baseUri', function () {
|
2017-03-05 12:11:55 +01:00
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.Helper.reset();
|
|
|
|
});
|
|
|
|
|
2017-02-05 16:45:11 +01:00
|
|
|
jsc.property(
|
|
|
|
'returns the URL without query & fragment',
|
2017-03-06 19:48:07 +01:00
|
|
|
jsc.elements(schemas),
|
2017-02-05 16:45:11 +01:00
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
|
|
|
'string',
|
|
|
|
function (schema, address, query, fragment) {
|
2017-03-06 19:48:07 +01:00
|
|
|
var expected = schema + '://' + address.join('') + '/',
|
2017-02-05 16:45:11 +01:00
|
|
|
clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
|
2017-02-25 09:35:55 +01:00
|
|
|
result = $.PrivateBin.Helper.baseUri();
|
2017-03-05 12:11:55 +01:00
|
|
|
$.PrivateBin.Helper.reset();
|
2017-02-05 16:45:11 +01:00
|
|
|
clean();
|
|
|
|
return expected === result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-02-05 16:58:58 +01:00
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('htmlEntities', function () {
|
|
|
|
after(function () {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'removes all HTML entities from any given string',
|
|
|
|
'string',
|
|
|
|
function (string) {
|
|
|
|
var result = $.PrivateBin.Helper.htmlEntities(string);
|
|
|
|
return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&/.test(result)));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-25 09:41:24 +01:00
|
|
|
describe('I18n', function () {
|
|
|
|
describe('translate', function () {
|
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'returns message ID unchanged if no translation found',
|
|
|
|
'string',
|
|
|
|
function (messageId) {
|
2017-03-26 09:24:42 +02:00
|
|
|
messageId = messageId.replace(/%(s|d)/g, '%%');
|
|
|
|
var plurals = [messageId, messageId + 's'],
|
|
|
|
fake = [messageId],
|
|
|
|
result = $.PrivateBin.I18n.translate(messageId);
|
2017-03-25 09:41:24 +01:00
|
|
|
$.PrivateBin.I18n.reset();
|
2017-03-26 09:24:42 +02:00
|
|
|
|
2017-03-25 09:41:24 +01:00
|
|
|
var alias = $.PrivateBin.I18n._(messageId);
|
|
|
|
$.PrivateBin.I18n.reset();
|
2017-03-26 09:24:42 +02:00
|
|
|
|
|
|
|
var p_result = $.PrivateBin.I18n.translate(plurals);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
var p_alias = $.PrivateBin.I18n._(plurals);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
var f_result = $.PrivateBin.I18n.translate(fake);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
var f_alias = $.PrivateBin.I18n._(fake);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
return messageId === result && messageId === alias &&
|
|
|
|
messageId === p_result && messageId === p_alias &&
|
|
|
|
messageId === f_result && messageId === f_alias;
|
2017-03-25 10:18:28 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %s in strings with first given parameter',
|
|
|
|
'string',
|
|
|
|
'(small nearray) string',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
2017-03-25 10:47:12 +01:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
params[0] = params[0].replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var translation = prefix + params[0] + postfix;
|
2017-03-25 10:18:28 +01:00
|
|
|
params.unshift(prefix + '%s' + postfix);
|
|
|
|
var result = $.PrivateBin.I18n.translate.apply(this, params);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
var alias = $.PrivateBin.I18n._.apply(this, params);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
return translation === result && translation === alias;
|
2017-03-25 09:41:24 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-03-26 09:24:42 +02:00
|
|
|
|
|
|
|
describe('getPluralForm', function () {
|
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'returns valid key for plural form',
|
|
|
|
jsc.elements(supportedLanguages),
|
|
|
|
'integer',
|
|
|
|
function(language, n) {
|
|
|
|
$.PrivateBin.I18n.reset(language);
|
|
|
|
var result = $.PrivateBin.I18n.getPluralForm(n);
|
|
|
|
// arabic seems to have the highest plural count with 6 forms
|
|
|
|
return result >= 0 && result <= 5;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// loading of JSON via AJAX needs to be tested in the browser, this just mocks it
|
|
|
|
// TODO: This needs to be tested using a browser.
|
|
|
|
describe('loadTranslations', function () {
|
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'downloads and handles any supported language',
|
|
|
|
jsc.elements(supportedLanguages),
|
|
|
|
function(language) {
|
|
|
|
var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
|
|
|
|
|
|
|
|
$.PrivateBin.I18n.reset('en');
|
|
|
|
$.PrivateBin.I18n.loadTranslations();
|
|
|
|
$.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
|
|
|
|
var result = $.PrivateBin.I18n.translate('en'),
|
|
|
|
alias = $.PrivateBin.I18n._('en');
|
|
|
|
|
|
|
|
clean();
|
|
|
|
return language === result && language === alias;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-03-25 09:41:24 +01:00
|
|
|
});
|
|
|
|
|
2017-03-26 11:34:19 +02:00
|
|
|
describe('CryptTool', function () {
|
|
|
|
describe('cipher & decipher', function () {
|
|
|
|
this.timeout(20000);
|
|
|
|
it('can en- and decrypt any message', function () {
|
|
|
|
jsc.check(jsc.forall(
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
function (key, password, message) {
|
|
|
|
return message === $.PrivateBin.CryptTool.decipher(
|
|
|
|
key,
|
|
|
|
password,
|
|
|
|
$.PrivateBin.CryptTool.cipher(key, password, message)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
),
|
|
|
|
// reducing amount of checks as running 100 takes about 5 minutes
|
|
|
|
{tests: 5, quiet: true});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('Model', function () {
|
|
|
|
describe('getPasteId', function () {
|
2017-03-05 12:11:55 +01:00
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
});
|
|
|
|
|
2017-02-05 16:58:58 +01:00
|
|
|
jsc.property(
|
|
|
|
'returns the query string without separator, if any',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
2017-02-25 09:35:55 +01:00
|
|
|
jsc.nearray(jsc.elements(queryString)),
|
2017-02-05 16:58:58 +01:00
|
|
|
'string',
|
|
|
|
function (schema, address, query, fragment) {
|
2017-02-05 21:22:09 +01:00
|
|
|
var queryString = query.join(''),
|
2017-02-05 16:58:58 +01:00
|
|
|
clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
2017-02-05 21:22:09 +01:00
|
|
|
'/?' + queryString + '#' + fragment
|
2017-02-05 16:58:58 +01:00
|
|
|
}),
|
2017-02-25 09:35:55 +01:00
|
|
|
result = $.PrivateBin.Model.getPasteId();
|
|
|
|
$.PrivateBin.Model.reset();
|
2017-02-05 16:58:58 +01:00
|
|
|
clean();
|
2017-02-05 21:22:09 +01:00
|
|
|
return queryString === result;
|
2017-02-05 16:58:58 +01:00
|
|
|
}
|
|
|
|
);
|
2017-03-25 10:18:28 +01:00
|
|
|
jsc.property(
|
|
|
|
'throws exception on empty query string',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
'string',
|
|
|
|
function (schema, address, fragment) {
|
|
|
|
var clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
|
|
|
'/#' + fragment
|
|
|
|
}),
|
|
|
|
result = false;
|
|
|
|
try {
|
|
|
|
$.PrivateBin.Model.getPasteId();
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
);
|
2017-02-05 16:58:58 +01:00
|
|
|
});
|
2017-02-05 18:03:42 +01:00
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('getPasteKey', function () {
|
2017-02-05 18:03:42 +01:00
|
|
|
jsc.property(
|
|
|
|
'returns the fragment of the URL',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
2017-02-25 09:35:55 +01:00
|
|
|
jsc.nearray(jsc.elements(base64String)),
|
2017-02-05 18:03:42 +01:00
|
|
|
function (schema, address, query, fragment) {
|
2017-02-05 21:22:09 +01:00
|
|
|
var fragmentString = fragment.join(''),
|
2017-02-05 18:03:42 +01:00
|
|
|
clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
2017-02-05 21:22:09 +01:00
|
|
|
'/?' + query.join('') + '#' + fragmentString
|
2017-02-05 18:03:42 +01:00
|
|
|
}),
|
2017-02-25 09:35:55 +01:00
|
|
|
result = $.PrivateBin.Model.getPasteKey();
|
|
|
|
$.PrivateBin.Model.reset();
|
2017-02-05 18:03:42 +01:00
|
|
|
clean();
|
2017-02-05 21:22:09 +01:00
|
|
|
return fragmentString === result;
|
2017-02-05 18:03:42 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'returns the fragment stripped of trailing query parts',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
2017-02-25 09:35:55 +01:00
|
|
|
jsc.nearray(jsc.elements(base64String)),
|
2017-02-05 18:03:42 +01:00
|
|
|
jsc.array(jsc.elements(queryString)),
|
|
|
|
function (schema, address, query, fragment, trail) {
|
2017-02-05 21:22:09 +01:00
|
|
|
var fragmentString = fragment.join(''),
|
2017-02-05 18:03:42 +01:00
|
|
|
clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') + '/?' +
|
2017-02-05 21:22:09 +01:00
|
|
|
query.join('') + '#' + fragmentString + '&' + trail.join('')
|
2017-02-05 18:03:42 +01:00
|
|
|
}),
|
2017-02-25 09:35:55 +01:00
|
|
|
result = $.PrivateBin.Model.getPasteKey();
|
|
|
|
$.PrivateBin.Model.reset();
|
2017-02-05 18:03:42 +01:00
|
|
|
clean();
|
2017-02-05 21:22:09 +01:00
|
|
|
return fragmentString === result;
|
2017-02-05 18:03:42 +01:00
|
|
|
}
|
|
|
|
);
|
2017-03-25 10:18:28 +01:00
|
|
|
jsc.property(
|
|
|
|
'throws exception on empty fragment of the URL',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
|
|
|
function (schema, address, query) {
|
|
|
|
var clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
|
|
|
'/?' + query.join('')
|
|
|
|
}),
|
|
|
|
result = false;
|
|
|
|
try {
|
|
|
|
$.PrivateBin.Model.getPasteKey();
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
);
|
2017-02-05 18:03:42 +01:00
|
|
|
});
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|