test.js 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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('./prettify');
  24. global.prettyPrint = window.PR.prettyPrint;
  25. global.prettyPrintOne = window.PR.prettyPrintOne;
  26. global.showdown = require('./showdown-1.6.1');
  27. require('./bootstrap-3.3.7');
  28. require('./privatebin');
  29. // redirect console messages to log file
  30. console.info = console.warn = console.error = function () {
  31. logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
  32. }
  33. describe('Helper', function () {
  34. describe('secondsToHuman', function () {
  35. after(function () {
  36. cleanup();
  37. });
  38. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  39. var result = $.PrivateBin.Helper.secondsToHuman(number);
  40. return Array.isArray(result) &&
  41. result.length === 2 &&
  42. result[0] === parseInt(result[0], 10) &&
  43. typeof result[1] === 'string';
  44. });
  45. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  46. return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
  47. });
  48. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  49. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  50. });
  51. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  52. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  53. });
  54. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  55. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  56. });
  57. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  58. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  59. });
  60. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  61. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  62. });
  63. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  64. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  65. });
  66. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  67. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  68. });
  69. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  70. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  71. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  72. });
  73. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  74. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  75. });
  76. });
  77. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  78. // TODO: This needs to be tested using a browser.
  79. describe('selectText', function () {
  80. this.timeout(30000);
  81. jsc.property(
  82. 'selection contains content of given ID',
  83. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  84. 'nearray string',
  85. function (ids, contents) {
  86. var html = '',
  87. result = true;
  88. ids.forEach(function(item, i) {
  89. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  90. });
  91. var clean = jsdom(html);
  92. ids.forEach(function(item, i) {
  93. $.PrivateBin.Helper.selectText(item.join(''));
  94. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  95. // Once there is one, uncomment the line below to actually check the result.
  96. //result *= (contents[i] || contents[0]) === window.getSelection().toString();
  97. });
  98. clean();
  99. return Boolean(result);
  100. }
  101. );
  102. });
  103. describe('setElementText', function () {
  104. after(function () {
  105. cleanup();
  106. });
  107. jsc.property(
  108. 'replaces the content of an element',
  109. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  110. 'nearray string',
  111. 'string',
  112. function (ids, contents, replacingContent) {
  113. var html = '',
  114. result = true;
  115. ids.forEach(function(item, i) {
  116. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  117. });
  118. var elements = $('<body />').html(html);
  119. ids.forEach(function(item, i) {
  120. var id = item.join(''),
  121. element = elements.find('#' + id).first();
  122. $.PrivateBin.Helper.setElementText(element, replacingContent);
  123. result *= replacingContent === element.text();
  124. });
  125. return Boolean(result);
  126. }
  127. );
  128. });
  129. describe('urls2links', function () {
  130. after(function () {
  131. cleanup();
  132. });
  133. jsc.property(
  134. 'ignores non-URL content',
  135. 'string',
  136. function (content) {
  137. var element = $('<div>' + content + '</div>'),
  138. before = element.html();
  139. $.PrivateBin.Helper.urls2links(element);
  140. return before === element.html();
  141. }
  142. );
  143. jsc.property(
  144. 'replaces URLs with anchors',
  145. 'string',
  146. jsc.elements(['http', 'https', 'ftp']),
  147. jsc.nearray(jsc.elements(a2zString)),
  148. jsc.array(jsc.elements(queryString)),
  149. jsc.array(jsc.elements(queryString)),
  150. 'string',
  151. function (prefix, schema, address, query, fragment, postfix) {
  152. var query = query.join(''),
  153. fragment = fragment.join(''),
  154. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  155. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  156. postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix),
  157. element = $('<div>' + prefix + url + postfix + '</div>');
  158. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  159. if (
  160. query.slice(-1) === '&' &&
  161. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  162. )
  163. {
  164. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  165. postfix = '';
  166. element = $('<div>' + prefix + url + '</div>');
  167. }
  168. $.PrivateBin.Helper.urls2links(element);
  169. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix + '</div>').html();
  170. }
  171. );
  172. jsc.property(
  173. 'replaces magnet links with anchors',
  174. 'string',
  175. jsc.array(jsc.elements(queryString)),
  176. 'string',
  177. function (prefix, query, postfix) {
  178. var url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''),
  179. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  180. postfix = $.PrivateBin.Helper.htmlEntities(postfix),
  181. element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
  182. $.PrivateBin.Helper.urls2links(element);
  183. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
  184. }
  185. );
  186. });
  187. describe('sprintf', function () {
  188. after(function () {
  189. cleanup();
  190. });
  191. jsc.property(
  192. 'replaces %s in strings with first given parameter',
  193. 'string',
  194. '(small nearray) string',
  195. 'string',
  196. function (prefix, params, postfix) {
  197. prefix = prefix.replace(/%(s|d)/g, '%%');
  198. params[0] = params[0].replace(/%(s|d)/g, '%%');
  199. postfix = postfix.replace(/%(s|d)/g, '%%');
  200. var result = prefix + params[0] + postfix;
  201. params.unshift(prefix + '%s' + postfix);
  202. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  203. }
  204. );
  205. jsc.property(
  206. 'replaces %d in strings with first given parameter',
  207. 'string',
  208. '(small nearray) nat',
  209. 'string',
  210. function (prefix, params, postfix) {
  211. prefix = prefix.replace(/%(s|d)/g, '%%');
  212. postfix = postfix.replace(/%(s|d)/g, '%%');
  213. var result = prefix + params[0] + postfix;
  214. params.unshift(prefix + '%d' + postfix);
  215. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  216. }
  217. );
  218. jsc.property(
  219. 'replaces %d in strings with 0 if first parameter is not a number',
  220. 'string',
  221. '(small nearray) falsy',
  222. 'string',
  223. function (prefix, params, postfix) {
  224. prefix = prefix.replace(/%(s|d)/g, '%%');
  225. postfix = postfix.replace(/%(s|d)/g, '%%');
  226. var result = prefix + '0' + postfix;
  227. params.unshift(prefix + '%d' + postfix);
  228. return result === $.PrivateBin.Helper.sprintf.apply(this, params)
  229. }
  230. );
  231. jsc.property(
  232. 'replaces %d and %s in strings in order',
  233. 'string',
  234. 'nat',
  235. 'string',
  236. 'string',
  237. 'string',
  238. function (prefix, uint, middle, string, postfix) {
  239. prefix = prefix.replace(/%(s|d)/g, '%%');
  240. middle = middle.replace(/%(s|d)/g, '%%');
  241. postfix = postfix.replace(/%(s|d)/g, '%%');
  242. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  243. result = prefix + uint + middle + string + postfix;
  244. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  245. }
  246. );
  247. jsc.property(
  248. 'replaces %d and %s in strings in reverse order',
  249. 'string',
  250. 'nat',
  251. 'string',
  252. 'string',
  253. 'string',
  254. function (prefix, uint, middle, string, postfix) {
  255. prefix = prefix.replace(/%(s|d)/g, '%%');
  256. middle = middle.replace(/%(s|d)/g, '%%');
  257. postfix = postfix.replace(/%(s|d)/g, '%%');
  258. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  259. result = prefix + string + middle + uint + postfix;
  260. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  261. }
  262. );
  263. });
  264. describe('getCookie', function () {
  265. this.timeout(30000);
  266. jsc.property(
  267. 'returns the requested cookie',
  268. 'nearray asciinestring',
  269. 'nearray asciistring',
  270. function (labels, values) {
  271. var selectedKey = '', selectedValue = '',
  272. cookieArray = [],
  273. count = 0;
  274. labels.forEach(function(item, i) {
  275. // deliberatly using a non-ascii key for replacing invalid characters
  276. var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
  277. value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
  278. cookieArray.push(key + '=' + value);
  279. if (Math.random() < 1 / i || selectedKey === key)
  280. {
  281. selectedKey = key;
  282. selectedValue = value;
  283. }
  284. });
  285. var clean = jsdom('', {cookie: cookieArray}),
  286. result = $.PrivateBin.Helper.getCookie(selectedKey);
  287. clean();
  288. return result === selectedValue;
  289. }
  290. );
  291. });
  292. describe('baseUri', function () {
  293. this.timeout(30000);
  294. before(function () {
  295. $.PrivateBin.Helper.reset();
  296. });
  297. jsc.property(
  298. 'returns the URL without query & fragment',
  299. jsc.elements(schemas),
  300. jsc.nearray(jsc.elements(a2zString)),
  301. jsc.array(jsc.elements(queryString)),
  302. 'string',
  303. function (schema, address, query, fragment) {
  304. var expected = schema + '://' + address.join('') + '/',
  305. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  306. result = $.PrivateBin.Helper.baseUri();
  307. $.PrivateBin.Helper.reset();
  308. clean();
  309. return expected === result;
  310. }
  311. );
  312. });
  313. describe('htmlEntities', function () {
  314. after(function () {
  315. cleanup();
  316. });
  317. jsc.property(
  318. 'removes all HTML entities from any given string',
  319. 'string',
  320. function (string) {
  321. var result = $.PrivateBin.Helper.htmlEntities(string);
  322. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  323. }
  324. );
  325. });
  326. });
  327. describe('I18n', function () {
  328. describe('translate', function () {
  329. before(function () {
  330. $.PrivateBin.I18n.reset();
  331. });
  332. jsc.property(
  333. 'returns message ID unchanged if no translation found',
  334. 'string',
  335. function (messageId) {
  336. messageId = messageId.replace(/%(s|d)/g, '%%');
  337. var plurals = [messageId, messageId + 's'],
  338. fake = [messageId],
  339. result = $.PrivateBin.I18n.translate(messageId);
  340. $.PrivateBin.I18n.reset();
  341. var alias = $.PrivateBin.I18n._(messageId);
  342. $.PrivateBin.I18n.reset();
  343. var p_result = $.PrivateBin.I18n.translate(plurals);
  344. $.PrivateBin.I18n.reset();
  345. var p_alias = $.PrivateBin.I18n._(plurals);
  346. $.PrivateBin.I18n.reset();
  347. var f_result = $.PrivateBin.I18n.translate(fake);
  348. $.PrivateBin.I18n.reset();
  349. var f_alias = $.PrivateBin.I18n._(fake);
  350. $.PrivateBin.I18n.reset();
  351. return messageId === result && messageId === alias &&
  352. messageId === p_result && messageId === p_alias &&
  353. messageId === f_result && messageId === f_alias;
  354. }
  355. );
  356. jsc.property(
  357. 'replaces %s in strings with first given parameter',
  358. 'string',
  359. '(small nearray) string',
  360. 'string',
  361. function (prefix, params, postfix) {
  362. prefix = prefix.replace(/%(s|d)/g, '%%');
  363. params[0] = params[0].replace(/%(s|d)/g, '%%');
  364. postfix = postfix.replace(/%(s|d)/g, '%%');
  365. var translation = prefix + params[0] + postfix;
  366. params.unshift(prefix + '%s' + postfix);
  367. var result = $.PrivateBin.I18n.translate.apply(this, params);
  368. $.PrivateBin.I18n.reset();
  369. var alias = $.PrivateBin.I18n._.apply(this, params);
  370. $.PrivateBin.I18n.reset();
  371. return translation === result && translation === alias;
  372. }
  373. );
  374. });
  375. describe('getPluralForm', function () {
  376. before(function () {
  377. $.PrivateBin.I18n.reset();
  378. });
  379. jsc.property(
  380. 'returns valid key for plural form',
  381. jsc.elements(supportedLanguages),
  382. 'integer',
  383. function(language, n) {
  384. $.PrivateBin.I18n.reset(language);
  385. var result = $.PrivateBin.I18n.getPluralForm(n);
  386. // arabic seems to have the highest plural count with 6 forms
  387. return result >= 0 && result <= 5;
  388. }
  389. );
  390. });
  391. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  392. // TODO: This needs to be tested using a browser.
  393. describe('loadTranslations', function () {
  394. this.timeout(30000);
  395. before(function () {
  396. $.PrivateBin.I18n.reset();
  397. });
  398. jsc.property(
  399. 'downloads and handles any supported language',
  400. jsc.elements(supportedLanguages),
  401. function(language) {
  402. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  403. $.PrivateBin.I18n.reset('en');
  404. $.PrivateBin.I18n.loadTranslations();
  405. $.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
  406. var result = $.PrivateBin.I18n.translate('en'),
  407. alias = $.PrivateBin.I18n._('en');
  408. clean();
  409. return language === result && language === alias;
  410. }
  411. );
  412. });
  413. });
  414. describe('CryptTool', function () {
  415. describe('cipher & decipher', function () {
  416. this.timeout(30000);
  417. it('can en- and decrypt any message', function () {
  418. jsc.check(jsc.forall(
  419. 'string',
  420. 'string',
  421. 'string',
  422. function (key, password, message) {
  423. return message === $.PrivateBin.CryptTool.decipher(
  424. key,
  425. password,
  426. $.PrivateBin.CryptTool.cipher(key, password, message)
  427. );
  428. }
  429. ),
  430. // reducing amount of checks as running 100 takes about 5 minutes
  431. {tests: 5, quiet: true});
  432. });
  433. // The below static unit tests are included to ensure deciphering of "classic"
  434. // SJCL based pastes still works
  435. it(
  436. 'supports PrivateBin v1 ciphertext (SJCL & Base64 2.1.9)',
  437. function () {
  438. // Of course you can easily decipher the following texts, if you like.
  439. // Bonus points for finding their sources and hidden meanings.
  440. var paste1 = $.PrivateBin.CryptTool.decipher(
  441. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  442. // -- "That's amazing. I've got the same combination on my luggage."
  443. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  444. '{"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="}'
  445. ),
  446. paste2 = $.PrivateBin.CryptTool.decipher(
  447. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  448. '', // no password
  449. '{"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"}'
  450. );
  451. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  452. throw Error('v1 (SJCL based) pastes could not be deciphered');
  453. }
  454. }
  455. );
  456. it(
  457. 'supports ZeroBin ciphertext (SJCL & Base64 1.7)',
  458. function () {
  459. var newBase64 = global.Base64;
  460. global.Base64 = require('./base64-1.7').Base64;
  461. jsdom();
  462. delete require.cache[require.resolve('./privatebin')];
  463. require('./privatebin');
  464. // Of course you can easily decipher the following texts, if you like.
  465. // Bonus points for finding their sources and hidden meanings.
  466. var paste1 = $.PrivateBin.CryptTool.decipher(
  467. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  468. // -- "That's amazing. I've got the same combination on my luggage."
  469. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  470. '{"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=="}'
  471. ),
  472. paste2 = $.PrivateBin.CryptTool.decipher(
  473. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  474. '', // no password
  475. '{"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="}'
  476. );
  477. global.Base64 = newBase64;
  478. jsdom();
  479. delete require.cache[require.resolve('./privatebin')];
  480. require('./privatebin');
  481. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  482. throw Error('v1 (SJCL based) pastes could not be deciphered');
  483. }
  484. }
  485. );
  486. });
  487. describe('isEntropyReady & addEntropySeedListener', function () {
  488. it(
  489. 'lets us know that enough entropy is collected or make us wait for it',
  490. function(done) {
  491. if ($.PrivateBin.CryptTool.isEntropyReady()) {
  492. done();
  493. } else {
  494. $.PrivateBin.CryptTool.addEntropySeedListener(function() {
  495. done();
  496. });
  497. }
  498. }
  499. );
  500. });
  501. describe('getSymmetricKey', function () {
  502. var keys = [];
  503. // the parameter is used to ensure the test is run more then one time
  504. jsc.property(
  505. 'returns random, non-empty keys',
  506. 'nat',
  507. function(n) {
  508. var key = $.PrivateBin.CryptTool.getSymmetricKey(),
  509. result = (key !== '' && keys.indexOf(key) === -1);
  510. keys.push(key);
  511. return result;
  512. }
  513. );
  514. });
  515. describe('Base64.js vs SJCL.js vs abab.js', function () {
  516. jsc.property(
  517. 'these all return the same base64 string',
  518. 'string',
  519. function(string) {
  520. var base64 = Base64.toBase64(string),
  521. sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)),
  522. abab = window.btoa(Base64.utob(string));
  523. return base64 === sjcl && sjcl === abab;
  524. }
  525. );
  526. });
  527. });
  528. describe('Model', function () {
  529. describe('getExpirationDefault', function () {
  530. before(function () {
  531. $.PrivateBin.Model.reset();
  532. cleanup();
  533. });
  534. jsc.property(
  535. 'returns the contents of the element with id "pasteExpiration"',
  536. 'array asciinestring',
  537. 'string',
  538. 'small nat',
  539. function (keys, value, key) {
  540. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  541. value = $.PrivateBin.Helper.htmlEntities(value);
  542. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  543. contents = '<select id="pasteExpiration" name="pasteExpiration">';
  544. keys.forEach(function(item) {
  545. contents += '<option value="' + item + '"';
  546. if (item === content) {
  547. contents += ' selected="selected"';
  548. }
  549. contents += '>' + value + '</option>';
  550. });
  551. contents += '</select>';
  552. $('body').html(contents);
  553. var result = $.PrivateBin.Helper.htmlEntities(
  554. $.PrivateBin.Model.getExpirationDefault()
  555. );
  556. $.PrivateBin.Model.reset();
  557. return content === result;
  558. }
  559. );
  560. });
  561. describe('getFormatDefault', function () {
  562. before(function () {
  563. $.PrivateBin.Model.reset();
  564. cleanup();
  565. });
  566. jsc.property(
  567. 'returns the contents of the element with id "pasteFormatter"',
  568. 'array asciinestring',
  569. 'string',
  570. 'small nat',
  571. function (keys, value, key) {
  572. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  573. value = $.PrivateBin.Helper.htmlEntities(value);
  574. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  575. contents = '<select id="pasteFormatter" name="pasteFormatter">';
  576. keys.forEach(function(item) {
  577. contents += '<option value="' + item + '"';
  578. if (item === content) {
  579. contents += ' selected="selected"';
  580. }
  581. contents += '>' + value + '</option>';
  582. });
  583. contents += '</select>';
  584. $('body').html(contents);
  585. var result = $.PrivateBin.Helper.htmlEntities(
  586. $.PrivateBin.Model.getFormatDefault()
  587. );
  588. $.PrivateBin.Model.reset();
  589. return content === result;
  590. }
  591. );
  592. });
  593. describe('hasCipherData', function () {
  594. before(function () {
  595. $.PrivateBin.Model.reset();
  596. cleanup();
  597. });
  598. jsc.property(
  599. 'checks if the element with id "cipherdata" contains any data',
  600. 'asciistring',
  601. function (value) {
  602. value = $.PrivateBin.Helper.htmlEntities(value).trim();
  603. $('body').html('<div id="cipherdata">' + value + '</div>');
  604. $.PrivateBin.Model.init();
  605. var result = $.PrivateBin.Model.hasCipherData();
  606. $.PrivateBin.Model.reset();
  607. return (value.length > 0) === result;
  608. }
  609. );
  610. });
  611. describe('getCipherData', function () {
  612. before(function () {
  613. $.PrivateBin.Model.reset();
  614. cleanup();
  615. });
  616. jsc.property(
  617. 'returns the contents of the element with id "cipherdata"',
  618. 'asciistring',
  619. function (value) {
  620. value = $.PrivateBin.Helper.htmlEntities(value).trim();
  621. $('body').html('<div id="cipherdata">' + value + '</div>');
  622. $.PrivateBin.Model.init();
  623. var result = $.PrivateBin.Helper.htmlEntities(
  624. $.PrivateBin.Model.getCipherData()
  625. );
  626. $.PrivateBin.Model.reset();
  627. return value === result;
  628. }
  629. );
  630. });
  631. describe('getPasteId', function () {
  632. this.timeout(30000);
  633. before(function () {
  634. $.PrivateBin.Model.reset();
  635. cleanup();
  636. });
  637. jsc.property(
  638. 'returns the query string without separator, if any',
  639. jsc.nearray(jsc.elements(a2zString)),
  640. jsc.nearray(jsc.elements(a2zString)),
  641. jsc.nearray(jsc.elements(queryString)),
  642. 'string',
  643. function (schema, address, query, fragment) {
  644. var queryString = query.join(''),
  645. clean = jsdom('', {
  646. url: schema.join('') + '://' + address.join('') +
  647. '/?' + queryString + '#' + fragment
  648. }),
  649. result = $.PrivateBin.Model.getPasteId();
  650. $.PrivateBin.Model.reset();
  651. clean();
  652. return queryString === result;
  653. }
  654. );
  655. jsc.property(
  656. 'throws exception on empty query string',
  657. jsc.nearray(jsc.elements(a2zString)),
  658. jsc.nearray(jsc.elements(a2zString)),
  659. 'string',
  660. function (schema, address, fragment) {
  661. var clean = jsdom('', {
  662. url: schema.join('') + '://' + address.join('') +
  663. '/#' + fragment
  664. }),
  665. result = false;
  666. try {
  667. $.PrivateBin.Model.getPasteId();
  668. }
  669. catch(err) {
  670. result = true;
  671. }
  672. $.PrivateBin.Model.reset();
  673. clean();
  674. return result;
  675. }
  676. );
  677. });
  678. describe('getPasteKey', function () {
  679. this.timeout(30000);
  680. jsc.property(
  681. 'returns the fragment of the URL',
  682. jsc.nearray(jsc.elements(a2zString)),
  683. jsc.nearray(jsc.elements(a2zString)),
  684. jsc.array(jsc.elements(queryString)),
  685. jsc.nearray(jsc.elements(base64String)),
  686. function (schema, address, query, fragment) {
  687. var fragmentString = fragment.join(''),
  688. clean = jsdom('', {
  689. url: schema.join('') + '://' + address.join('') +
  690. '/?' + query.join('') + '#' + fragmentString
  691. }),
  692. result = $.PrivateBin.Model.getPasteKey();
  693. $.PrivateBin.Model.reset();
  694. clean();
  695. return fragmentString === result;
  696. }
  697. );
  698. jsc.property(
  699. 'returns the fragment stripped of trailing query parts',
  700. jsc.nearray(jsc.elements(a2zString)),
  701. jsc.nearray(jsc.elements(a2zString)),
  702. jsc.array(jsc.elements(queryString)),
  703. jsc.nearray(jsc.elements(base64String)),
  704. jsc.array(jsc.elements(queryString)),
  705. function (schema, address, query, fragment, trail) {
  706. var fragmentString = fragment.join(''),
  707. clean = jsdom('', {
  708. url: schema.join('') + '://' + address.join('') + '/?' +
  709. query.join('') + '#' + fragmentString + '&' + trail.join('')
  710. }),
  711. result = $.PrivateBin.Model.getPasteKey();
  712. $.PrivateBin.Model.reset();
  713. clean();
  714. return fragmentString === result;
  715. }
  716. );
  717. jsc.property(
  718. 'throws exception on empty fragment of the URL',
  719. jsc.nearray(jsc.elements(a2zString)),
  720. jsc.nearray(jsc.elements(a2zString)),
  721. jsc.array(jsc.elements(queryString)),
  722. function (schema, address, query) {
  723. var clean = jsdom('', {
  724. url: schema.join('') + '://' + address.join('') +
  725. '/?' + query.join('')
  726. }),
  727. result = false;
  728. try {
  729. $.PrivateBin.Model.getPasteKey();
  730. }
  731. catch(err) {
  732. result = true;
  733. }
  734. $.PrivateBin.Model.reset();
  735. clean();
  736. return result;
  737. }
  738. );
  739. });
  740. describe('getTemplate', function () {
  741. before(function () {
  742. $.PrivateBin.Model.reset();
  743. cleanup();
  744. });
  745. jsc.property(
  746. 'returns the contents of the element with id "[name]template"',
  747. jsc.nearray(jsc.elements(alnumString)),
  748. jsc.nearray(jsc.elements(a2zString)),
  749. jsc.nearray(jsc.elements(alnumString)),
  750. function (id, element, value) {
  751. id = id.join('');
  752. element = element.join('');
  753. value = value.join('').trim();
  754. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  755. // table tags can't be alone, so test with a <p> instead
  756. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  757. element = 'p';
  758. }
  759. $('body').html(
  760. '<div id="templates"><' + element + ' id="' + id +
  761. 'template">' + value + '</' + element + '></div>'
  762. );
  763. $.PrivateBin.Model.init();
  764. var template = '<' + element + ' id="' + id + '">' + value +
  765. '</' + element + '>',
  766. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  767. $.PrivateBin.Model.reset();
  768. return template === result;
  769. }
  770. );
  771. });
  772. });
  773. describe('UiHelper', function () {
  774. // TODO: As per https://github.com/tmpvar/jsdom/issues/1565 there is no navigation support in jsdom, yet.
  775. // for now we use a mock function to trigger the event
  776. describe('historyChange', function () {
  777. this.timeout(30000);
  778. before(function () {
  779. $.PrivateBin.Helper.reset();
  780. });
  781. jsc.property(
  782. 'redirects to home, when the state is null',
  783. jsc.elements(schemas),
  784. jsc.nearray(jsc.elements(a2zString)),
  785. function (schema, address) {
  786. var expected = schema + '://' + address.join('') + '/',
  787. clean = jsdom('', {url: expected});
  788. // make window.location.href writable
  789. Object.defineProperty(window.location, 'href', {
  790. writable: true,
  791. value: window.location.href
  792. });
  793. $.PrivateBin.UiHelper.mockHistoryChange();
  794. $.PrivateBin.Helper.reset();
  795. var result = window.location.href;
  796. clean();
  797. return expected === result;
  798. }
  799. );
  800. jsc.property(
  801. 'does not redirect to home, when a new paste is created',
  802. jsc.elements(schemas),
  803. jsc.nearray(jsc.elements(a2zString)),
  804. jsc.array(jsc.elements(queryString)),
  805. jsc.nearray(jsc.elements(base64String)),
  806. function (schema, address, query, fragment) {
  807. var expected = schema + '://' + address.join('') + '/' + '?' +
  808. query.join('') + '#' + fragment.join(''),
  809. clean = jsdom('', {url: expected});
  810. // make window.location.href writable
  811. Object.defineProperty(window.location, 'href', {
  812. writable: true,
  813. value: window.location.href
  814. });
  815. $.PrivateBin.UiHelper.mockHistoryChange([
  816. {type: 'newpaste'}, '', expected
  817. ]);
  818. $.PrivateBin.Helper.reset();
  819. var result = window.location.href;
  820. clean();
  821. return expected === result;
  822. }
  823. );
  824. });
  825. describe('reloadHome', function () {
  826. this.timeout(30000);
  827. before(function () {
  828. $.PrivateBin.Helper.reset();
  829. });
  830. jsc.property(
  831. 'redirects to home',
  832. jsc.elements(schemas),
  833. jsc.nearray(jsc.elements(a2zString)),
  834. jsc.array(jsc.elements(queryString)),
  835. jsc.nearray(jsc.elements(base64String)),
  836. function (schema, address, query, fragment) {
  837. var expected = schema + '://' + address.join('') + '/',
  838. clean = jsdom('', {
  839. url: expected + '?' + query.join('') + '#' + fragment.join('')
  840. });
  841. // make window.location.href writable
  842. Object.defineProperty(window.location, 'href', {
  843. writable: true,
  844. value: window.location.href
  845. });
  846. $.PrivateBin.UiHelper.reloadHome();
  847. $.PrivateBin.Helper.reset();
  848. var result = window.location.href;
  849. clean();
  850. return expected === result;
  851. }
  852. );
  853. });
  854. describe('isVisible', function () {
  855. // TODO As per https://github.com/tmpvar/jsdom/issues/1048 there is no layout support in jsdom, yet.
  856. // once it is supported or a workaround is found, uncomment the section below
  857. /*
  858. before(function () {
  859. $.PrivateBin.Helper.reset();
  860. });
  861. jsc.property(
  862. 'detect visible elements',
  863. jsc.nearray(jsc.elements(alnumString)),
  864. jsc.nearray(jsc.elements(a2zString)),
  865. function (id, element) {
  866. id = id.join('');
  867. element = element.join('');
  868. var clean = jsdom(
  869. '<' + element + ' id="' + id + '"></' + element + '>'
  870. );
  871. var result = $.PrivateBin.UiHelper.isVisible($('#' + id));
  872. clean();
  873. return result;
  874. }
  875. );
  876. */
  877. });
  878. describe('scrollTo', function () {
  879. // TODO Did not find a way to test that, see isVisible test above
  880. });
  881. });
  882. describe('Alert', function () {
  883. describe('showStatus', function () {
  884. before(function () {
  885. cleanup();
  886. });
  887. jsc.property(
  888. 'shows a status message',
  889. jsc.array(jsc.elements(alnumString)),
  890. jsc.array(jsc.elements(alnumString)),
  891. function (icon, message) {
  892. icon = icon.join('');
  893. message = message.join('');
  894. var expected = '<div id="status" role="alert" ' +
  895. 'class="statusmessage alert alert-info"><span ' +
  896. 'class="glyphicon glyphicon-' + icon +
  897. '" aria-hidden="true"></span> ' + message + '</div>';
  898. $('body').html(
  899. '<div id="status" role="alert" class="statusmessage ' +
  900. 'alert alert-info hidden"><span class="glyphicon ' +
  901. 'glyphicon-info-sign" aria-hidden="true"></span> </div>'
  902. );
  903. $.PrivateBin.Alert.init();
  904. $.PrivateBin.Alert.showStatus(message, icon);
  905. var result = $('body').html();
  906. return expected === result;
  907. }
  908. );
  909. });
  910. describe('showError', function () {
  911. before(function () {
  912. cleanup();
  913. });
  914. jsc.property(
  915. 'shows an error message',
  916. jsc.array(jsc.elements(alnumString)),
  917. jsc.array(jsc.elements(alnumString)),
  918. function (icon, message) {
  919. icon = icon.join('');
  920. message = message.join('');
  921. var expected = '<div id="errormessage" role="alert" ' +
  922. 'class="statusmessage alert alert-danger"><span ' +
  923. 'class="glyphicon glyphicon-' + icon +
  924. '" aria-hidden="true"></span> ' + message + '</div>';
  925. $('body').html(
  926. '<div id="errormessage" role="alert" class="statusmessage ' +
  927. 'alert alert-danger hidden"><span class="glyphicon ' +
  928. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  929. );
  930. $.PrivateBin.Alert.init();
  931. $.PrivateBin.Alert.showError(message, icon);
  932. var result = $('body').html();
  933. return expected === result;
  934. }
  935. );
  936. });
  937. describe('showRemaining', function () {
  938. before(function () {
  939. cleanup();
  940. });
  941. jsc.property(
  942. 'shows remaining time',
  943. jsc.array(jsc.elements(alnumString)),
  944. jsc.array(jsc.elements(alnumString)),
  945. 'integer',
  946. function (message, string, number) {
  947. message = message.join('');
  948. string = string.join('');
  949. var expected = '<div id="remainingtime" role="alert" ' +
  950. 'class="alert alert-info"><span ' +
  951. 'class="glyphicon glyphicon-fire" aria-hidden="true">' +
  952. '</span> ' + string + message + number + '</div>';
  953. $('body').html(
  954. '<div id="remainingtime" role="alert" class="hidden ' +
  955. 'alert alert-info"><span class="glyphicon ' +
  956. 'glyphicon-fire" aria-hidden="true"></span> </div>'
  957. );
  958. $.PrivateBin.Alert.init();
  959. $.PrivateBin.Alert.showRemaining(['%s' + message + '%d', string, number]);
  960. var result = $('body').html();
  961. return expected === result;
  962. }
  963. );
  964. });
  965. describe('showLoading', function () {
  966. before(function () {
  967. cleanup();
  968. });
  969. jsc.property(
  970. 'shows a loading message',
  971. jsc.array(jsc.elements(alnumString)),
  972. jsc.array(jsc.elements(alnumString)),
  973. 'integer',
  974. function (icon, message, number) {
  975. icon = icon.join('');
  976. message = message.join('');
  977. var default_message = 'Loading…';
  978. if (message.length == 0) {
  979. message = default_message;
  980. }
  981. var expected = '<ul class="nav navbar-nav"><li ' +
  982. 'id="loadingindicator" class="navbar-text"><span ' +
  983. 'class="glyphicon glyphicon-' + icon +
  984. '" aria-hidden="true"></span> ' + message + '</li></ul>';
  985. $('body').html(
  986. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  987. 'class="navbar-text hidden"><span class="glyphicon ' +
  988. 'glyphicon-time" aria-hidden="true"></span> ' +
  989. default_message + '</li></ul>'
  990. );
  991. $.PrivateBin.Alert.init();
  992. $.PrivateBin.Alert.showLoading(message, number, icon);
  993. var result = $('body').html();
  994. return expected === result;
  995. }
  996. );
  997. });
  998. describe('hideLoading', function () {
  999. before(function () {
  1000. cleanup();
  1001. });
  1002. it(
  1003. 'hides the loading message',
  1004. function() {
  1005. $('body').html(
  1006. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  1007. 'class="navbar-text"><span class="glyphicon ' +
  1008. 'glyphicon-time" aria-hidden="true"></span> ' +
  1009. 'Loading…</li></ul>'
  1010. );
  1011. $('body').addClass('loading');
  1012. $.PrivateBin.Alert.init();
  1013. $.PrivateBin.Alert.hideLoading();
  1014. return !$('body').hasClass('loading') &&
  1015. $('#loadingindicator').hasClass('hidden');
  1016. }
  1017. );
  1018. });
  1019. describe('hideMessages', function () {
  1020. before(function () {
  1021. cleanup();
  1022. });
  1023. it(
  1024. 'hides all messages',
  1025. function() {
  1026. $('body').html(
  1027. '<div id="status" role="alert" class="statusmessage ' +
  1028. 'alert alert-info"><span class="glyphicon ' +
  1029. 'glyphicon-info-sign" aria-hidden="true"></span> </div>' +
  1030. '<div id="errormessage" role="alert" class="statusmessage ' +
  1031. 'alert alert-danger"><span class="glyphicon ' +
  1032. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  1033. );
  1034. $.PrivateBin.Alert.init();
  1035. $.PrivateBin.Alert.hideMessages();
  1036. return $('#statusmessage').hasClass('hidden') &&
  1037. $('#errormessage').hasClass('hidden');
  1038. }
  1039. );
  1040. });
  1041. describe('setCustomHandler', function () {
  1042. before(function () {
  1043. cleanup();
  1044. });
  1045. jsc.property(
  1046. 'calls a given handler function',
  1047. 'nat 3',
  1048. jsc.array(jsc.elements(alnumString)),
  1049. function (trigger, message) {
  1050. message = message.join('');
  1051. var handlerCalled = false,
  1052. default_message = 'Loading…',
  1053. functions = [
  1054. $.PrivateBin.Alert.showStatus,
  1055. $.PrivateBin.Alert.showError,
  1056. $.PrivateBin.Alert.showRemaining,
  1057. $.PrivateBin.Alert.showLoading
  1058. ];
  1059. if (message.length == 0) {
  1060. message = default_message;
  1061. }
  1062. $('body').html(
  1063. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  1064. 'class="navbar-text hidden"><span class="glyphicon ' +
  1065. 'glyphicon-time" aria-hidden="true"></span> ' +
  1066. default_message + '</li></ul>' +
  1067. '<div id="remainingtime" role="alert" class="hidden ' +
  1068. 'alert alert-info"><span class="glyphicon ' +
  1069. 'glyphicon-fire" aria-hidden="true"></span> </div>' +
  1070. '<div id="status" role="alert" class="statusmessage ' +
  1071. 'alert alert-info"><span class="glyphicon ' +
  1072. 'glyphicon-info-sign" aria-hidden="true"></span> </div>' +
  1073. '<div id="errormessage" role="alert" class="statusmessage ' +
  1074. 'alert alert-danger"><span class="glyphicon ' +
  1075. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  1076. );
  1077. $.PrivateBin.Alert.init();
  1078. $.PrivateBin.Alert.setCustomHandler(function(id, $element) {
  1079. handlerCalled = true;
  1080. return jsc.random(0, 1) ? true : $element;
  1081. });
  1082. functions[trigger](message);
  1083. return handlerCalled;
  1084. }
  1085. );
  1086. });
  1087. });
  1088. describe('PasteStatus', function () {
  1089. describe('createPasteNotification', function () {
  1090. this.timeout(30000);
  1091. before(function () {
  1092. cleanup();
  1093. });
  1094. jsc.property(
  1095. 'creates a notification after a successfull paste upload',
  1096. jsc.elements(schemas),
  1097. jsc.nearray(jsc.elements(a2zString)),
  1098. jsc.array(jsc.elements(queryString)),
  1099. 'string',
  1100. jsc.elements(schemas),
  1101. jsc.nearray(jsc.elements(a2zString)),
  1102. jsc.array(jsc.elements(queryString)),
  1103. function (
  1104. schema1, address1, query1, fragment1,
  1105. schema2, address2, query2
  1106. ) {
  1107. var expected1 = schema1 + '://' + address1.join('') + '/?' +
  1108. encodeURI(query1.join('').replace(/^&+|&+$/gm,'') + '#' + fragment1),
  1109. expected2 = schema2 + '://' + address2.join('') + '/?' +
  1110. encodeURI(query2.join('')),
  1111. clean = jsdom();
  1112. $('body').html('<div><div id="deletelink"></div><div id="pastelink"></div></div>');
  1113. $.PrivateBin.PasteStatus.init();
  1114. $.PrivateBin.PasteStatus.createPasteNotification(expected1, expected2);
  1115. var result1 = $('#pasteurl')[0].href,
  1116. result2 = $('#deletelink a')[0].href;
  1117. clean();
  1118. return result1 == expected1 && result2 == expected2;
  1119. }
  1120. );
  1121. });
  1122. describe('showRemainingTime', function () {
  1123. this.timeout(30000);
  1124. before(function () {
  1125. cleanup();
  1126. });
  1127. jsc.property(
  1128. 'shows burn after reading message or remaining time',
  1129. 'bool',
  1130. 'nat',
  1131. jsc.nearray(jsc.elements(a2zString)),
  1132. jsc.nearray(jsc.elements(a2zString)),
  1133. jsc.array(jsc.elements(queryString)),
  1134. 'string',
  1135. function (
  1136. burnafterreading, remaining_time,
  1137. schema, address, query, fragment
  1138. ) {
  1139. var clean = jsdom('', {
  1140. url: schema.join('') + '://' + address.join('') +
  1141. '/?' + queryString + '#' + fragment
  1142. });
  1143. $('body').html('<div id="remainingtime" class="hidden"></div>');
  1144. $.PrivateBin.PasteStatus.init();
  1145. $.PrivateBin.PasteStatus.showRemainingTime({
  1146. 'burnafterreading': burnafterreading,
  1147. 'remaining_time': remaining_time,
  1148. 'expire_date': remaining_time ? ((new Date()).getTime() / 1000) + remaining_time : 0
  1149. });
  1150. if (burnafterreading) {
  1151. var result = $('#remainingtime').hasClass('foryoureyesonly') &&
  1152. !$('#remainingtime').hasClass('hidden');
  1153. } else if (remaining_time) {
  1154. var result =!$('#remainingtime').hasClass('foryoureyesonly') &&
  1155. !$('#remainingtime').hasClass('hidden');
  1156. } else {
  1157. var result = $('#remainingtime').hasClass('hidden') &&
  1158. !$('#remainingtime').hasClass('foryoureyesonly');
  1159. }
  1160. clean();
  1161. return result;
  1162. }
  1163. );
  1164. });
  1165. describe('hideMessages', function () {
  1166. before(function () {
  1167. cleanup();
  1168. });
  1169. it(
  1170. 'hides all messages',
  1171. function() {
  1172. $('body').html(
  1173. '<div id="remainingtime"></div><div id="pastesuccess"></div>'
  1174. );
  1175. $.PrivateBin.PasteStatus.init();
  1176. $.PrivateBin.PasteStatus.hideMessages();
  1177. return $('#remainingtime').hasClass('hidden') &&
  1178. $('#pastesuccess').hasClass('hidden');
  1179. }
  1180. );
  1181. });
  1182. });
  1183. describe('Prompt', function () {
  1184. // TODO: this does not test the prompt() fallback, since that isn't available
  1185. // in nodejs -> replace the prompt in the "page" template with a modal
  1186. describe('requestPassword & getPassword', function () {
  1187. this.timeout(30000);
  1188. before(function () {
  1189. cleanup();
  1190. });
  1191. jsc.property(
  1192. 'returns the password fed into the dialog',
  1193. 'string',
  1194. function (password) {
  1195. password = password.replace(/\r+/g, '');
  1196. var clean = jsdom('', {url: 'ftp://example.com/#0'});
  1197. $('body').html(
  1198. '<div id="passwordmodal" class="modal fade" role="dialog">' +
  1199. '<div class="modal-dialog"><div class="modal-content">' +
  1200. '<div class="modal-body"><form id="passwordform" role="form">' +
  1201. '<div class="form-group"><input id="passworddecrypt" ' +
  1202. 'type="password" class="form-control" placeholder="Enter ' +
  1203. 'password"></div><button type="submit">Decrypt</button>' +
  1204. '</form></div></div></div></div><div id="cipherdata">{}</div>'
  1205. );
  1206. $.PrivateBin.Model.init();
  1207. $.PrivateBin.Prompt.init();
  1208. $.PrivateBin.Prompt.requestPassword();
  1209. $('#passworddecrypt').val(password);
  1210. $('#passwordform').submit();
  1211. var result = $.PrivateBin.Prompt.getPassword();
  1212. clean();
  1213. return result == password;
  1214. }
  1215. );
  1216. });
  1217. });
  1218. describe('Editor', function () {
  1219. describe('show, hide, getText, setText & isPreview', function () {
  1220. this.timeout(30000);
  1221. before(function () {
  1222. cleanup();
  1223. });
  1224. jsc.property(
  1225. 'returns text fed into the textarea, handles editor tabs',
  1226. 'string',
  1227. function (text) {
  1228. var clean = jsdom(),
  1229. results = [];
  1230. $('body').html(
  1231. '<ul id="editorTabs" class="nav nav-tabs hidden"><li ' +
  1232. 'role="presentation" class="active"><a id="messageedit" ' +
  1233. 'href="#">Editor</a></li><li role="presentation"><a ' +
  1234. 'id="messagepreview" href="#">Preview</a></li></ul><div ' +
  1235. 'id="placeholder" class="hidden">+++ no paste text +++</div>' +
  1236. '<div id="prettymessage" class="hidden"><pre id="prettyprint" ' +
  1237. 'class="prettyprint linenums:1"></pre></div><div ' +
  1238. 'id="plaintext" class="hidden"></div><p><textarea ' +
  1239. 'id="message" name="message" cols="80" rows="25" ' +
  1240. 'class="form-control hidden"></textarea></p>'
  1241. );
  1242. $.PrivateBin.Editor.init();
  1243. results.push(
  1244. $('#editorTabs').hasClass('hidden') &&
  1245. $('#message').hasClass('hidden')
  1246. );
  1247. $.PrivateBin.Editor.show();
  1248. results.push(
  1249. !$('#editorTabs').hasClass('hidden') &&
  1250. !$('#message').hasClass('hidden')
  1251. );
  1252. $.PrivateBin.Editor.hide();
  1253. results.push(
  1254. $('#editorTabs').hasClass('hidden') &&
  1255. $('#message').hasClass('hidden')
  1256. );
  1257. $.PrivateBin.Editor.show();
  1258. $.PrivateBin.Editor.focusInput();
  1259. results.push(
  1260. $.PrivateBin.Editor.getText().length == 0
  1261. );
  1262. $.PrivateBin.Editor.setText(text);
  1263. results.push(
  1264. $.PrivateBin.Editor.getText() == $('#message').val()
  1265. );
  1266. $.PrivateBin.Editor.setText();
  1267. results.push(
  1268. !$.PrivateBin.Editor.isPreview() &&
  1269. !$('#message').hasClass('hidden')
  1270. );
  1271. $('#messagepreview').click();
  1272. results.push(
  1273. $.PrivateBin.Editor.isPreview() &&
  1274. $('#message').hasClass('hidden')
  1275. );
  1276. $('#messageedit').click();
  1277. results.push(
  1278. !$.PrivateBin.Editor.isPreview() &&
  1279. !$('#message').hasClass('hidden')
  1280. );
  1281. clean();
  1282. return results.every(element => element);
  1283. }
  1284. );
  1285. });
  1286. });
  1287. describe('PasteViewer', function () {
  1288. describe('run, hide, getText, setText, getFormat, setFormat & isPrettyPrinted', function () {
  1289. this.timeout(30000);
  1290. before(function () {
  1291. cleanup();
  1292. });
  1293. jsc.property(
  1294. 'displays text according to format',
  1295. jsc.elements(['plaintext', 'markdown', 'syntaxhighlighting']),
  1296. 'nestring',
  1297. function (format, text) {
  1298. var clean = jsdom(),
  1299. results = [];
  1300. $('body').html(
  1301. '<div id="placeholder" class="hidden">+++ no paste text ' +
  1302. '+++</div><div id="prettymessage" class="hidden"><pre ' +
  1303. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  1304. '</div><div id="plaintext" class="hidden"></div>'
  1305. );
  1306. $.PrivateBin.PasteViewer.init();
  1307. $.PrivateBin.PasteViewer.setFormat(format);
  1308. $.PrivateBin.PasteViewer.setText('');
  1309. results.push(
  1310. $('#placeholder').hasClass('hidden') &&
  1311. $('#prettymessage').hasClass('hidden') &&
  1312. $('#plaintext').hasClass('hidden') &&
  1313. $.PrivateBin.PasteViewer.getFormat() == format &&
  1314. $.PrivateBin.PasteViewer.getText() == ''
  1315. );
  1316. $.PrivateBin.PasteViewer.run();
  1317. results.push(
  1318. !$('#placeholder').hasClass('hidden') &&
  1319. $('#prettymessage').hasClass('hidden') &&
  1320. $('#plaintext').hasClass('hidden')
  1321. );
  1322. $.PrivateBin.PasteViewer.hide();
  1323. results.push(
  1324. $('#placeholder').hasClass('hidden') &&
  1325. $('#prettymessage').hasClass('hidden') &&
  1326. $('#plaintext').hasClass('hidden')
  1327. );
  1328. $.PrivateBin.PasteViewer.setText(text);
  1329. $.PrivateBin.PasteViewer.run();
  1330. results.push(
  1331. $('#placeholder').hasClass('hidden') &&
  1332. !$.PrivateBin.PasteViewer.isPrettyPrinted() &&
  1333. $.PrivateBin.PasteViewer.getText() == text
  1334. );
  1335. if (format == 'markdown') {
  1336. results.push(
  1337. $('#prettymessage').hasClass('hidden') &&
  1338. !$('#plaintext').hasClass('hidden')
  1339. );
  1340. } else {
  1341. results.push(
  1342. !$('#prettymessage').hasClass('hidden') &&
  1343. $('#plaintext').hasClass('hidden')
  1344. );
  1345. }
  1346. clean();
  1347. return results.every(element => element);
  1348. }
  1349. );
  1350. jsc.property(
  1351. 'sanitizes XSS',
  1352. jsc.elements(['plaintext', 'markdown', 'syntaxhighlighting']),
  1353. 'string',
  1354. // https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
  1355. jsc.elements([
  1356. '<PLAINTEXT>',
  1357. '\';alert(String.fromCharCode(88,83,83))//\';alert(String.fromCharCode(88,83,83))//";',
  1358. 'alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//--',
  1359. '></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>',
  1360. '\'\';!--"<XSS>=&{()}',
  1361. '<SCRIPT SRC=http://example.com/xss.js></SCRIPT>',
  1362. '\'">><marquee><img src=x onerror=confirm(1)></marquee>"></plaintext\\></|\\><plaintext/onmouseover=prompt(1)><script>prompt(1)</script>@gmail.com<isindex formaction=javascript:alert(/XSS/) type=submit>\'-->"></script><script>alert(document.cookie)</script>"><img/id="confirm&lpar;1)"/alt="/"src="/"onerror=eval(id)>\'">',
  1363. '<IMG SRC="javascript:alert(\'XSS\');">',
  1364. '<IMG SRC=javascript:alert(\'XSS\')>',
  1365. '<IMG SRC=JaVaScRiPt:alert(\'XSS\')>',
  1366. '<IMG SRC=javascript:alert(&quot;XSS&quot;)>',
  1367. '<IMG SRC=`javascript:alert("RSnake says, \'XSS\'")`>',
  1368. '<a onmouseover="alert(document.cookie)">xxs link</a>',
  1369. '<a onmouseover=alert(document.cookie)>xxs link</a>',
  1370. '<IMG """><SCRIPT>alert("XSS")</SCRIPT>">',
  1371. '<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>'
  1372. // the list goes on…
  1373. ]),
  1374. 'string',
  1375. function (format, prefix, xss, suffix) {
  1376. var clean = jsdom(),
  1377. text = prefix + xss + suffix;
  1378. $('body').html(
  1379. '<div id="placeholder" class="hidden">+++ no paste text ' +
  1380. '+++</div><div id="prettymessage" class="hidden"><pre ' +
  1381. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  1382. '</div><div id="plaintext" class="hidden"></div>'
  1383. );
  1384. $.PrivateBin.PasteViewer.init();
  1385. $.PrivateBin.PasteViewer.setFormat(format);
  1386. $.PrivateBin.PasteViewer.setText(text);
  1387. $.PrivateBin.PasteViewer.run();
  1388. var result = $('body').html().indexOf(xss) !== -1;
  1389. clean();
  1390. return result;
  1391. }
  1392. );
  1393. });
  1394. });