journal: Refactor test-catalog importing tests

One function per test. Remove shared state between tests.
This commit is contained in:
Stef Walter 2015-11-08 12:51:51 +01:00
parent dbae138dc1
commit 9d85882aa8

View file

@ -46,10 +46,16 @@ static const char *no_catalog_dirs[] = {
NULL
};
static void test_import(Hashmap *h, const char* contents, ssize_t size, int code) {
static Hashmap * test_import(const char* contents, ssize_t size, int code) {
int r;
char name[] = "/tmp/test-catalog.XXXXXX";
_cleanup_close_ int fd;
Hashmap *h;
if (size < 0)
size = strlen(contents);
assert_se(h = hashmap_new(&catalog_hash_ops));
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
@ -59,45 +65,50 @@ static void test_import(Hashmap *h, const char* contents, ssize_t size, int code
assert_se(r == code);
unlink(name);
return h;
}
static void test_catalog_importing(void) {
Hashmap *h;
static void test_catalog_import_invalid(void) {
_cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
assert_se(h = hashmap_new(&catalog_hash_ops));
#define BUF "xxx"
test_import(h, BUF, sizeof(BUF), -EINVAL);
#undef BUF
h = test_import("xxx", -1, -EINVAL);
assert_se(hashmap_isempty(h));
log_debug("----------------------------------------");
}
#define BUF \
static void test_catalog_import_badid(void) {
_cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
const char *input =
"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
"Subject: message\n" \
"\n" \
"payload\n"
test_import(h, BUF, sizeof(BUF), -EINVAL);
#undef BUF
"payload\n";
h = test_import(input, -1, -EINVAL);
}
log_debug("----------------------------------------");
static void test_catalog_import_one(void) {
_cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
char *payload;
Iterator j;
#define BUF \
const char *input =
"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
"Subject: message\n" \
"\n" \
"payload\n"
test_import(h, BUF, sizeof(BUF), 0);
#undef BUF
"payload\n";
const char *expect =
"Subject: message\n" \
"\n" \
"payload\n";
h = test_import(input, -1, 0);
assert_se(hashmap_size(h) == 1);
log_debug("----------------------------------------");
hashmap_free_free(h);
HASHMAP_FOREACH(payload, h, j) {
assert_se(streq(expect, payload));
}
}
static const char* database = NULL;
static void test_catalog_update(void) {
@ -162,7 +173,9 @@ int main(int argc, char *argv[]) {
test_catalog_file_lang();
test_catalog_importing();
test_catalog_import_invalid();
test_catalog_import_badid();
test_catalog_import_one();
test_catalog_update();