test.js 20 KB

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