OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
6 #include "base/rand_util.h" | 6 #include "base/rand_util.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "mojo/public/cpp/application/application_impl.h" | 9 #include "mojo/public/cpp/application/application_impl.h" |
10 #include "mojo/public/cpp/application/application_test_base.h" | 10 #include "mojo/public/cpp/application/application_test_base.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 | 44 |
45 HttpHeaderPtr RandomEtagHeader() { | 45 HttpHeaderPtr RandomEtagHeader() { |
46 auto etag_header = HttpHeader::New(); | 46 auto etag_header = HttpHeader::New(); |
47 etag_header->name = "ETag"; | 47 etag_header->name = "ETag"; |
48 etag_header->value = base::StringPrintf("%f", base::RandDouble()); | 48 etag_header->value = base::StringPrintf("%f", base::RandDouble()); |
49 return etag_header; | 49 return etag_header; |
50 } | 50 } |
51 | 51 |
52 } // namespace | 52 } // namespace |
53 | 53 |
54 TEST_F(URLResponseDiskCacheAppTest, GetFile) { | 54 TEST_F(URLResponseDiskCacheAppTest, BaseCache) { |
| 55 const std::string url = "http://www.example.com/1"; |
| 56 |
| 57 url_response_disk_cache_->Get( |
| 58 url, [](URLResponsePtr url_response, Array<uint8_t> received_file_path, |
| 59 Array<uint8_t> received_cache_dir_path) { |
| 60 EXPECT_FALSE(url_response); |
| 61 }); |
| 62 url_response_disk_cache_.WaitForIncomingResponse(); |
| 63 |
55 URLResponsePtr url_response = mojo::URLResponse::New(); | 64 URLResponsePtr url_response = mojo::URLResponse::New(); |
56 url_response->url = "http://www.example.com/1"; | 65 url_response->url = "http://www.example.com/1"; |
57 url_response->headers.push_back(RandomEtagHeader()); | 66 url_response->headers.push_back(RandomEtagHeader()); |
| 67 DataPipe pipe; |
| 68 std::string content = base::RandBytesAsString(32); |
| 69 uint32_t num_bytes = content.size(); |
| 70 ASSERT_EQ(MOJO_RESULT_OK, |
| 71 WriteDataRaw(pipe.producer_handle.get(), content.c_str(), |
| 72 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
| 73 ASSERT_EQ(content.size(), num_bytes); |
| 74 pipe.producer_handle.reset(); |
| 75 url_response->body = pipe.consumer_handle.Pass(); |
| 76 |
| 77 url_response_disk_cache_->Update(url_response.Pass()); |
| 78 |
| 79 base::FilePath file; |
| 80 base::FilePath cache_dir; |
| 81 |
| 82 // Wait up to 1 second for the cache to be updated. |
| 83 base::Time start = base::Time::Now(); |
| 84 while (file.empty() && |
| 85 ((base::Time::Now() - start) < base::TimeDelta::FromSeconds(1))) { |
| 86 url_response_disk_cache_->Get( |
| 87 url, [&url_response, &file, &cache_dir]( |
| 88 URLResponsePtr response, Array<uint8_t> received_file_path, |
| 89 Array<uint8_t> received_cache_dir_path) { |
| 90 url_response = response.Pass(); |
| 91 file = toPath(received_file_path.Pass()); |
| 92 cache_dir = toPath(received_cache_dir_path.Pass()); |
| 93 }); |
| 94 url_response_disk_cache_.WaitForIncomingResponse(); |
| 95 } |
| 96 |
| 97 EXPECT_TRUE(url_response); |
| 98 EXPECT_FALSE(file.empty()); |
| 99 EXPECT_EQ(url, url_response->url); |
| 100 std::string received_content; |
| 101 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
| 102 EXPECT_EQ(content, received_content); |
| 103 } |
| 104 |
| 105 TEST_F(URLResponseDiskCacheAppTest, UpdateAndGet) { |
| 106 URLResponsePtr url_response = mojo::URLResponse::New(); |
| 107 url_response->url = "http://www.example.com/1"; |
| 108 url_response->headers.push_back(RandomEtagHeader()); |
58 DataPipe pipe; | 109 DataPipe pipe; |
59 std::string content = base::RandBytesAsString(32); | 110 std::string content = base::RandBytesAsString(32); |
60 uint32_t num_bytes = content.size(); | 111 uint32_t num_bytes = content.size(); |
61 ASSERT_EQ(MOJO_RESULT_OK, | 112 ASSERT_EQ(MOJO_RESULT_OK, |
62 WriteDataRaw(pipe.producer_handle.get(), content.c_str(), | 113 WriteDataRaw(pipe.producer_handle.get(), content.c_str(), |
63 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 114 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
64 ASSERT_EQ(content.size(), num_bytes); | 115 ASSERT_EQ(content.size(), num_bytes); |
65 pipe.producer_handle.reset(); | 116 pipe.producer_handle.reset(); |
66 url_response->body = pipe.consumer_handle.Pass(); | 117 url_response->body = pipe.consumer_handle.Pass(); |
67 base::FilePath file; | 118 base::FilePath file; |
68 base::FilePath cache_dir; | 119 base::FilePath cache_dir; |
69 url_response_disk_cache_->GetFile( | 120 url_response_disk_cache_->UpdateAndGet( |
70 url_response.Pass(), | 121 url_response.Pass(), |
71 [&file, &cache_dir](Array<uint8_t> received_file_path, | 122 [&file, &cache_dir](Array<uint8_t> received_file_path, |
72 Array<uint8_t> received_cache_dir_path) { | 123 Array<uint8_t> received_cache_dir_path) { |
73 file = toPath(received_file_path.Pass()); | 124 file = toPath(received_file_path.Pass()); |
74 cache_dir = toPath(received_cache_dir_path.Pass()); | 125 cache_dir = toPath(received_cache_dir_path.Pass()); |
75 }); | 126 }); |
76 url_response_disk_cache_.WaitForIncomingResponse(); | 127 url_response_disk_cache_.WaitForIncomingResponse(); |
77 ASSERT_FALSE(file.empty()); | 128 ASSERT_FALSE(file.empty()); |
78 std::string received_content; | 129 std::string received_content; |
79 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 130 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
80 EXPECT_EQ(content, received_content); | 131 EXPECT_EQ(content, received_content); |
81 } | 132 } |
82 | 133 |
83 TEST_F(URLResponseDiskCacheAppTest, GetExtractedContent) { | 134 TEST_F(URLResponseDiskCacheAppTest, UpdateAndGetExtracted) { |
84 URLResponsePtr url_response = mojo::URLResponse::New(); | 135 URLResponsePtr url_response = mojo::URLResponse::New(); |
85 url_response->url = "http://www.example.com/2"; | 136 url_response->url = "http://www.example.com/2"; |
86 url_response->headers.push_back(RandomEtagHeader()); | 137 url_response->headers.push_back(RandomEtagHeader()); |
87 DataPipe pipe; | 138 DataPipe pipe; |
88 std::string content = base::RandBytesAsString(32); | 139 std::string content = base::RandBytesAsString(32); |
89 uint32_t num_bytes = kTestData.size; | 140 uint32_t num_bytes = kTestData.size; |
90 ASSERT_EQ(MOJO_RESULT_OK, | 141 ASSERT_EQ(MOJO_RESULT_OK, |
91 WriteDataRaw(pipe.producer_handle.get(), kTestData.data, &num_bytes, | 142 WriteDataRaw(pipe.producer_handle.get(), kTestData.data, &num_bytes, |
92 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 143 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
93 ASSERT_EQ(kTestData.size, num_bytes); | 144 ASSERT_EQ(kTestData.size, num_bytes); |
94 pipe.producer_handle.reset(); | 145 pipe.producer_handle.reset(); |
95 url_response->body = pipe.consumer_handle.Pass(); | 146 url_response->body = pipe.consumer_handle.Pass(); |
96 base::FilePath extracted_dir; | 147 base::FilePath extracted_dir; |
97 base::FilePath cache_dir; | 148 base::FilePath cache_dir; |
98 url_response_disk_cache_->GetExtractedContent( | 149 url_response_disk_cache_->UpdateAndGetExtracted( |
99 url_response.Pass(), | 150 url_response.Pass(), |
100 [&extracted_dir, &cache_dir](Array<uint8_t> received_extracted_dir, | 151 [&extracted_dir, &cache_dir](Array<uint8_t> received_extracted_dir, |
101 Array<uint8_t> received_cache_dir_path) { | 152 Array<uint8_t> received_cache_dir_path) { |
102 extracted_dir = toPath(received_extracted_dir.Pass()); | 153 extracted_dir = toPath(received_extracted_dir.Pass()); |
103 cache_dir = toPath(received_cache_dir_path.Pass()); | 154 cache_dir = toPath(received_cache_dir_path.Pass()); |
104 }); | 155 }); |
105 url_response_disk_cache_.WaitForIncomingResponse(); | 156 url_response_disk_cache_.WaitForIncomingResponse(); |
106 ASSERT_FALSE(extracted_dir.empty()); | 157 ASSERT_FALSE(extracted_dir.empty()); |
107 base::FilePath file1 = extracted_dir.Append("file1"); | 158 base::FilePath file1 = extracted_dir.Append("file1"); |
108 std::string file_content; | 159 std::string file_content; |
(...skipping 18 matching lines...) Expand all Loading... |
127 std::string content = base::RandBytesAsString(32); | 178 std::string content = base::RandBytesAsString(32); |
128 uint32_t num_bytes = content.size(); | 179 uint32_t num_bytes = content.size(); |
129 ASSERT_EQ(MOJO_RESULT_OK, | 180 ASSERT_EQ(MOJO_RESULT_OK, |
130 WriteDataRaw(pipe1.producer_handle.get(), content.c_str(), | 181 WriteDataRaw(pipe1.producer_handle.get(), content.c_str(), |
131 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 182 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
132 ASSERT_EQ(content.size(), num_bytes); | 183 ASSERT_EQ(content.size(), num_bytes); |
133 pipe1.producer_handle.reset(); | 184 pipe1.producer_handle.reset(); |
134 url_response->body = pipe1.consumer_handle.Pass(); | 185 url_response->body = pipe1.consumer_handle.Pass(); |
135 base::FilePath file; | 186 base::FilePath file; |
136 base::FilePath cache_dir; | 187 base::FilePath cache_dir; |
137 url_response_disk_cache_->GetFile( | 188 url_response_disk_cache_->UpdateAndGet( |
138 url_response.Pass(), | 189 url_response.Pass(), |
139 [&file, &cache_dir](Array<uint8_t> received_file_path, | 190 [&file, &cache_dir](Array<uint8_t> received_file_path, |
140 Array<uint8_t> received_cache_dir_path) { | 191 Array<uint8_t> received_cache_dir_path) { |
141 file = toPath(received_file_path.Pass()); | 192 file = toPath(received_file_path.Pass()); |
142 cache_dir = toPath(received_cache_dir_path.Pass()); | 193 cache_dir = toPath(received_cache_dir_path.Pass()); |
143 }); | 194 }); |
144 url_response_disk_cache_.WaitForIncomingResponse(); | 195 url_response_disk_cache_.WaitForIncomingResponse(); |
145 ASSERT_FALSE(file.empty()); | 196 ASSERT_FALSE(file.empty()); |
146 std::string received_content; | 197 std::string received_content; |
147 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 198 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
(...skipping 16 matching lines...) Expand all Loading... |
164 } | 215 } |
165 DataPipe pipe2; | 216 DataPipe pipe2; |
166 std::string new_content = base::RandBytesAsString(32); | 217 std::string new_content = base::RandBytesAsString(32); |
167 num_bytes = new_content.size(); | 218 num_bytes = new_content.size(); |
168 ASSERT_EQ(MOJO_RESULT_OK, | 219 ASSERT_EQ(MOJO_RESULT_OK, |
169 WriteDataRaw(pipe2.producer_handle.get(), new_content.c_str(), | 220 WriteDataRaw(pipe2.producer_handle.get(), new_content.c_str(), |
170 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 221 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
171 ASSERT_EQ(new_content.size(), num_bytes); | 222 ASSERT_EQ(new_content.size(), num_bytes); |
172 pipe2.producer_handle.reset(); | 223 pipe2.producer_handle.reset(); |
173 url_response->body = pipe2.consumer_handle.Pass(); | 224 url_response->body = pipe2.consumer_handle.Pass(); |
174 url_response_disk_cache_->GetFile( | 225 url_response_disk_cache_->UpdateAndGet( |
175 url_response.Pass(), | 226 url_response.Pass(), |
176 [&file, &cache_dir](Array<uint8_t> received_file_path, | 227 [&file, &cache_dir](Array<uint8_t> received_file_path, |
177 Array<uint8_t> received_cache_dir_path) { | 228 Array<uint8_t> received_cache_dir_path) { |
178 file = toPath(received_file_path.Pass()); | 229 file = toPath(received_file_path.Pass()); |
179 cache_dir = toPath(received_cache_dir_path.Pass()); | 230 cache_dir = toPath(received_cache_dir_path.Pass()); |
180 }); | 231 }); |
181 url_response_disk_cache_.WaitForIncomingResponse(); | 232 url_response_disk_cache_.WaitForIncomingResponse(); |
182 ASSERT_FALSE(file.empty()); | 233 ASSERT_FALSE(file.empty()); |
183 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 234 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
184 EXPECT_EQ(content, received_content); | 235 EXPECT_EQ(content, received_content); |
(...skipping 10 matching lines...) Expand all Loading... |
195 url_response->headers.push_back(RandomEtagHeader()); | 246 url_response->headers.push_back(RandomEtagHeader()); |
196 DataPipe pipe3; | 247 DataPipe pipe3; |
197 new_content = base::RandBytesAsString(32); | 248 new_content = base::RandBytesAsString(32); |
198 num_bytes = new_content.size(); | 249 num_bytes = new_content.size(); |
199 ASSERT_EQ(MOJO_RESULT_OK, | 250 ASSERT_EQ(MOJO_RESULT_OK, |
200 WriteDataRaw(pipe3.producer_handle.get(), new_content.c_str(), | 251 WriteDataRaw(pipe3.producer_handle.get(), new_content.c_str(), |
201 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 252 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
202 ASSERT_EQ(new_content.size(), num_bytes); | 253 ASSERT_EQ(new_content.size(), num_bytes); |
203 pipe3.producer_handle.reset(); | 254 pipe3.producer_handle.reset(); |
204 url_response->body = pipe3.consumer_handle.Pass(); | 255 url_response->body = pipe3.consumer_handle.Pass(); |
205 url_response_disk_cache_->GetFile( | 256 url_response_disk_cache_->UpdateAndGet( |
206 url_response.Pass(), | 257 url_response.Pass(), |
207 [&file, &cache_dir](Array<uint8_t> received_file_path, | 258 [&file, &cache_dir](Array<uint8_t> received_file_path, |
208 Array<uint8_t> received_cache_dir_path) { | 259 Array<uint8_t> received_cache_dir_path) { |
209 file = toPath(received_file_path.Pass()); | 260 file = toPath(received_file_path.Pass()); |
210 cache_dir = toPath(received_cache_dir_path.Pass()); | 261 cache_dir = toPath(received_cache_dir_path.Pass()); |
211 }); | 262 }); |
212 url_response_disk_cache_.WaitForIncomingResponse(); | 263 url_response_disk_cache_.WaitForIncomingResponse(); |
213 ASSERT_FALSE(file.empty()); | 264 ASSERT_FALSE(file.empty()); |
214 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 265 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
215 EXPECT_EQ(new_content, received_content); | 266 EXPECT_EQ(new_content, received_content); |
216 saved_file = cache_dir.Append("file"); | 267 saved_file = cache_dir.Append("file"); |
217 EXPECT_FALSE(base::PathExists(saved_file)); | 268 EXPECT_FALSE(base::PathExists(saved_file)); |
218 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), | 269 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), |
219 cached_content.size())); | 270 cached_content.size())); |
220 | 271 |
221 // Request using a response without an etag header. Check that the new | 272 // Request using a response without an etag header. Check that the new |
222 // content is returned, and the cached files is deleted. | 273 // content is returned, and the cached files is deleted. |
223 url_response = mojo::URLResponse::New(); | 274 url_response = mojo::URLResponse::New(); |
224 url_response->url = "http://www.example.com/3"; | 275 url_response->url = "http://www.example.com/3"; |
225 DataPipe pipe4; | 276 DataPipe pipe4; |
226 new_content = base::RandBytesAsString(32); | 277 new_content = base::RandBytesAsString(32); |
227 num_bytes = new_content.size(); | 278 num_bytes = new_content.size(); |
228 ASSERT_EQ(MOJO_RESULT_OK, | 279 ASSERT_EQ(MOJO_RESULT_OK, |
229 WriteDataRaw(pipe4.producer_handle.get(), new_content.c_str(), | 280 WriteDataRaw(pipe4.producer_handle.get(), new_content.c_str(), |
230 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 281 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
231 ASSERT_EQ(new_content.size(), num_bytes); | 282 ASSERT_EQ(new_content.size(), num_bytes); |
232 pipe4.producer_handle.reset(); | 283 pipe4.producer_handle.reset(); |
233 url_response->body = pipe4.consumer_handle.Pass(); | 284 url_response->body = pipe4.consumer_handle.Pass(); |
234 url_response_disk_cache_->GetFile( | 285 url_response_disk_cache_->UpdateAndGet( |
235 url_response.Pass(), | 286 url_response.Pass(), |
236 [&file, &cache_dir](Array<uint8_t> received_file_path, | 287 [&file, &cache_dir](Array<uint8_t> received_file_path, |
237 Array<uint8_t> received_cache_dir_path) { | 288 Array<uint8_t> received_cache_dir_path) { |
238 file = toPath(received_file_path.Pass()); | 289 file = toPath(received_file_path.Pass()); |
239 cache_dir = toPath(received_cache_dir_path.Pass()); | 290 cache_dir = toPath(received_cache_dir_path.Pass()); |
240 }); | 291 }); |
241 url_response_disk_cache_.WaitForIncomingResponse(); | 292 url_response_disk_cache_.WaitForIncomingResponse(); |
242 ASSERT_FALSE(file.empty()); | 293 ASSERT_FALSE(file.empty()); |
243 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 294 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
244 EXPECT_EQ(new_content, received_content); | 295 EXPECT_EQ(new_content, received_content); |
245 saved_file = cache_dir.Append("file"); | 296 saved_file = cache_dir.Append("file"); |
246 EXPECT_FALSE(base::PathExists(saved_file)); | 297 EXPECT_FALSE(base::PathExists(saved_file)); |
247 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), | 298 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), |
248 cached_content.size())); | 299 cached_content.size())); |
249 | 300 |
250 // Request using a response with an etag header while the cache version | 301 // Request using a response with an etag header while the cache version |
251 // doesn't have one. Check that the new content is returned, and the cached | 302 // doesn't have one. Check that the new content is returned, and the cached |
252 // files is deleted. | 303 // files is deleted. |
253 url_response = mojo::URLResponse::New(); | 304 url_response = mojo::URLResponse::New(); |
254 url_response->url = "http://www.example.com/3"; | 305 url_response->url = "http://www.example.com/3"; |
255 DataPipe pipe5; | 306 DataPipe pipe5; |
256 new_content = base::RandBytesAsString(32); | 307 new_content = base::RandBytesAsString(32); |
257 num_bytes = new_content.size(); | 308 num_bytes = new_content.size(); |
258 ASSERT_EQ(MOJO_RESULT_OK, | 309 ASSERT_EQ(MOJO_RESULT_OK, |
259 WriteDataRaw(pipe5.producer_handle.get(), new_content.c_str(), | 310 WriteDataRaw(pipe5.producer_handle.get(), new_content.c_str(), |
260 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 311 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
261 ASSERT_EQ(new_content.size(), num_bytes); | 312 ASSERT_EQ(new_content.size(), num_bytes); |
262 pipe5.producer_handle.reset(); | 313 pipe5.producer_handle.reset(); |
263 url_response->body = pipe5.consumer_handle.Pass(); | 314 url_response->body = pipe5.consumer_handle.Pass(); |
264 url_response_disk_cache_->GetFile( | 315 url_response_disk_cache_->UpdateAndGet( |
265 url_response.Pass(), | 316 url_response.Pass(), |
266 [&file, &cache_dir](Array<uint8_t> received_file_path, | 317 [&file, &cache_dir](Array<uint8_t> received_file_path, |
267 Array<uint8_t> received_cache_dir_path) { | 318 Array<uint8_t> received_cache_dir_path) { |
268 file = toPath(received_file_path.Pass()); | 319 file = toPath(received_file_path.Pass()); |
269 cache_dir = toPath(received_cache_dir_path.Pass()); | 320 cache_dir = toPath(received_cache_dir_path.Pass()); |
270 }); | 321 }); |
271 url_response_disk_cache_.WaitForIncomingResponse(); | 322 url_response_disk_cache_.WaitForIncomingResponse(); |
272 ASSERT_FALSE(file.empty()); | 323 ASSERT_FALSE(file.empty()); |
273 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 324 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
274 EXPECT_EQ(new_content, received_content); | 325 EXPECT_EQ(new_content, received_content); |
275 saved_file = cache_dir.Append("file"); | 326 saved_file = cache_dir.Append("file"); |
276 EXPECT_FALSE(base::PathExists(saved_file)); | 327 EXPECT_FALSE(base::PathExists(saved_file)); |
277 } | 328 } |
278 | 329 |
279 } // namespace mojo | 330 } // namespace mojo |
OLD | NEW |