| OLD | NEW |
| 1 // Copyright (c) 2010 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 // This file contains unit tests for the job object. | 5 // This file contains unit tests for the job object. |
| 6 | 6 |
| 7 #include "base/win/scoped_process_information.h" |
| 7 #include "sandbox/src/job.h" | 8 #include "sandbox/src/job.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 10 |
| 10 namespace sandbox { | 11 namespace sandbox { |
| 11 | 12 |
| 12 // Tests the creation and destruction of the job. | 13 // Tests the creation and destruction of the job. |
| 13 TEST(JobTest, TestCreation) { | 14 TEST(JobTest, TestCreation) { |
| 14 // Scope the creation of Job. | 15 // Scope the creation of Job. |
| 15 { | 16 { |
| 16 // Create the job. | 17 // Create the job. |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 // Tests the method "AssignProcessToJob". | 153 // Tests the method "AssignProcessToJob". |
| 153 TEST(JobTest, ProcessInJob) { | 154 TEST(JobTest, ProcessInJob) { |
| 154 // Create the job. | 155 // Create the job. |
| 155 Job job; | 156 Job job; |
| 156 ASSERT_EQ(ERROR_SUCCESS, job.Init(JOB_UNPROTECTED, L"job_test_process", 0)); | 157 ASSERT_EQ(ERROR_SUCCESS, job.Init(JOB_UNPROTECTED, L"job_test_process", 0)); |
| 157 | 158 |
| 158 BOOL result = FALSE; | 159 BOOL result = FALSE; |
| 159 | 160 |
| 160 wchar_t notepad[] = L"notepad"; | 161 wchar_t notepad[] = L"notepad"; |
| 161 STARTUPINFO si = { sizeof(si) }; | 162 STARTUPINFO si = { sizeof(si) }; |
| 162 PROCESS_INFORMATION pi = {0}; | 163 base::win::ScopedProcessInformation pi; |
| 163 result = ::CreateProcess(NULL, notepad, NULL, NULL, FALSE, 0, NULL, NULL, &si, | 164 result = ::CreateProcess(NULL, notepad, NULL, NULL, FALSE, 0, NULL, NULL, &si, |
| 164 &pi); | 165 pi.Receive()); |
| 165 ASSERT_TRUE(result); | 166 ASSERT_TRUE(result); |
| 166 ASSERT_EQ(ERROR_SUCCESS, job.AssignProcessToJob(pi.hProcess)); | 167 ASSERT_EQ(ERROR_SUCCESS, job.AssignProcessToJob(pi.process_handle())); |
| 167 | 168 |
| 168 // Get the job handle. | 169 // Get the job handle. |
| 169 HANDLE job_handle = job.Detach(); | 170 HANDLE job_handle = job.Detach(); |
| 170 | 171 |
| 171 // Check if the process is in the job. | 172 // Check if the process is in the job. |
| 172 JOBOBJECT_BASIC_PROCESS_ID_LIST jbpidl = {0}; | 173 JOBOBJECT_BASIC_PROCESS_ID_LIST jbpidl = {0}; |
| 173 DWORD size = sizeof(jbpidl); | 174 DWORD size = sizeof(jbpidl); |
| 174 result = ::QueryInformationJobObject(job_handle, | 175 result = ::QueryInformationJobObject(job_handle, |
| 175 JobObjectBasicProcessIdList, | 176 JobObjectBasicProcessIdList, |
| 176 &jbpidl, size, &size); | 177 &jbpidl, size, &size); |
| 177 EXPECT_TRUE(result); | 178 EXPECT_TRUE(result); |
| 178 | 179 |
| 179 EXPECT_EQ(1, jbpidl.NumberOfAssignedProcesses); | 180 EXPECT_EQ(1, jbpidl.NumberOfAssignedProcesses); |
| 180 EXPECT_EQ(1, jbpidl.NumberOfProcessIdsInList); | 181 EXPECT_EQ(1, jbpidl.NumberOfProcessIdsInList); |
| 181 EXPECT_EQ(pi.dwProcessId, jbpidl.ProcessIdList[0]); | 182 EXPECT_EQ(pi.process_id(), jbpidl.ProcessIdList[0]); |
| 182 | 183 |
| 183 EXPECT_TRUE(::TerminateProcess(pi.hProcess, 0)); | 184 EXPECT_TRUE(::TerminateProcess(pi.process_handle(), 0)); |
| 184 | 185 |
| 185 EXPECT_TRUE(::CloseHandle(job_handle)); | 186 EXPECT_TRUE(::CloseHandle(job_handle)); |
| 186 } | 187 } |
| 187 | 188 |
| 188 } // namespace sandbox | 189 } // namespace sandbox |
| OLD | NEW |