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