| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/nacl_host/pnacl_host.h" | 5 #include "chrome/browser/nacl_host/pnacl_host.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 protected: | 25 protected: |
| 26 PnaclHostTest() | 26 PnaclHostTest() |
| 27 : host_(NULL), | 27 : host_(NULL), |
| 28 temp_callback_count_(0), | 28 temp_callback_count_(0), |
| 29 write_callback_count_(0), | 29 write_callback_count_(0), |
| 30 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 30 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 31 virtual void SetUp() { | 31 virtual void SetUp() { |
| 32 host_ = new PnaclHost(); | 32 host_ = new PnaclHost(); |
| 33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 34 host_->InitForTest(temp_dir_.path()); | 34 host_->InitForTest(temp_dir_.path()); |
| 35 EXPECT_EQ(host_->cache_state_, PnaclHost::CacheReady); | 35 base::RunLoop().RunUntilIdle(); |
| 36 EXPECT_EQ(PnaclHost::CacheReady, host_->cache_state_); |
| 36 } | 37 } |
| 37 virtual void TearDown() { | 38 virtual void TearDown() { |
| 38 EXPECT_EQ(0U, host_->pending_translations()); | 39 EXPECT_EQ(0U, host_->pending_translations()); |
| 40 // Give the host a chance to de-init the backend, and then delete it. |
| 41 host_->RendererClosing(0); |
| 42 FlushQueues(); |
| 43 EXPECT_EQ(PnaclHost::CacheUninitialized, host_->cache_state_); |
| 39 delete host_; | 44 delete host_; |
| 40 } | 45 } |
| 41 // Flush the blocking pool first, then any tasks it posted to the IO thread. | 46 // Flush the blocking pool first, then any tasks it posted to the IO thread. |
| 42 // Do 2 rounds of flushing, because some operations require 2 trips back and | 47 // Do 2 rounds of flushing, because some operations require 2 trips back and |
| 43 // forth between the threads. | 48 // forth between the threads. |
| 44 void FlushQueues() { | 49 void FlushQueues() { |
| 45 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 50 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 46 base::RunLoop().RunUntilIdle(); | 51 base::RunLoop().RunUntilIdle(); |
| 47 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 52 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 48 base::RunLoop().RunUntilIdle(); | 53 base::RunLoop().RunUntilIdle(); |
| 49 } | 54 } |
| 50 int GetCacheSize() { return host_->disk_cache_->Size(); } | 55 int GetCacheSize() { return host_->disk_cache_->Size(); } |
| 56 int CacheIsInitialized() { |
| 57 return host_->cache_state_ == PnaclHost::CacheReady; |
| 58 } |
| 59 void ReInitBackend() { |
| 60 host_->InitForTest(temp_dir_.path()); |
| 61 base::RunLoop().RunUntilIdle(); |
| 62 EXPECT_EQ(PnaclHost::CacheReady, host_->cache_state_); |
| 63 } |
| 51 | 64 |
| 52 public: // Required for derived classes to bind this method | 65 public: // Required for derived classes to bind this method |
| 53 // Callbacks used by tests which call GetNexeFd. | 66 // Callbacks used by tests which call GetNexeFd. |
| 54 // CallbackExpectMiss checks that the fd is valid and a miss is reported, | 67 // CallbackExpectMiss checks that the fd is valid and a miss is reported, |
| 55 // and also writes some data into the file, which is read back by | 68 // and also writes some data into the file, which is read back by |
| 56 // CallbackExpectHit | 69 // CallbackExpectHit |
| 57 void CallbackExpectMiss(base::PlatformFile fd, bool is_hit) { | 70 void CallbackExpectMiss(base::PlatformFile fd, bool is_hit) { |
| 58 EXPECT_FALSE(is_hit); | 71 EXPECT_FALSE(is_hit); |
| 59 ASSERT_FALSE(fd == base::kInvalidPlatformFileValue); | 72 ASSERT_FALSE(fd == base::kInvalidPlatformFileValue); |
| 60 base::PlatformFileInfo info; | 73 base::PlatformFileInfo info; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 host_->TranslationFinished(0, 0, true); | 139 host_->TranslationFinished(0, 0, true); |
| 127 FlushQueues(); | 140 FlushQueues(); |
| 128 EXPECT_EQ(0U, host_->pending_translations()); | 141 EXPECT_EQ(0U, host_->pending_translations()); |
| 129 // Test that a different cache info field also misses. | 142 // Test that a different cache info field also misses. |
| 130 info.etag = std::string("something else"); | 143 info.etag = std::string("something else"); |
| 131 GET_NEXE_FD(0, 0, false, info, false); | 144 GET_NEXE_FD(0, 0, false, info, false); |
| 132 FlushQueues(); | 145 FlushQueues(); |
| 133 EXPECT_EQ(2, temp_callback_count_); | 146 EXPECT_EQ(2, temp_callback_count_); |
| 134 EXPECT_EQ(1U, host_->pending_translations()); | 147 EXPECT_EQ(1U, host_->pending_translations()); |
| 135 host_->RendererClosing(0); | 148 host_->RendererClosing(0); |
| 149 FlushQueues(); |
| 150 // Check that the cache has de-initialized after the last renderer goes away. |
| 151 EXPECT_FALSE(CacheIsInitialized()); |
| 136 } | 152 } |
| 137 | 153 |
| 138 TEST_F(PnaclHostTest, BadArguments) { | 154 TEST_F(PnaclHostTest, BadArguments) { |
| 139 nacl::PnaclCacheInfo info = GetTestCacheInfo(); | 155 nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 140 GET_NEXE_FD(0, 0, false, info, false); | 156 GET_NEXE_FD(0, 0, false, info, false); |
| 141 EXPECT_EQ(1U, host_->pending_translations()); | 157 EXPECT_EQ(1U, host_->pending_translations()); |
| 142 host_->TranslationFinished(0, 1, true); // nonexistent translation | 158 host_->TranslationFinished(0, 1, true); // nonexistent translation |
| 143 EXPECT_EQ(1U, host_->pending_translations()); | 159 EXPECT_EQ(1U, host_->pending_translations()); |
| 144 host_->RendererClosing(1); // nonexistent renderer | 160 host_->RendererClosing(1); // nonexistent renderer |
| 145 EXPECT_EQ(1U, host_->pending_translations()); | 161 EXPECT_EQ(1U, host_->pending_translations()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 162 } | 178 } |
| 163 | 179 |
| 164 TEST_F(PnaclHostTest, TranslationErrors) { | 180 TEST_F(PnaclHostTest, TranslationErrors) { |
| 165 nacl::PnaclCacheInfo info = GetTestCacheInfo(); | 181 nacl::PnaclCacheInfo info = GetTestCacheInfo(); |
| 166 GET_NEXE_FD(0, 0, false, info, false); | 182 GET_NEXE_FD(0, 0, false, info, false); |
| 167 // Early abort, before temp file request returns | 183 // Early abort, before temp file request returns |
| 168 host_->TranslationFinished(0, 0, false); | 184 host_->TranslationFinished(0, 0, false); |
| 169 FlushQueues(); | 185 FlushQueues(); |
| 170 EXPECT_EQ(0U, host_->pending_translations()); | 186 EXPECT_EQ(0U, host_->pending_translations()); |
| 171 EXPECT_EQ(0, temp_callback_count_); | 187 EXPECT_EQ(0, temp_callback_count_); |
| 188 // The backend will have been freed when the query comes back and there |
| 189 // are no pending translations. |
| 190 EXPECT_FALSE(CacheIsInitialized()); |
| 191 ReInitBackend(); |
| 172 // Check that another request for the same info misses successfully. | 192 // Check that another request for the same info misses successfully. |
| 173 GET_NEXE_FD(0, 0, false, info, false); | 193 GET_NEXE_FD(0, 0, false, info, false); |
| 174 FlushQueues(); | 194 FlushQueues(); |
| 175 host_->TranslationFinished(0, 0, true); | 195 host_->TranslationFinished(0, 0, true); |
| 176 FlushQueues(); | 196 FlushQueues(); |
| 177 EXPECT_EQ(1, temp_callback_count_); | 197 EXPECT_EQ(1, temp_callback_count_); |
| 178 EXPECT_EQ(0U, host_->pending_translations()); | 198 EXPECT_EQ(0U, host_->pending_translations()); |
| 179 | 199 |
| 180 // Now try sending the error after the temp file request returns | 200 // Now try sending the error after the temp file request returns |
| 181 info.abi_version = 222; | 201 info.abi_version = 222; |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 EXPECT_EQ(2, temp_callback_count_); | 414 EXPECT_EQ(2, temp_callback_count_); |
| 395 host_->TranslationFinished(0, 0, true); | 415 host_->TranslationFinished(0, 0, true); |
| 396 host_->TranslationFinished(0, 1, true); | 416 host_->TranslationFinished(0, 1, true); |
| 397 FlushQueues(); | 417 FlushQueues(); |
| 398 EXPECT_EQ(0U, host_->pending_translations()); | 418 EXPECT_EQ(0U, host_->pending_translations()); |
| 399 EXPECT_EQ(2, GetCacheSize()); | 419 EXPECT_EQ(2, GetCacheSize()); |
| 400 net::TestCompletionCallback cb; | 420 net::TestCompletionCallback cb; |
| 401 // Since we are using a memory backend, the clear should happen immediately. | 421 // Since we are using a memory backend, the clear should happen immediately. |
| 402 host_->ClearTranslationCacheEntriesBetween( | 422 host_->ClearTranslationCacheEntriesBetween( |
| 403 base::Time(), base::Time(), base::Bind(cb.callback(), 0)); | 423 base::Time(), base::Time(), base::Bind(cb.callback(), 0)); |
| 424 // Check that the translation cache has been cleared before flushing the |
| 425 // queues, because the backend will be freed once it is. |
| 426 EXPECT_EQ(0, GetCacheSize()); |
| 404 EXPECT_EQ(0, cb.GetResult(net::ERR_IO_PENDING)); | 427 EXPECT_EQ(0, cb.GetResult(net::ERR_IO_PENDING)); |
| 405 // Check that the translation cache has been cleared | 428 // Now check that the backend has been freed. |
| 406 EXPECT_EQ(0, GetCacheSize()); | 429 EXPECT_FALSE(CacheIsInitialized()); |
| 407 host_->RendererClosing(0); | |
| 408 } | 430 } |
| 409 | 431 |
| 410 } // namespace pnacl | 432 } // namespace pnacl |
| OLD | NEW |