| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- 'use strict';
- var jsc = require('jsverify'),
- jsdom = require('jsdom-global'),
- cleanup = jsdom(),
- 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']),
- queryString = alnumString.concat(['+','%','&','.','*','-','_']),
- base64String = alnumString.concat(['+','/','=']).concat(
- a2zString.map(function(c) {
- return c.toUpperCase();
- })
- ),
- // schemas supported by the whatwg-url library
- 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');
- global.$ = global.jQuery = require('./jquery-3.1.1');
- global.sjcl = require('./sjcl-1.0.6');
- global.Base64 = require('./base64-2.1.9');
- global.RawDeflate = require('./rawdeflate-0.5');
- require('./rawinflate-0.3');
- require('./privatebin');
- // redirect console messages to log file
- console.warn = console.error = function (msg) {
- logFile.write(msg + '\n');
- }
- describe('Helper', function () {
- describe('secondsToHuman', function () {
- after(function () {
- cleanup();
- });
- jsc.property('returns an array with a number and a word', 'integer', function (number) {
- var result = $.PrivateBin.Helper.secondsToHuman(number);
- 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) {
- return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
- });
- jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
- return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
- });
- jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
- return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
- });
- jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
- return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
- });
- jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
- return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
- });
- jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
- return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
- });
- jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
- return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
- });
- jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
- return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
- });
- // 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) {
- return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
- });
- jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
- return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
- });
- });
- // 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',
- jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
- 'nearray string',
- function (ids, contents) {
- var html = '',
- result = true;
- ids.forEach(function(item, i) {
- html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
- });
- var clean = jsdom(html);
- ids.forEach(function(item, i) {
- $.PrivateBin.Helper.selectText(item.join(''));
- // 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();
- return Boolean(result);
- }
- );
- });
- describe('setElementText', function () {
- after(function () {
- cleanup();
- });
- 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) {
- html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
- });
- var elements = $('<body />').html(html);
- ids.forEach(function(item, i) {
- var id = item.join(''),
- element = elements.find('#' + id).first();
- $.PrivateBin.Helper.setElementText(element, replacingContent);
- result *= replacingContent === element.text();
- });
- return Boolean(result);
- }
- );
- });
- describe('urls2links', function () {
- after(function () {
- cleanup();
- });
- jsc.property(
- 'ignores non-URL content',
- 'string',
- function (content) {
- var element = $('<div>' + content + '</div>'),
- before = element.html();
- $.PrivateBin.Helper.urls2links(element);
- 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,
- prefix = $.PrivateBin.Helper.htmlEntities(prefix),
- postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix),
- 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>');
- }
- $.PrivateBin.Helper.urls2links(element);
- 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(''),
- prefix = $.PrivateBin.Helper.htmlEntities(prefix),
- postfix = $.PrivateBin.Helper.htmlEntities(postfix),
- element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
- $.PrivateBin.Helper.urls2links(element);
- return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
- }
- );
- });
- 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) {
- 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;
- params.unshift(prefix + '%s' + postfix);
- return result === $.PrivateBin.Helper.sprintf.apply(this, params);
- }
- );
- jsc.property(
- 'replaces %d in strings with first given parameter',
- 'string',
- '(small nearray) nat',
- 'string',
- function (prefix, params, postfix) {
- prefix = prefix.replace(/%(s|d)/g, '%%');
- postfix = postfix.replace(/%(s|d)/g, '%%');
- var result = prefix + params[0] + postfix;
- params.unshift(prefix + '%d' + postfix);
- return result === $.PrivateBin.Helper.sprintf.apply(this, params);
- }
- );
- jsc.property(
- 'replaces %d in strings with 0 if first parameter is not a number',
- 'string',
- '(small nearray) falsy',
- 'string',
- function (prefix, params, postfix) {
- prefix = prefix.replace(/%(s|d)/g, '%%');
- postfix = postfix.replace(/%(s|d)/g, '%%');
- var result = prefix + '0' + postfix;
- params.unshift(prefix + '%d' + postfix);
- return result === $.PrivateBin.Helper.sprintf.apply(this, params)
- }
- );
- jsc.property(
- 'replaces %d and %s in strings in order',
- 'string',
- 'nat',
- 'string',
- 'string',
- 'string',
- function (prefix, uint, middle, string, postfix) {
- 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],
- result = prefix + uint + middle + string + postfix;
- return result === $.PrivateBin.Helper.sprintf.apply(this, params);
- }
- );
- jsc.property(
- 'replaces %d and %s in strings in reverse order',
- 'string',
- 'nat',
- 'string',
- 'string',
- 'string',
- function (prefix, uint, middle, string, postfix) {
- 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],
- result = prefix + string + middle + uint + postfix;
- return result === $.PrivateBin.Helper.sprintf.apply(this, params);
- }
- );
- });
- 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) {
- // deliberatly using a non-ascii key for replacing invalid characters
- var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
- value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
- cookieArray.push(key + '=' + value);
- if (Math.random() < 1 / i || selectedKey === key)
- {
- selectedKey = key;
- selectedValue = value;
- }
- });
- var clean = jsdom('', {cookie: cookieArray}),
- result = $.PrivateBin.Helper.getCookie(selectedKey);
- clean();
- return result === selectedValue;
- }
- );
- });
- describe('baseUri', function () {
- before(function () {
- $.PrivateBin.Helper.reset();
- });
- jsc.property(
- 'returns the URL without query & fragment',
- jsc.elements(schemas),
- jsc.nearray(jsc.elements(a2zString)),
- jsc.array(jsc.elements(queryString)),
- 'string',
- function (schema, address, query, fragment) {
- var expected = schema + '://' + address.join('') + '/',
- clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
- result = $.PrivateBin.Helper.baseUri();
- $.PrivateBin.Helper.reset();
- clean();
- return expected === result;
- }
- );
- });
- 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)));
- }
- );
- });
- });
- describe('I18n', function () {
- describe('translate', function () {
- before(function () {
- $.PrivateBin.I18n.reset();
- });
- jsc.property(
- 'returns message ID unchanged if no translation found',
- 'string',
- function (messageId) {
- messageId = messageId.replace(/%(s|d)/g, '%%');
- var plurals = [messageId, messageId + 's'],
- fake = [messageId],
- result = $.PrivateBin.I18n.translate(messageId);
- $.PrivateBin.I18n.reset();
- var alias = $.PrivateBin.I18n._(messageId);
- $.PrivateBin.I18n.reset();
- 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;
- }
- );
- jsc.property(
- 'replaces %s in strings with first given parameter',
- 'string',
- '(small nearray) string',
- 'string',
- function (prefix, params, postfix) {
- 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;
- 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;
- }
- );
- });
- 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;
- }
- );
- });
- });
- describe('Model', function () {
- describe('getPasteId', function () {
- before(function () {
- $.PrivateBin.Model.reset();
- });
- jsc.property(
- 'returns the query string without separator, if any',
- jsc.nearray(jsc.elements(a2zString)),
- jsc.nearray(jsc.elements(a2zString)),
- jsc.nearray(jsc.elements(queryString)),
- 'string',
- function (schema, address, query, fragment) {
- var queryString = query.join(''),
- clean = jsdom('', {
- url: schema.join('') + '://' + address.join('') +
- '/?' + queryString + '#' + fragment
- }),
- result = $.PrivateBin.Model.getPasteId();
- $.PrivateBin.Model.reset();
- clean();
- return queryString === result;
- }
- );
- 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;
- }
- );
- });
- describe('getPasteKey', function () {
- jsc.property(
- 'returns the fragment of the URL',
- jsc.nearray(jsc.elements(a2zString)),
- jsc.nearray(jsc.elements(a2zString)),
- jsc.array(jsc.elements(queryString)),
- jsc.nearray(jsc.elements(base64String)),
- function (schema, address, query, fragment) {
- var fragmentString = fragment.join(''),
- clean = jsdom('', {
- url: schema.join('') + '://' + address.join('') +
- '/?' + query.join('') + '#' + fragmentString
- }),
- result = $.PrivateBin.Model.getPasteKey();
- $.PrivateBin.Model.reset();
- clean();
- return fragmentString === result;
- }
- );
- 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)),
- jsc.nearray(jsc.elements(base64String)),
- jsc.array(jsc.elements(queryString)),
- function (schema, address, query, fragment, trail) {
- var fragmentString = fragment.join(''),
- clean = jsdom('', {
- url: schema.join('') + '://' + address.join('') + '/?' +
- query.join('') + '#' + fragmentString + '&' + trail.join('')
- }),
- result = $.PrivateBin.Model.getPasteKey();
- $.PrivateBin.Model.reset();
- clean();
- return fragmentString === result;
- }
- );
- 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;
- }
- );
- });
- });
|