test.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. 'use strict';
  2. var jsc = require('jsverify'),
  3. jsdom = require('jsdom-global'),
  4. cleanup = jsdom(),
  5. a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  6. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  7. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  8. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  9. base64String = alnumString.concat(['+','/','=']).concat(
  10. a2zString.map(function(c) {
  11. return c.toUpperCase();
  12. })
  13. ),
  14. // schemas supported by the whatwg-url library
  15. schemas = ['ftp','gopher','http','https','ws','wss'],
  16. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  17. logFile = require('fs').createWriteStream('test.log');
  18. global.$ = global.jQuery = require('./jquery-3.1.1');
  19. global.sjcl = require('./sjcl-1.0.6');
  20. global.Base64 = require('./base64-2.1.9');
  21. global.RawDeflate = require('./rawdeflate-0.5');
  22. require('./rawinflate-0.3');
  23. require('./privatebin');
  24. // redirect console messages to log file
  25. console.warn = console.error = function (msg) {
  26. logFile.write(msg + '\n');
  27. }
  28. describe('Helper', function () {
  29. describe('secondsToHuman', function () {
  30. after(function () {
  31. cleanup();
  32. });
  33. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  34. var result = $.PrivateBin.Helper.secondsToHuman(number);
  35. return Array.isArray(result) &&
  36. result.length === 2 &&
  37. result[0] === parseInt(result[0], 10) &&
  38. typeof result[1] === 'string';
  39. });
  40. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  41. return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
  42. });
  43. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  44. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  45. });
  46. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  47. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  48. });
  49. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  50. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  51. });
  52. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  53. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  54. });
  55. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  56. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  57. });
  58. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  59. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  60. });
  61. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  62. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  63. });
  64. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  65. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  66. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  67. });
  68. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  69. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  70. });
  71. });
  72. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  73. // TODO: This needs to be tested using a browser.
  74. describe('selectText', function () {
  75. jsc.property(
  76. 'selection contains content of given ID',
  77. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  78. 'nearray string',
  79. function (ids, contents) {
  80. var html = '',
  81. result = true;
  82. ids.forEach(function(item, i) {
  83. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  84. });
  85. var clean = jsdom(html);
  86. ids.forEach(function(item, i) {
  87. $.PrivateBin.Helper.selectText(item.join(''));
  88. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  89. // Once there is one, uncomment the line below to actually check the result.
  90. //result *= (contents[i] || contents[0]) === window.getSelection().toString();
  91. });
  92. clean();
  93. return Boolean(result);
  94. }
  95. );
  96. });
  97. describe('setElementText', function () {
  98. after(function () {
  99. cleanup();
  100. });
  101. jsc.property(
  102. 'replaces the content of an element',
  103. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  104. 'nearray string',
  105. 'string',
  106. function (ids, contents, replacingContent) {
  107. var html = '',
  108. result = true;
  109. ids.forEach(function(item, i) {
  110. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  111. });
  112. var elements = $('<body />').html(html);
  113. ids.forEach(function(item, i) {
  114. var id = item.join(''),
  115. element = elements.find('#' + id).first();
  116. $.PrivateBin.Helper.setElementText(element, replacingContent);
  117. result *= replacingContent === element.text();
  118. });
  119. return Boolean(result);
  120. }
  121. );
  122. });
  123. describe('urls2links', function () {
  124. after(function () {
  125. cleanup();
  126. });
  127. jsc.property(
  128. 'ignores non-URL content',
  129. 'string',
  130. function (content) {
  131. var element = $('<div>' + content + '</div>'),
  132. before = element.html();
  133. $.PrivateBin.Helper.urls2links(element);
  134. return before === element.html();
  135. }
  136. );
  137. jsc.property(
  138. 'replaces URLs with anchors',
  139. 'string',
  140. jsc.elements(['http', 'https', 'ftp']),
  141. jsc.nearray(jsc.elements(a2zString)),
  142. jsc.array(jsc.elements(queryString)),
  143. jsc.array(jsc.elements(queryString)),
  144. 'string',
  145. function (prefix, schema, address, query, fragment, postfix) {
  146. var query = query.join(''),
  147. fragment = fragment.join(''),
  148. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  149. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  150. postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix),
  151. element = $('<div>' + prefix + url + postfix + '</div>');
  152. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  153. if (
  154. query.slice(-1) === '&' &&
  155. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  156. )
  157. {
  158. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  159. postfix = '';
  160. element = $('<div>' + prefix + url + '</div>');
  161. }
  162. $.PrivateBin.Helper.urls2links(element);
  163. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix + '</div>').html();
  164. }
  165. );
  166. jsc.property(
  167. 'replaces magnet links with anchors',
  168. 'string',
  169. jsc.array(jsc.elements(queryString)),
  170. 'string',
  171. function (prefix, query, postfix) {
  172. var url = 'magnet:?' + query.join(''),
  173. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  174. postfix = $.PrivateBin.Helper.htmlEntities(postfix),
  175. element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
  176. $.PrivateBin.Helper.urls2links(element);
  177. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
  178. }
  179. );
  180. });
  181. describe('sprintf', function () {
  182. after(function () {
  183. cleanup();
  184. });
  185. jsc.property(
  186. 'replaces %s in strings with first given parameter',
  187. 'string',
  188. '(small nearray) string',
  189. 'string',
  190. function (prefix, params, postfix) {
  191. prefix = prefix.replace(/%(s|d)/g, '%%');
  192. params[0] = params[0].replace(/%(s|d)/g, '%%');
  193. postfix = postfix.replace(/%(s|d)/g, '%%');
  194. var result = prefix + params[0] + postfix;
  195. params.unshift(prefix + '%s' + postfix);
  196. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  197. }
  198. );
  199. jsc.property(
  200. 'replaces %d in strings with first given parameter',
  201. 'string',
  202. '(small nearray) nat',
  203. 'string',
  204. function (prefix, params, postfix) {
  205. prefix = prefix.replace(/%(s|d)/g, '%%');
  206. postfix = postfix.replace(/%(s|d)/g, '%%');
  207. var result = prefix + params[0] + postfix;
  208. params.unshift(prefix + '%d' + postfix);
  209. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  210. }
  211. );
  212. jsc.property(
  213. 'replaces %d in strings with 0 if first parameter is not a number',
  214. 'string',
  215. '(small nearray) falsy',
  216. 'string',
  217. function (prefix, params, postfix) {
  218. prefix = prefix.replace(/%(s|d)/g, '%%');
  219. postfix = postfix.replace(/%(s|d)/g, '%%');
  220. var result = prefix + '0' + postfix;
  221. params.unshift(prefix + '%d' + postfix);
  222. return result === $.PrivateBin.Helper.sprintf.apply(this, params)
  223. }
  224. );
  225. jsc.property(
  226. 'replaces %d and %s in strings in order',
  227. 'string',
  228. 'nat',
  229. 'string',
  230. 'string',
  231. 'string',
  232. function (prefix, uint, middle, string, postfix) {
  233. prefix = prefix.replace(/%(s|d)/g, '%%');
  234. middle = middle.replace(/%(s|d)/g, '%%');
  235. postfix = postfix.replace(/%(s|d)/g, '%%');
  236. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  237. result = prefix + uint + middle + string + postfix;
  238. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  239. }
  240. );
  241. jsc.property(
  242. 'replaces %d and %s in strings in reverse order',
  243. 'string',
  244. 'nat',
  245. 'string',
  246. 'string',
  247. 'string',
  248. function (prefix, uint, middle, string, postfix) {
  249. prefix = prefix.replace(/%(s|d)/g, '%%');
  250. middle = middle.replace(/%(s|d)/g, '%%');
  251. postfix = postfix.replace(/%(s|d)/g, '%%');
  252. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  253. result = prefix + string + middle + uint + postfix;
  254. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  255. }
  256. );
  257. });
  258. describe('getCookie', function () {
  259. jsc.property(
  260. 'returns the requested cookie',
  261. 'nearray asciinestring',
  262. 'nearray asciistring',
  263. function (labels, values) {
  264. var selectedKey = '', selectedValue = '',
  265. cookieArray = [],
  266. count = 0;
  267. labels.forEach(function(item, i) {
  268. // deliberatly using a non-ascii key for replacing invalid characters
  269. var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
  270. value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
  271. cookieArray.push(key + '=' + value);
  272. if (Math.random() < 1 / i || selectedKey === key)
  273. {
  274. selectedKey = key;
  275. selectedValue = value;
  276. }
  277. });
  278. var clean = jsdom('', {cookie: cookieArray}),
  279. result = $.PrivateBin.Helper.getCookie(selectedKey);
  280. clean();
  281. return result === selectedValue;
  282. }
  283. );
  284. });
  285. describe('baseUri', function () {
  286. before(function () {
  287. $.PrivateBin.Helper.reset();
  288. });
  289. jsc.property(
  290. 'returns the URL without query & fragment',
  291. jsc.elements(schemas),
  292. jsc.nearray(jsc.elements(a2zString)),
  293. jsc.array(jsc.elements(queryString)),
  294. 'string',
  295. function (schema, address, query, fragment) {
  296. var expected = schema + '://' + address.join('') + '/',
  297. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  298. result = $.PrivateBin.Helper.baseUri();
  299. $.PrivateBin.Helper.reset();
  300. clean();
  301. return expected === result;
  302. }
  303. );
  304. });
  305. describe('htmlEntities', function () {
  306. after(function () {
  307. cleanup();
  308. });
  309. jsc.property(
  310. 'removes all HTML entities from any given string',
  311. 'string',
  312. function (string) {
  313. var result = $.PrivateBin.Helper.htmlEntities(string);
  314. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  315. }
  316. );
  317. });
  318. });
  319. describe('I18n', function () {
  320. describe('translate', function () {
  321. before(function () {
  322. $.PrivateBin.I18n.reset();
  323. });
  324. jsc.property(
  325. 'returns message ID unchanged if no translation found',
  326. 'string',
  327. function (messageId) {
  328. messageId = messageId.replace(/%(s|d)/g, '%%');
  329. var plurals = [messageId, messageId + 's'],
  330. fake = [messageId],
  331. result = $.PrivateBin.I18n.translate(messageId);
  332. $.PrivateBin.I18n.reset();
  333. var alias = $.PrivateBin.I18n._(messageId);
  334. $.PrivateBin.I18n.reset();
  335. var p_result = $.PrivateBin.I18n.translate(plurals);
  336. $.PrivateBin.I18n.reset();
  337. var p_alias = $.PrivateBin.I18n._(plurals);
  338. $.PrivateBin.I18n.reset();
  339. var f_result = $.PrivateBin.I18n.translate(fake);
  340. $.PrivateBin.I18n.reset();
  341. var f_alias = $.PrivateBin.I18n._(fake);
  342. $.PrivateBin.I18n.reset();
  343. return messageId === result && messageId === alias &&
  344. messageId === p_result && messageId === p_alias &&
  345. messageId === f_result && messageId === f_alias;
  346. }
  347. );
  348. jsc.property(
  349. 'replaces %s in strings with first given parameter',
  350. 'string',
  351. '(small nearray) string',
  352. 'string',
  353. function (prefix, params, postfix) {
  354. prefix = prefix.replace(/%(s|d)/g, '%%');
  355. params[0] = params[0].replace(/%(s|d)/g, '%%');
  356. postfix = postfix.replace(/%(s|d)/g, '%%');
  357. var translation = prefix + params[0] + postfix;
  358. params.unshift(prefix + '%s' + postfix);
  359. var result = $.PrivateBin.I18n.translate.apply(this, params);
  360. $.PrivateBin.I18n.reset();
  361. var alias = $.PrivateBin.I18n._.apply(this, params);
  362. $.PrivateBin.I18n.reset();
  363. return translation === result && translation === alias;
  364. }
  365. );
  366. });
  367. describe('getPluralForm', function () {
  368. before(function () {
  369. $.PrivateBin.I18n.reset();
  370. });
  371. jsc.property(
  372. 'returns valid key for plural form',
  373. jsc.elements(supportedLanguages),
  374. 'integer',
  375. function(language, n) {
  376. $.PrivateBin.I18n.reset(language);
  377. var result = $.PrivateBin.I18n.getPluralForm(n);
  378. // arabic seems to have the highest plural count with 6 forms
  379. return result >= 0 && result <= 5;
  380. }
  381. );
  382. });
  383. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  384. // TODO: This needs to be tested using a browser.
  385. describe('loadTranslations', function () {
  386. before(function () {
  387. $.PrivateBin.I18n.reset();
  388. });
  389. jsc.property(
  390. 'downloads and handles any supported language',
  391. jsc.elements(supportedLanguages),
  392. function(language) {
  393. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  394. $.PrivateBin.I18n.reset('en');
  395. $.PrivateBin.I18n.loadTranslations();
  396. $.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
  397. var result = $.PrivateBin.I18n.translate('en'),
  398. alias = $.PrivateBin.I18n._('en');
  399. clean();
  400. return language === result && language === alias;
  401. }
  402. );
  403. });
  404. });
  405. describe('Model', function () {
  406. describe('getPasteId', function () {
  407. before(function () {
  408. $.PrivateBin.Model.reset();
  409. });
  410. jsc.property(
  411. 'returns the query string without separator, if any',
  412. jsc.nearray(jsc.elements(a2zString)),
  413. jsc.nearray(jsc.elements(a2zString)),
  414. jsc.nearray(jsc.elements(queryString)),
  415. 'string',
  416. function (schema, address, query, fragment) {
  417. var queryString = query.join(''),
  418. clean = jsdom('', {
  419. url: schema.join('') + '://' + address.join('') +
  420. '/?' + queryString + '#' + fragment
  421. }),
  422. result = $.PrivateBin.Model.getPasteId();
  423. $.PrivateBin.Model.reset();
  424. clean();
  425. return queryString === result;
  426. }
  427. );
  428. jsc.property(
  429. 'throws exception on empty query string',
  430. jsc.nearray(jsc.elements(a2zString)),
  431. jsc.nearray(jsc.elements(a2zString)),
  432. 'string',
  433. function (schema, address, fragment) {
  434. var clean = jsdom('', {
  435. url: schema.join('') + '://' + address.join('') +
  436. '/#' + fragment
  437. }),
  438. result = false;
  439. try {
  440. $.PrivateBin.Model.getPasteId();
  441. }
  442. catch(err) {
  443. result = true;
  444. }
  445. $.PrivateBin.Model.reset();
  446. clean();
  447. return result;
  448. }
  449. );
  450. });
  451. describe('getPasteKey', function () {
  452. jsc.property(
  453. 'returns the fragment of the URL',
  454. jsc.nearray(jsc.elements(a2zString)),
  455. jsc.nearray(jsc.elements(a2zString)),
  456. jsc.array(jsc.elements(queryString)),
  457. jsc.nearray(jsc.elements(base64String)),
  458. function (schema, address, query, fragment) {
  459. var fragmentString = fragment.join(''),
  460. clean = jsdom('', {
  461. url: schema.join('') + '://' + address.join('') +
  462. '/?' + query.join('') + '#' + fragmentString
  463. }),
  464. result = $.PrivateBin.Model.getPasteKey();
  465. $.PrivateBin.Model.reset();
  466. clean();
  467. return fragmentString === result;
  468. }
  469. );
  470. jsc.property(
  471. 'returns the fragment stripped of trailing query parts',
  472. jsc.nearray(jsc.elements(a2zString)),
  473. jsc.nearray(jsc.elements(a2zString)),
  474. jsc.array(jsc.elements(queryString)),
  475. jsc.nearray(jsc.elements(base64String)),
  476. jsc.array(jsc.elements(queryString)),
  477. function (schema, address, query, fragment, trail) {
  478. var fragmentString = fragment.join(''),
  479. clean = jsdom('', {
  480. url: schema.join('') + '://' + address.join('') + '/?' +
  481. query.join('') + '#' + fragmentString + '&' + trail.join('')
  482. }),
  483. result = $.PrivateBin.Model.getPasteKey();
  484. $.PrivateBin.Model.reset();
  485. clean();
  486. return fragmentString === result;
  487. }
  488. );
  489. jsc.property(
  490. 'throws exception on empty fragment of the URL',
  491. jsc.nearray(jsc.elements(a2zString)),
  492. jsc.nearray(jsc.elements(a2zString)),
  493. jsc.array(jsc.elements(queryString)),
  494. function (schema, address, query) {
  495. var clean = jsdom('', {
  496. url: schema.join('') + '://' + address.join('') +
  497. '/?' + query.join('')
  498. }),
  499. result = false;
  500. try {
  501. $.PrivateBin.Model.getPasteKey();
  502. }
  503. catch(err) {
  504. result = true;
  505. }
  506. $.PrivateBin.Model.reset();
  507. clean();
  508. return result;
  509. }
  510. );
  511. });
  512. });