LCOV - code coverage report
Current view: top level - test - test-id128.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 92 94 97.9 %
Date: 2019-08-23 13:36:53 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 77 158 48.7 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <fcntl.h>
       4                 :            : #include <string.h>
       5                 :            : #include <sys/types.h>
       6                 :            : #include <unistd.h>
       7                 :            : 
       8                 :            : #include "sd-daemon.h"
       9                 :            : #include "sd-id128.h"
      10                 :            : 
      11                 :            : #include "alloc-util.h"
      12                 :            : #include "fd-util.h"
      13                 :            : #include "id128-util.h"
      14                 :            : #include "macro.h"
      15                 :            : #include "string-util.h"
      16                 :            : #include "tmpfile-util.h"
      17                 :            : #include "util.h"
      18                 :            : 
      19                 :            : #define ID128_WALDI SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10)
      20                 :            : #define STR_WALDI "0102030405060708090a0b0c0d0e0f10"
      21                 :            : #define UUID_WALDI "01020304-0506-0708-090a-0b0c0d0e0f10"
      22                 :            : 
      23                 :          4 : int main(int argc, char *argv[]) {
      24                 :            :         sd_id128_t id, id2;
      25                 :            :         char t[33], q[37];
      26                 :          4 :         _cleanup_free_ char *b = NULL;
      27                 :          8 :         _cleanup_close_ int fd = -1;
      28                 :            :         int r;
      29                 :            : 
      30         [ -  + ]:          4 :         assert_se(sd_id128_randomize(&id) == 0);
      31                 :          4 :         printf("random: %s\n", sd_id128_to_string(id, t));
      32                 :            : 
      33         [ -  + ]:          4 :         assert_se(sd_id128_from_string(t, &id2) == 0);
      34         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, id2));
      35                 :            : 
      36         [ +  - ]:          4 :         if (sd_booted() > 0) {
      37         [ -  + ]:          4 :                 assert_se(sd_id128_get_machine(&id) == 0);
      38                 :          4 :                 printf("machine: %s\n", sd_id128_to_string(id, t));
      39                 :            : 
      40         [ -  + ]:          4 :                 assert_se(sd_id128_get_boot(&id) == 0);
      41                 :          4 :                 printf("boot: %s\n", sd_id128_to_string(id, t));
      42                 :            :         }
      43                 :            : 
      44                 :          4 :         printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t));
      45         [ -  + ]:          4 :         assert_se(streq(t, STR_WALDI));
      46                 :            : 
      47         [ -  + ]:          4 :         assert_se(asprintf(&b, SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 32);
      48                 :          4 :         printf("waldi2: %s\n", b);
      49         [ -  + ]:          4 :         assert_se(streq(t, b));
      50                 :            : 
      51                 :          4 :         printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI, q));
      52         [ -  + ]:          4 :         assert_se(streq(q, UUID_WALDI));
      53                 :            : 
      54                 :          4 :         b = mfree(b);
      55         [ -  + ]:          4 :         assert_se(asprintf(&b, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 36);
      56                 :          4 :         printf("waldi4: %s\n", b);
      57         [ -  + ]:          4 :         assert_se(streq(q, b));
      58                 :            : 
      59         [ -  + ]:          4 :         assert_se(sd_id128_from_string(STR_WALDI, &id) >= 0);
      60         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, ID128_WALDI));
      61                 :            : 
      62         [ -  + ]:          4 :         assert_se(sd_id128_from_string(UUID_WALDI, &id) >= 0);
      63         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, ID128_WALDI));
      64                 :            : 
      65         [ -  + ]:          4 :         assert_se(sd_id128_from_string("", &id) < 0);
      66         [ -  + ]:          4 :         assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f101", &id) < 0);
      67         [ -  + ]:          4 :         assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f10-", &id) < 0);
      68         [ -  + ]:          4 :         assert_se(sd_id128_from_string("01020304-0506-0708-090a0b0c0d0e0f10", &id) < 0);
      69         [ -  + ]:          4 :         assert_se(sd_id128_from_string("010203040506-0708-090a-0b0c0d0e0f10", &id) < 0);
      70                 :            : 
      71         [ -  + ]:          4 :         assert_se(id128_is_valid(STR_WALDI));
      72         [ -  + ]:          4 :         assert_se(id128_is_valid(UUID_WALDI));
      73         [ -  + ]:          4 :         assert_se(!id128_is_valid(""));
      74         [ -  + ]:          4 :         assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f101"));
      75         [ -  + ]:          4 :         assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f10-"));
      76         [ -  + ]:          4 :         assert_se(!id128_is_valid("01020304-0506-0708-090a0b0c0d0e0f10"));
      77         [ -  + ]:          4 :         assert_se(!id128_is_valid("010203040506-0708-090a-0b0c0d0e0f10"));
      78                 :            : 
      79                 :          4 :         fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
      80         [ -  + ]:          4 :         assert_se(fd >= 0);
      81                 :            : 
      82                 :            :         /* First, write as UUID */
      83         [ -  + ]:          4 :         assert_se(sd_id128_randomize(&id) >= 0);
      84         [ -  + ]:          4 :         assert_se(id128_write_fd(fd, ID128_UUID, id, false) >= 0);
      85                 :            : 
      86         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
      87         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
      88                 :            : 
      89         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
      90         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
      91         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, id2));
      92                 :            : 
      93         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
      94         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
      95         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, id2));
      96                 :            : 
      97                 :            :         /* Second, write as plain */
      98         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
      99         [ -  + ]:          4 :         assert_se(ftruncate(fd, 0) >= 0);
     100                 :            : 
     101         [ -  + ]:          4 :         assert_se(sd_id128_randomize(&id) >= 0);
     102         [ -  + ]:          4 :         assert_se(id128_write_fd(fd, ID128_PLAIN, id, false) >= 0);
     103                 :            : 
     104         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     105         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
     106                 :            : 
     107         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     108         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
     109         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, id2));
     110                 :            : 
     111         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     112         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
     113         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, id2));
     114                 :            : 
     115                 :            :         /* Third, write plain without trailing newline */
     116         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     117         [ -  + ]:          4 :         assert_se(ftruncate(fd, 0) >= 0);
     118                 :            : 
     119         [ -  + ]:          4 :         assert_se(sd_id128_randomize(&id) >= 0);
     120         [ -  + ]:          4 :         assert_se(write(fd, sd_id128_to_string(id, t), 32) == 32);
     121                 :            : 
     122         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     123         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
     124                 :            : 
     125         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     126         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
     127         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, id2));
     128                 :            : 
     129                 :            :         /* Third, write UUID without trailing newline */
     130         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     131         [ -  + ]:          4 :         assert_se(ftruncate(fd, 0) >= 0);
     132                 :            : 
     133         [ -  + ]:          4 :         assert_se(sd_id128_randomize(&id) >= 0);
     134         [ -  + ]:          4 :         assert_se(write(fd, id128_to_uuid_string(id, q), 36) == 36);
     135                 :            : 
     136         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     137         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
     138                 :            : 
     139         [ -  + ]:          4 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
     140         [ -  + ]:          4 :         assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
     141         [ -  + ]:          4 :         assert_se(sd_id128_equal(id, id2));
     142                 :            : 
     143                 :          4 :         r = sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id);
     144         [ -  + ]:          4 :         if (r == -EOPNOTSUPP)
     145         [ #  # ]:          0 :                 log_info("khash not supported on this kernel, skipping sd_id128_get_machine_app_specific() checks");
     146                 :            :         else {
     147         [ -  + ]:          4 :                 assert_se(r >= 0);
     148         [ -  + ]:          4 :                 assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id2) >= 0);
     149         [ -  + ]:          4 :                 assert_se(sd_id128_equal(id, id2));
     150         [ -  + ]:          4 :                 assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8), &id2) >= 0);
     151         [ -  + ]:          4 :                 assert_se(!sd_id128_equal(id, id2));
     152                 :            :         }
     153                 :            : 
     154                 :            :         /* Query the invocation ID */
     155                 :          4 :         r = sd_id128_get_invocation(&id);
     156         [ +  - ]:          4 :         if (r < 0)
     157         [ +  - ]:          4 :                 log_warning_errno(r, "Failed to get invocation ID, ignoring: %m");
     158                 :            :         else
     159         [ #  # ]:          0 :                 log_info("Invocation ID: " SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id));
     160                 :            : 
     161                 :          4 :         return 0;
     162                 :            : }

Generated by: LCOV version 1.14