| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 1026 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1037 cache.disk_cache()->set_fail_requests(); | 1037 cache.disk_cache()->set_fail_requests(); |
| 1038 | 1038 |
| 1039 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); | 1039 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
| 1040 | 1040 |
| 1041 // Read from the network, and don't use the cache. | 1041 // Read from the network, and don't use the cache. |
| 1042 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, | 1042 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
| 1043 log.bound()); | 1043 log.bound()); |
| 1044 | 1044 |
| 1045 // Check that the NetLog was filled as expected. | 1045 // Check that the NetLog was filled as expected. |
| 1046 // (We attempted to both Open and Create entries, but both failed). | 1046 // (We attempted to both Open and Create entries, but both failed). |
| 1047 EXPECT_EQ(6u, log.entries().size()); | 1047 net::CapturingNetLog::EntryList entries; |
| 1048 log.GetEntries(&entries); |
| 1049 |
| 1050 EXPECT_EQ(6u, entries.size()); |
| 1048 EXPECT_TRUE(net::LogContainsBeginEvent( | 1051 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1049 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1052 entries, 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1050 EXPECT_TRUE(net::LogContainsEndEvent( | 1053 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1051 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1054 entries, 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1052 EXPECT_TRUE(net::LogContainsBeginEvent( | 1055 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1053 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1056 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 1054 EXPECT_TRUE(net::LogContainsEndEvent( | 1057 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1055 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1058 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 1056 EXPECT_TRUE(net::LogContainsBeginEvent( | 1059 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1057 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1060 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1058 EXPECT_TRUE(net::LogContainsEndEvent( | 1061 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1059 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1062 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1060 | 1063 |
| 1061 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1064 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1062 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1065 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1063 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 1066 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 1064 } | 1067 } |
| 1065 | 1068 |
| 1066 TEST(HttpCache, SimpleGETNoDiskCache2) { | 1069 TEST(HttpCache, SimpleGETNoDiskCache2) { |
| 1067 // This will initialize a cache object with NULL backend. | 1070 // This will initialize a cache object with NULL backend. |
| 1068 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | 1071 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1069 factory->set_fail(true); | 1072 factory->set_fail(true); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1138 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { | 1141 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { |
| 1139 MockHttpCache cache; | 1142 MockHttpCache cache; |
| 1140 | 1143 |
| 1141 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); | 1144 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
| 1142 | 1145 |
| 1143 // write to the cache | 1146 // write to the cache |
| 1144 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, | 1147 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
| 1145 log.bound()); | 1148 log.bound()); |
| 1146 | 1149 |
| 1147 // Check that the NetLog was filled as expected. | 1150 // Check that the NetLog was filled as expected. |
| 1148 EXPECT_EQ(8u, log.entries().size()); | 1151 net::CapturingNetLog::EntryList entries; |
| 1152 log.GetEntries(&entries); |
| 1153 |
| 1154 EXPECT_EQ(8u, entries.size()); |
| 1149 EXPECT_TRUE(net::LogContainsBeginEvent( | 1155 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1150 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1156 entries, 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1151 EXPECT_TRUE(net::LogContainsEndEvent( | 1157 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1152 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1158 entries, 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1153 EXPECT_TRUE(net::LogContainsBeginEvent( | 1159 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1154 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1160 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 1155 EXPECT_TRUE(net::LogContainsEndEvent( | 1161 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1156 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1162 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 1157 EXPECT_TRUE(net::LogContainsBeginEvent( | 1163 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1158 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1164 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1159 EXPECT_TRUE(net::LogContainsEndEvent( | 1165 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1160 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1166 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1161 EXPECT_TRUE(net::LogContainsBeginEvent( | 1167 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1162 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1168 entries, 6, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1163 EXPECT_TRUE(net::LogContainsEndEvent( | 1169 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1164 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1170 entries, 7, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1165 | 1171 |
| 1166 // force this transaction to read from the cache | 1172 // force this transaction to read from the cache |
| 1167 MockTransaction transaction(kSimpleGET_Transaction); | 1173 MockTransaction transaction(kSimpleGET_Transaction); |
| 1168 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 1174 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 1169 | 1175 |
| 1170 log.Clear(); | 1176 log.Clear(); |
| 1171 | 1177 |
| 1172 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); | 1178 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
| 1173 | 1179 |
| 1174 // Check that the NetLog was filled as expected. | 1180 // Check that the NetLog was filled as expected. |
| 1175 EXPECT_EQ(8u, log.entries().size()); | 1181 log.GetEntries(&entries); |
| 1182 |
| 1183 EXPECT_EQ(8u, entries.size()); |
| 1176 EXPECT_TRUE(net::LogContainsBeginEvent( | 1184 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1177 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1185 entries, 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1178 EXPECT_TRUE(net::LogContainsEndEvent( | 1186 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1179 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1187 entries, 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1180 EXPECT_TRUE(net::LogContainsBeginEvent( | 1188 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1181 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1189 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 1182 EXPECT_TRUE(net::LogContainsEndEvent( | 1190 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1183 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | 1191 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 1184 EXPECT_TRUE(net::LogContainsBeginEvent( | 1192 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1185 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1193 entries, 4, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1186 EXPECT_TRUE(net::LogContainsEndEvent( | 1194 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1187 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1195 entries, 5, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1188 EXPECT_TRUE(net::LogContainsBeginEvent( | 1196 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1189 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); | 1197 entries, 6, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
| 1190 EXPECT_TRUE(net::LogContainsEndEvent( | 1198 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1191 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); | 1199 entries, 7, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
| 1192 | 1200 |
| 1193 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1201 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1194 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 1202 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1195 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1203 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1196 } | 1204 } |
| 1197 | 1205 |
| 1198 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { | 1206 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
| 1199 MockHttpCache cache; | 1207 MockHttpCache cache; |
| 1200 | 1208 |
| 1201 // force this transaction to read from the cache | 1209 // force this transaction to read from the cache |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1261 | 1269 |
| 1262 // Force this transaction to write to the cache again. | 1270 // Force this transaction to write to the cache again. |
| 1263 MockTransaction transaction(kSimpleGET_Transaction); | 1271 MockTransaction transaction(kSimpleGET_Transaction); |
| 1264 transaction.load_flags |= net::LOAD_BYPASS_CACHE; | 1272 transaction.load_flags |= net::LOAD_BYPASS_CACHE; |
| 1265 | 1273 |
| 1266 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); | 1274 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
| 1267 | 1275 |
| 1268 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); | 1276 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
| 1269 | 1277 |
| 1270 // Check that the NetLog was filled as expected. | 1278 // Check that the NetLog was filled as expected. |
| 1271 EXPECT_EQ(8u, log.entries().size()); | 1279 net::CapturingNetLog::EntryList entries; |
| 1280 log.GetEntries(&entries); |
| 1281 |
| 1282 EXPECT_EQ(8u, entries.size()); |
| 1272 EXPECT_TRUE(net::LogContainsBeginEvent( | 1283 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1273 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1284 entries, 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1274 EXPECT_TRUE(net::LogContainsEndEvent( | 1285 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1275 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1286 entries, 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1276 EXPECT_TRUE(net::LogContainsBeginEvent( | 1287 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1277 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); | 1288 entries, 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
| 1278 EXPECT_TRUE(net::LogContainsEndEvent( | 1289 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1279 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); | 1290 entries, 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
| 1280 EXPECT_TRUE(net::LogContainsBeginEvent( | 1291 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1281 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1292 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1282 EXPECT_TRUE(net::LogContainsEndEvent( | 1293 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1283 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | 1294 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1284 EXPECT_TRUE(net::LogContainsBeginEvent( | 1295 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1285 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1296 entries, 6, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1286 EXPECT_TRUE(net::LogContainsEndEvent( | 1297 EXPECT_TRUE(net::LogContainsEndEvent( |
| 1287 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_WAITING)); | 1298 entries, 7, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
| 1288 | 1299 |
| 1289 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1300 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1290 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1301 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1291 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1302 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1292 } | 1303 } |
| 1293 | 1304 |
| 1294 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { | 1305 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { |
| 1295 MockHttpCache cache; | 1306 MockHttpCache cache; |
| 1296 | 1307 |
| 1297 // write to the cache | 1308 // write to the cache |
| (...skipping 3547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4845 // Now return 200 when validating the entry so the metadata will be lost. | 4856 // Now return 200 when validating the entry so the metadata will be lost. |
| 4846 MockTransaction trans2(kTypicalGET_Transaction); | 4857 MockTransaction trans2(kTypicalGET_Transaction); |
| 4847 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 4858 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
| 4848 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 4859 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
| 4849 EXPECT_TRUE(response.metadata.get() == NULL); | 4860 EXPECT_TRUE(response.metadata.get() == NULL); |
| 4850 | 4861 |
| 4851 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4862 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4852 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 4863 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| 4853 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4864 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4854 } | 4865 } |
| OLD | NEW |