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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 url_response->body = pipe.consumer_handle.Pass(); | 60 url_response->body = pipe.consumer_handle.Pass(); |
61 base::FilePath file; | 61 base::FilePath file; |
62 base::FilePath cache_dir; | 62 base::FilePath cache_dir; |
63 url_response_disk_cache_->GetFile( | 63 url_response_disk_cache_->GetFile( |
64 url_response.Pass(), | 64 url_response.Pass(), |
65 [&file, &cache_dir](Array<uint8_t> received_file_path, | 65 [&file, &cache_dir](Array<uint8_t> received_file_path, |
66 Array<uint8_t> received_cache_dir_path) { | 66 Array<uint8_t> received_cache_dir_path) { |
67 file = toPath(received_file_path.Pass()); | 67 file = toPath(received_file_path.Pass()); |
68 cache_dir = toPath(received_cache_dir_path.Pass()); | 68 cache_dir = toPath(received_cache_dir_path.Pass()); |
69 }); | 69 }); |
70 url_response_disk_cache_.WaitForIncomingMethodCall(); | 70 url_response_disk_cache_.WaitForIncomingResponse(); |
71 ASSERT_FALSE(file.empty()); | 71 ASSERT_FALSE(file.empty()); |
72 std::string received_content; | 72 std::string received_content; |
73 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 73 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
74 EXPECT_EQ(content, received_content); | 74 EXPECT_EQ(content, received_content); |
75 } | 75 } |
76 | 76 |
77 TEST_F(URLResponseDiskCacheAppTest, GetExtractedContent) { | 77 TEST_F(URLResponseDiskCacheAppTest, GetExtractedContent) { |
78 URLResponsePtr url_response = mojo::URLResponse::New(); | 78 URLResponsePtr url_response = mojo::URLResponse::New(); |
79 url_response->url = "http://www.example.com/2"; | 79 url_response->url = "http://www.example.com/2"; |
80 url_response->headers = Array<String>(1); | 80 url_response->headers = Array<String>(1); |
81 url_response->headers[0] = base::StringPrintf("ETag: %f", base::RandDouble()); | 81 url_response->headers[0] = base::StringPrintf("ETag: %f", base::RandDouble()); |
82 DataPipe pipe; | 82 DataPipe pipe; |
83 std::string content = base::RandBytesAsString(32); | 83 std::string content = base::RandBytesAsString(32); |
84 uint32_t num_bytes = kTestData.size; | 84 uint32_t num_bytes = kTestData.size; |
85 ASSERT_EQ(MOJO_RESULT_OK, | 85 ASSERT_EQ(MOJO_RESULT_OK, |
86 WriteDataRaw(pipe.producer_handle.get(), kTestData.data, &num_bytes, | 86 WriteDataRaw(pipe.producer_handle.get(), kTestData.data, &num_bytes, |
87 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 87 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
88 ASSERT_EQ(kTestData.size, num_bytes); | 88 ASSERT_EQ(kTestData.size, num_bytes); |
89 pipe.producer_handle.reset(); | 89 pipe.producer_handle.reset(); |
90 url_response->body = pipe.consumer_handle.Pass(); | 90 url_response->body = pipe.consumer_handle.Pass(); |
91 base::FilePath extracted_dir; | 91 base::FilePath extracted_dir; |
92 base::FilePath cache_dir; | 92 base::FilePath cache_dir; |
93 url_response_disk_cache_->GetExtractedContent( | 93 url_response_disk_cache_->GetExtractedContent( |
94 url_response.Pass(), | 94 url_response.Pass(), |
95 [&extracted_dir, &cache_dir](Array<uint8_t> received_extracted_dir, | 95 [&extracted_dir, &cache_dir](Array<uint8_t> received_extracted_dir, |
96 Array<uint8_t> received_cache_dir_path) { | 96 Array<uint8_t> received_cache_dir_path) { |
97 extracted_dir = toPath(received_extracted_dir.Pass()); | 97 extracted_dir = toPath(received_extracted_dir.Pass()); |
98 cache_dir = toPath(received_cache_dir_path.Pass()); | 98 cache_dir = toPath(received_cache_dir_path.Pass()); |
99 }); | 99 }); |
100 url_response_disk_cache_.WaitForIncomingMethodCall(); | 100 url_response_disk_cache_.WaitForIncomingResponse(); |
101 ASSERT_FALSE(extracted_dir.empty()); | 101 ASSERT_FALSE(extracted_dir.empty()); |
102 base::FilePath file1 = extracted_dir.Append("file1"); | 102 base::FilePath file1 = extracted_dir.Append("file1"); |
103 std::string file_content; | 103 std::string file_content; |
104 ASSERT_TRUE(base::ReadFileToString(file1, &file_content)); | 104 ASSERT_TRUE(base::ReadFileToString(file1, &file_content)); |
105 EXPECT_EQ("hello\n", file_content); | 105 EXPECT_EQ("hello\n", file_content); |
106 base::FilePath file2 = extracted_dir.Append("file2"); | 106 base::FilePath file2 = extracted_dir.Append("file2"); |
107 ASSERT_TRUE(base::ReadFileToString(file2, &file_content)); | 107 ASSERT_TRUE(base::ReadFileToString(file2, &file_content)); |
108 EXPECT_EQ("world\n", file_content); | 108 EXPECT_EQ("world\n", file_content); |
109 } | 109 } |
110 | 110 |
(...skipping 14 matching lines...) Expand all Loading... |
125 url_response->body = pipe1.consumer_handle.Pass(); | 125 url_response->body = pipe1.consumer_handle.Pass(); |
126 base::FilePath file; | 126 base::FilePath file; |
127 base::FilePath cache_dir; | 127 base::FilePath cache_dir; |
128 url_response_disk_cache_->GetFile( | 128 url_response_disk_cache_->GetFile( |
129 url_response.Pass(), | 129 url_response.Pass(), |
130 [&file, &cache_dir](Array<uint8_t> received_file_path, | 130 [&file, &cache_dir](Array<uint8_t> received_file_path, |
131 Array<uint8_t> received_cache_dir_path) { | 131 Array<uint8_t> received_cache_dir_path) { |
132 file = toPath(received_file_path.Pass()); | 132 file = toPath(received_file_path.Pass()); |
133 cache_dir = toPath(received_cache_dir_path.Pass()); | 133 cache_dir = toPath(received_cache_dir_path.Pass()); |
134 }); | 134 }); |
135 url_response_disk_cache_.WaitForIncomingMethodCall(); | 135 url_response_disk_cache_.WaitForIncomingResponse(); |
136 ASSERT_FALSE(file.empty()); | 136 ASSERT_FALSE(file.empty()); |
137 std::string received_content; | 137 std::string received_content; |
138 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 138 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
139 EXPECT_EQ(content, received_content); | 139 EXPECT_EQ(content, received_content); |
140 base::FilePath saved_file = cache_dir.Append("file"); | 140 base::FilePath saved_file = cache_dir.Append("file"); |
141 EXPECT_FALSE(base::PathExists(saved_file)); | 141 EXPECT_FALSE(base::PathExists(saved_file)); |
142 std::string cached_content = base::RandBytesAsString(32); | 142 std::string cached_content = base::RandBytesAsString(32); |
143 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), | 143 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), |
144 cached_content.size())); | 144 cached_content.size())); |
145 | 145 |
(...skipping 12 matching lines...) Expand all Loading... |
158 ASSERT_EQ(new_content.size(), num_bytes); | 158 ASSERT_EQ(new_content.size(), num_bytes); |
159 pipe2.producer_handle.reset(); | 159 pipe2.producer_handle.reset(); |
160 url_response->body = pipe2.consumer_handle.Pass(); | 160 url_response->body = pipe2.consumer_handle.Pass(); |
161 url_response_disk_cache_->GetFile( | 161 url_response_disk_cache_->GetFile( |
162 url_response.Pass(), | 162 url_response.Pass(), |
163 [&file, &cache_dir](Array<uint8_t> received_file_path, | 163 [&file, &cache_dir](Array<uint8_t> received_file_path, |
164 Array<uint8_t> received_cache_dir_path) { | 164 Array<uint8_t> received_cache_dir_path) { |
165 file = toPath(received_file_path.Pass()); | 165 file = toPath(received_file_path.Pass()); |
166 cache_dir = toPath(received_cache_dir_path.Pass()); | 166 cache_dir = toPath(received_cache_dir_path.Pass()); |
167 }); | 167 }); |
168 url_response_disk_cache_.WaitForIncomingMethodCall(); | 168 url_response_disk_cache_.WaitForIncomingResponse(); |
169 ASSERT_FALSE(file.empty()); | 169 ASSERT_FALSE(file.empty()); |
170 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 170 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
171 EXPECT_EQ(content, received_content); | 171 EXPECT_EQ(content, received_content); |
172 saved_file = cache_dir.Append("file"); | 172 saved_file = cache_dir.Append("file"); |
173 EXPECT_TRUE(base::PathExists(saved_file)); | 173 EXPECT_TRUE(base::PathExists(saved_file)); |
174 std::string received_cached_content; | 174 std::string received_cached_content; |
175 ASSERT_TRUE(base::ReadFileToString(saved_file, &received_cached_content)); | 175 ASSERT_TRUE(base::ReadFileToString(saved_file, &received_cached_content)); |
176 EXPECT_EQ(cached_content, received_cached_content); | 176 EXPECT_EQ(cached_content, received_cached_content); |
177 | 177 |
178 // Request using a response for the same URL, with the a different etag. Check | 178 // Request using a response for the same URL, with the a different etag. Check |
(...skipping 11 matching lines...) Expand all Loading... |
190 ASSERT_EQ(new_content.size(), num_bytes); | 190 ASSERT_EQ(new_content.size(), num_bytes); |
191 pipe3.producer_handle.reset(); | 191 pipe3.producer_handle.reset(); |
192 url_response->body = pipe3.consumer_handle.Pass(); | 192 url_response->body = pipe3.consumer_handle.Pass(); |
193 url_response_disk_cache_->GetFile( | 193 url_response_disk_cache_->GetFile( |
194 url_response.Pass(), | 194 url_response.Pass(), |
195 [&file, &cache_dir](Array<uint8_t> received_file_path, | 195 [&file, &cache_dir](Array<uint8_t> received_file_path, |
196 Array<uint8_t> received_cache_dir_path) { | 196 Array<uint8_t> received_cache_dir_path) { |
197 file = toPath(received_file_path.Pass()); | 197 file = toPath(received_file_path.Pass()); |
198 cache_dir = toPath(received_cache_dir_path.Pass()); | 198 cache_dir = toPath(received_cache_dir_path.Pass()); |
199 }); | 199 }); |
200 url_response_disk_cache_.WaitForIncomingMethodCall(); | 200 url_response_disk_cache_.WaitForIncomingResponse(); |
201 ASSERT_FALSE(file.empty()); | 201 ASSERT_FALSE(file.empty()); |
202 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 202 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
203 EXPECT_EQ(new_content, received_content); | 203 EXPECT_EQ(new_content, received_content); |
204 saved_file = cache_dir.Append("file"); | 204 saved_file = cache_dir.Append("file"); |
205 EXPECT_FALSE(base::PathExists(saved_file)); | 205 EXPECT_FALSE(base::PathExists(saved_file)); |
206 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), | 206 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), |
207 cached_content.size())); | 207 cached_content.size())); |
208 | 208 |
209 // Request using a response without an etag header. Check that the new | 209 // Request using a response without an etag header. Check that the new |
210 // content is returned, and the cached files is deleted. | 210 // content is returned, and the cached files is deleted. |
211 url_response = mojo::URLResponse::New(); | 211 url_response = mojo::URLResponse::New(); |
212 url_response->url = "http://www.example.com/3"; | 212 url_response->url = "http://www.example.com/3"; |
213 DataPipe pipe4; | 213 DataPipe pipe4; |
214 new_content = base::RandBytesAsString(32); | 214 new_content = base::RandBytesAsString(32); |
215 num_bytes = new_content.size(); | 215 num_bytes = new_content.size(); |
216 ASSERT_EQ(MOJO_RESULT_OK, | 216 ASSERT_EQ(MOJO_RESULT_OK, |
217 WriteDataRaw(pipe4.producer_handle.get(), new_content.c_str(), | 217 WriteDataRaw(pipe4.producer_handle.get(), new_content.c_str(), |
218 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 218 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
219 ASSERT_EQ(new_content.size(), num_bytes); | 219 ASSERT_EQ(new_content.size(), num_bytes); |
220 pipe4.producer_handle.reset(); | 220 pipe4.producer_handle.reset(); |
221 url_response->body = pipe4.consumer_handle.Pass(); | 221 url_response->body = pipe4.consumer_handle.Pass(); |
222 url_response_disk_cache_->GetFile( | 222 url_response_disk_cache_->GetFile( |
223 url_response.Pass(), | 223 url_response.Pass(), |
224 [&file, &cache_dir](Array<uint8_t> received_file_path, | 224 [&file, &cache_dir](Array<uint8_t> received_file_path, |
225 Array<uint8_t> received_cache_dir_path) { | 225 Array<uint8_t> received_cache_dir_path) { |
226 file = toPath(received_file_path.Pass()); | 226 file = toPath(received_file_path.Pass()); |
227 cache_dir = toPath(received_cache_dir_path.Pass()); | 227 cache_dir = toPath(received_cache_dir_path.Pass()); |
228 }); | 228 }); |
229 url_response_disk_cache_.WaitForIncomingMethodCall(); | 229 url_response_disk_cache_.WaitForIncomingResponse(); |
230 ASSERT_FALSE(file.empty()); | 230 ASSERT_FALSE(file.empty()); |
231 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 231 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
232 EXPECT_EQ(new_content, received_content); | 232 EXPECT_EQ(new_content, received_content); |
233 saved_file = cache_dir.Append("file"); | 233 saved_file = cache_dir.Append("file"); |
234 EXPECT_FALSE(base::PathExists(saved_file)); | 234 EXPECT_FALSE(base::PathExists(saved_file)); |
235 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), | 235 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), |
236 cached_content.size())); | 236 cached_content.size())); |
237 | 237 |
238 // Request using a response with an etag header while the cache version | 238 // Request using a response with an etag header while the cache version |
239 // doesn't have one. Check that the new content is returned, and the cached | 239 // doesn't have one. Check that the new content is returned, and the cached |
240 // files is deleted. | 240 // files is deleted. |
241 url_response = mojo::URLResponse::New(); | 241 url_response = mojo::URLResponse::New(); |
242 url_response->url = "http://www.example.com/3"; | 242 url_response->url = "http://www.example.com/3"; |
243 DataPipe pipe5; | 243 DataPipe pipe5; |
244 new_content = base::RandBytesAsString(32); | 244 new_content = base::RandBytesAsString(32); |
245 num_bytes = new_content.size(); | 245 num_bytes = new_content.size(); |
246 ASSERT_EQ(MOJO_RESULT_OK, | 246 ASSERT_EQ(MOJO_RESULT_OK, |
247 WriteDataRaw(pipe5.producer_handle.get(), new_content.c_str(), | 247 WriteDataRaw(pipe5.producer_handle.get(), new_content.c_str(), |
248 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); | 248 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
249 ASSERT_EQ(new_content.size(), num_bytes); | 249 ASSERT_EQ(new_content.size(), num_bytes); |
250 pipe5.producer_handle.reset(); | 250 pipe5.producer_handle.reset(); |
251 url_response->body = pipe5.consumer_handle.Pass(); | 251 url_response->body = pipe5.consumer_handle.Pass(); |
252 url_response_disk_cache_->GetFile( | 252 url_response_disk_cache_->GetFile( |
253 url_response.Pass(), | 253 url_response.Pass(), |
254 [&file, &cache_dir](Array<uint8_t> received_file_path, | 254 [&file, &cache_dir](Array<uint8_t> received_file_path, |
255 Array<uint8_t> received_cache_dir_path) { | 255 Array<uint8_t> received_cache_dir_path) { |
256 file = toPath(received_file_path.Pass()); | 256 file = toPath(received_file_path.Pass()); |
257 cache_dir = toPath(received_cache_dir_path.Pass()); | 257 cache_dir = toPath(received_cache_dir_path.Pass()); |
258 }); | 258 }); |
259 url_response_disk_cache_.WaitForIncomingMethodCall(); | 259 url_response_disk_cache_.WaitForIncomingResponse(); |
260 ASSERT_FALSE(file.empty()); | 260 ASSERT_FALSE(file.empty()); |
261 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); | 261 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
262 EXPECT_EQ(new_content, received_content); | 262 EXPECT_EQ(new_content, received_content); |
263 saved_file = cache_dir.Append("file"); | 263 saved_file = cache_dir.Append("file"); |
264 EXPECT_FALSE(base::PathExists(saved_file)); | 264 EXPECT_FALSE(base::PathExists(saved_file)); |
265 } | 265 } |
266 | 266 |
267 } // namespace mojo | 267 } // namespace mojo |
OLD | NEW |