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

Side by Side Diff: services/url_response_disk_cache/url_response_disk_cache_apptest.cc

Issue 1276073004: Offline By Default (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rename MoveIntoDir Created 5 years, 3 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
OLDNEW
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
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, UpdateAndGet) {
ppi 2015/09/08 15:46:26 Should we have some tests for the new methods too?
qsr 2015/09/11 15:47:57 It is extremely hard to do. Update is completely a
55 URLResponsePtr url_response = mojo::URLResponse::New(); 55 URLResponsePtr url_response = mojo::URLResponse::New();
56 url_response->url = "http://www.example.com/1"; 56 url_response->url = "http://www.example.com/1";
57 url_response->headers.push_back(RandomEtagHeader()); 57 url_response->headers.push_back(RandomEtagHeader());
58 DataPipe pipe; 58 DataPipe pipe;
59 std::string content = base::RandBytesAsString(32); 59 std::string content = base::RandBytesAsString(32);
60 uint32_t num_bytes = content.size(); 60 uint32_t num_bytes = content.size();
61 ASSERT_EQ(MOJO_RESULT_OK, 61 ASSERT_EQ(MOJO_RESULT_OK,
62 WriteDataRaw(pipe.producer_handle.get(), content.c_str(), 62 WriteDataRaw(pipe.producer_handle.get(), content.c_str(),
63 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 63 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
64 ASSERT_EQ(content.size(), num_bytes); 64 ASSERT_EQ(content.size(), num_bytes);
65 pipe.producer_handle.reset(); 65 pipe.producer_handle.reset();
66 url_response->body = pipe.consumer_handle.Pass(); 66 url_response->body = pipe.consumer_handle.Pass();
67 base::FilePath file; 67 base::FilePath file;
68 base::FilePath cache_dir; 68 base::FilePath cache_dir;
69 url_response_disk_cache_->GetFile( 69 url_response_disk_cache_->UpdateAndGet(
70 url_response.Pass(), 70 url_response.Pass(),
71 [&file, &cache_dir](Array<uint8_t> received_file_path, 71 [&file, &cache_dir](Array<uint8_t> received_file_path,
72 Array<uint8_t> received_cache_dir_path) { 72 Array<uint8_t> received_cache_dir_path) {
73 file = toPath(received_file_path.Pass()); 73 file = toPath(received_file_path.Pass());
74 cache_dir = toPath(received_cache_dir_path.Pass()); 74 cache_dir = toPath(received_cache_dir_path.Pass());
75 }); 75 });
76 url_response_disk_cache_.WaitForIncomingResponse(); 76 url_response_disk_cache_.WaitForIncomingResponse();
77 ASSERT_FALSE(file.empty()); 77 ASSERT_FALSE(file.empty());
78 std::string received_content; 78 std::string received_content;
79 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); 79 ASSERT_TRUE(base::ReadFileToString(file, &received_content));
80 EXPECT_EQ(content, received_content); 80 EXPECT_EQ(content, received_content);
81 } 81 }
82 82
83 TEST_F(URLResponseDiskCacheAppTest, GetExtractedContent) { 83 TEST_F(URLResponseDiskCacheAppTest, UpdateAndGetExtracted) {
84 URLResponsePtr url_response = mojo::URLResponse::New(); 84 URLResponsePtr url_response = mojo::URLResponse::New();
85 url_response->url = "http://www.example.com/2"; 85 url_response->url = "http://www.example.com/2";
86 url_response->headers.push_back(RandomEtagHeader()); 86 url_response->headers.push_back(RandomEtagHeader());
87 DataPipe pipe; 87 DataPipe pipe;
88 std::string content = base::RandBytesAsString(32); 88 std::string content = base::RandBytesAsString(32);
89 uint32_t num_bytes = kTestData.size; 89 uint32_t num_bytes = kTestData.size;
90 ASSERT_EQ(MOJO_RESULT_OK, 90 ASSERT_EQ(MOJO_RESULT_OK,
91 WriteDataRaw(pipe.producer_handle.get(), kTestData.data, &num_bytes, 91 WriteDataRaw(pipe.producer_handle.get(), kTestData.data, &num_bytes,
92 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 92 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
93 ASSERT_EQ(kTestData.size, num_bytes); 93 ASSERT_EQ(kTestData.size, num_bytes);
94 pipe.producer_handle.reset(); 94 pipe.producer_handle.reset();
95 url_response->body = pipe.consumer_handle.Pass(); 95 url_response->body = pipe.consumer_handle.Pass();
96 base::FilePath extracted_dir; 96 base::FilePath extracted_dir;
97 base::FilePath cache_dir; 97 base::FilePath cache_dir;
98 url_response_disk_cache_->GetExtractedContent( 98 url_response_disk_cache_->UpdateAndGetExtracted(
99 url_response.Pass(), 99 url_response.Pass(),
100 [&extracted_dir, &cache_dir](Array<uint8_t> received_extracted_dir, 100 [&extracted_dir, &cache_dir](Array<uint8_t> received_extracted_dir,
101 Array<uint8_t> received_cache_dir_path) { 101 Array<uint8_t> received_cache_dir_path) {
102 extracted_dir = toPath(received_extracted_dir.Pass()); 102 extracted_dir = toPath(received_extracted_dir.Pass());
103 cache_dir = toPath(received_cache_dir_path.Pass()); 103 cache_dir = toPath(received_cache_dir_path.Pass());
104 }); 104 });
105 url_response_disk_cache_.WaitForIncomingResponse(); 105 url_response_disk_cache_.WaitForIncomingResponse();
106 ASSERT_FALSE(extracted_dir.empty()); 106 ASSERT_FALSE(extracted_dir.empty());
107 base::FilePath file1 = extracted_dir.Append("file1"); 107 base::FilePath file1 = extracted_dir.Append("file1");
108 std::string file_content; 108 std::string file_content;
(...skipping 18 matching lines...) Expand all
127 std::string content = base::RandBytesAsString(32); 127 std::string content = base::RandBytesAsString(32);
128 uint32_t num_bytes = content.size(); 128 uint32_t num_bytes = content.size();
129 ASSERT_EQ(MOJO_RESULT_OK, 129 ASSERT_EQ(MOJO_RESULT_OK,
130 WriteDataRaw(pipe1.producer_handle.get(), content.c_str(), 130 WriteDataRaw(pipe1.producer_handle.get(), content.c_str(),
131 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 131 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
132 ASSERT_EQ(content.size(), num_bytes); 132 ASSERT_EQ(content.size(), num_bytes);
133 pipe1.producer_handle.reset(); 133 pipe1.producer_handle.reset();
134 url_response->body = pipe1.consumer_handle.Pass(); 134 url_response->body = pipe1.consumer_handle.Pass();
135 base::FilePath file; 135 base::FilePath file;
136 base::FilePath cache_dir; 136 base::FilePath cache_dir;
137 url_response_disk_cache_->GetFile( 137 url_response_disk_cache_->UpdateAndGet(
138 url_response.Pass(), 138 url_response.Pass(),
139 [&file, &cache_dir](Array<uint8_t> received_file_path, 139 [&file, &cache_dir](Array<uint8_t> received_file_path,
140 Array<uint8_t> received_cache_dir_path) { 140 Array<uint8_t> received_cache_dir_path) {
141 file = toPath(received_file_path.Pass()); 141 file = toPath(received_file_path.Pass());
142 cache_dir = toPath(received_cache_dir_path.Pass()); 142 cache_dir = toPath(received_cache_dir_path.Pass());
143 }); 143 });
144 url_response_disk_cache_.WaitForIncomingResponse(); 144 url_response_disk_cache_.WaitForIncomingResponse();
145 ASSERT_FALSE(file.empty()); 145 ASSERT_FALSE(file.empty());
146 std::string received_content; 146 std::string received_content;
147 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); 147 ASSERT_TRUE(base::ReadFileToString(file, &received_content));
(...skipping 16 matching lines...) Expand all
164 } 164 }
165 DataPipe pipe2; 165 DataPipe pipe2;
166 std::string new_content = base::RandBytesAsString(32); 166 std::string new_content = base::RandBytesAsString(32);
167 num_bytes = new_content.size(); 167 num_bytes = new_content.size();
168 ASSERT_EQ(MOJO_RESULT_OK, 168 ASSERT_EQ(MOJO_RESULT_OK,
169 WriteDataRaw(pipe2.producer_handle.get(), new_content.c_str(), 169 WriteDataRaw(pipe2.producer_handle.get(), new_content.c_str(),
170 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 170 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
171 ASSERT_EQ(new_content.size(), num_bytes); 171 ASSERT_EQ(new_content.size(), num_bytes);
172 pipe2.producer_handle.reset(); 172 pipe2.producer_handle.reset();
173 url_response->body = pipe2.consumer_handle.Pass(); 173 url_response->body = pipe2.consumer_handle.Pass();
174 url_response_disk_cache_->GetFile( 174 url_response_disk_cache_->UpdateAndGet(
175 url_response.Pass(), 175 url_response.Pass(),
176 [&file, &cache_dir](Array<uint8_t> received_file_path, 176 [&file, &cache_dir](Array<uint8_t> received_file_path,
177 Array<uint8_t> received_cache_dir_path) { 177 Array<uint8_t> received_cache_dir_path) {
178 file = toPath(received_file_path.Pass()); 178 file = toPath(received_file_path.Pass());
179 cache_dir = toPath(received_cache_dir_path.Pass()); 179 cache_dir = toPath(received_cache_dir_path.Pass());
180 }); 180 });
181 url_response_disk_cache_.WaitForIncomingResponse(); 181 url_response_disk_cache_.WaitForIncomingResponse();
182 ASSERT_FALSE(file.empty()); 182 ASSERT_FALSE(file.empty());
183 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); 183 ASSERT_TRUE(base::ReadFileToString(file, &received_content));
184 EXPECT_EQ(content, received_content); 184 EXPECT_EQ(content, received_content);
(...skipping 10 matching lines...) Expand all
195 url_response->headers.push_back(RandomEtagHeader()); 195 url_response->headers.push_back(RandomEtagHeader());
196 DataPipe pipe3; 196 DataPipe pipe3;
197 new_content = base::RandBytesAsString(32); 197 new_content = base::RandBytesAsString(32);
198 num_bytes = new_content.size(); 198 num_bytes = new_content.size();
199 ASSERT_EQ(MOJO_RESULT_OK, 199 ASSERT_EQ(MOJO_RESULT_OK,
200 WriteDataRaw(pipe3.producer_handle.get(), new_content.c_str(), 200 WriteDataRaw(pipe3.producer_handle.get(), new_content.c_str(),
201 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 201 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
202 ASSERT_EQ(new_content.size(), num_bytes); 202 ASSERT_EQ(new_content.size(), num_bytes);
203 pipe3.producer_handle.reset(); 203 pipe3.producer_handle.reset();
204 url_response->body = pipe3.consumer_handle.Pass(); 204 url_response->body = pipe3.consumer_handle.Pass();
205 url_response_disk_cache_->GetFile( 205 url_response_disk_cache_->UpdateAndGet(
206 url_response.Pass(), 206 url_response.Pass(),
207 [&file, &cache_dir](Array<uint8_t> received_file_path, 207 [&file, &cache_dir](Array<uint8_t> received_file_path,
208 Array<uint8_t> received_cache_dir_path) { 208 Array<uint8_t> received_cache_dir_path) {
209 file = toPath(received_file_path.Pass()); 209 file = toPath(received_file_path.Pass());
210 cache_dir = toPath(received_cache_dir_path.Pass()); 210 cache_dir = toPath(received_cache_dir_path.Pass());
211 }); 211 });
212 url_response_disk_cache_.WaitForIncomingResponse(); 212 url_response_disk_cache_.WaitForIncomingResponse();
213 ASSERT_FALSE(file.empty()); 213 ASSERT_FALSE(file.empty());
214 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); 214 ASSERT_TRUE(base::ReadFileToString(file, &received_content));
215 EXPECT_EQ(new_content, received_content); 215 EXPECT_EQ(new_content, received_content);
216 saved_file = cache_dir.Append("file"); 216 saved_file = cache_dir.Append("file");
217 EXPECT_FALSE(base::PathExists(saved_file)); 217 EXPECT_FALSE(base::PathExists(saved_file));
218 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), 218 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(),
219 cached_content.size())); 219 cached_content.size()));
220 220
221 // Request using a response without an etag header. Check that the new 221 // Request using a response without an etag header. Check that the new
222 // content is returned, and the cached files is deleted. 222 // content is returned, and the cached files is deleted.
223 url_response = mojo::URLResponse::New(); 223 url_response = mojo::URLResponse::New();
224 url_response->url = "http://www.example.com/3"; 224 url_response->url = "http://www.example.com/3";
225 DataPipe pipe4; 225 DataPipe pipe4;
226 new_content = base::RandBytesAsString(32); 226 new_content = base::RandBytesAsString(32);
227 num_bytes = new_content.size(); 227 num_bytes = new_content.size();
228 ASSERT_EQ(MOJO_RESULT_OK, 228 ASSERT_EQ(MOJO_RESULT_OK,
229 WriteDataRaw(pipe4.producer_handle.get(), new_content.c_str(), 229 WriteDataRaw(pipe4.producer_handle.get(), new_content.c_str(),
230 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 230 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
231 ASSERT_EQ(new_content.size(), num_bytes); 231 ASSERT_EQ(new_content.size(), num_bytes);
232 pipe4.producer_handle.reset(); 232 pipe4.producer_handle.reset();
233 url_response->body = pipe4.consumer_handle.Pass(); 233 url_response->body = pipe4.consumer_handle.Pass();
234 url_response_disk_cache_->GetFile( 234 url_response_disk_cache_->UpdateAndGet(
235 url_response.Pass(), 235 url_response.Pass(),
236 [&file, &cache_dir](Array<uint8_t> received_file_path, 236 [&file, &cache_dir](Array<uint8_t> received_file_path,
237 Array<uint8_t> received_cache_dir_path) { 237 Array<uint8_t> received_cache_dir_path) {
238 file = toPath(received_file_path.Pass()); 238 file = toPath(received_file_path.Pass());
239 cache_dir = toPath(received_cache_dir_path.Pass()); 239 cache_dir = toPath(received_cache_dir_path.Pass());
240 }); 240 });
241 url_response_disk_cache_.WaitForIncomingResponse(); 241 url_response_disk_cache_.WaitForIncomingResponse();
242 ASSERT_FALSE(file.empty()); 242 ASSERT_FALSE(file.empty());
243 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); 243 ASSERT_TRUE(base::ReadFileToString(file, &received_content));
244 EXPECT_EQ(new_content, received_content); 244 EXPECT_EQ(new_content, received_content);
245 saved_file = cache_dir.Append("file"); 245 saved_file = cache_dir.Append("file");
246 EXPECT_FALSE(base::PathExists(saved_file)); 246 EXPECT_FALSE(base::PathExists(saved_file));
247 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), 247 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(),
248 cached_content.size())); 248 cached_content.size()));
249 249
250 // Request using a response with an etag header while the cache version 250 // 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 251 // doesn't have one. Check that the new content is returned, and the cached
252 // files is deleted. 252 // files is deleted.
253 url_response = mojo::URLResponse::New(); 253 url_response = mojo::URLResponse::New();
254 url_response->url = "http://www.example.com/3"; 254 url_response->url = "http://www.example.com/3";
255 DataPipe pipe5; 255 DataPipe pipe5;
256 new_content = base::RandBytesAsString(32); 256 new_content = base::RandBytesAsString(32);
257 num_bytes = new_content.size(); 257 num_bytes = new_content.size();
258 ASSERT_EQ(MOJO_RESULT_OK, 258 ASSERT_EQ(MOJO_RESULT_OK,
259 WriteDataRaw(pipe5.producer_handle.get(), new_content.c_str(), 259 WriteDataRaw(pipe5.producer_handle.get(), new_content.c_str(),
260 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 260 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
261 ASSERT_EQ(new_content.size(), num_bytes); 261 ASSERT_EQ(new_content.size(), num_bytes);
262 pipe5.producer_handle.reset(); 262 pipe5.producer_handle.reset();
263 url_response->body = pipe5.consumer_handle.Pass(); 263 url_response->body = pipe5.consumer_handle.Pass();
264 url_response_disk_cache_->GetFile( 264 url_response_disk_cache_->UpdateAndGet(
265 url_response.Pass(), 265 url_response.Pass(),
266 [&file, &cache_dir](Array<uint8_t> received_file_path, 266 [&file, &cache_dir](Array<uint8_t> received_file_path,
267 Array<uint8_t> received_cache_dir_path) { 267 Array<uint8_t> received_cache_dir_path) {
268 file = toPath(received_file_path.Pass()); 268 file = toPath(received_file_path.Pass());
269 cache_dir = toPath(received_cache_dir_path.Pass()); 269 cache_dir = toPath(received_cache_dir_path.Pass());
270 }); 270 });
271 url_response_disk_cache_.WaitForIncomingResponse(); 271 url_response_disk_cache_.WaitForIncomingResponse();
272 ASSERT_FALSE(file.empty()); 272 ASSERT_FALSE(file.empty());
273 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); 273 ASSERT_TRUE(base::ReadFileToString(file, &received_content));
274 EXPECT_EQ(new_content, received_content); 274 EXPECT_EQ(new_content, received_content);
275 saved_file = cache_dir.Append("file"); 275 saved_file = cache_dir.Append("file");
276 EXPECT_FALSE(base::PathExists(saved_file)); 276 EXPECT_FALSE(base::PathExists(saved_file));
277 } 277 }
278 278
279 } // namespace mojo 279 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698