gamemode-tests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. Copyright (c) 2017-2018, Feral Interactive
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #define _GNU_SOURCE
  27. #include "gamemode.h"
  28. #include "helpers.h"
  29. #include "logging.h"
  30. #include <libgen.h>
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <unistd.h>
  34. #include "daemon_config.h"
  35. #include "gamemode_client.h"
  36. #include "governors-query.h"
  37. /* Initial verify step to ensure gamemode isn't already active */
  38. static int verify_gamemode_initial(void)
  39. {
  40. int status = 0;
  41. if ((status = gamemode_query_status()) != 0 && status != -1) {
  42. LOG_ERROR("gamemode is currently active, tests require gamemode to start deactivated!\n");
  43. status = -1;
  44. } else if (status == -1) {
  45. LOG_ERROR("gamemode_query_status failed: %s!\n", gamemode_error_string());
  46. LOG_ERROR("is gamemode installed correctly?\n");
  47. status = -1;
  48. } else {
  49. status = 0;
  50. }
  51. return status;
  52. }
  53. /* Check if gamemode is active and this client is registered */
  54. static int verify_active_and_registered(void)
  55. {
  56. int status = gamemode_query_status();
  57. if (status != 2) {
  58. if (status == -1) {
  59. LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
  60. } else if (status == 1) {
  61. LOG_ERROR("gamemode was active but did not have this process registered\n");
  62. }
  63. LOG_ERROR("gamemode failed to activate correctly when requested (expected 2)!\n");
  64. status = -1;
  65. } else {
  66. status = 0;
  67. }
  68. return status;
  69. }
  70. /* Ensure gamemode is deactivated when it should be */
  71. static int verify_deactivated(void)
  72. {
  73. int status = gamemode_query_status();
  74. if (status != 0) {
  75. if (status == -1) {
  76. LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
  77. }
  78. LOG_ERROR("gamemode failed to deactivate when requested (expected 0)!\n");
  79. status = -1;
  80. } else {
  81. status = 0;
  82. }
  83. return status;
  84. }
  85. /* Ensure another client is connected */
  86. static int verify_other_client_connected(void)
  87. {
  88. int status = gamemode_query_status();
  89. if (status != 1) {
  90. if (status == -1) {
  91. LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
  92. }
  93. LOG_ERROR("gamemode_query_status failed to return other client connected (expected 1)!\n");
  94. status = -1;
  95. } else {
  96. status = 0;
  97. }
  98. return status;
  99. }
  100. /* Run basic client tests
  101. * Tests a simple request_start and request_end works
  102. */
  103. static int run_basic_client_tests(void)
  104. {
  105. LOG_MSG(":: Basic client tests\n");
  106. /* First verify that gamemode is not currently active on the system
  107. * As well as it being currently installed and queryable
  108. */
  109. if (verify_gamemode_initial() != 0)
  110. return -1;
  111. /* Verify that gamemode_request_start correctly start gamemode */
  112. if (gamemode_request_start() != 0) {
  113. LOG_ERROR("gamemode_request_start failed: %s\n", gamemode_error_string());
  114. return -1;
  115. }
  116. /* Verify that gamemode is now active and this client is registered*/
  117. if (verify_active_and_registered() != 0)
  118. return -1;
  119. /* Verify that gamemode_request_end corrently de-registers gamemode */
  120. if (gamemode_request_end() != 0) {
  121. LOG_ERROR("gamemode_request_end failed: %s!\n", gamemode_error_string());
  122. return -1;
  123. }
  124. /* Verify that gamemode is now innactive */
  125. if (verify_deactivated() != 0)
  126. return -1;
  127. LOG_MSG(":: Passed\n\n");
  128. return 0;
  129. }
  130. /* Run some dual client tests
  131. * This also tests that the "-r" argument works correctly and cleans up correctly
  132. */
  133. static int run_dual_client_tests(void)
  134. {
  135. int status = 0;
  136. /* Try running some process interop tests */
  137. LOG_MSG(":: Dual client tests\n");
  138. /* Get the current path to this binary */
  139. char mypath[PATH_MAX];
  140. memset(mypath, 0, sizeof(mypath));
  141. if (readlink("/proc/self/exe", mypath, PATH_MAX) == -1) {
  142. LOG_ERROR("could not read current exe path: %s\n", strerror(errno));
  143. return -1;
  144. }
  145. /* Fork so that the child can request gamemode */
  146. int child = fork();
  147. if (child == 0) {
  148. /* Relaunch self with -r (request and wait for signal) */
  149. if (execl(mypath, mypath, "-r", (char *)NULL) == -1) {
  150. LOG_ERROR("failed to re-launch self (%s) with execl: %s\n", mypath, strerror(errno));
  151. return -1;
  152. }
  153. }
  154. /* Parent process */
  155. /* None of these should early-out as we need to clean up the child */
  156. /* Give the child a chance to reqeust gamemode */
  157. usleep(1000);
  158. /* Check that when we request gamemode, it replies that the other client is connected */
  159. if (verify_other_client_connected() != 0)
  160. status = -1;
  161. /* Verify that gamemode_request_start correctly start gamemode */
  162. if (gamemode_request_start() != 0) {
  163. LOG_ERROR("gamemode_request_start failed: %s\n", gamemode_error_string());
  164. status = -1;
  165. }
  166. /* Verify that gamemode is now active and this client is registered*/
  167. if (verify_active_and_registered() != 0)
  168. status = -1;
  169. /* Request end of gamemode (de-register ourselves) */
  170. if (gamemode_request_end() != 0) {
  171. LOG_ERROR("gamemode_request_end failed: %s!\n", gamemode_error_string());
  172. status = -1;
  173. }
  174. /* Check that when we request gamemode, it replies that the other client is connected */
  175. if (verify_other_client_connected() != 0)
  176. status = -1;
  177. /* Send SIGINT to child to wake it up*/
  178. if (kill(child, SIGINT) == -1) {
  179. LOG_ERROR("failed to send continue signal to other client: %s\n", strerror(errno));
  180. status = -1;
  181. }
  182. /* Give the child a chance to finish */
  183. usleep(10000);
  184. // Wait for the child to finish up
  185. int wstatus;
  186. while (waitpid(child, &wstatus, WNOHANG) == 0) {
  187. LOG_MSG("...Waiting for child to quit...\n");
  188. usleep(10000);
  189. }
  190. /* Verify that gamemode is now innactive */
  191. if (verify_deactivated() != 0)
  192. return -1;
  193. if (status == 0)
  194. LOG_MSG(":: Passed\n\n");
  195. return status;
  196. }
  197. /**
  198. * game_mode_run_feature_tests runs a set of tests for each current feature (based on the current
  199. * config) returns 0 for success, -1 for failure
  200. */
  201. static int game_mode_run_feature_tests(void)
  202. {
  203. int status = 0;
  204. LOG_MSG(":: Feature tests\n");
  205. /* If we reach here, we should assume the basic requests and register functions are working */
  206. /* Grab the config */
  207. /* Note: this config may pick up a local gamemode.ini, or the daemon may have one, we may need
  208. * to cope with that */
  209. GameModeConfig *config = config_create();
  210. config_init(config);
  211. /* Does the CPU governor get set properly? */
  212. {
  213. int cpustatus = 0;
  214. LOG_MSG("::: Verifying CPU governor setting\n");
  215. /* get the two config parameters we care about */
  216. char desiredgov[CONFIG_VALUE_MAX] = { 0 };
  217. config_get_desired_governor(config, desiredgov);
  218. if (desiredgov[0] == '\0')
  219. strcpy(desiredgov, "performance");
  220. char defaultgov[CONFIG_VALUE_MAX] = { 0 };
  221. config_get_default_governor(config, defaultgov);
  222. if (desiredgov[0] == '\0') {
  223. const char *currentgov = get_gov_state();
  224. if (currentgov) {
  225. strncpy(desiredgov, currentgov, CONFIG_VALUE_MAX);
  226. } else {
  227. LOG_ERROR(
  228. "Could not get current CPU governor state, this indicates an error! See rest "
  229. "of log.\n");
  230. cpustatus = -1;
  231. }
  232. }
  233. /* Only continue if we had no error before */
  234. if (cpustatus == 0) {
  235. /* Start gamemode */
  236. gamemode_request_start();
  237. /* Verify the governor is the desired one */
  238. const char *currentgov = get_gov_state();
  239. if (strncmp(currentgov, desiredgov, CONFIG_VALUE_MAX) != 0) {
  240. LOG_ERROR("Govenor was not set to %s (was actually %s)!", desiredgov, currentgov);
  241. cpustatus = -1;
  242. }
  243. /* End gamemode */
  244. gamemode_request_end();
  245. /* Verify the governor has been set back */
  246. currentgov = get_gov_state();
  247. if (strncmp(currentgov, defaultgov, CONFIG_VALUE_MAX) != 0) {
  248. LOG_ERROR("Govenor was not set back to %s (was actually %s)!",
  249. defaultgov,
  250. currentgov);
  251. cpustatus = -1;
  252. }
  253. }
  254. if (cpustatus == 0)
  255. LOG_MSG("::: Passed\n");
  256. else {
  257. LOG_MSG("::: Failed!\n");
  258. status = 1;
  259. }
  260. }
  261. /* Do custom scripts run? */
  262. {
  263. int scriptstatus = 0;
  264. LOG_MSG("::: Verifying Scripts\n");
  265. /* Grab and test the start scripts */
  266. char startscripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
  267. memset(startscripts, 0, sizeof(startscripts));
  268. config_get_gamemode_start_scripts(config, startscripts);
  269. if (startscripts[0][0] != '\0') {
  270. int i = 0;
  271. while (*startscripts[i] != '\0' && i < CONFIG_LIST_MAX) {
  272. LOG_MSG(":::: Running start script [%s]\n", startscripts[i]);
  273. int ret = system(startscripts[i]);
  274. if (ret == 0)
  275. LOG_MSG(":::: Passed\n");
  276. else {
  277. LOG_MSG(":::: Failed!\n");
  278. scriptstatus = -1;
  279. }
  280. i++;
  281. }
  282. }
  283. /* Grab and test the end scripts */
  284. char endscripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
  285. memset(endscripts, 0, sizeof(endscripts));
  286. config_get_gamemode_end_scripts(config, endscripts);
  287. if (endscripts[0][0] != '\0') {
  288. int i = 0;
  289. while (*endscripts[i] != '\0' && i < CONFIG_LIST_MAX) {
  290. LOG_MSG(":::: Running end script [%s]\n", endscripts[i]);
  291. int ret = system(endscripts[i]);
  292. if (ret == 0)
  293. LOG_MSG(":::: Passed\n");
  294. else {
  295. LOG_MSG(":::: Failed!\n");
  296. scriptstatus = -1;
  297. }
  298. i++;
  299. }
  300. }
  301. if (endscripts[0][0] == '\0' && startscripts[0][0] == '\0')
  302. LOG_MSG("::: Passed (no scripts configured to run)\n");
  303. else if (scriptstatus == 0)
  304. LOG_MSG("::: Passed\n");
  305. else {
  306. LOG_MSG("::: Failed!\n");
  307. status = 1;
  308. }
  309. }
  310. /* Does the screensaver get inhibited? */
  311. /* TODO */
  312. /* Do GPU optimisations get applied? */
  313. /* TODO */
  314. /* Was the process reniced? */
  315. /* TODO */
  316. /* Was the scheduling applied? */
  317. /* TODO */
  318. /* Were io priorities changed? */
  319. /* TODO */
  320. if (status != -1)
  321. LOG_MSG(":: Passed%s\n\n", status > 0 ? " (with optional failures)" : "");
  322. else
  323. LOG_ERROR(":: Failed!\n");
  324. return status;
  325. }
  326. /**
  327. * game_mode_run_client_tests runs a set of tests of the client code
  328. * we simply verify that the client can request the status and recieves the correct results
  329. *
  330. * returns 0 for success, -1 for failure
  331. */
  332. int game_mode_run_client_tests()
  333. {
  334. int status = 0;
  335. LOG_MSG(": Running tests\n\n");
  336. /* Run the basic tests */
  337. if (run_basic_client_tests() != 0)
  338. status = -1;
  339. /* Run the dual client tests */
  340. if (run_dual_client_tests() != 0)
  341. status = -1;
  342. if (status != 0) {
  343. LOG_MSG(": Client tests failed, skipping feature tests\n");
  344. } else {
  345. /* Run the feature tests */
  346. status = game_mode_run_feature_tests();
  347. }
  348. if (status >= 0)
  349. LOG_MSG(": All Tests Passed%s!\n", status > 0 ? " (with optional failures)" : "");
  350. else
  351. LOG_MSG(": Tests Failed!\n");
  352. return status;
  353. }