Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: net/http/http_cache_unittest.cc

Issue 517043: Http cache: Avoid resuming (and keeping) truncated entries... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Add Accept-Ranges:none check Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_response_headers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 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/http/http_cache.h" 5 #include "net/http/http_cache.h"
6 6
7 #include "base/hash_tables.h" 7 #include "base/hash_tables.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/scoped_vector.h" 9 #include "base/scoped_vector.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 EXPECT_EQ(0, len % 10); 724 EXPECT_EQ(0, len % 10);
725 std::string content_length = StringPrintf("Content-Length: %d\n", len); 725 std::string content_length = StringPrintf("Content-Length: %d\n", len);
726 response_headers->replace(response_headers->find("Content-Length:"), 726 response_headers->replace(response_headers->find("Content-Length:"),
727 content_length.size(), content_length); 727 content_length.size(), content_length);
728 } 728 }
729 if (bad_200_) { 729 if (bad_200_) {
730 // We return a range, but with a response code of 200. 730 // We return a range, but with a response code of 200.
731 response_status->assign("HTTP/1.1 200 Success"); 731 response_status->assign("HTTP/1.1 200 Success");
732 } 732 }
733 } else { 733 } else {
734 // Check that we use only one validation header.
735 EXPECT_EQ(std::string::npos,
736 request->extra_headers.find("If-Modified-Since"));
737 response_status->assign("HTTP/1.1 304 Not Modified"); 734 response_status->assign("HTTP/1.1 304 Not Modified");
738 response_data->clear(); 735 response_data->clear();
739 } 736 }
740 } 737 }
741 738
742 const MockTransaction kRangeGET_TransactionOK = { 739 const MockTransaction kRangeGET_TransactionOK = {
743 "http://www.google.com/range", 740 "http://www.google.com/range",
744 "GET", 741 "GET",
745 base::Time(), 742 base::Time(),
746 "Range: bytes = 40-49\r\n" 743 "Range: bytes = 40-49\r\n"
(...skipping 2512 matching lines...) Expand 10 before | Expand all | Expand 10 after
3259 // entry. 3256 // entry.
3260 delete c; 3257 delete c;
3261 3258
3262 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 3259 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
3263 3260
3264 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3261 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3265 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3262 EXPECT_EQ(0, cache.disk_cache()->open_count());
3266 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3263 EXPECT_EQ(2, cache.disk_cache()->create_count());
3267 } 3264 }
3268 3265
3266 // Tests that we delete an entry when the request is cancelled if the response
3267 // does not have content-length and strong validators.
3268 TEST(HttpCache, DoomOnDestruction2) {
3269 MockHttpCache cache;
3270 cache.http_cache()->set_enable_range_support(true);
3271
3272 MockHttpRequest request(kSimpleGET_Transaction);
3273
3274 Context* c = new Context();
3275 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3276 EXPECT_EQ(net::OK, rv);
3277
3278 rv = c->trans->Start(&request, &c->callback, NULL);
3279 if (rv == net::ERR_IO_PENDING)
3280 rv = c->callback.WaitForResult();
3281
3282 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3283 EXPECT_EQ(0, cache.disk_cache()->open_count());
3284 EXPECT_EQ(1, cache.disk_cache()->create_count());
3285
3286 // Make sure that the entry has some data stored.
3287 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10);
3288 rv = c->trans->Read(buf, buf->size(), &c->callback);
3289 if (rv == net::ERR_IO_PENDING)
3290 rv = c->callback.WaitForResult();
3291 EXPECT_EQ(buf->size(), rv);
3292
3293 // Destroy the transaction.
3294 delete c;
3295
3296 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
3297
3298 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3299 EXPECT_EQ(0, cache.disk_cache()->open_count());
3300 EXPECT_EQ(2, cache.disk_cache()->create_count());
3301 }
3302
3303 // Tests that we delete an entry when the request is cancelled if the response
3304 // has an "Accept-Ranges: none" header.
3305 TEST(HttpCache, DoomOnDestruction3) {
3306 MockHttpCache cache;
3307 cache.http_cache()->set_enable_range_support(true);
3308
3309 MockTransaction transaction(kSimpleGET_Transaction);
3310 transaction.response_headers =
3311 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
3312 "Content-Length: 22\n"
3313 "Accept-Ranges: none\n"
3314 "Etag: foopy\n";
3315 AddMockTransaction(&transaction);
3316 MockHttpRequest request(transaction);
3317
3318 Context* c = new Context();
3319 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3320 EXPECT_EQ(net::OK, rv);
3321
3322 rv = c->trans->Start(&request, &c->callback, NULL);
3323 if (rv == net::ERR_IO_PENDING)
3324 rv = c->callback.WaitForResult();
3325
3326 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3327 EXPECT_EQ(0, cache.disk_cache()->open_count());
3328 EXPECT_EQ(1, cache.disk_cache()->create_count());
3329
3330 // Make sure that the entry has some data stored.
3331 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10);
3332 rv = c->trans->Read(buf, buf->size(), &c->callback);
3333 if (rv == net::ERR_IO_PENDING)
3334 rv = c->callback.WaitForResult();
3335 EXPECT_EQ(buf->size(), rv);
3336
3337 // Destroy the transaction.
3338 delete c;
3339
3340 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
3341
3342 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3343 EXPECT_EQ(0, cache.disk_cache()->open_count());
3344 EXPECT_EQ(2, cache.disk_cache()->create_count());
3345
3346 RemoveMockTransaction(&transaction);
3347 }
3348
3269 // Tests that we mark an entry as incomplete when the request is cancelled. 3349 // Tests that we mark an entry as incomplete when the request is cancelled.
3270 TEST(HttpCache, Set_Truncated_Flag) { 3350 TEST(HttpCache, Set_Truncated_Flag) {
3271 MockHttpCache cache; 3351 MockHttpCache cache;
3272 cache.http_cache()->set_enable_range_support(true); 3352 cache.http_cache()->set_enable_range_support(true);
3273 3353
3274 MockHttpRequest request(kSimpleGET_Transaction); 3354 MockTransaction transaction(kSimpleGET_Transaction);
3355 transaction.response_headers =
3356 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
3357 "Content-Length: 22\n"
3358 "Etag: foopy\n";
3359 AddMockTransaction(&transaction);
3360 MockHttpRequest request(transaction);
3275 3361
3276 Context* c = new Context(); 3362 Context* c = new Context();
3277 int rv = cache.http_cache()->CreateTransaction(&c->trans); 3363 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3278 EXPECT_EQ(net::OK, rv); 3364 EXPECT_EQ(net::OK, rv);
3279 3365
3280 rv = c->trans->Start(&request, &c->callback, NULL); 3366 rv = c->trans->Start(&request, &c->callback, NULL);
3281 if (rv == net::ERR_IO_PENDING) 3367 if (rv == net::ERR_IO_PENDING)
3282 rv = c->callback.WaitForResult(); 3368 rv = c->callback.WaitForResult();
3283 3369
3284 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3370 EXPECT_EQ(1, cache.network_layer()->transaction_count());
(...skipping 12 matching lines...) Expand all
3297 3383
3298 // Verify that the entry is marked as incomplete. 3384 // Verify that the entry is marked as incomplete.
3299 disk_cache::Entry* entry; 3385 disk_cache::Entry* entry;
3300 ASSERT_TRUE(cache.disk_cache()->OpenEntry(kSimpleGET_Transaction.url, 3386 ASSERT_TRUE(cache.disk_cache()->OpenEntry(kSimpleGET_Transaction.url,
3301 &entry)); 3387 &entry));
3302 net::HttpResponseInfo response; 3388 net::HttpResponseInfo response;
3303 bool truncated = false; 3389 bool truncated = false;
3304 EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated)); 3390 EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated));
3305 EXPECT_TRUE(truncated); 3391 EXPECT_TRUE(truncated);
3306 entry->Close(); 3392 entry->Close();
3393
3394 RemoveMockTransaction(&transaction);
3307 } 3395 }
3308 3396
3309 // Tests that we can continue with a request that was interrupted. 3397 // Tests that we can continue with a request that was interrupted.
3310 TEST(HttpCache, GET_IncompleteResource) { 3398 TEST(HttpCache, GET_IncompleteResource) {
3311 MockHttpCache cache; 3399 MockHttpCache cache;
3312 cache.http_cache()->set_enable_range_support(true); 3400 cache.http_cache()->set_enable_range_support(true);
3313 AddMockTransaction(&kRangeGET_TransactionOK); 3401 AddMockTransaction(&kRangeGET_TransactionOK);
3314 3402
3315 // Create a disk cache entry that stores an incomplete resource. 3403 // Create a disk cache entry that stores an incomplete resource.
3316 disk_cache::Entry* entry; 3404 disk_cache::Entry* entry;
3317 ASSERT_TRUE(cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url, 3405 ASSERT_TRUE(cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url,
3318 &entry)); 3406 &entry));
3319 3407
3320 // Content-length will be intentionally bogus.
3321 std::string raw_headers("HTTP/1.1 200 OK\n" 3408 std::string raw_headers("HTTP/1.1 200 OK\n"
3322 "Last-Modified: something\n" 3409 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
3323 "ETag: \"foo\"\n" 3410 "ETag: \"foo\"\n"
3324 "Accept-Ranges: bytes\n" 3411 "Accept-Ranges: bytes\n"
3325 "Content-Length: 10\n"); 3412 "Content-Length: 80\n");
3326 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), 3413 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
3327 raw_headers.size()); 3414 raw_headers.size());
3328 3415
3329 net::HttpResponseInfo response; 3416 net::HttpResponseInfo response;
3330 response.headers = new net::HttpResponseHeaders(raw_headers); 3417 response.headers = new net::HttpResponseHeaders(raw_headers);
3331 // Set the last argument for this to be an incomplete request. 3418 // Set the last argument for this to be an incomplete request.
3332 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true)); 3419 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true));
3333 3420
3334 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); 3421 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
3335 int len = static_cast<int>(base::strlcpy(buf->data(), 3422 int len = static_cast<int>(base::strlcpy(buf->data(),
3336 "rg: 00-09 rg: 10-19 ", 100)); 3423 "rg: 00-09 rg: 10-19 ", 100));
3337 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); 3424 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true));
3338 3425
3339 // Now make a regular request. 3426 // Now make a regular request.
3340 std::string headers; 3427 std::string headers;
3341 MockTransaction transaction(kRangeGET_TransactionOK); 3428 MockTransaction transaction(kRangeGET_TransactionOK);
3342 transaction.request_headers = EXTRA_HEADER; 3429 transaction.request_headers = EXTRA_HEADER;
3343 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " 3430 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
3344 "rg: 50-59 rg: 60-69 rg: 70-79 "; 3431 "rg: 50-59 rg: 60-69 rg: 70-79 ";
3345 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 3432 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3346 3433
3347 // We update the headers with the ones received while revalidating. 3434 // We update the headers with the ones received while revalidating.
3348 std::string expected_headers( 3435 std::string expected_headers(
3349 "HTTP/1.1 200 OK\n" 3436 "HTTP/1.1 200 OK\n"
3350 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" 3437 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
3351 "Accept-Ranges: bytes\n" 3438 "Accept-Ranges: bytes\n"
3352 "ETag: \"foo\"\n" 3439 "ETag: \"foo\"\n"
3353 "Content-Length: 10\n"); 3440 "Content-Length: 80\n");
3354 3441
3355 EXPECT_EQ(expected_headers, headers); 3442 EXPECT_EQ(expected_headers, headers);
3356 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3443 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3357 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3444 EXPECT_EQ(1, cache.disk_cache()->open_count());
3358 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3445 EXPECT_EQ(1, cache.disk_cache()->create_count());
3359 3446
3360 RemoveMockTransaction(&kRangeGET_TransactionOK); 3447 RemoveMockTransaction(&kRangeGET_TransactionOK);
3361 3448
3362 // Verify that the disk entry was updated. 3449 // Verify that the disk entry was updated.
3363 EXPECT_EQ(80, entry->GetDataSize(1)); 3450 EXPECT_EQ(80, entry->GetDataSize(1));
3364 bool truncated = true; 3451 bool truncated = true;
3365 EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated)); 3452 EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated));
3366 EXPECT_FALSE(truncated); 3453 EXPECT_FALSE(truncated);
3367 entry->Close(); 3454 entry->Close();
3368 } 3455 }
3369 3456
3457 // Tests that we delete truncated entries if the server changes its mind midway.
3458 TEST(HttpCache, GET_IncompleteResource2) {
3459 MockHttpCache cache;
3460 cache.http_cache()->set_enable_range_support(true);
3461 AddMockTransaction(&kRangeGET_TransactionOK);
3462
3463 // Create a disk cache entry that stores an incomplete resource.
3464 disk_cache::Entry* entry;
3465 ASSERT_TRUE(cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url,
3466 &entry));
3467
3468 // Content-length will be intentionally bad.
3469 std::string raw_headers("HTTP/1.1 200 OK\n"
3470 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
3471 "ETag: \"foo\"\n"
3472 "Accept-Ranges: bytes\n"
3473 "Content-Length: 50\n");
3474 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
3475 raw_headers.size());
3476
3477 net::HttpResponseInfo response;
3478 response.headers = new net::HttpResponseHeaders(raw_headers);
3479 // Set the last argument for this to be an incomplete request.
3480 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true));
3481
3482 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
3483 int len = static_cast<int>(base::strlcpy(buf->data(),
3484 "rg: 00-09 rg: 10-19 ", 100));
3485 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true));
3486 entry->Close();
3487
3488 // Now make a regular request.
3489 std::string headers;
3490 MockTransaction transaction(kRangeGET_TransactionOK);
3491 transaction.request_headers = EXTRA_HEADER;
3492 transaction.data = "rg: 00-09 rg: 10-19 ";
3493 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3494
3495 // We update the headers with the ones received while revalidating.
3496 std::string expected_headers(
3497 "HTTP/1.1 200 OK\n"
3498 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
3499 "Accept-Ranges: bytes\n"
3500 "ETag: \"foo\"\n"
3501 "Content-Length: 50\n");
3502
3503 EXPECT_EQ(expected_headers, headers);
3504 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3505 EXPECT_EQ(1, cache.disk_cache()->open_count());
3506 EXPECT_EQ(1, cache.disk_cache()->create_count());
3507
3508 RemoveMockTransaction(&kRangeGET_TransactionOK);
3509
3510 // Verify that the disk entry was deleted.
3511 EXPECT_FALSE(cache.disk_cache()->OpenEntry(kRangeGET_TransactionOK.url,
3512 &entry));
3513 }
3514
3370 // Tests that when we cancel a request that was interrupted, we mark it again 3515 // Tests that when we cancel a request that was interrupted, we mark it again
3371 // as truncated. 3516 // as truncated.
3372 TEST(HttpCache, GET_CancelIncompleteResource) { 3517 TEST(HttpCache, GET_CancelIncompleteResource) {
3373 MockHttpCache cache; 3518 MockHttpCache cache;
3374 cache.http_cache()->set_enable_range_support(true); 3519 cache.http_cache()->set_enable_range_support(true);
3375 AddMockTransaction(&kRangeGET_TransactionOK); 3520 AddMockTransaction(&kRangeGET_TransactionOK);
3376 3521
3377 // Create a disk cache entry that stores an incomplete resource. 3522 // Create a disk cache entry that stores an incomplete resource.
3378 disk_cache::Entry* entry; 3523 disk_cache::Entry* entry;
3379 ASSERT_TRUE(cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url, 3524 ASSERT_TRUE(cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url,
3380 &entry)); 3525 &entry));
3381 3526
3382 // Content-length will be intentionally bogus.
3383 std::string raw_headers("HTTP/1.1 200 OK\n" 3527 std::string raw_headers("HTTP/1.1 200 OK\n"
3384 "Last-Modified: something\n" 3528 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
3385 "ETag: \"foo\"\n" 3529 "ETag: \"foo\"\n"
3386 "Accept-Ranges: bytes\n" 3530 "Accept-Ranges: bytes\n"
3387 "Content-Length: 10\n"); 3531 "Content-Length: 80\n");
3388 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), 3532 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
3389 raw_headers.size()); 3533 raw_headers.size());
3390 3534
3391 net::HttpResponseInfo response; 3535 net::HttpResponseInfo response;
3392 response.headers = new net::HttpResponseHeaders(raw_headers); 3536 response.headers = new net::HttpResponseHeaders(raw_headers);
3393 3537
3394 // Set the last argument for this to be an incomplete request. 3538 // Set the last argument for this to be an incomplete request.
3395 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true)); 3539 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true));
3396 3540
3397 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(100)); 3541 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(100));
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
3811 std::string headers; 3955 std::string headers;
3812 response.headers->GetNormalizedHeaders(&headers); 3956 response.headers->GetNormalizedHeaders(&headers);
3813 3957
3814 EXPECT_EQ("HTTP/1.1 200 OK\n" 3958 EXPECT_EQ("HTTP/1.1 200 OK\n"
3815 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" 3959 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
3816 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", 3960 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
3817 headers); 3961 headers);
3818 3962
3819 RemoveMockTransaction(&mock_network_response); 3963 RemoveMockTransaction(&mock_network_response);
3820 } 3964 }
OLDNEW
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_response_headers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698