| 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/path_service.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 "chrome/browser/nacl_host/nacl_browser.h" | 13 #include "chrome/browser/nacl_host/nacl_browser.h" |
| 13 #include "components/nacl/common/nacl_browser_delegate.h" | 14 #include "components/nacl/common/nacl_browser_delegate.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 18 |
| 16 using nacl_file_host::PnaclCanOpenFile; | 19 using nacl_file_host::PnaclCanOpenFile; |
| 20 using nacl_file_host::EnsurePnaclInstalled; |
| 17 | 21 |
| 18 class DummyNaClBrowserDelegate : public NaClBrowserDelegate { | 22 class TestNaClBrowserDelegate : public NaClBrowserDelegate { |
| 19 public: | 23 public: |
| 24 |
| 25 TestNaClBrowserDelegate() : should_pnacl_install_succeed_(false) { } |
| 26 |
| 20 virtual void ShowNaClInfobar(int render_process_id, | 27 virtual void ShowNaClInfobar(int render_process_id, |
| 21 int render_view_id, | 28 int render_view_id, |
| 22 int error_id) OVERRIDE { | 29 int error_id) OVERRIDE { |
| 23 } | 30 } |
| 24 | 31 |
| 25 virtual bool DialogsAreSuppressed() OVERRIDE { | 32 virtual bool DialogsAreSuppressed() OVERRIDE { |
| 26 return false; | 33 return false; |
| 27 } | 34 } |
| 28 | 35 |
| 29 virtual bool GetCacheDirectory(base::FilePath* cache_dir) OVERRIDE { | 36 virtual bool GetCacheDirectory(base::FilePath* cache_dir) OVERRIDE { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 47 return std::string(); | 54 return std::string(); |
| 48 } | 55 } |
| 49 | 56 |
| 50 virtual ppapi::host::HostFactory* CreatePpapiHostFactory( | 57 virtual ppapi::host::HostFactory* CreatePpapiHostFactory( |
| 51 content::BrowserPpapiHost* ppapi_host) OVERRIDE { | 58 content::BrowserPpapiHost* ppapi_host) OVERRIDE { |
| 52 return NULL; | 59 return NULL; |
| 53 } | 60 } |
| 54 | 61 |
| 55 virtual void TryInstallPnacl( | 62 virtual void TryInstallPnacl( |
| 56 const base::Callback<void(bool)>& installed) OVERRIDE { | 63 const base::Callback<void(bool)>& installed) OVERRIDE { |
| 57 NOTREACHED(); | 64 installed.Run(should_pnacl_install_succeed_); |
| 58 } | 65 } |
| 59 | 66 |
| 60 void SetPnaclDirectory(const base::FilePath& pnacl_dir) { | 67 void SetPnaclDirectory(const base::FilePath& pnacl_dir) { |
| 61 pnacl_path_ = pnacl_dir; | 68 pnacl_path_ = pnacl_dir; |
| 62 } | 69 } |
| 63 | 70 |
| 71 // Indicate if we should mock the PNaCl install as succeeding |
| 72 // or failing for the next test. |
| 73 void SetShouldPnaclInstallSucceed(bool succeed) { |
| 74 should_pnacl_install_succeed_ = succeed; |
| 75 } |
| 76 |
| 64 private: | 77 private: |
| 65 base::FilePath pnacl_path_; | 78 base::FilePath pnacl_path_; |
| 79 bool should_pnacl_install_succeed_; |
| 66 }; | 80 }; |
| 67 | 81 |
| 68 // Try to pass a few funny filenames with a dummy pnacl directory set. | 82 class NaClFileHostTest : public testing::Test { |
| 69 TEST(PnaclFileHostTest, TestFilenamesWithPnaclPath) { | 83 protected: |
| 84 NaClFileHostTest(); |
| 85 virtual ~NaClFileHostTest(); |
| 86 |
| 87 virtual void SetUp() OVERRIDE { |
| 88 nacl_browser_delegate_ = new TestNaClBrowserDelegate; |
| 89 NaClBrowser::SetDelegate(nacl_browser_delegate_); |
| 90 } |
| 91 |
| 92 virtual void TearDown() OVERRIDE { |
| 93 NaClBrowser::SetDelegate(NULL); // This deletes nacl_browser_delegate_ |
| 94 } |
| 95 |
| 96 TestNaClBrowserDelegate* nacl_browser_delegate() { |
| 97 return nacl_browser_delegate_; |
| 98 } |
| 99 |
| 100 bool install_success() { return install_success_; } |
| 101 size_t install_call_count() { |
| 102 return std::count(events_.begin(), events_.end(), INSTALL_DONE); |
| 103 } |
| 104 size_t progress_call_count() { |
| 105 return std::count(events_.begin(), events_.end(), INSTALL_PROGRESS); |
| 106 } |
| 107 bool events_in_correct_order() { |
| 108 // INSTALL_DONE should be the last thing. |
| 109 // The rest should be progress events. |
| 110 size_t size = events_.size(); |
| 111 return size > 0 && events_[size - 1] == INSTALL_DONE |
| 112 && progress_call_count() == (size - 1); |
| 113 } |
| 114 |
| 115 public: // Allow classes to bind these callback methods. |
| 116 void CallbackInstall(bool success) { |
| 117 install_success_ = success; |
| 118 events_.push_back(INSTALL_DONE); |
| 119 } |
| 120 |
| 121 void CallbackProgress(int64 current_progress, |
| 122 int64 total_progress) { |
| 123 events_.push_back(INSTALL_PROGRESS); |
| 124 // TODO(jvoung): be able to check that current_progress |
| 125 // goes up monotonically and hits total_progress at the end, |
| 126 // when we actually get real progress events. |
| 127 } |
| 128 |
| 129 private: |
| 130 enum EventType { |
| 131 INSTALL_DONE, |
| 132 INSTALL_PROGRESS |
| 133 }; |
| 134 TestNaClBrowserDelegate* nacl_browser_delegate_; |
| 135 bool install_success_; |
| 136 std::vector<EventType> events_; |
| 137 content::TestBrowserThreadBundle thread_bundle_; |
| 138 DISALLOW_COPY_AND_ASSIGN(NaClFileHostTest); |
| 139 }; |
| 140 |
| 141 NaClFileHostTest::NaClFileHostTest() |
| 142 : nacl_browser_delegate_(NULL), |
| 143 install_success_(false), |
| 144 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 145 } |
| 146 |
| 147 NaClFileHostTest::~NaClFileHostTest() { |
| 148 } |
| 149 |
| 150 // Try to pass a few funny filenames with a dummy PNaCl directory set. |
| 151 TEST_F(NaClFileHostTest, TestFilenamesWithPnaclPath) { |
| 70 base::ScopedTempDir scoped_tmp_dir; | 152 base::ScopedTempDir scoped_tmp_dir; |
| 71 ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir()); | 153 ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir()); |
| 72 | 154 |
| 73 base::FilePath kDummyPnaclPath = scoped_tmp_dir.path(); | 155 base::FilePath kTestPnaclPath = scoped_tmp_dir.path(); |
| 74 | 156 |
| 75 DummyNaClBrowserDelegate* nacl_browser_delegate = | 157 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); |
| 76 new DummyNaClBrowserDelegate; | 158 ASSERT_TRUE(NaClBrowser::GetDelegate()->GetPnaclDirectory(&kTestPnaclPath)); |
| 77 nacl_browser_delegate->SetPnaclDirectory(kDummyPnaclPath); | |
| 78 NaClBrowser::SetDelegate(nacl_browser_delegate); | |
| 79 ASSERT_TRUE(NaClBrowser::GetDelegate()->GetPnaclDirectory(&kDummyPnaclPath)); | |
| 80 | 159 |
| 81 // Check allowed strings, and check that the expected prefix is added. | 160 // Check allowed strings, and check that the expected prefix is added. |
| 82 base::FilePath out_path; | 161 base::FilePath out_path; |
| 83 EXPECT_TRUE(PnaclCanOpenFile("pnacl_json", &out_path)); | 162 EXPECT_TRUE(PnaclCanOpenFile("pnacl_json", &out_path)); |
| 84 base::FilePath expected_path = kDummyPnaclPath.Append( | 163 base::FilePath expected_path = kTestPnaclPath.Append( |
| 85 FILE_PATH_LITERAL("pnacl_public_pnacl_json")); | 164 FILE_PATH_LITERAL("pnacl_public_pnacl_json")); |
| 86 EXPECT_EQ(out_path, expected_path); | 165 EXPECT_EQ(expected_path, out_path); |
| 87 | 166 |
| 88 EXPECT_TRUE(PnaclCanOpenFile("x86_32_llc", &out_path)); | 167 EXPECT_TRUE(PnaclCanOpenFile("x86_32_llc", &out_path)); |
| 89 expected_path = kDummyPnaclPath.Append( | 168 expected_path = kTestPnaclPath.Append( |
| 90 FILE_PATH_LITERAL("pnacl_public_x86_32_llc")); | 169 FILE_PATH_LITERAL("pnacl_public_x86_32_llc")); |
| 91 EXPECT_EQ(out_path, expected_path); | 170 EXPECT_EQ(expected_path, out_path); |
| 92 | 171 |
| 93 // Check character ranges. | 172 // Check character ranges. |
| 94 EXPECT_FALSE(PnaclCanOpenFile(".xchars", &out_path)); | 173 EXPECT_FALSE(PnaclCanOpenFile(".xchars", &out_path)); |
| 95 EXPECT_FALSE(PnaclCanOpenFile("/xchars", &out_path)); | 174 EXPECT_FALSE(PnaclCanOpenFile("/xchars", &out_path)); |
| 96 EXPECT_FALSE(PnaclCanOpenFile("x/chars", &out_path)); | 175 EXPECT_FALSE(PnaclCanOpenFile("x/chars", &out_path)); |
| 97 EXPECT_FALSE(PnaclCanOpenFile("\\xchars", &out_path)); | 176 EXPECT_FALSE(PnaclCanOpenFile("\\xchars", &out_path)); |
| 98 EXPECT_FALSE(PnaclCanOpenFile("x\\chars", &out_path)); | 177 EXPECT_FALSE(PnaclCanOpenFile("x\\chars", &out_path)); |
| 99 EXPECT_FALSE(PnaclCanOpenFile("$xchars", &out_path)); | 178 EXPECT_FALSE(PnaclCanOpenFile("$xchars", &out_path)); |
| 100 EXPECT_FALSE(PnaclCanOpenFile("%xchars", &out_path)); | 179 EXPECT_FALSE(PnaclCanOpenFile("%xchars", &out_path)); |
| 101 EXPECT_FALSE(PnaclCanOpenFile("CAPS", &out_path)); | 180 EXPECT_FALSE(PnaclCanOpenFile("CAPS", &out_path)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 #if defined(OS_WIN) | 194 #if defined(OS_WIN) |
| 116 EXPECT_FALSE(PnaclCanOpenFile("..\\llc", &out_path)); | 195 EXPECT_FALSE(PnaclCanOpenFile("..\\llc", &out_path)); |
| 117 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%", &out_path)); | 196 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%", &out_path)); |
| 118 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%\\explorer.exe", &out_path)); | 197 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%\\explorer.exe", &out_path)); |
| 119 #else | 198 #else |
| 120 EXPECT_FALSE(PnaclCanOpenFile("../llc", &out_path)); | 199 EXPECT_FALSE(PnaclCanOpenFile("../llc", &out_path)); |
| 121 EXPECT_FALSE(PnaclCanOpenFile("/bin/sh", &out_path)); | 200 EXPECT_FALSE(PnaclCanOpenFile("/bin/sh", &out_path)); |
| 122 EXPECT_FALSE(PnaclCanOpenFile("$HOME", &out_path)); | 201 EXPECT_FALSE(PnaclCanOpenFile("$HOME", &out_path)); |
| 123 EXPECT_FALSE(PnaclCanOpenFile("$HOME/.bashrc", &out_path)); | 202 EXPECT_FALSE(PnaclCanOpenFile("$HOME/.bashrc", &out_path)); |
| 124 #endif | 203 #endif |
| 125 NaClBrowser::SetDelegate(NULL); | |
| 126 } | 204 } |
| 205 |
| 206 // Test that callbacks return success when PNaCl looks like it is |
| 207 // already installed. No intermediate progress events are expected. |
| 208 TEST_F(NaClFileHostTest, TestEnsureInstalledAlreadyInstalled) { |
| 209 base::ScopedTempDir scoped_tmp_dir; |
| 210 ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir()); |
| 211 |
| 212 base::FilePath kTestPnaclPath = scoped_tmp_dir.path(); |
| 213 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); |
| 214 ASSERT_TRUE(NaClBrowser::GetDelegate()->GetPnaclDirectory(&kTestPnaclPath)); |
| 215 |
| 216 EnsurePnaclInstalled( |
| 217 base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), |
| 218 base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); |
| 219 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 220 base::RunLoop().RunUntilIdle(); |
| 221 |
| 222 EXPECT_TRUE(install_success()); |
| 223 EXPECT_EQ(1u, install_call_count()); |
| 224 EXPECT_EQ(0u, progress_call_count()); |
| 225 EXPECT_TRUE(events_in_correct_order()); |
| 226 } |
| 227 |
| 228 // Test that final callback is called with success, when PNaCl does not |
| 229 // look like it's already installed, but mock installer says success. |
| 230 // Also check that intermediate progress events are called. |
| 231 TEST_F(NaClFileHostTest, TestEnsureInstalledNotInstalledSuccess) { |
| 232 base::FilePath kTestPnaclPath; |
| 233 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); |
| 234 nacl_browser_delegate()->SetShouldPnaclInstallSucceed(true); |
| 235 |
| 236 EnsurePnaclInstalled( |
| 237 base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), |
| 238 base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); |
| 239 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 240 base::RunLoop().RunUntilIdle(); |
| 241 |
| 242 EXPECT_TRUE(install_success()); |
| 243 EXPECT_EQ(1u, install_call_count()); |
| 244 EXPECT_GE(progress_call_count(), 1u); |
| 245 EXPECT_TRUE(events_in_correct_order()); |
| 246 } |
| 247 |
| 248 // Test that final callback is called with error, when PNaCl does not look |
| 249 // like it's already installed, but mock installer says error. |
| 250 // Also check that intermediate progress events are called. |
| 251 TEST_F(NaClFileHostTest, TestEnsureInstalledNotInstalledError) { |
| 252 base::FilePath kTestPnaclPath; |
| 253 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); |
| 254 nacl_browser_delegate()->SetShouldPnaclInstallSucceed(false); |
| 255 |
| 256 EnsurePnaclInstalled( |
| 257 base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), |
| 258 base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); |
| 259 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 260 base::RunLoop().RunUntilIdle(); |
| 261 |
| 262 EXPECT_FALSE(install_success()); |
| 263 EXPECT_EQ(1u, install_call_count()); |
| 264 EXPECT_GE(progress_call_count(), 1u); |
| 265 EXPECT_TRUE(events_in_correct_order()); |
| 266 } |
| OLD | NEW |