test.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. 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('CryptTool', function () {
  406. describe('cipher & decipher', function () {
  407. this.timeout(30000);
  408. it('can en- and decrypt any message', function () {
  409. jsc.check(jsc.forall(
  410. 'string',
  411. 'string',
  412. 'string',
  413. function (key, password, message) {
  414. return message === $.PrivateBin.CryptTool.decipher(
  415. key,
  416. password,
  417. $.PrivateBin.CryptTool.cipher(key, password, message)
  418. );
  419. }
  420. ),
  421. // reducing amount of checks as running 100 takes about 5 minutes
  422. {tests: 5, quiet: true});
  423. });
  424. // The below static unit tests are included to ensure deciphering of "classic"
  425. // SJCL based pastes still works
  426. it(
  427. 'supports PrivateBin v1 ciphertext (SJCL & Base64 2.1.9)',
  428. function () {
  429. // Of course you can easily decipher the following texts, if you like.
  430. // Bonus points for finding their sources and hidden meanings.
  431. var paste1 = $.PrivateBin.CryptTool.decipher(
  432. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  433. // -- "That's amazing. I've got the same combination on my luggage."
  434. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  435. '{"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="}'
  436. ),
  437. paste2 = $.PrivateBin.CryptTool.decipher(
  438. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  439. '', // no password
  440. '{"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"}'
  441. );
  442. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  443. throw Error('v1 (SJCL based) pastes could not be deciphered');
  444. }
  445. }
  446. );
  447. it(
  448. 'supports ZeroBin ciphertext (SJCL & Base64 1.7)',
  449. function () {
  450. var newBase64 = global.Base64;
  451. global.Base64 = require('./base64-1.7').Base64;
  452. jsdom();
  453. delete require.cache[require.resolve('./privatebin')];
  454. require('./privatebin');
  455. // Of course you can easily decipher the following texts, if you like.
  456. // Bonus points for finding their sources and hidden meanings.
  457. var paste1 = $.PrivateBin.CryptTool.decipher(
  458. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  459. // -- "That's amazing. I've got the same combination on my luggage."
  460. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  461. '{"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=="}'
  462. ),
  463. paste2 = $.PrivateBin.CryptTool.decipher(
  464. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  465. '', // no password
  466. '{"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="}'
  467. );
  468. global.Base64 = newBase64;
  469. jsdom();
  470. delete require.cache[require.resolve('./privatebin')];
  471. require('./privatebin');
  472. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  473. throw Error('v1 (SJCL based) pastes could not be deciphered');
  474. }
  475. }
  476. );
  477. });
  478. describe('isEntropyReady & addEntropySeedListener', function () {
  479. it(
  480. 'lets us know that enough entropy is collected or make us wait for it',
  481. function(done) {
  482. if ($.PrivateBin.CryptTool.isEntropyReady()) {
  483. done();
  484. } else {
  485. $.PrivateBin.CryptTool.addEntropySeedListener(function() {
  486. done();
  487. });
  488. }
  489. }
  490. );
  491. });
  492. describe('getSymmetricKey', function () {
  493. var keys = [];
  494. // the parameter is used to ensure the test is run more then one time
  495. jsc.property(
  496. 'returns random, non-empty keys',
  497. 'nat',
  498. function(n) {
  499. var key = $.PrivateBin.CryptTool.getSymmetricKey(),
  500. result = (key !== '' && keys.indexOf(key) === -1);
  501. keys.push(key);
  502. return result;
  503. }
  504. );
  505. });
  506. describe('Base64.js vs SJCL.js vs abab.js', function () {
  507. jsc.property(
  508. 'these all return the same base64 string',
  509. 'string',
  510. function(string) {
  511. var base64 = Base64.toBase64(string),
  512. sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)),
  513. abab = window.btoa(Base64.utob(string));
  514. return base64 === sjcl && sjcl === abab;
  515. }
  516. );
  517. });
  518. });
  519. describe('Model', function () {
  520. describe('getPasteId', function () {
  521. before(function () {
  522. $.PrivateBin.Model.reset();
  523. cleanup();
  524. });
  525. jsc.property(
  526. 'returns the query string without separator, if any',
  527. jsc.nearray(jsc.elements(a2zString)),
  528. jsc.nearray(jsc.elements(a2zString)),
  529. jsc.nearray(jsc.elements(queryString)),
  530. 'string',
  531. function (schema, address, query, fragment) {
  532. var queryString = query.join(''),
  533. clean = jsdom('', {
  534. url: schema.join('') + '://' + address.join('') +
  535. '/?' + queryString + '#' + fragment
  536. }),
  537. result = $.PrivateBin.Model.getPasteId();
  538. $.PrivateBin.Model.reset();
  539. clean();
  540. return queryString === result;
  541. }
  542. );
  543. jsc.property(
  544. 'throws exception on empty query string',
  545. jsc.nearray(jsc.elements(a2zString)),
  546. jsc.nearray(jsc.elements(a2zString)),
  547. 'string',
  548. function (schema, address, fragment) {
  549. var clean = jsdom('', {
  550. url: schema.join('') + '://' + address.join('') +
  551. '/#' + fragment
  552. }),
  553. result = false;
  554. try {
  555. $.PrivateBin.Model.getPasteId();
  556. }
  557. catch(err) {
  558. result = true;
  559. }
  560. $.PrivateBin.Model.reset();
  561. clean();
  562. return result;
  563. }
  564. );
  565. });
  566. describe('getPasteKey', function () {
  567. jsc.property(
  568. 'returns the fragment of the URL',
  569. jsc.nearray(jsc.elements(a2zString)),
  570. jsc.nearray(jsc.elements(a2zString)),
  571. jsc.array(jsc.elements(queryString)),
  572. jsc.nearray(jsc.elements(base64String)),
  573. function (schema, address, query, fragment) {
  574. var fragmentString = fragment.join(''),
  575. clean = jsdom('', {
  576. url: schema.join('') + '://' + address.join('') +
  577. '/?' + query.join('') + '#' + fragmentString
  578. }),
  579. result = $.PrivateBin.Model.getPasteKey();
  580. $.PrivateBin.Model.reset();
  581. clean();
  582. return fragmentString === result;
  583. }
  584. );
  585. jsc.property(
  586. 'returns the fragment stripped of trailing query parts',
  587. jsc.nearray(jsc.elements(a2zString)),
  588. jsc.nearray(jsc.elements(a2zString)),
  589. jsc.array(jsc.elements(queryString)),
  590. jsc.nearray(jsc.elements(base64String)),
  591. jsc.array(jsc.elements(queryString)),
  592. function (schema, address, query, fragment, trail) {
  593. var fragmentString = fragment.join(''),
  594. clean = jsdom('', {
  595. url: schema.join('') + '://' + address.join('') + '/?' +
  596. query.join('') + '#' + fragmentString + '&' + trail.join('')
  597. }),
  598. result = $.PrivateBin.Model.getPasteKey();
  599. $.PrivateBin.Model.reset();
  600. clean();
  601. return fragmentString === result;
  602. }
  603. );
  604. jsc.property(
  605. 'throws exception on empty fragment of the URL',
  606. jsc.nearray(jsc.elements(a2zString)),
  607. jsc.nearray(jsc.elements(a2zString)),
  608. jsc.array(jsc.elements(queryString)),
  609. function (schema, address, query) {
  610. var clean = jsdom('', {
  611. url: schema.join('') + '://' + address.join('') +
  612. '/?' + query.join('')
  613. }),
  614. result = false;
  615. try {
  616. $.PrivateBin.Model.getPasteKey();
  617. }
  618. catch(err) {
  619. result = true;
  620. }
  621. $.PrivateBin.Model.reset();
  622. clean();
  623. return result;
  624. }
  625. );
  626. });
  627. });