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