test.js 59 KB

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