LCOV - code coverage report
Current view: top level - journal - test-catalog.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 100 100 100.0 %
Date: 2019-08-22 15:41:25 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <fcntl.h>
       5             : #include <locale.h>
       6             : #include <unistd.h>
       7             : 
       8             : #include "sd-messages.h"
       9             : 
      10             : #include "alloc-util.h"
      11             : #include "catalog.h"
      12             : #include "fd-util.h"
      13             : #include "fs-util.h"
      14             : #include "log.h"
      15             : #include "macro.h"
      16             : #include "path-util.h"
      17             : #include "string-util.h"
      18             : #include "strv.h"
      19             : #include "tests.h"
      20             : #include "tmpfile-util.h"
      21             : #include "util.h"
      22             : 
      23             : static char** catalog_dirs = NULL;
      24             : static const char *no_catalog_dirs[] = {
      25             :         "/bin/hopefully/with/no/catalog",
      26             :         NULL
      27             : };
      28             : 
      29           5 : static Hashmap* test_import(const char* contents, ssize_t size, int code) {
      30           5 :         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-catalog.XXXXXX";
      31           5 :         _cleanup_close_ int fd;
      32             :         Hashmap *h;
      33             : 
      34           5 :         if (size < 0)
      35           5 :                 size = strlen(contents);
      36             : 
      37           5 :         assert_se(h = hashmap_new(&catalog_hash_ops));
      38             : 
      39           5 :         fd = mkostemp_safe(name);
      40           5 :         assert_se(fd >= 0);
      41           5 :         assert_se(write(fd, contents, size) == size);
      42             : 
      43           5 :         assert_se(catalog_import_file(h, name) == code);
      44             : 
      45           5 :         return h;
      46             : }
      47             : 
      48           1 : static void test_catalog_import_invalid(void) {
      49           1 :         _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
      50             : 
      51           1 :         h = test_import("xxx", -1, -EINVAL);
      52           1 :         assert_se(hashmap_isempty(h));
      53           1 : }
      54             : 
      55           1 : static void test_catalog_import_badid(void) {
      56           2 :         _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
      57           1 :         const char *input =
      58             : "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
      59             : "Subject: message\n" \
      60             : "\n" \
      61             : "payload\n";
      62           1 :         h = test_import(input, -1, -EINVAL);
      63           1 : }
      64             : 
      65           1 : static void test_catalog_import_one(void) {
      66           1 :         _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
      67             :         char *payload;
      68             :         Iterator j;
      69             : 
      70           1 :         const char *input =
      71             : "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
      72             : "Subject: message\n" \
      73             : "\n" \
      74             : "payload\n";
      75           1 :         const char *expect =
      76             : "Subject: message\n" \
      77             : "\n" \
      78             : "payload\n";
      79             : 
      80           1 :         h = test_import(input, -1, 0);
      81           1 :         assert_se(hashmap_size(h) == 1);
      82             : 
      83           2 :         HASHMAP_FOREACH(payload, h, j) {
      84           1 :                 printf("expect: %s\n", expect);
      85           1 :                 printf("actual: %s\n", payload);
      86           1 :                 assert_se(streq(expect, payload));
      87             :         }
      88           1 : }
      89             : 
      90           1 : static void test_catalog_import_merge(void) {
      91           1 :         _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
      92             :         char *payload;
      93             :         Iterator j;
      94             : 
      95           1 :         const char *input =
      96             : "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
      97             : "Subject: message\n" \
      98             : "Defined-By: me\n" \
      99             : "\n" \
     100             : "payload\n" \
     101             : "\n" \
     102             : "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
     103             : "Subject: override subject\n" \
     104             : "X-Header: hello\n" \
     105             : "\n" \
     106             : "override payload\n";
     107             : 
     108           1 :         const char *combined =
     109             : "Subject: override subject\n" \
     110             : "X-Header: hello\n" \
     111             : "Subject: message\n" \
     112             : "Defined-By: me\n" \
     113             : "\n" \
     114             : "override payload\n";
     115             : 
     116           1 :         h = test_import(input, -1, 0);
     117           1 :         assert_se(hashmap_size(h) == 1);
     118             : 
     119           2 :         HASHMAP_FOREACH(payload, h, j) {
     120           1 :                 assert_se(streq(combined, payload));
     121             :         }
     122           1 : }
     123             : 
     124           1 : static void test_catalog_import_merge_no_body(void) {
     125           1 :         _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
     126             :         char *payload;
     127             :         Iterator j;
     128             : 
     129           1 :         const char *input =
     130             : "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
     131             : "Subject: message\n" \
     132             : "Defined-By: me\n" \
     133             : "\n" \
     134             : "payload\n" \
     135             : "\n" \
     136             : "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
     137             : "Subject: override subject\n" \
     138             : "X-Header: hello\n" \
     139             : "\n";
     140             : 
     141           1 :         const char *combined =
     142             : "Subject: override subject\n" \
     143             : "X-Header: hello\n" \
     144             : "Subject: message\n" \
     145             : "Defined-By: me\n" \
     146             : "\n" \
     147             : "payload\n";
     148             : 
     149           1 :         h = test_import(input, -1, 0);
     150           1 :         assert_se(hashmap_size(h) == 1);
     151             : 
     152           2 :         HASHMAP_FOREACH(payload, h, j) {
     153           1 :                 assert_se(streq(combined, payload));
     154             :         }
     155           1 : }
     156             : 
     157           1 : static void test_catalog_update(const char *database) {
     158             :         int r;
     159             : 
     160             :         /* Test what happens if there are no files. */
     161           1 :         r = catalog_update(database, NULL, NULL);
     162           1 :         assert_se(r == 0);
     163             : 
     164             :         /* Test what happens if there are no files in the directory. */
     165           1 :         r = catalog_update(database, NULL, no_catalog_dirs);
     166           1 :         assert_se(r == 0);
     167             : 
     168             :         /* Make sure that we at least have some files loaded or the
     169             :          * catalog_list below will fail. */
     170           1 :         r = catalog_update(database, NULL, (const char * const *) catalog_dirs);
     171           1 :         assert_se(r == 0);
     172           1 : }
     173             : 
     174           1 : static void test_catalog_file_lang(void) {
     175           1 :         _cleanup_free_ char *lang = NULL, *lang2 = NULL, *lang3 = NULL, *lang4 = NULL;
     176             : 
     177           1 :         assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang) == 1);
     178           1 :         assert_se(streq(lang, "de_DE"));
     179             : 
     180           1 :         assert_se(catalog_file_lang("systemd..catalog", &lang2) == 0);
     181           1 :         assert_se(lang2 == NULL);
     182             : 
     183           1 :         assert_se(catalog_file_lang("systemd.fr.catalog", &lang2) == 1);
     184           1 :         assert_se(streq(lang2, "fr"));
     185             : 
     186           1 :         assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3) == 0);
     187           1 :         assert_se(lang3 == NULL);
     188             : 
     189           1 :         assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3) == 0);
     190           1 :         assert_se(lang3 == NULL);
     191             : 
     192           1 :         assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3) == 1);
     193           1 :         assert_se(streq(lang3, "0123456789012345678901234567890"));
     194             : 
     195           1 :         assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4) == 0);
     196           1 :         assert_se(lang4 == NULL);
     197             : 
     198           1 :         assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4) == 1);
     199           1 :         assert_se(streq(lang4, "ru_RU"));
     200           1 : }
     201             : 
     202           1 : int main(int argc, char *argv[]) {
     203           1 :         _cleanup_(unlink_tempfilep) char database[] = "/tmp/test-catalog.XXXXXX";
     204           2 :         _cleanup_free_ char *text = NULL;
     205             :         int r;
     206             : 
     207           1 :         setlocale(LC_ALL, "de_DE.UTF-8");
     208             : 
     209           1 :         test_setup_logging(LOG_DEBUG);
     210             : 
     211             :         /* If test-catalog is located at the build directory, then use catalogs in that.
     212             :          * If it is not, e.g. installed by systemd-tests package, then use installed catalogs. */
     213           1 :         catalog_dirs = STRV_MAKE(get_catalog_dir());
     214             : 
     215           1 :         assert_se(access(catalog_dirs[0], F_OK) >= 0);
     216           1 :         log_notice("Using catalog directory '%s'", catalog_dirs[0]);
     217             : 
     218           1 :         test_catalog_file_lang();
     219             : 
     220           1 :         test_catalog_import_invalid();
     221           1 :         test_catalog_import_badid();
     222           1 :         test_catalog_import_one();
     223           1 :         test_catalog_import_merge();
     224           1 :         test_catalog_import_merge_no_body();
     225             : 
     226           1 :         assert_se(mkostemp_safe(database) >= 0);
     227             : 
     228           1 :         test_catalog_update(database);
     229             : 
     230           1 :         r = catalog_list(stdout, database, true);
     231           1 :         assert_se(r >= 0);
     232             : 
     233           1 :         r = catalog_list(stdout, database, false);
     234           1 :         assert_se(r >= 0);
     235             : 
     236           1 :         assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
     237           1 :         printf(">>>%s<<<\n", text);
     238             : 
     239           1 :         return 0;
     240             : }

Generated by: LCOV version 1.14