| 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 | 6 |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 8 |
| 12 TEST(ScopedHandleTest, ScopedHandle) { | 9 TEST(ScopedHandleTest, ScopedHandle) { |
| 13 // 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 |
| 14 // by ScopedHandle to avoid bug 528394. | 11 // by ScopedHandle to avoid bug 528394. |
| 15 const DWORD magic_error = 0x12345678; | 12 const DWORD magic_error = 0x12345678; |
| 16 | 13 |
| 17 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | 14 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); |
| 18 // Call SetLastError after creating the handle. | 15 // Call SetLastError after creating the handle. |
| 19 ::SetLastError(magic_error); | 16 ::SetLastError(magic_error); |
| 20 base::win::ScopedHandle handle_holder(handle); | 17 base::win::ScopedHandle handle_holder(handle); |
| 21 EXPECT_EQ(magic_error, ::GetLastError()); | 18 EXPECT_EQ(magic_error, ::GetLastError()); |
| 22 | 19 |
| 23 // Create a new handle and then set LastError again. | 20 // Create a new handle and then set LastError again. |
| 24 handle = ::CreateMutex(nullptr, FALSE, nullptr); | 21 handle = ::CreateMutex(nullptr, FALSE, nullptr); |
| 25 ::SetLastError(magic_error); | 22 ::SetLastError(magic_error); |
| 26 handle_holder.Set(handle); | 23 handle_holder.Set(handle); |
| 27 EXPECT_EQ(magic_error, ::GetLastError()); | 24 EXPECT_EQ(magic_error, ::GetLastError()); |
| 28 | 25 |
| 29 // Create a new handle and then set LastError again. | 26 // Create a new handle and then set LastError again. |
| 30 handle = ::CreateMutex(nullptr, FALSE, nullptr); | 27 handle = ::CreateMutex(nullptr, FALSE, nullptr); |
| 31 base::win::ScopedHandle handle_source(handle); | 28 base::win::ScopedHandle handle_source(handle); |
| 32 ::SetLastError(magic_error); | 29 ::SetLastError(magic_error); |
| 33 handle_holder = handle_source.Pass(); | 30 handle_holder = handle_source.Pass(); |
| 34 EXPECT_EQ(magic_error, ::GetLastError()); | 31 EXPECT_EQ(magic_error, ::GetLastError()); |
| 35 } | 32 } |
| 36 | |
| 37 TEST(ScopedHandleTest, ActiveVerifierCloseTracked) { | |
| 38 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | |
| 39 ASSERT_NE(HANDLE(NULL), handle); | |
| 40 ASSERT_DEATH({ | |
| 41 base::win::ScopedHandle handle_holder(handle); | |
| 42 // Calling CloseHandle on a tracked handle should crash. | |
| 43 ::CloseHandle(handle); | |
| 44 }, "CloseHandle called on tracked handle."); | |
| 45 } | |
| 46 | |
| 47 TEST(ScopedHandleTest, ActiveVerifierTrackedHasBeenClosed) { | |
| 48 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | |
| 49 ASSERT_NE(HANDLE(NULL), handle); | |
| 50 typedef NTSTATUS(WINAPI * NtCloseFunc)(HANDLE); | |
| 51 NtCloseFunc ntclose = reinterpret_cast<NtCloseFunc>( | |
| 52 GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtClose")); | |
| 53 ASSERT_NE(nullptr, ntclose); | |
| 54 | |
| 55 ASSERT_DEATH({ | |
| 56 base::win::ScopedHandle handle_holder(handle); | |
| 57 ntclose(handle); | |
| 58 // Destructing a ScopedHandle with an illegally closed handle should fail. | |
| 59 }, "CloseHandle failed."); | |
| 60 } | |
| 61 | |
| 62 TEST(ScopedHandleTest, ActiveVerifierDoubleTracking) { | |
| 63 HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr); | |
| 64 ASSERT_NE(HANDLE(NULL), handle); | |
| 65 | |
| 66 ASSERT_DEATH({ | |
| 67 base::win::ScopedHandle handle_holder(handle); | |
| 68 base::win::ScopedHandle handle_holder2(handle); | |
| 69 }, "Attempt to start tracking already tracked handle."); | |
| 70 } | |
| OLD | NEW |