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 "apps/app_shim/app_shim_host_manager_mac.h" | 5 #include "apps/app_shim/app_shim_host_manager_mac.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include "apps/app_shim/app_shim_messages.h" | 9 #include "apps/app_shim/app_shim_messages.h" |
10 #include "apps/app_shim/test/app_shim_host_manager_test_api_mac.h" | 10 #include "apps/app_shim/test/app_shim_host_manager_test_api_mac.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #include "ipc/ipc_listener.h" | 22 #include "ipc/ipc_listener.h" |
23 #include "ipc/ipc_message.h" | 23 #include "ipc/ipc_message.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kTestAppMode[] = "test_app"; | 27 const char kTestAppMode[] = "test_app"; |
28 | 28 |
29 // A test version of the AppShimController IPC client in chrome_main_app_mode. | 29 // A test version of the AppShimController IPC client in chrome_main_app_mode. |
30 class TestShimClient : public IPC::Listener { | 30 class TestShimClient : public IPC::Listener { |
31 public: | 31 public: |
32 TestShimClient(const base::FilePath& socket_path); | 32 TestShimClient(); |
33 virtual ~TestShimClient(); | 33 virtual ~TestShimClient(); |
34 | 34 |
35 template <class T> | 35 template <class T> |
36 void Send(const T& message) { | 36 void Send(const T& message) { |
37 channel_->Send(message); | 37 channel_->Send(message); |
38 } | 38 } |
39 | 39 |
40 private: | 40 private: |
41 // IPC::Listener overrides: | 41 // IPC::Listener overrides: |
42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
43 virtual void OnChannelError() OVERRIDE; | 43 virtual void OnChannelError() OVERRIDE; |
44 | 44 |
45 base::Thread io_thread_; | 45 base::Thread io_thread_; |
46 scoped_ptr<IPC::ChannelProxy> channel_; | 46 scoped_ptr<IPC::ChannelProxy> channel_; |
47 | 47 |
48 DISALLOW_COPY_AND_ASSIGN(TestShimClient); | 48 DISALLOW_COPY_AND_ASSIGN(TestShimClient); |
49 }; | 49 }; |
50 | 50 |
51 TestShimClient::TestShimClient(const base::FilePath& socket_path) | 51 TestShimClient::TestShimClient() |
52 : io_thread_("TestShimClientIO") { | 52 : io_thread_("TestShimClientIO") { |
53 base::Thread::Options io_thread_options; | 53 base::Thread::Options io_thread_options; |
54 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 54 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
55 io_thread_.StartWithOptions(io_thread_options); | 55 io_thread_.StartWithOptions(io_thread_options); |
56 | 56 |
57 base::FilePath user_data_dir; | |
58 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); | |
59 base::FilePath symlink_path = | |
60 user_data_dir.Append(app_mode::kAppShimSocketSymlinkName); | |
61 | |
62 base::FilePath socket_path; | |
63 CHECK(base::ReadSymbolicLink(symlink_path, &socket_path)); | |
64 app_mode::VerifySocketPermissions(socket_path); | |
65 | |
57 IPC::ChannelHandle handle(socket_path.value()); | 66 IPC::ChannelHandle handle(socket_path.value()); |
58 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, | 67 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, |
59 this, io_thread_.message_loop_proxy().get())); | 68 this, io_thread_.message_loop_proxy().get())); |
60 } | 69 } |
61 | 70 |
62 TestShimClient::~TestShimClient() {} | 71 TestShimClient::~TestShimClient() {} |
63 | 72 |
64 bool TestShimClient::OnMessageReceived(const IPC::Message& message) { | 73 bool TestShimClient::OnMessageReceived(const IPC::Message& message) { |
65 return true; | 74 return true; |
66 } | 75 } |
(...skipping 12 matching lines...) Expand all Loading... | |
79 virtual ~AppShimHostManagerBrowserTest(); | 88 virtual ~AppShimHostManagerBrowserTest(); |
80 | 89 |
81 protected: | 90 protected: |
82 // Wait for OnShimLaunch, then send a quit, and wait for the response. Used to | 91 // Wait for OnShimLaunch, then send a quit, and wait for the response. Used to |
83 // test launch behavior. | 92 // test launch behavior. |
84 void RunAndExitGracefully(); | 93 void RunAndExitGracefully(); |
85 | 94 |
86 // InProcessBrowserTest overrides: | 95 // InProcessBrowserTest overrides: |
87 virtual void SetUpOnMainThread() OVERRIDE; | 96 virtual void SetUpOnMainThread() OVERRIDE; |
88 virtual void TearDownOnMainThread() OVERRIDE; | 97 virtual void TearDownOnMainThread() OVERRIDE; |
89 virtual bool SetUpUserDataDirectory() OVERRIDE; | |
90 | 98 |
91 // AppShimHandler overrides: | 99 // AppShimHandler overrides: |
92 virtual void OnShimLaunch(apps::AppShimHandler::Host* host, | 100 virtual void OnShimLaunch(apps::AppShimHandler::Host* host, |
93 apps::AppShimLaunchType launch_type, | 101 apps::AppShimLaunchType launch_type, |
94 const std::vector<base::FilePath>& files) OVERRIDE; | 102 const std::vector<base::FilePath>& files) OVERRIDE; |
95 virtual void OnShimClose(apps::AppShimHandler::Host* host) OVERRIDE {} | 103 virtual void OnShimClose(apps::AppShimHandler::Host* host) OVERRIDE {} |
96 virtual void OnShimFocus(apps::AppShimHandler::Host* host, | 104 virtual void OnShimFocus(apps::AppShimHandler::Host* host, |
97 apps::AppShimFocusType focus_type, | 105 apps::AppShimFocusType focus_type, |
98 const std::vector<base::FilePath>& files) OVERRIDE {} | 106 const std::vector<base::FilePath>& files) OVERRIDE {} |
99 virtual void OnShimSetHidden(apps::AppShimHandler::Host* host, | 107 virtual void OnShimSetHidden(apps::AppShimHandler::Host* host, |
100 bool hidden) OVERRIDE {} | 108 bool hidden) OVERRIDE {} |
101 virtual void OnShimQuit(apps::AppShimHandler::Host* host) OVERRIDE; | 109 virtual void OnShimQuit(apps::AppShimHandler::Host* host) OVERRIDE; |
102 | 110 |
103 scoped_ptr<TestShimClient> test_client_; | 111 scoped_ptr<TestShimClient> test_client_; |
104 base::FilePath short_socket_path_; | |
105 std::vector<base::FilePath> last_launch_files_; | 112 std::vector<base::FilePath> last_launch_files_; |
106 apps::AppShimLaunchType last_launch_type_; | 113 apps::AppShimLaunchType last_launch_type_; |
107 | 114 |
108 private: | 115 private: |
109 scoped_refptr<content::MessageLoopRunner> runner_; | 116 scoped_refptr<content::MessageLoopRunner> runner_; |
110 base::ScopedTempDir short_temp_dir_; | |
111 | 117 |
112 int launch_count_; | 118 int launch_count_; |
113 int quit_count_; | 119 int quit_count_; |
114 | 120 |
115 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTest); | 121 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTest); |
116 }; | 122 }; |
117 | 123 |
118 AppShimHostManagerBrowserTest::AppShimHostManagerBrowserTest() | 124 AppShimHostManagerBrowserTest::AppShimHostManagerBrowserTest() |
119 : last_launch_type_(apps::APP_SHIM_LAUNCH_NUM_TYPES), | 125 : last_launch_type_(apps::APP_SHIM_LAUNCH_NUM_TYPES), |
120 launch_count_(0), | 126 launch_count_(0), |
(...skipping 20 matching lines...) Expand all Loading... | |
141 | 147 |
142 void AppShimHostManagerBrowserTest::SetUpOnMainThread() { | 148 void AppShimHostManagerBrowserTest::SetUpOnMainThread() { |
143 // Can't do this in the constructor, it needs a BrowserProcess. | 149 // Can't do this in the constructor, it needs a BrowserProcess. |
144 apps::AppShimHandler::RegisterHandler(kTestAppMode, this); | 150 apps::AppShimHandler::RegisterHandler(kTestAppMode, this); |
145 } | 151 } |
146 | 152 |
147 void AppShimHostManagerBrowserTest::TearDownOnMainThread() { | 153 void AppShimHostManagerBrowserTest::TearDownOnMainThread() { |
148 apps::AppShimHandler::RemoveHandler(kTestAppMode); | 154 apps::AppShimHandler::RemoveHandler(kTestAppMode); |
149 } | 155 } |
150 | 156 |
151 bool AppShimHostManagerBrowserTest::SetUpUserDataDirectory() { | |
152 // Create a symlink at /tmp/scoped_dir_XXXXXX/udd that points to the real user | |
153 // data dir, and use this as the domain socket path. This is required because | |
154 // there is a path length limit for named sockets that is exceeded in | |
155 // multi-process test spawning. | |
156 base::FilePath real_user_data_dir; | |
157 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &real_user_data_dir)); | |
158 EXPECT_TRUE( | |
159 short_temp_dir_.CreateUniqueTempDirUnderPath(base::FilePath("/tmp"))); | |
160 base::FilePath shortened_user_data_dir = short_temp_dir_.path().Append("udd"); | |
161 EXPECT_EQ(0, ::symlink(real_user_data_dir.AsUTF8Unsafe().c_str(), | |
162 shortened_user_data_dir.AsUTF8Unsafe().c_str())); | |
163 | |
164 test::AppShimHostManagerTestApi::OverrideUserDataDir(shortened_user_data_dir); | |
165 short_socket_path_ = | |
166 shortened_user_data_dir.Append(app_mode::kAppShimSocketName); | |
167 | |
168 return InProcessBrowserTest::SetUpUserDataDirectory(); | |
169 } | |
170 | |
171 void AppShimHostManagerBrowserTest::OnShimLaunch( | 157 void AppShimHostManagerBrowserTest::OnShimLaunch( |
172 apps::AppShimHandler::Host* host, | 158 apps::AppShimHandler::Host* host, |
173 apps::AppShimLaunchType launch_type, | 159 apps::AppShimLaunchType launch_type, |
174 const std::vector<base::FilePath>& files) { | 160 const std::vector<base::FilePath>& files) { |
175 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS); | 161 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS); |
176 ++launch_count_; | 162 ++launch_count_; |
177 last_launch_type_ = launch_type; | 163 last_launch_type_ = launch_type; |
178 last_launch_files_ = files; | 164 last_launch_files_ = files; |
179 runner_->Quit(); | 165 runner_->Quit(); |
180 } | 166 } |
181 | 167 |
182 void AppShimHostManagerBrowserTest::OnShimQuit( | 168 void AppShimHostManagerBrowserTest::OnShimQuit( |
183 apps::AppShimHandler::Host* host) { | 169 apps::AppShimHandler::Host* host) { |
184 ++quit_count_; | 170 ++quit_count_; |
185 runner_->Quit(); | 171 runner_->Quit(); |
186 } | 172 } |
187 | 173 |
188 // Test regular launch, which would ask Chrome to launch the app. | 174 // Test regular launch, which would ask Chrome to launch the app. |
189 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) { | 175 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) { |
190 test_client_.reset(new TestShimClient(short_socket_path_)); | 176 test_client_.reset(new TestShimClient()); |
191 test_client_->Send(new AppShimHostMsg_LaunchApp( | 177 test_client_->Send(new AppShimHostMsg_LaunchApp( |
192 browser()->profile()->GetPath(), | 178 browser()->profile()->GetPath(), |
193 kTestAppMode, | 179 kTestAppMode, |
194 apps::APP_SHIM_LAUNCH_NORMAL, | 180 apps::APP_SHIM_LAUNCH_NORMAL, |
195 std::vector<base::FilePath>())); | 181 std::vector<base::FilePath>())); |
196 | 182 |
197 RunAndExitGracefully(); | 183 RunAndExitGracefully(); |
198 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_); | 184 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_); |
199 EXPECT_TRUE(last_launch_files_.empty()); | 185 EXPECT_TRUE(last_launch_files_.empty()); |
200 } | 186 } |
201 | 187 |
202 // Test register-only launch, used when Chrome has already launched the app. | 188 // Test register-only launch, used when Chrome has already launched the app. |
203 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) { | 189 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) { |
204 test_client_.reset(new TestShimClient(short_socket_path_)); | 190 test_client_.reset(new TestShimClient()); |
205 test_client_->Send(new AppShimHostMsg_LaunchApp( | 191 test_client_->Send(new AppShimHostMsg_LaunchApp( |
206 browser()->profile()->GetPath(), | 192 browser()->profile()->GetPath(), |
207 kTestAppMode, | 193 kTestAppMode, |
208 apps::APP_SHIM_LAUNCH_REGISTER_ONLY, | 194 apps::APP_SHIM_LAUNCH_REGISTER_ONLY, |
209 std::vector<base::FilePath>())); | 195 std::vector<base::FilePath>())); |
210 | 196 |
211 RunAndExitGracefully(); | 197 RunAndExitGracefully(); |
212 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_); | 198 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_); |
213 EXPECT_TRUE(last_launch_files_.empty()); | 199 EXPECT_TRUE(last_launch_files_.empty()); |
214 } | 200 } |
215 | 201 |
216 // Ensure the domain socket can be created in a fresh user data dir. | 202 // Ensure the domain socket can be created in a fresh user data dir. |
217 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, | 203 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, |
218 PRE_ReCreate) { | 204 PRE_ReCreate) { |
219 test::AppShimHostManagerTestApi test_api( | 205 test::AppShimHostManagerTestApi test_api( |
220 g_browser_process->platform_part()->app_shim_host_manager()); | 206 g_browser_process->platform_part()->app_shim_host_manager()); |
221 EXPECT_TRUE(test_api.factory()); | 207 EXPECT_TRUE(test_api.factory()); |
222 } | 208 } |
223 | 209 |
224 // Ensure the domain socket can be re-created after a prior browser process has | 210 // Ensure the domain socket can be re-created after a prior browser process has |
225 // quit. | 211 // quit. |
226 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, | 212 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, |
227 ReCreate) { | 213 ReCreate) { |
228 test::AppShimHostManagerTestApi test_api( | 214 test::AppShimHostManagerTestApi test_api( |
229 g_browser_process->platform_part()->app_shim_host_manager()); | 215 g_browser_process->platform_part()->app_shim_host_manager()); |
230 EXPECT_TRUE(test_api.factory()); | 216 EXPECT_TRUE(test_api.factory()); |
231 } | 217 } |
232 | 218 |
233 // Test for AppShimHostManager that fails to create the socket. | 219 // Tests for the files created by AppShimHostManager. |
234 class AppShimHostManagerBrowserTestFailsCreate : | 220 class AppShimHostManagerBrowserTestSocketFiles : |
235 public AppShimHostManagerBrowserTest { | 221 public AppShimHostManagerBrowserTest { |
236 public: | 222 public: |
237 AppShimHostManagerBrowserTestFailsCreate() {} | 223 AppShimHostManagerBrowserTestSocketFiles() {} |
224 | |
225 protected: | |
226 base::FilePath directory_in_tmp_; | |
227 base::FilePath symlink_path_; | |
238 | 228 |
239 private: | 229 private: |
240 virtual bool SetUpUserDataDirectory() OVERRIDE; | 230 virtual bool SetUpUserDataDirectory() OVERRIDE; |
231 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE; | |
241 | 232 |
242 base::ScopedTempDir barrier_dir_; | 233 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTestSocketFiles); |
243 | |
244 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTestFailsCreate); | |
245 }; | 234 }; |
246 | 235 |
247 bool AppShimHostManagerBrowserTestFailsCreate::SetUpUserDataDirectory() { | 236 bool AppShimHostManagerBrowserTestSocketFiles::SetUpUserDataDirectory() { |
237 // Create an existing symlink. It should be replaced by AppShimHostManager. | |
248 base::FilePath user_data_dir; | 238 base::FilePath user_data_dir; |
249 // Start in the "real" user data dir for this test. This is a meta-test for | |
250 // the symlinking steps used in the superclass. That is, by putting the | |
251 // clobber in the actual user data dir, the test will fail if the symlink | |
252 // does not actually point to the user data dir, since it won't be clobbered. | |
253 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); | 239 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); |
254 base::FilePath socket_path = | 240 symlink_path_ = user_data_dir.Append(app_mode::kAppShimSocketSymlinkName); |
255 user_data_dir.Append(app_mode::kAppShimSocketName); | 241 EXPECT_TRUE(base::CreateSymbolicLink(base::FilePath("/tmp"), symlink_path_)); |
tapted
2014/01/31 07:04:12
does this need to be updated?
jackhou1
2014/01/31 07:19:21
Done.
| |
256 // Create a "barrier" to forming the UNIX domain socket. This is just a | |
257 // pre-existing directory which can not be unlink()ed, in order to place a | |
258 // named socked there instead. | |
259 EXPECT_TRUE(barrier_dir_.Set(socket_path)); | |
260 return AppShimHostManagerBrowserTest::SetUpUserDataDirectory(); | 242 return AppShimHostManagerBrowserTest::SetUpUserDataDirectory(); |
261 } | 243 } |
262 | 244 |
263 // Test error handling. This is essentially testing for lifetime correctness | 245 void AppShimHostManagerBrowserTestSocketFiles:: |
264 // during startup for unexpected failures. | 246 TearDownInProcessBrowserTestFixture() { |
265 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestFailsCreate, | 247 // Check that created files have been deleted. |
266 SocketFailure) { | 248 EXPECT_FALSE(base::PathExists(directory_in_tmp_)); |
249 EXPECT_FALSE(base::PathExists(symlink_path_)); | |
250 } | |
251 | |
252 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestSocketFiles, | |
253 ReplacesSymlinkAndCleansUpFiles) { | |
254 // Get the directory created by AppShimHostManager. | |
267 test::AppShimHostManagerTestApi test_api( | 255 test::AppShimHostManagerTestApi test_api( |
268 g_browser_process->platform_part()->app_shim_host_manager()); | 256 g_browser_process->platform_part()->app_shim_host_manager()); |
269 EXPECT_FALSE(test_api.factory()); | 257 directory_in_tmp_ = test_api.directory_in_tmp(); |
258 | |
259 // Check that socket files have been created. | |
260 EXPECT_TRUE(base::PathExists(directory_in_tmp_)); | |
261 EXPECT_TRUE(base::PathExists(symlink_path_)); | |
262 | |
263 // Check that the symlink has been replaced. | |
264 base::FilePath socket_path; | |
265 ASSERT_TRUE(base::ReadSymbolicLink(symlink_path_, &socket_path)); | |
266 EXPECT_EQ(app_mode::kAppShimSocketShortName, socket_path.BaseName().value()); | |
270 } | 267 } |
271 | 268 |
272 } // namespace | 269 } // namespace |
OLD | NEW |