OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <windows.h> |
| 6 #include <winternl.h> |
| 7 |
5 #include "base/win/scoped_handle.h" | 8 #include "base/win/scoped_handle.h" |
| 9 #include "base/win/windows_version.h" |
6 | 10 |
7 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
8 | 12 |
| 13 namespace base { |
| 14 namespace win { |
| 15 |
9 TEST(ScopedHandleTest, ScopedHandle) { | 16 TEST(ScopedHandleTest, ScopedHandle) { |
10 // Any illegal error code will do. We just need to test that it is preserved | 17 // Any illegal error code will do. We just need to test that it is preserved |
11 // by ScopedHandle to avoid bug 528394. | 18 // by ScopedHandle to avoid bug 528394. |
12 const DWORD magic_error = 0x12345678; | 19 const DWORD magic_error = 0x12345678; |
13 | 20 |
14 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | 21 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); |
15 // Call SetLastError after creating the handle. | 22 // Call SetLastError after creating the handle. |
16 ::SetLastError(magic_error); | 23 ::SetLastError(magic_error); |
17 base::win::ScopedHandle handle_holder(handle); | 24 base::win::ScopedHandle handle_holder(handle); |
18 EXPECT_EQ(magic_error, ::GetLastError()); | 25 EXPECT_EQ(magic_error, ::GetLastError()); |
19 | 26 |
20 // Create a new handle and then set LastError again. | 27 // Create a new handle and then set LastError again. |
21 handle = ::CreateMutex(nullptr, FALSE, nullptr); | 28 handle = ::CreateMutex(nullptr, FALSE, nullptr); |
22 ::SetLastError(magic_error); | 29 ::SetLastError(magic_error); |
23 handle_holder.Set(handle); | 30 handle_holder.Set(handle); |
24 EXPECT_EQ(magic_error, ::GetLastError()); | 31 EXPECT_EQ(magic_error, ::GetLastError()); |
25 | 32 |
26 // Create a new handle and then set LastError again. | 33 // Create a new handle and then set LastError again. |
27 handle = ::CreateMutex(nullptr, FALSE, nullptr); | 34 handle = ::CreateMutex(nullptr, FALSE, nullptr); |
28 base::win::ScopedHandle handle_source(handle); | 35 base::win::ScopedHandle handle_source(handle); |
29 ::SetLastError(magic_error); | 36 ::SetLastError(magic_error); |
30 handle_holder = handle_source.Pass(); | 37 handle_holder = handle_source.Pass(); |
31 EXPECT_EQ(magic_error, ::GetLastError()); | 38 EXPECT_EQ(magic_error, ::GetLastError()); |
32 } | 39 } |
| 40 |
| 41 // This test requires that the CloseHandle hook be in place. |
| 42 #if !defined(DISABLE_HANDLE_VERIFIER_HOOKS) |
| 43 TEST(ScopedHandleTest, ActiveVerifierCloseTracked) { |
| 44 #if defined(_DEBUG) |
| 45 // Handle hooks cause shutdown asserts in Debug on Windows 7. crbug.com/571304 |
| 46 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 47 return; |
| 48 #endif |
| 49 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); |
| 50 ASSERT_NE(HANDLE(NULL), handle); |
| 51 ASSERT_DEATH({ |
| 52 base::win::ScopedHandle handle_holder(handle); |
| 53 // Calling CloseHandle on a tracked handle should crash. |
| 54 ::CloseHandle(handle); |
| 55 }, "CloseHandle called on tracked handle."); |
| 56 } |
| 57 #endif |
| 58 |
| 59 TEST(ScopedHandleTest, ActiveVerifierTrackedHasBeenClosed) { |
| 60 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); |
| 61 ASSERT_NE(HANDLE(NULL), handle); |
| 62 typedef NTSTATUS(WINAPI * NtCloseFunc)(HANDLE); |
| 63 NtCloseFunc ntclose = reinterpret_cast<NtCloseFunc>( |
| 64 GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtClose")); |
| 65 ASSERT_NE(nullptr, ntclose); |
| 66 |
| 67 ASSERT_DEATH({ |
| 68 base::win::ScopedHandle handle_holder(handle); |
| 69 ntclose(handle); |
| 70 // Destructing a ScopedHandle with an illegally closed handle should fail. |
| 71 }, "CloseHandle failed."); |
| 72 } |
| 73 |
| 74 TEST(ScopedHandleTest, ActiveVerifierDoubleTracking) { |
| 75 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); |
| 76 ASSERT_NE(HANDLE(NULL), handle); |
| 77 |
| 78 base::win::ScopedHandle handle_holder(handle); |
| 79 |
| 80 ASSERT_DEATH({ |
| 81 base::win::ScopedHandle handle_holder2(handle); |
| 82 }, "Attempt to start tracking already tracked handle."); |
| 83 } |
| 84 |
| 85 TEST(ScopedHandleTest, ActiveVerifierWrongOwner) { |
| 86 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); |
| 87 ASSERT_NE(HANDLE(NULL), handle); |
| 88 |
| 89 base::win::ScopedHandle handle_holder(handle); |
| 90 ASSERT_DEATH({ |
| 91 base::win::ScopedHandle handle_holder2; |
| 92 handle_holder2.handle_ = handle; |
| 93 }, "Attempting to close a handle not owned by opener."); |
| 94 ASSERT_TRUE(handle_holder.IsValid()); |
| 95 handle_holder.Close(); |
| 96 } |
| 97 |
| 98 TEST(ScopedHandleTest, ActiveVerifierUntrackedHandle) { |
| 99 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); |
| 100 ASSERT_NE(HANDLE(NULL), handle); |
| 101 |
| 102 ASSERT_DEATH({ |
| 103 base::win::ScopedHandle handle_holder; |
| 104 handle_holder.handle_ = handle; |
| 105 }, "Attempting to close an untracked handle."); |
| 106 |
| 107 ASSERT_TRUE(::CloseHandle(handle)); |
| 108 } |
| 109 |
| 110 } // namespace win |
| 111 } // namespace base |
OLD | NEW |