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