You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
8.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2010, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/gunit.h"
  28. #include "talk/base/pathutils.h"
  29. #include "talk/base/scoped_ptr.h"
  30. #include "talk/base/win32toolhelp.h"
  31. namespace talk_base {
  32. typedef struct {
  33. // Required to match the toolhelp api struct 'design'.
  34. DWORD dwSize;
  35. int a;
  36. uint32 b;
  37. } TestData;
  38. class Win32ToolhelpTest : public testing::Test {
  39. public:
  40. Win32ToolhelpTest() {
  41. }
  42. HANDLE AsHandle() {
  43. return reinterpret_cast<HANDLE>(this);
  44. }
  45. static Win32ToolhelpTest* AsFixture(HANDLE handle) {
  46. return reinterpret_cast<Win32ToolhelpTest*>(handle);
  47. }
  48. static bool First(HANDLE handle, TestData* d) {
  49. Win32ToolhelpTest* tst = Win32ToolhelpTest::AsFixture(handle);
  50. // This method should be called only once for every test.
  51. // If it is called more than once it return false which
  52. // should break the test.
  53. EXPECT_EQ(0, tst->first_called_); // Just to be safe.
  54. if (tst->first_called_ > 0) {
  55. return false;
  56. }
  57. *d = kTestData[0];
  58. tst->index_ = 1;
  59. ++(tst->first_called_);
  60. return true;
  61. }
  62. static bool Next(HANDLE handle, TestData* d) {
  63. Win32ToolhelpTest* tst = Win32ToolhelpTest::AsFixture(handle);
  64. ++(tst->next_called_);
  65. if (tst->index_ >= kTestDataSize) {
  66. return FALSE;
  67. }
  68. *d = kTestData[tst->index_];
  69. ++(tst->index_);
  70. return true;
  71. }
  72. static bool Fail(HANDLE handle, TestData* d) {
  73. Win32ToolhelpTest* tst = Win32ToolhelpTest::AsFixture(handle);
  74. ++(tst->fail_called_);
  75. return false;
  76. }
  77. static bool CloseHandle(HANDLE handle) {
  78. Win32ToolhelpTest* tst = Win32ToolhelpTest::AsFixture(handle);
  79. ++(tst->close_handle_called_);
  80. return true;
  81. }
  82. protected:
  83. virtual void SetUp() {
  84. fail_called_ = 0;
  85. first_called_ = 0;
  86. next_called_ = 0;
  87. close_handle_called_ = 0;
  88. index_ = 0;
  89. }
  90. static bool AllZero(const TestData& data) {
  91. return data.dwSize == 0 && data.a == 0 && data.b == 0;
  92. }
  93. static bool Equals(const TestData& expected, const TestData& actual) {
  94. return expected.dwSize == actual.dwSize
  95. && expected.a == actual.a
  96. && expected.b == actual.b;
  97. }
  98. bool CheckCallCounters(int first, int next, int fail, int close) {
  99. bool match = first_called_ == first && next_called_ == next
  100. && fail_called_ == fail && close_handle_called_ == close;
  101. if (!match) {
  102. LOG(LS_ERROR) << "Expected: ("
  103. << first << ", "
  104. << next << ", "
  105. << fail << ", "
  106. << close << ")";
  107. LOG(LS_ERROR) << "Actual: ("
  108. << first_called_ << ", "
  109. << next_called_ << ", "
  110. << fail_called_ << ", "
  111. << close_handle_called_ << ")";
  112. }
  113. return match;
  114. }
  115. static const int kTestDataSize = 3;
  116. static const TestData kTestData[];
  117. int index_;
  118. int first_called_;
  119. int fail_called_;
  120. int next_called_;
  121. int close_handle_called_;
  122. };
  123. const TestData Win32ToolhelpTest::kTestData[] = {
  124. {1, 1, 1}, {2, 2, 2}, {3, 3, 3}
  125. };
  126. class TestTraits {
  127. public:
  128. typedef TestData Type;
  129. static bool First(HANDLE handle, Type* t) {
  130. return Win32ToolhelpTest::First(handle, t);
  131. }
  132. static bool Next(HANDLE handle, Type* t) {
  133. return Win32ToolhelpTest::Next(handle, t);
  134. }
  135. static bool CloseHandle(HANDLE handle) {
  136. return Win32ToolhelpTest::CloseHandle(handle);
  137. }
  138. };
  139. class BadFirstTraits {
  140. public:
  141. typedef TestData Type;
  142. static bool First(HANDLE handle, Type* t) {
  143. return Win32ToolhelpTest::Fail(handle, t);
  144. }
  145. static bool Next(HANDLE handle, Type* t) {
  146. // This should never be called.
  147. ADD_FAILURE();
  148. return false;
  149. }
  150. static bool CloseHandle(HANDLE handle) {
  151. return Win32ToolhelpTest::CloseHandle(handle);
  152. }
  153. };
  154. class BadNextTraits {
  155. public:
  156. typedef TestData Type;
  157. static bool First(HANDLE handle, Type* t) {
  158. return Win32ToolhelpTest::First(handle, t);
  159. }
  160. static bool Next(HANDLE handle, Type* t) {
  161. return Win32ToolhelpTest::Fail(handle, t);
  162. }
  163. static bool CloseHandle(HANDLE handle) {
  164. return Win32ToolhelpTest::CloseHandle(handle);
  165. }
  166. };
  167. // The toolhelp in normally inherited but most of
  168. // these tests only excercise the methods from the
  169. // traits therefore I use a typedef to make the
  170. // test code easier to read.
  171. typedef talk_base::ToolhelpEnumeratorBase<TestTraits> EnumeratorForTest;
  172. TEST_F(Win32ToolhelpTest, TestNextWithInvalidCtorHandle) {
  173. EnumeratorForTest t(INVALID_HANDLE_VALUE);
  174. EXPECT_FALSE(t.Next());
  175. EXPECT_TRUE(CheckCallCounters(0, 0, 0, 0));
  176. }
  177. // Tests that Next() returns false if the first-pointer
  178. // function fails.
  179. TEST_F(Win32ToolhelpTest, TestNextFirstFails) {
  180. typedef talk_base::ToolhelpEnumeratorBase<BadFirstTraits> BadEnumerator;
  181. talk_base::scoped_ptr<BadEnumerator> t(new BadEnumerator(AsHandle()));
  182. // If next ever fails it shall always fail.
  183. EXPECT_FALSE(t->Next());
  184. EXPECT_FALSE(t->Next());
  185. EXPECT_FALSE(t->Next());
  186. t.reset();
  187. EXPECT_TRUE(CheckCallCounters(0, 0, 1, 1));
  188. }
  189. // Tests that Next() returns false if the next-pointer
  190. // function fails.
  191. TEST_F(Win32ToolhelpTest, TestNextNextFails) {
  192. typedef talk_base::ToolhelpEnumeratorBase<BadNextTraits> BadEnumerator;
  193. talk_base::scoped_ptr<BadEnumerator> t(new BadEnumerator(AsHandle()));
  194. // If next ever fails it shall always fail. No more calls
  195. // shall be dispatched to Next(...).
  196. EXPECT_TRUE(t->Next());
  197. EXPECT_FALSE(t->Next());
  198. EXPECT_FALSE(t->Next());
  199. t.reset();
  200. EXPECT_TRUE(CheckCallCounters(1, 0, 1, 1));
  201. }
  202. // Tests that current returns an object is all zero's
  203. // if Next() hasn't been called.
  204. TEST_F(Win32ToolhelpTest, TestCurrentNextNotCalled) {
  205. talk_base::scoped_ptr<EnumeratorForTest> t(new EnumeratorForTest(AsHandle()));
  206. EXPECT_TRUE(AllZero(t->current()));
  207. t.reset();
  208. EXPECT_TRUE(CheckCallCounters(0, 0, 0, 1));
  209. }
  210. // Tests the simple everything works path through the code.
  211. TEST_F(Win32ToolhelpTest, TestCurrentNextCalled) {
  212. talk_base::scoped_ptr<EnumeratorForTest> t(new EnumeratorForTest(AsHandle()));
  213. EXPECT_TRUE(t->Next());
  214. EXPECT_TRUE(Equals(t->current(), kTestData[0]));
  215. EXPECT_TRUE(t->Next());
  216. EXPECT_TRUE(Equals(t->current(), kTestData[1]));
  217. EXPECT_TRUE(t->Next());
  218. EXPECT_TRUE(Equals(t->current(), kTestData[2]));
  219. EXPECT_FALSE(t->Next());
  220. t.reset();
  221. EXPECT_TRUE(CheckCallCounters(1, 3, 0, 1));
  222. }
  223. TEST_F(Win32ToolhelpTest, TestCurrentProcess) {
  224. int size = MAX_PATH;
  225. WCHAR buf[MAX_PATH];
  226. GetModuleFileName(NULL, buf, ARRAY_SIZE(buf));
  227. std::wstring name = ToUtf16(Pathname(ToUtf8(buf)).filename());
  228. talk_base::ProcessEnumerator processes;
  229. bool found = false;
  230. while (processes.Next()) {
  231. if (!name.compare(processes.current().szExeFile)) {
  232. found = true;
  233. break;
  234. }
  235. }
  236. EXPECT_TRUE(found);
  237. talk_base::ModuleEnumerator modules(processes.current().th32ProcessID);
  238. found = false;
  239. while (modules.Next()) {
  240. if (!name.compare(modules.current().szModule)) {
  241. found = true;
  242. break;
  243. }
  244. }
  245. EXPECT_TRUE(found);
  246. }
  247. } // namespace talk_base