void test2() { assert(get_blocked_count() == 0); int status; char *argva[] = {A_NAME, 0}; char *argvb[] = {B_NAME, 0}; if (!fork()) { assert(get_blocked_count() == 0); execv(PROG_A, argva); exit(1); // shouldn't happen } wait(&status); assert(WEXITSTATUS(status) == 0); // assuming a.out exits with 0 assert(block_program(PROG_A, strlen(PROG_A)) == 0); assert(block_program(PROG_B, strlen(PROG_B)) == 0); assert(block_program(PROG_A, strlen(PROG_A)) == 1); assert(block_program(PROG_B, strlen(PROG_B)) == 1); if (!fork()) { assert(get_blocked_count() == 2); execv(PROG_B, argvb); execv(PROG_A, argva); execv(PROG_B, argvb); int cpid = fork(); if (!cpid) { char log[20][256]; int n = get_forbidden_tries(getppid(), log, 2); assert(n == 2); assert(strncmp(log[0], PROG_B, strlen(PROG_B)) == 0); assert(strncmp(log[1], PROG_A, strlen(PROG_A)) == 0); exit(1); } int status2; waitpid(cpid, &status2, 0); assert(WEXITSTATUS(status2) == 1); exit(1); } wait(&status); assert(WEXITSTATUS(status) == 1); assert(unblock_program(PROG_A, strlen(PROG_A)) == 0); assert(unblock_program(PROG_B, strlen(PROG_B)) == 0); assert(get_blocked_count() == 0); }
bool get_blocked_count_test() { ASSERT_TEST(get_blocked_count() == 0); const char* name1 = "prog1"; const char* name2 = "prog2"; const char* name3 = "prog3"; block_program(name1, 5); ASSERT_TEST(get_blocked_count() == 1); block_program(name1, 5); ASSERT_TEST(get_blocked_count() == 1); block_program(name2, 5); ASSERT_TEST(get_blocked_count() == 2); unblock_program(name1, 5); ASSERT_TEST(get_blocked_count() == 1); block_program(NULL, 5); ASSERT_TEST(get_blocked_count() == 1); block_program(name3, 0); ASSERT_TEST(get_blocked_count() == 1); unblock_program(name2, 5); ASSERT_TEST(get_blocked_count() == 0); return true; }
void test1() { /* Assuming no blocked programs before the test runs. If that's not * the case, you should unblock any programs before */ assert(get_blocked_count() == 0); assert(block_program(0, strlen(PROG_A)) == -1); assert(errno == EINVAL); assert(is_program_blocked(PROG_A, strlen(PROG_A)) == 0); assert(block_program(PROG_A, strlen(PROG_A)) == 0); assert(is_program_blocked(PROG_A, strlen(PROG_A)) == 1); assert(block_program(PROG_A, strlen(PROG_A)) == 1); assert(get_blocked_count() == 1); assert(block_program(PROG_B, strlen(PROG_B)) == 0); assert(block_program(PROG_B, strlen(PROG_B)) == 1); assert(get_blocked_count() == 2); char *argva[] = {A_NAME, 0}; char *argvb[] = {B_NAME, 0}; char log[20][256]; // should all fail execv(PROG_A, argva); assert(get_forbidden_tries(getpid(), log, 20)==1); execv(PROG_B, argvb); assert(get_forbidden_tries(getpid(), log, 20)==2); execv(PROG_A, argva); assert(get_forbidden_tries(getpid(), log, 20)==3); execv(PROG_A, argva); assert(get_forbidden_tries(getpid(), log, 20)==4); execv(PROG_B, argvb); assert(get_forbidden_tries(getpid(), log, 20)==5); execv(PROG_B, argvb); assert(get_forbidden_tries(getpid(), log, 20)==6); assert(get_forbidden_tries(getpid(), log, 0) == -1); assert(errno == EINVAL); assert(get_forbidden_tries(-5, log, 20) == -1); assert(errno == ESRCH); assert(get_forbidden_tries(getpid(), (void*)5, 20) == -1); assert(errno == EFAULT); int n = get_forbidden_tries(getpid(), log, 20); assert(n == 6); assert(strncmp(log[0], PROG_A, strlen(PROG_A)) == 0); assert(strncmp(log[1], PROG_B, strlen(PROG_B)) == 0); assert(strncmp(log[2], PROG_A, strlen(PROG_A)) == 0); assert(strncmp(log[3], PROG_A, strlen(PROG_A)) == 0); assert(strncmp(log[4], PROG_B, strlen(PROG_B)) == 0); assert(strncmp(log[5], PROG_B, strlen(PROG_B)) == 0); assert(unblock_program(0, 11) == -1); assert(errno == EINVAL); assert(unblock_program(PROG_A, strlen(PROG_A)) == 0); assert(unblock_program(PROG_A, strlen(PROG_A)) == 1); assert(is_program_blocked(PROG_B, strlen(PROG_B)) == 1); assert(get_blocked_count() == 1); assert(unblock_program(PROG_B, strlen(PROG_B)) == 0); assert(is_program_blocked(PROG_A, strlen(PROG_A)) == 0); assert(is_program_blocked(PROG_B, strlen(PROG_B)) == 0); assert(unblock_program(PROG_B, strlen(PROG_B)) == 1); assert(is_program_blocked(PROG_A, strlen(PROG_A)) == 0); assert(get_blocked_count() == 0); }