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.

654 lines
16 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: test/unit/test.unit.fe.WindowsRegistry/test.unit.fe.WindowsRegistry.cpp
  3. *
  4. * Purpose: Implementation file for the test.unit.fe.WindowsRegistry project.
  5. *
  6. * Created: 14th May 2008
  7. * Updated: 4th January 2011
  8. *
  9. * Status: Wizard-generated
  10. *
  11. * License: (Licensed under the Synesis Software Open License)
  12. *
  13. * Copyright (c) 2008-2011, 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. #include <pantheios/frontends/fe.WindowsRegistry.h>
  21. #include <pantheios/init_codes.h>
  22. #include <xtests/xtests.h>
  23. #include <stlsoft/stlsoft.h> /* for STLSOFT_NUM_ELEMENTS */
  24. #include <stlsoft/conversion/integer_to_string.hpp> /* stlsoft::integer_to_string */
  25. #include <stlsoft/synch/lock_scope.hpp> /* stlsoft::lock_scope */
  26. #include <winstl/synch/process_mutex.hpp> /* winstl::process_mutex */
  27. #include <winstl/registry/reg_key.hpp> /* for Windows Registry Library reg_key class */
  28. #include <stdio.h> /* for fprintf() */
  29. #include <stdlib.h> /* for EXIT_SUCCESS, EXIT_FAILURE */
  30. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>
  31. /* /////////////////////////////////////////////////////////////////////////
  32. * Character encoding
  33. */
  34. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  35. # define XTESTS_TEST_STRING_EQUAL XTESTS_TEST_WIDE_STRING_EQUAL
  36. #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  37. # define XTESTS_TEST_STRING_EQUAL XTESTS_TEST_MULTIBYTE_STRING_EQUAL
  38. #endif /* PANTHEIOS_USE_WIDE_STRINGS */
  39. #ifndef XTESTS_TEST_NOTICE
  40. # define XTESTS_TEST_NOTICE(x) ::OutputDebugString(PANTHEIOS_LITERAL_STRING(x))
  41. #endif // XTESTS_TEST_NOTICE
  42. #ifdef PSTR
  43. # undef PSTR
  44. #endif
  45. #define PSTR PANTHEIOS_LITERAL_STRING
  46. /* /////////////////////////////////////////////////////////////////////////
  47. * Globals
  48. */
  49. const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("test.unit.fe.WindowsRegistry");
  50. /* /////////////////////////////////////////////////////////////////////////
  51. * Forward declarations
  52. */
  53. namespace
  54. {
  55. static void test_can_init(void);
  56. static void test_cannot_init(void);
  57. static void test_levels_in_HKCU(void);
  58. static void test_levels_in_HKLM(void);
  59. static void test_HKCU_overrides_HKLM(void);
  60. static void test_star(void);
  61. static void test_1_6(void);
  62. static void test_1_7(void);
  63. static void test_1_8(void);
  64. static void test_1_9(void);
  65. static void test_1_10(void);
  66. static void test_1_11(void);
  67. static void test_1_12(void);
  68. static void test_1_13(void);
  69. static void test_1_14(void);
  70. static void test_1_15(void);
  71. static void test_1_16(void);
  72. static void test_1_17(void);
  73. static void test_1_18(void);
  74. static void test_cleanup(void);
  75. } // anonymous namespace
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Main
  78. */
  79. static int main_(int argc, char** argv)
  80. {
  81. // Lock out other test program instances, otherwise they'll interfere
  82. // with each other
  83. winstl::process_mutex mx("test.unit.fe.WindowsRegistry.MX");
  84. stlsoft::lock_scope<winstl::process_mutex> lock(mx);
  85. int retCode = EXIT_SUCCESS;
  86. int verbosity = 2;
  87. XTESTS_COMMANDLINE_PARSEVERBOSITY(argc, argv, &verbosity);
  88. if(XTESTS_START_RUNNER("test.unit.fe.WindowsRegistry", verbosity))
  89. {
  90. XTESTS_RUN_CASE(test_can_init);
  91. XTESTS_RUN_CASE(test_cannot_init);
  92. XTESTS_RUN_CASE(test_levels_in_HKCU);
  93. XTESTS_RUN_CASE(test_levels_in_HKLM);
  94. XTESTS_RUN_CASE(test_HKCU_overrides_HKLM);
  95. XTESTS_RUN_CASE(test_star);
  96. XTESTS_RUN_CASE(test_1_6);
  97. XTESTS_RUN_CASE(test_1_7);
  98. XTESTS_RUN_CASE(test_1_8);
  99. XTESTS_RUN_CASE(test_1_9);
  100. XTESTS_RUN_CASE(test_1_10);
  101. XTESTS_RUN_CASE(test_1_11);
  102. XTESTS_RUN_CASE(test_1_12);
  103. XTESTS_RUN_CASE(test_1_13);
  104. XTESTS_RUN_CASE(test_1_14);
  105. XTESTS_RUN_CASE(test_1_15);
  106. XTESTS_RUN_CASE(test_1_16);
  107. XTESTS_RUN_CASE(test_1_17);
  108. XTESTS_RUN_CASE(test_1_18);
  109. XTESTS_RUN_CASE(test_cleanup);
  110. XTESTS_PRINT_RESULTS();
  111. XTESTS_END_RUNNER_UPDATE_EXITCODE(&retCode);
  112. }
  113. return retCode;
  114. }
  115. int main(int argc, char** argv)
  116. {
  117. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  118. try
  119. {
  120. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  121. return main_(argc, argv);
  122. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  123. }
  124. catch(std::bad_alloc&)
  125. {
  126. fprintf(stderr, "out of memory\n");
  127. }
  128. catch(std::exception& x)
  129. {
  130. fprintf(stderr, "exception: %s\n", x.what());
  131. }
  132. catch(...)
  133. {
  134. fprintf(stderr, "unexpected condition\n");
  135. }
  136. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  137. return EXIT_FAILURE;
  138. }
  139. namespace stub
  140. {
  141. PANTHEIOS_CALL(void) pantheios_onBailOut3(int severity, char const* message, char const* processId);
  142. PANTHEIOS_CALL(void) pantheios_onBailOut3(int severity, char const* message, char const* processId)
  143. {
  144. #if 0
  145. fprintf(stdout, "%d: %s: %s", severity, (NULL != processId) ? processId : "", message);
  146. fprintf(stderr, "%d: %s: %s", severity, (NULL != processId) ? processId : "", message);
  147. #else /* ? 0 */
  148. STLSOFT_SUPPRESS_UNUSED(severity);
  149. STLSOFT_SUPPRESS_UNUSED(message);
  150. STLSOFT_SUPPRESS_UNUSED(processId);
  151. #endif /* 0 */
  152. }
  153. } /* namespace stub */
  154. /* /////////////////////////////////////////////////////////////////////////
  155. * Test function implementations
  156. */
  157. namespace
  158. {
  159. static const PAN_CHAR_T ROOT_KEY_PATH[] = PANTHEIOS_LITERAL_STRING("SOFTWARE\\Synesis Software\\Logging Tools\\Pantheios\\fe.WindowsRegistry");
  160. static const int s_severityLevels[] =
  161. {
  162. PANTHEIOS_SEV_EMERGENCY
  163. , PANTHEIOS_SEV_ALERT
  164. , PANTHEIOS_SEV_CRITICAL
  165. , PANTHEIOS_SEV_ERROR
  166. , PANTHEIOS_SEV_WARNING
  167. , PANTHEIOS_SEV_NOTICE
  168. , PANTHEIOS_SEV_INFORMATIONAL
  169. , PANTHEIOS_SEV_DEBUG
  170. };
  171. static void ensure_HKCU(int debugLevel, int releaseLevel, int starLevel)
  172. {
  173. winstl::reg_key key0 = winstl::reg_key::create_key(HKEY_CURRENT_USER, ROOT_KEY_PATH);
  174. winstl::reg_key key = key0.create_sub_key(PANTHEIOS_FE_PROCESS_IDENTITY);
  175. if(debugLevel >= 0)
  176. {
  177. key.set_value(PSTR("Debug"), DWORD(debugLevel));
  178. }
  179. if(releaseLevel >= 0)
  180. {
  181. key.set_value(PSTR("Release"), DWORD(releaseLevel));
  182. }
  183. if(starLevel >= 0)
  184. {
  185. key.set_value(PSTR("*"), DWORD(starLevel));
  186. }
  187. }
  188. static bool ensure_HKLM(int debugLevel, int releaseLevel, int starLevel)
  189. {
  190. try
  191. {
  192. winstl::reg_key key0 = winstl::reg_key::create_key(HKEY_LOCAL_MACHINE, ROOT_KEY_PATH);
  193. winstl::reg_key key = key0.create_sub_key(PANTHEIOS_FE_PROCESS_IDENTITY);
  194. if(debugLevel >= 0)
  195. {
  196. key.set_value(PSTR("Debug"), DWORD(debugLevel));
  197. }
  198. if(releaseLevel >= 0)
  199. {
  200. key.set_value(PSTR("Release"), DWORD(releaseLevel));
  201. }
  202. if(starLevel >= 0)
  203. {
  204. key.set_value(PSTR("*"), DWORD(starLevel));
  205. }
  206. return true;
  207. }
  208. catch(winstl::windows_exception& x)
  209. {
  210. if(ERROR_ACCESS_DENIED == x.get_error_code())
  211. {
  212. return false;
  213. }
  214. else
  215. {
  216. throw;
  217. }
  218. }
  219. }
  220. static void delete_HKCU()
  221. {
  222. winstl::reg_key key0 = winstl::reg_key::create_key(HKEY_CURRENT_USER, ROOT_KEY_PATH);
  223. key0.delete_sub_key(PANTHEIOS_FE_PROCESS_IDENTITY);
  224. }
  225. static bool delete_HKLM()
  226. {
  227. try
  228. {
  229. winstl::reg_key key0 = winstl::reg_key::create_key(HKEY_LOCAL_MACHINE, ROOT_KEY_PATH);
  230. key0.delete_sub_key(PANTHEIOS_FE_PROCESS_IDENTITY);
  231. return true;
  232. }
  233. catch(winstl::windows_exception& x)
  234. {
  235. if(ERROR_ACCESS_DENIED == x.get_error_code())
  236. {
  237. return false;
  238. }
  239. else
  240. {
  241. throw;
  242. }
  243. }
  244. }
  245. static void test_can_init()
  246. {
  247. // Ensure that the HKCU key exists
  248. ensure_HKCU(0, 0, 0);
  249. if(!delete_HKLM())
  250. {
  251. XTESTS_TEST_NOTICE("could not change HKEY_LOCAL_MACHINE, so skipping test case");
  252. return;
  253. }
  254. void* token;
  255. int res;
  256. res = pantheios_fe_init(NULL, &token);
  257. XTESTS_TEST_INTEGER_EQUAL(PANTHEIOS_INIT_RC_SUCCESS, res);
  258. if(PANTHEIOS_INIT_RC_SUCCESS != res)
  259. {
  260. char num[21];
  261. XTESTS_TEST_FAIL_WITH_QUALIFIER("failed to initialise front-end", stlsoft::integer_to_string(&num[0], STLSOFT_NUM_ELEMENTS(num), res));
  262. }
  263. else
  264. {
  265. XTESTS_TEST_PASSED();
  266. pantheios_fe_uninit(token);
  267. }
  268. }
  269. static void test_cannot_init()
  270. {
  271. // Ensure that neither the HKCU key nor the HKLM key exist
  272. delete_HKCU();
  273. if(!delete_HKLM())
  274. {
  275. XTESTS_TEST_NOTICE("could not change HKEY_LOCAL_MACHINE, so skipping test case");
  276. return;
  277. }
  278. void* token;
  279. int res;
  280. res = pantheios_fe_init(NULL, &token);
  281. XTESTS_TEST_INTEGER_NOT_EQUAL(PANTHEIOS_INIT_RC_SUCCESS, res);
  282. if(PANTHEIOS_INIT_RC_SUCCESS != res)
  283. {
  284. XTESTS_TEST_PASSED();
  285. }
  286. else
  287. {
  288. XTESTS_TEST_FAIL("unexpectedly able to initialise front-end");
  289. pantheios_fe_uninit(token);
  290. }
  291. }
  292. static void test_levels_in_HKCU()
  293. {
  294. // Ensure that the HKCU key exists, and has the right levels
  295. ensure_HKCU(0xff, 0x3f, -1);
  296. if(!delete_HKLM())
  297. {
  298. XTESTS_TEST_NOTICE("could not change HKEY_LOCAL_MACHINE, so skipping test case");
  299. return;
  300. }
  301. static const int s_severityLevelResults[] =
  302. {
  303. 1
  304. , 1
  305. , 1
  306. , 1
  307. , 1
  308. , 1
  309. #ifdef NDEBUG
  310. , 0
  311. , 0
  312. #else /* ? NDEBUG */
  313. , 1
  314. , 1
  315. #endif /* NDEBUG */
  316. };
  317. STLSOFT_STATIC_ASSERT(STLSOFT_NUM_ELEMENTS(s_severityLevels) == STLSOFT_NUM_ELEMENTS(s_severityLevelResults));
  318. void* token;
  319. int res;
  320. res = pantheios_fe_init(NULL, &token);
  321. XTESTS_TEST_INTEGER_EQUAL(PANTHEIOS_INIT_RC_SUCCESS, res);
  322. if(PANTHEIOS_INIT_RC_SUCCESS != res)
  323. {
  324. char num[21];
  325. XTESTS_TEST_FAIL_WITH_QUALIFIER("failed to initialise front-end", stlsoft::integer_to_string(&num[0], STLSOFT_NUM_ELEMENTS(num), res));
  326. }
  327. else
  328. {
  329. XTESTS_TEST_PASSED();
  330. // Now verify the levels
  331. { for(size_t i = 0; i != STLSOFT_NUM_ELEMENTS(s_severityLevels); ++i)
  332. {
  333. int b = pantheios_fe_isSeverityLogged(token, s_severityLevels[i], 0);
  334. XTESTS_TEST_INTEGER_EQUAL(s_severityLevelResults[i], b);
  335. }}
  336. pantheios_fe_uninit(token);
  337. }
  338. }
  339. static void test_levels_in_HKLM()
  340. {
  341. // Ensure that the HKCU key exists, and has the right levels
  342. delete_HKCU();
  343. if(!ensure_HKLM(0xff, 0x3f, -1))
  344. {
  345. XTESTS_TEST_NOTICE("could not change HKEY_LOCAL_MACHINE, so skipping test case");
  346. return;
  347. }
  348. static const int s_severityLevelResults[] =
  349. {
  350. 1
  351. , 1
  352. , 1
  353. , 1
  354. , 1
  355. , 1
  356. #ifdef NDEBUG
  357. , 0
  358. , 0
  359. #else /* ? NDEBUG */
  360. , 1
  361. , 1
  362. #endif /* NDEBUG */
  363. };
  364. STLSOFT_STATIC_ASSERT(STLSOFT_NUM_ELEMENTS(s_severityLevels) == STLSOFT_NUM_ELEMENTS(s_severityLevelResults));
  365. void* token;
  366. int res;
  367. res = pantheios_fe_init(NULL, &token);
  368. XTESTS_TEST_INTEGER_EQUAL(PANTHEIOS_INIT_RC_SUCCESS, res);
  369. if(PANTHEIOS_INIT_RC_SUCCESS != res)
  370. {
  371. char num[21];
  372. XTESTS_TEST_FAIL_WITH_QUALIFIER("failed to initialise front-end", stlsoft::integer_to_string(&num[0], STLSOFT_NUM_ELEMENTS(num), res));
  373. }
  374. else
  375. {
  376. XTESTS_TEST_PASSED();
  377. // Now verify the levels
  378. { for(size_t i = 0; i != STLSOFT_NUM_ELEMENTS(s_severityLevels); ++i)
  379. {
  380. int b = pantheios_fe_isSeverityLogged(token, s_severityLevels[i], 0);
  381. XTESTS_TEST_INTEGER_EQUAL(s_severityLevelResults[i], b);
  382. }}
  383. pantheios_fe_uninit(token);
  384. }
  385. }
  386. static void test_HKCU_overrides_HKLM()
  387. {
  388. // Ensure that the HKCU key exists, and has the right levels
  389. ensure_HKCU(0xff, 0x3f, -1);
  390. if(!ensure_HKLM(0, 0, 0))
  391. {
  392. XTESTS_TEST_NOTICE("could not change HKEY_LOCAL_MACHINE, so skipping test case");
  393. return;
  394. }
  395. static const int s_severityLevelResults[] =
  396. {
  397. 1
  398. , 1
  399. , 1
  400. , 1
  401. , 1
  402. , 1
  403. #ifdef NDEBUG
  404. , 0
  405. , 0
  406. #else /* ? NDEBUG */
  407. , 1
  408. , 1
  409. #endif /* NDEBUG */
  410. };
  411. STLSOFT_STATIC_ASSERT(STLSOFT_NUM_ELEMENTS(s_severityLevels) == STLSOFT_NUM_ELEMENTS(s_severityLevelResults));
  412. void* token;
  413. int res;
  414. res = pantheios_fe_init(NULL, &token);
  415. XTESTS_TEST_INTEGER_EQUAL(PANTHEIOS_INIT_RC_SUCCESS, res);
  416. if(PANTHEIOS_INIT_RC_SUCCESS != res)
  417. {
  418. char num[21];
  419. XTESTS_TEST_FAIL_WITH_QUALIFIER("failed to initialise front-end", stlsoft::integer_to_string(&num[0], STLSOFT_NUM_ELEMENTS(num), res));
  420. }
  421. else
  422. {
  423. XTESTS_TEST_PASSED();
  424. // Now verify the levels
  425. { for(size_t i = 0; i != STLSOFT_NUM_ELEMENTS(s_severityLevels); ++i)
  426. {
  427. int b = pantheios_fe_isSeverityLogged(token, s_severityLevels[i], 0);
  428. XTESTS_TEST_INTEGER_EQUAL(s_severityLevelResults[i], b);
  429. }}
  430. pantheios_fe_uninit(token);
  431. }
  432. }
  433. static void test_star()
  434. {
  435. // Ensure that the HKCU key exists, and has the right levels
  436. delete_HKCU();
  437. ensure_HKCU(-1, -1, 0x0f);
  438. if(!delete_HKLM())
  439. {
  440. XTESTS_TEST_NOTICE("could not change HKEY_LOCAL_MACHINE, so skipping test case");
  441. return;
  442. }
  443. static const int s_severityLevelResults[] =
  444. {
  445. 1
  446. , 1
  447. , 1
  448. , 1
  449. , 0
  450. , 0
  451. , 0
  452. , 0
  453. };
  454. STLSOFT_STATIC_ASSERT(STLSOFT_NUM_ELEMENTS(s_severityLevels) == STLSOFT_NUM_ELEMENTS(s_severityLevelResults));
  455. void* token;
  456. int res;
  457. res = pantheios_fe_init(NULL, &token);
  458. XTESTS_TEST_INTEGER_EQUAL(PANTHEIOS_INIT_RC_SUCCESS, res);
  459. if(PANTHEIOS_INIT_RC_SUCCESS != res)
  460. {
  461. char num[21];
  462. XTESTS_TEST_FAIL_WITH_QUALIFIER("failed to initialise front-end", stlsoft::integer_to_string(&num[0], STLSOFT_NUM_ELEMENTS(num), res));
  463. }
  464. else
  465. {
  466. XTESTS_TEST_PASSED();
  467. // Now verify the levels
  468. { for(size_t i = 0; i != STLSOFT_NUM_ELEMENTS(s_severityLevels); ++i)
  469. {
  470. int b = pantheios_fe_isSeverityLogged(token, s_severityLevels[i], 0);
  471. XTESTS_TEST_INTEGER_EQUAL(s_severityLevelResults[i], b);
  472. }}
  473. pantheios_fe_uninit(token);
  474. }
  475. }
  476. static void test_1_6()
  477. {
  478. }
  479. static void test_1_7()
  480. {
  481. }
  482. static void test_1_8()
  483. {
  484. }
  485. static void test_1_9()
  486. {
  487. }
  488. static void test_1_10()
  489. {
  490. }
  491. static void test_1_11()
  492. {
  493. }
  494. static void test_1_12()
  495. {
  496. }
  497. static void test_1_13()
  498. {
  499. }
  500. static void test_1_14()
  501. {
  502. }
  503. static void test_1_15()
  504. {
  505. }
  506. static void test_1_16()
  507. {
  508. }
  509. static void test_1_17()
  510. {
  511. }
  512. static void test_1_18()
  513. {
  514. }
  515. static void test_cleanup()
  516. {
  517. delete_HKCU();
  518. delete_HKLM();
  519. XTESTS_TEST_PASSED();
  520. }
  521. } // anonymous namespace
  522. /* ///////////////////////////// end of file //////////////////////////// */