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