test_os.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* test_os.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_OS_H
  31. #define TEST_OS_H
  32. #include "core/os/os.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestOS {
  35. TEST_CASE("[OS] Environment variables") {
  36. #ifdef WINDOWS_ENABLED
  37. CHECK_MESSAGE(
  38. OS::get_singleton()->has_environment("USERPROFILE"),
  39. "The USERPROFILE environment variable should be present.");
  40. #else
  41. CHECK_MESSAGE(
  42. OS::get_singleton()->has_environment("HOME"),
  43. "The HOME environment variable should be present.");
  44. #endif
  45. }
  46. TEST_CASE("[OS] UTF-8 environment variables") {
  47. String value = String::utf8("hell\xc3\xb6"); // "hellö", UTF-8 encoded
  48. OS::get_singleton()->set_environment("HELLO", value);
  49. String val = OS::get_singleton()->get_environment("HELLO");
  50. CHECK_MESSAGE(
  51. val == value,
  52. "The previously-set HELLO environment variable should return the expected value.");
  53. CHECK_MESSAGE(
  54. val.length() == 5,
  55. "The previously-set HELLO environment variable was decoded as UTF-8 and should have a length of 5.");
  56. OS::get_singleton()->unset_environment("HELLO");
  57. }
  58. TEST_CASE("[OS] Non-UTF-8 environment variables") {
  59. String value = String("\xff t\xf6rkylempij\xe4vongahdus"); // hex FF and a Finnish pangram, latin-1
  60. OS::get_singleton()->set_environment("HELLO", value);
  61. String val = OS::get_singleton()->get_environment("HELLO");
  62. CHECK_MESSAGE(
  63. val == value,
  64. "The previously-set HELLO environment variable should return the expected value.");
  65. CHECK_MESSAGE(
  66. val.length() == 23,
  67. "The previously-set HELLO environment variable was not decoded from Latin-1.");
  68. OS::get_singleton()->unset_environment("HELLO");
  69. }
  70. TEST_CASE("[OS] Command line arguments") {
  71. List<String> arguments = OS::get_singleton()->get_cmdline_args();
  72. bool found = false;
  73. for (const String &arg : arguments) {
  74. if (arg == "--test") {
  75. found = true;
  76. break;
  77. }
  78. }
  79. CHECK_MESSAGE(
  80. found,
  81. "The `--test` option must be present in the list of command line arguments.");
  82. }
  83. TEST_CASE("[OS] Executable and data paths") {
  84. CHECK_MESSAGE(
  85. OS::get_singleton()->get_executable_path().is_absolute_path(),
  86. "The executable path returned should be an absolute path.");
  87. CHECK_MESSAGE(
  88. OS::get_singleton()->get_data_path().is_absolute_path(),
  89. "The user data path returned should be an absolute path.");
  90. CHECK_MESSAGE(
  91. OS::get_singleton()->get_config_path().is_absolute_path(),
  92. "The user configuration path returned should be an absolute path.");
  93. CHECK_MESSAGE(
  94. OS::get_singleton()->get_cache_path().is_absolute_path(),
  95. "The cache path returned should be an absolute path.");
  96. }
  97. TEST_CASE("[OS] Ticks") {
  98. CHECK_MESSAGE(
  99. OS::get_singleton()->get_ticks_usec() > 1000,
  100. "The returned ticks (in microseconds) must be greater than 1,000.");
  101. CHECK_MESSAGE(
  102. OS::get_singleton()->get_ticks_msec() > 1,
  103. "The returned ticks (in milliseconds) must be greater than 1.");
  104. }
  105. TEST_CASE("[OS] Feature tags") {
  106. #ifdef TOOLS_ENABLED
  107. CHECK_MESSAGE(
  108. OS::get_singleton()->has_feature("editor"),
  109. "The binary has the \"editor\" feature tag.");
  110. CHECK_MESSAGE(
  111. !OS::get_singleton()->has_feature("template"),
  112. "The binary does not have the \"template\" feature tag.");
  113. CHECK_MESSAGE(
  114. !OS::get_singleton()->has_feature("template_debug"),
  115. "The binary does not have the \"template_debug\" feature tag.");
  116. CHECK_MESSAGE(
  117. !OS::get_singleton()->has_feature("template_release"),
  118. "The binary does not have the \"template_release\" feature tag.");
  119. #else
  120. CHECK_MESSAGE(
  121. !OS::get_singleton()->has_feature("editor"),
  122. "The binary does not have the \"editor\" feature tag.");
  123. CHECK_MESSAGE(
  124. OS::get_singleton()->has_feature("template"),
  125. "The binary has the \"template\" feature tag.");
  126. #ifdef DEBUG_ENABLED
  127. CHECK_MESSAGE(
  128. OS::get_singleton()->has_feature("template_debug"),
  129. "The binary has the \"template_debug\" feature tag.");
  130. CHECK_MESSAGE(
  131. !OS::get_singleton()->has_feature("template_release"),
  132. "The binary does not have the \"template_release\" feature tag.");
  133. #else
  134. CHECK_MESSAGE(
  135. !OS::get_singleton()->has_feature("template_debug"),
  136. "The binary does not have the \"template_debug\" feature tag.");
  137. CHECK_MESSAGE(
  138. OS::get_singleton()->has_feature("template_release"),
  139. "The binary has the \"template_release\" feature tag.");
  140. #endif // DEBUG_ENABLED
  141. #endif // TOOLS_ENABLED
  142. }
  143. TEST_CASE("[OS] Process ID") {
  144. CHECK_MESSAGE(
  145. OS::get_singleton()->get_process_id() >= 1,
  146. "The returned process ID should be greater than zero.");
  147. }
  148. TEST_CASE("[OS] Processor count and memory information") {
  149. CHECK_MESSAGE(
  150. OS::get_singleton()->get_processor_count() >= 1,
  151. "The returned processor count should be greater than zero.");
  152. #ifdef DEBUG_ENABLED
  153. CHECK_MESSAGE(
  154. OS::get_singleton()->get_static_memory_usage() >= 1,
  155. "The returned static memory usage should be greater than zero.");
  156. CHECK_MESSAGE(
  157. OS::get_singleton()->get_static_memory_peak_usage() >= 1,
  158. "The returned static memory peak usage should be greater than zero.");
  159. #endif // DEBUG_ENABLED
  160. }
  161. TEST_CASE("[OS] Execute") {
  162. #ifdef WINDOWS_ENABLED
  163. List<String> arguments;
  164. arguments.push_back("/C");
  165. arguments.push_back("dir > NUL");
  166. int exit_code;
  167. const Error err = OS::get_singleton()->execute("cmd", arguments, nullptr, &exit_code);
  168. CHECK_MESSAGE(
  169. err == OK,
  170. "(Running the command `cmd /C \"dir > NUL\"` returns the expected Godot error code (OK).");
  171. CHECK_MESSAGE(
  172. exit_code == 0,
  173. "Running the command `cmd /C \"dir > NUL\"` returns a zero (successful) exit code.");
  174. #else
  175. List<String> arguments;
  176. arguments.push_back("-c");
  177. arguments.push_back("ls > /dev/null");
  178. int exit_code;
  179. const Error err = OS::get_singleton()->execute("sh", arguments, nullptr, &exit_code);
  180. CHECK_MESSAGE(
  181. err == OK,
  182. "(Running the command `sh -c \"ls > /dev/null\"` returns the expected Godot error code (OK).");
  183. CHECK_MESSAGE(
  184. exit_code == 0,
  185. "Running the command `sh -c \"ls > /dev/null\"` returns a zero (successful) exit code.");
  186. #endif
  187. }
  188. } // namespace TestOS
  189. #endif // TEST_OS_H