test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. params.unshift(prefix + '%s' + postfix);
  232. var result = prefix + params[1] + postfix;
  233. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  234. result === $.PrivateBin.helper.sprintf(params);
  235. }
  236. );
  237. jsc.property(
  238. 'replaces %d in strings with first given parameter',
  239. 'string',
  240. '(small nearray) nat',
  241. 'string',
  242. function (prefix, params, postfix) {
  243. params.unshift(prefix + '%d' + postfix);
  244. var result = prefix + params[1] + postfix;
  245. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  246. result === $.PrivateBin.helper.sprintf(params);
  247. }
  248. );
  249. jsc.property(
  250. 'replaces %d in strings with 0 if first parameter is not a number',
  251. 'string',
  252. '(small nearray) falsy',
  253. 'string',
  254. function (prefix, params, postfix) {
  255. params.unshift(prefix + '%d' + postfix);
  256. var result = prefix + '0' + postfix;
  257. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  258. result === $.PrivateBin.helper.sprintf(params);
  259. }
  260. );
  261. jsc.property(
  262. 'replaces %d and %s in strings in order',
  263. 'string',
  264. 'nat',
  265. 'string',
  266. 'string',
  267. 'string',
  268. function (prefix, uint, middle, string, postfix) {
  269. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  270. result = prefix + uint + middle + string + postfix;
  271. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  272. result === $.PrivateBin.helper.sprintf(params);
  273. }
  274. );
  275. jsc.property(
  276. 'replaces %d and %s in strings in reverse order',
  277. 'string',
  278. 'nat',
  279. 'string',
  280. 'string',
  281. 'string',
  282. function (prefix, uint, middle, string, postfix) {
  283. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  284. result = prefix + string + middle + uint + postfix;
  285. return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
  286. result === $.PrivateBin.helper.sprintf(params);
  287. }
  288. );
  289. });
  290. describe('scriptLocation', function () {
  291. jsc.property(
  292. 'returns the URL without query & fragment',
  293. jsc.nearray(jsc.elements(a2zString)),
  294. jsc.nearray(jsc.elements(a2zString)),
  295. jsc.array(jsc.elements(queryString)),
  296. 'string',
  297. function (schema, address, query, fragment) {
  298. var expected = schema.join('') + '://' + address.join('') + '/',
  299. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  300. result = $.PrivateBin.helper.scriptLocation();
  301. clean();
  302. return expected === result;
  303. }
  304. );
  305. });
  306. describe('pasteId', function () {
  307. jsc.property(
  308. 'returns the query string without separator, if any',
  309. jsc.nearray(jsc.elements(a2zString)),
  310. jsc.nearray(jsc.elements(a2zString)),
  311. jsc.array(jsc.elements(queryString)),
  312. 'string',
  313. function (schema, address, query, fragment) {
  314. var queryString = query.join(''),
  315. clean = jsdom('', {
  316. url: schema.join('') + '://' + address.join('') +
  317. '/?' + queryString + '#' + fragment
  318. }),
  319. result = $.PrivateBin.helper.pasteId();
  320. clean();
  321. return queryString === result;
  322. }
  323. );
  324. });
  325. describe('pageKey', function () {
  326. jsc.property(
  327. 'returns the fragment of the URL',
  328. jsc.nearray(jsc.elements(a2zString)),
  329. jsc.nearray(jsc.elements(a2zString)),
  330. jsc.array(jsc.elements(queryString)),
  331. jsc.array(jsc.elements(base64String)),
  332. function (schema, address, query, fragment) {
  333. var fragmentString = fragment.join(''),
  334. clean = jsdom('', {
  335. url: schema.join('') + '://' + address.join('') +
  336. '/?' + query.join('') + '#' + fragmentString
  337. }),
  338. result = $.PrivateBin.helper.pageKey();
  339. clean();
  340. return fragmentString === result;
  341. }
  342. );
  343. jsc.property(
  344. 'returns the fragment stripped of trailing query parts',
  345. jsc.nearray(jsc.elements(a2zString)),
  346. jsc.nearray(jsc.elements(a2zString)),
  347. jsc.array(jsc.elements(queryString)),
  348. jsc.array(jsc.elements(base64String)),
  349. jsc.array(jsc.elements(queryString)),
  350. function (schema, address, query, fragment, trail) {
  351. var fragmentString = fragment.join(''),
  352. clean = jsdom('', {
  353. url: schema.join('') + '://' + address.join('') + '/?' +
  354. query.join('') + '#' + fragmentString + '&' + trail.join('')
  355. }),
  356. result = $.PrivateBin.helper.pageKey();
  357. clean();
  358. return fragmentString === result;
  359. }
  360. );
  361. });
  362. describe('htmlEntities', function () {
  363. after(function () {
  364. cleanup();
  365. });
  366. jsc.property(
  367. 'removes all HTML entities from any given string',
  368. 'string',
  369. function (string) {
  370. var result = $.PrivateBin.helper.htmlEntities(string);
  371. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  372. }
  373. );
  374. });
  375. });