test.js 69 KB

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