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 TEST(ScopedHandleTest, ActiveVerifierCloseTracked) { | |
42 #if defined(_DEBUG) | |
43 // Handle hooks cause shutdown asserts in Debug on Windows 7. crbug.com/571304 | |
44 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
45 return; | |
46 #endif | |
47 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | |
48 ASSERT_NE(HANDLE(NULL), handle); | |
49 ASSERT_DEATH({ | |
50 base::win::ScopedHandle handle_holder(handle); | |
51 // Calling CloseHandle on a tracked handle should crash. | |
52 ::CloseHandle(handle); | |
53 }, "CloseHandle called on tracked handle."); | |
54 } | |
55 | |
56 TEST(ScopedHandleTest, ActiveVerifierTrackedHasBeenClosed) { | |
57 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | |
58 ASSERT_NE(HANDLE(NULL), handle); | |
59 typedef NTSTATUS(WINAPI * NtCloseFunc)(HANDLE); | |
60 NtCloseFunc ntclose = reinterpret_cast<NtCloseFunc>( | |
61 GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtClose")); | |
62 ASSERT_NE(nullptr, ntclose); | |
63 | |
64 ASSERT_DEATH({ | |
65 base::win::ScopedHandle handle_holder(handle); | |
66 ntclose(handle); | |
67 // Destructing a ScopedHandle with an illegally closed handle should fail. | |
68 }, "CloseHandle failed."); | |
69 } | |
70 | |
71 TEST(ScopedHandleTest, ActiveVerifierDoubleTracking) { | |
72 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | |
73 ASSERT_NE(HANDLE(NULL), handle); | |
74 | |
75 base::win::ScopedHandle handle_holder(handle); | |
76 | |
77 ASSERT_DEATH({ | |
78 base::win::ScopedHandle handle_holder2(handle); | |
79 }, "Attempt to start tracking already tracked handle."); | |
80 } | |
81 | |
82 TEST(ScopedHandleTest, ActiveVerifierWrongOwner) { | |
83 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | |
84 ASSERT_NE(HANDLE(NULL), handle); | |
85 | |
86 base::win::ScopedHandle handle_holder(handle); | |
87 ASSERT_DEATH({ | |
88 base::win::ScopedHandle handle_holder2; | |
89 handle_holder2.handle_ = handle; | |
90 }, "Attempting to close a handle not owned by opener."); | |
91 ASSERT_TRUE(handle_holder.IsValid()); | |
92 handle_holder.Close(); | |
93 } | |
94 | |
95 TEST(ScopedHandleTest, ActiveVerifierUntrackedHandle) { | |
96 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | |
97 ASSERT_NE(HANDLE(NULL), handle); | |
98 | |
99 ASSERT_DEATH({ | |
100 base::win::ScopedHandle handle_holder; | |
101 handle_holder.handle_ = handle; | |
102 }, "Attempting to close an untracked handle."); | |
103 | |
104 ASSERT_TRUE(::CloseHandle(handle)); | |
105 } | |
106 | |
107 } // namespace win | |
108 } // namespace base | |
OLD | NEW |