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