Helper.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. 'use strict';
  2. const common = require('../common');
  3. const fc = require('fast-check');
  4. describe('Helper', function () {
  5. describe('secondsToHuman', function () {
  6. it('returns an array with a number and a word', () => {
  7. fc.assert(fc.property(fc.integer(), function (number) {
  8. var result = PrivateBin.Helper.secondsToHuman(number);
  9. return Array.isArray(result) &&
  10. result.length === 2 &&
  11. result[0] === parseInt(result[0], 10) &&
  12. typeof result[1] === 'string';
  13. }));
  14. });
  15. it('returns seconds on the first array position', () => {
  16. fc.assert(fc.property(fc.integer({max: 59}), function (number) {
  17. return PrivateBin.Helper.secondsToHuman(number)[0] === number;
  18. }));
  19. });
  20. it('returns seconds on the second array position', () => {
  21. fc.assert(fc.property(fc.integer({max: 59}), function (number) {
  22. return PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  23. }));
  24. });
  25. it('returns minutes on the first array position', () => {
  26. fc.assert(fc.property(fc.integer({min: 60, max: 3599}), function (number) {
  27. return PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  28. }));
  29. });
  30. it('returns minutes on the second array position', () => {
  31. fc.assert(fc.property(fc.integer({min: 60, max: 3599}), function (number) {
  32. return PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  33. }));
  34. });
  35. it('returns hours on the first array position', () => {
  36. fc.assert(fc.property(fc.integer({min: 3600, max: 86399}), function (number) {
  37. return PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  38. }));
  39. });
  40. it('returns hours on the second array position', () => {
  41. fc.assert(fc.property(fc.integer({min: 3600, max: 86399}), function (number) {
  42. return PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  43. }));
  44. });
  45. it('returns days on the first array position', () => {
  46. fc.assert(fc.property(fc.integer({min: 86400, max: 5184000}), function (number) {
  47. return PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  48. }));
  49. });
  50. it('returns days on the second array position', () => {
  51. fc.assert(fc.property(fc.integer({min: 86400, max: 5184000}), function (number) {
  52. return PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  53. }));
  54. });
  55. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  56. it('returns months on the first array position', () => {
  57. fc.assert(fc.property(fc.integer({min: 5184001}), function (number) {
  58. return PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  59. }));
  60. });
  61. it('returns months on the second array position', () => {
  62. fc.assert(fc.property(fc.integer({min: 5184001}), function (number) {
  63. return PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  64. }));
  65. });
  66. });
  67. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  68. // TODO: This needs to be tested using a browser.
  69. describe('selectText', function () {
  70. this.timeout(30000);
  71. it('selection contains content of given ID', () => {
  72. fc.assert(fc.property(
  73. fc.array(fc.array(common.fcAlnumString(), {minLength: 1}), {minLength: 1}),
  74. fc.array(fc.string(), {minLength: 1}),
  75. function (ids, contents) {
  76. var html = '',
  77. result = true,
  78. clean = globalThis.cleanup(html);
  79. ids.forEach(function(item, i) {
  80. html += '<div id="' + item.join('') + '">' + PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  81. });
  82. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  83. // Once there is one, uncomment the block below to actually check the result.
  84. /*
  85. ids.forEach(function(item, i) {
  86. PrivateBin.Helper.selectText(item.join(''));
  87. result *= (contents[i] || contents[0]) === window.getSelection().toString();
  88. });
  89. */
  90. clean();
  91. return Boolean(result);
  92. }
  93. ));
  94. });
  95. });
  96. describe('urls2links', function () {
  97. function getTextAsRenderedHtml(stringContent) {
  98. const tempDiv = document.createElement('div');
  99. tempDiv.textContent = stringContent;
  100. return tempDiv.innerHTML;
  101. }
  102. this.timeout(30000);
  103. before(function () {
  104. globalThis.cleanup();
  105. });
  106. it('ignores non-URL content', () => {
  107. fc.assert(fc.property(
  108. fc.string(),
  109. function (content) {
  110. // eslint-disable-next-line no-control-regex
  111. content = content.replace(/\r|\f/g, '\n').replace(/\u0000|\u000b/g, '');
  112. let clean = globalThis.cleanup();
  113. document.body.innerHTML = '<div id="foo"></div>';
  114. let e = document.getElementById('foo');
  115. e.textContent = content;
  116. PrivateBin.Helper.urls2links(e);
  117. let result = e.textContent;
  118. clean();
  119. return content === result;
  120. }
  121. ));
  122. });
  123. it('replaces URLs with anchors', () => {
  124. fc.assert(fc.property(
  125. fc.string(),
  126. common.fcUrl(),
  127. fc.array(common.fcHashString()),
  128. fc.string(),
  129. function (prefix, url, fragment, postfix) {
  130. // eslint-disable-next-line no-control-regex
  131. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000|\u000b/g, '');
  132. // eslint-disable-next-line no-control-regex
  133. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  134. url.fragment = fragment.join('');
  135. let urlString = common.urlToString(url),
  136. clean = globalThis.cleanup();
  137. document.body.innerHTML = '<div id="foo"></div>';
  138. let e = document.getElementById('foo');
  139. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  140. if (
  141. url.query[-1] === '&' &&
  142. (parseInt(url.fragment.charAt(0), 10) >= 0 || url.fragment.charAt(0) === 'x')
  143. ) {
  144. url.query.pop();
  145. urlString = common.urlToString(url);
  146. postfix = '';
  147. }
  148. e.textContent = prefix + urlString + postfix;
  149. PrivateBin.Helper.urls2links(e);
  150. let result = e.innerHTML;
  151. clean();
  152. urlString = getTextAsRenderedHtml(urlString);
  153. const expected = getTextAsRenderedHtml(prefix) + '<a href="' + urlString + '" target="_blank" rel="nofollow noopener noreferrer">' + urlString + '</a>' + getTextAsRenderedHtml(postfix);
  154. const expectedElement = document.createElement('div');
  155. expectedElement.innerHTML = expected;
  156. return expectedElement.innerHTML === result;
  157. }
  158. ));
  159. });
  160. it('replaces magnet links with anchors', () => {
  161. fc.assert(fc.property(
  162. fc.string(),
  163. fc.array(common.fcQueryString()),
  164. fc.string(),
  165. function (prefix, query, postfix) {
  166. // eslint-disable-next-line no-control-regex
  167. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000|\u000b/g, '');
  168. // eslint-disable-next-line no-control-regex
  169. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  170. let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm, ''),
  171. clean = globalThis.cleanup();
  172. document.body.innerHTML = '<div id="foo"></div>';
  173. let e = document.getElementById('foo');
  174. e.textContent = prefix + url + postfix;
  175. PrivateBin.Helper.urls2links(e);
  176. let result = e.innerHTML;
  177. clean();
  178. url = getTextAsRenderedHtml(url);
  179. const expected = getTextAsRenderedHtml(prefix) + '<a href="' + url + '" target="_blank" rel="nofollow noopener noreferrer">' + url + '</a>' + getTextAsRenderedHtml(postfix);
  180. const expectedElement = document.createElement('div');
  181. expectedElement.innerHTML = expected;
  182. return expectedElement.innerHTML === result;
  183. }
  184. ));
  185. });
  186. });
  187. describe('sprintf', function () {
  188. it('replaces %s in strings with first given parameter', () => {
  189. fc.assert(fc.property(
  190. fc.string(),
  191. fc.array(fc.string(), {minLength: 1}),
  192. fc.string(),
  193. function (prefix, params, postfix) {
  194. prefix = prefix.replace(/%(s|d)/g, '%%');
  195. params[0] = params[0].replace(/%(s|d)/g, '%%');
  196. postfix = postfix.replace(/%(s|d)/g, '%%');
  197. var result = prefix + params[0] + postfix;
  198. params.unshift(prefix + '%s' + postfix);
  199. return result === PrivateBin.Helper.sprintf.apply(this, params);
  200. }
  201. ));
  202. });
  203. it('replaces %d in strings with first given parameter', () => {
  204. fc.assert(fc.property(
  205. fc.string(),
  206. fc.array(fc.nat(), {minLength: 1}),
  207. fc.string(),
  208. function (prefix, params, postfix) {
  209. prefix = prefix.replace(/%(s|d)/g, '%%');
  210. postfix = postfix.replace(/%(s|d)/g, '%%');
  211. var result = prefix + params[0] + postfix;
  212. params.unshift(prefix + '%d' + postfix);
  213. return result === PrivateBin.Helper.sprintf.apply(this, params);
  214. }
  215. ));
  216. });
  217. it('replaces %d in strings with 0 if first parameter is not a number', () => {
  218. fc.assert(fc.property(
  219. fc.string(),
  220. fc.array(fc.falsy(), {minLength: 1}),
  221. fc.string(),
  222. function (prefix, params, postfix) {
  223. prefix = prefix.replace(/%(s|d)/g, '%%');
  224. postfix = postfix.replace(/%(s|d)/g, '%%');
  225. var result = prefix + '0' + postfix;
  226. params.unshift(prefix + '%d' + postfix);
  227. return result === PrivateBin.Helper.sprintf.apply(this, params);
  228. }
  229. ));
  230. });
  231. it('replaces %d and %s in strings in order', () => {
  232. fc.assert(fc.property(
  233. fc.string(),
  234. fc.nat(),
  235. fc.string(),
  236. fc.string(),
  237. fc.string(),
  238. function (prefix, uint, middle, string, postfix) {
  239. prefix = prefix.replace(/%(s|d)/g, '');
  240. middle = middle.replace(/%(s|d)/g, '');
  241. postfix = postfix.replace(/%(s|d)/g, '');
  242. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  243. result = prefix + uint + middle + string + postfix;
  244. return result === PrivateBin.Helper.sprintf.apply(this, params);
  245. }
  246. ));
  247. });
  248. it('replaces %d and %s in strings in reverse order', () => {
  249. fc.assert(fc.property(
  250. fc.string(),
  251. fc.nat(),
  252. fc.string(),
  253. fc.string(),
  254. fc.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. });
  266. describe('getCookie', function () {
  267. this.timeout(30000);
  268. before(function () {
  269. globalThis.cleanup();
  270. });
  271. /* TODO test fails since jsDOM version 17 - document.cookie remains empty
  272. it('returns the requested cookie', () => {
  273. fc.assert(fc.property(
  274. fc.array(fc.array(common.fcAlnumString(), {minLength: 1}), {minLength: 1}),
  275. fc.array(fc.array(common.fcAlnumString(), {minLength: 1}), {minLength: 1}),
  276. function (labels, values) {
  277. let selectedKey = '', selectedValue = '';
  278. const clean = globalThis.cleanup();
  279. labels.forEach(function(item, i) {
  280. const key = item.join(''),
  281. value = (values[i] || values[0]).join('');
  282. document.cookie = key + '=' + value;
  283. if (Math.random() < 1 / i || selectedKey === key)
  284. {
  285. selectedKey = key;
  286. selectedValue = value;
  287. }
  288. });
  289. const result = PrivateBin.Helper.getCookie(selectedKey);
  290. PrivateBin.Helper.reset();
  291. clean();
  292. return result === selectedValue;
  293. }
  294. ));
  295. }); */
  296. });
  297. describe('baseUri', function () {
  298. this.timeout(30000);
  299. it('returns the URL without query & fragment', () => {
  300. fc.assert(fc.property(
  301. common.fcSchemas(false),
  302. common.fcUrl(),
  303. function (schema, url) {
  304. url.schema = schema;
  305. const fullUrl = common.urlToString(url);
  306. delete(url.query);
  307. delete(url.fragment);
  308. PrivateBin.Helper.reset();
  309. const expected = common.urlToString(url),
  310. clean = globalThis.cleanup('', {url: fullUrl}),
  311. result = PrivateBin.Helper.baseUri();
  312. clean();
  313. return expected === result;
  314. }
  315. ));
  316. });
  317. });
  318. describe('htmlEntities', function () {
  319. before(function () {
  320. globalThis.cleanup();
  321. });
  322. it('removes all HTML entities from any given string', () => {
  323. fc.assert(fc.property(
  324. fc.string(),
  325. function (string) {
  326. var result = PrivateBin.Helper.htmlEntities(string);
  327. return !(/[<>]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  328. }
  329. ));
  330. });
  331. });
  332. describe('formatBytes', function () {
  333. it('returns 0 B for 0 bytes', function () {
  334. return PrivateBin.Helper.formatBytes(0) === '0 B';
  335. });
  336. it('formats bytes < 1000 as B', function () {
  337. return PrivateBin.Helper.formatBytes(500) === '500 B';
  338. });
  339. it('formats kilobytes correctly', function () {
  340. return PrivateBin.Helper.formatBytes(1500) === '1.5 kB';
  341. });
  342. it('formats megabytes correctly', function () {
  343. return PrivateBin.Helper.formatBytes(2 * 1000 * 1000) === '2 MB';
  344. });
  345. it('formats gigabytes correctly', function () {
  346. return PrivateBin.Helper.formatBytes(3.45 * 1000 * 1000 * 1000) === '3.45 GB';
  347. });
  348. it('formats terabytes correctly', function () {
  349. return PrivateBin.Helper.formatBytes(1.75 * 1000 ** 4) === '1.75 TB';
  350. });
  351. it('formats petabytes correctly', function () {
  352. return PrivateBin.Helper.formatBytes(1.5 * 1000 ** 5) === '1.5 PB';
  353. });
  354. it('formats exabytes correctly', function () {
  355. return PrivateBin.Helper.formatBytes(1.2345 * 1000 ** 6).startsWith('1.23 EB');
  356. });
  357. it('formats yottabytes correctly', function () {
  358. return PrivateBin.Helper.formatBytes(1.23 * 1000 ** 8).startsWith('1.23 YB');
  359. });
  360. it('rounds to two decimal places', function () {
  361. return PrivateBin.Helper.formatBytes(1234567) === '1.23 MB';
  362. });
  363. });
  364. describe('isBootstrap5', function () {
  365. it('Bootstrap 5 has been detected', function () {
  366. global.bootstrap = {};
  367. return PrivateBin.Helper.isBootstrap5() === true;
  368. });
  369. it('Bootstrap 5 has not been detected', function () {
  370. delete global.bootstrap;
  371. return PrivateBin.Helper.isBootstrap5() === false;
  372. });
  373. });
  374. });