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.

379 lines
10 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: test/unit/test.unit.util.onbailout/test.unit.util.onbailout.cpp
  3. *
  4. * Purpose: Implementation file for the test.unit.util.onbailout project.
  5. *
  6. * Created: 29th April 2008
  7. * Updated: 6th August 2012
  8. *
  9. * Status: Wizard-generated
  10. *
  11. * License: (Licensed under the Synesis Software Open License)
  12. *
  13. * Copyright (c) 2008-2012, Synesis Software Pty Ltd.
  14. * All rights reserved.
  15. *
  16. * www: http://www.synesis.com.au/software
  17. *
  18. * ////////////////////////////////////////////////////////////////////// */
  19. #include <pantheios/util/test/compiler_warnings_suppression.first_include.h>
  20. #define PANTHEIOS_NO_NAMESPACE
  21. #define PANTHEIOS_BAILOUT_NO_OPERATING_SYSTEM_SPECIFICS
  22. #define PANTHEIOS_BAILOUT_NO_UNIX_SYSLOG
  23. #define PANTHEIOS_BAILOUT_NO_WINDOWS_EVENTLOG
  24. #ifdef UNIX
  25. # define _WINSOCKAPI_
  26. # define _WINSOCK2API_
  27. # define _WINSOCK2_H
  28. #endif
  29. #include <pantheios/pantheios.h>
  30. #include <pantheios/internal/lean.h>
  31. #include <pantheios/init_codes.h>
  32. #include <xtests/xtests.h>
  33. #include <shwild/shwild.hpp>
  34. #include <stlsoft/shims/access/string/fwd.h>
  35. #include <stlsoft/shims/access/string/std/basic_string.hpp>
  36. #include <platformstl/platformstl.h>
  37. #include <string>
  38. #include <vector>
  39. #include <stdlib.h>
  40. #if defined(PLATFORMSTL_OS_IS_UNIX)
  41. # undef _WINSOCKAPI_
  42. # undef _WINSOCK2API_
  43. # undef _WINSOCK2_H
  44. # include <sys/time.h> // for gettimeofday()
  45. #endif /* PLATFORMSTL_OS_IS_UNIX */
  46. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>
  47. /* /////////////////////////////////////////////////////////////////////////
  48. * xTests extensions
  49. */
  50. #define XTESTS_TEST_MULTIBYTE_STRING_MATCHES(pattern, value) \
  51. \
  52. ((0 == shwild::match(pattern, stlsoft::c_str_ptr(value), 0)) \
  53. ? XTESTS_TEST_PASSED() \
  54. : XTESTS_TEST_FAIL_WITH_QUALIFIER(std::string("the actual value did not match the pattern '") + pattern + "'", value))
  55. /* /////////////////////////////////////////////////////////////////////////
  56. * Forward declarations
  57. */
  58. namespace
  59. {
  60. static void test_1_01();
  61. static void test_1_02();
  62. static void test_1_03();
  63. static void test_1_04();
  64. static void test_1_05();
  65. static void test_1_06();
  66. static void test_1_07();
  67. static void test_1_08();
  68. static void test_1_09();
  69. static void test_1_10();
  70. static void test_1_11();
  71. static void test_1_12();
  72. } // anonymous namespace
  73. /* /////////////////////////////////////////////////////////////////////////
  74. * Classes
  75. */
  76. namespace
  77. {
  78. class OnBailOutReceiver
  79. {
  80. public:
  81. typedef OnBailOutReceiver class_type;
  82. typedef std::string string_type;
  83. struct entry_type
  84. {
  85. string_type message;
  86. string_type processId;
  87. string_type qualifier;
  88. entry_type(string_type const& msg, string_type const& prId, string_type const& qual)
  89. : message(msg)
  90. , processId(prId)
  91. , qualifier(qual)
  92. {}
  93. };
  94. typedef std::vector<entry_type> entries_type;
  95. public:
  96. void Clear()
  97. {
  98. entries_type().swap(ConsoleResults);
  99. #if defined(PLATFORMSTL_OS_IS_WINDOWS) && \
  100. !defined(PANTHEIOS_BAILOUT_NO_WINDOWS_EVENTLOG)
  101. entries_type().swap(EventLogResults);
  102. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  103. entries_type().swap(LoggingBailoutTxtResults);
  104. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  105. entries_type().swap(OutputDebugStringResults);
  106. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  107. #if defined(PLATFORMSTL_OS_IS_UNIX) && \
  108. !defined(PANTHEIOS_BAILOUT_NO_UNIX_SYSLOG)
  109. entries_type().swap(SyslogResults);
  110. #endif /* PLATFORMSTL_OS_IS_UNIX */
  111. }
  112. void onfprintf(string_type const& msg)
  113. {
  114. ConsoleResults.push_back(entry_type(msg, "", ""));
  115. }
  116. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  117. void onOutputDebugString(char const* s)
  118. {
  119. OutputDebugStringResults.push_back(entry_type(s, "", ""));
  120. }
  121. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  122. public:
  123. entries_type ConsoleResults;
  124. #if defined(PLATFORMSTL_OS_IS_WINDOWS) && \
  125. !defined(PANTHEIOS_BAILOUT_NO_WINDOWS_EVENTLOG)
  126. entries_type EventLogResults;
  127. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  128. entries_type LoggingBailoutTxtResults;
  129. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  130. entries_type OutputDebugStringResults;
  131. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  132. #if defined(PLATFORMSTL_OS_IS_UNIX) && \
  133. !defined(PANTHEIOS_BAILOUT_NO_UNIX_SYSLOG)
  134. entries_type SyslogResults;
  135. #endif /* PLATFORMSTL_OS_IS_UNIX */
  136. };
  137. OnBailOutReceiver receiver;
  138. } // anonymous namespace
  139. /* ////////////////////////////////////////////////////////////////////// */
  140. PANTHEIOS_EXTERN_C PAN_CHAR_T const PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("test.unit.util.onbailout");
  141. /* ////////////////////////////////////////////////////////////////////// */
  142. int main(int argc, char** argv)
  143. {
  144. int retCode = EXIT_SUCCESS;
  145. int verbosity = 2;
  146. XTESTS_COMMANDLINE_PARSEVERBOSITY(argc, argv, &verbosity);
  147. if(XTESTS_START_RUNNER(PANTHEIOS_FE_PROCESS_IDENTITY, verbosity))
  148. {
  149. XTESTS_RUN_CASE(test_1_01);
  150. XTESTS_RUN_CASE(test_1_02);
  151. XTESTS_RUN_CASE(test_1_03);
  152. XTESTS_RUN_CASE(test_1_04);
  153. XTESTS_RUN_CASE(test_1_05);
  154. XTESTS_RUN_CASE(test_1_06);
  155. XTESTS_RUN_CASE(test_1_07);
  156. XTESTS_RUN_CASE(test_1_08);
  157. XTESTS_RUN_CASE(test_1_09);
  158. XTESTS_RUN_CASE(test_1_10);
  159. XTESTS_RUN_CASE(test_1_11);
  160. XTESTS_RUN_CASE(test_1_12);
  161. XTESTS_PRINT_RESULTS();
  162. XTESTS_END_RUNNER_UPDATE_EXITCODE(&retCode);
  163. }
  164. return retCode;
  165. }
  166. /* ////////////////////////////////////////////////////////////////////// */
  167. namespace
  168. {
  169. static void test_1_01()
  170. {
  171. receiver.Clear();
  172. XTESTS_TEST_INTEGER_EQUAL(0u, receiver.ConsoleResults.size());
  173. #if defined(PLATFORMSTL_OS_IS_WINDOWS) && \
  174. !defined(PANTHEIOS_BAILOUT_NO_WINDOWS_EVENTLOG)
  175. XTESTS_TEST_INTEGER_EQUAL(0u, receiver.EventLogResults.size());
  176. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  177. XTESTS_TEST_INTEGER_EQUAL(0u, receiver.LoggingBailoutTxtResults.size());
  178. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  179. XTESTS_TEST_INTEGER_EQUAL(0u, receiver.OutputDebugStringResults.size());
  180. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  181. #if defined(PLATFORMSTL_OS_IS_UNIX) && \
  182. !defined(PANTHEIOS_BAILOUT_NO_UNIX_SYSLOG)
  183. XTESTS_TEST_INTEGER_EQUAL(0u, receiver.SyslogResults.size());
  184. #endif /* PLATFORMSTL_OS_IS_UNIX */
  185. }
  186. static void test_1_02()
  187. {
  188. receiver.Clear();
  189. const char message[] = "hello";
  190. const char pattern[] = "2[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9]: hello";
  191. pantheios_onBailOut3(PANTHEIOS_SEV_DEBUG, message, "process-#1");
  192. XTESTS_TEST_INTEGER_EQUAL(1u, receiver.ConsoleResults.size());
  193. XTESTS_TEST_MULTIBYTE_STRING_MATCHES(pattern, receiver.ConsoleResults[0].message);
  194. #if defined(PLATFORMSTL_OS_IS_WINDOWS) && \
  195. !defined(PANTHEIOS_BAILOUT_NO_WINDOWS_EVENTLOG)
  196. XTESTS_TEST_INTEGER_EQUAL(1u, receiver.EventLogResults.size());
  197. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  198. // XTESTS_TEST_INTEGER_EQUAL(1u, receiver.LoggingBailoutTxtResults.size());
  199. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  200. XTESTS_TEST_INTEGER_EQUAL(1u, receiver.OutputDebugStringResults.size());
  201. XTESTS_TEST_MULTIBYTE_STRING_MATCHES(pattern, receiver.ConsoleResults[0].message);
  202. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  203. #if defined(PLATFORMSTL_OS_IS_UNIX) && \
  204. !defined(PANTHEIOS_BAILOUT_NO_UNIX_SYSLOG)
  205. XTESTS_TEST_INTEGER_EQUAL(1u, receiver.SyslogResults.size());
  206. #endif /* PLATFORMSTL_OS_IS_UNIX */
  207. }
  208. static void test_1_03()
  209. {
  210. }
  211. static void test_1_04()
  212. {
  213. }
  214. static void test_1_05()
  215. {
  216. }
  217. static void test_1_06()
  218. {
  219. }
  220. static void test_1_07()
  221. {
  222. }
  223. static void test_1_08()
  224. {
  225. }
  226. static void test_1_09()
  227. {
  228. }
  229. static void test_1_10()
  230. {
  231. }
  232. static void test_1_11()
  233. {
  234. }
  235. static void test_1_12()
  236. {
  237. }
  238. } // anonymous namespace
  239. /* ////////////////////////////////////////////////////////////////////// */
  240. #include <stdio.h>
  241. #if defined(PLATFORMSTL_OS_IS_UNIX)
  242. # include <unistd.h>
  243. # define fprintf bailout_test_fprintf_
  244. void bailout_test_fprintf_(FILE*, char const*, int, char const*);
  245. # define fopen(fileName, mode) bailout_test_fopen_()
  246. FILE* bailout_test_fopen_(void);
  247. #endif /* PLATFORMSTL_OS_IS_UNIX */
  248. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  249. # include <windows.h>
  250. # define OutputDebugStringA bailout_test_OutputDebugStringA_
  251. void bailout_test_OutputDebugStringA_(char const*);
  252. # define WriteFile bailout_test_WriteFile_
  253. void bailout_test_WriteFile_(FILE*, char const*, DWORD, DWORD*, void*);
  254. void bailout_test_WriteFile_(HANDLE, char const*, DWORD, DWORD*, void*);
  255. # define GetStdHandle(x) bailout_test_GetStdHandle_()
  256. FILE* bailout_test_GetStdHandle_(void);
  257. # define CreateFileA bailout_test_CreateFileA
  258. HANDLE bailout_test_CreateFileA(char const*, DWORD, DWORD, void*, DWORD, DWORD, void*);
  259. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  260. #include <../src/util/bailout.c>
  261. #include <../src/util/snprintf.c>
  262. /* ////////////////////////////////////////////////////////////////////// */
  263. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  264. void bailout_test_OutputDebugStringA_(char const* msg)
  265. {
  266. receiver.onOutputDebugString(msg);
  267. }
  268. void bailout_test_WriteFile_(FILE*, char const* msg, DWORD cchMsg, DWORD*, void*)
  269. {
  270. receiver.onfprintf(OnBailOutReceiver::string_type(msg, cchMsg - 2));
  271. }
  272. void bailout_test_WriteFile_(HANDLE, char const*, DWORD, DWORD*, void*)
  273. {
  274. }
  275. FILE* bailout_test_GetStdHandle_(void)
  276. {
  277. return NULL;
  278. }
  279. HANDLE bailout_test_CreateFileA(char const*, DWORD, DWORD, void*, DWORD, DWORD, void*)
  280. {
  281. return INVALID_HANDLE_VALUE;
  282. }
  283. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  284. #if defined(PLATFORMSTL_OS_IS_UNIX)
  285. void bailout_test_fprintf_(FILE*, char const*, int cchMsg, char const* msg)
  286. {
  287. receiver.onfprintf(OnBailOutReceiver::string_type(msg, size_t(cchMsg)));
  288. }
  289. FILE* bailout_test_fopen_(void)
  290. {
  291. return NULL;
  292. }
  293. #endif /* PLATFORMSTL_OS_IS_UNIX */
  294. /* ////////////////////////////////////////////////////////////////////// */