Helper.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 = jsdom(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. this.timeout(30000);
  74. before(function () {
  75. cleanup = jsdom();
  76. });
  77. jsc.property(
  78. 'ignores non-URL content',
  79. 'string',
  80. function (content) {
  81. // eslint-disable-next-line no-control-regex
  82. content = content.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  83. let clean = jsdom();
  84. $('body').html('<div id="foo"></div>');
  85. let e = $('#foo');
  86. e.text(content);
  87. $.PrivateBin.Helper.urls2links(e);
  88. let result = e.text();
  89. clean();
  90. return content === result;
  91. }
  92. );
  93. jsc.property(
  94. 'replaces URLs with anchors',
  95. 'string',
  96. common.jscUrl(),
  97. jsc.array(common.jscHashString()),
  98. 'string',
  99. function (prefix, url, fragment, postfix) {
  100. // eslint-disable-next-line no-control-regex
  101. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  102. // eslint-disable-next-line no-control-regex
  103. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  104. url.fragment = fragment.join('');
  105. let urlString = common.urlToString(url),
  106. clean = jsdom();
  107. $('body').html('<div id="foo"></div>');
  108. let e = $('#foo');
  109. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  110. if (
  111. url.query[-1] === '&' &&
  112. (parseInt(url.fragment.charAt(0), 10) >= 0 || url.fragment.charAt(0) === 'x')
  113. ) {
  114. url.query.pop();
  115. urlString = common.urlToString(url);
  116. postfix = '';
  117. }
  118. e.text(prefix + urlString + postfix);
  119. $.PrivateBin.Helper.urls2links(e);
  120. let result = e.html();
  121. clean();
  122. urlString = $('<div />').text(urlString).html();
  123. const expected = $('<div />').text(prefix).html() + '<a href="' + urlString + '" target="_blank" rel="nofollow noopener noreferrer">' + urlString + '</a>' + $('<div />').text(postfix).html();
  124. return $('<div />').text(prefix).html() + '<a href="' + urlString + '" target="_blank" rel="nofollow noopener noreferrer">' + urlString + '</a>' + $('<div />').text(postfix).html() === result;
  125. }
  126. );
  127. jsc.property(
  128. 'replaces magnet links with anchors',
  129. 'string',
  130. jsc.array(common.jscQueryString()),
  131. 'string',
  132. function (prefix, query, postfix) {
  133. // eslint-disable-next-line no-control-regex
  134. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  135. // eslint-disable-next-line no-control-regex
  136. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  137. let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm, ''),
  138. clean = jsdom();
  139. $('body').html('<div id="foo"></div>');
  140. let e = $('#foo');
  141. e.text(prefix + url + postfix);
  142. $.PrivateBin.Helper.urls2links(e);
  143. let result = e.html();
  144. clean();
  145. url = $('<div />').text(url).html();
  146. return $('<div />').text(prefix).html() + '<a href="' + url + '" target="_blank" rel="nofollow noopener noreferrer">' + url + '</a>' + $('<div />').text(postfix).html() === result;
  147. }
  148. );
  149. });
  150. describe('sprintf', function () {
  151. jsc.property(
  152. 'replaces %s in strings with first given parameter',
  153. 'string',
  154. '(small nearray) string',
  155. 'string',
  156. function (prefix, params, postfix) {
  157. prefix = prefix.replace(/%(s|d)/g, '%%');
  158. params[0] = params[0].replace(/%(s|d)/g, '%%');
  159. postfix = postfix.replace(/%(s|d)/g, '%%');
  160. var result = prefix + params[0] + postfix;
  161. params.unshift(prefix + '%s' + postfix);
  162. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  163. }
  164. );
  165. jsc.property(
  166. 'replaces %d in strings with first given parameter',
  167. 'string',
  168. '(small nearray) nat',
  169. 'string',
  170. function (prefix, params, postfix) {
  171. prefix = prefix.replace(/%(s|d)/g, '%%');
  172. postfix = postfix.replace(/%(s|d)/g, '%%');
  173. var result = prefix + params[0] + postfix;
  174. params.unshift(prefix + '%d' + postfix);
  175. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  176. }
  177. );
  178. jsc.property(
  179. 'replaces %d in strings with 0 if first parameter is not a number',
  180. 'string',
  181. '(small nearray) falsy',
  182. 'string',
  183. function (prefix, params, postfix) {
  184. prefix = prefix.replace(/%(s|d)/g, '%%');
  185. postfix = postfix.replace(/%(s|d)/g, '%%');
  186. var result = prefix + '0' + postfix;
  187. params.unshift(prefix + '%d' + postfix);
  188. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  189. }
  190. );
  191. jsc.property(
  192. 'replaces %d and %s in strings in order',
  193. 'string',
  194. 'nat',
  195. 'string',
  196. 'string',
  197. 'string',
  198. function (prefix, uint, middle, string, postfix) {
  199. prefix = prefix.replace(/%(s|d)/g, '');
  200. middle = middle.replace(/%(s|d)/g, '');
  201. postfix = postfix.replace(/%(s|d)/g, '');
  202. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  203. result = prefix + uint + middle + string + postfix;
  204. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  205. }
  206. );
  207. jsc.property(
  208. 'replaces %d and %s in strings in reverse order',
  209. 'string',
  210. 'nat',
  211. 'string',
  212. 'string',
  213. 'string',
  214. function (prefix, uint, middle, string, postfix) {
  215. prefix = prefix.replace(/%(s|d)/g, '');
  216. middle = middle.replace(/%(s|d)/g, '');
  217. postfix = postfix.replace(/%(s|d)/g, '');
  218. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  219. result = prefix + string + middle + uint + postfix;
  220. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  221. }
  222. );
  223. });
  224. describe('getCookie', function () {
  225. this.timeout(30000);
  226. before(function () {
  227. cleanup();
  228. });
  229. /* TODO test fails since jsDOM version 17 - document.cookie remains empty
  230. jsc.property(
  231. 'returns the requested cookie',
  232. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  233. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  234. function (labels, values) {
  235. let selectedKey = '', selectedValue = '';
  236. const clean = jsdom();
  237. labels.forEach(function(item, i) {
  238. const key = item.join(''),
  239. value = (values[i] || values[0]).join('');
  240. document.cookie = key + '=' + value;
  241. if (Math.random() < 1 / i || selectedKey === key)
  242. {
  243. selectedKey = key;
  244. selectedValue = value;
  245. }
  246. });
  247. const result = $.PrivateBin.Helper.getCookie(selectedKey);
  248. $.PrivateBin.Helper.reset();
  249. clean();
  250. return result === selectedValue;
  251. }
  252. ); */
  253. });
  254. describe('baseUri', function () {
  255. this.timeout(30000);
  256. jsc.property(
  257. 'returns the URL without query & fragment',
  258. common.jscSchemas(false),
  259. common.jscUrl(),
  260. function (schema, url) {
  261. url.schema = schema;
  262. const fullUrl = common.urlToString(url);
  263. delete(url.query);
  264. delete(url.fragment);
  265. $.PrivateBin.Helper.reset();
  266. const expected = common.urlToString(url),
  267. clean = jsdom('', {url: fullUrl}),
  268. result = $.PrivateBin.Helper.baseUri();
  269. clean();
  270. return expected === result;
  271. }
  272. );
  273. });
  274. describe('htmlEntities', function () {
  275. before(function () {
  276. cleanup = jsdom();
  277. });
  278. jsc.property(
  279. 'removes all HTML entities from any given string',
  280. 'string',
  281. function (string) {
  282. var result = $.PrivateBin.Helper.htmlEntities(string);
  283. return !(/[<>]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  284. }
  285. );
  286. });
  287. describe('formatBytes', function () {
  288. jsc.property('returns 0 B for 0 bytes', function () {
  289. return $.PrivateBin.Helper.formatBytes(0) === '0 B';
  290. });
  291. jsc.property('formats bytes < 1000 as B', function () {
  292. return $.PrivateBin.Helper.formatBytes(500) === '500 B';
  293. });
  294. jsc.property('formats kilobytes correctly', function () {
  295. return $.PrivateBin.Helper.formatBytes(1500) === '1.5 kB';
  296. });
  297. jsc.property('formats megabytes correctly', function () {
  298. return $.PrivateBin.Helper.formatBytes(2 * 1000 * 1000) === '2 MB';
  299. });
  300. jsc.property('formats gigabytes correctly', function () {
  301. return $.PrivateBin.Helper.formatBytes(3.45 * 1000 * 1000 * 1000) === '3.45 GB';
  302. });
  303. jsc.property('formats terabytes correctly', function () {
  304. return $.PrivateBin.Helper.formatBytes(1.75 * 1000 ** 4) === '1.75 TB';
  305. });
  306. jsc.property('formats petabytes correctly', function () {
  307. return $.PrivateBin.Helper.formatBytes(1.5 * 1000 ** 5) === '1.5 PB';
  308. });
  309. jsc.property('formats exabytes correctly', function () {
  310. return $.PrivateBin.Helper.formatBytes(1.2345 * 1000 ** 6).startsWith('1.23 EB');
  311. });
  312. jsc.property('formats yottabytes correctly', function () {
  313. return $.PrivateBin.Helper.formatBytes(1.23 * 1000 ** 8).startsWith('1.23 YB');
  314. });
  315. jsc.property('rounds to two decimal places', function () {
  316. return $.PrivateBin.Helper.formatBytes(1234567) === '1.23 MB';
  317. });
  318. });
  319. describe('isBootstrap5', function () {
  320. jsc.property('Bootstrap 5 has been detected', function () {
  321. global.bootstrap = {};
  322. return $.PrivateBin.Helper.isBootstrap5() === true;
  323. });
  324. jsc.property('Bootstrap 5 has not been detected', function () {
  325. delete global.bootstrap;
  326. return $.PrivateBin.Helper.isBootstrap5() === false;
  327. });
  328. });
  329. });