test_security-info-state.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Tests that security info parser gives correct general security state for
  5. // different cases.
  6. const { require } = Components.utils.import("resource://devtools/shared/Loader.jsm", {});
  7. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  8. Object.defineProperty(this, "NetworkHelper", {
  9. get: function () {
  10. return require("devtools/shared/webconsole/network-helper");
  11. },
  12. configurable: true,
  13. writeable: false,
  14. enumerable: true
  15. });
  16. var Ci = Components.interfaces;
  17. const wpl = Ci.nsIWebProgressListener;
  18. const MockSecurityInfo = {
  19. QueryInterface: XPCOMUtils.generateQI([Ci.nsITransportSecurityInfo,
  20. Ci.nsISSLStatusProvider]),
  21. securityState: wpl.STATE_IS_BROKEN,
  22. errorCode: 0,
  23. SSLStatus: {
  24. protocolVersion: 3, // nsISSLStatus.TLS_VERSION_1_2
  25. cipherSuite: "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256",
  26. }
  27. };
  28. function run_test() {
  29. test_nullSecurityInfo();
  30. test_insecureSecurityInfoWithNSSError();
  31. test_insecureSecurityInfoWithoutNSSError();
  32. test_brokenSecurityInfo();
  33. test_secureSecurityInfo();
  34. }
  35. /**
  36. * Test that undefined security information is returns "insecure".
  37. */
  38. function test_nullSecurityInfo() {
  39. let result = NetworkHelper.parseSecurityInfo(null, {});
  40. equal(result.state, "insecure",
  41. "state == 'insecure' when securityInfo was undefined");
  42. }
  43. /**
  44. * Test that STATE_IS_INSECURE with NSSError returns "broken"
  45. */
  46. function test_insecureSecurityInfoWithNSSError() {
  47. MockSecurityInfo.securityState = wpl.STATE_IS_INSECURE;
  48. // Taken from security/manager/ssl/tests/unit/head_psm.js.
  49. MockSecurityInfo.errorCode = -8180;
  50. let result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {});
  51. equal(result.state, "broken",
  52. "state == 'broken' if securityState contains STATE_IS_INSECURE flag AND " +
  53. "errorCode is NSS error.");
  54. MockSecurityInfo.errorCode = 0;
  55. }
  56. /**
  57. * Test that STATE_IS_INSECURE without NSSError returns "insecure"
  58. */
  59. function test_insecureSecurityInfoWithoutNSSError() {
  60. MockSecurityInfo.securityState = wpl.STATE_IS_INSECURE;
  61. let result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {});
  62. equal(result.state, "insecure",
  63. "state == 'insecure' if securityState contains STATE_IS_INSECURE flag BUT " +
  64. "errorCode is not NSS error.");
  65. }
  66. /**
  67. * Test that STATE_IS_SECURE returns "secure"
  68. */
  69. function test_secureSecurityInfo() {
  70. MockSecurityInfo.securityState = wpl.STATE_IS_SECURE;
  71. let result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {});
  72. equal(result.state, "secure",
  73. "state == 'secure' if securityState contains STATE_IS_SECURE flag");
  74. }
  75. /**
  76. * Test that STATE_IS_BROKEN returns "weak"
  77. */
  78. function test_brokenSecurityInfo() {
  79. MockSecurityInfo.securityState = wpl.STATE_IS_BROKEN;
  80. let result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {});
  81. equal(result.state, "weak",
  82. "state == 'weak' if securityState contains STATE_IS_BROKEN flag");
  83. }