| 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 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 | 
|---|