| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "base/win_util.h" | 5 #include "base/win_util.h" |
| 6 #include "sandbox/src/sandbox.h" | 6 #include "sandbox/src/sandbox.h" |
| 7 #include "sandbox/src/sandbox_factory.h" | 7 #include "sandbox/src/sandbox_factory.h" |
| 8 #include "sandbox/src/sandbox_utils.h" | 8 #include "sandbox/src/sandbox_utils.h" |
| 9 #include "sandbox/src/target_services.h" | 9 #include "sandbox/src/target_services.h" |
| 10 #include "sandbox/tests/common/controller.h" | 10 #include "sandbox/tests/common/controller.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"PolicyTargetTest_process")) << | 158 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"PolicyTargetTest_process")) << |
| 159 "Opens a process"; | 159 "Opens a process"; |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Launches the app in the sandbox and ask it to wait in an | 162 // Launches the app in the sandbox and ask it to wait in an |
| 163 // infinite loop. Waits for 2 seconds and then check if the | 163 // infinite loop. Waits for 2 seconds and then check if the |
| 164 // desktop associated with the app thread is not the same as the | 164 // desktop associated with the app thread is not the same as the |
| 165 // current desktop. | 165 // current desktop. |
| 166 TEST(PolicyTargetTest, DesktopPolicy) { | 166 TEST(PolicyTargetTest, DesktopPolicy) { |
| 167 BrokerServices* broker = GetBroker(); | 167 BrokerServices* broker = GetBroker(); |
| 168 |
| 169 // Precreate the desktop. |
| 170 TargetPolicy* temp_policy = broker->CreatePolicy(); |
| 171 temp_policy->CreateAlternateDesktop(false); |
| 172 temp_policy->Release(); |
| 173 |
| 168 ASSERT_TRUE(broker != NULL); | 174 ASSERT_TRUE(broker != NULL); |
| 169 | 175 |
| 170 // Get the path to the sandboxed app. | 176 // Get the path to the sandboxed app. |
| 171 wchar_t prog_name[MAX_PATH]; | 177 wchar_t prog_name[MAX_PATH]; |
| 172 GetModuleFileNameW(NULL, prog_name, MAX_PATH); | 178 GetModuleFileNameW(NULL, prog_name, MAX_PATH); |
| 173 | 179 |
| 174 std::wstring arguments(L"\""); | 180 std::wstring arguments(L"\""); |
| 175 arguments += prog_name; | 181 arguments += prog_name; |
| 176 arguments += L"\" -child 0 wait"; // Don't care about the "state" argument. | 182 arguments += L"\" -child 0 wait"; // Don't care about the "state" argument. |
| 177 | 183 |
| 178 // Launch the app. | 184 // Launch the app. |
| 179 ResultCode result = SBOX_ALL_OK; | 185 ResultCode result = SBOX_ALL_OK; |
| 180 PROCESS_INFORMATION target = {0}; | 186 PROCESS_INFORMATION target = {0}; |
| 181 | 187 |
| 182 TargetPolicy* policy = broker->CreatePolicy(); | 188 TargetPolicy* policy = broker->CreatePolicy(); |
| 183 policy->SetDesktop(L"desktop_for_sbox"); | 189 policy->SetAlternateDesktop(false); |
| 184 policy->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); | 190 policy->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); |
| 185 result = broker->SpawnTarget(prog_name, arguments.c_str(), policy, &target); | 191 result = broker->SpawnTarget(prog_name, arguments.c_str(), policy, &target); |
| 186 policy->Release(); | 192 policy->Release(); |
| 187 | 193 |
| 188 EXPECT_EQ(SBOX_ALL_OK, result); | 194 EXPECT_EQ(SBOX_ALL_OK, result); |
| 189 | 195 |
| 190 EXPECT_EQ(1, ::ResumeThread(target.hThread)); | 196 EXPECT_EQ(1, ::ResumeThread(target.hThread)); |
| 191 | 197 |
| 192 EXPECT_EQ(WAIT_TIMEOUT, ::WaitForSingleObject(target.hProcess, 2000)); | 198 EXPECT_EQ(WAIT_TIMEOUT, ::WaitForSingleObject(target.hProcess, 2000)); |
| 193 | 199 |
| 194 EXPECT_NE(::GetThreadDesktop(target.dwThreadId), | 200 EXPECT_NE(::GetThreadDesktop(target.dwThreadId), |
| 195 ::GetThreadDesktop(::GetCurrentThreadId())); | 201 ::GetThreadDesktop(::GetCurrentThreadId())); |
| 196 | 202 |
| 197 HDESK desk = ::OpenDesktop(L"desktop_for_sbox", 0, FALSE, DESKTOP_ENUMERATE); | 203 std::wstring desktop_name = policy->GetAlternateDesktop(); |
| 204 HDESK desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); |
| 198 EXPECT_TRUE(NULL != desk); | 205 EXPECT_TRUE(NULL != desk); |
| 199 EXPECT_TRUE(::CloseDesktop(desk)); | 206 EXPECT_TRUE(::CloseDesktop(desk)); |
| 200 EXPECT_TRUE(::TerminateProcess(target.hProcess, 0)); | 207 EXPECT_TRUE(::TerminateProcess(target.hProcess, 0)); |
| 201 | 208 |
| 202 ::WaitForSingleObject(target.hProcess, INFINITE); | 209 ::WaitForSingleObject(target.hProcess, INFINITE); |
| 203 | 210 |
| 204 EXPECT_TRUE(::CloseHandle(target.hProcess)); | 211 EXPECT_TRUE(::CloseHandle(target.hProcess)); |
| 205 EXPECT_TRUE(::CloseHandle(target.hThread)); | 212 EXPECT_TRUE(::CloseHandle(target.hThread)); |
| 206 | 213 |
| 207 // Wait for the desktop to be deleted by the destructor of TargetProcess | 214 // Close the desktop handle. |
| 208 Sleep(2000); | 215 temp_policy = broker->CreatePolicy(); |
| 216 temp_policy->DestroyAlternateDesktop(); |
| 217 temp_policy->Release(); |
| 209 | 218 |
| 210 desk = ::OpenDesktop(L"desktop_for_sbox", 0, FALSE, DESKTOP_ENUMERATE); | 219 // Make sure the desktop does not exist anymore. |
| 220 desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); |
| 211 EXPECT_TRUE(NULL == desk); | 221 EXPECT_TRUE(NULL == desk); |
| 212 } | 222 } |
| 213 | 223 |
| 224 // Launches the app in the sandbox and ask it to wait in an |
| 225 // infinite loop. Waits for 2 seconds and then check if the |
| 226 // winstation associated with the app thread is not the same as the |
| 227 // current desktop. |
| 228 TEST(PolicyTargetTest, WinstaPolicy) { |
| 229 BrokerServices* broker = GetBroker(); |
| 230 |
| 231 // Precreate the desktop. |
| 232 TargetPolicy* temp_policy = broker->CreatePolicy(); |
| 233 temp_policy->CreateAlternateDesktop(true); |
| 234 temp_policy->Release(); |
| 235 |
| 236 ASSERT_TRUE(broker != NULL); |
| 237 |
| 238 // Get the path to the sandboxed app. |
| 239 wchar_t prog_name[MAX_PATH]; |
| 240 GetModuleFileNameW(NULL, prog_name, MAX_PATH); |
| 241 |
| 242 std::wstring arguments(L"\""); |
| 243 arguments += prog_name; |
| 244 arguments += L"\" -child 0 wait"; // Don't care about the "state" argument. |
| 245 |
| 246 // Launch the app. |
| 247 ResultCode result = SBOX_ALL_OK; |
| 248 PROCESS_INFORMATION target = {0}; |
| 249 |
| 250 TargetPolicy* policy = broker->CreatePolicy(); |
| 251 policy->SetAlternateDesktop(true); |
| 252 policy->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); |
| 253 result = broker->SpawnTarget(prog_name, arguments.c_str(), policy, &target); |
| 254 policy->Release(); |
| 255 |
| 256 EXPECT_EQ(SBOX_ALL_OK, result); |
| 257 |
| 258 EXPECT_EQ(1, ::ResumeThread(target.hThread)); |
| 259 |
| 260 EXPECT_EQ(WAIT_TIMEOUT, ::WaitForSingleObject(target.hProcess, 2000)); |
| 261 |
| 262 EXPECT_NE(::GetThreadDesktop(target.dwThreadId), |
| 263 ::GetThreadDesktop(::GetCurrentThreadId())); |
| 264 |
| 265 std::wstring desktop_name = policy->GetAlternateDesktop(); |
| 266 ASSERT_FALSE(desktop_name.empty()); |
| 267 |
| 268 // Make sure there is a backslash, for the window station name. |
| 269 EXPECT_NE(desktop_name.find_first_of(L'\\'), std::wstring::npos); |
| 270 |
| 271 // Isolate the desktop name. |
| 272 desktop_name = desktop_name.substr(desktop_name.find_first_of(L'\\') + 1); |
| 273 |
| 274 HDESK desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); |
| 275 // This should fail if the desktop is really on another window station. |
| 276 EXPECT_FALSE(NULL != desk); |
| 277 EXPECT_TRUE(::TerminateProcess(target.hProcess, 0)); |
| 278 |
| 279 ::WaitForSingleObject(target.hProcess, INFINITE); |
| 280 |
| 281 EXPECT_TRUE(::CloseHandle(target.hProcess)); |
| 282 EXPECT_TRUE(::CloseHandle(target.hThread)); |
| 283 |
| 284 // Close the desktop handle. |
| 285 temp_policy = broker->CreatePolicy(); |
| 286 temp_policy->DestroyAlternateDesktop(); |
| 287 temp_policy->Release(); |
| 288 } |
| 289 |
| 214 } // namespace sandbox | 290 } // namespace sandbox |
| OLD | NEW |