Helper.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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: 5184000}), 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: 5184000}), 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. return expected === result;
  155. }
  156. ));
  157. });
  158. it('replaces magnet links with anchors', () => {
  159. fc.assert(fc.property(
  160. fc.string(),
  161. fc.array(common.fcQueryString()),
  162. fc.string(),
  163. function (prefix, query, postfix) {
  164. // eslint-disable-next-line no-control-regex
  165. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000|\u000b/g, '');
  166. // eslint-disable-next-line no-control-regex
  167. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  168. let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm, ''),
  169. clean = globalThis.cleanup();
  170. document.body.innerHTML = '<div id="foo"></div>';
  171. let e = document.getElementById('foo');
  172. e.textContent = prefix + url + postfix;
  173. PrivateBin.Helper.urls2links(e);
  174. let result = e.innerHTML;
  175. clean();
  176. url = getTextAsRenderedHtml(url);
  177. return getTextAsRenderedHtml(prefix) + '<a href="' + url + '" target="_blank" rel="nofollow noopener noreferrer">' + url + '</a>' + getTextAsRenderedHtml(postfix) === result;
  178. }
  179. ));
  180. });
  181. });
  182. describe('sprintf', function () {
  183. it('replaces %s in strings with first given parameter', () => {
  184. fc.assert(fc.property(
  185. fc.string(),
  186. fc.array(fc.string(), {minLength: 1}),
  187. fc.string(),
  188. function (prefix, params, postfix) {
  189. prefix = prefix.replace(/%(s|d)/g, '%%');
  190. params[0] = params[0].replace(/%(s|d)/g, '%%');
  191. postfix = postfix.replace(/%(s|d)/g, '%%');
  192. var result = prefix + params[0] + postfix;
  193. params.unshift(prefix + '%s' + postfix);
  194. return result === PrivateBin.Helper.sprintf.apply(this, params);
  195. }
  196. ));
  197. });
  198. it('replaces %d in strings with first given parameter', () => {
  199. fc.assert(fc.property(
  200. fc.string(),
  201. fc.array(fc.nat(), {minLength: 1}),
  202. fc.string(),
  203. function (prefix, params, postfix) {
  204. prefix = prefix.replace(/%(s|d)/g, '%%');
  205. postfix = postfix.replace(/%(s|d)/g, '%%');
  206. var result = prefix + params[0] + postfix;
  207. params.unshift(prefix + '%d' + postfix);
  208. return result === PrivateBin.Helper.sprintf.apply(this, params);
  209. }
  210. ));
  211. });
  212. it('replaces %d in strings with 0 if first parameter is not a number', () => {
  213. fc.assert(fc.property(
  214. fc.string(),
  215. fc.array(fc.falsy(), {minLength: 1}),
  216. fc.string(),
  217. function (prefix, params, postfix) {
  218. prefix = prefix.replace(/%(s|d)/g, '%%');
  219. postfix = postfix.replace(/%(s|d)/g, '%%');
  220. var result = prefix + '0' + postfix;
  221. params.unshift(prefix + '%d' + postfix);
  222. return result === PrivateBin.Helper.sprintf.apply(this, params);
  223. }
  224. ));
  225. });
  226. it('replaces %d and %s in strings in order', () => {
  227. fc.assert(fc.property(
  228. fc.string(),
  229. fc.nat(),
  230. fc.string(),
  231. fc.string(),
  232. fc.string(),
  233. function (prefix, uint, middle, string, postfix) {
  234. prefix = prefix.replace(/%(s|d)/g, '');
  235. middle = middle.replace(/%(s|d)/g, '');
  236. postfix = postfix.replace(/%(s|d)/g, '');
  237. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  238. result = prefix + uint + middle + string + postfix;
  239. return result === PrivateBin.Helper.sprintf.apply(this, params);
  240. }
  241. ));
  242. });
  243. it('replaces %d and %s in strings in reverse order', () => {
  244. fc.assert(fc.property(
  245. fc.string(),
  246. fc.nat(),
  247. fc.string(),
  248. fc.string(),
  249. fc.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. });
  261. describe('getCookie', function () {
  262. this.timeout(30000);
  263. before(function () {
  264. globalThis.cleanup();
  265. });
  266. /* TODO test fails since jsDOM version 17 - document.cookie remains empty
  267. it('returns the requested cookie', () => {
  268. fc.assert(fc.property(
  269. fc.array(fc.array(common.fcAlnumString(), {minLength: 1}), {minLength: 1}),
  270. fc.array(fc.array(common.fcAlnumString(), {minLength: 1}), {minLength: 1}),
  271. function (labels, values) {
  272. let selectedKey = '', selectedValue = '';
  273. const clean = globalThis.cleanup();
  274. labels.forEach(function(item, i) {
  275. const key = item.join(''),
  276. value = (values[i] || values[0]).join('');
  277. document.cookie = key + '=' + value;
  278. if (Math.random() < 1 / i || selectedKey === key)
  279. {
  280. selectedKey = key;
  281. selectedValue = value;
  282. }
  283. });
  284. const result = PrivateBin.Helper.getCookie(selectedKey);
  285. PrivateBin.Helper.reset();
  286. clean();
  287. return result === selectedValue;
  288. }
  289. ));
  290. }); */
  291. });
  292. describe('baseUri', function () {
  293. this.timeout(30000);
  294. it('returns the URL without query & fragment', () => {
  295. fc.assert(fc.property(
  296. common.fcSchemas(false),
  297. common.fcUrl(),
  298. function (schema, url) {
  299. url.schema = schema;
  300. const fullUrl = common.urlToString(url);
  301. delete(url.query);
  302. delete(url.fragment);
  303. PrivateBin.Helper.reset();
  304. const expected = common.urlToString(url),
  305. clean = globalThis.cleanup('', {url: fullUrl}),
  306. result = PrivateBin.Helper.baseUri();
  307. clean();
  308. return expected === result;
  309. }
  310. ));
  311. });
  312. });
  313. describe('htmlEntities', function () {
  314. before(function () {
  315. globalThis.cleanup();
  316. });
  317. it('removes all HTML entities from any given string', () => {
  318. fc.assert(fc.property(
  319. fc.string(),
  320. function (string) {
  321. var result = PrivateBin.Helper.htmlEntities(string);
  322. return !(/[<>]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  323. }
  324. ));
  325. });
  326. });
  327. describe('formatBytes', function () {
  328. it('returns 0 B for 0 bytes', function () {
  329. return PrivateBin.Helper.formatBytes(0) === '0 B';
  330. });
  331. it('formats bytes < 1000 as B', function () {
  332. return PrivateBin.Helper.formatBytes(500) === '500 B';
  333. });
  334. it('formats kilobytes correctly', function () {
  335. return PrivateBin.Helper.formatBytes(1500) === '1.5 kB';
  336. });
  337. it('formats megabytes correctly', function () {
  338. return PrivateBin.Helper.formatBytes(2 * 1000 * 1000) === '2 MB';
  339. });
  340. it('formats gigabytes correctly', function () {
  341. return PrivateBin.Helper.formatBytes(3.45 * 1000 * 1000 * 1000) === '3.45 GB';
  342. });
  343. it('formats terabytes correctly', function () {
  344. return PrivateBin.Helper.formatBytes(1.75 * 1000 ** 4) === '1.75 TB';
  345. });
  346. it('formats petabytes correctly', function () {
  347. return PrivateBin.Helper.formatBytes(1.5 * 1000 ** 5) === '1.5 PB';
  348. });
  349. it('formats exabytes correctly', function () {
  350. return PrivateBin.Helper.formatBytes(1.2345 * 1000 ** 6).startsWith('1.23 EB');
  351. });
  352. it('formats yottabytes correctly', function () {
  353. return PrivateBin.Helper.formatBytes(1.23 * 1000 ** 8).startsWith('1.23 YB');
  354. });
  355. it('rounds to two decimal places', function () {
  356. return PrivateBin.Helper.formatBytes(1234567) === '1.23 MB';
  357. });
  358. });
  359. describe('isBootstrap5', function () {
  360. it('Bootstrap 5 has been detected', function () {
  361. global.bootstrap = {};
  362. return PrivateBin.Helper.isBootstrap5() === true;
  363. });
  364. it('Bootstrap 5 has not been detected', function () {
  365. delete global.bootstrap;
  366. return PrivateBin.Helper.isBootstrap5() === false;
  367. });
  368. });
  369. });