test.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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').Base64;
  21. global.RawDeflate = require('./rawdeflate-0.5').RawDeflate;
  22. global.RawDeflate.inflate = require('./rawinflate-0.3').RawDeflate.inflate;
  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. this.timeout(30000);
  76. jsc.property(
  77. 'selection contains content of given ID',
  78. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  79. 'nearray string',
  80. function (ids, contents) {
  81. var html = '',
  82. result = true;
  83. ids.forEach(function(item, i) {
  84. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  85. });
  86. var clean = jsdom(html);
  87. ids.forEach(function(item, i) {
  88. $.PrivateBin.Helper.selectText(item.join(''));
  89. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  90. // Once there is one, uncomment the line below to actually check the result.
  91. //result *= (contents[i] || contents[0]) === window.getSelection().toString();
  92. });
  93. clean();
  94. return Boolean(result);
  95. }
  96. );
  97. });
  98. describe('setElementText', function () {
  99. after(function () {
  100. cleanup();
  101. });
  102. jsc.property(
  103. 'replaces the content of an element',
  104. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  105. 'nearray string',
  106. 'string',
  107. function (ids, contents, replacingContent) {
  108. var html = '',
  109. result = true;
  110. ids.forEach(function(item, i) {
  111. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  112. });
  113. var elements = $('<body />').html(html);
  114. ids.forEach(function(item, i) {
  115. var id = item.join(''),
  116. element = elements.find('#' + id).first();
  117. $.PrivateBin.Helper.setElementText(element, replacingContent);
  118. result *= replacingContent === element.text();
  119. });
  120. return Boolean(result);
  121. }
  122. );
  123. });
  124. describe('urls2links', function () {
  125. after(function () {
  126. cleanup();
  127. });
  128. jsc.property(
  129. 'ignores non-URL content',
  130. 'string',
  131. function (content) {
  132. var element = $('<div>' + content + '</div>'),
  133. before = element.html();
  134. $.PrivateBin.Helper.urls2links(element);
  135. return before === element.html();
  136. }
  137. );
  138. jsc.property(
  139. 'replaces URLs with anchors',
  140. 'string',
  141. jsc.elements(['http', 'https', 'ftp']),
  142. jsc.nearray(jsc.elements(a2zString)),
  143. jsc.array(jsc.elements(queryString)),
  144. jsc.array(jsc.elements(queryString)),
  145. 'string',
  146. function (prefix, schema, address, query, fragment, postfix) {
  147. var query = query.join(''),
  148. fragment = fragment.join(''),
  149. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  150. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  151. postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix),
  152. element = $('<div>' + prefix + url + postfix + '</div>');
  153. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  154. if (
  155. query.slice(-1) === '&' &&
  156. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  157. )
  158. {
  159. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  160. postfix = '';
  161. element = $('<div>' + prefix + url + '</div>');
  162. }
  163. $.PrivateBin.Helper.urls2links(element);
  164. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix + '</div>').html();
  165. }
  166. );
  167. jsc.property(
  168. 'replaces magnet links with anchors',
  169. 'string',
  170. jsc.array(jsc.elements(queryString)),
  171. 'string',
  172. function (prefix, query, postfix) {
  173. var url = 'magnet:?' + query.join(''),
  174. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  175. postfix = $.PrivateBin.Helper.htmlEntities(postfix),
  176. element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
  177. $.PrivateBin.Helper.urls2links(element);
  178. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
  179. }
  180. );
  181. });
  182. describe('sprintf', function () {
  183. after(function () {
  184. cleanup();
  185. });
  186. jsc.property(
  187. 'replaces %s in strings with first given parameter',
  188. 'string',
  189. '(small nearray) string',
  190. 'string',
  191. function (prefix, params, postfix) {
  192. prefix = prefix.replace(/%(s|d)/g, '%%');
  193. params[0] = params[0].replace(/%(s|d)/g, '%%');
  194. postfix = postfix.replace(/%(s|d)/g, '%%');
  195. var result = prefix + params[0] + postfix;
  196. params.unshift(prefix + '%s' + postfix);
  197. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  198. }
  199. );
  200. jsc.property(
  201. 'replaces %d in strings with first given parameter',
  202. 'string',
  203. '(small nearray) nat',
  204. 'string',
  205. function (prefix, params, postfix) {
  206. prefix = prefix.replace(/%(s|d)/g, '%%');
  207. postfix = postfix.replace(/%(s|d)/g, '%%');
  208. var result = prefix + params[0] + postfix;
  209. params.unshift(prefix + '%d' + postfix);
  210. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  211. }
  212. );
  213. jsc.property(
  214. 'replaces %d in strings with 0 if first parameter is not a number',
  215. 'string',
  216. '(small nearray) falsy',
  217. 'string',
  218. function (prefix, params, postfix) {
  219. prefix = prefix.replace(/%(s|d)/g, '%%');
  220. postfix = postfix.replace(/%(s|d)/g, '%%');
  221. var result = prefix + '0' + postfix;
  222. params.unshift(prefix + '%d' + postfix);
  223. return result === $.PrivateBin.Helper.sprintf.apply(this, params)
  224. }
  225. );
  226. jsc.property(
  227. 'replaces %d and %s in strings in order',
  228. 'string',
  229. 'nat',
  230. 'string',
  231. 'string',
  232. 'string',
  233. function (prefix, uint, middle, string, postfix) {
  234. prefix = prefix.replace(/%(s|d)/g, '%%');
  235. middle = middle.replace(/%(s|d)/g, '%%');
  236. postfix = postfix.replace(/%(s|d)/g, '%%');
  237. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  238. result = prefix + uint + middle + string + postfix;
  239. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  240. }
  241. );
  242. jsc.property(
  243. 'replaces %d and %s in strings in reverse order',
  244. 'string',
  245. 'nat',
  246. 'string',
  247. 'string',
  248. 'string',
  249. function (prefix, uint, middle, string, postfix) {
  250. prefix = prefix.replace(/%(s|d)/g, '%%');
  251. middle = middle.replace(/%(s|d)/g, '%%');
  252. postfix = postfix.replace(/%(s|d)/g, '%%');
  253. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  254. result = prefix + string + middle + uint + postfix;
  255. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  256. }
  257. );
  258. });
  259. describe('getCookie', function () {
  260. this.timeout(30000);
  261. jsc.property(
  262. 'returns the requested cookie',
  263. 'nearray asciinestring',
  264. 'nearray asciistring',
  265. function (labels, values) {
  266. var selectedKey = '', selectedValue = '',
  267. cookieArray = [],
  268. count = 0;
  269. labels.forEach(function(item, i) {
  270. // deliberatly using a non-ascii key for replacing invalid characters
  271. var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
  272. value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
  273. cookieArray.push(key + '=' + value);
  274. if (Math.random() < 1 / i || selectedKey === key)
  275. {
  276. selectedKey = key;
  277. selectedValue = value;
  278. }
  279. });
  280. var clean = jsdom('', {cookie: cookieArray}),
  281. result = $.PrivateBin.Helper.getCookie(selectedKey);
  282. clean();
  283. return result === selectedValue;
  284. }
  285. );
  286. });
  287. describe('baseUri', function () {
  288. this.timeout(30000);
  289. before(function () {
  290. $.PrivateBin.Helper.reset();
  291. });
  292. jsc.property(
  293. 'returns the URL without query & fragment',
  294. jsc.elements(schemas),
  295. jsc.nearray(jsc.elements(a2zString)),
  296. jsc.array(jsc.elements(queryString)),
  297. 'string',
  298. function (schema, address, query, fragment) {
  299. var expected = schema + '://' + address.join('') + '/',
  300. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  301. result = $.PrivateBin.Helper.baseUri();
  302. $.PrivateBin.Helper.reset();
  303. clean();
  304. return expected === result;
  305. }
  306. );
  307. });
  308. describe('htmlEntities', function () {
  309. after(function () {
  310. cleanup();
  311. });
  312. jsc.property(
  313. 'removes all HTML entities from any given string',
  314. 'string',
  315. function (string) {
  316. var result = $.PrivateBin.Helper.htmlEntities(string);
  317. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  318. }
  319. );
  320. });
  321. });
  322. describe('I18n', function () {
  323. describe('translate', function () {
  324. before(function () {
  325. $.PrivateBin.I18n.reset();
  326. });
  327. jsc.property(
  328. 'returns message ID unchanged if no translation found',
  329. 'string',
  330. function (messageId) {
  331. messageId = messageId.replace(/%(s|d)/g, '%%');
  332. var plurals = [messageId, messageId + 's'],
  333. fake = [messageId],
  334. result = $.PrivateBin.I18n.translate(messageId);
  335. $.PrivateBin.I18n.reset();
  336. var alias = $.PrivateBin.I18n._(messageId);
  337. $.PrivateBin.I18n.reset();
  338. var p_result = $.PrivateBin.I18n.translate(plurals);
  339. $.PrivateBin.I18n.reset();
  340. var p_alias = $.PrivateBin.I18n._(plurals);
  341. $.PrivateBin.I18n.reset();
  342. var f_result = $.PrivateBin.I18n.translate(fake);
  343. $.PrivateBin.I18n.reset();
  344. var f_alias = $.PrivateBin.I18n._(fake);
  345. $.PrivateBin.I18n.reset();
  346. return messageId === result && messageId === alias &&
  347. messageId === p_result && messageId === p_alias &&
  348. messageId === f_result && messageId === f_alias;
  349. }
  350. );
  351. jsc.property(
  352. 'replaces %s in strings with first given parameter',
  353. 'string',
  354. '(small nearray) string',
  355. 'string',
  356. function (prefix, params, postfix) {
  357. prefix = prefix.replace(/%(s|d)/g, '%%');
  358. params[0] = params[0].replace(/%(s|d)/g, '%%');
  359. postfix = postfix.replace(/%(s|d)/g, '%%');
  360. var translation = prefix + params[0] + postfix;
  361. params.unshift(prefix + '%s' + postfix);
  362. var result = $.PrivateBin.I18n.translate.apply(this, params);
  363. $.PrivateBin.I18n.reset();
  364. var alias = $.PrivateBin.I18n._.apply(this, params);
  365. $.PrivateBin.I18n.reset();
  366. return translation === result && translation === alias;
  367. }
  368. );
  369. });
  370. describe('getPluralForm', function () {
  371. before(function () {
  372. $.PrivateBin.I18n.reset();
  373. });
  374. jsc.property(
  375. 'returns valid key for plural form',
  376. jsc.elements(supportedLanguages),
  377. 'integer',
  378. function(language, n) {
  379. $.PrivateBin.I18n.reset(language);
  380. var result = $.PrivateBin.I18n.getPluralForm(n);
  381. // arabic seems to have the highest plural count with 6 forms
  382. return result >= 0 && result <= 5;
  383. }
  384. );
  385. });
  386. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  387. // TODO: This needs to be tested using a browser.
  388. describe('loadTranslations', function () {
  389. this.timeout(30000);
  390. before(function () {
  391. $.PrivateBin.I18n.reset();
  392. });
  393. jsc.property(
  394. 'downloads and handles any supported language',
  395. jsc.elements(supportedLanguages),
  396. function(language) {
  397. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  398. $.PrivateBin.I18n.reset('en');
  399. $.PrivateBin.I18n.loadTranslations();
  400. $.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
  401. var result = $.PrivateBin.I18n.translate('en'),
  402. alias = $.PrivateBin.I18n._('en');
  403. clean();
  404. return language === result && language === alias;
  405. }
  406. );
  407. });
  408. });
  409. describe('CryptTool', function () {
  410. describe('cipher & decipher', function () {
  411. this.timeout(30000);
  412. it('can en- and decrypt any message', function () {
  413. jsc.check(jsc.forall(
  414. 'string',
  415. 'string',
  416. 'string',
  417. function (key, password, message) {
  418. return message === $.PrivateBin.CryptTool.decipher(
  419. key,
  420. password,
  421. $.PrivateBin.CryptTool.cipher(key, password, message)
  422. );
  423. }
  424. ),
  425. // reducing amount of checks as running 100 takes about 5 minutes
  426. {tests: 5, quiet: true});
  427. });
  428. // The below static unit tests are included to ensure deciphering of "classic"
  429. // SJCL based pastes still works
  430. it(
  431. 'supports PrivateBin v1 ciphertext (SJCL & Base64 2.1.9)',
  432. function () {
  433. // Of course you can easily decipher the following texts, if you like.
  434. // Bonus points for finding their sources and hidden meanings.
  435. var paste1 = $.PrivateBin.CryptTool.decipher(
  436. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  437. // -- "That's amazing. I've got the same combination on my luggage."
  438. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  439. '{"iv":"4HNFIl7eYbCh6HuShctTIA==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"u0lQvePq6L0=","ct":"fGPUVrDyaVr1ZDGb+kqQ3CPEW8x4YKGfzHDmA0Vjkh250aWNe7Cnigkps9aaFVMX9AaerrTp3yZbojJtNqVGMfLdUTu+53xmZHqRKxCCqSfDNSNoW4Oxk5OVgAtRyuG4bXHDsWTXDNz2xceqzVFqhkwTwlUchrV7uuFK/XUKTNjPFM744moivIcBbfM2FOeKlIFs8RYPYuvqQhp2rMLlNGwwKh//4kykQsHMQDeSDuJl8stMQzgWR/btUBZuwNZEydkMH6IPpTdf5WTSrZ+wC2OK0GutCm4UaEe6txzaTMfu+WRVu4PN6q+N+2zljWJ1XdpVcN/i0Sv4QVMym0Xa6y0eccEhj/69o47PmExmMMeEwExImPalMNT9JUSiZdOZJ/GdzwrwoIuq1mdQR6vSH+XJ/8jXJQ7bjjJVJYXTcT0Di5jixArI2Kpp1GGlGVFbLgPugwU1wczg+byqeDOAECXRRnQcogeaJtVcRwXwfy4j3ORFcblYMilxyHqKBewcYPRVBGtBs50cVjSIkAfR84rnc1nfvnxK/Gmm+4VBNHI6ODWNpRolVMCzXjbKYnV3Are5AgSpsTqaGl41VJGpcco6cAwi4K0Bys1seKR+bLSdUgqRrkEqSRSdu3/VTu9HhEk8an0rjTE4CBB5/LMn16p0TGLoOb32odKFIEtpanVvLjeyiVMvSxcgYLNnTi/5FiaAC4pJxRD+AZHedU1FICUeEXxIcac/4E5qjkHjX9SpQtLl80QLIVnjNliZm7QLB/nKu7W8Jb0+/CiTdV3Q9LhxlH4ciprnX+W0B00BKYFHnL9jRVzKdXhf1EHydbXMAfpCjHAXIVCkFakJinQBDIIw/SC6Yig0u0ddEID2B7LYAP1iE4RZwzTrxCB+ke2jQr8c20Jj6u6ShFOPC9DCw9XupZ4HAalVG00kSgjus+b8zrVji3/LKEhb4EBzp1ctBJCFTeXwej8ZETLoXTylev5dlwZSYAbuBPPcbFR/xAIPx3uDabd1E1gTqUc68ICIGhd197Mb2eRWiSvHr5SPsASerMxId6XA6+iQlRiI+NDR+TGVNmCnfxSlyPFMOHGTmslXOGIqGfBR8l4ft8YVZ70lCwmwTuViGc75ULSf9mM57/LmRzQFMYQtvI8IFK9JaQEMY5xz0HLtR4iyQUUdwR9e0ytBNdWF2a2WPDEnJuY/QJo4GzTlgv4QUxMXI5htsn2rf0HxCFu7Po8DNYLxTS+67hYjDIYWYaEIc8LXWMLyDm9C5fARPJ4F2BIWgzgzkNj+dVjusft2XnziamWdbS5u3kuRlVuz5LQj+R5imnqQAincdZTkTT1nYx+DatlOLllCYIHffpI="}'
  440. ),
  441. paste2 = $.PrivateBin.CryptTool.decipher(
  442. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  443. '', // no password
  444. '{"iv":"WA42mdxIVXUwBqZu7JYNiw==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"jN6CjbQMJCM=","ct":"kYYMo5DFG1+w0UHiYXT5pdV0IUuXxzOlslkW/c3DRCbGFROCVkAskHce7HoRczee1N9c5MhHjVMJUIZE02qIS8UyHdJ/GqcPVidTUcj9rnDNWsTXkjVv8jCwHS/cwmAjDTWpwp5ThECN+ov/wNp/NdtTj8Qj7f/T3rfZIOCWfwLH9s4Des35UNcUidfPTNQ1l0Gm0X+r98CCUSYZjQxkZc6hRZBLPQ8EaNVooUwd5eP4GiYlmSDNA0wOSA+5isPYxomVCt+kFf58VBlNhpfNi7BLYAUTPpXT4SfH5drR9+C7NTeZ+tTCYjbU94PzYItOpu8vgnB1/a6BAM5h3m9w+giUb0df4hgTWeZnZxLjo5BN8WV+kdTXMj3/Vv0gw0DQrDcCuX/cBAjpy3lQGwlAN1vXoOIyZJUjMpQRrOLdKvLB+zcmVNtGDbgnfP2IYBzk9NtodpUa27ne0T0ZpwOPlVwevsIVZO224WLa+iQmmHOWDFFpVDlS0t0fLfOk7Hcb2xFsTxiCIiyKMho/IME1Du3X4e6BVa3hobSSZv0rRtNgY1KcyYPrUPW2fxZ+oik3y9SgGvb7XpjVIta8DWlDWRfZ9kzoweWEYqz9IA8Xd373RefpyuWI25zlHoX3nwljzsZU6dC//h/Dt2DNr+IAvKO3+u23cWoB9kgcZJ2FJuqjLvVfCF+OWcig7zs2pTYJW6Rg6lqbBCxiUUlae6xJrjfv0pzD2VYCLY7v1bVTagppwKzNI3WaluCOrdDYUCxUSe56yd1oAoLPRVbYvomRboUO6cjQhEknERyvt45og2kORJOEJayHW+jZgR0Y0jM3Nk17ubpij2gHxNx9kiLDOiCGSV5mn9mV7qd3HHcOMSykiBgbyzjobi96LT2dIGLeDXTIdPOog8wyobO4jWq0GGs0vBB8oSYXhHvixZLcSjX2KQuHmEoWzmJcr3DavdoXZmAurGWLKjzEdJc5dSD/eNr99gjHX7wphJ6umKMM+fn6PcbYJkhDh2GlJL5COXjXfm/5aj/vuyaRRWZMZtmnYpGAtAPg7AUG"}'
  445. );
  446. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  447. throw Error('v1 (SJCL based) pastes could not be deciphered');
  448. }
  449. }
  450. );
  451. it(
  452. 'supports ZeroBin ciphertext (SJCL & Base64 1.7)',
  453. function () {
  454. var newBase64 = global.Base64;
  455. global.Base64 = require('./base64-1.7').Base64;
  456. jsdom();
  457. delete require.cache[require.resolve('./privatebin')];
  458. require('./privatebin');
  459. // Of course you can easily decipher the following texts, if you like.
  460. // Bonus points for finding their sources and hidden meanings.
  461. var paste1 = $.PrivateBin.CryptTool.decipher(
  462. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  463. // -- "That's amazing. I've got the same combination on my luggage."
  464. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  465. '{"iv":"aTnR2qBL1CAmLX8FdWe3VA==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"u0lQvePq6L0=","ct":"A3nBTvICZtYy6xqbIJE0c8Veored5lMJUGgGUm4581wjrPFlU0Q0tUZSf+RUUoZj2jqDa4kiyyZ5YNMe30hNMV0oVSalNhRgD9svVMnPuF162IbyhVCwr7ULjT981CHxVlGNqGqmIU6L/XixgdArxAA8x1GCrfAkBWWGeq8Qw5vJPG/RCHpwR4Wy3azrluqeyERBzmaOQjO/kM35TiI6IrLYFyYyL7upYlxAaxS0XBMZvN8QU8Lnerwvh5JVC6OkkKrhogajTJIKozCF79yI78c50LUh7tTuI3Yoh7+fXxhoODvQdYFmoiUlrutN7Y5ZMRdITvVu8fTYtX9c7Fiufmcq5icEimiHp2g1bvfpOaGOsFT+XNFgC9215jcp5mpBdN852xs7bUtw+nDrf+LsDEX6iRpRZ+PYgLDN5xQT1ByEtYbeP+tO38pnx72oZdIB3cj8UkOxnxdNiZM5YB5egn4jUj1fHot1I69WoTiUJipZ5PIATv7ScymRB+AYzjxjurQ9lVfX9QtAbEH2dhdmoUo3IDRSXpWNCe9RC1aUIyWfZO7oI7FEohNscHNTLEcT+wFnFUPByLlXmjNZ7FKeNpvUm3jTY4t4sbZH8o2dUl624PAw1INcJ6FKqWGWwoFT2j1MYC+YV/LkLTdjuWfayvwLMh27G/FfKCRbW36vqinegqpPDylsx9+3oFkEw3y5Z8+44oN91rE/4Md7JhPJeRVlFC9TNCj4dA+EVhbbQqscvSnIH2uHkMw7mNNo7xba/YT9KoPDaniqnYqb+q2pX1WNWE7dLS2wfroMAS3kh8P22DAV37AeiNoD2PcI6ZcHbRdPa+XRrRcJhSPPW7UQ0z4OvBfjdu/w390QxAxSxvZewoh49fKKB6hTsRnZb4tpHkjlww=="}'
  466. ),
  467. paste2 = $.PrivateBin.CryptTool.decipher(
  468. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  469. '', // no password
  470. '{"iv":"Z7lAZQbkrqGMvruxoSm6Pw==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"jN6CjbQMJCM=","ct":"PuOPWB3i2FPcreSrLYeQf84LdE8RHjsc+MGtiOr4b7doNyWKYtkNorbRadxaPnEee2/Utrp1MIIfY5juJSy8RGwEPX5ciWcYe6EzsXWznsnvhmpKNj9B7eIIrfSbxfy8E2e/g7xav1nive+ljToka3WT1DZ8ILQd/NbnJeHWaoSEOfvz8+d8QJPb1tNZvs7zEY95DumQwbyOsIMKAvcZHJ9OJNpujXzdMyt6DpcFcqlldWBZ/8q5rAUTw0HNx/rCgbhAxRYfNoTLIcMM4L0cXbPSgCjwf5FuO3EdE13mgEDhcClW79m0QvcnIh8xgzYoxLbp0+AwvC/MbZM8savN/0ieWr2EKkZ04ggiOIEyvfCUuNprQBYO+y8kKduNEN6by0Yf4LRCPfmwN+GezDLuzTnZIMhPbGqUAdgV6ExqK2ULEEIrQEMoOuQIxfoMhqLlzG79vXGt2O+BY+4IiYfvmuRLks4UXfyHqxPXTJg48IYbGs0j4TtJPUgp3523EyYLwEGyVTAuWhYAmVIwd/hoV7d7tmfcF73w9dufDFI3LNca2KxzBnWNPYvIZKBwWbq8ncxkb191dP6mjEi7NnhqVk5A6vIBbu4AC5PZf76l6yep4xsoy/QtdDxCMocCXeAML9MQ9uPQbuspOKrBvMfN5igA1kBqasnxI472KBNXsdZnaDddSVUuvhTcETM="}'
  471. );
  472. global.Base64 = newBase64;
  473. jsdom();
  474. delete require.cache[require.resolve('./privatebin')];
  475. require('./privatebin');
  476. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  477. throw Error('v1 (SJCL based) pastes could not be deciphered');
  478. }
  479. }
  480. );
  481. });
  482. describe('isEntropyReady & addEntropySeedListener', function () {
  483. it(
  484. 'lets us know that enough entropy is collected or make us wait for it',
  485. function(done) {
  486. if ($.PrivateBin.CryptTool.isEntropyReady()) {
  487. done();
  488. } else {
  489. $.PrivateBin.CryptTool.addEntropySeedListener(function() {
  490. done();
  491. });
  492. }
  493. }
  494. );
  495. });
  496. describe('getSymmetricKey', function () {
  497. var keys = [];
  498. // the parameter is used to ensure the test is run more then one time
  499. jsc.property(
  500. 'returns random, non-empty keys',
  501. 'nat',
  502. function(n) {
  503. var key = $.PrivateBin.CryptTool.getSymmetricKey(),
  504. result = (key !== '' && keys.indexOf(key) === -1);
  505. keys.push(key);
  506. return result;
  507. }
  508. );
  509. });
  510. describe('Base64.js vs SJCL.js vs abab.js', function () {
  511. jsc.property(
  512. 'these all return the same base64 string',
  513. 'string',
  514. function(string) {
  515. var base64 = Base64.toBase64(string),
  516. sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)),
  517. abab = window.btoa(Base64.utob(string));
  518. return base64 === sjcl && sjcl === abab;
  519. }
  520. );
  521. });
  522. });
  523. describe('Model', function () {
  524. describe('getExpirationDefault', function () {
  525. before(function () {
  526. $.PrivateBin.Model.reset();
  527. cleanup();
  528. });
  529. jsc.property(
  530. 'returns the contents of the element with id "pasteExpiration"',
  531. 'array asciinestring',
  532. 'string',
  533. 'small nat',
  534. function (keys, value, key) {
  535. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  536. value = $.PrivateBin.Helper.htmlEntities(value);
  537. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  538. contents = '<select id="pasteExpiration" name="pasteExpiration">';
  539. keys.forEach(function(item) {
  540. contents += '<option value="' + item + '"';
  541. if (item === content) {
  542. contents += ' selected="selected"';
  543. }
  544. contents += '>' + value + '</option>';
  545. });
  546. contents += '</select>';
  547. $('body').html(contents);
  548. var result = $.PrivateBin.Helper.htmlEntities(
  549. $.PrivateBin.Model.getExpirationDefault()
  550. );
  551. $.PrivateBin.Model.reset();
  552. return content === result;
  553. }
  554. );
  555. });
  556. describe('getFormatDefault', function () {
  557. before(function () {
  558. $.PrivateBin.Model.reset();
  559. cleanup();
  560. });
  561. jsc.property(
  562. 'returns the contents of the element with id "pasteFormatter"',
  563. 'array asciinestring',
  564. 'string',
  565. 'small nat',
  566. function (keys, value, key) {
  567. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  568. value = $.PrivateBin.Helper.htmlEntities(value);
  569. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  570. contents = '<select id="pasteFormatter" name="pasteFormatter">';
  571. keys.forEach(function(item) {
  572. contents += '<option value="' + item + '"';
  573. if (item === content) {
  574. contents += ' selected="selected"';
  575. }
  576. contents += '>' + value + '</option>';
  577. });
  578. contents += '</select>';
  579. $('body').html(contents);
  580. var result = $.PrivateBin.Helper.htmlEntities(
  581. $.PrivateBin.Model.getFormatDefault()
  582. );
  583. $.PrivateBin.Model.reset();
  584. return content === result;
  585. }
  586. );
  587. });
  588. describe('hasCipherData', function () {
  589. before(function () {
  590. $.PrivateBin.Model.reset();
  591. cleanup();
  592. });
  593. jsc.property(
  594. 'checks if the element with id "cipherdata" contains any data',
  595. 'asciistring',
  596. function (value) {
  597. value = $.PrivateBin.Helper.htmlEntities(value).trim();
  598. $('body').html('<div id="cipherdata">' + value + '</div>');
  599. $.PrivateBin.Model.init();
  600. var result = $.PrivateBin.Model.hasCipherData();
  601. $.PrivateBin.Model.reset();
  602. return (value.length > 0) === result;
  603. }
  604. );
  605. });
  606. describe('getCipherData', function () {
  607. before(function () {
  608. $.PrivateBin.Model.reset();
  609. cleanup();
  610. });
  611. jsc.property(
  612. 'returns the contents of the element with id "cipherdata"',
  613. 'asciistring',
  614. function (value) {
  615. value = $.PrivateBin.Helper.htmlEntities(value).trim();
  616. $('body').html('<div id="cipherdata">' + value + '</div>');
  617. $.PrivateBin.Model.init();
  618. var result = $.PrivateBin.Helper.htmlEntities(
  619. $.PrivateBin.Model.getCipherData()
  620. );
  621. $.PrivateBin.Model.reset();
  622. return value === result;
  623. }
  624. );
  625. });
  626. describe('getPasteId', function () {
  627. this.timeout(30000);
  628. before(function () {
  629. $.PrivateBin.Model.reset();
  630. cleanup();
  631. });
  632. jsc.property(
  633. 'returns the query string without separator, if any',
  634. jsc.nearray(jsc.elements(a2zString)),
  635. jsc.nearray(jsc.elements(a2zString)),
  636. jsc.nearray(jsc.elements(queryString)),
  637. 'string',
  638. function (schema, address, query, fragment) {
  639. var queryString = query.join(''),
  640. clean = jsdom('', {
  641. url: schema.join('') + '://' + address.join('') +
  642. '/?' + queryString + '#' + fragment
  643. }),
  644. result = $.PrivateBin.Model.getPasteId();
  645. $.PrivateBin.Model.reset();
  646. clean();
  647. return queryString === result;
  648. }
  649. );
  650. jsc.property(
  651. 'throws exception on empty query string',
  652. jsc.nearray(jsc.elements(a2zString)),
  653. jsc.nearray(jsc.elements(a2zString)),
  654. 'string',
  655. function (schema, address, fragment) {
  656. var clean = jsdom('', {
  657. url: schema.join('') + '://' + address.join('') +
  658. '/#' + fragment
  659. }),
  660. result = false;
  661. try {
  662. $.PrivateBin.Model.getPasteId();
  663. }
  664. catch(err) {
  665. result = true;
  666. }
  667. $.PrivateBin.Model.reset();
  668. clean();
  669. return result;
  670. }
  671. );
  672. });
  673. describe('getPasteKey', function () {
  674. this.timeout(30000);
  675. jsc.property(
  676. 'returns the fragment of the URL',
  677. jsc.nearray(jsc.elements(a2zString)),
  678. jsc.nearray(jsc.elements(a2zString)),
  679. jsc.array(jsc.elements(queryString)),
  680. jsc.nearray(jsc.elements(base64String)),
  681. function (schema, address, query, fragment) {
  682. var fragmentString = fragment.join(''),
  683. clean = jsdom('', {
  684. url: schema.join('') + '://' + address.join('') +
  685. '/?' + query.join('') + '#' + fragmentString
  686. }),
  687. result = $.PrivateBin.Model.getPasteKey();
  688. $.PrivateBin.Model.reset();
  689. clean();
  690. return fragmentString === result;
  691. }
  692. );
  693. jsc.property(
  694. 'returns the fragment stripped of trailing query parts',
  695. jsc.nearray(jsc.elements(a2zString)),
  696. jsc.nearray(jsc.elements(a2zString)),
  697. jsc.array(jsc.elements(queryString)),
  698. jsc.nearray(jsc.elements(base64String)),
  699. jsc.array(jsc.elements(queryString)),
  700. function (schema, address, query, fragment, trail) {
  701. var fragmentString = fragment.join(''),
  702. clean = jsdom('', {
  703. url: schema.join('') + '://' + address.join('') + '/?' +
  704. query.join('') + '#' + fragmentString + '&' + trail.join('')
  705. }),
  706. result = $.PrivateBin.Model.getPasteKey();
  707. $.PrivateBin.Model.reset();
  708. clean();
  709. return fragmentString === result;
  710. }
  711. );
  712. jsc.property(
  713. 'throws exception on empty fragment of the URL',
  714. jsc.nearray(jsc.elements(a2zString)),
  715. jsc.nearray(jsc.elements(a2zString)),
  716. jsc.array(jsc.elements(queryString)),
  717. function (schema, address, query) {
  718. var clean = jsdom('', {
  719. url: schema.join('') + '://' + address.join('') +
  720. '/?' + query.join('')
  721. }),
  722. result = false;
  723. try {
  724. $.PrivateBin.Model.getPasteKey();
  725. }
  726. catch(err) {
  727. result = true;
  728. }
  729. $.PrivateBin.Model.reset();
  730. clean();
  731. return result;
  732. }
  733. );
  734. });
  735. describe('getTemplate', function () {
  736. before(function () {
  737. $.PrivateBin.Model.reset();
  738. cleanup();
  739. });
  740. jsc.property(
  741. 'returns the contents of the element with id "[name]template"',
  742. jsc.nearray(jsc.elements(alnumString)),
  743. jsc.nearray(jsc.elements(a2zString)),
  744. jsc.nearray(jsc.elements(alnumString)),
  745. function (id, element, value) {
  746. id = id.join('');
  747. element = element.join('');
  748. value = value.join('').trim();
  749. $('body').html(
  750. '<div id="templates"><' + element + ' id="' + id +
  751. 'template">' + value + '</' + element + '></div>'
  752. );
  753. $.PrivateBin.Model.init();
  754. var template = '<' + element + ' id="' + id + '">' + value +
  755. '</' + element + '>',
  756. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  757. $.PrivateBin.Model.reset();
  758. return template === result;
  759. }
  760. );
  761. });
  762. });
  763. describe('UiHelper', function () {
  764. // TODO: As per https://github.com/tmpvar/jsdom/issues/1565 there is no navigation support in jsdom, yet.
  765. // for now we use a mock function to trigger the event
  766. describe('historyChange', function () {
  767. before(function () {
  768. $.PrivateBin.Helper.reset();
  769. });
  770. jsc.property(
  771. 'redirects to home, when the state is null',
  772. jsc.elements(schemas),
  773. jsc.nearray(jsc.elements(a2zString)),
  774. function (schema, address) {
  775. var expected = schema + '://' + address.join('') + '/',
  776. clean = jsdom('', {url: expected});
  777. $.PrivateBin.UiHelper.mockHistoryChange();
  778. var result = window.location.href;
  779. clean();
  780. return expected === result;
  781. }
  782. );
  783. jsc.property(
  784. 'does not redirect to home, when a new paste is created',
  785. jsc.elements(schemas),
  786. jsc.nearray(jsc.elements(a2zString)),
  787. jsc.array(jsc.elements(queryString)),
  788. jsc.nearray(jsc.elements(base64String)),
  789. function (schema, address, query, fragment) {
  790. var expected = schema + '://' + address.join('') + '/' + '?' + query.join('') + '#' + fragment.join(''),
  791. clean = jsdom('', {url: expected});
  792. $.PrivateBin.UiHelper.mockHistoryChange([{type: 'newpaste'}, '', expected]);
  793. var result = window.location.href;
  794. clean();
  795. return expected === result;
  796. }
  797. );
  798. });
  799. });