| 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 |
|
| 32 |
|
| 33 |
static void set_up(void) { |
| 34 |
if (p == NULL) { |
| 35 |
p = permanent_pool = make_sub_pool(NULL); |
| 36 |
} |
| 37 |
|
| 38 |
(void) var_init(); |
| 39 |
} |
| 40 |
|
| 41 |
static void tear_down(void) { |
| 42 |
(void) var_free(); |
| 43 |
|
| 44 |
if (p) { |
| 45 |
destroy_pool(p); |
| 46 |
p = NULL; |
| 47 |
permanent_pool = NULL; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static const char *var_cb(void *data, size_t datasz) { |
| 54 |
return "baz"; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
START_TEST (var_set_test) { |
| 60 |
int res; |
| 61 |
const char *key; |
| 62 |
|
| 63 |
(void) var_free(); |
| 64 |
|
| 65 |
res = pr_var_set(NULL, NULL, NULL, 0, NULL, NULL, 0); |
| 66 |
ck_assert_msg(res == -1, "Failed to handle uninitialized Var table"); |
| 67 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM"); |
| 68 |
|
| 69 |
(void) var_init(); |
| 70 |
|
| 71 |
res = pr_var_set(NULL, NULL, NULL, 0, NULL, NULL, 0); |
| 72 |
ck_assert_msg(res == -1, "Failed to handle null arguments"); |
| 73 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 74 |
|
| 75 |
res = pr_var_set(p, NULL, NULL, 0, NULL, NULL, 0); |
| 76 |
ck_assert_msg(res == -1, "Failed to handle null key"); |
| 77 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 78 |
|
| 79 |
key = "fo"; |
| 80 |
|
| 81 |
res = pr_var_set(p, key, NULL, 0, NULL, NULL, 0); |
| 82 |
ck_assert_msg(res == -1, "Failed to handle null val"); |
| 83 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 84 |
|
| 85 |
res = pr_var_set(p, key, NULL, 0, "bar", NULL, 0); |
| 86 |
ck_assert_msg(res == -1, "Failed to handle bad key name"); |
| 87 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 88 |
|
| 89 |
key = "fooo"; |
| 90 |
|
| 91 |
res = pr_var_set(p, key, NULL, 0, "bar", NULL, 0); |
| 92 |
ck_assert_msg(res == -1, "Failed to handle bad key name"); |
| 93 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 94 |
|
| 95 |
key = "%{foo"; |
| 96 |
|
| 97 |
res = pr_var_set(p, key, NULL, 0, "bar", NULL, 0); |
| 98 |
ck_assert_msg(res == -1, "Failed to handle bad key name"); |
| 99 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 100 |
|
| 101 |
key = "%{}"; |
| 102 |
|
| 103 |
res = pr_var_set(p, key, NULL, 0, "bar", NULL, 0); |
| 104 |
ck_assert_msg(res == -1, "Failed to handle bad key name"); |
| 105 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 106 |
|
| 107 |
key = "%{foo}"; |
| 108 |
|
| 109 |
res = pr_var_set(p, key, NULL, 0, "bar", NULL, 0); |
| 110 |
ck_assert_msg(res == -1, "Failed to handle unknown type"); |
| 111 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 112 |
|
| 113 |
res = pr_var_set(p, key, NULL, PR_VAR_TYPE_STR, "bar", "bar", 0); |
| 114 |
ck_assert_msg(res == -1, "Failed to handle data with zero len"); |
| 115 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 116 |
|
| 117 |
res = pr_var_set(p, key, NULL, PR_VAR_TYPE_STR, "bar", NULL, 1); |
| 118 |
ck_assert_msg(res == -1, "Failed to handle null data with non-zero len"); |
| 119 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 120 |
|
| 121 |
res = pr_var_set(p, key, NULL, PR_VAR_TYPE_STR, "bar", NULL, 0); |
| 122 |
ck_assert_msg(res == 0, "Failed to add str var: %s", strerror(errno)); |
| 123 |
|
| 124 |
res = pr_var_set(p, key, "test", PR_VAR_TYPE_FUNC, var_cb, NULL, 0); |
| 125 |
ck_assert_msg(res == 0, "Failed to add cb var: %s", strerror(errno)); |
| 126 |
} |
| 127 |
END_TEST |
| 128 |
|
| 129 |
START_TEST (var_delete_test) { |
| 130 |
int res; |
| 131 |
const char *key = "%{foo}"; |
| 132 |
|
| 133 |
(void) var_free(); |
| 134 |
|
| 135 |
res = pr_var_delete(NULL); |
| 136 |
ck_assert_msg(res == -1, "Failed to handle uninitialized Var table"); |
| 137 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM"); |
| 138 |
|
| 139 |
(void) var_init(); |
| 140 |
|
| 141 |
res = pr_var_delete(NULL); |
| 142 |
ck_assert_msg(res == -1, "Failed to handle null key"); |
| 143 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 144 |
|
| 145 |
res = pr_var_delete(key); |
| 146 |
ck_assert_msg(res == -1, "Failed to handle absent key"); |
| 147 |
ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT"); |
| 148 |
|
| 149 |
res = pr_var_set(p, key, "test", PR_VAR_TYPE_STR, "bar", NULL, 0); |
| 150 |
ck_assert_msg(res == 0, "Failed to add var: %s", strerror(errno)); |
| 151 |
|
| 152 |
res = pr_var_delete(key); |
| 153 |
ck_assert_msg(res == 0, "Failed to delete var: %s", strerror(errno)); |
| 154 |
|
| 155 |
res = pr_var_delete(key); |
| 156 |
ck_assert_msg(res == -1, "Failed to handle absent key"); |
| 157 |
ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT"); |
| 158 |
} |
| 159 |
END_TEST |
| 160 |
|
| 161 |
START_TEST (var_exists_test) { |
| 162 |
int res; |
| 163 |
const char *key; |
| 164 |
|
| 165 |
(void) var_free(); |
| 166 |
|
| 167 |
res = pr_var_exists(NULL); |
| 168 |
ck_assert_msg(res == -1, "Failed to handle uninitialized Var table"); |
| 169 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM"); |
| 170 |
|
| 171 |
(void) var_init(); |
| 172 |
|
| 173 |
res = pr_var_exists(NULL); |
| 174 |
ck_assert_msg(res == -1, "Failed to handle null key"); |
| 175 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 176 |
|
| 177 |
key = "%{foo}"; |
| 178 |
res = pr_var_exists(key); |
| 179 |
ck_assert_msg(res == FALSE, "Failed to handle absent key"); |
| 180 |
|
| 181 |
res = pr_var_set(p, key, NULL, PR_VAR_TYPE_STR, "bar", NULL, 0); |
| 182 |
ck_assert_msg(res == 0, "Failed to add var: %s", strerror(errno)); |
| 183 |
|
| 184 |
res = pr_var_exists(key); |
| 185 |
ck_assert_msg(res == TRUE, "Failed to detect present key"); |
| 186 |
} |
| 187 |
END_TEST |
| 188 |
|
| 189 |
START_TEST (var_get_test) { |
| 190 |
int ok; |
| 191 |
const char *key, *res; |
| 192 |
|
| 193 |
(void) var_free(); |
| 194 |
|
| 195 |
res = pr_var_get(NULL); |
| 196 |
ck_assert_msg(res == NULL, "Failed to handle uninitialized Var table"); |
| 197 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM"); |
| 198 |
|
| 199 |
(void) var_init(); |
| 200 |
|
| 201 |
res = pr_var_get(NULL); |
| 202 |
ck_assert_msg(res == NULL, "Failed to handle null key"); |
| 203 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 204 |
|
| 205 |
key = "%{foo}"; |
| 206 |
|
| 207 |
res = pr_var_get(key); |
| 208 |
ck_assert_msg(res == NULL, "Failed to absent key"); |
| 209 |
ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT"); |
| 210 |
|
| 211 |
ok = pr_var_set(p, key, NULL, PR_VAR_TYPE_STR, "bar", NULL, 0); |
| 212 |
ck_assert_msg(ok == 0, "Failed to add str var: %s", strerror(errno)); |
| 213 |
|
| 214 |
res = pr_var_get(key); |
| 215 |
ck_assert_msg(res != NULL, "Failed to get str var: %s", strerror(errno)); |
| 216 |
ck_assert_msg(strcmp(res, "bar") == 0, "Expected '%s', got '%s'", "bar", res); |
| 217 |
|
| 218 |
ok = pr_var_set(p, key, "test", PR_VAR_TYPE_FUNC, var_cb, NULL, 0); |
| 219 |
ck_assert_msg(ok == 0, "Failed to add cb var: %s", strerror(errno)); |
| 220 |
|
| 221 |
res = pr_var_get(key); |
| 222 |
ck_assert_msg(res != NULL, "Failed to get str var: %s", strerror(errno)); |
| 223 |
ck_assert_msg(strcmp(res, "baz") == 0, "Expected '%s', got '%s'", "baz", res); |
| 224 |
} |
| 225 |
END_TEST |
| 226 |
|
| 227 |
START_TEST (var_next_test) { |
| 228 |
int ok; |
| 229 |
const char *res, *desc; |
| 230 |
|
| 231 |
(void) var_free(); |
| 232 |
|
| 233 |
res = pr_var_next(NULL); |
| 234 |
ck_assert_msg(res == NULL, "Failed to handle uninitialized Var table"); |
| 235 |
ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM"); |
| 236 |
|
| 237 |
(void) var_init(); |
| 238 |
|
| 239 |
res = pr_var_next(NULL); |
| 240 |
ck_assert_msg(res == NULL, "Failed to handle empty table"); |
| 241 |
|
| 242 |
ok = pr_var_set(p, "%{foo}", NULL, PR_VAR_TYPE_STR, "bar", NULL, 0); |
| 243 |
ck_assert_msg(ok == 0, "Failed to add var: %s", strerror(errno)); |
| 244 |
|
| 245 |
res = pr_var_next(&desc); |
| 246 |
ck_assert_msg(res != NULL, "Failed to get next key: %s", strerror(errno)); |
| 247 |
ck_assert_msg(desc == NULL, "Expected no desc, got '%s'", desc); |
| 248 |
|
| 249 |
res = pr_var_next(&desc); |
| 250 |
ck_assert_msg(res == NULL, "Expected no more keys, got '%s'", res); |
| 251 |
} |
| 252 |
END_TEST |
| 253 |
|
| 254 |
START_TEST (var_rewind_test) { |
| 255 |
int ok; |
| 256 |
const char *res, *desc; |
| 257 |
|
| 258 |
(void) var_free(); |
| 259 |
|
| 260 |
mark_point(); |
| 261 |
pr_var_rewind(); |
| 262 |
|
| 263 |
(void) var_init(); |
| 264 |
|
| 265 |
pr_var_rewind(); |
| 266 |
|
| 267 |
ok = pr_var_set(p, "%{foo}", "test", PR_VAR_TYPE_STR, "bar", NULL, 0); |
| 268 |
ck_assert_msg(ok == 0, "Failed to add var: %s", strerror(errno)); |
| 269 |
|
| 270 |
res = pr_var_next(&desc); |
| 271 |
ck_assert_msg(res != NULL, "Failed to get next key: %s", strerror(errno)); |
| 272 |
ck_assert_msg(desc != NULL, "Expected non-null desc"); |
| 273 |
ck_assert_msg(strcmp(desc, "test") == 0, "Expected desc '%s', got '%s'", |
| 274 |
"test", desc); |
| 275 |
|
| 276 |
res = pr_var_next(&desc); |
| 277 |
ck_assert_msg(res == NULL, "Expected no more keys, got '%s'", res); |
| 278 |
|
| 279 |
pr_var_rewind(); |
| 280 |
|
| 281 |
res = pr_var_next(&desc); |
| 282 |
ck_assert_msg(res != NULL, "Failed to get next key: %s", strerror(errno)); |
| 283 |
ck_assert_msg(desc != NULL, "Expected non-null desc"); |
| 284 |
ck_assert_msg(strcmp(desc, "test") == 0, "Expected desc '%s', got '%s'", |
| 285 |
"test", desc); |
| 286 |
|
| 287 |
res = pr_var_next(&desc); |
| 288 |
ck_assert_msg(res == NULL, "Expected no more keys, got '%s'", res); |
| 289 |
} |
| 290 |
END_TEST |
| 291 |
|
| 292 |
Suite *tests_get_var_suite(void) { |
| 293 |
Suite *suite; |
| 294 |
TCase *testcase; |
| 295 |
|
| 296 |
suite = suite_create("var"); |
| 297 |
testcase = tcase_create("base"); |
| 298 |
|
| 299 |
tcase_add_checked_fixture(testcase, set_up, tear_down); |
| 300 |
|
| 301 |
tcase_add_test(testcase, var_set_test); |
| 302 |
tcase_add_test(testcase, var_delete_test); |
| 303 |
tcase_add_test(testcase, var_exists_test); |
| 304 |
tcase_add_test(testcase, var_get_test); |
| 305 |
tcase_add_test(testcase, var_next_test); |
| 306 |
tcase_add_test(testcase, var_rewind_test); |
| 307 |
|
| 308 |
suite_add_tcase(suite, testcase); |
| 309 |
return suite; |
| 310 |
} |