PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/chrome/browser/ssl/ssl_error_handler_unittest.cc

https://gitlab.com/jonnialva90/iridium-browser
C++ | 339 lines | 270 code | 56 blank | 13 comment | 1 complexity | 1d17fee97f1840dc124fbc864633288e MD5 | raw file
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chrome/browser/ssl/ssl_error_handler.h"
  5. #include "base/callback.h"
  6. #include "base/message_loop/message_loop.h"
  7. #include "base/metrics/field_trial.h"
  8. #include "base/run_loop.h"
  9. #include "base/time/time.h"
  10. #include "chrome/browser/captive_portal/captive_portal_service.h"
  11. #include "chrome/browser/profiles/profile.h"
  12. #include "chrome/browser/ssl/common_name_mismatch_handler.h"
  13. #include "chrome/test/base/chrome_render_view_host_test_harness.h"
  14. #include "chrome/test/base/testing_profile.h"
  15. #include "components/captive_portal/captive_portal_testing_utils.h"
  16. #include "content/public/browser/notification_service.h"
  17. #include "net/base/net_errors.h"
  18. #include "net/base/test_data_directory.h"
  19. #include "net/cert/x509_certificate.h"
  20. #include "net/ssl/ssl_info.h"
  21. #include "net/test/cert_test_util.h"
  22. #include "net/test/test_certificate_data.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. class SSLErrorHandlerForTest : public SSLErrorHandler {
  25. public:
  26. SSLErrorHandlerForTest(Profile* profile,
  27. content::WebContents* web_contents,
  28. const net::SSLInfo& ssl_info)
  29. : SSLErrorHandler(web_contents,
  30. net::ERR_CERT_COMMON_NAME_INVALID,
  31. ssl_info,
  32. GURL(),
  33. 0,
  34. nullptr,
  35. base::Callback<void(bool)>()),
  36. profile_(profile),
  37. captive_portal_checked_(false),
  38. suggested_url_exists_(false),
  39. suggested_url_checked_(false),
  40. ssl_interstitial_shown_(false),
  41. captive_portal_interstitial_shown_(false),
  42. redirected_to_suggested_url_(false),
  43. is_overridable_error_(true) {}
  44. using SSLErrorHandler::StartHandlingError;
  45. void SendCaptivePortalNotification(
  46. captive_portal::CaptivePortalResult result) {
  47. CaptivePortalService::Results results;
  48. results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED;
  49. results.result = result;
  50. content::NotificationService::current()->Notify(
  51. chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
  52. content::Source<Profile>(profile_),
  53. content::Details<CaptivePortalService::Results>(&results));
  54. }
  55. void SendSuggestedUrlCheckResult(
  56. const CommonNameMismatchHandler::SuggestedUrlCheckResult& result,
  57. const GURL& suggested_url) {
  58. CommonNameMismatchHandlerCallback(result, suggested_url);
  59. }
  60. bool IsTimerRunning() const { return get_timer().IsRunning(); }
  61. int captive_portal_checked() const { return captive_portal_checked_; }
  62. int ssl_interstitial_shown() const { return ssl_interstitial_shown_; }
  63. int captive_portal_interstitial_shown() const {
  64. return captive_portal_interstitial_shown_;
  65. }
  66. bool suggested_url_checked() const { return suggested_url_checked_; }
  67. bool redirected_to_suggested_url() const {
  68. return redirected_to_suggested_url_;
  69. }
  70. void set_suggested_url_exists() { suggested_url_exists_ = true; }
  71. void set_non_overridable_error() { is_overridable_error_ = false; }
  72. void ClearSeenOperations() {
  73. captive_portal_checked_ = false;
  74. suggested_url_exists_ = false;
  75. suggested_url_checked_ = false;
  76. ssl_interstitial_shown_ = false;
  77. captive_portal_interstitial_shown_ = false;
  78. redirected_to_suggested_url_ = false;
  79. }
  80. private:
  81. void CheckForCaptivePortal() override {
  82. captive_portal_checked_ = true;
  83. }
  84. bool GetSuggestedUrl(const std::vector<std::string>& dns_names,
  85. GURL* suggested_url) const override {
  86. if (!suggested_url_exists_)
  87. return false;
  88. *suggested_url = GURL("www.example.com");
  89. return true;
  90. }
  91. void ShowSSLInterstitial() override { ssl_interstitial_shown_ = true; }
  92. void ShowCaptivePortalInterstitial(const GURL& landing_url) override {
  93. captive_portal_interstitial_shown_ = true;
  94. }
  95. void CheckSuggestedUrl(const GURL& suggested_url) override {
  96. suggested_url_checked_ = true;
  97. }
  98. void NavigateToSuggestedURL(const GURL& suggested_url) override {
  99. redirected_to_suggested_url_ = true;
  100. }
  101. bool IsErrorOverridable() const override { return is_overridable_error_; }
  102. Profile* profile_;
  103. bool captive_portal_checked_;
  104. bool suggested_url_exists_;
  105. bool suggested_url_checked_;
  106. bool ssl_interstitial_shown_;
  107. bool captive_portal_interstitial_shown_;
  108. bool redirected_to_suggested_url_;
  109. bool is_overridable_error_;
  110. DISALLOW_COPY_AND_ASSIGN(SSLErrorHandlerForTest);
  111. };
  112. class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness {
  113. public:
  114. SSLErrorHandlerTest() : field_trial_list_(nullptr) {}
  115. void SetUp() override {
  116. ChromeRenderViewHostTestHarness::SetUp();
  117. SSLErrorHandler::SetInterstitialDelayForTest(base::TimeDelta());
  118. ssl_info_.cert =
  119. net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem");
  120. ssl_info_.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID;
  121. error_handler_.reset(
  122. new SSLErrorHandlerForTest(profile(), web_contents(), ssl_info_));
  123. // Enable finch experiment for captive portal interstitials.
  124. ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
  125. "CaptivePortalInterstitial", "Enabled"));
  126. // Enable finch experiment for SSL common name mismatch handling.
  127. ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
  128. "SSLCommonNameMismatchHandling", "Enabled"));
  129. }
  130. void TearDown() override {
  131. EXPECT_FALSE(error_handler()->IsTimerRunning());
  132. error_handler_.reset(nullptr);
  133. ChromeRenderViewHostTestHarness::TearDown();
  134. }
  135. SSLErrorHandlerForTest* error_handler() { return error_handler_.get(); }
  136. private:
  137. net::SSLInfo ssl_info_;
  138. scoped_ptr<SSLErrorHandlerForTest> error_handler_;
  139. base::FieldTrialList field_trial_list_;
  140. };
  141. #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
  142. TEST_F(SSLErrorHandlerTest,
  143. ShouldShowSSLInterstitialOnTimerExpired) {
  144. EXPECT_FALSE(error_handler()->IsTimerRunning());
  145. error_handler()->StartHandlingError();
  146. EXPECT_TRUE(error_handler()->IsTimerRunning());
  147. EXPECT_TRUE(error_handler()->captive_portal_checked());
  148. EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
  149. EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
  150. error_handler()->ClearSeenOperations();
  151. base::MessageLoop::current()->RunUntilIdle();
  152. EXPECT_FALSE(error_handler()->IsTimerRunning());
  153. EXPECT_FALSE(error_handler()->captive_portal_checked());
  154. EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
  155. EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
  156. }
  157. TEST_F(SSLErrorHandlerTest,
  158. ShouldShowCustomInterstitialOnCaptivePortalResult) {
  159. EXPECT_FALSE(error_handler()->IsTimerRunning());
  160. error_handler()->StartHandlingError();
  161. EXPECT_TRUE(error_handler()->IsTimerRunning());
  162. EXPECT_TRUE(error_handler()->captive_portal_checked());
  163. EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
  164. EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
  165. // Fake a captive portal result.
  166. error_handler()->ClearSeenOperations();
  167. error_handler()->SendCaptivePortalNotification(
  168. captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL);
  169. base::MessageLoop::current()->RunUntilIdle();
  170. EXPECT_FALSE(error_handler()->IsTimerRunning());
  171. EXPECT_FALSE(error_handler()->captive_portal_checked());
  172. EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
  173. EXPECT_TRUE(error_handler()->captive_portal_interstitial_shown());
  174. }
  175. TEST_F(SSLErrorHandlerTest,
  176. ShouldShowSSLInterstitialOnNoCaptivePortalResult) {
  177. EXPECT_FALSE(error_handler()->IsTimerRunning());
  178. error_handler()->StartHandlingError();
  179. EXPECT_TRUE(error_handler()->IsTimerRunning());
  180. EXPECT_TRUE(error_handler()->captive_portal_checked());
  181. EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
  182. EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
  183. // Fake a "connected to internet" result for the captive portal check.
  184. // This should immediately trigger an SSL interstitial without waiting for
  185. // the timer to expire.
  186. error_handler()->ClearSeenOperations();
  187. error_handler()->SendCaptivePortalNotification(
  188. captive_portal::RESULT_INTERNET_CONNECTED);
  189. base::MessageLoop::current()->RunUntilIdle();
  190. EXPECT_FALSE(error_handler()->IsTimerRunning());
  191. EXPECT_FALSE(error_handler()->captive_portal_checked());
  192. EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
  193. EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
  194. }
  195. TEST_F(SSLErrorHandlerTest, ShouldNotCheckSuggestedUrlIfNoSuggestedUrl) {
  196. error_handler()->StartHandlingError();
  197. EXPECT_TRUE(error_handler()->captive_portal_checked());
  198. EXPECT_TRUE(error_handler()->IsTimerRunning());
  199. EXPECT_FALSE(error_handler()->suggested_url_checked());
  200. base::RunLoop().RunUntilIdle();
  201. EXPECT_FALSE(error_handler()->IsTimerRunning());
  202. EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
  203. }
  204. TEST_F(SSLErrorHandlerTest, ShouldNotCheckCaptivePortalIfSuggestedUrlExists) {
  205. EXPECT_FALSE(error_handler()->IsTimerRunning());
  206. error_handler()->set_suggested_url_exists();
  207. error_handler()->StartHandlingError();
  208. EXPECT_TRUE(error_handler()->IsTimerRunning());
  209. EXPECT_TRUE(error_handler()->suggested_url_checked());
  210. EXPECT_FALSE(error_handler()->captive_portal_checked());
  211. base::RunLoop().RunUntilIdle();
  212. EXPECT_FALSE(error_handler()->IsTimerRunning());
  213. EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
  214. }
  215. TEST_F(SSLErrorHandlerTest, ShouldNotHandleNameMismatchOnNonOverridableError) {
  216. error_handler()->set_non_overridable_error();
  217. error_handler()->set_suggested_url_exists();
  218. error_handler()->StartHandlingError();
  219. EXPECT_FALSE(error_handler()->suggested_url_checked());
  220. EXPECT_TRUE(error_handler()->captive_portal_checked());
  221. EXPECT_TRUE(error_handler()->IsTimerRunning());
  222. base::RunLoop().RunUntilIdle();
  223. EXPECT_FALSE(error_handler()->IsTimerRunning());
  224. EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
  225. }
  226. #else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
  227. TEST_F(SSLErrorHandlerTest,
  228. ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) {
  229. EXPECT_FALSE(error_handler()->IsTimerRunning());
  230. error_handler()->StartHandlingError();
  231. EXPECT_FALSE(error_handler()->IsTimerRunning());
  232. EXPECT_FALSE(error_handler()->captive_portal_checked());
  233. EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
  234. EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
  235. }
  236. #endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
  237. TEST_F(SSLErrorHandlerTest,
  238. ShouldShowSSLInterstitialOnTimerExpiredWhenSuggestedUrlExists) {
  239. error_handler()->set_suggested_url_exists();
  240. error_handler()->StartHandlingError();
  241. EXPECT_TRUE(error_handler()->IsTimerRunning());
  242. EXPECT_TRUE(error_handler()->suggested_url_checked());
  243. EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
  244. EXPECT_FALSE(error_handler()->redirected_to_suggested_url());
  245. base::RunLoop().RunUntilIdle();
  246. EXPECT_FALSE(error_handler()->IsTimerRunning());
  247. EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
  248. EXPECT_FALSE(error_handler()->redirected_to_suggested_url());
  249. }
  250. TEST_F(SSLErrorHandlerTest, ShouldRedirectOnSuggestedUrlCheckResult) {
  251. error_handler()->set_suggested_url_exists();
  252. error_handler()->StartHandlingError();
  253. EXPECT_TRUE(error_handler()->IsTimerRunning());
  254. EXPECT_TRUE(error_handler()->suggested_url_checked());
  255. EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
  256. EXPECT_FALSE(error_handler()->redirected_to_suggested_url());
  257. // Fake a valid suggested URL check result.
  258. // The URL returned by |SuggestedUrlCheckResult| can be different from
  259. // |suggested_url|, if there is a redirect.
  260. error_handler()->SendSuggestedUrlCheckResult(
  261. CommonNameMismatchHandler::SuggestedUrlCheckResult::
  262. SUGGESTED_URL_AVAILABLE,
  263. GURL("https://random.example.com"));
  264. EXPECT_FALSE(error_handler()->IsTimerRunning());
  265. EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
  266. EXPECT_TRUE(error_handler()->redirected_to_suggested_url());
  267. }
  268. TEST_F(SSLErrorHandlerTest, ShouldShowSSLInterstitialOnInvalidUrlCheckResult) {
  269. error_handler()->set_suggested_url_exists();
  270. error_handler()->StartHandlingError();
  271. EXPECT_TRUE(error_handler()->IsTimerRunning());
  272. EXPECT_TRUE(error_handler()->suggested_url_checked());
  273. EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
  274. EXPECT_FALSE(error_handler()->redirected_to_suggested_url());
  275. // Fake an Invalid Suggested URL Check result.
  276. error_handler()->SendSuggestedUrlCheckResult(
  277. CommonNameMismatchHandler::SuggestedUrlCheckResult::
  278. SUGGESTED_URL_NOT_AVAILABLE,
  279. GURL());
  280. EXPECT_FALSE(error_handler()->IsTimerRunning());
  281. EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
  282. EXPECT_FALSE(error_handler()->redirected_to_suggested_url());
  283. }