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 | |
8 #include "base/win/scoped_handle.h" | 5 #include "base/win/scoped_handle.h" |
9 #include "base/win/windows_version.h" | |
10 | 6 |
11 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
12 | 8 |
13 namespace base { | |
14 namespace win { | |
15 | |
16 TEST(ScopedHandleTest, ScopedHandle) { | 9 TEST(ScopedHandleTest, ScopedHandle) { |
17 // Any illegal error code will do. We just need to test that it is preserved | 10 // Any illegal error code will do. We just need to test that it is preserved |
18 // by ScopedHandle to avoid bug 528394. | 11 // by ScopedHandle to avoid bug 528394. |
19 const DWORD magic_error = 0x12345678; | 12 const DWORD magic_error = 0x12345678; |
20 | 13 |
21 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | 14 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); |
22 // Call SetLastError after creating the handle. | 15 // Call SetLastError after creating the handle. |
23 ::SetLastError(magic_error); | 16 ::SetLastError(magic_error); |
24 base::win::ScopedHandle handle_holder(handle); | 17 base::win::ScopedHandle handle_holder(handle); |
25 EXPECT_EQ(magic_error, ::GetLastError()); | 18 EXPECT_EQ(magic_error, ::GetLastError()); |
26 | 19 |
27 // Create a new handle and then set LastError again. | 20 // Create a new handle and then set LastError again. |
28 handle = ::CreateMutex(nullptr, FALSE, nullptr); | 21 handle = ::CreateMutex(nullptr, FALSE, nullptr); |
29 ::SetLastError(magic_error); | 22 ::SetLastError(magic_error); |
30 handle_holder.Set(handle); | 23 handle_holder.Set(handle); |
31 EXPECT_EQ(magic_error, ::GetLastError()); | 24 EXPECT_EQ(magic_error, ::GetLastError()); |
32 | 25 |
33 // Create a new handle and then set LastError again. | 26 // Create a new handle and then set LastError again. |
34 handle = ::CreateMutex(nullptr, FALSE, nullptr); | 27 handle = ::CreateMutex(nullptr, FALSE, nullptr); |
35 base::win::ScopedHandle handle_source(handle); | 28 base::win::ScopedHandle handle_source(handle); |
36 ::SetLastError(magic_error); | 29 ::SetLastError(magic_error); |
37 handle_holder = handle_source.Pass(); | 30 handle_holder = handle_source.Pass(); |
38 EXPECT_EQ(magic_error, ::GetLastError()); | 31 EXPECT_EQ(magic_error, ::GetLastError()); |
39 } | 32 } |
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 |