Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(440)

Unified Diff: base/win/scoped_handle_unittest.cc

Issue 1580873003: Enable handle verifier for tests and add some tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: do not copy variable up scope Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/win/scoped_handle.cc ('k') | build/common.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/win/scoped_handle_unittest.cc
diff --git a/base/win/scoped_handle_unittest.cc b/base/win/scoped_handle_unittest.cc
index b573b66450195bfc9f02193564d846b32fb160fe..b9663ef437c94db4c7458c607498f2fa5c5899e6 100644
--- a/base/win/scoped_handle_unittest.cc
+++ b/base/win/scoped_handle_unittest.cc
@@ -2,10 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <windows.h>
+#include <winternl.h>
+
#include "base/win/scoped_handle.h"
+#include "base/win/windows_version.h"
#include "testing/gtest/include/gtest/gtest.h"
+namespace base {
+namespace win {
+
TEST(ScopedHandleTest, ScopedHandle) {
// Any illegal error code will do. We just need to test that it is preserved
// by ScopedHandle to avoid bug 528394.
@@ -30,3 +37,75 @@ TEST(ScopedHandleTest, ScopedHandle) {
handle_holder = handle_source.Pass();
EXPECT_EQ(magic_error, ::GetLastError());
}
+
+// This test requires that the CloseHandle hook be in place.
+#if !defined(DISABLE_HANDLE_VERIFIER_HOOKS)
+TEST(ScopedHandleTest, ActiveVerifierCloseTracked) {
+#if defined(_DEBUG)
+ // Handle hooks cause shutdown asserts in Debug on Windows 7. crbug.com/571304
+ if (base::win::GetVersion() < base::win::VERSION_WIN8)
+ return;
+#endif
+ HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr);
+ ASSERT_NE(HANDLE(NULL), handle);
+ ASSERT_DEATH({
+ base::win::ScopedHandle handle_holder(handle);
+ // Calling CloseHandle on a tracked handle should crash.
+ ::CloseHandle(handle);
+ }, "CloseHandle called on tracked handle.");
+}
+#endif
+
+TEST(ScopedHandleTest, ActiveVerifierTrackedHasBeenClosed) {
+ HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr);
+ ASSERT_NE(HANDLE(NULL), handle);
+ typedef NTSTATUS(WINAPI * NtCloseFunc)(HANDLE);
+ NtCloseFunc ntclose = reinterpret_cast<NtCloseFunc>(
+ GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtClose"));
+ ASSERT_NE(nullptr, ntclose);
+
+ ASSERT_DEATH({
+ base::win::ScopedHandle handle_holder(handle);
+ ntclose(handle);
+ // Destructing a ScopedHandle with an illegally closed handle should fail.
+ }, "CloseHandle failed.");
+}
+
+TEST(ScopedHandleTest, ActiveVerifierDoubleTracking) {
+ HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr);
+ ASSERT_NE(HANDLE(NULL), handle);
+
+ base::win::ScopedHandle handle_holder(handle);
+
+ ASSERT_DEATH({
+ base::win::ScopedHandle handle_holder2(handle);
+ }, "Attempt to start tracking already tracked handle.");
+}
+
+TEST(ScopedHandleTest, ActiveVerifierWrongOwner) {
+ HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr);
+ ASSERT_NE(HANDLE(NULL), handle);
+
+ base::win::ScopedHandle handle_holder(handle);
+ ASSERT_DEATH({
+ base::win::ScopedHandle handle_holder2;
+ handle_holder2.handle_ = handle;
+ }, "Attempting to close a handle not owned by opener.");
+ ASSERT_TRUE(handle_holder.IsValid());
+ handle_holder.Close();
+}
+
+TEST(ScopedHandleTest, ActiveVerifierUntrackedHandle) {
+ HANDLE handle = ::CreateMutex(nullptr, FALSE, nullptr);
+ ASSERT_NE(HANDLE(NULL), handle);
+
+ ASSERT_DEATH({
+ base::win::ScopedHandle handle_holder;
+ handle_holder.handle_ = handle;
+ }, "Attempting to close an untracked handle.");
+
+ ASSERT_TRUE(::CloseHandle(handle));
+}
+
+} // namespace win
+} // namespace base
« no previous file with comments | « base/win/scoped_handle.cc ('k') | build/common.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698