| 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 |
#include "tests.h" |
| 28 |
|
| 29 |
static pool *p = NULL; |
| 30 |
|
| 31 |
static void set_up(void) { |
| 32 |
if (p == NULL) { |
| 33 |
p = make_sub_pool(NULL); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
static void tear_down(void) { |
| 38 |
if (p) { |
| 39 |
destroy_pool(p); |
| 40 |
p = NULL; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
START_TEST (rlimit_core_test) { |
| 45 |
int res; |
| 46 |
rlim_t curr_rlim = 0, max_rlim = 0 ; |
| 47 |
|
| 48 |
res = pr_rlimit_get_core(NULL, NULL); |
| 49 |
ck_assert_msg(res == -1, "Failed to handle null arguments"); |
| 50 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", |
| 51 |
strerror(errno), errno); |
| 52 |
|
| 53 |
res = pr_rlimit_get_core(&curr_rlim, &max_rlim); |
| 54 |
ck_assert_msg(res == 0, "Failed to get core resource limits: %s", |
| 55 |
strerror(errno)); |
| 56 |
|
| 57 |
curr_rlim = max_rlim = -1; |
| 58 |
res = pr_rlimit_set_core(curr_rlim, max_rlim); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
if (res < 0) { |
| 64 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)", |
| 65 |
strerror(errno), errno); |
| 66 |
} |
| 67 |
} |
| 68 |
END_TEST |
| 69 |
|
| 70 |
START_TEST (rlimit_cpu_test) { |
| 71 |
int res; |
| 72 |
rlim_t curr_rlim = 0, max_rlim = 0 ; |
| 73 |
|
| 74 |
res = pr_rlimit_get_cpu(NULL, NULL); |
| 75 |
ck_assert_msg(res == -1, "Failed to handle null arguments"); |
| 76 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", |
| 77 |
strerror(errno), errno); |
| 78 |
|
| 79 |
res = pr_rlimit_get_cpu(&curr_rlim, &max_rlim); |
| 80 |
ck_assert_msg(res == 0, "Failed to get CPU resource limits: %s", |
| 81 |
strerror(errno)); |
| 82 |
|
| 83 |
curr_rlim = max_rlim = -1; |
| 84 |
res = pr_rlimit_set_cpu(curr_rlim, max_rlim); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if (res < 0) { |
| 90 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)", |
| 91 |
strerror(errno), errno); |
| 92 |
} |
| 93 |
} |
| 94 |
END_TEST |
| 95 |
|
| 96 |
START_TEST (rlimit_files_test) { |
| 97 |
int res; |
| 98 |
rlim_t curr_rlim = 0, max_rlim = 0 ; |
| 99 |
|
| 100 |
res = pr_rlimit_get_files(NULL, NULL); |
| 101 |
ck_assert_msg(res == -1, "Failed to handle null arguments"); |
| 102 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", |
| 103 |
strerror(errno), errno); |
| 104 |
|
| 105 |
res = pr_rlimit_get_files(&curr_rlim, &max_rlim); |
| 106 |
ck_assert_msg(res == 0, "Failed to get file resource limits: %s", |
| 107 |
strerror(errno)); |
| 108 |
|
| 109 |
curr_rlim = max_rlim = -1; |
| 110 |
res = pr_rlimit_set_files(curr_rlim, max_rlim); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
if (res < 0) { |
| 116 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)", |
| 117 |
strerror(errno), errno); |
| 118 |
} |
| 119 |
} |
| 120 |
END_TEST |
| 121 |
|
| 122 |
START_TEST (rlimit_memory_test) { |
| 123 |
int res; |
| 124 |
rlim_t curr_rlim = 0, max_rlim = 0 ; |
| 125 |
|
| 126 |
res = pr_rlimit_get_memory(NULL, NULL); |
| 127 |
ck_assert_msg(res == -1, "Failed to handle null arguments"); |
| 128 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", |
| 129 |
strerror(errno), errno); |
| 130 |
|
| 131 |
res = pr_rlimit_get_memory(&curr_rlim, &max_rlim); |
| 132 |
ck_assert_msg(res == 0, "Failed to get memory resource limits: %s", |
| 133 |
strerror(errno)); |
| 134 |
|
| 135 |
curr_rlim = max_rlim = -1; |
| 136 |
res = pr_rlimit_set_memory(curr_rlim, max_rlim); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if (res < 0) { |
| 142 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)", |
| 143 |
strerror(errno), errno); |
| 144 |
} |
| 145 |
} |
| 146 |
END_TEST |
| 147 |
|
| 148 |
#ifdef RLIMIT_NPROC |
| 149 |
START_TEST (rlimit_nproc_test) { |
| 150 |
int res; |
| 151 |
rlim_t curr_rlim = 0, max_rlim = 0 ; |
| 152 |
|
| 153 |
res = pr_rlimit_get_nproc(NULL, NULL); |
| 154 |
ck_assert_msg(res == -1, "Failed to handle null arguments"); |
| 155 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", |
| 156 |
strerror(errno), errno); |
| 157 |
|
| 158 |
res = pr_rlimit_get_nproc(&curr_rlim, &max_rlim); |
| 159 |
ck_assert_msg(res == 0, "Failed to get nproc resource limits: %s", |
| 160 |
strerror(errno)); |
| 161 |
|
| 162 |
curr_rlim = max_rlim = -1; |
| 163 |
res = pr_rlimit_set_nproc(curr_rlim, max_rlim); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if (res < 0) { |
| 169 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)", |
| 170 |
strerror(errno), errno); |
| 171 |
} |
| 172 |
} |
| 173 |
END_TEST |
| 174 |
#endif |
| 175 |
|
| 176 |
Suite *tests_get_rlimit_suite(void) { |
| 177 |
Suite *suite; |
| 178 |
TCase *testcase; |
| 179 |
|
| 180 |
suite = suite_create("rlimit"); |
| 181 |
testcase = tcase_create("base"); |
| 182 |
|
| 183 |
tcase_add_checked_fixture(testcase, set_up, tear_down); |
| 184 |
|
| 185 |
tcase_add_test(testcase, rlimit_core_test); |
| 186 |
tcase_add_test(testcase, rlimit_cpu_test); |
| 187 |
tcase_add_test(testcase, rlimit_files_test); |
| 188 |
tcase_add_test(testcase, rlimit_memory_test); |
| 189 |
#ifdef RLIMIT_NPROC |
| 190 |
tcase_add_test(testcase, rlimit_nproc_test); |
| 191 |
#endif |
| 192 |
|
| 193 |
suite_add_tcase(suite, testcase); |
| 194 |
return suite; |
| 195 |
} |