[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 1 | // Copyright 2013 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 | |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame] | 5 | #include "components/nacl/browser/pnacl_host.h" |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 6 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 8 | #include <stdio.h> |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 9 | #include <string.h> |
mtomasz | f7d99a5c | 2014-09-15 16:23:46 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 12 | #include "base/bind.h" |
| 13 | #include "base/files/scoped_temp_dir.h" |
| 14 | #include "base/run_loop.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 30a9071 | 2013-11-18 16:26:11 | [diff] [blame] | 16 | #include "components/nacl/browser/pnacl_translation_cache.h" |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 18 | #include "content/public/test/browser_task_environment.h" |
mtomasz | f7d99a5c | 2014-09-15 16:23:46 | [diff] [blame] | 19 | #include "content/public/test/test_utils.h" |
[email protected] | abc31ee | 2013-08-09 07:25:35 | [diff] [blame] | 20 | #include "net/base/test_completion_callback.h" |
Maks Orlovich | 61419e7 | 2017-08-15 14:42:05 | [diff] [blame] | 21 | #include "net/disk_cache/disk_cache.h" |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 24 | #if defined(OS_WIN) |
| 25 | #define snprintf _snprintf |
| 26 | #endif |
| 27 | |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 28 | namespace pnacl { |
mtomasz | f7d99a5c | 2014-09-15 16:23:46 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | // Size of a buffer used for writing and reading from a file. |
| 32 | const size_t kBufferSize = 16u; |
| 33 | |
| 34 | } // namespace |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 35 | |
| 36 | class PnaclHostTest : public testing::Test { |
| 37 | protected: |
| 38 | PnaclHostTest() |
Lukasz Anforowicz | c695e53 | 2020-06-09 02:09:45 | [diff] [blame] | 39 | : host_(nullptr), |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 40 | temp_callback_count_(0), |
| 41 | write_callback_count_(0), |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 42 | task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP) {} |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 43 | void SetUp() override { |
gab | 459e9aa8 | 2016-08-02 22:52:42 | [diff] [blame] | 44 | host_ = PnaclHost::GetInstance(); |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 45 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
vabr | 6c5aae8 | 2016-09-15 07:31:09 | [diff] [blame] | 46 | host_->InitForTest(temp_dir_.GetPath(), true); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 47 | base::RunLoop().RunUntilIdle(); |
| 48 | EXPECT_EQ(PnaclHost::CacheReady, host_->cache_state_); |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 49 | } |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 50 | void TearDown() override { |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 51 | EXPECT_EQ(0U, host_->pending_translations()); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 52 | // Give the host a chance to de-init the backend, and then delete it. |
| 53 | host_->RendererClosing(0); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 54 | content::RunAllTasksUntilIdle(); |
Maks Orlovich | 61419e7 | 2017-08-15 14:42:05 | [diff] [blame] | 55 | disk_cache::FlushCacheThreadForTesting(); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 56 | EXPECT_EQ(PnaclHost::CacheUninitialized, host_->cache_state_); |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 57 | } |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 58 | int GetCacheSize() { return host_->disk_cache_->Size(); } |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 59 | int CacheIsInitialized() { |
| 60 | return host_->cache_state_ == PnaclHost::CacheReady; |
| 61 | } |
| 62 | void ReInitBackend() { |
vabr | 6c5aae8 | 2016-09-15 07:31:09 | [diff] [blame] | 63 | host_->InitForTest(temp_dir_.GetPath(), true); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 64 | base::RunLoop().RunUntilIdle(); |
| 65 | EXPECT_EQ(PnaclHost::CacheReady, host_->cache_state_); |
| 66 | } |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 67 | |
| 68 | public: // Required for derived classes to bind this method |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 69 | // Callbacks used by tests which call GetNexeFd. |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 70 | // CallbackExpectMiss checks that the fd is valid and a miss is reported, |
| 71 | // and also writes some data into the file, which is read back by |
| 72 | // CallbackExpectHit |
[email protected] | 7744e73 | 2014-06-04 12:15:55 | [diff] [blame] | 73 | void CallbackExpectMiss(const base::File& file, bool is_hit) { |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 74 | EXPECT_FALSE(is_hit); |
[email protected] | 7744e73 | 2014-06-04 12:15:55 | [diff] [blame] | 75 | ASSERT_TRUE(file.IsValid()); |
| 76 | base::File::Info info; |
| 77 | base::File* mutable_file = const_cast<base::File*>(&file); |
| 78 | EXPECT_TRUE(mutable_file->GetInfo(&info)); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 79 | EXPECT_FALSE(info.is_directory); |
| 80 | EXPECT_EQ(0LL, info.size); |
mtomasz | f7d99a5c | 2014-09-15 16:23:46 | [diff] [blame] | 81 | char str[kBufferSize]; |
| 82 | memset(str, 0x0, kBufferSize); |
| 83 | snprintf(str, kBufferSize, "testdata%d", ++write_callback_count_); |
| 84 | EXPECT_EQ(kBufferSize, |
| 85 | static_cast<size_t>(mutable_file->Write(0, str, kBufferSize))); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 86 | temp_callback_count_++; |
| 87 | } |
[email protected] | 7744e73 | 2014-06-04 12:15:55 | [diff] [blame] | 88 | void CallbackExpectHit(const base::File& file, bool is_hit) { |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 89 | EXPECT_TRUE(is_hit); |
[email protected] | 7744e73 | 2014-06-04 12:15:55 | [diff] [blame] | 90 | ASSERT_TRUE(file.IsValid()); |
| 91 | base::File::Info info; |
| 92 | base::File* mutable_file = const_cast<base::File*>(&file); |
| 93 | EXPECT_TRUE(mutable_file->GetInfo(&info)); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 94 | EXPECT_FALSE(info.is_directory); |
mtomasz | f7d99a5c | 2014-09-15 16:23:46 | [diff] [blame] | 95 | EXPECT_EQ(kBufferSize, static_cast<size_t>(info.size)); |
| 96 | char data[kBufferSize]; |
| 97 | memset(data, 0x0, kBufferSize); |
| 98 | char str[kBufferSize]; |
| 99 | memset(str, 0x0, kBufferSize); |
| 100 | snprintf(str, kBufferSize, "testdata%d", write_callback_count_); |
| 101 | EXPECT_EQ(kBufferSize, |
| 102 | static_cast<size_t>(mutable_file->Read(0, data, kBufferSize))); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 103 | EXPECT_STREQ(str, data); |
| 104 | temp_callback_count_++; |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | protected: |
| 108 | PnaclHost* host_; |
| 109 | int temp_callback_count_; |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 110 | int write_callback_count_; |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 111 | content::BrowserTaskEnvironment task_environment_; |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 112 | base::ScopedTempDir temp_dir_; |
| 113 | }; |
| 114 | |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 115 | static nacl::PnaclCacheInfo GetTestCacheInfo() { |
| 116 | nacl::PnaclCacheInfo info; |
| 117 | info.pexe_url = GURL("http://www.google.com"); |
| 118 | info.abi_version = 0; |
| 119 | info.opt_level = 0; |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 120 | info.has_no_store_header = false; |
jvoung | 58bea96 | 2015-03-31 17:19:30 | [diff] [blame] | 121 | info.use_subzero = false; |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 122 | return info; |
| 123 | } |
| 124 | |
Yoichi Osato | 6a3408c | 2020-08-14 04:18:37 | [diff] [blame] | 125 | #define GET_NEXE_FD(renderer, instance, incognito, info, expect_hit) \ |
| 126 | do { \ |
| 127 | SCOPED_TRACE(""); \ |
| 128 | host_->GetNexeFd( \ |
| 129 | renderer, 0, /* ignore render_view_id for now */ \ |
| 130 | instance, incognito, info, \ |
| 131 | base::BindRepeating(expect_hit ? &PnaclHostTest::CallbackExpectHit \ |
| 132 | : &PnaclHostTest::CallbackExpectMiss, \ |
| 133 | base::Unretained(this))); \ |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 134 | } while (0) |
| 135 | |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 136 | TEST_F(PnaclHostTest, BasicMiss) { |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 137 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 138 | // Test cold miss. |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 139 | GET_NEXE_FD(0, 0, false, info, false); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 140 | EXPECT_EQ(1U, host_->pending_translations()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 141 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 142 | EXPECT_EQ(1U, host_->pending_translations()); |
| 143 | EXPECT_EQ(1, temp_callback_count_); |
[email protected] | 26f14b5a | 2013-08-07 19:23:13 | [diff] [blame] | 144 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 145 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 146 | EXPECT_EQ(0U, host_->pending_translations()); |
| 147 | // Test that a different cache info field also misses. |
| 148 | info.etag = std::string("something else"); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 149 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 150 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 151 | EXPECT_EQ(2, temp_callback_count_); |
| 152 | EXPECT_EQ(1U, host_->pending_translations()); |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 153 | host_->RendererClosing(0); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 154 | content::RunAllTasksUntilIdle(); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 155 | // Check that the cache has de-initialized after the last renderer goes away. |
| 156 | EXPECT_FALSE(CacheIsInitialized()); |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST_F(PnaclHostTest, BadArguments) { |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 160 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 161 | GET_NEXE_FD(0, 0, false, info, false); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 162 | EXPECT_EQ(1U, host_->pending_translations()); |
[email protected] | 26f14b5a | 2013-08-07 19:23:13 | [diff] [blame] | 163 | host_->TranslationFinished(0, 1, true); // nonexistent translation |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 164 | EXPECT_EQ(1U, host_->pending_translations()); |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 165 | host_->RendererClosing(1); // nonexistent renderer |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 166 | EXPECT_EQ(1U, host_->pending_translations()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 167 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 168 | EXPECT_EQ(1, temp_callback_count_); |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 169 | host_->RendererClosing(0); // close without finishing |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | TEST_F(PnaclHostTest, BasicHit) { |
| 173 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 174 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 175 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 176 | EXPECT_EQ(1, temp_callback_count_); |
| 177 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 178 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 179 | GET_NEXE_FD(0, 1, false, info, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 180 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 181 | EXPECT_EQ(2, temp_callback_count_); |
| 182 | EXPECT_EQ(0U, host_->pending_translations()); |
| 183 | } |
| 184 | |
| 185 | TEST_F(PnaclHostTest, TranslationErrors) { |
| 186 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 187 | GET_NEXE_FD(0, 0, false, info, false); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 188 | // Early abort, before temp file request returns |
| 189 | host_->TranslationFinished(0, 0, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 190 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 191 | EXPECT_EQ(0U, host_->pending_translations()); |
| 192 | EXPECT_EQ(0, temp_callback_count_); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 193 | // The backend will have been freed when the query comes back and there |
| 194 | // are no pending translations. |
| 195 | EXPECT_FALSE(CacheIsInitialized()); |
| 196 | ReInitBackend(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 197 | // Check that another request for the same info misses successfully. |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 198 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 199 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 200 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 201 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 202 | EXPECT_EQ(1, temp_callback_count_); |
| 203 | EXPECT_EQ(0U, host_->pending_translations()); |
| 204 | |
| 205 | // Now try sending the error after the temp file request returns |
| 206 | info.abi_version = 222; |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 207 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 208 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 209 | EXPECT_EQ(2, temp_callback_count_); |
| 210 | host_->TranslationFinished(0, 0, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 211 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 212 | EXPECT_EQ(0U, host_->pending_translations()); |
| 213 | // Check another successful miss |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 214 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 215 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 216 | EXPECT_EQ(3, temp_callback_count_); |
| 217 | host_->TranslationFinished(0, 0, false); |
| 218 | EXPECT_EQ(0U, host_->pending_translations()); |
| 219 | } |
| 220 | |
[email protected] | ac65e6f | 2013-08-30 00:47:32 | [diff] [blame] | 221 | TEST_F(PnaclHostTest, OverlappedMissesAfterTempReturn) { |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 222 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 223 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 224 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 225 | EXPECT_EQ(1, temp_callback_count_); |
| 226 | EXPECT_EQ(1U, host_->pending_translations()); |
| 227 | // Test that a second request for the same nexe while the first one is still |
| 228 | // outstanding eventually hits. |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 229 | GET_NEXE_FD(0, 1, false, info, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 230 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 231 | EXPECT_EQ(2U, host_->pending_translations()); |
| 232 | // The temp file should not be returned to the second request until after the |
| 233 | // first is finished translating. |
| 234 | EXPECT_EQ(1, temp_callback_count_); |
| 235 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 236 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 237 | EXPECT_EQ(2, temp_callback_count_); |
| 238 | EXPECT_EQ(0U, host_->pending_translations()); |
| 239 | } |
| 240 | |
[email protected] | ac65e6f | 2013-08-30 00:47:32 | [diff] [blame] | 241 | TEST_F(PnaclHostTest, OverlappedMissesBeforeTempReturn) { |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 242 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 243 | GET_NEXE_FD(0, 0, false, info, false); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 244 | // Send the 2nd fd request before the first one returns a temp file. |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 245 | GET_NEXE_FD(0, 1, false, info, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 246 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 247 | EXPECT_EQ(1, temp_callback_count_); |
| 248 | EXPECT_EQ(2U, host_->pending_translations()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 249 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 250 | EXPECT_EQ(2U, host_->pending_translations()); |
| 251 | EXPECT_EQ(1, temp_callback_count_); |
| 252 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 253 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 254 | EXPECT_EQ(2, temp_callback_count_); |
| 255 | EXPECT_EQ(0U, host_->pending_translations()); |
| 256 | } |
| 257 | |
| 258 | TEST_F(PnaclHostTest, OverlappedHitsBeforeTempReturn) { |
| 259 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 260 | // Store one in the cache and complete it. |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 261 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 262 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 263 | EXPECT_EQ(1, temp_callback_count_); |
| 264 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 265 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 266 | EXPECT_EQ(0U, host_->pending_translations()); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 267 | GET_NEXE_FD(0, 0, false, info, true); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 268 | // Request the second before the first temp file returns. |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 269 | GET_NEXE_FD(0, 1, false, info, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 270 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 271 | EXPECT_EQ(3, temp_callback_count_); |
| 272 | EXPECT_EQ(0U, host_->pending_translations()); |
| 273 | } |
| 274 | |
| 275 | TEST_F(PnaclHostTest, OverlappedHitsAfterTempReturn) { |
| 276 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 277 | // Store one in the cache and complete it. |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 278 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 279 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 280 | EXPECT_EQ(1, temp_callback_count_); |
| 281 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 282 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 283 | EXPECT_EQ(0U, host_->pending_translations()); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 284 | GET_NEXE_FD(0, 0, false, info, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 285 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 286 | GET_NEXE_FD(0, 1, false, info, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 287 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 288 | EXPECT_EQ(3, temp_callback_count_); |
| 289 | EXPECT_EQ(0U, host_->pending_translations()); |
| 290 | } |
| 291 | |
[email protected] | ac65e6f | 2013-08-30 00:47:32 | [diff] [blame] | 292 | TEST_F(PnaclHostTest, OverlappedMissesRendererClosing) { |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 293 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 294 | GET_NEXE_FD(0, 0, false, info, false); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 295 | // Send the 2nd fd request from a different renderer. |
| 296 | // Test that it eventually gets an fd after the first renderer closes. |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 297 | GET_NEXE_FD(1, 1, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 298 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 299 | EXPECT_EQ(1, temp_callback_count_); |
| 300 | EXPECT_EQ(2U, host_->pending_translations()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 301 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 302 | EXPECT_EQ(2U, host_->pending_translations()); |
| 303 | EXPECT_EQ(1, temp_callback_count_); |
| 304 | host_->RendererClosing(0); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 305 | content::RunAllTasksUntilIdle(); |
[email protected] | d40657e | 2013-08-08 18:19:23 | [diff] [blame] | 306 | EXPECT_EQ(2, temp_callback_count_); |
| 307 | EXPECT_EQ(1U, host_->pending_translations()); |
| 308 | host_->RendererClosing(1); |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 311 | TEST_F(PnaclHostTest, Incognito) { |
| 312 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 313 | GET_NEXE_FD(0, 0, true, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 314 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 315 | EXPECT_EQ(1, temp_callback_count_); |
| 316 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 317 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 318 | // Check that an incognito translation is not stored in the cache |
| 319 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 320 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 321 | EXPECT_EQ(2, temp_callback_count_); |
| 322 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 323 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 324 | // Check that an incognito translation can hit from a normal one. |
| 325 | GET_NEXE_FD(0, 0, true, info, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 326 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 327 | EXPECT_EQ(3, temp_callback_count_); |
| 328 | } |
| 329 | |
[email protected] | ac65e6f | 2013-08-30 00:47:32 | [diff] [blame] | 330 | TEST_F(PnaclHostTest, IncognitoOverlappedMiss) { |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 331 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 332 | GET_NEXE_FD(0, 0, true, info, false); |
| 333 | GET_NEXE_FD(0, 1, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 334 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 335 | // Check that both translations have returned misses, (i.e. that the |
| 336 | // second one has not blocked on the incognito one) |
| 337 | EXPECT_EQ(2, temp_callback_count_); |
| 338 | host_->TranslationFinished(0, 0, true); |
| 339 | host_->TranslationFinished(0, 1, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 340 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 341 | EXPECT_EQ(0U, host_->pending_translations()); |
| 342 | |
| 343 | // Same test, but issue the 2nd request after the first has returned a miss. |
| 344 | info.abi_version = 222; |
| 345 | GET_NEXE_FD(0, 0, true, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 346 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 347 | EXPECT_EQ(3, temp_callback_count_); |
| 348 | GET_NEXE_FD(0, 1, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 349 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 350 | EXPECT_EQ(4, temp_callback_count_); |
| 351 | host_->RendererClosing(0); |
| 352 | } |
| 353 | |
[email protected] | ac65e6f | 2013-08-30 00:47:32 | [diff] [blame] | 354 | TEST_F(PnaclHostTest, IncognitoSecondOverlappedMiss) { |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 355 | // If the non-incognito request comes first, it should |
| 356 | // behave exactly like OverlappedMissBeforeTempReturn |
| 357 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 358 | GET_NEXE_FD(0, 0, false, info, false); |
| 359 | // Send the 2nd fd request before the first one returns a temp file. |
| 360 | GET_NEXE_FD(0, 1, true, info, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 361 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 362 | EXPECT_EQ(1, temp_callback_count_); |
| 363 | EXPECT_EQ(2U, host_->pending_translations()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 364 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 365 | EXPECT_EQ(2U, host_->pending_translations()); |
| 366 | EXPECT_EQ(1, temp_callback_count_); |
| 367 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 368 | content::RunAllTasksUntilIdle(); |
[email protected] | 4241eb0 | 2013-08-08 22:15:41 | [diff] [blame] | 369 | EXPECT_EQ(2, temp_callback_count_); |
| 370 | EXPECT_EQ(0U, host_->pending_translations()); |
| 371 | } |
| 372 | |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 373 | // Test that pexes with the no-store header do not get cached. |
| 374 | TEST_F(PnaclHostTest, CacheControlNoStore) { |
| 375 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 376 | info.has_no_store_header = true; |
| 377 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 378 | content::RunAllTasksUntilIdle(); |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 379 | EXPECT_EQ(1, temp_callback_count_); |
| 380 | host_->TranslationFinished(0, 0, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 381 | content::RunAllTasksUntilIdle(); |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 382 | EXPECT_EQ(0U, host_->pending_translations()); |
| 383 | EXPECT_EQ(0, GetCacheSize()); |
| 384 | } |
| 385 | |
| 386 | // Test that no-store pexes do not wait, but do duplicate translations |
| 387 | TEST_F(PnaclHostTest, NoStoreOverlappedMiss) { |
| 388 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 389 | info.has_no_store_header = true; |
| 390 | GET_NEXE_FD(0, 0, false, info, false); |
| 391 | GET_NEXE_FD(0, 1, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 392 | content::RunAllTasksUntilIdle(); |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 393 | // Check that both translations have returned misses, (i.e. that the |
| 394 | // second one has not blocked on the first one) |
| 395 | EXPECT_EQ(2, temp_callback_count_); |
| 396 | host_->TranslationFinished(0, 0, true); |
| 397 | host_->TranslationFinished(0, 1, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 398 | content::RunAllTasksUntilIdle(); |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 399 | EXPECT_EQ(0U, host_->pending_translations()); |
| 400 | |
| 401 | // Same test, but issue the 2nd request after the first has returned a miss. |
| 402 | info.abi_version = 222; |
| 403 | GET_NEXE_FD(0, 0, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 404 | content::RunAllTasksUntilIdle(); |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 405 | EXPECT_EQ(3, temp_callback_count_); |
| 406 | GET_NEXE_FD(0, 1, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 407 | content::RunAllTasksUntilIdle(); |
[email protected] | 8311d57 | 2013-09-05 21:46:16 | [diff] [blame] | 408 | EXPECT_EQ(4, temp_callback_count_); |
| 409 | host_->RendererClosing(0); |
| 410 | } |
| 411 | |
[email protected] | abc31ee | 2013-08-09 07:25:35 | [diff] [blame] | 412 | TEST_F(PnaclHostTest, ClearTranslationCache) { |
| 413 | nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 414 | // Add 2 entries in the cache |
| 415 | GET_NEXE_FD(0, 0, false, info, false); |
| 416 | info.abi_version = 222; |
| 417 | GET_NEXE_FD(0, 1, false, info, false); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 418 | content::RunAllTasksUntilIdle(); |
[email protected] | abc31ee | 2013-08-09 07:25:35 | [diff] [blame] | 419 | EXPECT_EQ(2, temp_callback_count_); |
| 420 | host_->TranslationFinished(0, 0, true); |
| 421 | host_->TranslationFinished(0, 1, true); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 422 | content::RunAllTasksUntilIdle(); |
[email protected] | abc31ee | 2013-08-09 07:25:35 | [diff] [blame] | 423 | EXPECT_EQ(0U, host_->pending_translations()); |
| 424 | EXPECT_EQ(2, GetCacheSize()); |
| 425 | net::TestCompletionCallback cb; |
| 426 | // Since we are using a memory backend, the clear should happen immediately. |
Mario Sanchez Prada | 3659616 | 2019-04-02 09:37:15 | [diff] [blame] | 427 | host_->ClearTranslationCacheEntriesBetween(base::Time(), base::Time(), |
| 428 | base::BindOnce(cb.callback(), 0)); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 429 | // Check that the translation cache has been cleared before flushing the |
| 430 | // queues, because the backend will be freed once it is. |
[email protected] | abc31ee | 2013-08-09 07:25:35 | [diff] [blame] | 431 | EXPECT_EQ(0, GetCacheSize()); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 432 | EXPECT_EQ(0, cb.GetResult(net::ERR_IO_PENDING)); |
bnc | eb9aa711 | 2017-01-05 01:03:46 | [diff] [blame] | 433 | // Call posted PnaclHost::CopyFileToBuffer() tasks. |
| 434 | base::RunLoop().RunUntilIdle(); |
[email protected] | 779dc178 | 2013-10-25 22:07:30 | [diff] [blame] | 435 | // Now check that the backend has been freed. |
| 436 | EXPECT_FALSE(CacheIsInitialized()); |
[email protected] | abc31ee | 2013-08-09 07:25:35 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | 8a4a948 | 2014-01-31 19:56:34 | [diff] [blame] | 439 | // A version of PnaclHostTest that initializes cache on disk. |
| 440 | class PnaclHostTestDisk : public PnaclHostTest { |
| 441 | protected: |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 442 | void SetUp() override { |
gab | 459e9aa8 | 2016-08-02 22:52:42 | [diff] [blame] | 443 | host_ = PnaclHost::GetInstance(); |
[email protected] | 8a4a948 | 2014-01-31 19:56:34 | [diff] [blame] | 444 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
vabr | 6c5aae8 | 2016-09-15 07:31:09 | [diff] [blame] | 445 | host_->InitForTest(temp_dir_.GetPath(), false); |
[email protected] | 8a4a948 | 2014-01-31 19:56:34 | [diff] [blame] | 446 | EXPECT_EQ(PnaclHost::CacheInitializing, host_->cache_state_); |
| 447 | } |
| 448 | void DeInit() { |
| 449 | host_->DeInitIfSafe(); |
| 450 | } |
| 451 | }; |
| 452 | TEST_F(PnaclHostTestDisk, DeInitWhileInitializing) { |
| 453 | // Since there's no easy way to pump message queues one message at a time, we |
| 454 | // have to simulate what would happen if 1 DeInitIfsafe task gets queued, then |
| 455 | // a GetNexeFd gets queued, and then another DeInitIfSafe gets queued before |
| 456 | // the first one runs. We can just shortcut and call DeInitIfSafe while the |
| 457 | // cache is still initializing. |
| 458 | DeInit(); |
Maks Orlovich | 61419e7 | 2017-08-15 14:42:05 | [diff] [blame] | 459 | |
| 460 | // Now let it finish initializing. (Other tests don't need this since they |
| 461 | // use in-memory storage). |
| 462 | disk_cache::FlushCacheThreadForTesting(); |
[email protected] | 8a4a948 | 2014-01-31 19:56:34 | [diff] [blame] | 463 | base::RunLoop().RunUntilIdle(); |
Maks Orlovich | 61419e7 | 2017-08-15 14:42:05 | [diff] [blame] | 464 | EXPECT_TRUE(CacheIsInitialized()); |
[email protected] | 8a4a948 | 2014-01-31 19:56:34 | [diff] [blame] | 465 | } |
| 466 | |
[email protected] | 57bd6798 | 2013-07-21 06:07:30 | [diff] [blame] | 467 | } // namespace pnacl |