test.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. 'use strict';
  2. var jsc = require('jsverify'),
  3. jsdom = require('jsdom-global'),
  4. cleanup = jsdom(),
  5. EventEmitter = require('events'),
  6. a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  7. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  8. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  9. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  10. base64String = alnumString.concat(['+','/','=']).concat(
  11. a2zString.map(function(c) {
  12. return c.toUpperCase();
  13. })
  14. ),
  15. // schemas supported by the whatwg-url library
  16. schemas = ['ftp','gopher','http','https','ws','wss'],
  17. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  18. logFile = require('fs').createWriteStream('test.log');
  19. global.$ = global.jQuery = require('./jquery-3.1.1');
  20. global.sjcl = require('./sjcl-1.0.6');
  21. global.Base64 = require('./base64-2.1.9').Base64;
  22. global.RawDeflate = require('./rawdeflate-0.5').RawDeflate;
  23. global.RawDeflate.inflate = require('./rawinflate-0.3').RawDeflate.inflate;
  24. require('./privatebin');
  25. // redirect console messages to log file
  26. console.warn = console.error = function (msg) {
  27. logFile.write(msg + '\n');
  28. }
  29. describe('Helper', function () {
  30. describe('secondsToHuman', function () {
  31. after(function () {
  32. cleanup();
  33. });
  34. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  35. var result = $.PrivateBin.Helper.secondsToHuman(number);
  36. return Array.isArray(result) &&
  37. result.length === 2 &&
  38. result[0] === parseInt(result[0], 10) &&
  39. typeof result[1] === 'string';
  40. });
  41. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  42. return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
  43. });
  44. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  45. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  46. });
  47. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  48. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  49. });
  50. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  51. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  52. });
  53. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  54. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  55. });
  56. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  57. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  58. });
  59. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  60. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  61. });
  62. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  63. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  64. });
  65. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  66. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  67. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  68. });
  69. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  70. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  71. });
  72. });
  73. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  74. // TODO: This needs to be tested using a browser.
  75. describe('selectText', function () {
  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. jsc.property(
  261. 'returns the requested cookie',
  262. 'nearray asciinestring',
  263. 'nearray asciistring',
  264. function (labels, values) {
  265. var selectedKey = '', selectedValue = '',
  266. cookieArray = [],
  267. count = 0;
  268. labels.forEach(function(item, i) {
  269. // deliberatly using a non-ascii key for replacing invalid characters
  270. var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
  271. value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
  272. cookieArray.push(key + '=' + value);
  273. if (Math.random() < 1 / i || selectedKey === key)
  274. {
  275. selectedKey = key;
  276. selectedValue = value;
  277. }
  278. });
  279. var clean = jsdom('', {cookie: cookieArray}),
  280. result = $.PrivateBin.Helper.getCookie(selectedKey);
  281. clean();
  282. return result === selectedValue;
  283. }
  284. );
  285. });
  286. describe('baseUri', function () {
  287. before(function () {
  288. $.PrivateBin.Helper.reset();
  289. });
  290. jsc.property(
  291. 'returns the URL without query & fragment',
  292. jsc.elements(schemas),
  293. jsc.nearray(jsc.elements(a2zString)),
  294. jsc.array(jsc.elements(queryString)),
  295. 'string',
  296. function (schema, address, query, fragment) {
  297. var expected = schema + '://' + address.join('') + '/',
  298. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  299. result = $.PrivateBin.Helper.baseUri();
  300. $.PrivateBin.Helper.reset();
  301. clean();
  302. return expected === result;
  303. }
  304. );
  305. });
  306. describe('htmlEntities', function () {
  307. after(function () {
  308. cleanup();
  309. });
  310. jsc.property(
  311. 'removes all HTML entities from any given string',
  312. 'string',
  313. function (string) {
  314. var result = $.PrivateBin.Helper.htmlEntities(string);
  315. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  316. }
  317. );
  318. });
  319. });
  320. describe('I18n', function () {
  321. describe('translate', function () {
  322. before(function () {
  323. $.PrivateBin.I18n.reset();
  324. });
  325. jsc.property(
  326. 'returns message ID unchanged if no translation found',
  327. 'string',
  328. function (messageId) {
  329. messageId = messageId.replace(/%(s|d)/g, '%%');
  330. var plurals = [messageId, messageId + 's'],
  331. fake = [messageId],
  332. result = $.PrivateBin.I18n.translate(messageId);
  333. $.PrivateBin.I18n.reset();
  334. var alias = $.PrivateBin.I18n._(messageId);
  335. $.PrivateBin.I18n.reset();
  336. var p_result = $.PrivateBin.I18n.translate(plurals);
  337. $.PrivateBin.I18n.reset();
  338. var p_alias = $.PrivateBin.I18n._(plurals);
  339. $.PrivateBin.I18n.reset();
  340. var f_result = $.PrivateBin.I18n.translate(fake);
  341. $.PrivateBin.I18n.reset();
  342. var f_alias = $.PrivateBin.I18n._(fake);
  343. $.PrivateBin.I18n.reset();
  344. return messageId === result && messageId === alias &&
  345. messageId === p_result && messageId === p_alias &&
  346. messageId === f_result && messageId === f_alias;
  347. }
  348. );
  349. jsc.property(
  350. 'replaces %s in strings with first given parameter',
  351. 'string',
  352. '(small nearray) string',
  353. 'string',
  354. function (prefix, params, postfix) {
  355. prefix = prefix.replace(/%(s|d)/g, '%%');
  356. params[0] = params[0].replace(/%(s|d)/g, '%%');
  357. postfix = postfix.replace(/%(s|d)/g, '%%');
  358. var translation = prefix + params[0] + postfix;
  359. params.unshift(prefix + '%s' + postfix);
  360. var result = $.PrivateBin.I18n.translate.apply(this, params);
  361. $.PrivateBin.I18n.reset();
  362. var alias = $.PrivateBin.I18n._.apply(this, params);
  363. $.PrivateBin.I18n.reset();
  364. return translation === result && translation === alias;
  365. }
  366. );
  367. });
  368. describe('getPluralForm', function () {
  369. before(function () {
  370. $.PrivateBin.I18n.reset();
  371. });
  372. jsc.property(
  373. 'returns valid key for plural form',
  374. jsc.elements(supportedLanguages),
  375. 'integer',
  376. function(language, n) {
  377. $.PrivateBin.I18n.reset(language);
  378. var result = $.PrivateBin.I18n.getPluralForm(n);
  379. // arabic seems to have the highest plural count with 6 forms
  380. return result >= 0 && result <= 5;
  381. }
  382. );
  383. });
  384. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  385. // TODO: This needs to be tested using a browser.
  386. describe('loadTranslations', function () {
  387. before(function () {
  388. $.PrivateBin.I18n.reset();
  389. });
  390. jsc.property(
  391. 'downloads and handles any supported language',
  392. jsc.elements(supportedLanguages),
  393. function(language) {
  394. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  395. $.PrivateBin.I18n.reset('en');
  396. $.PrivateBin.I18n.loadTranslations();
  397. $.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
  398. var result = $.PrivateBin.I18n.translate('en'),
  399. alias = $.PrivateBin.I18n._('en');
  400. clean();
  401. return language === result && language === alias;
  402. }
  403. );
  404. });
  405. });
  406. describe('CryptTool', function () {
  407. describe('cipher & decipher', function () {
  408. this.timeout(30000);
  409. it('can en- and decrypt any message', function () {
  410. jsc.check(jsc.forall(
  411. 'string',
  412. 'string',
  413. 'string',
  414. function (key, password, message) {
  415. return message === $.PrivateBin.CryptTool.decipher(
  416. key,
  417. password,
  418. $.PrivateBin.CryptTool.cipher(key, password, message)
  419. );
  420. }
  421. ),
  422. // reducing amount of checks as running 100 takes about 5 minutes
  423. {tests: 5, quiet: true});
  424. });
  425. // The below static unit tests are included to ensure deciphering of "classic"
  426. // SJCL based pastes still works
  427. it(
  428. 'supports PrivateBin v1 ciphertext (SJCL & Base64 2.1.9)',
  429. function () {
  430. // Of course you can easily decipher the following texts, if you like.
  431. // Bonus points for finding their sources and hidden meanings.
  432. var paste1 = $.PrivateBin.CryptTool.decipher(
  433. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  434. // -- "That's amazing. I've got the same combination on my luggage."
  435. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  436. '{"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="}'
  437. ),
  438. paste2 = $.PrivateBin.CryptTool.decipher(
  439. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  440. '', // no password
  441. '{"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"}'
  442. );
  443. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  444. throw Error('v1 (SJCL based) pastes could not be deciphered');
  445. }
  446. }
  447. );
  448. it(
  449. 'supports ZeroBin ciphertext (SJCL & Base64 1.7)',
  450. function () {
  451. var newBase64 = global.Base64;
  452. global.Base64 = require('./base64-1.7').Base64;
  453. jsdom();
  454. delete require.cache[require.resolve('./privatebin')];
  455. require('./privatebin');
  456. // Of course you can easily decipher the following texts, if you like.
  457. // Bonus points for finding their sources and hidden meanings.
  458. var paste1 = $.PrivateBin.CryptTool.decipher(
  459. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  460. // -- "That's amazing. I've got the same combination on my luggage."
  461. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  462. '{"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=="}'
  463. ),
  464. paste2 = $.PrivateBin.CryptTool.decipher(
  465. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  466. '', // no password
  467. '{"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="}'
  468. );
  469. global.Base64 = newBase64;
  470. jsdom();
  471. delete require.cache[require.resolve('./privatebin')];
  472. require('./privatebin');
  473. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  474. throw Error('v1 (SJCL based) pastes could not be deciphered');
  475. }
  476. }
  477. );
  478. });
  479. describe('isEntropyReady & addEntropySeedListener', function () {
  480. it(
  481. 'lets us know that enough entropy is collected or make us wait for it',
  482. function(done) {
  483. if ($.PrivateBin.CryptTool.isEntropyReady()) {
  484. done();
  485. } else {
  486. $.PrivateBin.CryptTool.addEntropySeedListener(function() {
  487. done();
  488. });
  489. }
  490. }
  491. );
  492. });
  493. describe('getSymmetricKey', function () {
  494. var keys = [];
  495. // the parameter is used to ensure the test is run more then one time
  496. jsc.property(
  497. 'returns random, non-empty keys',
  498. 'nat',
  499. function(n) {
  500. var key = $.PrivateBin.CryptTool.getSymmetricKey(),
  501. result = (key !== '' && keys.indexOf(key) === -1);
  502. keys.push(key);
  503. return result;
  504. }
  505. );
  506. });
  507. describe('Base64.js vs SJCL.js vs abab.js', function () {
  508. jsc.property(
  509. 'these all return the same base64 string',
  510. 'string',
  511. function(string) {
  512. var base64 = Base64.toBase64(string),
  513. sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)),
  514. abab = window.btoa(Base64.utob(string));
  515. return base64 === sjcl && sjcl === abab;
  516. }
  517. );
  518. });
  519. });
  520. describe('Model', function () {
  521. describe('getExpirationDefault', function () {
  522. before(function () {
  523. $.PrivateBin.Model.reset();
  524. cleanup();
  525. });
  526. jsc.property(
  527. 'returns the contents of the element with id "pasteExpiration"',
  528. 'array asciinestring',
  529. 'string',
  530. 'small nat',
  531. function (keys, value, key) {
  532. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  533. value = $.PrivateBin.Helper.htmlEntities(value);
  534. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  535. contents = '<select id="pasteExpiration" name="pasteExpiration">';
  536. keys.forEach(function(item) {
  537. contents += '<option value="' + item + '"';
  538. if (item === content) {
  539. contents += ' selected="selected"';
  540. }
  541. contents += '>' + value + '</option>';
  542. });
  543. contents += '</select>';
  544. $('body').html(contents);
  545. var result = $.PrivateBin.Helper.htmlEntities(
  546. $.PrivateBin.Model.getExpirationDefault()
  547. );
  548. $.PrivateBin.Model.reset();
  549. return content === result;
  550. }
  551. );
  552. });
  553. describe('getFormatDefault', function () {
  554. before(function () {
  555. $.PrivateBin.Model.reset();
  556. cleanup();
  557. });
  558. jsc.property(
  559. 'returns the contents of the element with id "pasteFormatter"',
  560. 'array asciinestring',
  561. 'string',
  562. 'small nat',
  563. function (keys, value, key) {
  564. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  565. value = $.PrivateBin.Helper.htmlEntities(value);
  566. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  567. contents = '<select id="pasteFormatter" name="pasteFormatter">';
  568. keys.forEach(function(item) {
  569. contents += '<option value="' + item + '"';
  570. if (item === content) {
  571. contents += ' selected="selected"';
  572. }
  573. contents += '>' + value + '</option>';
  574. });
  575. contents += '</select>';
  576. $('body').html(contents);
  577. var result = $.PrivateBin.Helper.htmlEntities(
  578. $.PrivateBin.Model.getFormatDefault()
  579. );
  580. $.PrivateBin.Model.reset();
  581. return content === result;
  582. }
  583. );
  584. });
  585. describe('hasCipherData', function () {
  586. before(function () {
  587. $.PrivateBin.Model.reset();
  588. cleanup();
  589. });
  590. jsc.property(
  591. 'checks if the element with id "cipherdata" contains any data',
  592. 'asciistring',
  593. function (value) {
  594. value = $.PrivateBin.Helper.htmlEntities(value).trim();
  595. $('body').html('<div id="cipherdata">' + value + '</div>');
  596. $.PrivateBin.Model.init();
  597. var result = $.PrivateBin.Model.hasCipherData();
  598. $.PrivateBin.Model.reset();
  599. return (value.length > 0) === result;
  600. }
  601. );
  602. });
  603. describe('getCipherData', function () {
  604. before(function () {
  605. $.PrivateBin.Model.reset();
  606. cleanup();
  607. });
  608. jsc.property(
  609. 'returns the contents of the element with id "cipherdata"',
  610. 'asciistring',
  611. function (value) {
  612. value = $.PrivateBin.Helper.htmlEntities(value).trim();
  613. $('body').html('<div id="cipherdata">' + value + '</div>');
  614. $.PrivateBin.Model.init();
  615. var result = $.PrivateBin.Helper.htmlEntities(
  616. $.PrivateBin.Model.getCipherData()
  617. );
  618. $.PrivateBin.Model.reset();
  619. return value === result;
  620. }
  621. );
  622. });
  623. describe('getPasteId', function () {
  624. before(function () {
  625. $.PrivateBin.Model.reset();
  626. cleanup();
  627. });
  628. jsc.property(
  629. 'returns the query string without separator, if any',
  630. jsc.nearray(jsc.elements(a2zString)),
  631. jsc.nearray(jsc.elements(a2zString)),
  632. jsc.nearray(jsc.elements(queryString)),
  633. 'string',
  634. function (schema, address, query, fragment) {
  635. var queryString = query.join(''),
  636. clean = jsdom('', {
  637. url: schema.join('') + '://' + address.join('') +
  638. '/?' + queryString + '#' + fragment
  639. }),
  640. result = $.PrivateBin.Model.getPasteId();
  641. $.PrivateBin.Model.reset();
  642. clean();
  643. return queryString === result;
  644. }
  645. );
  646. jsc.property(
  647. 'throws exception on empty query string',
  648. jsc.nearray(jsc.elements(a2zString)),
  649. jsc.nearray(jsc.elements(a2zString)),
  650. 'string',
  651. function (schema, address, fragment) {
  652. var clean = jsdom('', {
  653. url: schema.join('') + '://' + address.join('') +
  654. '/#' + fragment
  655. }),
  656. result = false;
  657. try {
  658. $.PrivateBin.Model.getPasteId();
  659. }
  660. catch(err) {
  661. result = true;
  662. }
  663. $.PrivateBin.Model.reset();
  664. clean();
  665. return result;
  666. }
  667. );
  668. });
  669. describe('getPasteKey', function () {
  670. jsc.property(
  671. 'returns the fragment of the URL',
  672. jsc.nearray(jsc.elements(a2zString)),
  673. jsc.nearray(jsc.elements(a2zString)),
  674. jsc.array(jsc.elements(queryString)),
  675. jsc.nearray(jsc.elements(base64String)),
  676. function (schema, address, query, fragment) {
  677. var fragmentString = fragment.join(''),
  678. clean = jsdom('', {
  679. url: schema.join('') + '://' + address.join('') +
  680. '/?' + query.join('') + '#' + fragmentString
  681. }),
  682. result = $.PrivateBin.Model.getPasteKey();
  683. $.PrivateBin.Model.reset();
  684. clean();
  685. return fragmentString === result;
  686. }
  687. );
  688. jsc.property(
  689. 'returns the fragment stripped of trailing query parts',
  690. jsc.nearray(jsc.elements(a2zString)),
  691. jsc.nearray(jsc.elements(a2zString)),
  692. jsc.array(jsc.elements(queryString)),
  693. jsc.nearray(jsc.elements(base64String)),
  694. jsc.array(jsc.elements(queryString)),
  695. function (schema, address, query, fragment, trail) {
  696. var fragmentString = fragment.join(''),
  697. clean = jsdom('', {
  698. url: schema.join('') + '://' + address.join('') + '/?' +
  699. query.join('') + '#' + fragmentString + '&' + trail.join('')
  700. }),
  701. result = $.PrivateBin.Model.getPasteKey();
  702. $.PrivateBin.Model.reset();
  703. clean();
  704. return fragmentString === result;
  705. }
  706. );
  707. jsc.property(
  708. 'throws exception on empty fragment of the URL',
  709. jsc.nearray(jsc.elements(a2zString)),
  710. jsc.nearray(jsc.elements(a2zString)),
  711. jsc.array(jsc.elements(queryString)),
  712. function (schema, address, query) {
  713. var clean = jsdom('', {
  714. url: schema.join('') + '://' + address.join('') +
  715. '/?' + query.join('')
  716. }),
  717. result = false;
  718. try {
  719. $.PrivateBin.Model.getPasteKey();
  720. }
  721. catch(err) {
  722. result = true;
  723. }
  724. $.PrivateBin.Model.reset();
  725. clean();
  726. return result;
  727. }
  728. );
  729. });
  730. describe('getTemplate', function () {
  731. before(function () {
  732. $.PrivateBin.Model.reset();
  733. cleanup();
  734. });
  735. jsc.property(
  736. 'returns the contents of the element with id "[name]template"',
  737. jsc.nearray(jsc.elements(alnumString)),
  738. jsc.nearray(jsc.elements(a2zString)),
  739. jsc.nearray(jsc.elements(alnumString)),
  740. function (id, element, value) {
  741. id = id.join('');
  742. element = element.join('');
  743. value = value.join('').trim();
  744. $('body').html(
  745. '<div id="templates"><' + element + ' id="' + id +
  746. 'template">' + value + '</' + element + '></div>'
  747. );
  748. $.PrivateBin.Model.init();
  749. var template = '<' + element + ' id="' + id + '">' + value +
  750. '</' + element + '>',
  751. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  752. $.PrivateBin.Model.reset();
  753. return template === result;
  754. }
  755. );
  756. });
  757. });
  758. describe('UiHelper', function () {
  759. describe('historyChange', function () {
  760. before(function () {
  761. $.PrivateBin.Helper.reset();
  762. });
  763. jsc.property(
  764. 'returns the URL without query & fragment',
  765. jsc.elements(schemas),
  766. jsc.nearray(jsc.elements(a2zString)),
  767. jsc.array(jsc.elements(queryString)),
  768. 'string',
  769. function (schema, address, query, fragment) {
  770. var expected = schema + '://' + address.join('') + '/',
  771. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  772. emitter = new EventEmitter();
  773. $.PrivateBin.UiHelper.init();
  774. emitter.emit('popstate');
  775. var result = window.location.href;
  776. clean();
  777. console.log(expected, result);
  778. return expected === result;
  779. }
  780. );
  781. });
  782. });