| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 | |
| 5 #include "chrome/browser/captive_portal/captive_portal_detector.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "chrome/browser/captive_portal/testing_utils.h" | |
| 12 #include "chrome/test/base/testing_profile.h" | |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | |
| 14 #include "net/base/net_errors.h" | |
| 15 #include "net/url_request/url_fetcher.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace captive_portal { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class CaptivePortalClient { | |
| 24 public: | |
| 25 explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector) | |
| 26 : num_results_received_(0) { | |
| 27 } | |
| 28 | |
| 29 void OnPortalDetectionCompleted( | |
| 30 const CaptivePortalDetector::Results& results) { | |
| 31 results_ = results; | |
| 32 ++num_results_received_; | |
| 33 } | |
| 34 | |
| 35 const CaptivePortalDetector::Results& captive_portal_results() const { | |
| 36 return results_; | |
| 37 } | |
| 38 | |
| 39 int num_results_received() const { return num_results_received_; } | |
| 40 | |
| 41 private: | |
| 42 CaptivePortalDetector::Results results_; | |
| 43 int num_results_received_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient); | |
| 46 }; | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 class CaptivePortalDetectorTest : public testing::Test, | |
| 51 public CaptivePortalDetectorTestBase { | |
| 52 public: | |
| 53 CaptivePortalDetectorTest() : detector_(profile_.GetRequestContext()) { | |
| 54 set_detector(&detector_); | |
| 55 } | |
| 56 | |
| 57 virtual ~CaptivePortalDetectorTest() {} | |
| 58 | |
| 59 void RunTest(const CaptivePortalDetector::Results& expected_results, | |
| 60 int net_error, | |
| 61 int status_code, | |
| 62 const char* response_headers) { | |
| 63 ASSERT_FALSE(FetchingURL()); | |
| 64 | |
| 65 GURL url(CaptivePortalDetector::kDefaultURL); | |
| 66 CaptivePortalClient client(detector()); | |
| 67 | |
| 68 detector()->DetectCaptivePortal(url, | |
| 69 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted, | |
| 70 base::Unretained(&client))); | |
| 71 | |
| 72 ASSERT_TRUE(FetchingURL()); | |
| 73 base::RunLoop().RunUntilIdle(); | |
| 74 | |
| 75 CompleteURLFetch(net_error, status_code, response_headers); | |
| 76 | |
| 77 EXPECT_FALSE(FetchingURL()); | |
| 78 EXPECT_EQ(1, client.num_results_received()); | |
| 79 EXPECT_EQ(expected_results.result, client.captive_portal_results().result); | |
| 80 EXPECT_EQ(expected_results.response_code, | |
| 81 client.captive_portal_results().response_code); | |
| 82 EXPECT_EQ(expected_results.retry_after_delta, | |
| 83 client.captive_portal_results().retry_after_delta); | |
| 84 } | |
| 85 | |
| 86 void RunCancelTest() { | |
| 87 ASSERT_FALSE(FetchingURL()); | |
| 88 | |
| 89 GURL url(CaptivePortalDetector::kDefaultURL); | |
| 90 CaptivePortalClient client(detector()); | |
| 91 | |
| 92 detector()->DetectCaptivePortal(url, | |
| 93 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted, | |
| 94 base::Unretained(&client))); | |
| 95 | |
| 96 ASSERT_TRUE(FetchingURL()); | |
| 97 base::RunLoop().RunUntilIdle(); | |
| 98 | |
| 99 detector()->Cancel(); | |
| 100 | |
| 101 ASSERT_FALSE(FetchingURL()); | |
| 102 EXPECT_EQ(0, client.num_results_received()); | |
| 103 } | |
| 104 | |
| 105 private: | |
| 106 content::TestBrowserThreadBundle thread_bundle_; | |
| 107 | |
| 108 // Definition order does matter. | |
| 109 TestingProfile profile_; | |
| 110 CaptivePortalDetector detector_; | |
| 111 }; | |
| 112 | |
| 113 // Test that the CaptivePortalDetector returns the expected result | |
| 114 // codes in response to a variety of probe results. | |
| 115 TEST_F(CaptivePortalDetectorTest, CaptivePortalResultCodes) { | |
| 116 CaptivePortalDetector::Results results; | |
| 117 results.result = RESULT_INTERNET_CONNECTED; | |
| 118 results.response_code = 204; | |
| 119 | |
| 120 RunTest(results, net::OK, 204, NULL); | |
| 121 | |
| 122 // The server may return an HTTP error when it's acting up. | |
| 123 results.result = RESULT_NO_RESPONSE; | |
| 124 results.response_code = 500; | |
| 125 RunTest(results, net::OK, 500, NULL); | |
| 126 | |
| 127 // Generic network error case. | |
| 128 results.result = RESULT_NO_RESPONSE; | |
| 129 results.response_code = net::URLFetcher::RESPONSE_CODE_INVALID; | |
| 130 RunTest(results, net::ERR_TIMED_OUT, net::URLFetcher::RESPONSE_CODE_INVALID, | |
| 131 NULL); | |
| 132 | |
| 133 // In the general captive portal case, the portal will return a page with a | |
| 134 // 200 status. | |
| 135 results.result = RESULT_BEHIND_CAPTIVE_PORTAL; | |
| 136 results.response_code = 200; | |
| 137 RunTest(results, net::OK, 200, NULL); | |
| 138 | |
| 139 // Some captive portals return 511 instead, to advertise their captive | |
| 140 // portal-ness. | |
| 141 results.result = RESULT_BEHIND_CAPTIVE_PORTAL; | |
| 142 results.response_code = 511; | |
| 143 RunTest(results, net::OK, 511, NULL); | |
| 144 } | |
| 145 | |
| 146 // Check a Retry-After header that contains a delay in seconds. | |
| 147 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterSeconds) { | |
| 148 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n"; | |
| 149 CaptivePortalDetector::Results results; | |
| 150 | |
| 151 // Check that Retry-After headers work both on the first request to return a | |
| 152 // result and on subsequent requests. | |
| 153 results.result = RESULT_NO_RESPONSE; | |
| 154 results.response_code = 503; | |
| 155 results.retry_after_delta = base::TimeDelta::FromSeconds(101); | |
| 156 RunTest(results, net::OK, 503, retry_after); | |
| 157 | |
| 158 results.result = RESULT_INTERNET_CONNECTED; | |
| 159 results.response_code = 204; | |
| 160 results.retry_after_delta = base::TimeDelta(); | |
| 161 RunTest(results, net::OK, 204, NULL); | |
| 162 } | |
| 163 | |
| 164 // Check a Retry-After header that contains a date. | |
| 165 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterDate) { | |
| 166 const char* retry_after = | |
| 167 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"; | |
| 168 CaptivePortalDetector::Results results; | |
| 169 | |
| 170 // base has a function to get a time in the right format from a string, but | |
| 171 // not the other way around. | |
| 172 base::Time start_time; | |
| 173 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", | |
| 174 &start_time)); | |
| 175 base::Time retry_after_time; | |
| 176 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT", | |
| 177 &retry_after_time)); | |
| 178 | |
| 179 SetTime(start_time); | |
| 180 | |
| 181 results.result = RESULT_NO_RESPONSE; | |
| 182 results.response_code = 503; | |
| 183 results.retry_after_delta = retry_after_time - start_time; | |
| 184 RunTest(results, net::OK, 503, retry_after); | |
| 185 } | |
| 186 | |
| 187 // Check invalid Retry-After headers are ignored. | |
| 188 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterInvalid) { | |
| 189 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"; | |
| 190 CaptivePortalDetector::Results results; | |
| 191 | |
| 192 results.result = RESULT_NO_RESPONSE; | |
| 193 results.response_code = 503; | |
| 194 RunTest(results, net::OK, 503, retry_after); | |
| 195 } | |
| 196 | |
| 197 TEST_F(CaptivePortalDetectorTest, Cancel) { | |
| 198 RunCancelTest(); | |
| 199 CaptivePortalDetector::Results results; | |
| 200 results.result = RESULT_INTERNET_CONNECTED; | |
| 201 results.response_code = 204; | |
| 202 RunTest(results, net::OK, 204, NULL); | |
| 203 } | |
| 204 | |
| 205 } // namespace captive_portal | |
| OLD | NEW |