| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/nacl_file_host.h" | 5 #include "chrome/browser/nacl_host/nacl_file_host.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.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" |
| 11 #include "base/test/scoped_path_override.h" | 11 #include "base/test/scoped_path_override.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "chrome/browser/nacl_host/nacl_browser.h" | 13 #include "chrome/browser/nacl_host/nacl_browser.h" |
| 14 #include "components/nacl/common/nacl_browser_delegate.h" | 14 #include "components/nacl/common/nacl_browser_delegate.h" |
| 15 #include "components/nacl/common/pnacl_types.h" | |
| 16 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| 20 using nacl_file_host::PnaclCanOpenFile; | 19 using nacl_file_host::PnaclCanOpenFile; |
| 21 using nacl_file_host::EnsurePnaclInstalled; | |
| 22 | 20 |
| 23 class TestNaClBrowserDelegate : public NaClBrowserDelegate { | 21 class TestNaClBrowserDelegate : public NaClBrowserDelegate { |
| 24 public: | 22 public: |
| 25 | 23 |
| 26 TestNaClBrowserDelegate() : should_pnacl_install_succeed_(false) { } | 24 TestNaClBrowserDelegate() {} |
| 27 | 25 |
| 28 virtual void ShowNaClInfobar(int render_process_id, | 26 virtual void ShowNaClInfobar(int render_process_id, |
| 29 int render_view_id, | 27 int render_view_id, |
| 30 int error_id) OVERRIDE { | 28 int error_id) OVERRIDE { |
| 31 } | 29 } |
| 32 | 30 |
| 33 virtual bool DialogsAreSuppressed() OVERRIDE { | 31 virtual bool DialogsAreSuppressed() OVERRIDE { |
| 34 return false; | 32 return false; |
| 35 } | 33 } |
| 36 | 34 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 66 return false; | 64 return false; |
| 67 } | 65 } |
| 68 | 66 |
| 69 virtual void SetDebugPatterns(std::string debug_patterns) OVERRIDE { | 67 virtual void SetDebugPatterns(std::string debug_patterns) OVERRIDE { |
| 70 } | 68 } |
| 71 | 69 |
| 72 virtual bool URLMatchesDebugPatterns(const GURL& manifest_url) OVERRIDE { | 70 virtual bool URLMatchesDebugPatterns(const GURL& manifest_url) OVERRIDE { |
| 73 return false; | 71 return false; |
| 74 } | 72 } |
| 75 | 73 |
| 76 virtual void TryInstallPnacl( | |
| 77 const base::Callback<void(bool)>& installed) OVERRIDE { | |
| 78 installed.Run(should_pnacl_install_succeed_); | |
| 79 } | |
| 80 | |
| 81 void SetPnaclDirectory(const base::FilePath& pnacl_dir) { | 74 void SetPnaclDirectory(const base::FilePath& pnacl_dir) { |
| 82 pnacl_path_ = pnacl_dir; | 75 pnacl_path_ = pnacl_dir; |
| 83 } | 76 } |
| 84 | 77 |
| 85 // Indicate if we should mock the PNaCl install as succeeding | |
| 86 // or failing for the next test. | |
| 87 void SetShouldPnaclInstallSucceed(bool succeed) { | |
| 88 should_pnacl_install_succeed_ = succeed; | |
| 89 } | |
| 90 | |
| 91 private: | 78 private: |
| 92 base::FilePath pnacl_path_; | 79 base::FilePath pnacl_path_; |
| 93 bool should_pnacl_install_succeed_; | |
| 94 }; | 80 }; |
| 95 | 81 |
| 96 class NaClFileHostTest : public testing::Test { | 82 class NaClFileHostTest : public testing::Test { |
| 97 protected: | 83 protected: |
| 98 NaClFileHostTest(); | 84 NaClFileHostTest(); |
| 99 virtual ~NaClFileHostTest(); | 85 virtual ~NaClFileHostTest(); |
| 100 | 86 |
| 101 virtual void SetUp() OVERRIDE { | 87 virtual void SetUp() OVERRIDE { |
| 102 nacl_browser_delegate_ = new TestNaClBrowserDelegate; | 88 nacl_browser_delegate_ = new TestNaClBrowserDelegate; |
| 103 NaClBrowser::SetDelegate(nacl_browser_delegate_); | 89 NaClBrowser::SetDelegate(nacl_browser_delegate_); |
| 104 } | 90 } |
| 105 | 91 |
| 106 virtual void TearDown() OVERRIDE { | 92 virtual void TearDown() OVERRIDE { |
| 107 NaClBrowser::SetDelegate(NULL); // This deletes nacl_browser_delegate_ | 93 NaClBrowser::SetDelegate(NULL); // This deletes nacl_browser_delegate_ |
| 108 } | 94 } |
| 109 | 95 |
| 110 TestNaClBrowserDelegate* nacl_browser_delegate() { | 96 TestNaClBrowserDelegate* nacl_browser_delegate() { |
| 111 return nacl_browser_delegate_; | 97 return nacl_browser_delegate_; |
| 112 } | 98 } |
| 113 | 99 |
| 114 bool install_success() { return install_success_; } | |
| 115 size_t install_call_count() { | |
| 116 return std::count(events_.begin(), events_.end(), INSTALL_DONE); | |
| 117 } | |
| 118 size_t progress_call_count() { | |
| 119 return std::count(events_.begin(), events_.end(), INSTALL_PROGRESS); | |
| 120 } | |
| 121 bool events_in_correct_order() { | |
| 122 // INSTALL_DONE should be the last thing. | |
| 123 // The rest should be progress events. | |
| 124 size_t size = events_.size(); | |
| 125 return size > 0 && events_[size - 1] == INSTALL_DONE | |
| 126 && progress_call_count() == (size - 1); | |
| 127 } | |
| 128 | |
| 129 public: // Allow classes to bind these callback methods. | |
| 130 void CallbackInstall(bool success) { | |
| 131 install_success_ = success; | |
| 132 events_.push_back(INSTALL_DONE); | |
| 133 } | |
| 134 | |
| 135 void CallbackProgress(const nacl::PnaclInstallProgress& p) { | |
| 136 // Check that the first event has an unknown total. | |
| 137 if (events_.size() == 0) { | |
| 138 EXPECT_FALSE(nacl::PnaclInstallProgress::progress_known(p)); | |
| 139 } | |
| 140 events_.push_back(INSTALL_PROGRESS); | |
| 141 // TODO(jvoung): be able to check that current_progress | |
| 142 // goes up monotonically and hits total_progress at the end, | |
| 143 // when we actually get real progress events. | |
| 144 } | |
| 145 | |
| 146 private: | 100 private: |
| 147 enum EventType { | |
| 148 INSTALL_DONE, | |
| 149 INSTALL_PROGRESS | |
| 150 }; | |
| 151 TestNaClBrowserDelegate* nacl_browser_delegate_; | 101 TestNaClBrowserDelegate* nacl_browser_delegate_; |
| 152 bool install_success_; | |
| 153 std::vector<EventType> events_; | |
| 154 content::TestBrowserThreadBundle thread_bundle_; | 102 content::TestBrowserThreadBundle thread_bundle_; |
| 155 DISALLOW_COPY_AND_ASSIGN(NaClFileHostTest); | 103 DISALLOW_COPY_AND_ASSIGN(NaClFileHostTest); |
| 156 }; | 104 }; |
| 157 | 105 |
| 158 NaClFileHostTest::NaClFileHostTest() | 106 NaClFileHostTest::NaClFileHostTest() |
| 159 : nacl_browser_delegate_(NULL), | 107 : nacl_browser_delegate_(NULL), |
| 160 install_success_(false), | |
| 161 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 108 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 162 } | 109 } |
| 163 | 110 |
| 164 NaClFileHostTest::~NaClFileHostTest() { | 111 NaClFileHostTest::~NaClFileHostTest() { |
| 165 } | 112 } |
| 166 | 113 |
| 167 // Try to pass a few funny filenames with a dummy PNaCl directory set. | 114 // Try to pass a few funny filenames with a dummy PNaCl directory set. |
| 168 TEST_F(NaClFileHostTest, TestFilenamesWithPnaclPath) { | 115 TEST_F(NaClFileHostTest, TestFilenamesWithPnaclPath) { |
| 169 base::ScopedTempDir scoped_tmp_dir; | 116 base::ScopedTempDir scoped_tmp_dir; |
| 170 ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir()); | 117 ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 EXPECT_FALSE(PnaclCanOpenFile("..\\llc", &out_path)); | 159 EXPECT_FALSE(PnaclCanOpenFile("..\\llc", &out_path)); |
| 213 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%", &out_path)); | 160 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%", &out_path)); |
| 214 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%\\explorer.exe", &out_path)); | 161 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%\\explorer.exe", &out_path)); |
| 215 #else | 162 #else |
| 216 EXPECT_FALSE(PnaclCanOpenFile("../llc", &out_path)); | 163 EXPECT_FALSE(PnaclCanOpenFile("../llc", &out_path)); |
| 217 EXPECT_FALSE(PnaclCanOpenFile("/bin/sh", &out_path)); | 164 EXPECT_FALSE(PnaclCanOpenFile("/bin/sh", &out_path)); |
| 218 EXPECT_FALSE(PnaclCanOpenFile("$HOME", &out_path)); | 165 EXPECT_FALSE(PnaclCanOpenFile("$HOME", &out_path)); |
| 219 EXPECT_FALSE(PnaclCanOpenFile("$HOME/.bashrc", &out_path)); | 166 EXPECT_FALSE(PnaclCanOpenFile("$HOME/.bashrc", &out_path)); |
| 220 #endif | 167 #endif |
| 221 } | 168 } |
| 222 | |
| 223 // Test that callbacks return success when PNaCl looks like it is | |
| 224 // already installed. No intermediate progress events are expected. | |
| 225 TEST_F(NaClFileHostTest, TestEnsureInstalledAlreadyInstalled) { | |
| 226 base::ScopedTempDir scoped_tmp_dir; | |
| 227 ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir()); | |
| 228 | |
| 229 base::FilePath kTestPnaclPath = scoped_tmp_dir.path(); | |
| 230 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); | |
| 231 ASSERT_TRUE(NaClBrowser::GetDelegate()->GetPnaclDirectory(&kTestPnaclPath)); | |
| 232 | |
| 233 EnsurePnaclInstalled( | |
| 234 base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), | |
| 235 base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); | |
| 236 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
| 237 base::RunLoop().RunUntilIdle(); | |
| 238 | |
| 239 EXPECT_TRUE(install_success()); | |
| 240 EXPECT_EQ(1u, install_call_count()); | |
| 241 EXPECT_EQ(0u, progress_call_count()); | |
| 242 EXPECT_TRUE(events_in_correct_order()); | |
| 243 } | |
| 244 | |
| 245 // Test that final callback is called with success, when PNaCl does not | |
| 246 // look like it's already installed, but mock installer says success. | |
| 247 // Also check that intermediate progress events are called. | |
| 248 TEST_F(NaClFileHostTest, TestEnsureInstalledNotInstalledSuccess) { | |
| 249 base::FilePath kTestPnaclPath; | |
| 250 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); | |
| 251 nacl_browser_delegate()->SetShouldPnaclInstallSucceed(true); | |
| 252 | |
| 253 EnsurePnaclInstalled( | |
| 254 base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), | |
| 255 base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); | |
| 256 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
| 257 base::RunLoop().RunUntilIdle(); | |
| 258 | |
| 259 EXPECT_TRUE(install_success()); | |
| 260 EXPECT_EQ(1u, install_call_count()); | |
| 261 EXPECT_GE(progress_call_count(), 1u); | |
| 262 EXPECT_TRUE(events_in_correct_order()); | |
| 263 } | |
| 264 | |
| 265 // Test that final callback is called with error, when PNaCl does not look | |
| 266 // like it's already installed, but mock installer says error. | |
| 267 // Also check that intermediate progress events are called. | |
| 268 TEST_F(NaClFileHostTest, TestEnsureInstalledNotInstalledError) { | |
| 269 base::FilePath kTestPnaclPath; | |
| 270 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); | |
| 271 nacl_browser_delegate()->SetShouldPnaclInstallSucceed(false); | |
| 272 | |
| 273 EnsurePnaclInstalled( | |
| 274 base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), | |
| 275 base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); | |
| 276 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
| 277 base::RunLoop().RunUntilIdle(); | |
| 278 | |
| 279 EXPECT_FALSE(install_success()); | |
| 280 EXPECT_EQ(1u, install_call_count()); | |
| 281 EXPECT_GE(progress_call_count(), 1u); | |
| 282 EXPECT_TRUE(events_in_correct_order()); | |
| 283 } | |
| OLD | NEW |