]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/test-catalog.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / journal / test-catalog.c
index 25980b7744e794d70a9ae6072bb2ec2558f6b7b2..192bb0cb07e0bac67847069f21870ebd0dd87659 100644 (file)
@@ -1,24 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-  Copyright 2013 Zbigniew JÄ™drzejewski-Szmek
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <errno.h>
 #include <fcntl.h>
 #include "alloc-util.h"
 #include "catalog.h"
 #include "fd-util.h"
-#include "fileio.h"
+#include "fs-util.h"
 #include "log.h"
 #include "macro.h"
+#include "path-util.h"
 #include "string-util.h"
+#include "strv.h"
+#include "tests.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
-static const char *catalog_dirs[] = {
-        CATALOG_DIR,
-        NULL,
-};
-
+static char** catalog_dirs = NULL;
 static const char *no_catalog_dirs[] = {
         "/bin/hopefully/with/no/catalog",
         NULL
 };
 
-static void test_import(Hashmap *h, struct strbuf *sb,
-                        const char* contents, ssize_t size, int code) {
-        int r;
-        char name[] = "/tmp/test-catalog.XXXXXX";
+static Hashmap* test_import(const char* contents, ssize_t size, int code) {
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-catalog.XXXXXX";
         _cleanup_close_ int fd;
+        Hashmap *h;
 
-        fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
+        if (size < 0)
+                size = strlen(contents);
+
+        assert_se(h = hashmap_new(&catalog_hash_ops));
+
+        fd = mkostemp_safe(name);
         assert_se(fd >= 0);
         assert_se(write(fd, contents, size) == size);
 
-        r = catalog_import_file(h, sb, name);
-        assert_se(r == code);
+        assert_se(catalog_import_file(h, name) == code);
 
-        unlink(name);
+        return h;
 }
 
-static void test_catalog_importing(void) {
-        Hashmap *h;
-        struct strbuf *sb;
+static void test_catalog_import_invalid(void) {
+        _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
 
-        assert_se(h = hashmap_new(&catalog_hash_ops));
-        assert_se(sb = strbuf_new());
-
-#define BUF "xxx"
-        test_import(h, sb, 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, sb, 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, sb, 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_FOREACH(payload, h, j) {
+                printf("expect: %s\n", expect);
+                printf("actual: %s\n", payload);
+                assert_se(streq(expect, payload));
+        }
+}
+
+static void test_catalog_import_merge(void) {
+        _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
+        char *payload;
+        Iterator j;
 
-        hashmap_free_free(h);
-        strbuf_cleanup(sb);
+        const char *input =
+"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
+"Subject: message\n" \
+"Defined-By: me\n" \
+"\n" \
+"payload\n" \
+"\n" \
+"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
+"Subject: override subject\n" \
+"X-Header: hello\n" \
+"\n" \
+"override payload\n";
+
+        const char *combined =
+"Subject: override subject\n" \
+"X-Header: hello\n" \
+"Subject: message\n" \
+"Defined-By: me\n" \
+"\n" \
+"override payload\n";
+
+        h = test_import(input, -1, 0);
+        assert_se(hashmap_size(h) == 1);
+
+        HASHMAP_FOREACH(payload, h, j) {
+                assert_se(streq(combined, payload));
+        }
 }
 
+static void test_catalog_import_merge_no_body(void) {
+        _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
+        char *payload;
+        Iterator j;
 
-static const char* database = NULL;
+        const char *input =
+"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
+"Subject: message\n" \
+"Defined-By: me\n" \
+"\n" \
+"payload\n" \
+"\n" \
+"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
+"Subject: override subject\n" \
+"X-Header: hello\n" \
+"\n";
 
-static void test_catalog_update(void) {
-        static char name[] = "/tmp/test-catalog.XXXXXX";
-        int r;
+        const char *combined =
+"Subject: override subject\n" \
+"X-Header: hello\n" \
+"Subject: message\n" \
+"Defined-By: me\n" \
+"\n" \
+"payload\n";
 
-        r = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
-        assert_se(r >= 0);
+        h = test_import(input, -1, 0);
+        assert_se(hashmap_size(h) == 1);
 
-        database = name;
+        HASHMAP_FOREACH(payload, h, j) {
+                assert_se(streq(combined, payload));
+        }
+}
+
+static void test_catalog_update(const char *database) {
+        int r;
 
         /* Test what happens if there are no files. */
         r = catalog_update(database, NULL, NULL);
-        assert_se(r >= 0);
+        assert_se(r == 0);
 
         /* Test what happens if there are no files in the directory. */
         r = catalog_update(database, NULL, no_catalog_dirs);
-        assert_se(r >= 0);
+        assert_se(r == 0);
 
         /* Make sure that we at least have some files loaded or the
-           catalog_list below will fail. */
-        r = catalog_update(database, NULL, catalog_dirs);
-        assert_se(r >= 0);
+         * catalog_list below will fail. */
+        r = catalog_update(database, NULL, (const char * const *) catalog_dirs);
+        assert_se(r == 0);
 }
 
 static void test_catalog_file_lang(void) {
@@ -156,19 +200,32 @@ static void test_catalog_file_lang(void) {
 }
 
 int main(int argc, char *argv[]) {
+        _cleanup_(unlink_tempfilep) char database[] = "/tmp/test-catalog.XXXXXX";
         _cleanup_free_ char *text = NULL;
         int r;
 
         setlocale(LC_ALL, "de_DE.UTF-8");
 
-        log_parse_environment();
-        log_open();
+        test_setup_logging(LOG_DEBUG);
+
+        /* If test-catalog is located at the build directory, then use catalogs in that.
+         * If it is not, e.g. installed by systemd-tests package, then use installed catalogs. */
+        catalog_dirs = STRV_MAKE(get_catalog_dir());
+
+        assert_se(access(catalog_dirs[0], F_OK) >= 0);
+        log_notice("Using catalog directory '%s'", catalog_dirs[0]);
 
         test_catalog_file_lang();
 
-        test_catalog_importing();
+        test_catalog_import_invalid();
+        test_catalog_import_badid();
+        test_catalog_import_one();
+        test_catalog_import_merge();
+        test_catalog_import_merge_no_body();
+
+        assert_se(mkostemp_safe(database) >= 0);
 
-        test_catalog_update();
+        test_catalog_update(database);
 
         r = catalog_list(stdout, database, true);
         assert_se(r >= 0);
@@ -179,8 +236,5 @@ int main(int argc, char *argv[]) {
         assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
         printf(">>>%s<<<\n", text);
 
-        if (database)
-                unlink(database);
-
         return 0;
 }