blob: 22d186962d8ba340265e4dee00fdcf7475b0c8e3 [file] [log] [blame]
[email protected]57bd67982013-07-21 06:07:301// 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]7c766e92013-11-22 20:44:025#include "components/nacl/browser/pnacl_host.h"
[email protected]57bd67982013-07-21 06:07:306
avi26062922015-12-26 00:14:187#include <stddef.h>
[email protected]d40657e2013-08-08 18:19:238#include <stdio.h>
avi26062922015-12-26 00:14:189#include <string.h>
mtomaszf7d99a5c2014-09-15 16:23:4610#include <string>
11
[email protected]57bd67982013-07-21 06:07:3012#include "base/bind.h"
13#include "base/files/scoped_temp_dir.h"
14#include "base/run_loop.h"
avi26062922015-12-26 00:14:1815#include "build/build_config.h"
[email protected]30a90712013-11-18 16:26:1116#include "components/nacl/browser/pnacl_translation_cache.h"
[email protected]57bd67982013-07-21 06:07:3017#include "content/public/browser/browser_thread.h"
Gabriel Charettec7108742019-08-23 03:31:4018#include "content/public/test/browser_task_environment.h"
mtomaszf7d99a5c2014-09-15 16:23:4619#include "content/public/test/test_utils.h"
[email protected]abc31ee2013-08-09 07:25:3520#include "net/base/test_completion_callback.h"
Maks Orlovich61419e72017-08-15 14:42:0521#include "net/disk_cache/disk_cache.h"
[email protected]57bd67982013-07-21 06:07:3022#include "testing/gtest/include/gtest/gtest.h"
23
[email protected]d40657e2013-08-08 18:19:2324#if defined(OS_WIN)
25#define snprintf _snprintf
26#endif
27
[email protected]57bd67982013-07-21 06:07:3028namespace pnacl {
mtomaszf7d99a5c2014-09-15 16:23:4629namespace {
30
31// Size of a buffer used for writing and reading from a file.
32const size_t kBufferSize = 16u;
33
34} // namespace
[email protected]57bd67982013-07-21 06:07:3035
36class PnaclHostTest : public testing::Test {
37 protected:
38 PnaclHostTest()
Lukasz Anforowiczc695e532020-06-09 02:09:4539 : host_(nullptr),
[email protected]d40657e2013-08-08 18:19:2340 temp_callback_count_(0),
41 write_callback_count_(0),
Gabriel Charette798fde72019-08-20 22:24:0442 task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP) {}
dcheng30a1b1542014-10-29 21:27:5043 void SetUp() override {
gab459e9aa82016-08-02 22:52:4244 host_ = PnaclHost::GetInstance();
[email protected]57bd67982013-07-21 06:07:3045 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
vabr6c5aae82016-09-15 07:31:0946 host_->InitForTest(temp_dir_.GetPath(), true);
[email protected]779dc1782013-10-25 22:07:3047 base::RunLoop().RunUntilIdle();
48 EXPECT_EQ(PnaclHost::CacheReady, host_->cache_state_);
[email protected]57bd67982013-07-21 06:07:3049 }
dcheng30a1b1542014-10-29 21:27:5050 void TearDown() override {
[email protected]d40657e2013-08-08 18:19:2351 EXPECT_EQ(0U, host_->pending_translations());
[email protected]779dc1782013-10-25 22:07:3052 // Give the host a chance to de-init the backend, and then delete it.
53 host_->RendererClosing(0);
Gabriel Charette01507a22017-09-27 21:30:0854 content::RunAllTasksUntilIdle();
Maks Orlovich61419e72017-08-15 14:42:0555 disk_cache::FlushCacheThreadForTesting();
[email protected]779dc1782013-10-25 22:07:3056 EXPECT_EQ(PnaclHost::CacheUninitialized, host_->cache_state_);
[email protected]57bd67982013-07-21 06:07:3057 }
[email protected]8311d572013-09-05 21:46:1658 int GetCacheSize() { return host_->disk_cache_->Size(); }
[email protected]779dc1782013-10-25 22:07:3059 int CacheIsInitialized() {
60 return host_->cache_state_ == PnaclHost::CacheReady;
61 }
62 void ReInitBackend() {
vabr6c5aae82016-09-15 07:31:0963 host_->InitForTest(temp_dir_.GetPath(), true);
[email protected]779dc1782013-10-25 22:07:3064 base::RunLoop().RunUntilIdle();
65 EXPECT_EQ(PnaclHost::CacheReady, host_->cache_state_);
66 }
[email protected]57bd67982013-07-21 06:07:3067
68 public: // Required for derived classes to bind this method
[email protected]8311d572013-09-05 21:46:1669 // Callbacks used by tests which call GetNexeFd.
[email protected]d40657e2013-08-08 18:19:2370 // 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]7744e732014-06-04 12:15:5573 void CallbackExpectMiss(const base::File& file, bool is_hit) {
[email protected]57bd67982013-07-21 06:07:3074 EXPECT_FALSE(is_hit);
[email protected]7744e732014-06-04 12:15:5575 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]d40657e2013-08-08 18:19:2379 EXPECT_FALSE(info.is_directory);
80 EXPECT_EQ(0LL, info.size);
mtomaszf7d99a5c2014-09-15 16:23:4681 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]d40657e2013-08-08 18:19:2386 temp_callback_count_++;
87 }
[email protected]7744e732014-06-04 12:15:5588 void CallbackExpectHit(const base::File& file, bool is_hit) {
[email protected]d40657e2013-08-08 18:19:2389 EXPECT_TRUE(is_hit);
[email protected]7744e732014-06-04 12:15:5590 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]d40657e2013-08-08 18:19:2394 EXPECT_FALSE(info.is_directory);
mtomaszf7d99a5c2014-09-15 16:23:4695 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]d40657e2013-08-08 18:19:23103 EXPECT_STREQ(str, data);
104 temp_callback_count_++;
[email protected]57bd67982013-07-21 06:07:30105 }
106
107 protected:
108 PnaclHost* host_;
109 int temp_callback_count_;
[email protected]d40657e2013-08-08 18:19:23110 int write_callback_count_;
Gabriel Charette798fde72019-08-20 22:24:04111 content::BrowserTaskEnvironment task_environment_;
[email protected]57bd67982013-07-21 06:07:30112 base::ScopedTempDir temp_dir_;
113};
114
[email protected]d40657e2013-08-08 18:19:23115static 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]8311d572013-09-05 21:46:16120 info.has_no_store_header = false;
jvoung58bea962015-03-31 17:19:30121 info.use_subzero = false;
[email protected]d40657e2013-08-08 18:19:23122 return info;
123}
124
Yoichi Osato6a3408c2020-08-14 04:18:37125#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]57bd67982013-07-21 06:07:30134 } while (0)
135
[email protected]57bd67982013-07-21 06:07:30136TEST_F(PnaclHostTest, BasicMiss) {
[email protected]d40657e2013-08-08 18:19:23137 nacl::PnaclCacheInfo info = GetTestCacheInfo();
138 // Test cold miss.
[email protected]4241eb02013-08-08 22:15:41139 GET_NEXE_FD(0, 0, false, info, false);
[email protected]d40657e2013-08-08 18:19:23140 EXPECT_EQ(1U, host_->pending_translations());
Gabriel Charette01507a22017-09-27 21:30:08141 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23142 EXPECT_EQ(1U, host_->pending_translations());
143 EXPECT_EQ(1, temp_callback_count_);
[email protected]26f14b5a2013-08-07 19:23:13144 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08145 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23146 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]4241eb02013-08-08 22:15:41149 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08150 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23151 EXPECT_EQ(2, temp_callback_count_);
152 EXPECT_EQ(1U, host_->pending_translations());
[email protected]57bd67982013-07-21 06:07:30153 host_->RendererClosing(0);
Gabriel Charette01507a22017-09-27 21:30:08154 content::RunAllTasksUntilIdle();
[email protected]779dc1782013-10-25 22:07:30155 // Check that the cache has de-initialized after the last renderer goes away.
156 EXPECT_FALSE(CacheIsInitialized());
[email protected]57bd67982013-07-21 06:07:30157}
158
159TEST_F(PnaclHostTest, BadArguments) {
[email protected]d40657e2013-08-08 18:19:23160 nacl::PnaclCacheInfo info = GetTestCacheInfo();
[email protected]4241eb02013-08-08 22:15:41161 GET_NEXE_FD(0, 0, false, info, false);
[email protected]d40657e2013-08-08 18:19:23162 EXPECT_EQ(1U, host_->pending_translations());
[email protected]26f14b5a2013-08-07 19:23:13163 host_->TranslationFinished(0, 1, true); // nonexistent translation
[email protected]d40657e2013-08-08 18:19:23164 EXPECT_EQ(1U, host_->pending_translations());
[email protected]57bd67982013-07-21 06:07:30165 host_->RendererClosing(1); // nonexistent renderer
[email protected]d40657e2013-08-08 18:19:23166 EXPECT_EQ(1U, host_->pending_translations());
Gabriel Charette01507a22017-09-27 21:30:08167 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23168 EXPECT_EQ(1, temp_callback_count_);
[email protected]57bd67982013-07-21 06:07:30169 host_->RendererClosing(0); // close without finishing
[email protected]d40657e2013-08-08 18:19:23170}
171
172TEST_F(PnaclHostTest, BasicHit) {
173 nacl::PnaclCacheInfo info = GetTestCacheInfo();
[email protected]4241eb02013-08-08 22:15:41174 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08175 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23176 EXPECT_EQ(1, temp_callback_count_);
177 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08178 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41179 GET_NEXE_FD(0, 1, false, info, true);
Gabriel Charette01507a22017-09-27 21:30:08180 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23181 EXPECT_EQ(2, temp_callback_count_);
182 EXPECT_EQ(0U, host_->pending_translations());
183}
184
185TEST_F(PnaclHostTest, TranslationErrors) {
186 nacl::PnaclCacheInfo info = GetTestCacheInfo();
[email protected]4241eb02013-08-08 22:15:41187 GET_NEXE_FD(0, 0, false, info, false);
[email protected]d40657e2013-08-08 18:19:23188 // Early abort, before temp file request returns
189 host_->TranslationFinished(0, 0, false);
Gabriel Charette01507a22017-09-27 21:30:08190 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23191 EXPECT_EQ(0U, host_->pending_translations());
192 EXPECT_EQ(0, temp_callback_count_);
[email protected]779dc1782013-10-25 22:07:30193 // 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]d40657e2013-08-08 18:19:23197 // Check that another request for the same info misses successfully.
[email protected]4241eb02013-08-08 22:15:41198 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08199 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23200 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08201 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23202 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]4241eb02013-08-08 22:15:41207 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08208 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23209 EXPECT_EQ(2, temp_callback_count_);
210 host_->TranslationFinished(0, 0, false);
Gabriel Charette01507a22017-09-27 21:30:08211 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23212 EXPECT_EQ(0U, host_->pending_translations());
213 // Check another successful miss
[email protected]4241eb02013-08-08 22:15:41214 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08215 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23216 EXPECT_EQ(3, temp_callback_count_);
217 host_->TranslationFinished(0, 0, false);
218 EXPECT_EQ(0U, host_->pending_translations());
219}
220
[email protected]ac65e6f2013-08-30 00:47:32221TEST_F(PnaclHostTest, OverlappedMissesAfterTempReturn) {
[email protected]d40657e2013-08-08 18:19:23222 nacl::PnaclCacheInfo info = GetTestCacheInfo();
[email protected]4241eb02013-08-08 22:15:41223 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08224 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23225 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]4241eb02013-08-08 22:15:41229 GET_NEXE_FD(0, 1, false, info, true);
Gabriel Charette01507a22017-09-27 21:30:08230 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23231 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 Charette01507a22017-09-27 21:30:08236 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23237 EXPECT_EQ(2, temp_callback_count_);
238 EXPECT_EQ(0U, host_->pending_translations());
239}
240
[email protected]ac65e6f2013-08-30 00:47:32241TEST_F(PnaclHostTest, OverlappedMissesBeforeTempReturn) {
[email protected]d40657e2013-08-08 18:19:23242 nacl::PnaclCacheInfo info = GetTestCacheInfo();
[email protected]4241eb02013-08-08 22:15:41243 GET_NEXE_FD(0, 0, false, info, false);
[email protected]d40657e2013-08-08 18:19:23244 // Send the 2nd fd request before the first one returns a temp file.
[email protected]4241eb02013-08-08 22:15:41245 GET_NEXE_FD(0, 1, false, info, true);
Gabriel Charette01507a22017-09-27 21:30:08246 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23247 EXPECT_EQ(1, temp_callback_count_);
248 EXPECT_EQ(2U, host_->pending_translations());
Gabriel Charette01507a22017-09-27 21:30:08249 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23250 EXPECT_EQ(2U, host_->pending_translations());
251 EXPECT_EQ(1, temp_callback_count_);
252 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08253 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23254 EXPECT_EQ(2, temp_callback_count_);
255 EXPECT_EQ(0U, host_->pending_translations());
256}
257
258TEST_F(PnaclHostTest, OverlappedHitsBeforeTempReturn) {
259 nacl::PnaclCacheInfo info = GetTestCacheInfo();
260 // Store one in the cache and complete it.
[email protected]4241eb02013-08-08 22:15:41261 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08262 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23263 EXPECT_EQ(1, temp_callback_count_);
264 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08265 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23266 EXPECT_EQ(0U, host_->pending_translations());
[email protected]4241eb02013-08-08 22:15:41267 GET_NEXE_FD(0, 0, false, info, true);
[email protected]d40657e2013-08-08 18:19:23268 // Request the second before the first temp file returns.
[email protected]4241eb02013-08-08 22:15:41269 GET_NEXE_FD(0, 1, false, info, true);
Gabriel Charette01507a22017-09-27 21:30:08270 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23271 EXPECT_EQ(3, temp_callback_count_);
272 EXPECT_EQ(0U, host_->pending_translations());
273}
274
275TEST_F(PnaclHostTest, OverlappedHitsAfterTempReturn) {
276 nacl::PnaclCacheInfo info = GetTestCacheInfo();
277 // Store one in the cache and complete it.
[email protected]4241eb02013-08-08 22:15:41278 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08279 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23280 EXPECT_EQ(1, temp_callback_count_);
281 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08282 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23283 EXPECT_EQ(0U, host_->pending_translations());
[email protected]4241eb02013-08-08 22:15:41284 GET_NEXE_FD(0, 0, false, info, true);
Gabriel Charette01507a22017-09-27 21:30:08285 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41286 GET_NEXE_FD(0, 1, false, info, true);
Gabriel Charette01507a22017-09-27 21:30:08287 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23288 EXPECT_EQ(3, temp_callback_count_);
289 EXPECT_EQ(0U, host_->pending_translations());
290}
291
[email protected]ac65e6f2013-08-30 00:47:32292TEST_F(PnaclHostTest, OverlappedMissesRendererClosing) {
[email protected]d40657e2013-08-08 18:19:23293 nacl::PnaclCacheInfo info = GetTestCacheInfo();
[email protected]4241eb02013-08-08 22:15:41294 GET_NEXE_FD(0, 0, false, info, false);
[email protected]d40657e2013-08-08 18:19:23295 // Send the 2nd fd request from a different renderer.
296 // Test that it eventually gets an fd after the first renderer closes.
[email protected]4241eb02013-08-08 22:15:41297 GET_NEXE_FD(1, 1, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08298 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23299 EXPECT_EQ(1, temp_callback_count_);
300 EXPECT_EQ(2U, host_->pending_translations());
Gabriel Charette01507a22017-09-27 21:30:08301 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23302 EXPECT_EQ(2U, host_->pending_translations());
303 EXPECT_EQ(1, temp_callback_count_);
304 host_->RendererClosing(0);
Gabriel Charette01507a22017-09-27 21:30:08305 content::RunAllTasksUntilIdle();
[email protected]d40657e2013-08-08 18:19:23306 EXPECT_EQ(2, temp_callback_count_);
307 EXPECT_EQ(1U, host_->pending_translations());
308 host_->RendererClosing(1);
[email protected]57bd67982013-07-21 06:07:30309}
310
[email protected]4241eb02013-08-08 22:15:41311TEST_F(PnaclHostTest, Incognito) {
312 nacl::PnaclCacheInfo info = GetTestCacheInfo();
313 GET_NEXE_FD(0, 0, true, info, false);
Gabriel Charette01507a22017-09-27 21:30:08314 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41315 EXPECT_EQ(1, temp_callback_count_);
316 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08317 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41318 // Check that an incognito translation is not stored in the cache
319 GET_NEXE_FD(0, 0, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08320 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41321 EXPECT_EQ(2, temp_callback_count_);
322 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08323 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41324 // Check that an incognito translation can hit from a normal one.
325 GET_NEXE_FD(0, 0, true, info, true);
Gabriel Charette01507a22017-09-27 21:30:08326 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41327 EXPECT_EQ(3, temp_callback_count_);
328}
329
[email protected]ac65e6f2013-08-30 00:47:32330TEST_F(PnaclHostTest, IncognitoOverlappedMiss) {
[email protected]4241eb02013-08-08 22:15:41331 nacl::PnaclCacheInfo info = GetTestCacheInfo();
332 GET_NEXE_FD(0, 0, true, info, false);
333 GET_NEXE_FD(0, 1, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08334 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41335 // 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 Charette01507a22017-09-27 21:30:08340 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41341 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 Charette01507a22017-09-27 21:30:08346 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41347 EXPECT_EQ(3, temp_callback_count_);
348 GET_NEXE_FD(0, 1, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08349 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41350 EXPECT_EQ(4, temp_callback_count_);
351 host_->RendererClosing(0);
352}
353
[email protected]ac65e6f2013-08-30 00:47:32354TEST_F(PnaclHostTest, IncognitoSecondOverlappedMiss) {
[email protected]4241eb02013-08-08 22:15:41355 // 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 Charette01507a22017-09-27 21:30:08361 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41362 EXPECT_EQ(1, temp_callback_count_);
363 EXPECT_EQ(2U, host_->pending_translations());
Gabriel Charette01507a22017-09-27 21:30:08364 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41365 EXPECT_EQ(2U, host_->pending_translations());
366 EXPECT_EQ(1, temp_callback_count_);
367 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08368 content::RunAllTasksUntilIdle();
[email protected]4241eb02013-08-08 22:15:41369 EXPECT_EQ(2, temp_callback_count_);
370 EXPECT_EQ(0U, host_->pending_translations());
371}
372
[email protected]8311d572013-09-05 21:46:16373// Test that pexes with the no-store header do not get cached.
374TEST_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 Charette01507a22017-09-27 21:30:08378 content::RunAllTasksUntilIdle();
[email protected]8311d572013-09-05 21:46:16379 EXPECT_EQ(1, temp_callback_count_);
380 host_->TranslationFinished(0, 0, true);
Gabriel Charette01507a22017-09-27 21:30:08381 content::RunAllTasksUntilIdle();
[email protected]8311d572013-09-05 21:46:16382 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
387TEST_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 Charette01507a22017-09-27 21:30:08392 content::RunAllTasksUntilIdle();
[email protected]8311d572013-09-05 21:46:16393 // 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 Charette01507a22017-09-27 21:30:08398 content::RunAllTasksUntilIdle();
[email protected]8311d572013-09-05 21:46:16399 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 Charette01507a22017-09-27 21:30:08404 content::RunAllTasksUntilIdle();
[email protected]8311d572013-09-05 21:46:16405 EXPECT_EQ(3, temp_callback_count_);
406 GET_NEXE_FD(0, 1, false, info, false);
Gabriel Charette01507a22017-09-27 21:30:08407 content::RunAllTasksUntilIdle();
[email protected]8311d572013-09-05 21:46:16408 EXPECT_EQ(4, temp_callback_count_);
409 host_->RendererClosing(0);
410}
411
[email protected]abc31ee2013-08-09 07:25:35412TEST_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 Charette01507a22017-09-27 21:30:08418 content::RunAllTasksUntilIdle();
[email protected]abc31ee2013-08-09 07:25:35419 EXPECT_EQ(2, temp_callback_count_);
420 host_->TranslationFinished(0, 0, true);
421 host_->TranslationFinished(0, 1, true);
Gabriel Charette01507a22017-09-27 21:30:08422 content::RunAllTasksUntilIdle();
[email protected]abc31ee2013-08-09 07:25:35423 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 Prada36596162019-04-02 09:37:15427 host_->ClearTranslationCacheEntriesBetween(base::Time(), base::Time(),
428 base::BindOnce(cb.callback(), 0));
[email protected]779dc1782013-10-25 22:07:30429 // Check that the translation cache has been cleared before flushing the
430 // queues, because the backend will be freed once it is.
[email protected]abc31ee2013-08-09 07:25:35431 EXPECT_EQ(0, GetCacheSize());
[email protected]779dc1782013-10-25 22:07:30432 EXPECT_EQ(0, cb.GetResult(net::ERR_IO_PENDING));
bnceb9aa7112017-01-05 01:03:46433 // Call posted PnaclHost::CopyFileToBuffer() tasks.
434 base::RunLoop().RunUntilIdle();
[email protected]779dc1782013-10-25 22:07:30435 // Now check that the backend has been freed.
436 EXPECT_FALSE(CacheIsInitialized());
[email protected]abc31ee2013-08-09 07:25:35437}
438
[email protected]8a4a9482014-01-31 19:56:34439// A version of PnaclHostTest that initializes cache on disk.
440class PnaclHostTestDisk : public PnaclHostTest {
441 protected:
dcheng30a1b1542014-10-29 21:27:50442 void SetUp() override {
gab459e9aa82016-08-02 22:52:42443 host_ = PnaclHost::GetInstance();
[email protected]8a4a9482014-01-31 19:56:34444 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
vabr6c5aae82016-09-15 07:31:09445 host_->InitForTest(temp_dir_.GetPath(), false);
[email protected]8a4a9482014-01-31 19:56:34446 EXPECT_EQ(PnaclHost::CacheInitializing, host_->cache_state_);
447 }
448 void DeInit() {
449 host_->DeInitIfSafe();
450 }
451};
452TEST_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 Orlovich61419e72017-08-15 14:42:05459
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]8a4a9482014-01-31 19:56:34463 base::RunLoop().RunUntilIdle();
Maks Orlovich61419e72017-08-15 14:42:05464 EXPECT_TRUE(CacheIsInitialized());
[email protected]8a4a9482014-01-31 19:56:34465}
466
[email protected]57bd67982013-07-21 06:07:30467} // namespace pnacl