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.

200 lines
5.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: test/unit/test.unit.util.gethostname/test.unit.util.gethostname.cpp
  3. *
  4. * Purpose: Implementation file for the test.unit.util.gethostname project.
  5. *
  6. * Created: 14th 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. /* xTests Header Files */
  21. #include <xtests/xtests.h>
  22. /* Pantheios Header Files */
  23. #include <pantheios/pantheios.h> // Pantheios C++ main header
  24. #include <pantheios/util/system/hostname.h> // for pantheios::getHostName()
  25. #include <pantheios/backends/bec.test.h>
  26. /* STLSoft Header Files */
  27. #include <pantheios/util/memory/auto_buffer_selector.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. * Character encoding
  47. */
  48. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  49. # define pantheios_GetComputerName_ ::GetComputerNameW
  50. # define XTESTS_TEST_STRING_EQUAL XTESTS_TEST_WIDE_STRING_EQUAL
  51. #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  52. # define pantheios_GetComputerName_ ::GetComputerNameA
  53. # define XTESTS_TEST_STRING_EQUAL XTESTS_TEST_MULTIBYTE_STRING_EQUAL
  54. #endif /* PANTHEIOS_USE_WIDE_STRINGS */
  55. /* /////////////////////////////////////////////////////////////////////////
  56. * Forward declarations
  57. */
  58. static void test_1_01();
  59. static void test_1_02();
  60. static string_t pan_get_hid_();
  61. /* ////////////////////////////////////////////////////////////////////// */
  62. /* Define the stock front-end process identity, so that it links when using
  63. * fe.N, fe.simple, etc. */
  64. PANTHEIOS_EXTERN_C PAN_CHAR_T const PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("test.unit.util.gethostname");
  65. /* ////////////////////////////////////////////////////////////////////// */
  66. int main(int argc, char** argv)
  67. {
  68. int retCode = EXIT_SUCCESS;
  69. int verbosity = 2;
  70. XTESTS_COMMANDLINE_PARSEVERBOSITY(argc, argv, &verbosity);
  71. if(XTESTS_START_RUNNER("test.unit.util.gethostname", verbosity))
  72. {
  73. XTESTS_RUN_CASE(test_1_01);
  74. XTESTS_RUN_CASE(test_1_02);
  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. PAN_CHAR_T hostname[1000];
  84. const string_t hid = pan_get_hid_();
  85. { for(size_t i = 0; i != STLSOFT_NUM_ELEMENTS(hostname); ++i)
  86. {
  87. ::memset(&hostname[0], 0, sizeof(hostname));
  88. const size_t len = pantheios::getHostName(&hostname[0], i);
  89. if(len == i)
  90. {
  91. // The function did not have enough space to write in, so it
  92. // will return the length passed to it ...
  93. XTESTS_TEST_INTEGER_EQUAL(i, len);
  94. // ... and will not have written anything to the file
  95. XTESTS_TEST_STRING_EQUAL(PANTHEIOS_LITERAL_STRING(""), hostname);
  96. }
  97. else
  98. {
  99. // The function had enough space, so it will return the length
  100. // of the intended hostname ...
  101. XTESTS_TEST_INTEGER_EQUAL(hid.size(), len);
  102. // ... and will have written the hostname
  103. XTESTS_TEST_STRING_EQUAL(hid, hostname);
  104. }
  105. }}
  106. }
  107. static void test_1_02()
  108. {
  109. const string_t hid = pan_get_hid_();
  110. { for(size_t i = 1; i != 1001; ++i)
  111. {
  112. pantheios::util::auto_buffer_selector<PAN_CHAR_T, 256>::type hostname(i);
  113. const size_t len = pantheios::getHostName(hostname);
  114. // The function had enough space, so it will return the length
  115. // of the intended hostname ...
  116. XTESTS_TEST_INTEGER_EQUAL(hid.size(), len);
  117. // ... and will have written the hostname
  118. XTESTS_TEST_STRING_EQUAL(hid, hostname.data());
  119. }}
  120. }
  121. /* ////////////////////////////////////////////////////////////////////// */
  122. static string_t pan_get_hid_()
  123. {
  124. #if defined(PLATFORMSTL_OS_IS_UNIX)
  125. PAN_CHAR_T szHostName[1001];
  126. if(0 != ::gethostname(&szHostName[0], STLSOFT_NUM_ELEMENTS(szHostName)))
  127. {
  128. return PANTHEIOS_LITERAL_STRING("localhost");
  129. }
  130. else
  131. {
  132. return szHostName;
  133. }
  134. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  135. PAN_CHAR_T szHostName[1001];
  136. DWORD cchHostName = STLSOFT_NUM_ELEMENTS(szHostName);
  137. if(!pantheios_GetComputerName_(&szHostName[0], &cchHostName))
  138. {
  139. return PANTHEIOS_LITERAL_STRING("localhost");
  140. }
  141. else
  142. {
  143. return string_t(szHostName, static_cast<size_t>(cchHostName));
  144. }
  145. #else /* ? OS */
  146. # error Not discriminated for platforms other than UNIX and Windows
  147. #endif /* OS */
  148. }
  149. /* ///////////////////////////// end of file //////////////////////////// */