Helper.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. content = content.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  82. let clean = jsdom();
  83. $('body').html('<div id="foo"></div>');
  84. let e = $('#foo');
  85. e.text(content);
  86. $.PrivateBin.Helper.urls2links(e);
  87. let result = e.text();
  88. clean();
  89. return content === result;
  90. }
  91. );
  92. jsc.property(
  93. 'replaces URLs with anchors',
  94. 'string',
  95. jsc.elements(['http', 'https', 'ftp']),
  96. jsc.nearray(common.jscA2zString()),
  97. jsc.array(common.jscQueryString()),
  98. jsc.array(common.jscHashString()),
  99. 'string',
  100. function (prefix, schema, address, query, fragment, postfix) {
  101. query = query.join('');
  102. fragment = fragment.join('');
  103. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  104. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  105. let url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  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. query.slice(-1) === '&' &&
  112. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  113. )
  114. {
  115. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  116. postfix = '';
  117. }
  118. e.text(prefix + url + postfix);
  119. $.PrivateBin.Helper.urls2links(e);
  120. let result = e.html();
  121. clean();
  122. url = $('<div />').text(url).html();
  123. return $('<div />').text(prefix).html() + '<a href="' + url + '" target="_blank" rel="nofollow noopener noreferrer">' + url + '</a>' + $('<div />').text(postfix).html() === result;
  124. }
  125. );
  126. jsc.property(
  127. 'replaces magnet links with anchors',
  128. 'string',
  129. jsc.array(common.jscQueryString()),
  130. 'string',
  131. function (prefix, query, postfix) {
  132. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  133. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  134. let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''),
  135. clean = jsdom();
  136. $('body').html('<div id="foo"></div>');
  137. let e = $('#foo');
  138. e.text(prefix + url + postfix);
  139. $.PrivateBin.Helper.urls2links(e);
  140. let result = e.html();
  141. clean();
  142. url = $('<div />').text(url).html();
  143. return $('<div />').text(prefix).html() + '<a href="' + url + '" target="_blank" rel="nofollow noopener noreferrer">' + url + '</a>' + $('<div />').text(postfix).html() === result;
  144. }
  145. );
  146. });
  147. describe('sprintf', function () {
  148. jsc.property(
  149. 'replaces %s in strings with first given parameter',
  150. 'string',
  151. '(small nearray) string',
  152. 'string',
  153. function (prefix, params, postfix) {
  154. prefix = prefix.replace(/%(s|d)/g, '%%');
  155. params[0] = params[0].replace(/%(s|d)/g, '%%');
  156. postfix = postfix.replace(/%(s|d)/g, '%%');
  157. var result = prefix + params[0] + postfix;
  158. params.unshift(prefix + '%s' + postfix);
  159. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  160. }
  161. );
  162. jsc.property(
  163. 'replaces %d in strings with first given parameter',
  164. 'string',
  165. '(small nearray) nat',
  166. 'string',
  167. function (prefix, params, postfix) {
  168. prefix = prefix.replace(/%(s|d)/g, '%%');
  169. postfix = postfix.replace(/%(s|d)/g, '%%');
  170. var result = prefix + params[0] + postfix;
  171. params.unshift(prefix + '%d' + postfix);
  172. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  173. }
  174. );
  175. jsc.property(
  176. 'replaces %d in strings with 0 if first parameter is not a number',
  177. 'string',
  178. '(small nearray) falsy',
  179. 'string',
  180. function (prefix, params, postfix) {
  181. prefix = prefix.replace(/%(s|d)/g, '%%');
  182. postfix = postfix.replace(/%(s|d)/g, '%%');
  183. var result = prefix + '0' + postfix;
  184. params.unshift(prefix + '%d' + postfix);
  185. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  186. }
  187. );
  188. jsc.property(
  189. 'replaces %d and %s in strings in order',
  190. 'string',
  191. 'nat',
  192. 'string',
  193. 'string',
  194. 'string',
  195. function (prefix, uint, middle, string, postfix) {
  196. prefix = prefix.replace(/%(s|d)/g, '');
  197. middle = middle.replace(/%(s|d)/g, '');
  198. postfix = postfix.replace(/%(s|d)/g, '');
  199. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  200. result = prefix + uint + middle + string + postfix;
  201. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  202. }
  203. );
  204. jsc.property(
  205. 'replaces %d and %s in strings in reverse order',
  206. 'string',
  207. 'nat',
  208. 'string',
  209. 'string',
  210. 'string',
  211. function (prefix, uint, middle, string, postfix) {
  212. prefix = prefix.replace(/%(s|d)/g, '');
  213. middle = middle.replace(/%(s|d)/g, '');
  214. postfix = postfix.replace(/%(s|d)/g, '');
  215. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  216. result = prefix + string + middle + uint + postfix;
  217. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  218. }
  219. );
  220. });
  221. describe('getCookie', function () {
  222. this.timeout(30000);
  223. before(function () {
  224. cleanup();
  225. });
  226. jsc.property(
  227. 'returns the requested cookie',
  228. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  229. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  230. function (labels, values) {
  231. var selectedKey = '', selectedValue = '',
  232. cookieArray = [];
  233. labels.forEach(function(item, i) {
  234. var key = item.join(''),
  235. value = (values[i] || values[0]).join('');
  236. cookieArray.push(key + '=' + value);
  237. if (Math.random() < 1 / i || selectedKey === key)
  238. {
  239. selectedKey = key;
  240. selectedValue = value;
  241. }
  242. });
  243. var clean = jsdom('', {cookie: cookieArray}),
  244. result = $.PrivateBin.Helper.getCookie(selectedKey);
  245. $.PrivateBin.Helper.reset();
  246. clean();
  247. return result === selectedValue;
  248. }
  249. );
  250. });
  251. describe('baseUri', function () {
  252. this.timeout(30000);
  253. jsc.property(
  254. 'returns the URL without query & fragment',
  255. jsc.elements(['http', 'https']),
  256. jsc.nearray(common.jscA2zString()),
  257. jsc.array(common.jscA2zString()),
  258. jsc.array(common.jscQueryString()),
  259. 'string',
  260. function (schema, address, path, query, fragment) {
  261. $.PrivateBin.Helper.reset();
  262. var path = path.join('') + (path.length > 0 ? '/' : ''),
  263. expected = schema + '://' + address.join('') + '/' + path,
  264. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  265. result = $.PrivateBin.Helper.baseUri();
  266. clean();
  267. return expected === result;
  268. }
  269. );
  270. });
  271. describe('htmlEntities', function () {
  272. before(function () {
  273. cleanup = jsdom();
  274. });
  275. jsc.property(
  276. 'removes all HTML entities from any given string',
  277. 'string',
  278. function (string) {
  279. var result = $.PrivateBin.Helper.htmlEntities(string);
  280. return !(/[<>]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  281. }
  282. );
  283. });
  284. });