Chromium Code Reviews| 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::kAppShimSocketName); | |
| 61 | |
| 62 base::FilePath socket_path; | |
| 63 ASSERT_TRUE(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_; | 117 base::ScopedTempDir short_temp_dir_; |
| 111 | 118 |
| 112 int launch_count_; | 119 int launch_count_; |
| 113 int quit_count_; | 120 int quit_count_; |
| 114 | 121 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 141 | 148 |
| 142 void AppShimHostManagerBrowserTest::SetUpOnMainThread() { | 149 void AppShimHostManagerBrowserTest::SetUpOnMainThread() { |
| 143 // Can't do this in the constructor, it needs a BrowserProcess. | 150 // Can't do this in the constructor, it needs a BrowserProcess. |
| 144 apps::AppShimHandler::RegisterHandler(kTestAppMode, this); | 151 apps::AppShimHandler::RegisterHandler(kTestAppMode, this); |
| 145 } | 152 } |
| 146 | 153 |
| 147 void AppShimHostManagerBrowserTest::TearDownOnMainThread() { | 154 void AppShimHostManagerBrowserTest::TearDownOnMainThread() { |
| 148 apps::AppShimHandler::RemoveHandler(kTestAppMode); | 155 apps::AppShimHandler::RemoveHandler(kTestAppMode); |
| 149 } | 156 } |
| 150 | 157 |
| 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( | 158 void AppShimHostManagerBrowserTest::OnShimLaunch( |
| 172 apps::AppShimHandler::Host* host, | 159 apps::AppShimHandler::Host* host, |
| 173 apps::AppShimLaunchType launch_type, | 160 apps::AppShimLaunchType launch_type, |
| 174 const std::vector<base::FilePath>& files) { | 161 const std::vector<base::FilePath>& files) { |
| 175 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS); | 162 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS); |
| 176 ++launch_count_; | 163 ++launch_count_; |
| 177 last_launch_type_ = launch_type; | 164 last_launch_type_ = launch_type; |
| 178 last_launch_files_ = files; | 165 last_launch_files_ = files; |
| 179 runner_->Quit(); | 166 runner_->Quit(); |
| 180 } | 167 } |
| 181 | 168 |
| 182 void AppShimHostManagerBrowserTest::OnShimQuit( | 169 void AppShimHostManagerBrowserTest::OnShimQuit( |
| 183 apps::AppShimHandler::Host* host) { | 170 apps::AppShimHandler::Host* host) { |
| 184 ++quit_count_; | 171 ++quit_count_; |
| 185 runner_->Quit(); | 172 runner_->Quit(); |
| 186 } | 173 } |
| 187 | 174 |
| 188 // Test regular launch, which would ask Chrome to launch the app. | 175 // Test regular launch, which would ask Chrome to launch the app. |
| 189 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) { | 176 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) { |
| 190 test_client_.reset(new TestShimClient(short_socket_path_)); | 177 test_client_.reset(new TestShimClient()); |
| 191 test_client_->Send(new AppShimHostMsg_LaunchApp( | 178 test_client_->Send(new AppShimHostMsg_LaunchApp( |
| 192 browser()->profile()->GetPath(), | 179 browser()->profile()->GetPath(), |
| 193 kTestAppMode, | 180 kTestAppMode, |
| 194 apps::APP_SHIM_LAUNCH_NORMAL, | 181 apps::APP_SHIM_LAUNCH_NORMAL, |
| 195 std::vector<base::FilePath>())); | 182 std::vector<base::FilePath>())); |
| 196 | 183 |
| 197 RunAndExitGracefully(); | 184 RunAndExitGracefully(); |
| 198 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_); | 185 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_); |
| 199 EXPECT_TRUE(last_launch_files_.empty()); | 186 EXPECT_TRUE(last_launch_files_.empty()); |
| 200 } | 187 } |
| 201 | 188 |
| 202 // Test register-only launch, used when Chrome has already launched the app. | 189 // Test register-only launch, used when Chrome has already launched the app. |
| 203 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) { | 190 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) { |
| 204 test_client_.reset(new TestShimClient(short_socket_path_)); | 191 test_client_.reset(new TestShimClient()); |
| 205 test_client_->Send(new AppShimHostMsg_LaunchApp( | 192 test_client_->Send(new AppShimHostMsg_LaunchApp( |
| 206 browser()->profile()->GetPath(), | 193 browser()->profile()->GetPath(), |
| 207 kTestAppMode, | 194 kTestAppMode, |
| 208 apps::APP_SHIM_LAUNCH_REGISTER_ONLY, | 195 apps::APP_SHIM_LAUNCH_REGISTER_ONLY, |
| 209 std::vector<base::FilePath>())); | 196 std::vector<base::FilePath>())); |
| 210 | 197 |
| 211 RunAndExitGracefully(); | 198 RunAndExitGracefully(); |
| 212 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_); | 199 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_); |
| 213 EXPECT_TRUE(last_launch_files_.empty()); | 200 EXPECT_TRUE(last_launch_files_.empty()); |
| 214 } | 201 } |
| 215 | 202 |
| 216 // Ensure the domain socket can be created in a fresh user data dir. | 203 // Ensure the domain socket can be created in a fresh user data dir. |
| 217 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, | 204 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, |
| 218 PRE_ReCreate) { | 205 PRE_ReCreate) { |
| 219 test::AppShimHostManagerTestApi test_api( | 206 test::AppShimHostManagerTestApi test_api( |
| 220 g_browser_process->platform_part()->app_shim_host_manager()); | 207 g_browser_process->platform_part()->app_shim_host_manager()); |
| 221 EXPECT_TRUE(test_api.factory()); | 208 EXPECT_TRUE(test_api.factory()); |
| 222 } | 209 } |
| 223 | 210 |
| 224 // Ensure the domain socket can be re-created after a prior browser process has | 211 // Ensure the domain socket can be re-created after a prior browser process has |
| 225 // quit. | 212 // quit. |
| 226 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, | 213 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, |
| 227 ReCreate) { | 214 ReCreate) { |
| 228 test::AppShimHostManagerTestApi test_api( | 215 test::AppShimHostManagerTestApi test_api( |
| 229 g_browser_process->platform_part()->app_shim_host_manager()); | 216 g_browser_process->platform_part()->app_shim_host_manager()); |
| 230 EXPECT_TRUE(test_api.factory()); | 217 EXPECT_TRUE(test_api.factory()); |
| 231 } | 218 } |
| 232 | 219 |
| 233 // Test for AppShimHostManager that fails to create the socket. | 220 // Test where a symlink already exists in the UDD. |
|
tapted
2014/01/03 13:06:54
nit: maybe expand `UDD` -> `user data dir` - I thi
jackhou1
2014/01/06 05:29:52
Done.
| |
| 234 class AppShimHostManagerBrowserTestFailsCreate : | 221 class AppShimHostManagerBrowserTestExistingSymlink : |
| 235 public AppShimHostManagerBrowserTest { | 222 public AppShimHostManagerBrowserTest { |
| 236 public: | 223 public: |
| 237 AppShimHostManagerBrowserTestFailsCreate() {} | 224 AppShimHostManagerBrowserTestExistingSymlink() {} |
| 238 | 225 |
| 239 private: | 226 private: |
| 240 virtual bool SetUpUserDataDirectory() OVERRIDE; | 227 virtual bool SetUpUserDataDirectory() OVERRIDE; |
| 241 | 228 |
| 242 base::ScopedTempDir barrier_dir_; | 229 base::ScopedTempDir barrier_dir_; |
| 243 | 230 |
| 244 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTestFailsCreate); | 231 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTestExistingSymlink); |
| 245 }; | 232 }; |
| 246 | 233 |
| 247 bool AppShimHostManagerBrowserTestFailsCreate::SetUpUserDataDirectory() { | 234 bool AppShimHostManagerBrowserTestExistingSymlink::SetUpUserDataDirectory() { |
| 248 base::FilePath user_data_dir; | 235 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)); | 236 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); |
| 254 base::FilePath socket_path = | 237 base::FilePath symlink_path = |
| 255 user_data_dir.Append(app_mode::kAppShimSocketName); | 238 user_data_dir.Append(app_mode::kAppShimSocketName); |
| 256 // Create a "barrier" to forming the UNIX domain socket. This is just a | 239 EXPECT_TRUE(base::CreateSymbolicLink(base::FilePath("/tmp"), symlink_path)); |
| 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(); | 240 return AppShimHostManagerBrowserTest::SetUpUserDataDirectory(); |
| 261 } | 241 } |
| 262 | 242 |
| 263 // Test error handling. This is essentially testing for lifetime correctness | 243 // Test that existing symlinks are deleted. |
|
tapted
2014/01/03 13:06:54
Can this be double-checked in ~AppShimHostManagerB
jackhou1
2014/01/06 05:29:52
This checks that an existing file at the symlink p
tapted
2014/01/06 07:31:28
Is it possible to have a regression test for Chrom
jackhou1
2014/01/07 05:24:36
Done.
| |
| 264 // during startup for unexpected failures. | 244 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestExistingSymlink, |
| 265 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestFailsCreate, | 245 DeletesSymlink) { |
| 266 SocketFailure) { | |
| 267 test::AppShimHostManagerTestApi test_api( | 246 test::AppShimHostManagerTestApi test_api( |
| 268 g_browser_process->platform_part()->app_shim_host_manager()); | 247 g_browser_process->platform_part()->app_shim_host_manager()); |
| 269 EXPECT_FALSE(test_api.factory()); | 248 EXPECT_TRUE(test_api.factory()); |
| 270 } | 249 } |
| 271 | 250 |
| 272 } // namespace | 251 } // namespace |
| OLD | NEW |