Helper.js 15 KB

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