Helper.js 13 KB

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