test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. 'use strict';
  2. var jsc = require('jsverify'),
  3. jsdom = require('jsdom-global'),
  4. cleanup = jsdom(),
  5. a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  6. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  7. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  8. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  9. base64String = alnumString.concat(['+','/','=']).concat(
  10. a2zString.map(function(c) {
  11. return c.toUpperCase();
  12. })
  13. );
  14. global.$ = global.jQuery = require('./jquery-3.1.1');
  15. global.sjcl = require('./sjcl-1.0.6');
  16. global.Base64 = require('./base64-2.1.9');
  17. global.RawDeflate = require('./rawdeflate-0.5');
  18. require('./rawinflate-0.3');
  19. require('./privatebin');
  20. describe('helper', function () {
  21. describe('secondsToHuman', function () {
  22. after(function () {
  23. cleanup();
  24. });
  25. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  26. var result = $.PrivateBin.helper.secondsToHuman(number);
  27. return Array.isArray(result) &&
  28. result.length === 2 &&
  29. result[0] === parseInt(result[0], 10) &&
  30. typeof result[1] === 'string';
  31. });
  32. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  33. return $.PrivateBin.helper.secondsToHuman(number)[0] === number;
  34. });
  35. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  36. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'second';
  37. });
  38. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  39. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  40. });
  41. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  42. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'minute';
  43. });
  44. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  45. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  46. });
  47. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  48. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'hour';
  49. });
  50. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  51. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  52. });
  53. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  54. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'day';
  55. });
  56. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  57. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  58. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  59. });
  60. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  61. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'month';
  62. });
  63. });
  64. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  65. // TODO: This needs to be tested using a browser.
  66. describe('selectText', function () {
  67. jsc.property(
  68. 'selection contains content of given ID',
  69. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  70. 'nearray string',
  71. function (ids, contents) {
  72. var html = '',
  73. result = true;
  74. ids.forEach(function(item, i) {
  75. html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  76. });
  77. var clean = jsdom(html);
  78. ids.forEach(function(item, i) {
  79. $.PrivateBin.helper.selectText(item.join(''));
  80. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  81. // Once there is one, uncomment the line below to actually check the result.
  82. //result *= (contents[i] || contents[0]) === window.getSelection().toString();
  83. });
  84. clean();
  85. return Boolean(result);
  86. }
  87. );
  88. });
  89. describe('setElementText', function () {
  90. after(function () {
  91. cleanup();
  92. });
  93. jsc.property(
  94. 'replaces the content of an element',
  95. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  96. 'nearray string',
  97. 'string',
  98. function (ids, contents, replacingContent) {
  99. var html = '',
  100. result = true;
  101. ids.forEach(function(item, i) {
  102. html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  103. });
  104. var elements = $('<body />').html(html);
  105. ids.forEach(function(item, i) {
  106. var id = item.join(''),
  107. element = elements.find('#' + id).first();
  108. $.PrivateBin.helper.setElementText(element, replacingContent);
  109. result *= replacingContent === element.text();
  110. });
  111. return Boolean(result);
  112. }
  113. );
  114. });
  115. describe('setMessage', function () {
  116. after(function () {
  117. cleanup();
  118. });
  119. jsc.property(
  120. 'replaces the content of an empty element, analog to setElementText',
  121. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  122. 'nearray string',
  123. 'string',
  124. function (ids, contents, replacingContent) {
  125. var html = '',
  126. result = true;
  127. ids.forEach(function(item, i) {
  128. html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  129. });
  130. var elements = $('<body />').html(html);
  131. ids.forEach(function(item, i) {
  132. var id = item.join(''),
  133. element = elements.find('#' + id).first();
  134. $.PrivateBin.helper.setMessage(element, replacingContent);
  135. result *= replacingContent === element.text();
  136. });
  137. return Boolean(result);
  138. }
  139. );
  140. jsc.property(
  141. 'replaces the last child of a non-empty element',
  142. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  143. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  144. 'nearray string',
  145. 'string',
  146. function (ids, classes, contents, replacingContent) {
  147. var html = '',
  148. result = true;
  149. ids.forEach(function(item, i) {
  150. html += '<div id="' + item.join('') + '"><span class="' + (classes[i] || classes[0]) + '"></span> ' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  151. });
  152. var elements = $('<body />').html(html);
  153. ids.forEach(function(item, i) {
  154. var id = item.join(''),
  155. element = elements.find('#' + id).first();
  156. $.PrivateBin.helper.setMessage(element, replacingContent);
  157. result *= ' ' + replacingContent === element.contents()[1].nodeValue;
  158. });
  159. return Boolean(result);
  160. }
  161. );
  162. });
  163. describe('urls2links', function () {
  164. after(function () {
  165. cleanup();
  166. });
  167. jsc.property(
  168. 'ignores non-URL content',
  169. 'string',
  170. function (content) {
  171. var element = $('<div>' + content + '</div>'),
  172. before = element.html();
  173. $.PrivateBin.helper.urls2links(element);
  174. return before === element.html();
  175. }
  176. );
  177. jsc.property(
  178. 'replaces URLs with anchors',
  179. 'string',
  180. jsc.elements(['http', 'https', 'ftp']),
  181. jsc.nearray(jsc.elements(a2zString)),
  182. jsc.array(jsc.elements(queryString)),
  183. jsc.array(jsc.elements(queryString)),
  184. 'string',
  185. function (prefix, schema, address, query, fragment, postfix) {
  186. var query = query.join(''),
  187. fragment = fragment.join(''),
  188. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  189. prefix = $.PrivateBin.helper.htmlEntities(prefix),
  190. postfix = ' ' + $.PrivateBin.helper.htmlEntities(postfix),
  191. element = $('<div>' + prefix + url + postfix + '</div>');
  192. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  193. if (
  194. query.slice(-1) === '&' &&
  195. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  196. )
  197. {
  198. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  199. postfix = '';
  200. element = $('<div>' + prefix + url + '</div>');
  201. }
  202. $.PrivateBin.helper.urls2links(element);
  203. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix + '</div>').html();
  204. }
  205. );
  206. jsc.property(
  207. 'replaces magnet links with anchors',
  208. 'string',
  209. jsc.array(jsc.elements(queryString)),
  210. 'string',
  211. function (prefix, query, postfix) {
  212. var url = 'magnet:?' + query.join(''),
  213. prefix = $.PrivateBin.helper.htmlEntities(prefix),
  214. postfix = $.PrivateBin.helper.htmlEntities(postfix),
  215. element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
  216. $.PrivateBin.helper.urls2links(element);
  217. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
  218. }
  219. );
  220. });
  221. describe('sprintf', function () {
  222. after(function () {
  223. cleanup();
  224. });
  225. jsc.property(
  226. 'replaces %s in strings with first given parameter',
  227. 'string',
  228. '(small nearray) string',
  229. 'string',
  230. function (prefix, params, postfix) {
  231. var prefix = prefix.replace(/%(s|d)/g, '%%'),
  232. postfix = postfix.replace(/%(s|d)/g, '%%'),
  233. result = prefix + params[0] + postfix;
  234. params.unshift(prefix + '%s' + postfix);
  235. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  236. result === $.PrivateBin.helper.sprintf(params);
  237. }
  238. );
  239. jsc.property(
  240. 'replaces %d in strings with first given parameter',
  241. 'string',
  242. '(small nearray) nat',
  243. 'string',
  244. function (prefix, params, postfix) {
  245. var prefix = prefix.replace(/%(s|d)/g, '%%'),
  246. postfix = postfix.replace(/%(s|d)/g, '%%'),
  247. result = prefix + params[0] + postfix;
  248. params.unshift(prefix + '%d' + postfix);
  249. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  250. result === $.PrivateBin.helper.sprintf(params);
  251. }
  252. );
  253. jsc.property(
  254. 'replaces %d in strings with 0 if first parameter is not a number',
  255. 'string',
  256. '(small nearray) falsy',
  257. 'string',
  258. function (prefix, params, postfix) {
  259. var prefix = prefix.replace(/%(s|d)/g, '%%'),
  260. postfix = postfix.replace(/%(s|d)/g, '%%'),
  261. result = prefix + '0' + postfix;
  262. params.unshift(prefix + '%d' + postfix);
  263. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  264. result === $.PrivateBin.helper.sprintf(params);
  265. }
  266. );
  267. jsc.property(
  268. 'replaces %d and %s in strings in order',
  269. 'string',
  270. 'nat',
  271. 'string',
  272. 'string',
  273. 'string',
  274. function (prefix, uint, middle, string, postfix) {
  275. var prefix = prefix.replace(/%(s|d)/g, '%%'),
  276. postfix = postfix.replace(/%(s|d)/g, '%%'),
  277. params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  278. result = prefix + uint + middle + string + postfix;
  279. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  280. result === $.PrivateBin.helper.sprintf(params);
  281. }
  282. );
  283. jsc.property(
  284. 'replaces %d and %s in strings in reverse order',
  285. 'string',
  286. 'nat',
  287. 'string',
  288. 'string',
  289. 'string',
  290. function (prefix, uint, middle, string, postfix) {
  291. var prefix = prefix.replace(/%(s|d)/g, '%%'),
  292. postfix = postfix.replace(/%(s|d)/g, '%%'),
  293. params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  294. result = prefix + string + middle + uint + postfix;
  295. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  296. result === $.PrivateBin.helper.sprintf(params);
  297. }
  298. );
  299. });
  300. describe('scriptLocation', function () {
  301. jsc.property(
  302. 'returns the URL without query & fragment',
  303. jsc.nearray(jsc.elements(a2zString)),
  304. jsc.nearray(jsc.elements(a2zString)),
  305. jsc.array(jsc.elements(queryString)),
  306. 'string',
  307. function (schema, address, query, fragment) {
  308. var expected = schema.join('') + '://' + address.join('') + '/',
  309. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  310. result = $.PrivateBin.helper.scriptLocation();
  311. clean();
  312. return expected === result;
  313. }
  314. );
  315. });
  316. describe('pasteId', function () {
  317. jsc.property(
  318. 'returns the query string without separator, if any',
  319. jsc.nearray(jsc.elements(a2zString)),
  320. jsc.nearray(jsc.elements(a2zString)),
  321. jsc.array(jsc.elements(queryString)),
  322. 'string',
  323. function (schema, address, query, fragment) {
  324. var queryString = query.join(''),
  325. clean = jsdom('', {
  326. url: schema.join('') + '://' + address.join('') +
  327. '/?' + queryString + '#' + fragment
  328. }),
  329. result = $.PrivateBin.helper.pasteId();
  330. clean();
  331. return queryString === result;
  332. }
  333. );
  334. });
  335. describe('pageKey', function () {
  336. jsc.property(
  337. 'returns the fragment of the URL',
  338. jsc.nearray(jsc.elements(a2zString)),
  339. jsc.nearray(jsc.elements(a2zString)),
  340. jsc.array(jsc.elements(queryString)),
  341. jsc.array(jsc.elements(base64String)),
  342. function (schema, address, query, fragment) {
  343. var fragmentString = fragment.join(''),
  344. clean = jsdom('', {
  345. url: schema.join('') + '://' + address.join('') +
  346. '/?' + query.join('') + '#' + fragmentString
  347. }),
  348. result = $.PrivateBin.helper.pageKey();
  349. clean();
  350. return fragmentString === result;
  351. }
  352. );
  353. jsc.property(
  354. 'returns the fragment stripped of trailing query parts',
  355. jsc.nearray(jsc.elements(a2zString)),
  356. jsc.nearray(jsc.elements(a2zString)),
  357. jsc.array(jsc.elements(queryString)),
  358. jsc.array(jsc.elements(base64String)),
  359. jsc.array(jsc.elements(queryString)),
  360. function (schema, address, query, fragment, trail) {
  361. var fragmentString = fragment.join(''),
  362. clean = jsdom('', {
  363. url: schema.join('') + '://' + address.join('') + '/?' +
  364. query.join('') + '#' + fragmentString + '&' + trail.join('')
  365. }),
  366. result = $.PrivateBin.helper.pageKey();
  367. clean();
  368. return fragmentString === result;
  369. }
  370. );
  371. });
  372. describe('htmlEntities', function () {
  373. after(function () {
  374. cleanup();
  375. });
  376. jsc.property(
  377. 'removes all HTML entities from any given string',
  378. 'string',
  379. function (string) {
  380. var result = $.PrivateBin.helper.htmlEntities(string);
  381. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  382. }
  383. );
  384. });
  385. });