Check.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. var common = require('../common');
  3. /* global Legacy, WebCrypto */
  4. describe('Check', function () {
  5. describe('init', function () {
  6. this.timeout(30000);
  7. it('returns false and shows error, if a bot UA is detected', function () {
  8. jsc.assert(jsc.forall(
  9. 'string',
  10. jsc.elements(['Bot', 'bot']),
  11. 'string',
  12. function (prefix, botBit, suffix) {
  13. const clean = globalThis.cleanup(
  14. '<html><body><div id="errormessage" class="hidden"></div>' +
  15. '</body></html>', {
  16. 'userAgent': prefix + botBit + suffix
  17. }
  18. );
  19. Legacy.Check.init();
  20. const result = Legacy.Check.getInit() && !Legacy.Check.getStatus();
  21. clean();
  22. return result;
  23. }
  24. ),
  25. {tests: 10});
  26. });
  27. jsc.property(
  28. 'shows error, if no webcrypto is detected',
  29. 'bool',
  30. jsc.elements(['localhost', '127.0.0.1', '[::1]', '']),
  31. jsc.nearray(common.jscA2zString()),
  32. jsc.elements(['.onion', '.i2p', '']),
  33. function (secureProtocol, localhost, domain, tld) {
  34. const isDomain = localhost === '',
  35. isSecureContext = secureProtocol || !isDomain || tld.length > 0,
  36. clean = globalThis.cleanup(
  37. '<html><body><div id="errormessage" class="hidden"></div>' +
  38. '<div id="oldnotice" class="hidden"></div>' +
  39. '<div id="insecurecontextnotice" class="hidden"></div></body></html>',
  40. {
  41. 'url': (secureProtocol ? 'https' : 'http' ) + '://' +
  42. (isDomain ? domain.join('') + tld : localhost) + '/'
  43. }
  44. );
  45. Legacy.Check.init();
  46. const result1 = Legacy.Check.getInit() && !Legacy.Check.getStatus(),
  47. result2 = isSecureContext === (document.getElementById('insecurecontextnotice').className === 'hidden'),
  48. result3 = (document.getElementById('oldnotice').className !== 'hidden');
  49. clean();
  50. if (result1 && result2 && result3) {
  51. return true;
  52. }
  53. console.log(result1, result2, result3);
  54. return false;
  55. }
  56. );
  57. jsc.property(
  58. 'shows error, if HTTP only site is detected',
  59. 'bool',
  60. jsc.nearray(common.jscA2zString()),
  61. function (secureProtocol, domain) {
  62. const clean = globalThis.cleanup(
  63. '<html><body><div id="httpnotice" class="hidden"></div>' +
  64. '</body></html>',
  65. {
  66. 'url': (secureProtocol ? 'https' : 'http' ) + '://' + domain.join('') + '/'
  67. }
  68. );
  69. Object.defineProperty(window, 'crypto', {
  70. value: new WebCrypto(),
  71. writeable: false
  72. });
  73. Legacy.Check.init();
  74. const result1 = Legacy.Check.getInit() && Legacy.Check.getStatus(),
  75. result2 = secureProtocol === (document.getElementById('httpnotice').className === 'hidden');
  76. clean();
  77. if (result1 && result2) {
  78. return true;
  79. }
  80. console.log(result1, result2);
  81. return false;
  82. }
  83. );
  84. });
  85. });