123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- #define _GNU_SOURCE
- #include "gamemode.h"
- #include "helpers.h"
- #include "logging.h"
- #include <libgen.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include "daemon_config.h"
- #include "gamemode_client.h"
- #include "governors-query.h"
- static int verify_gamemode_initial(void)
- {
- int status = 0;
- if ((status = gamemode_query_status()) != 0 && status != -1) {
- LOG_ERROR("gamemode is currently active, tests require gamemode to start deactivated!\n");
- status = -1;
- } else if (status == -1) {
- LOG_ERROR("gamemode_query_status failed: %s!\n", gamemode_error_string());
- LOG_ERROR("is gamemode installed correctly?\n");
- status = -1;
- } else {
- status = 0;
- }
- return status;
- }
- static int verify_active_and_registered(void)
- {
- int status = gamemode_query_status();
- if (status != 2) {
- if (status == -1) {
- LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
- } else if (status == 1) {
- LOG_ERROR("gamemode was active but did not have this process registered\n");
- }
- LOG_ERROR("gamemode failed to activate correctly when requested (expected 2)!\n");
- status = -1;
- } else {
- status = 0;
- }
- return status;
- }
- static int verify_deactivated(void)
- {
- int status = gamemode_query_status();
- if (status != 0) {
- if (status == -1) {
- LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
- }
- LOG_ERROR("gamemode failed to deactivate when requested (expected 0)!\n");
- status = -1;
- } else {
- status = 0;
- }
- return status;
- }
- static int verify_other_client_connected(void)
- {
- int status = gamemode_query_status();
- if (status != 1) {
- if (status == -1) {
- LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
- }
- LOG_ERROR("gamemode_query_status failed to return other client connected (expected 1)!\n");
- status = -1;
- } else {
- status = 0;
- }
- return status;
- }
- static int run_basic_client_tests(void)
- {
- LOG_MSG(" *basic client tests*\n");
-
- if (verify_gamemode_initial() != 0)
- return -1;
-
- if (gamemode_request_start() != 0) {
- LOG_ERROR("gamemode_request_start failed: %s\n", gamemode_error_string());
- return -1;
- }
-
- if (verify_active_and_registered() != 0)
- return -1;
-
- if (gamemode_request_end() != 0) {
- LOG_ERROR("gamemode_request_end failed: %s!\n", gamemode_error_string());
- return -1;
- }
-
- if (verify_deactivated() != 0)
- return -1;
- LOG_MSG(" *passed*\n");
- return 0;
- }
- static int run_dual_client_tests(void)
- {
- int status = 0;
-
- LOG_MSG(" *dual clients tests*\n");
-
- char mypath[PATH_MAX];
- memset(mypath, 0, sizeof(mypath));
- if (readlink("/proc/self/exe", mypath, PATH_MAX) == -1) {
- LOG_ERROR("could not read current exe path: %s\n", strerror(errno));
- return -1;
- }
-
- int child = fork();
- if (child == 0) {
-
- if (execl(mypath, mypath, "-r", (char *)NULL) == -1) {
- LOG_ERROR("failed to re-launch self (%s) with execl: %s\n", mypath, strerror(errno));
- return -1;
- }
- }
-
-
-
- usleep(1000);
-
- if (verify_other_client_connected() != 0)
- status = -1;
-
- if (gamemode_request_start() != 0) {
- LOG_ERROR("gamemode_request_start failed: %s\n", gamemode_error_string());
- status = -1;
- }
-
- if (verify_active_and_registered() != 0)
- status = -1;
-
- if (gamemode_request_end() != 0) {
- LOG_ERROR("gamemode_request_end failed: %s!\n", gamemode_error_string());
- status = -1;
- }
-
- if (verify_other_client_connected() != 0)
- status = -1;
-
- if (kill(child, SIGINT) == -1) {
- LOG_ERROR("failed to send continue signal to other client: %s\n", strerror(errno));
- status = -1;
- }
-
- usleep(10000);
-
- int wstatus;
- while (waitpid(child, &wstatus, WNOHANG) == 0) {
- LOG_MSG(" Waiting for child to quit...\n");
- usleep(10000);
- }
-
- if (verify_deactivated() != 0)
- return -1;
- if (status == 0)
- LOG_MSG(" *passed*\n");
- return status;
- }
- static int game_mode_run_feature_tests(void)
- {
- int status = 0;
- LOG_MSG(" *feature tests*\n");
-
-
-
- GameModeConfig *config = config_create();
- config_init(config);
-
- int cpustatus = 0;
- {
-
- char desiredgov[CONFIG_VALUE_MAX] = { 0 };
- config_get_desired_governor(config, desiredgov);
- if (desiredgov[0] == '\0')
- strcpy(desiredgov, "performance");
- char defaultgov[CONFIG_VALUE_MAX] = { 0 };
- config_get_default_governor(config, defaultgov);
- if (desiredgov[0] == '\0') {
- const char *currentgov = get_gov_state();
- if (currentgov) {
- strncpy(desiredgov, currentgov, CONFIG_VALUE_MAX);
- } else {
- LOG_ERROR(
- "Could not get current CPU governor state, this indicates an error! See rest "
- "of log.\n");
- cpustatus = -1;
- }
- }
-
- if (cpustatus == 0) {
-
- gamemode_request_start();
-
- const char *currentgov = get_gov_state();
- if (strncmp(currentgov, desiredgov, CONFIG_VALUE_MAX) != 0) {
- LOG_ERROR("Govenor was not set to %s (was actually %s)!", desiredgov, currentgov);
- cpustatus = -1;
- }
-
- gamemode_request_end();
-
- currentgov = get_gov_state();
- if (strncmp(currentgov, defaultgov, CONFIG_VALUE_MAX) != 0) {
- LOG_ERROR("Govenor was not set back to %s (was actually %s)!",
- defaultgov,
- currentgov);
- cpustatus = -1;
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- if (status != -1)
- LOG_MSG(" *passed*%s\n", status > 0 ? " (with optional failures)" : "");
- return status;
- }
- int game_mode_run_client_tests()
- {
- int status = 0;
- LOG_MSG("Running tests...\n");
-
- if (run_basic_client_tests() != 0)
- return -1;
-
- if (run_dual_client_tests() != 0)
- return -1;
-
- if (game_mode_run_feature_tests() != 0)
- return -1;
- return status;
- }
|