OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/files/file_util.h" |
| 6 #include "base/rand_util.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/application/application_test_base.h" |
| 11 #include "mojo/public/cpp/system/data_pipe.h" |
| 12 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" |
| 13 #include "mojo/services/service_cache/public/interfaces/service_cache.mojom.h" |
| 14 #include "services/service_cache/kTestData.h" |
| 15 |
| 16 namespace mojo { |
| 17 namespace service_cache { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class ServiceCacheAppTest : public mojo::test::ApplicationTestBase { |
| 22 public: |
| 23 ServiceCacheAppTest() : ApplicationTestBase() {} |
| 24 ~ServiceCacheAppTest() override {} |
| 25 |
| 26 void SetUp() override { |
| 27 mojo::test::ApplicationTestBase::SetUp(); |
| 28 application_impl()->ConnectToService("mojo:service_cache", &service_cache_); |
| 29 } |
| 30 |
| 31 protected: |
| 32 ServiceCachePtr service_cache_; |
| 33 |
| 34 DISALLOW_COPY_AND_ASSIGN(ServiceCacheAppTest); |
| 35 }; |
| 36 |
| 37 base::FilePath toPath(Array<uint8_t> path) { |
| 38 if (path.is_null()) { |
| 39 return base::FilePath(); |
| 40 } |
| 41 return base::FilePath( |
| 42 std::string(reinterpret_cast<char*>(&path.front()), path.size())); |
| 43 } |
| 44 |
| 45 } // namespace |
| 46 |
| 47 TEST_F(ServiceCacheAppTest, GetFile) { |
| 48 URLResponsePtr url_response = mojo::URLResponse::New(); |
| 49 url_response->url = "http://www.example.com/1"; |
| 50 url_response->headers = Array<String>(1); |
| 51 url_response->headers[0] = base::StringPrintf("ETag: %f", base::RandDouble()); |
| 52 DataPipe pipe; |
| 53 std::string content = base::RandBytesAsString(32); |
| 54 uint32_t num_bytes = content.size(); |
| 55 ASSERT_EQ(MOJO_RESULT_OK, |
| 56 WriteDataRaw(pipe.producer_handle.get(), content.c_str(), |
| 57 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
| 58 ASSERT_EQ(content.size(), num_bytes); |
| 59 pipe.producer_handle.reset(); |
| 60 url_response->body = pipe.consumer_handle.Pass(); |
| 61 base::FilePath file; |
| 62 base::FilePath cache_dir; |
| 63 service_cache_->GetFile( |
| 64 url_response.Pass(), |
| 65 [&file, &cache_dir](Array<uint8_t> received_file_path, |
| 66 Array<uint8_t> received_cache_dir_path) { |
| 67 file = toPath(received_file_path.Pass()); |
| 68 cache_dir = toPath(received_cache_dir_path.Pass()); |
| 69 }); |
| 70 service_cache_.WaitForIncomingMethodCall(); |
| 71 ASSERT_FALSE(file.empty()); |
| 72 std::string received_content; |
| 73 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
| 74 EXPECT_EQ(content, received_content); |
| 75 } |
| 76 |
| 77 TEST_F(ServiceCacheAppTest, GetExtractedContent) { |
| 78 URLResponsePtr url_response = mojo::URLResponse::New(); |
| 79 url_response->url = "http://www.example.com/2"; |
| 80 url_response->headers = Array<String>(1); |
| 81 url_response->headers[0] = base::StringPrintf("ETag: %f", base::RandDouble()); |
| 82 DataPipe pipe; |
| 83 std::string content = base::RandBytesAsString(32); |
| 84 uint32_t num_bytes = kTestData.size; |
| 85 ASSERT_EQ(MOJO_RESULT_OK, |
| 86 WriteDataRaw(pipe.producer_handle.get(), kTestData.data, &num_bytes, |
| 87 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
| 88 ASSERT_EQ(kTestData.size, num_bytes); |
| 89 pipe.producer_handle.reset(); |
| 90 url_response->body = pipe.consumer_handle.Pass(); |
| 91 base::FilePath extracted_dir; |
| 92 base::FilePath cache_dir; |
| 93 service_cache_->GetExtractedContent( |
| 94 url_response.Pass(), |
| 95 [&extracted_dir, &cache_dir](Array<uint8_t> received_extracted_dir, |
| 96 Array<uint8_t> received_cache_dir_path) { |
| 97 extracted_dir = toPath(received_extracted_dir.Pass()); |
| 98 cache_dir = toPath(received_cache_dir_path.Pass()); |
| 99 }); |
| 100 service_cache_.WaitForIncomingMethodCall(); |
| 101 ASSERT_FALSE(extracted_dir.empty()); |
| 102 base::FilePath file1 = extracted_dir.Append("file1"); |
| 103 std::string file_content; |
| 104 ASSERT_TRUE(base::ReadFileToString(file1, &file_content)); |
| 105 EXPECT_EQ("hello\n", file_content); |
| 106 base::FilePath file2 = extracted_dir.Append("file2"); |
| 107 ASSERT_TRUE(base::ReadFileToString(file2, &file_content)); |
| 108 EXPECT_EQ("world\n", file_content); |
| 109 } |
| 110 |
| 111 TEST_F(ServiceCacheAppTest, CacheTest) { |
| 112 URLResponsePtr url_response = mojo::URLResponse::New(); |
| 113 url_response->url = "http://www.example.com/3"; |
| 114 url_response->headers = Array<String>(1); |
| 115 std::string etag = base::StringPrintf("ETag: %f", base::RandDouble()); |
| 116 url_response->headers[0] = etag; |
| 117 DataPipe pipe1; |
| 118 std::string content = base::RandBytesAsString(32); |
| 119 uint32_t num_bytes = content.size(); |
| 120 ASSERT_EQ(MOJO_RESULT_OK, |
| 121 WriteDataRaw(pipe1.producer_handle.get(), content.c_str(), |
| 122 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
| 123 ASSERT_EQ(content.size(), num_bytes); |
| 124 pipe1.producer_handle.reset(); |
| 125 url_response->body = pipe1.consumer_handle.Pass(); |
| 126 base::FilePath file; |
| 127 base::FilePath cache_dir; |
| 128 service_cache_->GetFile( |
| 129 url_response.Pass(), |
| 130 [&file, &cache_dir](Array<uint8_t> received_file_path, |
| 131 Array<uint8_t> received_cache_dir_path) { |
| 132 file = toPath(received_file_path.Pass()); |
| 133 cache_dir = toPath(received_cache_dir_path.Pass()); |
| 134 }); |
| 135 service_cache_.WaitForIncomingMethodCall(); |
| 136 ASSERT_FALSE(file.empty()); |
| 137 std::string received_content; |
| 138 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
| 139 EXPECT_EQ(content, received_content); |
| 140 base::FilePath saved_file = cache_dir.Append("file"); |
| 141 EXPECT_FALSE(base::PathExists(saved_file)); |
| 142 std::string cached_content = base::RandBytesAsString(32); |
| 143 ASSERT_TRUE(base::WriteFile(saved_file, cached_content.data(), |
| 144 cached_content.size())); |
| 145 |
| 146 // Request using a response for the same URL, with the same etag, but a |
| 147 // different content. The cached value should be returned. |
| 148 url_response = mojo::URLResponse::New(); |
| 149 url_response->url = "http://www.example.com/3"; |
| 150 url_response->headers = Array<String>(1); |
| 151 url_response->headers[0] = etag; |
| 152 DataPipe pipe2; |
| 153 std::string new_content = base::RandBytesAsString(22); |
| 154 num_bytes = new_content.size(); |
| 155 ASSERT_EQ(MOJO_RESULT_OK, |
| 156 WriteDataRaw(pipe2.producer_handle.get(), new_content.c_str(), |
| 157 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
| 158 ASSERT_EQ(new_content.size(), num_bytes); |
| 159 pipe2.producer_handle.reset(); |
| 160 url_response->body = pipe2.consumer_handle.Pass(); |
| 161 service_cache_->GetFile( |
| 162 url_response.Pass(), |
| 163 [&file, &cache_dir](Array<uint8_t> received_file_path, |
| 164 Array<uint8_t> received_cache_dir_path) { |
| 165 file = toPath(received_file_path.Pass()); |
| 166 cache_dir = toPath(received_cache_dir_path.Pass()); |
| 167 }); |
| 168 service_cache_.WaitForIncomingMethodCall(); |
| 169 ASSERT_FALSE(file.empty()); |
| 170 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
| 171 EXPECT_EQ(content, received_content); |
| 172 saved_file = cache_dir.Append("file"); |
| 173 EXPECT_TRUE(base::PathExists(saved_file)); |
| 174 std::string received_cached_content; |
| 175 ASSERT_TRUE(base::ReadFileToString(saved_file, &received_cached_content)); |
| 176 EXPECT_EQ(cached_content, received_cached_content); |
| 177 |
| 178 // Request using a response for the same URL, with the a different etag. Check |
| 179 // that the new content is returned, and the cached files is deleted. |
| 180 url_response = mojo::URLResponse::New(); |
| 181 url_response->url = "http://www.example.com/3"; |
| 182 url_response->headers = Array<String>(1); |
| 183 url_response->headers[0] = base::StringPrintf("ETag: %f", base::RandDouble()); |
| 184 DataPipe pipe3; |
| 185 new_content = base::RandBytesAsString(22); |
| 186 num_bytes = new_content.size(); |
| 187 ASSERT_EQ(MOJO_RESULT_OK, |
| 188 WriteDataRaw(pipe3.producer_handle.get(), new_content.c_str(), |
| 189 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
| 190 ASSERT_EQ(new_content.size(), num_bytes); |
| 191 pipe3.producer_handle.reset(); |
| 192 url_response->body = pipe3.consumer_handle.Pass(); |
| 193 service_cache_->GetFile( |
| 194 url_response.Pass(), |
| 195 [&file, &cache_dir](Array<uint8_t> received_file_path, |
| 196 Array<uint8_t> received_cache_dir_path) { |
| 197 file = toPath(received_file_path.Pass()); |
| 198 cache_dir = toPath(received_cache_dir_path.Pass()); |
| 199 }); |
| 200 service_cache_.WaitForIncomingMethodCall(); |
| 201 ASSERT_FALSE(file.empty()); |
| 202 ASSERT_TRUE(base::ReadFileToString(file, &received_content)); |
| 203 EXPECT_EQ(new_content, received_content); |
| 204 saved_file = cache_dir.Append("file"); |
| 205 EXPECT_FALSE(base::PathExists(saved_file)); |
| 206 } |
| 207 |
| 208 } // namespace service_cache |
| 209 } // mojo service_cache |
OLD | NEW |