journal: Refactor test-catalog importing tests
authorStef Walter <stef@thewalter.net>
Sun, 8 Nov 2015 11:51:51 +0000 (12:51 +0100)
committerStef Walter <stef@thewalter.net>
Fri, 5 Feb 2016 16:07:01 +0000 (17:07 +0100)
One function per test. Remove shared state between tests.

src/journal/test-catalog.c

index 4f1392c..b03a16d 100644 (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);
-}
 
-static void test_catalog_importing(void) {
-        Hashmap *h;
+        return h;
+}
 
-        assert_se(h = hashmap_new(&catalog_hash_ops));
+static void test_catalog_import_invalid(void) {
+        _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
 
-#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();