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.

181 lines
4.8 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: test/component/test.component.inserters.hostId/test.component.inserters.hostId.cpp
  3. *
  4. * Purpose: Implementation file for the test.component.inserters.hostId project.
  5. *
  6. * Created: 14th April 2008
  7. * Updated: 23rd March 2010
  8. *
  9. * Status: Wizard-generated
  10. *
  11. * License: (Licensed under the Synesis Software Open License)
  12. *
  13. * Copyright (c) 2008-2010, 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. /* xTests Header Files */
  21. #include <xtests/xtests.h>
  22. /* Pantheios Header Files */
  23. #include <pantheios/pantheios.hpp> // Pantheios C++ main header
  24. #include <pantheios/inserters/hostid.hpp> // for pantheios::hostId
  25. #include <pantheios/backends/bec.test.h>
  26. /* STLSoft Header Files */
  27. #include <stlsoft/conversion/integer_to_string.hpp>
  28. #include <platformstl/platformstl.h>
  29. /* Standard C++ Header Files */
  30. #include <string>
  31. /* Standard C Header Files */
  32. #include <stdlib.h> // for exit codes
  33. #if defined(PLATFORMSTL_OS_IS_UNIX)
  34. # include <unistd.h>
  35. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  36. # include <windows.h>
  37. #else /* ? OS */
  38. # error Not discriminated for platforms other than UNIX and Windows
  39. #endif /* OS */
  40. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>
  41. /* /////////////////////////////////////////////////////////////////////////
  42. * Typedefs
  43. */
  44. typedef std::basic_string<PAN_CHAR_T> string_t;
  45. /* /////////////////////////////////////////////////////////////////////////
  46. * Forward declarations
  47. */
  48. static void test_1_01();
  49. static string_t pan_get_hid_();
  50. /* ////////////////////////////////////////////////////////////////////// */
  51. /* Define the stock front-end process identity, so that it links when using
  52. * fe.N, fe.simple, etc. */
  53. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("test.component.inserters.hostId");
  54. /* ////////////////////////////////////////////////////////////////////// */
  55. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  56. /* /////////////////////////////////////////////////////////////////////////
  57. * Character encoding
  58. */
  59. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  60. # define pantheios_GetComputerName_ ::GetComputerNameW
  61. # define XTESTS_TEST_STRING_EQUAL XTESTS_TEST_WIDE_STRING_EQUAL
  62. #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  63. # define pantheios_GetComputerName_ ::GetComputerNameA
  64. # define XTESTS_TEST_STRING_EQUAL XTESTS_TEST_MULTIBYTE_STRING_EQUAL
  65. #endif /* PANTHEIOS_USE_WIDE_STRINGS */
  66. /* ////////////////////////////////////////////////////////////////////// */
  67. int main(int argc, char** argv)
  68. {
  69. int retCode = EXIT_SUCCESS;
  70. int verbosity = 2;
  71. XTESTS_COMMANDLINE_PARSEVERBOSITY(argc, argv, &verbosity);
  72. if(XTESTS_START_RUNNER("test.component.inserters.hostId", verbosity))
  73. {
  74. XTESTS_RUN_CASE(test_1_01);
  75. XTESTS_PRINT_RESULTS();
  76. XTESTS_END_RUNNER_UPDATE_EXITCODE(&retCode);
  77. }
  78. return retCode;
  79. }
  80. /* ////////////////////////////////////////////////////////////////////// */
  81. static void test_1_01()
  82. {
  83. // 1. Setup
  84. const PAN_CHAR_T prefix[] = PSTR("host: ");
  85. const string_t hid = pan_get_hid_();
  86. const string_t stmt = prefix + hid;
  87. pantheios::be::test::reset();
  88. // 2. Create test data
  89. pantheios::log_NOTICE(pantheios::hostId);
  90. pantheios::log_NOTICE(prefix, pantheios::hostId);
  91. // 3. Verification
  92. pantheios::be::test::Results results = pantheios::be::test::results();
  93. XTESTS_TEST(!results.empty());
  94. XTESTS_TEST(2 == results.size());
  95. XTESTS_TEST_STRING_EQUAL(hid, results[0].statement);
  96. XTESTS_TEST_STRING_EQUAL(stmt, results[1].statement);
  97. }
  98. static string_t pan_get_hid_()
  99. {
  100. #if defined(PLATFORMSTL_OS_IS_UNIX)
  101. PAN_CHAR_T szHostName[1001];
  102. if(0 != ::gethostname(&szHostName[0], STLSOFT_NUM_ELEMENTS(szHostName)))
  103. {
  104. return PANTHEIOS_LITERAL_STRING("localhost");
  105. }
  106. else
  107. {
  108. return szHostName;
  109. }
  110. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  111. PAN_CHAR_T szHostName[1001];
  112. DWORD cchHostName = STLSOFT_NUM_ELEMENTS(szHostName);
  113. if(!pantheios_GetComputerName_(&szHostName[0], &cchHostName))
  114. {
  115. return PANTHEIOS_LITERAL_STRING("localhost");
  116. }
  117. else
  118. {
  119. return string_t(szHostName, static_cast<size_t>(cchHostName));
  120. }
  121. #else /* ? OS */
  122. # error Not discriminated for platforms other than UNIX and Windows
  123. #endif /* OS */
  124. }
  125. /* ///////////////////////////// end of file //////////////////////////// */