diff --git a/webrtc/base/base_tests.gyp b/webrtc/base/base_tests.gyp index bff20dbaab..ffb98c0be2 100644 --- a/webrtc/base/base_tests.gyp +++ b/webrtc/base/base_tests.gyp @@ -23,7 +23,6 @@ 'testbase64.h', 'testechoserver.h', 'testutils.h', - 'win32toolhelp.h', ], 'defines': [ 'GTEST_RELATIVE_PATH', @@ -129,7 +128,6 @@ 'win32_unittest.cc', 'win32regkey_unittest.cc', 'win32socketserver_unittest.cc', - 'win32toolhelp_unittest.cc', 'win32window_unittest.cc', 'win32windowpicker_unittest.cc', 'winfirewall_unittest.cc', diff --git a/webrtc/base/win32toolhelp.h b/webrtc/base/win32toolhelp.h deleted file mode 100644 index 2c69f58c85..0000000000 --- a/webrtc/base/win32toolhelp.h +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2010 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ -#ifndef WEBRTC_BASE_WIN32TOOLHELP_H_ -#define WEBRTC_BASE_WIN32TOOLHELP_H_ - -#if !defined(WEBRTC_WIN) -#error WEBRTC_WIN Only -#endif - -#include "webrtc/base/win32.h" - -// Should be included first, but that causes redefinitions. -#include - -#include "webrtc/base/constructormagic.h" - -namespace rtc { - -// The toolhelp api used to enumerate processes and their modules -// on Windows is very repetetive and clunky to use. This little -// template wraps it to make it a little more programmer friendly. -// -// Traits: Traits type that adapts the enumerator to the corresponding -// win32 toolhelp api. Each traits class need to: -// - define the type of the enumerated data as a public symbol Type -// -// - implement bool First(HANDLE, T*) normally calls a -// Xxxx32First method in the toolhelp API. Ex Process32First(...) -// -// - implement bool Next(HANDLE, T*) normally calls a -// Xxxx32Next method in the toolhelp API. Ex Process32Next(...) -// -// - implement bool CloseHandle(HANDLE) -// -template -class ToolhelpEnumeratorBase { - public: - ToolhelpEnumeratorBase(HANDLE snapshot) - : snapshot_(snapshot), broken_(false), first_(true) { - - // Clear out the Traits::Type structure instance. - Zero(¤t_); - } - - virtual ~ToolhelpEnumeratorBase() { - Close(); - } - - // Moves forward to the next object using the First and Next - // pointers. If either First or Next ever indicates an failure - // all subsequent calls to this method will fail; the enumerator - // object is considered broken. - bool Next() { - if (!Valid()) { - return false; - } - - // Move the iteration forward. - current_.dwSize = sizeof(typename Traits::Type); - bool incr_ok = false; - if (first_) { - incr_ok = Traits::First(snapshot_, ¤t_); - first_ = false; - } else { - incr_ok = Traits::Next(snapshot_, ¤t_); - } - - if (!incr_ok) { - Zero(¤t_); - broken_ = true; - } - - return incr_ok; - } - - const typename Traits::Type& current() const { - return current_; - } - - void Close() { - if (snapshot_ != INVALID_HANDLE_VALUE) { - Traits::CloseHandle(snapshot_); - snapshot_ = INVALID_HANDLE_VALUE; - } - } - - private: - // Checks the state of the snapshot handle. - bool Valid() { - return snapshot_ != INVALID_HANDLE_VALUE && !broken_; - } - - static void Zero(typename Traits::Type* buff) { - ZeroMemory(buff, sizeof(typename Traits::Type)); - } - - HANDLE snapshot_; - typename Traits::Type current_; - bool broken_; - bool first_; -}; - -class ToolhelpTraits { - public: - static HANDLE CreateSnapshot(uint32 flags, uint32 process_id) { - return CreateToolhelp32Snapshot(flags, process_id); - } - - static bool CloseHandle(HANDLE handle) { - return ::CloseHandle(handle) == TRUE; - } -}; - -class ToolhelpProcessTraits : public ToolhelpTraits { - public: - typedef PROCESSENTRY32 Type; - - static bool First(HANDLE handle, Type* t) { - return ::Process32First(handle, t) == TRUE; - } - - static bool Next(HANDLE handle, Type* t) { - return ::Process32Next(handle, t) == TRUE; - } -}; - -class ProcessEnumerator : public ToolhelpEnumeratorBase { - public: - ProcessEnumerator() - : ToolhelpEnumeratorBase( - ToolhelpProcessTraits::CreateSnapshot(TH32CS_SNAPPROCESS, 0)) { - } - - private: - DISALLOW_COPY_AND_ASSIGN(ProcessEnumerator); -}; - -class ToolhelpModuleTraits : public ToolhelpTraits { - public: - typedef MODULEENTRY32 Type; - - static bool First(HANDLE handle, Type* t) { - return ::Module32First(handle, t) == TRUE; - } - - static bool Next(HANDLE handle, Type* t) { - return ::Module32Next(handle, t) == TRUE; - } -}; - -class ModuleEnumerator : public ToolhelpEnumeratorBase { - public: - explicit ModuleEnumerator(uint32 process_id) - : ToolhelpEnumeratorBase( - ToolhelpModuleTraits::CreateSnapshot(TH32CS_SNAPMODULE, - process_id)) { - } - - private: - DISALLOW_COPY_AND_ASSIGN(ModuleEnumerator); -}; - -} // namespace rtc - -#endif // WEBRTC_BASE_WIN32TOOLHELP_H_ diff --git a/webrtc/base/win32toolhelp_unittest.cc b/webrtc/base/win32toolhelp_unittest.cc deleted file mode 100644 index 280f2ec98d..0000000000 --- a/webrtc/base/win32toolhelp_unittest.cc +++ /dev/null @@ -1,278 +0,0 @@ -/* - * Copyright 2010 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "webrtc/base/gunit.h" -#include "webrtc/base/pathutils.h" -#include "webrtc/base/scoped_ptr.h" -#include "webrtc/base/win32toolhelp.h" - -namespace rtc { - -typedef struct { - // Required to match the toolhelp api struct 'design'. - DWORD dwSize; - int a; - uint32 b; -} TestData; - -class Win32ToolhelpTest : public testing::Test { - public: - Win32ToolhelpTest() { - } - - HANDLE AsHandle() { - return reinterpret_cast(this); - } - - static Win32ToolhelpTest* AsFixture(HANDLE handle) { - return reinterpret_cast(handle); - } - - static bool First(HANDLE handle, TestData* d) { - Win32ToolhelpTest* tst = Win32ToolhelpTest::AsFixture(handle); - // This method should be called only once for every test. - // If it is called more than once it return false which - // should break the test. - EXPECT_EQ(0, tst->first_called_); // Just to be safe. - if (tst->first_called_ > 0) { - return false; - } - - *d = kTestData[0]; - tst->index_ = 1; - ++(tst->first_called_); - return true; - } - - static bool Next(HANDLE handle, TestData* d) { - Win32ToolhelpTest* tst = Win32ToolhelpTest::AsFixture(handle); - ++(tst->next_called_); - - if (tst->index_ >= kTestDataSize) { - return FALSE; - } - - *d = kTestData[tst->index_]; - ++(tst->index_); - return true; - } - - static bool Fail(HANDLE handle, TestData* d) { - Win32ToolhelpTest* tst = Win32ToolhelpTest::AsFixture(handle); - ++(tst->fail_called_); - return false; - } - - static bool CloseHandle(HANDLE handle) { - Win32ToolhelpTest* tst = Win32ToolhelpTest::AsFixture(handle); - ++(tst->close_handle_called_); - return true; - } - - protected: - virtual void SetUp() { - fail_called_ = 0; - first_called_ = 0; - next_called_ = 0; - close_handle_called_ = 0; - index_ = 0; - } - - static bool AllZero(const TestData& data) { - return data.dwSize == 0 && data.a == 0 && data.b == 0; - } - - static bool Equals(const TestData& expected, const TestData& actual) { - return expected.dwSize == actual.dwSize - && expected.a == actual.a - && expected.b == actual.b; - } - - bool CheckCallCounters(int first, int next, int fail, int close) { - bool match = first_called_ == first && next_called_ == next - && fail_called_ == fail && close_handle_called_ == close; - - if (!match) { - LOG(LS_ERROR) << "Expected: (" - << first << ", " - << next << ", " - << fail << ", " - << close << ")"; - - LOG(LS_ERROR) << "Actual: (" - << first_called_ << ", " - << next_called_ << ", " - << fail_called_ << ", " - << close_handle_called_ << ")"; - } - return match; - } - - static const int kTestDataSize = 3; - static const TestData kTestData[]; - int index_; - int first_called_; - int fail_called_; - int next_called_; - int close_handle_called_; -}; - -const TestData Win32ToolhelpTest::kTestData[] = { - {1, 1, 1}, {2, 2, 2}, {3, 3, 3} -}; - - -class TestTraits { - public: - typedef TestData Type; - - static bool First(HANDLE handle, Type* t) { - return Win32ToolhelpTest::First(handle, t); - } - - static bool Next(HANDLE handle, Type* t) { - return Win32ToolhelpTest::Next(handle, t); - } - - static bool CloseHandle(HANDLE handle) { - return Win32ToolhelpTest::CloseHandle(handle); - } -}; - -class BadFirstTraits { - public: - typedef TestData Type; - - static bool First(HANDLE handle, Type* t) { - return Win32ToolhelpTest::Fail(handle, t); - } - - static bool Next(HANDLE handle, Type* t) { - // This should never be called. - ADD_FAILURE(); - return false; - } - - static bool CloseHandle(HANDLE handle) { - return Win32ToolhelpTest::CloseHandle(handle); - } -}; - -class BadNextTraits { - public: - typedef TestData Type; - - static bool First(HANDLE handle, Type* t) { - return Win32ToolhelpTest::First(handle, t); - } - - static bool Next(HANDLE handle, Type* t) { - return Win32ToolhelpTest::Fail(handle, t); - } - - static bool CloseHandle(HANDLE handle) { - return Win32ToolhelpTest::CloseHandle(handle); - } -}; - -// The toolhelp in normally inherited but most of -// these tests only excercise the methods from the -// traits therefore I use a typedef to make the -// test code easier to read. -typedef rtc::ToolhelpEnumeratorBase EnumeratorForTest; - -TEST_F(Win32ToolhelpTest, TestNextWithInvalidCtorHandle) { - EnumeratorForTest t(INVALID_HANDLE_VALUE); - - EXPECT_FALSE(t.Next()); - EXPECT_TRUE(CheckCallCounters(0, 0, 0, 0)); -} - -// Tests that Next() returns false if the first-pointer -// function fails. -TEST_F(Win32ToolhelpTest, TestNextFirstFails) { - typedef rtc::ToolhelpEnumeratorBase BadEnumerator; - rtc::scoped_ptr t(new BadEnumerator(AsHandle())); - - // If next ever fails it shall always fail. - EXPECT_FALSE(t->Next()); - EXPECT_FALSE(t->Next()); - EXPECT_FALSE(t->Next()); - t.reset(); - EXPECT_TRUE(CheckCallCounters(0, 0, 1, 1)); -} - -// Tests that Next() returns false if the next-pointer -// function fails. -TEST_F(Win32ToolhelpTest, TestNextNextFails) { - typedef rtc::ToolhelpEnumeratorBase BadEnumerator; - rtc::scoped_ptr t(new BadEnumerator(AsHandle())); - - // If next ever fails it shall always fail. No more calls - // shall be dispatched to Next(...). - EXPECT_TRUE(t->Next()); - EXPECT_FALSE(t->Next()); - EXPECT_FALSE(t->Next()); - t.reset(); - EXPECT_TRUE(CheckCallCounters(1, 0, 1, 1)); -} - - -// Tests that current returns an object is all zero's -// if Next() hasn't been called. -TEST_F(Win32ToolhelpTest, TestCurrentNextNotCalled) { - rtc::scoped_ptr t(new EnumeratorForTest(AsHandle())); - EXPECT_TRUE(AllZero(t->current())); - t.reset(); - EXPECT_TRUE(CheckCallCounters(0, 0, 0, 1)); -} - -// Tests the simple everything works path through the code. -TEST_F(Win32ToolhelpTest, TestCurrentNextCalled) { - rtc::scoped_ptr t(new EnumeratorForTest(AsHandle())); - - EXPECT_TRUE(t->Next()); - EXPECT_TRUE(Equals(t->current(), kTestData[0])); - EXPECT_TRUE(t->Next()); - EXPECT_TRUE(Equals(t->current(), kTestData[1])); - EXPECT_TRUE(t->Next()); - EXPECT_TRUE(Equals(t->current(), kTestData[2])); - EXPECT_FALSE(t->Next()); - t.reset(); - EXPECT_TRUE(CheckCallCounters(1, 3, 0, 1)); -} - -TEST_F(Win32ToolhelpTest, TestCurrentProcess) { - WCHAR buf[MAX_PATH]; - GetModuleFileName(NULL, buf, ARRAY_SIZE(buf)); - std::wstring name = ToUtf16(Pathname(ToUtf8(buf)).filename()); - - rtc::ProcessEnumerator processes; - bool found = false; - while (processes.Next()) { - if (!name.compare(processes.current().szExeFile)) { - found = true; - break; - } - } - EXPECT_TRUE(found); - - rtc::ModuleEnumerator modules(processes.current().th32ProcessID); - found = false; - while (modules.Next()) { - if (!name.compare(modules.current().szModule)) { - found = true; - break; - } - } - EXPECT_TRUE(found); -} - -} // namespace rtc