test.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. 'use strict';
  2. var jsc = require('jsverify'),
  3. jsdom = require('jsdom-global'),
  4. cleanup = jsdom(),
  5. base64lib = require('./base64-2.1.9'),
  6. rawdeflatelib = require('./rawdeflate-0.5'),
  7. rawinflatelib = require('./rawinflate-0.3'),
  8. a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  9. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  10. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  11. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  12. base64String = alnumString.concat(['+','/','=']).concat(
  13. a2zString.map(function(c) {
  14. return c.toUpperCase();
  15. })
  16. ),
  17. // schemas supported by the whatwg-url library
  18. schemas = ['ftp','gopher','http','https','ws','wss'],
  19. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  20. logFile = require('fs').createWriteStream('test.log');
  21. global.$ = global.jQuery = require('./jquery-3.1.1');
  22. global.sjcl = require('./sjcl-1.0.6');
  23. global.Base64 = base64lib.Base64;
  24. global.RawDeflate = rawdeflatelib.RawDeflate;
  25. global.RawDeflate.inflate = rawinflatelib.RawDeflate.inflate;
  26. require('./privatebin');
  27. // redirect console messages to log file
  28. console.warn = console.error = function (msg) {
  29. logFile.write(msg + '\n');
  30. }
  31. describe('Helper', function () {
  32. describe('secondsToHuman', function () {
  33. after(function () {
  34. cleanup();
  35. });
  36. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  37. var result = $.PrivateBin.Helper.secondsToHuman(number);
  38. return Array.isArray(result) &&
  39. result.length === 2 &&
  40. result[0] === parseInt(result[0], 10) &&
  41. typeof result[1] === 'string';
  42. });
  43. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  44. return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
  45. });
  46. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  47. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  48. });
  49. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  50. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  51. });
  52. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  53. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  54. });
  55. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  56. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  57. });
  58. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  59. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  60. });
  61. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  62. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  63. });
  64. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  65. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  66. });
  67. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  68. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  69. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  70. });
  71. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  72. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  73. });
  74. });
  75. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  76. // TODO: This needs to be tested using a browser.
  77. describe('selectText', function () {
  78. jsc.property(
  79. 'selection contains content of given ID',
  80. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  81. 'nearray string',
  82. function (ids, contents) {
  83. var html = '',
  84. result = true;
  85. ids.forEach(function(item, i) {
  86. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  87. });
  88. var clean = jsdom(html);
  89. ids.forEach(function(item, i) {
  90. $.PrivateBin.Helper.selectText(item.join(''));
  91. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  92. // Once there is one, uncomment the line below to actually check the result.
  93. //result *= (contents[i] || contents[0]) === window.getSelection().toString();
  94. });
  95. clean();
  96. return Boolean(result);
  97. }
  98. );
  99. });
  100. describe('setElementText', function () {
  101. after(function () {
  102. cleanup();
  103. });
  104. jsc.property(
  105. 'replaces the content of an element',
  106. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  107. 'nearray string',
  108. 'string',
  109. function (ids, contents, replacingContent) {
  110. var html = '',
  111. result = true;
  112. ids.forEach(function(item, i) {
  113. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  114. });
  115. var elements = $('<body />').html(html);
  116. ids.forEach(function(item, i) {
  117. var id = item.join(''),
  118. element = elements.find('#' + id).first();
  119. $.PrivateBin.Helper.setElementText(element, replacingContent);
  120. result *= replacingContent === element.text();
  121. });
  122. return Boolean(result);
  123. }
  124. );
  125. });
  126. describe('urls2links', function () {
  127. after(function () {
  128. cleanup();
  129. });
  130. jsc.property(
  131. 'ignores non-URL content',
  132. 'string',
  133. function (content) {
  134. var element = $('<div>' + content + '</div>'),
  135. before = element.html();
  136. $.PrivateBin.Helper.urls2links(element);
  137. return before === element.html();
  138. }
  139. );
  140. jsc.property(
  141. 'replaces URLs with anchors',
  142. 'string',
  143. jsc.elements(['http', 'https', 'ftp']),
  144. jsc.nearray(jsc.elements(a2zString)),
  145. jsc.array(jsc.elements(queryString)),
  146. jsc.array(jsc.elements(queryString)),
  147. 'string',
  148. function (prefix, schema, address, query, fragment, postfix) {
  149. var query = query.join(''),
  150. fragment = fragment.join(''),
  151. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  152. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  153. postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix),
  154. element = $('<div>' + prefix + url + postfix + '</div>');
  155. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  156. if (
  157. query.slice(-1) === '&' &&
  158. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  159. )
  160. {
  161. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  162. postfix = '';
  163. element = $('<div>' + prefix + url + '</div>');
  164. }
  165. $.PrivateBin.Helper.urls2links(element);
  166. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix + '</div>').html();
  167. }
  168. );
  169. jsc.property(
  170. 'replaces magnet links with anchors',
  171. 'string',
  172. jsc.array(jsc.elements(queryString)),
  173. 'string',
  174. function (prefix, query, postfix) {
  175. var url = 'magnet:?' + query.join(''),
  176. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  177. postfix = $.PrivateBin.Helper.htmlEntities(postfix),
  178. element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
  179. $.PrivateBin.Helper.urls2links(element);
  180. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
  181. }
  182. );
  183. });
  184. describe('sprintf', function () {
  185. after(function () {
  186. cleanup();
  187. });
  188. jsc.property(
  189. 'replaces %s in strings with first given parameter',
  190. 'string',
  191. '(small nearray) string',
  192. 'string',
  193. function (prefix, params, postfix) {
  194. prefix = prefix.replace(/%(s|d)/g, '%%');
  195. params[0] = params[0].replace(/%(s|d)/g, '%%');
  196. postfix = postfix.replace(/%(s|d)/g, '%%');
  197. var result = prefix + params[0] + postfix;
  198. params.unshift(prefix + '%s' + postfix);
  199. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  200. }
  201. );
  202. jsc.property(
  203. 'replaces %d in strings with first given parameter',
  204. 'string',
  205. '(small nearray) nat',
  206. 'string',
  207. function (prefix, params, postfix) {
  208. prefix = prefix.replace(/%(s|d)/g, '%%');
  209. postfix = postfix.replace(/%(s|d)/g, '%%');
  210. var result = prefix + params[0] + postfix;
  211. params.unshift(prefix + '%d' + postfix);
  212. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  213. }
  214. );
  215. jsc.property(
  216. 'replaces %d in strings with 0 if first parameter is not a number',
  217. 'string',
  218. '(small nearray) falsy',
  219. 'string',
  220. function (prefix, params, postfix) {
  221. prefix = prefix.replace(/%(s|d)/g, '%%');
  222. postfix = postfix.replace(/%(s|d)/g, '%%');
  223. var result = prefix + '0' + postfix;
  224. params.unshift(prefix + '%d' + postfix);
  225. return result === $.PrivateBin.Helper.sprintf.apply(this, params)
  226. }
  227. );
  228. jsc.property(
  229. 'replaces %d and %s in strings in order',
  230. 'string',
  231. 'nat',
  232. 'string',
  233. 'string',
  234. 'string',
  235. function (prefix, uint, middle, string, postfix) {
  236. prefix = prefix.replace(/%(s|d)/g, '%%');
  237. middle = middle.replace(/%(s|d)/g, '%%');
  238. postfix = postfix.replace(/%(s|d)/g, '%%');
  239. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  240. result = prefix + uint + middle + string + postfix;
  241. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  242. }
  243. );
  244. jsc.property(
  245. 'replaces %d and %s in strings in reverse order',
  246. 'string',
  247. 'nat',
  248. 'string',
  249. 'string',
  250. 'string',
  251. function (prefix, uint, middle, string, postfix) {
  252. prefix = prefix.replace(/%(s|d)/g, '%%');
  253. middle = middle.replace(/%(s|d)/g, '%%');
  254. postfix = postfix.replace(/%(s|d)/g, '%%');
  255. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  256. result = prefix + string + middle + uint + postfix;
  257. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  258. }
  259. );
  260. });
  261. describe('getCookie', function () {
  262. jsc.property(
  263. 'returns the requested cookie',
  264. 'nearray asciinestring',
  265. 'nearray asciistring',
  266. function (labels, values) {
  267. var selectedKey = '', selectedValue = '',
  268. cookieArray = [],
  269. count = 0;
  270. labels.forEach(function(item, i) {
  271. // deliberatly using a non-ascii key for replacing invalid characters
  272. var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
  273. value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
  274. cookieArray.push(key + '=' + value);
  275. if (Math.random() < 1 / i || selectedKey === key)
  276. {
  277. selectedKey = key;
  278. selectedValue = value;
  279. }
  280. });
  281. var clean = jsdom('', {cookie: cookieArray}),
  282. result = $.PrivateBin.Helper.getCookie(selectedKey);
  283. clean();
  284. return result === selectedValue;
  285. }
  286. );
  287. });
  288. describe('baseUri', function () {
  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. before(function () {
  390. $.PrivateBin.I18n.reset();
  391. });
  392. jsc.property(
  393. 'downloads and handles any supported language',
  394. jsc.elements(supportedLanguages),
  395. function(language) {
  396. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  397. $.PrivateBin.I18n.reset('en');
  398. $.PrivateBin.I18n.loadTranslations();
  399. $.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
  400. var result = $.PrivateBin.I18n.translate('en'),
  401. alias = $.PrivateBin.I18n._('en');
  402. clean();
  403. return language === result && language === alias;
  404. }
  405. );
  406. });
  407. });
  408. describe('CryptTool', function () {
  409. describe('cipher & decipher', function () {
  410. this.timeout(20000);
  411. it('can en- and decrypt any message', function () {
  412. jsc.check(jsc.forall(
  413. 'string',
  414. 'string',
  415. 'string',
  416. function (key, password, message) {
  417. return message === $.PrivateBin.CryptTool.decipher(
  418. key,
  419. password,
  420. $.PrivateBin.CryptTool.cipher(key, password, message)
  421. );
  422. }
  423. ),
  424. // reducing amount of checks as running 100 takes about 5 minutes
  425. {tests: 5, quiet: true});
  426. });
  427. // The below static unit test is included to ensure deciphering of "classic"
  428. // SJCL based pastes still works
  429. it('supports v1 ciphertext (SJCL)', 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. });
  449. describe('Model', function () {
  450. describe('getPasteId', function () {
  451. before(function () {
  452. $.PrivateBin.Model.reset();
  453. });
  454. jsc.property(
  455. 'returns the query string without separator, if any',
  456. jsc.nearray(jsc.elements(a2zString)),
  457. jsc.nearray(jsc.elements(a2zString)),
  458. jsc.nearray(jsc.elements(queryString)),
  459. 'string',
  460. function (schema, address, query, fragment) {
  461. var queryString = query.join(''),
  462. clean = jsdom('', {
  463. url: schema.join('') + '://' + address.join('') +
  464. '/?' + queryString + '#' + fragment
  465. }),
  466. result = $.PrivateBin.Model.getPasteId();
  467. $.PrivateBin.Model.reset();
  468. clean();
  469. return queryString === result;
  470. }
  471. );
  472. jsc.property(
  473. 'throws exception on empty query string',
  474. jsc.nearray(jsc.elements(a2zString)),
  475. jsc.nearray(jsc.elements(a2zString)),
  476. 'string',
  477. function (schema, address, fragment) {
  478. var clean = jsdom('', {
  479. url: schema.join('') + '://' + address.join('') +
  480. '/#' + fragment
  481. }),
  482. result = false;
  483. try {
  484. $.PrivateBin.Model.getPasteId();
  485. }
  486. catch(err) {
  487. result = true;
  488. }
  489. $.PrivateBin.Model.reset();
  490. clean();
  491. return result;
  492. }
  493. );
  494. });
  495. describe('getPasteKey', function () {
  496. jsc.property(
  497. 'returns the fragment of the URL',
  498. jsc.nearray(jsc.elements(a2zString)),
  499. jsc.nearray(jsc.elements(a2zString)),
  500. jsc.array(jsc.elements(queryString)),
  501. jsc.nearray(jsc.elements(base64String)),
  502. function (schema, address, query, fragment) {
  503. var fragmentString = fragment.join(''),
  504. clean = jsdom('', {
  505. url: schema.join('') + '://' + address.join('') +
  506. '/?' + query.join('') + '#' + fragmentString
  507. }),
  508. result = $.PrivateBin.Model.getPasteKey();
  509. $.PrivateBin.Model.reset();
  510. clean();
  511. return fragmentString === result;
  512. }
  513. );
  514. jsc.property(
  515. 'returns the fragment stripped of trailing query parts',
  516. jsc.nearray(jsc.elements(a2zString)),
  517. jsc.nearray(jsc.elements(a2zString)),
  518. jsc.array(jsc.elements(queryString)),
  519. jsc.nearray(jsc.elements(base64String)),
  520. jsc.array(jsc.elements(queryString)),
  521. function (schema, address, query, fragment, trail) {
  522. var fragmentString = fragment.join(''),
  523. clean = jsdom('', {
  524. url: schema.join('') + '://' + address.join('') + '/?' +
  525. query.join('') + '#' + fragmentString + '&' + trail.join('')
  526. }),
  527. result = $.PrivateBin.Model.getPasteKey();
  528. $.PrivateBin.Model.reset();
  529. clean();
  530. return fragmentString === result;
  531. }
  532. );
  533. jsc.property(
  534. 'throws exception on empty fragment of the URL',
  535. jsc.nearray(jsc.elements(a2zString)),
  536. jsc.nearray(jsc.elements(a2zString)),
  537. jsc.array(jsc.elements(queryString)),
  538. function (schema, address, query) {
  539. var clean = jsdom('', {
  540. url: schema.join('') + '://' + address.join('') +
  541. '/?' + query.join('')
  542. }),
  543. result = false;
  544. try {
  545. $.PrivateBin.Model.getPasteKey();
  546. }
  547. catch(err) {
  548. result = true;
  549. }
  550. $.PrivateBin.Model.reset();
  551. clean();
  552. return result;
  553. }
  554. );
  555. });
  556. });