test.js 16 KB

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