diff options
author | jschuh@chromium.org <jschuh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-24 22:17:06 +0000 |
---|---|---|
committer | jschuh@chromium.org <jschuh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-24 22:17:06 +0000 |
commit | 284dadf39378ae2ef6b18edce0a7b4f54364f9a0 (patch) | |
tree | 538aaaa212067f195cb138d0d5a8322aea7d0d50 /sandbox/src/handle_table_unittest.cc | |
parent | 1f751e27b7e0153f718564989bd9064b461e8ed2 (diff) | |
download | chromium_src-284dadf39378ae2ef6b18edce0a7b4f54364f9a0.zip chromium_src-284dadf39378ae2ef6b18edce0a7b4f54364f9a0.tar.gz chromium_src-284dadf39378ae2ef6b18edce0a7b4f54364f9a0.tar.bz2 |
Factor Windows handle enumeration code into its own classesI did some general cleanup and isolated out the handle enumeration.
TEST=sbox_unittests.exe --gtest_filter=HandleTable.*
BUG=86521
Review URL: http://codereview.chromium.org/7206007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90449 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sandbox/src/handle_table_unittest.cc')
-rw-r--r-- | sandbox/src/handle_table_unittest.cc | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/sandbox/src/handle_table_unittest.cc b/sandbox/src/handle_table_unittest.cc new file mode 100644 index 0000000..696037f --- /dev/null +++ b/sandbox/src/handle_table_unittest.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <windows.h> + +#include "sandbox/src/handle_table.h" +#include "sandbox/tests/common/test_utils.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(HandleTable, IsTableValid) { + using sandbox::HandleTable; + const ULONG my_process_id = ::GetCurrentProcessId(); + ULONG last_process_id = 0; + ULONG total_handles = 0; + bool found_my_process = false; + bool found_other_process = false; + + HandleTable handles; + EXPECT_NE(0u, handles.handle_info()->NumberOfHandles); + + for (HandleTable::Iterator it = handles.begin(); it != handles.end(); ++it) { + ULONG process_id = it->handle_entry()->ProcessId; + bool is_current_process = process_id == my_process_id; + found_my_process |= is_current_process; + found_other_process |= !is_current_process; + + EXPECT_LE(last_process_id, process_id); + last_process_id = process_id; + total_handles++; + } + + EXPECT_EQ(handles.handle_info()->NumberOfHandles, total_handles); + EXPECT_TRUE(found_my_process); + EXPECT_TRUE(found_other_process); +} + +TEST(HandleTable, FindHandle) { + using sandbox::HandleTable; + + // Create a temp file so we have a handle to find. + wchar_t temp_directory[MAX_PATH]; + wchar_t my_file[MAX_PATH]; + ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); + ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_file), 0u); + HANDLE file = ::CreateFile(my_file, FILE_ALL_ACCESS, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, + OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL); + EXPECT_NE(INVALID_HANDLE_VALUE, file); + + // Look for the handle in our process + bool handle_found = false; + HandleTable handles; + for (HandleTable::Iterator it = + handles.HandlesForProcess(::GetCurrentProcessId()); + it != handles.end(); ++it) { + if (it->IsType(HandleTable::kTypeFile) && it->Name().compare(my_file)) { + handle_found = true; + break; + } + } + EXPECT_TRUE(handle_found); + + // Clean up the file we made. + EXPECT_TRUE(::CloseHandle(file)); +} |