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 <limits.h> | 5 #include <limits.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "net/base/sdch_manager.h" | 11 #include "net/base/sdch_manager.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 //------------------------------------------------------------------------------ | 16 //------------------------------------------------------------------------------ |
17 // Provide sample data and compression results with a sample VCDIFF dictionary. | 17 // Provide sample data and compression results with a sample VCDIFF dictionary. |
18 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. | 18 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. |
19 static const char kTestVcdiffDictionary[] = "DictionaryFor" | 19 static const char kTestVcdiffDictionary[] = |
| 20 "DictionaryFor" |
20 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; | 21 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; |
21 | 22 |
22 //------------------------------------------------------------------------------ | 23 //------------------------------------------------------------------------------ |
23 | 24 |
24 class SdchManagerTest : public testing::Test { | 25 class SdchManagerTest : public testing::Test { |
25 protected: | 26 protected: |
26 SdchManagerTest() | 27 SdchManagerTest() : sdch_manager_(new SdchManager) {} |
27 : sdch_manager_(new SdchManager) { | |
28 } | |
29 | 28 |
30 scoped_ptr<SdchManager> sdch_manager_; // A singleton database. | 29 scoped_ptr<SdchManager> sdch_manager_; // A singleton database. |
31 }; | 30 }; |
32 | 31 |
33 //------------------------------------------------------------------------------ | 32 //------------------------------------------------------------------------------ |
34 static std::string NewSdchDictionary(const std::string& domain) { | 33 static std::string NewSdchDictionary(const std::string& domain) { |
35 std::string dictionary; | 34 std::string dictionary; |
36 if (!domain.empty()) { | 35 if (!domain.empty()) { |
37 dictionary.append("Domain: "); | 36 dictionary.append("Domain: "); |
38 dictionary.append(domain); | 37 dictionary.append(domain); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 if (exponential < 0) | 123 if (exponential < 0) |
125 exponential = INT_MAX; // We don't wrap. | 124 exponential = INT_MAX; // We don't wrap. |
126 } | 125 } |
127 } | 126 } |
128 | 127 |
129 TEST_F(SdchManagerTest, CanSetExactMatchDictionary) { | 128 TEST_F(SdchManagerTest, CanSetExactMatchDictionary) { |
130 std::string dictionary_domain("x.y.z.google.com"); | 129 std::string dictionary_domain("x.y.z.google.com"); |
131 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 130 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
132 | 131 |
133 // Perfect match should work. | 132 // Perfect match should work. |
134 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, | 133 EXPECT_TRUE(sdch_manager_->AddSdchDictionary( |
135 GURL("http://" + dictionary_domain))); | 134 dictionary_text, GURL("http://" + dictionary_domain))); |
136 } | 135 } |
137 | 136 |
138 TEST_F(SdchManagerTest, CanAdvertiseDictionaryOverHTTP) { | 137 TEST_F(SdchManagerTest, CanAdvertiseDictionaryOverHTTP) { |
139 std::string dictionary_domain("x.y.z.google.com"); | 138 std::string dictionary_domain("x.y.z.google.com"); |
140 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 139 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
141 | 140 |
142 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, | 141 EXPECT_TRUE(sdch_manager_->AddSdchDictionary( |
143 GURL("http://" + dictionary_domain))); | 142 dictionary_text, GURL("http://" + dictionary_domain))); |
144 | 143 |
145 std::string dictionary_list; | 144 std::string dictionary_list; |
146 // HTTP target URL can advertise dictionary. | 145 // HTTP target URL can advertise dictionary. |
147 sdch_manager_->GetAvailDictionaryList( | 146 sdch_manager_->GetAvailDictionaryList( |
148 GURL("http://" + dictionary_domain + "/test"), | 147 GURL("http://" + dictionary_domain + "/test"), &dictionary_list); |
149 &dictionary_list); | |
150 EXPECT_FALSE(dictionary_list.empty()); | 148 EXPECT_FALSE(dictionary_list.empty()); |
151 } | 149 } |
152 | 150 |
153 TEST_F(SdchManagerTest, CanNotAdvertiseDictionaryOverHTTPS) { | 151 TEST_F(SdchManagerTest, CanNotAdvertiseDictionaryOverHTTPS) { |
154 std::string dictionary_domain("x.y.z.google.com"); | 152 std::string dictionary_domain("x.y.z.google.com"); |
155 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 153 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
156 | 154 |
157 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, | 155 EXPECT_TRUE(sdch_manager_->AddSdchDictionary( |
158 GURL("http://" + dictionary_domain))); | 156 dictionary_text, GURL("http://" + dictionary_domain))); |
159 | 157 |
160 std::string dictionary_list; | 158 std::string dictionary_list; |
161 // HTTPS target URL should NOT advertise dictionary. | 159 // HTTPS target URL should NOT advertise dictionary. |
162 sdch_manager_->GetAvailDictionaryList( | 160 sdch_manager_->GetAvailDictionaryList( |
163 GURL("https://" + dictionary_domain + "/test"), | 161 GURL("https://" + dictionary_domain + "/test"), &dictionary_list); |
164 &dictionary_list); | |
165 EXPECT_TRUE(dictionary_list.empty()); | 162 EXPECT_TRUE(dictionary_list.empty()); |
166 } | 163 } |
167 | 164 |
168 TEST_F(SdchManagerTest, CanUseHTTPSDictionaryOverHTTPSIfEnabled) { | 165 TEST_F(SdchManagerTest, CanUseHTTPSDictionaryOverHTTPSIfEnabled) { |
169 std::string dictionary_domain("x.y.z.google.com"); | 166 std::string dictionary_domain("x.y.z.google.com"); |
170 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 167 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
171 | 168 |
172 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, | 169 EXPECT_TRUE(sdch_manager_->AddSdchDictionary( |
173 GURL("https://" + dictionary_domain))); | 170 dictionary_text, GURL("https://" + dictionary_domain))); |
174 | 171 |
175 GURL target_url("https://" + dictionary_domain + "/test"); | 172 GURL target_url("https://" + dictionary_domain + "/test"); |
176 std::string dictionary_list; | 173 std::string dictionary_list; |
177 // HTTPS target URL should advertise dictionary if secure scheme support is | 174 // HTTPS target URL should advertise dictionary if secure scheme support is |
178 // enabled. | 175 // enabled. |
179 sdch_manager_->EnableSecureSchemeSupport(true); | 176 sdch_manager_->EnableSecureSchemeSupport(true); |
180 sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list); | 177 sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list); |
181 EXPECT_FALSE(dictionary_list.empty()); | 178 EXPECT_FALSE(dictionary_list.empty()); |
182 | 179 |
183 // Dictionary should be available. | 180 // Dictionary should be available. |
184 SdchManager::Dictionary* dictionary = NULL; | 181 SdchManager::Dictionary* dictionary = NULL; |
185 std::string client_hash; | 182 std::string client_hash; |
186 std::string server_hash; | 183 std::string server_hash; |
187 sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash); | 184 sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash); |
188 sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary); | 185 sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary); |
189 EXPECT_TRUE(dictionary != NULL); | 186 EXPECT_TRUE(dictionary != NULL); |
190 } | 187 } |
191 | 188 |
192 TEST_F(SdchManagerTest, CanNotUseHTTPDictionaryOverHTTPS) { | 189 TEST_F(SdchManagerTest, CanNotUseHTTPDictionaryOverHTTPS) { |
193 std::string dictionary_domain("x.y.z.google.com"); | 190 std::string dictionary_domain("x.y.z.google.com"); |
194 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 191 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
195 | 192 |
196 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, | 193 EXPECT_TRUE(sdch_manager_->AddSdchDictionary( |
197 GURL("http://" + dictionary_domain))); | 194 dictionary_text, GURL("http://" + dictionary_domain))); |
198 | 195 |
199 GURL target_url("https://" + dictionary_domain + "/test"); | 196 GURL target_url("https://" + dictionary_domain + "/test"); |
200 std::string dictionary_list; | 197 std::string dictionary_list; |
201 // HTTPS target URL should not advertise dictionary acquired over HTTP even if | 198 // HTTPS target URL should not advertise dictionary acquired over HTTP even if |
202 // secure scheme support is enabled. | 199 // secure scheme support is enabled. |
203 sdch_manager_->EnableSecureSchemeSupport(true); | 200 sdch_manager_->EnableSecureSchemeSupport(true); |
204 sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list); | 201 sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list); |
205 EXPECT_TRUE(dictionary_list.empty()); | 202 EXPECT_TRUE(dictionary_list.empty()); |
206 | 203 |
207 SdchManager::Dictionary* dictionary = NULL; | 204 SdchManager::Dictionary* dictionary = NULL; |
208 std::string client_hash; | 205 std::string client_hash; |
209 std::string server_hash; | 206 std::string server_hash; |
210 sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash); | 207 sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash); |
211 sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary); | 208 sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary); |
212 EXPECT_TRUE(dictionary == NULL); | 209 EXPECT_TRUE(dictionary == NULL); |
213 } | 210 } |
214 | 211 |
215 TEST_F(SdchManagerTest, FailToSetDomainMismatchDictionary) { | 212 TEST_F(SdchManagerTest, FailToSetDomainMismatchDictionary) { |
216 std::string dictionary_domain("x.y.z.google.com"); | 213 std::string dictionary_domain("x.y.z.google.com"); |
217 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 214 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
218 | 215 |
219 // Fail the "domain match" requirement. | 216 // Fail the "domain match" requirement. |
220 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, | 217 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, |
221 GURL("http://y.z.google.com"))); | 218 GURL("http://y.z.google.com"))); |
222 } | 219 } |
223 | 220 |
224 TEST_F(SdchManagerTest, FailToSetDotHostPrefixDomainDictionary) { | 221 TEST_F(SdchManagerTest, FailToSetDotHostPrefixDomainDictionary) { |
225 std::string dictionary_domain("x.y.z.google.com"); | 222 std::string dictionary_domain("x.y.z.google.com"); |
226 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 223 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
227 | 224 |
228 // Fail the HD with D being the domain and H having a dot requirement. | 225 // Fail the HD with D being the domain and H having a dot requirement. |
229 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, | 226 EXPECT_FALSE(sdch_manager_->AddSdchDictionary( |
230 GURL("http://w.x.y.z.google.com"))); | 227 dictionary_text, GURL("http://w.x.y.z.google.com"))); |
231 } | 228 } |
232 | 229 |
233 TEST_F(SdchManagerTest, FailToSetRepeatPrefixWithDotDictionary) { | 230 TEST_F(SdchManagerTest, FailToSetRepeatPrefixWithDotDictionary) { |
234 // Make sure that a prefix that matches the domain postfix won't confuse | 231 // Make sure that a prefix that matches the domain postfix won't confuse |
235 // the validation checks. | 232 // the validation checks. |
236 std::string dictionary_domain("www.google.com"); | 233 std::string dictionary_domain("www.google.com"); |
237 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 234 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
238 | 235 |
239 // Fail the HD with D being the domain and H having a dot requirement. | 236 // Fail the HD with D being the domain and H having a dot requirement. |
240 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, | 237 EXPECT_FALSE(sdch_manager_->AddSdchDictionary( |
241 GURL("http://www.google.com.www.google.com"))); | 238 dictionary_text, GURL("http://www.google.com.www.google.com"))); |
242 } | 239 } |
243 | 240 |
244 TEST_F(SdchManagerTest, CanSetLeadingDotDomainDictionary) { | 241 TEST_F(SdchManagerTest, CanSetLeadingDotDomainDictionary) { |
245 // Make sure that a prefix that matches the domain postfix won't confuse | 242 // Make sure that a prefix that matches the domain postfix won't confuse |
246 // the validation checks. | 243 // the validation checks. |
247 std::string dictionary_domain(".google.com"); | 244 std::string dictionary_domain(".google.com"); |
248 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 245 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
249 | 246 |
250 // Verify that a leading dot in the domain is acceptable, as long as the host | 247 // Verify that a leading dot in the domain is acceptable, as long as the host |
251 // name does not contain any dots preceding the matched domain name. | 248 // name does not contain any dots preceding the matched domain name. |
252 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, | 249 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
253 GURL("http://www.google.com"))); | 250 GURL("http://www.google.com"))); |
254 } | 251 } |
255 | 252 |
256 // Make sure the order of the tests is not helping us or confusing things. | 253 // Make sure the order of the tests is not helping us or confusing things. |
257 // See test CanSetExactMatchDictionary above for first try. | 254 // See test CanSetExactMatchDictionary above for first try. |
258 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) { | 255 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) { |
259 std::string dictionary_domain("x.y.z.google.com"); | 256 std::string dictionary_domain("x.y.z.google.com"); |
260 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 257 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
261 | 258 |
262 // Perfect match should *STILL* work. | 259 // Perfect match should *STILL* work. |
263 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, | 260 EXPECT_TRUE(sdch_manager_->AddSdchDictionary( |
264 GURL("http://" + dictionary_domain))); | 261 dictionary_text, GURL("http://" + dictionary_domain))); |
265 } | 262 } |
266 | 263 |
267 // Make sure the DOS protection precludes the addition of too many dictionaries. | 264 // Make sure the DOS protection precludes the addition of too many dictionaries. |
268 TEST_F(SdchManagerTest, TooManyDictionaries) { | 265 TEST_F(SdchManagerTest, TooManyDictionaries) { |
269 std::string dictionary_domain(".google.com"); | 266 std::string dictionary_domain(".google.com"); |
270 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 267 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
271 | 268 |
272 size_t count = 0; | 269 size_t count = 0; |
273 while (count <= SdchManager::kMaxDictionaryCount + 1) { | 270 while (count <= SdchManager::kMaxDictionaryCount + 1) { |
274 if (!sdch_manager_->AddSdchDictionary(dictionary_text, | 271 if (!sdch_manager_->AddSdchDictionary(dictionary_text, |
275 GURL("http://www.google.com"))) | 272 GURL("http://www.google.com"))) |
276 break; | 273 break; |
277 | 274 |
278 dictionary_text += " "; // Create dictionary with different SHA signature. | 275 dictionary_text += " "; // Create dictionary with different SHA signature. |
279 ++count; | 276 ++count; |
280 } | 277 } |
281 EXPECT_EQ(SdchManager::kMaxDictionaryCount, count); | 278 EXPECT_EQ(SdchManager::kMaxDictionaryCount, count); |
282 } | 279 } |
283 | 280 |
284 TEST_F(SdchManagerTest, DictionaryNotTooLarge) { | 281 TEST_F(SdchManagerTest, DictionaryNotTooLarge) { |
285 std::string dictionary_domain(".google.com"); | 282 std::string dictionary_domain(".google.com"); |
286 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 283 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
287 | 284 |
288 dictionary_text.append( | 285 dictionary_text.append( |
289 SdchManager::kMaxDictionarySize - dictionary_text.size(), ' '); | 286 SdchManager::kMaxDictionarySize - dictionary_text.size(), ' '); |
290 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, | 287 EXPECT_TRUE(sdch_manager_->AddSdchDictionary( |
291 GURL("http://" + dictionary_domain))); | 288 dictionary_text, GURL("http://" + dictionary_domain))); |
292 } | 289 } |
293 | 290 |
294 TEST_F(SdchManagerTest, DictionaryTooLarge) { | 291 TEST_F(SdchManagerTest, DictionaryTooLarge) { |
295 std::string dictionary_domain(".google.com"); | 292 std::string dictionary_domain(".google.com"); |
296 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 293 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
297 | 294 |
298 dictionary_text.append( | 295 dictionary_text.append( |
299 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' '); | 296 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' '); |
300 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, | 297 EXPECT_FALSE(sdch_manager_->AddSdchDictionary( |
301 GURL("http://" + dictionary_domain))); | 298 dictionary_text, GURL("http://" + dictionary_domain))); |
302 } | 299 } |
303 | 300 |
304 TEST_F(SdchManagerTest, PathMatch) { | 301 TEST_F(SdchManagerTest, PathMatch) { |
305 bool (*PathMatch)(const std::string& path, const std::string& restriction) = | 302 bool (*PathMatch)(const std::string& path, const std::string& restriction) = |
306 SdchManager::Dictionary::PathMatch; | 303 SdchManager::Dictionary::PathMatch; |
307 // Perfect match is supported. | 304 // Perfect match is supported. |
308 EXPECT_TRUE(PathMatch("/search", "/search")); | 305 EXPECT_TRUE(PathMatch("/search", "/search")); |
309 EXPECT_TRUE(PathMatch("/search/", "/search/")); | 306 EXPECT_TRUE(PathMatch("/search/", "/search/")); |
310 | 307 |
311 // Prefix only works if last character of restriction is a slash, or first | 308 // Prefix only works if last character of restriction is a slash, or first |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 sdch_manager_->SetAllowLatencyExperiment(url, false); | 361 sdch_manager_->SetAllowLatencyExperiment(url, false); |
365 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url)); | 362 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url)); |
366 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url2)); | 363 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url2)); |
367 | 364 |
368 sdch_manager_->SetAllowLatencyExperiment(url2, false); | 365 sdch_manager_->SetAllowLatencyExperiment(url2, false); |
369 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url)); | 366 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url)); |
370 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2)); | 367 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2)); |
371 } | 368 } |
372 | 369 |
373 } // namespace net | 370 } // namespace net |
374 | |
OLD | NEW |