test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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('scriptLocation', function () {
  161. jsc.property(
  162. 'returns the URL without query & fragment',
  163. jsc.nearray(jsc.elements(a2zString)),
  164. jsc.nearray(jsc.elements(a2zString)),
  165. jsc.array(jsc.elements(queryString)),
  166. 'string',
  167. function (schema, address, query, fragment) {
  168. var expected = schema.join('') + '://' + address.join('') + '/',
  169. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  170. result = $.PrivateBin.helper.scriptLocation();
  171. clean();
  172. return expected === result;
  173. }
  174. );
  175. });
  176. describe('pasteId', function () {
  177. jsc.property(
  178. 'returns the query string without separator, if any',
  179. jsc.nearray(jsc.elements(a2zString)),
  180. jsc.nearray(jsc.elements(a2zString)),
  181. jsc.array(jsc.elements(queryString)),
  182. 'string',
  183. function (schema, address, query, fragment) {
  184. var queryString = query.join(''),
  185. clean = jsdom('', {
  186. url: schema.join('') + '://' + address.join('') +
  187. '/?' + queryString + '#' + fragment
  188. }),
  189. result = $.PrivateBin.helper.pasteId();
  190. clean();
  191. return queryString === result;
  192. }
  193. );
  194. });
  195. describe('pageKey', function () {
  196. jsc.property(
  197. 'returns the fragment of the URL',
  198. jsc.nearray(jsc.elements(a2zString)),
  199. jsc.nearray(jsc.elements(a2zString)),
  200. jsc.array(jsc.elements(queryString)),
  201. jsc.array(jsc.elements(base64String)),
  202. function (schema, address, query, fragment) {
  203. var fragmentString = fragment.join(''),
  204. clean = jsdom('', {
  205. url: schema.join('') + '://' + address.join('') +
  206. '/?' + query.join('') + '#' + fragmentString
  207. }),
  208. result = $.PrivateBin.helper.pageKey();
  209. clean();
  210. return fragmentString === result;
  211. }
  212. );
  213. jsc.property(
  214. 'returns the fragment stripped of trailing query parts',
  215. jsc.nearray(jsc.elements(a2zString)),
  216. jsc.nearray(jsc.elements(a2zString)),
  217. jsc.array(jsc.elements(queryString)),
  218. jsc.array(jsc.elements(base64String)),
  219. jsc.array(jsc.elements(queryString)),
  220. function (schema, address, query, fragment, trail) {
  221. var fragmentString = fragment.join(''),
  222. clean = jsdom('', {
  223. url: schema.join('') + '://' + address.join('') + '/?' +
  224. query.join('') + '#' + fragmentString + '&' + trail.join('')
  225. }),
  226. result = $.PrivateBin.helper.pageKey();
  227. clean();
  228. return fragmentString === result;
  229. }
  230. );
  231. });
  232. describe('htmlEntities', function () {
  233. after(function () {
  234. cleanup();
  235. });
  236. jsc.property(
  237. 'removes all HTML entities from any given string',
  238. 'string',
  239. function (string) {
  240. var result = $.PrivateBin.helper.htmlEntities(string);
  241. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  242. }
  243. );
  244. });
  245. });