| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 dictionary.append(domain); | 140 dictionary.append(domain); |
| 141 dictionary.append("\n"); | 141 dictionary.append("\n"); |
| 142 } | 142 } |
| 143 dictionary.append("\n"); | 143 dictionary.append("\n"); |
| 144 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); | 144 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); |
| 145 return dictionary; | 145 return dictionary; |
| 146 } | 146 } |
| 147 | 147 |
| 148 //------------------------------------------------------------------------------ | 148 //------------------------------------------------------------------------------ |
| 149 | 149 |
| 150 TEST_F(SdchFilterTest, BasicBadDictionary) { | 150 TEST_F(SdchFilterTest, EmptyInputOk) { |
| 151 std::vector<Filter::FilterType> filter_types; | 151 std::vector<Filter::FilterType> filter_types; |
| 152 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 152 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 153 const int kInputBufferSize(30); | 153 const int kInputBufferSize(30); |
| 154 char output_buffer[20]; | 154 char output_buffer[20]; |
| 155 MockFilterContext filter_context(kInputBufferSize); | 155 MockFilterContext filter_context(kInputBufferSize); |
| 156 std::string url_string("http://ignore.com"); | 156 std::string url_string("http://ignore.com"); |
| 157 filter_context.SetURL(GURL(url_string)); | 157 filter_context.SetURL(GURL(url_string)); |
| 158 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); | 158 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 159 | 159 |
| 160 | 160 |
| 161 // With no input data, try to read output. | 161 // With no input data, try to read output. |
| 162 int output_bytes_or_buffer_size = sizeof(output_buffer); | 162 int output_bytes_or_buffer_size = sizeof(output_buffer); |
| 163 Filter::FilterStatus status = filter->ReadData(output_buffer, | 163 Filter::FilterStatus status = filter->ReadData(output_buffer, |
| 164 &output_bytes_or_buffer_size); | 164 &output_bytes_or_buffer_size); |
| 165 | 165 |
| 166 EXPECT_EQ(0, output_bytes_or_buffer_size); | 166 EXPECT_EQ(0, output_bytes_or_buffer_size); |
| 167 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); | 167 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); |
| 168 } |
| 168 | 169 |
| 169 // Supply bogus data (which doesnt't yet specify a full dictionary hash). | 170 TEST_F(SdchFilterTest, PassThroughWhenTentative) { |
| 171 std::vector<Filter::FilterType> filter_types; |
| 172 // Selective a tentative filter (which can fall back to pass through). |
| 173 filter_types.push_back(Filter::FILTER_TYPE_SDCH_POSSIBLE); |
| 174 const int kInputBufferSize(30); |
| 175 char output_buffer[20]; |
| 176 MockFilterContext filter_context(kInputBufferSize); |
| 177 // Response code needs to be 200 to allow a pass through. |
| 178 filter_context.SetResponseCode(200); |
| 179 std::string url_string("http://ignore.com"); |
| 180 filter_context.SetURL(GURL(url_string)); |
| 181 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 182 |
| 183 // Supply enough data to force a pass-through mode, which means we have |
| 184 // provided more than 9 characters that can't be a dictionary hash. |
| 185 std::string non_sdch_content("This is not SDCH"); |
| 186 |
| 187 char* input_buffer = filter->stream_buffer()->data(); |
| 188 int input_buffer_size = filter->stream_buffer_size(); |
| 189 EXPECT_EQ(kInputBufferSize, input_buffer_size); |
| 190 |
| 191 EXPECT_LT(static_cast<int>(non_sdch_content.size()), |
| 192 input_buffer_size); |
| 193 memcpy(input_buffer, non_sdch_content.data(), |
| 194 non_sdch_content.size()); |
| 195 filter->FlushStreamBuffer(non_sdch_content.size()); |
| 196 |
| 197 // Try to read output. |
| 198 int output_bytes_or_buffer_size = sizeof(output_buffer); |
| 199 Filter::FilterStatus status = filter->ReadData(output_buffer, |
| 200 &output_bytes_or_buffer_size); |
| 201 |
| 202 EXPECT_TRUE(non_sdch_content.size() == output_bytes_or_buffer_size); |
| 203 ASSERT_TRUE(sizeof(output_buffer) > output_bytes_or_buffer_size); |
| 204 output_buffer[output_bytes_or_buffer_size] = '\0'; |
| 205 EXPECT_TRUE(non_sdch_content == output_buffer); |
| 206 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); |
| 207 } |
| 208 |
| 209 TEST_F(SdchFilterTest, RefreshBadReturnCode) { |
| 210 std::vector<Filter::FilterType> filter_types; |
| 211 // Selective a tentative filter (which can fall back to pass through). |
| 212 filter_types.push_back(Filter::FILTER_TYPE_SDCH_POSSIBLE); |
| 213 const int kInputBufferSize(30); |
| 214 char output_buffer[20]; |
| 215 MockFilterContext filter_context(kInputBufferSize); |
| 216 // Response code needs to be 200 to allow a pass through. |
| 217 filter_context.SetResponseCode(403); |
| 218 // Meta refresh will only appear for html content |
| 219 filter_context.SetMimeType("text/html"); |
| 220 std::string url_string("http://ignore.com"); |
| 221 filter_context.SetURL(GURL(url_string)); |
| 222 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 223 |
| 224 // Supply enough data to force a pass-through mode, which means we have |
| 225 // provided more than 9 characters that can't be a dictionary hash. |
| 226 std::string non_sdch_content("This is not SDCH"); |
| 227 |
| 228 char* input_buffer = filter->stream_buffer()->data(); |
| 229 int input_buffer_size = filter->stream_buffer_size(); |
| 230 EXPECT_EQ(kInputBufferSize, input_buffer_size); |
| 231 |
| 232 EXPECT_LT(static_cast<int>(non_sdch_content.size()), |
| 233 input_buffer_size); |
| 234 memcpy(input_buffer, non_sdch_content.data(), |
| 235 non_sdch_content.size()); |
| 236 filter->FlushStreamBuffer(non_sdch_content.size()); |
| 237 |
| 238 // Try to read output. |
| 239 int output_bytes_or_buffer_size = sizeof(output_buffer); |
| 240 Filter::FilterStatus status = filter->ReadData(output_buffer, |
| 241 &output_bytes_or_buffer_size); |
| 242 |
| 243 // We should have read a long and complicated meta-refresh request. |
| 244 EXPECT_TRUE(sizeof(output_buffer) == output_bytes_or_buffer_size); |
| 245 // Check at least the prefix of the return. |
| 246 EXPECT_EQ(0, strncmp(output_buffer, |
| 247 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>", |
| 248 sizeof(output_buffer))); |
| 249 EXPECT_EQ(Filter::FILTER_OK, status); |
| 250 } |
| 251 |
| 252 TEST_F(SdchFilterTest, ErrorOnBadReturnCode) { |
| 253 std::vector<Filter::FilterType> filter_types; |
| 254 // Selective a tentative filter (which can fall back to pass through). |
| 255 filter_types.push_back(Filter::FILTER_TYPE_SDCH_POSSIBLE); |
| 256 const int kInputBufferSize(30); |
| 257 char output_buffer[20]; |
| 258 MockFilterContext filter_context(kInputBufferSize); |
| 259 // Response code needs to be 200 to allow a pass through. |
| 260 filter_context.SetResponseCode(403); |
| 261 // Meta refresh will only appear for html content, so set to something else |
| 262 // to induce an error (we can't meta refresh). |
| 263 filter_context.SetMimeType("anything"); |
| 264 std::string url_string("http://ignore.com"); |
| 265 filter_context.SetURL(GURL(url_string)); |
| 266 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 267 |
| 268 // Supply enough data to force a pass-through mode, which means we have |
| 269 // provided more than 9 characters that can't be a dictionary hash. |
| 270 std::string non_sdch_content("This is not SDCH"); |
| 271 |
| 272 char* input_buffer = filter->stream_buffer()->data(); |
| 273 int input_buffer_size = filter->stream_buffer_size(); |
| 274 EXPECT_EQ(kInputBufferSize, input_buffer_size); |
| 275 |
| 276 EXPECT_LT(static_cast<int>(non_sdch_content.size()), |
| 277 input_buffer_size); |
| 278 memcpy(input_buffer, non_sdch_content.data(), |
| 279 non_sdch_content.size()); |
| 280 filter->FlushStreamBuffer(non_sdch_content.size()); |
| 281 |
| 282 // Try to read output. |
| 283 int output_bytes_or_buffer_size = sizeof(output_buffer); |
| 284 Filter::FilterStatus status = filter->ReadData(output_buffer, |
| 285 &output_bytes_or_buffer_size); |
| 286 |
| 287 EXPECT_EQ(0, output_bytes_or_buffer_size); |
| 288 EXPECT_EQ(Filter::FILTER_ERROR, status); |
| 289 } |
| 290 |
| 291 TEST_F(SdchFilterTest, ErrorOnBadReturnCodeWithHtml) { |
| 292 std::vector<Filter::FilterType> filter_types; |
| 293 // Selective a tentative filter (which can fall back to pass through). |
| 294 filter_types.push_back(Filter::FILTER_TYPE_SDCH_POSSIBLE); |
| 295 const int kInputBufferSize(30); |
| 296 char output_buffer[20]; |
| 297 MockFilterContext filter_context(kInputBufferSize); |
| 298 // Response code needs to be 200 to allow a pass through. |
| 299 filter_context.SetResponseCode(403); |
| 300 // Meta refresh will only appear for html content |
| 301 filter_context.SetMimeType("text/html"); |
| 302 std::string url_string("http://ignore.com"); |
| 303 filter_context.SetURL(GURL(url_string)); |
| 304 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 305 |
| 306 // Supply enough data to force a pass-through mode, which means we have |
| 307 // provided more than 9 characters that can't be a dictionary hash. |
| 308 std::string non_sdch_content("This is not SDCH"); |
| 309 |
| 310 char* input_buffer = filter->stream_buffer()->data(); |
| 311 int input_buffer_size = filter->stream_buffer_size(); |
| 312 EXPECT_EQ(kInputBufferSize, input_buffer_size); |
| 313 |
| 314 EXPECT_LT(static_cast<int>(non_sdch_content.size()), |
| 315 input_buffer_size); |
| 316 memcpy(input_buffer, non_sdch_content.data(), |
| 317 non_sdch_content.size()); |
| 318 filter->FlushStreamBuffer(non_sdch_content.size()); |
| 319 |
| 320 // Try to read output. |
| 321 int output_bytes_or_buffer_size = sizeof(output_buffer); |
| 322 Filter::FilterStatus status = filter->ReadData(output_buffer, |
| 323 &output_bytes_or_buffer_size); |
| 324 |
| 325 // We should have read a long and complicated meta-refresh request. |
| 326 EXPECT_EQ(sizeof(output_buffer), output_bytes_or_buffer_size); |
| 327 // Check at least the prefix of the return. |
| 328 EXPECT_EQ(0, strncmp(output_buffer, |
| 329 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>", |
| 330 sizeof(output_buffer))); |
| 331 EXPECT_EQ(Filter::FILTER_OK, status); |
| 332 } |
| 333 |
| 334 TEST_F(SdchFilterTest, BasicBadDictionary) { |
| 335 std::vector<Filter::FilterType> filter_types; |
| 336 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 337 const int kInputBufferSize(30); |
| 338 char output_buffer[20]; |
| 339 MockFilterContext filter_context(kInputBufferSize); |
| 340 std::string url_string("http://ignore.com"); |
| 341 filter_context.SetURL(GURL(url_string)); |
| 342 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 343 |
| 344 // Supply bogus data (which doesn't yet specify a full dictionary hash). |
| 170 // Dictionary hash is 8 characters followed by a null. | 345 // Dictionary hash is 8 characters followed by a null. |
| 171 std::string dictionary_hash_prefix("123"); | 346 std::string dictionary_hash_prefix("123"); |
| 172 | 347 |
| 173 char* input_buffer = filter->stream_buffer()->data(); | 348 char* input_buffer = filter->stream_buffer()->data(); |
| 174 int input_buffer_size = filter->stream_buffer_size(); | 349 int input_buffer_size = filter->stream_buffer_size(); |
| 175 EXPECT_EQ(kInputBufferSize, input_buffer_size); | 350 EXPECT_EQ(kInputBufferSize, input_buffer_size); |
| 176 | 351 |
| 177 EXPECT_LT(static_cast<int>(dictionary_hash_prefix.size()), | 352 EXPECT_LT(static_cast<int>(dictionary_hash_prefix.size()), |
| 178 input_buffer_size); | 353 input_buffer_size); |
| 179 memcpy(input_buffer, dictionary_hash_prefix.data(), | 354 memcpy(input_buffer, dictionary_hash_prefix.data(), |
| 180 dictionary_hash_prefix.size()); | 355 dictionary_hash_prefix.size()); |
| 181 filter->FlushStreamBuffer(dictionary_hash_prefix.size()); | 356 filter->FlushStreamBuffer(dictionary_hash_prefix.size()); |
| 182 | 357 |
| 183 // With less than a dictionary specifier, try to read output. | 358 // With less than a dictionary specifier, try to read output. |
| 184 output_bytes_or_buffer_size = sizeof(output_buffer); | 359 int output_bytes_or_buffer_size = sizeof(output_buffer); |
| 185 status = filter->ReadData(output_buffer, &output_bytes_or_buffer_size); | 360 Filter::FilterStatus status = filter->ReadData(output_buffer, |
| 361 &output_bytes_or_buffer_size); |
| 186 | 362 |
| 187 EXPECT_EQ(0, output_bytes_or_buffer_size); | 363 EXPECT_EQ(0, output_bytes_or_buffer_size); |
| 188 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); | 364 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); |
| 189 | 365 |
| 190 // Provide enough data to complete *a* hash, but it is bogus, and not in our | 366 // Provide enough data to complete *a* hash, but it is bogus, and not in our |
| 191 // list of dictionaries, so the filter should error out immediately. | 367 // list of dictionaries, so the filter should error out immediately. |
| 192 std::string dictionary_hash_postfix("4abcd\0", 6); | 368 std::string dictionary_hash_postfix("4abcd\0", 6); |
| 193 | 369 |
| 194 CHECK(dictionary_hash_postfix.size() < | 370 CHECK(dictionary_hash_postfix.size() < |
| 195 static_cast<size_t>(input_buffer_size)); | 371 static_cast<size_t>(input_buffer_size)); |
| (...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 958 | 1134 |
| 959 TEST_F(SdchFilterTest, DictionaryTooLarge) { | 1135 TEST_F(SdchFilterTest, DictionaryTooLarge) { |
| 960 std::string dictionary_domain(".google.com"); | 1136 std::string dictionary_domain(".google.com"); |
| 961 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 1137 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 962 | 1138 |
| 963 dictionary_text.append( | 1139 dictionary_text.append( |
| 964 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' '); | 1140 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' '); |
| 965 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, | 1141 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 966 GURL("http://" + dictionary_domain))); | 1142 GURL("http://" + dictionary_domain))); |
| 967 } | 1143 } |
| OLD | NEW |