OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/sdch_dictionary_fetcher.h" | 5 #include "net/url_request/sdch_dictionary_fetcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/callback.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
14 #include "net/base/sdch_manager.h" | 14 #include "net/base/sdch_manager.h" |
15 #include "net/url_request/url_request_data_job.h" | 15 #include "net/url_request/url_request_data_job.h" |
16 #include "net/url_request/url_request_filter.h" | 16 #include "net/url_request/url_request_filter.h" |
17 #include "net/url_request/url_request_interceptor.h" | 17 #include "net/url_request/url_request_interceptor.h" |
18 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 static const char kSampleBufferContext[] = "This is a sample buffer."; | 24 namespace { |
25 static const char kTestDomain[] = "top.domain.test"; | 25 |
| 26 const char kSampleBufferContext[] = "This is a sample buffer."; |
| 27 const char kTestDomain[] = "top.domain.test"; |
26 | 28 |
27 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { | 29 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { |
28 public: | 30 public: |
29 URLRequestSpecifiedResponseJob(URLRequest* request, | 31 URLRequestSpecifiedResponseJob(URLRequest* request, |
30 NetworkDelegate* network_delegate) | 32 NetworkDelegate* network_delegate, |
31 : URLRequestSimpleJob(request, network_delegate) {} | 33 const base::Closure& destruction_callback) |
32 | 34 : URLRequestSimpleJob(request, network_delegate), |
33 static void AddUrlHandler() { | 35 destruction_callback_(destruction_callback) { |
34 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 36 DCHECK(!destruction_callback.is_null()); |
35 jobs_requested_ = 0; | |
36 filter->AddHostnameHandler( | |
37 "http", kTestDomain, &URLRequestSpecifiedResponseJob::Factory); | |
38 } | |
39 | |
40 static void RemoveUrlHandler() { | |
41 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | |
42 filter->RemoveHostnameHandler("http", kTestDomain); | |
43 jobs_requested_ = 0; | |
44 } | |
45 | |
46 static URLRequestJob* Factory(URLRequest* request, | |
47 net::NetworkDelegate* network_delegate, | |
48 const std::string& scheme) { | |
49 ++jobs_requested_; | |
50 return new URLRequestSpecifiedResponseJob(request, network_delegate); | |
51 } | 37 } |
52 | 38 |
53 static std::string ExpectedResponseForURL(const GURL& url) { | 39 static std::string ExpectedResponseForURL(const GURL& url) { |
54 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", | 40 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", |
55 url.spec().c_str(), | 41 url.spec().c_str(), |
56 kSampleBufferContext, | 42 kSampleBufferContext, |
57 url.spec().c_str()); | 43 url.spec().c_str()); |
58 } | 44 } |
59 | 45 |
60 static int jobs_requested() { return jobs_requested_; } | |
61 | |
62 private: | 46 private: |
63 ~URLRequestSpecifiedResponseJob() override{}; | 47 ~URLRequestSpecifiedResponseJob() override { destruction_callback_.Run(); } |
64 | 48 |
65 // URLRequestSimpleJob implementation: | 49 // URLRequestSimpleJob implementation: |
66 | |
67 int GetData(std::string* mime_type, | 50 int GetData(std::string* mime_type, |
68 std::string* charset, | 51 std::string* charset, |
69 std::string* data, | 52 std::string* data, |
70 const CompletionCallback& callback) const override { | 53 const CompletionCallback& callback) const override { |
71 GURL url(request_->url()); | 54 GURL url(request_->url()); |
72 *data = ExpectedResponseForURL(url); | 55 *data = ExpectedResponseForURL(url); |
73 return OK; | 56 return OK; |
74 } | 57 } |
75 | 58 |
76 base::TaskRunner* GetTaskRunner() const override { | 59 const base::Closure destruction_callback_; |
77 return base::MessageLoopProxy::current().get(); | 60 }; |
| 61 |
| 62 class SpecifiedResponseJobInterceptor : public URLRequestInterceptor { |
| 63 public: |
| 64 // A callback to be called whenever a URLRequestSpecifiedResponseJob is |
| 65 // created or destroyed. The argument will be the change in number of |
| 66 // jobs (i.e. +1 for created, -1 for destroyed). |
| 67 typedef base::Callback<void(int outstanding_job_delta)> LifecycleCallback; |
| 68 |
| 69 explicit SpecifiedResponseJobInterceptor( |
| 70 const LifecycleCallback& lifecycle_callback) |
| 71 : lifecycle_callback_(lifecycle_callback), factory_(this) { |
| 72 DCHECK(!lifecycle_callback_.is_null()); |
| 73 } |
| 74 ~SpecifiedResponseJobInterceptor() override {} |
| 75 |
| 76 URLRequestJob* MaybeInterceptRequest( |
| 77 URLRequest* request, |
| 78 NetworkDelegate* network_delegate) const override { |
| 79 if (!lifecycle_callback_.is_null()) |
| 80 lifecycle_callback_.Run(1); |
| 81 |
| 82 return new URLRequestSpecifiedResponseJob( |
| 83 request, network_delegate, base::Bind(lifecycle_callback_, -1)); |
78 } | 84 } |
79 | 85 |
80 static int jobs_requested_; | 86 // The caller must ensure that the callback is valid to call for the |
| 87 // lifetime of the SpecifiedResponseJobInterceptor (i.e. until |
| 88 // Unregister() is called). |
| 89 static void RegisterWithFilter(const LifecycleCallback& lifecycle_callback) { |
| 90 scoped_ptr<SpecifiedResponseJobInterceptor> interceptor( |
| 91 new SpecifiedResponseJobInterceptor(lifecycle_callback)); |
| 92 |
| 93 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
| 94 "http", kTestDomain, interceptor.Pass()); |
| 95 } |
| 96 |
| 97 static void Unregister() { |
| 98 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
| 99 kTestDomain); |
| 100 } |
| 101 |
| 102 private: |
| 103 void OnSpecfiedResponseJobDestruction() const { |
| 104 if (!lifecycle_callback_.is_null()) |
| 105 lifecycle_callback_.Run(-1); |
| 106 } |
| 107 |
| 108 LifecycleCallback lifecycle_callback_; |
| 109 mutable base::WeakPtrFactory<SpecifiedResponseJobInterceptor> factory_; |
81 }; | 110 }; |
82 | 111 |
83 int URLRequestSpecifiedResponseJob::jobs_requested_(0); | 112 // Local test infrastructure |
| 113 // * URLRequestSpecifiedResponseJob: A URLRequestJob that returns |
| 114 // a different but derivable response for each URL (used for all |
| 115 // url requests in this file). The class provides interfaces to |
| 116 // signal whenever the total number of jobs transitions to zero. |
| 117 // * SdchDictionaryFetcherTest: Registers a callback with the above |
| 118 // class, and provides blocking interfaces for a transition to zero jobs. |
| 119 // Contains an SdchDictionaryFetcher, and tracks fetcher dictionary |
| 120 // addition callbacks. |
| 121 // Most tests schedule a dictionary fetch, wait for no jobs outstanding, |
| 122 // then test that the fetch results are as expected. |
84 | 123 |
85 class SdchDictionaryFetcherTest : public ::testing::Test { | 124 class SdchDictionaryFetcherTest : public ::testing::Test { |
86 public: | 125 public: |
87 struct DictionaryAdditions { | 126 struct DictionaryAdditions { |
88 DictionaryAdditions(const std::string& dictionary_text, | 127 DictionaryAdditions(const std::string& dictionary_text, |
89 const GURL& dictionary_url) | 128 const GURL& dictionary_url) |
90 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} | 129 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} |
91 | 130 |
92 std::string dictionary_text; | 131 std::string dictionary_text; |
93 GURL dictionary_url; | 132 GURL dictionary_url; |
94 }; | 133 }; |
95 | 134 |
96 SdchDictionaryFetcherTest() { | 135 SdchDictionaryFetcherTest() |
97 URLRequestSpecifiedResponseJob::AddUrlHandler(); | 136 : jobs_requested_(0), |
98 context_.reset(new TestURLRequestContext); | 137 jobs_outstanding_(0), |
99 fetcher_.reset(new SdchDictionaryFetcher( | 138 context_(new TestURLRequestContext), |
100 context_.get(), | 139 fetcher_(new SdchDictionaryFetcher( |
101 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, | 140 context_.get(), |
102 base::Unretained(this)))); | 141 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, |
| 142 base::Unretained(this)))), |
| 143 factory_(this) { |
| 144 SpecifiedResponseJobInterceptor::RegisterWithFilter( |
| 145 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged, |
| 146 factory_.GetWeakPtr())); |
103 } | 147 } |
104 | 148 |
105 ~SdchDictionaryFetcherTest() override { | 149 ~SdchDictionaryFetcherTest() override { |
106 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); | 150 SpecifiedResponseJobInterceptor::Unregister(); |
107 } | 151 } |
108 | 152 |
109 void OnDictionaryFetched(const std::string& dictionary_text, | 153 void OnDictionaryFetched(const std::string& dictionary_text, |
110 const GURL& dictionary_url, | 154 const GURL& dictionary_url, |
111 const BoundNetLog& net_log) { | 155 const BoundNetLog& net_log) { |
112 dictionary_additions.push_back( | 156 dictionary_additions.push_back( |
113 DictionaryAdditions(dictionary_text, dictionary_url)); | 157 DictionaryAdditions(dictionary_text, dictionary_url)); |
114 } | 158 } |
115 | 159 |
| 160 // Return (in |*out|) all dictionary additions since the last time |
| 161 // this function was called. |
116 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { | 162 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { |
117 out->swap(dictionary_additions); | 163 out->swap(dictionary_additions); |
118 dictionary_additions.clear(); | 164 dictionary_additions.clear(); |
119 } | 165 } |
120 | 166 |
121 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } | 167 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } |
122 | 168 |
123 // May not be called outside the SetUp()/TearDown() interval. | 169 // May not be called outside the SetUp()/TearDown() interval. |
124 int JobsRequested() { | 170 int jobs_requested() const { return jobs_requested_; } |
125 return URLRequestSpecifiedResponseJob::jobs_requested(); | |
126 } | |
127 | 171 |
128 GURL PathToGurl(const char* path) { | 172 GURL PathToGurl(const char* path) { |
129 std::string gurl_string("http://"); | 173 std::string gurl_string("http://"); |
130 gurl_string += kTestDomain; | 174 gurl_string += kTestDomain; |
131 gurl_string += "/"; | 175 gurl_string += "/"; |
132 gurl_string += path; | 176 gurl_string += path; |
133 return GURL(gurl_string); | 177 return GURL(gurl_string); |
134 } | 178 } |
135 | 179 |
| 180 // Block until there are no outstanding URLRequestSpecifiedResponseJobs. |
| 181 void WaitForNoJobs() { |
| 182 if (jobs_outstanding_ == 0) |
| 183 return; |
| 184 |
| 185 run_loop_.reset(new base::RunLoop); |
| 186 run_loop_->Run(); |
| 187 run_loop_.reset(); |
| 188 } |
| 189 |
136 private: | 190 private: |
| 191 void OnNumberJobsChanged(int outstanding_jobs_delta) { |
| 192 if (outstanding_jobs_delta > 0) |
| 193 jobs_requested_ += outstanding_jobs_delta; |
| 194 jobs_outstanding_ += outstanding_jobs_delta; |
| 195 if (jobs_outstanding_ == 0 && run_loop_) |
| 196 run_loop_->Quit(); |
| 197 } |
| 198 |
| 199 int jobs_requested_; |
| 200 int jobs_outstanding_; |
| 201 scoped_ptr<base::RunLoop> run_loop_; |
137 scoped_ptr<TestURLRequestContext> context_; | 202 scoped_ptr<TestURLRequestContext> context_; |
138 scoped_ptr<SdchDictionaryFetcher> fetcher_; | 203 scoped_ptr<SdchDictionaryFetcher> fetcher_; |
139 std::vector<DictionaryAdditions> dictionary_additions; | 204 std::vector<DictionaryAdditions> dictionary_additions; |
| 205 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_; |
140 }; | 206 }; |
141 | 207 |
142 // Schedule a fetch and make sure it happens. | 208 // Schedule a fetch and make sure it happens. |
143 TEST_F(SdchDictionaryFetcherTest, Basic) { | 209 TEST_F(SdchDictionaryFetcherTest, Basic) { |
144 GURL dictionary_url(PathToGurl("dictionary")); | 210 GURL dictionary_url(PathToGurl("dictionary")); |
145 fetcher()->Schedule(dictionary_url); | 211 fetcher()->Schedule(dictionary_url); |
| 212 WaitForNoJobs(); |
146 | 213 |
147 base::RunLoop().RunUntilIdle(); | 214 EXPECT_EQ(1, jobs_requested()); |
148 EXPECT_EQ(1, JobsRequested()); | |
149 std::vector<DictionaryAdditions> additions; | 215 std::vector<DictionaryAdditions> additions; |
150 GetDictionaryAdditions(&additions); | 216 GetDictionaryAdditions(&additions); |
151 ASSERT_EQ(1u, additions.size()); | 217 ASSERT_EQ(1u, additions.size()); |
152 EXPECT_EQ( | 218 EXPECT_EQ( |
153 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | 219 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
154 additions[0].dictionary_text); | 220 additions[0].dictionary_text); |
155 } | 221 } |
156 | 222 |
157 // Multiple fetches of the same URL should result in only one request. | 223 // Multiple fetches of the same URL should result in only one request. |
158 TEST_F(SdchDictionaryFetcherTest, Multiple) { | 224 TEST_F(SdchDictionaryFetcherTest, Multiple) { |
159 GURL dictionary_url(PathToGurl("dictionary")); | 225 GURL dictionary_url(PathToGurl("dictionary")); |
160 fetcher()->Schedule(dictionary_url); | 226 fetcher()->Schedule(dictionary_url); |
161 fetcher()->Schedule(dictionary_url); | 227 fetcher()->Schedule(dictionary_url); |
162 fetcher()->Schedule(dictionary_url); | 228 fetcher()->Schedule(dictionary_url); |
163 base::RunLoop().RunUntilIdle(); | 229 WaitForNoJobs(); |
164 | 230 |
165 EXPECT_EQ(1, JobsRequested()); | 231 EXPECT_EQ(1, jobs_requested()); |
166 std::vector<DictionaryAdditions> additions; | 232 std::vector<DictionaryAdditions> additions; |
167 GetDictionaryAdditions(&additions); | 233 GetDictionaryAdditions(&additions); |
168 ASSERT_EQ(1u, additions.size()); | 234 ASSERT_EQ(1u, additions.size()); |
169 EXPECT_EQ( | 235 EXPECT_EQ( |
170 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | 236 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
171 additions[0].dictionary_text); | 237 additions[0].dictionary_text); |
172 } | 238 } |
173 | 239 |
174 // A cancel should result in no actual requests being generated. | 240 // A cancel should result in no actual requests being generated. |
175 TEST_F(SdchDictionaryFetcherTest, Cancel) { | 241 TEST_F(SdchDictionaryFetcherTest, Cancel) { |
176 GURL dictionary_url_1(PathToGurl("dictionary_1")); | 242 GURL dictionary_url_1(PathToGurl("dictionary_1")); |
177 GURL dictionary_url_2(PathToGurl("dictionary_2")); | 243 GURL dictionary_url_2(PathToGurl("dictionary_2")); |
178 GURL dictionary_url_3(PathToGurl("dictionary_3")); | 244 GURL dictionary_url_3(PathToGurl("dictionary_3")); |
179 | 245 |
180 fetcher()->Schedule(dictionary_url_1); | 246 fetcher()->Schedule(dictionary_url_1); |
181 fetcher()->Schedule(dictionary_url_2); | 247 fetcher()->Schedule(dictionary_url_2); |
182 fetcher()->Schedule(dictionary_url_3); | 248 fetcher()->Schedule(dictionary_url_3); |
183 fetcher()->Cancel(); | 249 fetcher()->Cancel(); |
184 base::RunLoop().RunUntilIdle(); | 250 WaitForNoJobs(); |
185 | 251 |
186 // Synchronous execution may have resulted in a single job being scheduled. | 252 // Synchronous execution may have resulted in a single job being scheduled. |
187 EXPECT_GE(1, JobsRequested()); | 253 EXPECT_GE(1, jobs_requested()); |
188 } | 254 } |
| 255 |
| 256 // Attempt to confuse the fetcher loop processing by scheduling a |
| 257 // dictionary addition while another fetch is in process. |
| 258 TEST_F(SdchDictionaryFetcherTest, LoopRace) { |
| 259 GURL dictionary0_url(PathToGurl("dictionary0")); |
| 260 GURL dictionary1_url(PathToGurl("dictionary1")); |
| 261 fetcher()->Schedule(dictionary0_url); |
| 262 fetcher()->Schedule(dictionary1_url); |
| 263 WaitForNoJobs(); |
| 264 |
| 265 ASSERT_EQ(2, jobs_requested()); |
| 266 std::vector<DictionaryAdditions> additions; |
| 267 GetDictionaryAdditions(&additions); |
| 268 ASSERT_EQ(2u, additions.size()); |
| 269 EXPECT_EQ( |
| 270 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary0_url), |
| 271 additions[0].dictionary_text); |
| 272 EXPECT_EQ( |
| 273 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url), |
| 274 additions[1].dictionary_text); |
189 } | 275 } |
| 276 |
| 277 } // namespace |
| 278 |
| 279 } // namespace net |
OLD | NEW |