test.js 64 KB

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