123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #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 "gamemode_client.h"
- static int verify_gamemode_initial(void)
- {
- int status = 0;
- if ((status = gamemode_query_status()) != 0 && status != -1) {
- fprintf(
- stderr,
- "ERROR: gamemode is currently active, tests require gamemode to start deactivated!\n");
- status = -1;
- } else if (status == -1) {
- fprintf(stderr, "ERROR: gamemode_query_status failed: %s!\n", gamemode_error_string());
- fprintf(stderr, "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) {
- fprintf(stderr, "ERROR: gamemode_query_status failed: %s\n", gamemode_error_string());
- } else if (status == 1) {
- fprintf(stderr,
- "ERROR: gamemode was active but did not have this process registered\n");
- }
- fprintf(stderr,
- "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) {
- fprintf(stderr, "ERROR: gamemode_query_status failed: %s\n", gamemode_error_string());
- }
- fprintf(stderr, "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) {
- fprintf(stderr, "ERROR: gamemode_query_status failed: %s\n", gamemode_error_string());
- }
- fprintf(
- stderr,
- "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)
- {
- fprintf(stdout, " *basic client tests*\n");
-
- if (verify_gamemode_initial() != 0)
- return -1;
-
- if (gamemode_request_start() != 0) {
- fprintf(stderr, "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) {
- fprintf(stderr, "ERROR: gamemode_request_end failed: %s!\n", gamemode_error_string());
- return -1;
- }
-
- if (verify_deactivated() != 0)
- return -1;
- fprintf(stdout, " *passed*\n");
- return 0;
- }
- static int run_dual_client_tests(void)
- {
- int status = 0;
-
- fprintf(stdout, " *dual clients tests*\n");
-
- char mypath[PATH_MAX];
- if (readlink("/proc/self/exe", mypath, PATH_MAX) == -1) {
- fprintf(stderr, "ERROR: could not read current exe path: %s\n", strerror(errno));
- return -1;
- }
-
- int child = fork();
- if (child == 0) {
-
- if (execl(mypath, mypath, "-r") == -1) {
- fprintf(stderr, "ERROR: failed to re-launch self with execv: %s\n", strerror(errno));
- return -1;
- }
- }
-
-
-
- usleep(1000);
-
- if (verify_other_client_connected() != 0)
- status = -1;
-
- if (gamemode_request_start() != 0) {
- fprintf(stderr, "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) {
- fprintf(stderr, "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) {
- fprintf(stderr,
- "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) {
- fprintf(stdout, " Waiting for child to quit...\n");
- usleep(10000);
- }
-
- if (verify_deactivated() != 0)
- return -1;
- if (status == 0)
- fprintf(stdout, " *passed*\n");
- return status;
- }
- int game_mode_run_client_tests()
- {
- int status = 0;
- fprintf(stdout, "Running tests...\n");
-
- if (run_basic_client_tests() != 0)
- return -1;
-
- if (run_dual_client_tests() != 0)
- return -1;
- return status;
- }
|