| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
|  | 2 // Use of this source code is governed by a BSD-style license that can be | 
|  | 3 // found in the LICENSE file. | 
|  | 4 | 
|  | 5 #include "net/http/infinite_cache.h" | 
|  | 6 | 
|  | 7 #include "base/file_util.h" | 
|  | 8 #include "base/scoped_temp_dir.h" | 
|  | 9 #include "base/threading/platform_thread.h" | 
|  | 10 #include "base/time.h" | 
|  | 11 #include "net/base/net_errors.h" | 
|  | 12 #include "net/base/test_completion_callback.h" | 
|  | 13 #include "net/http/http_transaction_unittest.h" | 
|  | 14 #include "testing/gtest/include/gtest/gtest.h" | 
|  | 15 | 
|  | 16 using base::Time; | 
|  | 17 using base::TimeDelta; | 
|  | 18 using net::InfiniteCache; | 
|  | 19 using net::InfiniteCacheTransaction; | 
|  | 20 | 
|  | 21 namespace { | 
|  | 22 | 
|  | 23 void StartRequest(const MockTransaction& http_transaction, | 
|  | 24                   InfiniteCacheTransaction* transaction) { | 
|  | 25   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 26       new net::HttpResponseHeaders(http_transaction.response_headers)); | 
|  | 27   net::HttpResponseInfo response; | 
|  | 28   response.headers = headers; | 
|  | 29   response.request_time = http_transaction.request_time.is_null() ? | 
|  | 30                           Time::Now() : http_transaction.request_time; | 
|  | 31   response.response_time = Time::Now(); | 
|  | 32 | 
|  | 33   MockHttpRequest request(http_transaction); | 
|  | 34   transaction->OnRequestStart(&request); | 
|  | 35   transaction->OnResponseReceived(&response); | 
|  | 36 } | 
|  | 37 | 
|  | 38 void ProcessRequest(const MockTransaction& http_transaction, | 
|  | 39                     InfiniteCache* cache) { | 
|  | 40   scoped_ptr<InfiniteCacheTransaction> transaction | 
|  | 41       (cache->CreateInfiniteCacheTransaction()); | 
|  | 42 | 
|  | 43   StartRequest(http_transaction, transaction.get()); | 
|  | 44   transaction->OnDataRead(http_transaction.data, strlen(http_transaction.data)); | 
|  | 45 } | 
|  | 46 | 
|  | 47 void ProcessRequestWithTime(const MockTransaction& http_transaction, | 
|  | 48                             InfiniteCache* cache, | 
|  | 49                             Time time) { | 
|  | 50   scoped_ptr<InfiniteCacheTransaction> transaction | 
|  | 51       (cache->CreateInfiniteCacheTransaction()); | 
|  | 52 | 
|  | 53   MockTransaction timed_transaction = http_transaction; | 
|  | 54   timed_transaction.request_time = time; | 
|  | 55   StartRequest(timed_transaction, transaction.get()); | 
|  | 56   transaction->OnDataRead(http_transaction.data, strlen(http_transaction.data)); | 
|  | 57 } | 
|  | 58 | 
|  | 59 }  // namespace | 
|  | 60 | 
|  | 61 TEST(InfiniteCache, Basics) { | 
|  | 62   InfiniteCache cache; | 
|  | 63   cache.Init(FilePath()); | 
|  | 64 | 
|  | 65   scoped_ptr<InfiniteCacheTransaction> transaction | 
|  | 66       (cache.CreateInfiniteCacheTransaction()); | 
|  | 67 | 
|  | 68   // Don't even Start() this transaction. | 
|  | 69   transaction.reset(cache.CreateInfiniteCacheTransaction()); | 
|  | 70 | 
|  | 71   net::TestCompletionCallback cb; | 
|  | 72   EXPECT_EQ(0, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 73 | 
|  | 74   MockHttpRequest request(kTypicalGET_Transaction); | 
|  | 75   transaction->OnRequestStart(&request); | 
|  | 76 | 
|  | 77   // Don't have a response yet. | 
|  | 78   transaction.reset(cache.CreateInfiniteCacheTransaction()); | 
|  | 79   EXPECT_EQ(0, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 80 | 
|  | 81   net::HttpResponseInfo response; | 
|  | 82   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 83       new net::HttpResponseHeaders(kTypicalGET_Transaction.response_headers)); | 
|  | 84   response.headers = headers; | 
|  | 85 | 
|  | 86   transaction->OnRequestStart(&request); | 
|  | 87   transaction->OnResponseReceived(&response); | 
|  | 88   transaction.reset(cache.CreateInfiniteCacheTransaction()); | 
|  | 89   EXPECT_EQ(1, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 90 | 
|  | 91   // Hit the same URL again. | 
|  | 92   transaction->OnRequestStart(&request); | 
|  | 93   transaction->OnResponseReceived(&response); | 
|  | 94   transaction.reset(cache.CreateInfiniteCacheTransaction()); | 
|  | 95   EXPECT_EQ(1, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 96 | 
|  | 97   // Now a different URL. | 
|  | 98   MockHttpRequest request2(kSimpleGET_Transaction); | 
|  | 99   transaction->OnRequestStart(&request2); | 
|  | 100   transaction->OnResponseReceived(&response); | 
|  | 101   transaction.reset(); | 
|  | 102   EXPECT_EQ(2, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 103 } | 
|  | 104 | 
|  | 105 TEST(InfiniteCache, Save_Restore) { | 
|  | 106   ScopedTempDir dir; | 
|  | 107   ASSERT_TRUE(dir.CreateUniqueTempDir()); | 
|  | 108   FilePath path = dir.path().Append(FILE_PATH_LITERAL("infinite")); | 
|  | 109 | 
|  | 110   scoped_ptr<InfiniteCache> cache(new InfiniteCache); | 
|  | 111   cache->Init(path); | 
|  | 112   net::TestCompletionCallback cb; | 
|  | 113 | 
|  | 114   ProcessRequest(kTypicalGET_Transaction, cache.get()); | 
|  | 115   ProcessRequest(kSimpleGET_Transaction, cache.get()); | 
|  | 116   ProcessRequest(kETagGET_Transaction, cache.get()); | 
|  | 117   ProcessRequest(kSimplePOST_Transaction, cache.get()); | 
|  | 118 | 
|  | 119   EXPECT_EQ(3, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 120   EXPECT_EQ(net::OK, cb.GetResult(cache->FlushDataForTest(cb.callback()))); | 
|  | 121 | 
|  | 122   cache.reset(new InfiniteCache); | 
|  | 123   cache->Init(path); | 
|  | 124   EXPECT_EQ(3, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 125 | 
|  | 126   ProcessRequest(kTypicalGET_Transaction, cache.get()); | 
|  | 127   EXPECT_EQ(3, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 128   EXPECT_EQ(net::OK, cb.GetResult(cache->FlushDataForTest(cb.callback()))); | 
|  | 129 } | 
|  | 130 | 
|  | 131 TEST(InfiniteCache, DoomMethod) { | 
|  | 132   InfiniteCache cache; | 
|  | 133   cache.Init(FilePath()); | 
|  | 134 | 
|  | 135   ProcessRequest(kTypicalGET_Transaction, &cache); | 
|  | 136   ProcessRequest(kSimpleGET_Transaction, &cache); | 
|  | 137   ProcessRequest(kETagGET_Transaction, &cache); | 
|  | 138   net::TestCompletionCallback cb; | 
|  | 139   EXPECT_EQ(3, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 140 | 
|  | 141   MockTransaction request(kTypicalGET_Transaction); | 
|  | 142   request.method = "PUT"; | 
|  | 143   ProcessRequest(request, &cache); | 
|  | 144   EXPECT_EQ(2, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 145 | 
|  | 146   request.method = "POST"; | 
|  | 147   request.url = kSimpleGET_Transaction.url; | 
|  | 148   ProcessRequest(request, &cache); | 
|  | 149   EXPECT_EQ(1, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 150 | 
|  | 151   request.method = "DELETE"; | 
|  | 152   request.url = kETagGET_Transaction.url; | 
|  | 153   ProcessRequest(request, &cache); | 
|  | 154   EXPECT_EQ(0, cb.GetResult(cache.QueryItemsForTest(cb.callback()))); | 
|  | 155 } | 
|  | 156 | 
|  | 157 TEST(InfiniteCache, Delete) { | 
|  | 158   ScopedTempDir dir; | 
|  | 159   ASSERT_TRUE(dir.CreateUniqueTempDir()); | 
|  | 160   FilePath path = dir.path().Append(FILE_PATH_LITERAL("infinite")); | 
|  | 161 | 
|  | 162   scoped_ptr<InfiniteCache> cache(new InfiniteCache); | 
|  | 163   cache->Init(path); | 
|  | 164   net::TestCompletionCallback cb; | 
|  | 165 | 
|  | 166   ProcessRequest(kTypicalGET_Transaction, cache.get()); | 
|  | 167   ProcessRequest(kSimpleGET_Transaction, cache.get()); | 
|  | 168   EXPECT_EQ(2, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 169   EXPECT_EQ(net::OK, cb.GetResult(cache->FlushDataForTest(cb.callback()))); | 
|  | 170   EXPECT_TRUE(file_util::PathExists(path)); | 
|  | 171 | 
|  | 172   cache.reset(new InfiniteCache); | 
|  | 173   cache->Init(path); | 
|  | 174   EXPECT_EQ(2, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 175   EXPECT_EQ(net::OK, cb.GetResult(cache->DeleteData(cb.callback()))); | 
|  | 176   EXPECT_EQ(0, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 177   EXPECT_FALSE(file_util::PathExists(path)); | 
|  | 178 | 
|  | 179   EXPECT_EQ(net::OK, cb.GetResult(cache->FlushDataForTest(cb.callback()))); | 
|  | 180 } | 
|  | 181 | 
|  | 182 TEST(InfiniteCache, DeleteBetween) { | 
|  | 183   ScopedTempDir dir; | 
|  | 184   ASSERT_TRUE(dir.CreateUniqueTempDir()); | 
|  | 185   FilePath path = dir.path().Append(FILE_PATH_LITERAL("infinite")); | 
|  | 186 | 
|  | 187   scoped_ptr<InfiniteCache> cache(new InfiniteCache); | 
|  | 188   cache->Init(path); | 
|  | 189   net::TestCompletionCallback cb; | 
|  | 190 | 
|  | 191   Time::Exploded baseline = {}; | 
|  | 192   baseline.year = 2012; | 
|  | 193   baseline.month = 1; | 
|  | 194   baseline.day_of_month = 1; | 
|  | 195   Time base_time = Time::FromUTCExploded(baseline); | 
|  | 196 | 
|  | 197   ProcessRequestWithTime(kTypicalGET_Transaction, cache.get(), base_time); | 
|  | 198 | 
|  | 199   Time start = base_time + TimeDelta::FromSeconds(2); | 
|  | 200   ProcessRequestWithTime(kSimpleGET_Transaction, cache.get(), start); | 
|  | 201   Time end = start + TimeDelta::FromSeconds(2); | 
|  | 202 | 
|  | 203   ProcessRequestWithTime(kETagGET_Transaction, cache.get(), | 
|  | 204                           end + TimeDelta::FromSeconds(2)); | 
|  | 205 | 
|  | 206   EXPECT_EQ(3, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 207   EXPECT_EQ(net::OK, | 
|  | 208             cb.GetResult(cache->DeleteDataBetween(start, end, | 
|  | 209                                                   cb.callback()))); | 
|  | 210   EXPECT_EQ(2, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 211 | 
|  | 212   // Make sure the data is deleted from disk. | 
|  | 213   EXPECT_EQ(net::OK, cb.GetResult(cache->FlushDataForTest(cb.callback()))); | 
|  | 214   cache.reset(new InfiniteCache); | 
|  | 215   cache->Init(path); | 
|  | 216 | 
|  | 217   EXPECT_EQ(2, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 218   ProcessRequest(kETagGET_Transaction, cache.get()); | 
|  | 219   EXPECT_EQ(2, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 220 | 
|  | 221   EXPECT_EQ(net::OK, | 
|  | 222             cb.GetResult(cache->DeleteDataBetween(start, Time::Now(), | 
|  | 223                                                   cb.callback()))); | 
|  | 224   EXPECT_EQ(1, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 225 | 
|  | 226   // Make sure the data is deleted from disk. | 
|  | 227   EXPECT_EQ(net::OK, cb.GetResult(cache->FlushDataForTest(cb.callback()))); | 
|  | 228   cache.reset(new InfiniteCache); | 
|  | 229   cache->Init(path); | 
|  | 230 | 
|  | 231   EXPECT_EQ(1, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 232   ProcessRequest(kTypicalGET_Transaction, cache.get()); | 
|  | 233   EXPECT_EQ(1, cb.GetResult(cache->QueryItemsForTest(cb.callback()))); | 
|  | 234 } | 
| OLD | NEW | 
|---|