test.js 66 KB

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