test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. jsc.property(
  91. 'replaces the content of an element',
  92. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  93. 'nearray string',
  94. 'string',
  95. function (ids, contents, replacingContent) {
  96. var html = '',
  97. result = true;
  98. ids.forEach(function(item, i) {
  99. html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  100. });
  101. var clean = jsdom(html);
  102. ids.forEach(function(item, i) {
  103. var id = item.join(''),
  104. element = $(document.getElementById(id));
  105. $.PrivateBin.helper.setElementText(element, replacingContent);
  106. result *= replacingContent === $(document.getElementById(id)).text();
  107. });
  108. clean();
  109. return Boolean(result);
  110. }
  111. );
  112. });
  113. describe('setMessage', function () {
  114. jsc.property(
  115. 'replaces the content of an empty element, analog to setElementText',
  116. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  117. 'nearray string',
  118. 'string',
  119. function (ids, contents, replacingContent) {
  120. var html = '',
  121. result = true;
  122. ids.forEach(function(item, i) {
  123. html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  124. });
  125. var clean = jsdom(html);
  126. ids.forEach(function(item, i) {
  127. var id = item.join(''),
  128. element = $(document.getElementById(id));
  129. $.PrivateBin.helper.setMessage(element, replacingContent);
  130. result *= replacingContent === $(document.getElementById(id)).text();
  131. });
  132. clean();
  133. return Boolean(result);
  134. }
  135. );
  136. jsc.property(
  137. 'replaces the last child of a non-empty element',
  138. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  139. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  140. 'nearray string',
  141. 'string',
  142. function (ids, classes, contents, replacingContent) {
  143. var html = '',
  144. result = true;
  145. ids.forEach(function(item, i) {
  146. html += '<div id="' + item.join('') + '"><span class="' + (classes[i] || classes[0]) + '"></span> ' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  147. });
  148. var clean = jsdom(html);
  149. ids.forEach(function(item, i) {
  150. var id = item.join(''),
  151. element = $(document.getElementById(id));
  152. $.PrivateBin.helper.setMessage(element, replacingContent);
  153. result *= ' ' + replacingContent === $(document.getElementById(id)).contents()[1].nodeValue;
  154. });
  155. clean();
  156. return Boolean(result);
  157. }
  158. );
  159. });
  160. describe('urls2links', function () {
  161. jsc.property(
  162. 'ignores non-URL content',
  163. 'string',
  164. function (content) {
  165. var element = $('<div>' + content + '</div>'),
  166. before = element.html();
  167. $.PrivateBin.helper.urls2links(element);
  168. return before === element.html();
  169. }
  170. );
  171. jsc.property(
  172. 'replaces URLs with anchors',
  173. 'string',
  174. jsc.elements(['http', 'https', 'ftp']),
  175. jsc.nearray(jsc.elements(a2zString)),
  176. jsc.array(jsc.elements(queryString)),
  177. jsc.array(jsc.elements(queryString)),
  178. 'string',
  179. function (prefix, schema, address, query, fragment, postfix) {
  180. var query = query.join(''),
  181. fragment = fragment.join(''),
  182. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  183. prefix = $.PrivateBin.helper.htmlEntities(prefix),
  184. postfix = ' ' + $.PrivateBin.helper.htmlEntities(postfix),
  185. element = $('<div>' + prefix + url + postfix + '</div>');
  186. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  187. if (
  188. query.slice(-1) === '&' &&
  189. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  190. )
  191. {
  192. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  193. postfix = '';
  194. element = $('<div>' + prefix + url + '</div>');
  195. }
  196. $.PrivateBin.helper.urls2links(element);
  197. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix + '</div>').html();
  198. }
  199. );
  200. jsc.property(
  201. 'replaces magnet links with anchors',
  202. 'string',
  203. jsc.array(jsc.elements(queryString)),
  204. 'string',
  205. function (prefix, query, postfix) {
  206. var url = 'magnet:?' + query.join(''),
  207. prefix = $.PrivateBin.helper.htmlEntities(prefix),
  208. postfix = $.PrivateBin.helper.htmlEntities(postfix),
  209. element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
  210. $.PrivateBin.helper.urls2links(element);
  211. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
  212. }
  213. );
  214. });
  215. describe('scriptLocation', function () {
  216. jsc.property(
  217. 'returns the URL without query & fragment',
  218. jsc.nearray(jsc.elements(a2zString)),
  219. jsc.nearray(jsc.elements(a2zString)),
  220. jsc.array(jsc.elements(queryString)),
  221. 'string',
  222. function (schema, address, query, fragment) {
  223. var expected = schema.join('') + '://' + address.join('') + '/',
  224. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  225. result = $.PrivateBin.helper.scriptLocation();
  226. clean();
  227. return expected === result;
  228. }
  229. );
  230. });
  231. describe('pasteId', function () {
  232. jsc.property(
  233. 'returns the query string without separator, if any',
  234. jsc.nearray(jsc.elements(a2zString)),
  235. jsc.nearray(jsc.elements(a2zString)),
  236. jsc.array(jsc.elements(queryString)),
  237. 'string',
  238. function (schema, address, query, fragment) {
  239. var queryString = query.join(''),
  240. clean = jsdom('', {
  241. url: schema.join('') + '://' + address.join('') +
  242. '/?' + queryString + '#' + fragment
  243. }),
  244. result = $.PrivateBin.helper.pasteId();
  245. clean();
  246. return queryString === result;
  247. }
  248. );
  249. });
  250. describe('pageKey', function () {
  251. jsc.property(
  252. 'returns the fragment of the URL',
  253. jsc.nearray(jsc.elements(a2zString)),
  254. jsc.nearray(jsc.elements(a2zString)),
  255. jsc.array(jsc.elements(queryString)),
  256. jsc.array(jsc.elements(base64String)),
  257. function (schema, address, query, fragment) {
  258. var fragmentString = fragment.join(''),
  259. clean = jsdom('', {
  260. url: schema.join('') + '://' + address.join('') +
  261. '/?' + query.join('') + '#' + fragmentString
  262. }),
  263. result = $.PrivateBin.helper.pageKey();
  264. clean();
  265. return fragmentString === result;
  266. }
  267. );
  268. jsc.property(
  269. 'returns the fragment stripped of trailing query parts',
  270. jsc.nearray(jsc.elements(a2zString)),
  271. jsc.nearray(jsc.elements(a2zString)),
  272. jsc.array(jsc.elements(queryString)),
  273. jsc.array(jsc.elements(base64String)),
  274. jsc.array(jsc.elements(queryString)),
  275. function (schema, address, query, fragment, trail) {
  276. var fragmentString = fragment.join(''),
  277. clean = jsdom('', {
  278. url: schema.join('') + '://' + address.join('') + '/?' +
  279. query.join('') + '#' + fragmentString + '&' + trail.join('')
  280. }),
  281. result = $.PrivateBin.helper.pageKey();
  282. clean();
  283. return fragmentString === result;
  284. }
  285. );
  286. });
  287. describe('htmlEntities', function () {
  288. after(function () {
  289. cleanup();
  290. });
  291. jsc.property(
  292. 'removes all HTML entities from any given string',
  293. 'string',
  294. function (string) {
  295. var result = $.PrivateBin.helper.htmlEntities(string);
  296. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  297. }
  298. );
  299. });
  300. });