File size: 4,227 Bytes
78d2150 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <inttypes.h>
#include <limits.h>
/* Helper to format a pid_t as a decimal string */
static void pid_to_str(pid_t pid, char *buf, size_t buflen) {
snprintf(buf, buflen, "%jd", (intmax_t)pid);
}
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* Test: valid self PID with signal 0 should succeed */
void test_send_signals_self_pid_signal0_success(void) {
char pidbuf[64];
pid_to_str(getpid(), pidbuf, sizeof(pidbuf));
char *argv[] = { pidbuf, NULL };
int rc = send_signals(0, argv);
TEST_ASSERT_EQUAL_INT(EXIT_SUCCESS, rc);
}
/* Test: invalid PID string should fail */
void test_send_signals_invalid_pid_string_failure(void) {
char invalid[] = "abc";
char *argv[] = { invalid, NULL };
int rc = send_signals(0, argv);
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, rc);
}
/* Test: empty string PID should fail */
void test_send_signals_empty_string_failure(void) {
char empty[] = "";
char *argv[] = { empty, NULL };
int rc = send_signals(0, argv);
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, rc);
}
/* Test: PID string with trailing characters should fail */
void test_send_signals_trailing_chars_failure(void) {
char bad[] = "123abc";
char *argv[] = { bad, NULL };
int rc = send_signals(0, argv);
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, rc);
}
/* Test: PID string with trailing space should fail */
void test_send_signals_trailing_space_failure(void) {
char bad[] = "123 ";
char *argv[] = { bad, NULL };
int rc = send_signals(0, argv);
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, rc);
}
/* Test: overflow-sized PID string should fail (ERANGE/ckd_add triggers) */
void test_send_signals_overflow_pid_failure(void) {
char huge[1024];
/* Fill with many '9's to ensure overflow for strtoimax or pid_t conversion */
memset(huge, '9', sizeof(huge)-1);
huge[sizeof(huge)-1] = '\0';
char *argv[] = { huge, NULL };
int rc = send_signals(0, argv);
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, rc);
}
/* Test: invalid signal number (e.g., -1) with valid PID should fail (EINVAL) */
void test_send_signals_invalid_signal_einval_failure(void) {
char pidbuf[64];
pid_to_str(getpid(), pidbuf, sizeof(pidbuf));
char *argv[] = { pidbuf, NULL };
int rc = send_signals(-1, argv);
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, rc);
}
/* Test: nonexistent PID should fail (ESRCH) */
void test_send_signals_nonexistent_pid_esrch_failure(void) {
pid_t child = fork();
TEST_ASSERT_MESSAGE(child >= 0, "fork() failed unexpectedly");
if (child == 0) {
_exit(0);
}
int status = 0;
pid_t w = waitpid(child, &status, 0);
TEST_ASSERT_EQUAL_INT_MESSAGE(child, w, "waitpid did not return child pid");
char pidbuf[64];
pid_to_str(child, pidbuf, sizeof(pidbuf));
char *argv[] = { pidbuf, NULL };
int rc = send_signals(0, argv);
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, rc);
}
/* Test: mixture of valid and invalid operands should yield failure */
void test_send_signals_mixed_operands_returns_failure(void) {
char pidbuf1[64];
pid_to_str(getpid(), pidbuf1, sizeof(pidbuf1));
char bad[] = "notanumber";
char pidbuf2[64];
pid_to_str(getpid(), pidbuf2, sizeof(pidbuf2));
char *argv[] = { pidbuf1, bad, pidbuf2, NULL };
int rc = send_signals(0, argv);
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, rc);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_send_signals_self_pid_signal0_success);
RUN_TEST(test_send_signals_invalid_pid_string_failure);
RUN_TEST(test_send_signals_empty_string_failure);
RUN_TEST(test_send_signals_trailing_chars_failure);
RUN_TEST(test_send_signals_trailing_space_failure);
RUN_TEST(test_send_signals_overflow_pid_failure);
RUN_TEST(test_send_signals_invalid_signal_einval_failure);
RUN_TEST(test_send_signals_nonexistent_pid_esrch_failure);
RUN_TEST(test_send_signals_mixed_operands_returns_failure);
return UNITY_END();
} |