#include "components/security_interstitials/content/ssl_error_handler.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#include "components/captive_portal/content/captive_portal_service.h"
#include "components/captive_portal/core/buildflags.h"
#include "components/captive_portal/core/captive_portal_testing_utils.h"
#include "components/embedder_support/pref_names.h"
#include "components/network_time/network_time_test_utils.h"
#include "components/network_time/network_time_tracker.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/security_interstitials/content/common_name_mismatch_handler.h"
#include "components/security_interstitials/content/ssl_error_assistant.h"
#include "components/security_interstitials/content/ssl_error_assistant.pb.h"
#include "components/security_interstitials/core/ssl_error_options_mask.h"
#include "components/security_interstitials/core/ssl_error_ui.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/test_renderer_host.h"
#include "net/base/net_errors.h"
#include "net/cert/cert_status_flags.h"
#include "net/cert/x509_certificate.h"
#include "net/http/http_response_headers.h"
#include "net/ssl/ssl_info.h"
#include "net/test/cert_test_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/test/test_certificate_data.h"
#include "net/test/test_data_directory.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const net::SHA256HashValue kCertPublicKeyHashValue = …;
const char kOkayCertName[] = …;
const uint32_t kLargeVersionId = …;
const char kMisconfiguredFirewallCert[] = …;
const char kCertWithoutOrganizationOrCommonName[] = …;
std::unique_ptr<net::test_server::HttpResponse> WaitForRequest(
base::OnceClosure quit_closure,
const net::test_server::HttpRequest& request) { … }
class TestSSLErrorHandler : public SSLErrorHandler { … };
class TestSSLErrorHandlerDelegate : public SSLErrorHandler::Delegate { … };
}
class SSLErrorHandlerNameMismatchTest
: public content::RenderViewHostTestHarness { … };
class SSLErrorHandlerNameMismatchNoSANTest
: public SSLErrorHandlerNameMismatchTest { … };
class SSLErrorAssistantProtoTest : public content::RenderViewHostTestHarness { … };
class SSLErrorAssistantProtoCaptivePortalEnabledTest
: public SSLErrorAssistantProtoTest { … };
class SSLErrorAssistantProtoCaptivePortalDisabledTest
: public SSLErrorAssistantProtoTest { … };
class SSLErrorAssistantProtoMITMSoftwareEnabledTest
: public SSLErrorAssistantProtoTest { … };
class SSLErrorAssistantProtoMITMSoftwareDisabledTest
: public SSLErrorAssistantProtoTest { … };
class SSLErrorHandlerDateInvalidTest
: public content::RenderViewHostTestHarness { … };
#if BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION)
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldShowSSLInterstitialOnTimerExpired) { … }
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldShowCustomInterstitialOnCaptivePortalResult) { … }
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldShowSSLInterstitialOnNoCaptivePortalResult) { … }
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldNotCheckSuggestedUrlIfNoSuggestedUrl) { … }
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldNotCheckCaptivePortalIfSuggestedUrlExists) { … }
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldNotHandleNameMismatchOnNonOverridableError) { … }
#else
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) {
base::HistogramTester histograms;
EXPECT_FALSE(error_handler()->IsTimerRunningForTesting());
error_handler()->StartHandlingError();
EXPECT_FALSE(error_handler()->IsTimerRunningForTesting());
EXPECT_FALSE(delegate()->captive_portal_checked());
EXPECT_TRUE(delegate()->ssl_interstitial_shown());
EXPECT_FALSE(delegate()->captive_portal_interstitial_shown());
histograms.ExpectTotalCount(SSLErrorHandler::GetHistogramNameForTesting(), 2);
histograms.ExpectBucketCount(SSLErrorHandler::GetHistogramNameForTesting(),
SSLErrorHandler::HANDLE_ALL, 1);
histograms.ExpectBucketCount(
SSLErrorHandler::GetHistogramNameForTesting(),
SSLErrorHandler::SHOW_SSL_INTERSTITIAL_OVERRIDABLE, 1);
}
#endif
TEST_F(SSLErrorHandlerNameMismatchTest, OSReportsCaptivePortal) { … }
class SSLErrorHandlerNameMismatchCaptivePortalInterstitialDisabledTest
: public SSLErrorHandlerNameMismatchTest { … };
TEST_F(SSLErrorHandlerNameMismatchCaptivePortalInterstitialDisabledTest,
OSReportsCaptivePortal_FeatureDisabled) { … }
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldShowSSLInterstitialOnTimerExpiredWhenSuggestedUrlExists) { … }
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldRedirectOnSuggestedUrlCheckResult) { … }
TEST_F(SSLErrorHandlerNameMismatchNoSANTest,
SSLCommonNameMismatchHandlingRequiresSubjectAltName) { … }
TEST_F(SSLErrorHandlerNameMismatchTest,
ShouldShowSSLInterstitialOnInvalidUrlCheckResult) { … }
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(THREAD_SANITIZER)
#define MAYBE_TimeQueryStarted …
#else
#define MAYBE_TimeQueryStarted …
#endif
TEST_F(SSLErrorHandlerDateInvalidTest, MAYBE_TimeQueryStarted) { … }
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(THREAD_SANITIZER)
#define MAYBE_NoTimeQueries …
#else
#define MAYBE_NoTimeQueries …
#endif
TEST_F(SSLErrorHandlerDateInvalidTest, MAYBE_NoTimeQueries) { … }
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(THREAD_SANITIZER)
#define MAYBE_TimeQueryHangs …
#else
#define MAYBE_TimeQueryHangs …
#endif
TEST_F(SSLErrorHandlerDateInvalidTest, MAYBE_TimeQueryHangs) { … }
TEST_F(SSLErrorAssistantProtoCaptivePortalEnabledTest,
CaptivePortal_FeatureEnabled) { … }
TEST_F(SSLErrorAssistantProtoCaptivePortalDisabledTest,
CaptivePortal_FeatureDisabled) { … }
TEST_F(SSLErrorAssistantProtoCaptivePortalEnabledTest,
CaptivePortal_AuthorityInvalidError_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoCaptivePortalEnabledTest,
CaptivePortal_TwoErrors_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoCaptivePortalEnabledTest,
CaptivePortal_TwoErrorsIncludingNameMismatch_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_CertificateDoesNotMatchOrganizationName_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_CertificateDoesNotMatchCommonName_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_CertificateWithNoOrganizationOrCommonName_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_CertificateMatchesCommonNameAndOrganizationName) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_CertificateMatchesCommonName) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_CertificateMatchesOrganizationName) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_PartialRegexMatch_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_NonMatchingCertificate_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareDisabledTest,
MITMSoftware_FeatureDisabled) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_WrongError_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_TwoErrors_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_Overridable_NoInterstitial) { … }
TEST_F(SSLErrorAssistantProtoMITMSoftwareEnabledTest,
MITMSoftware_IgnoreDynamicUpdateWithSmallVersionId) { … }
SSLErrorHandlerTest;
TEST_F(SSLErrorHandlerTest, BlockedInterceptionInterstitial) { … }
TEST_F(SSLErrorHandlerTest, NonPrimaryMainframeShouldNotAffectSSLErrorHandler) { … }