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 1 : int main(int argc, char *argv[]) {
24 : sd_id128_t id, id2;
25 : char t[33], q[37];
26 1 : _cleanup_free_ char *b = NULL;
27 2 : _cleanup_close_ int fd = -1;
28 : int r;
29 :
30 1 : assert_se(sd_id128_randomize(&id) == 0);
31 1 : printf("random: %s\n", sd_id128_to_string(id, t));
32 :
33 1 : assert_se(sd_id128_from_string(t, &id2) == 0);
34 1 : assert_se(sd_id128_equal(id, id2));
35 :
36 1 : if (sd_booted() > 0) {
37 1 : assert_se(sd_id128_get_machine(&id) == 0);
38 1 : printf("machine: %s\n", sd_id128_to_string(id, t));
39 :
40 1 : assert_se(sd_id128_get_boot(&id) == 0);
41 1 : printf("boot: %s\n", sd_id128_to_string(id, t));
42 : }
43 :
44 1 : printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t));
45 1 : assert_se(streq(t, STR_WALDI));
46 :
47 1 : assert_se(asprintf(&b, SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 32);
48 1 : printf("waldi2: %s\n", b);
49 1 : assert_se(streq(t, b));
50 :
51 1 : printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI, q));
52 1 : assert_se(streq(q, UUID_WALDI));
53 :
54 1 : b = mfree(b);
55 1 : assert_se(asprintf(&b, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 36);
56 1 : printf("waldi4: %s\n", b);
57 1 : assert_se(streq(q, b));
58 :
59 1 : assert_se(sd_id128_from_string(STR_WALDI, &id) >= 0);
60 1 : assert_se(sd_id128_equal(id, ID128_WALDI));
61 :
62 1 : assert_se(sd_id128_from_string(UUID_WALDI, &id) >= 0);
63 1 : assert_se(sd_id128_equal(id, ID128_WALDI));
64 :
65 1 : assert_se(sd_id128_from_string("", &id) < 0);
66 1 : assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f101", &id) < 0);
67 1 : assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f10-", &id) < 0);
68 1 : assert_se(sd_id128_from_string("01020304-0506-0708-090a0b0c0d0e0f10", &id) < 0);
69 1 : assert_se(sd_id128_from_string("010203040506-0708-090a-0b0c0d0e0f10", &id) < 0);
70 :
71 1 : assert_se(id128_is_valid(STR_WALDI));
72 1 : assert_se(id128_is_valid(UUID_WALDI));
73 1 : assert_se(!id128_is_valid(""));
74 1 : assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f101"));
75 1 : assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f10-"));
76 1 : assert_se(!id128_is_valid("01020304-0506-0708-090a0b0c0d0e0f10"));
77 1 : assert_se(!id128_is_valid("010203040506-0708-090a-0b0c0d0e0f10"));
78 :
79 1 : fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
80 1 : assert_se(fd >= 0);
81 :
82 : /* First, write as UUID */
83 1 : assert_se(sd_id128_randomize(&id) >= 0);
84 1 : assert_se(id128_write_fd(fd, ID128_UUID, id, false) >= 0);
85 :
86 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
87 1 : assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
88 :
89 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
90 1 : assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
91 1 : assert_se(sd_id128_equal(id, id2));
92 :
93 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
94 1 : assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
95 1 : assert_se(sd_id128_equal(id, id2));
96 :
97 : /* Second, write as plain */
98 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
99 1 : assert_se(ftruncate(fd, 0) >= 0);
100 :
101 1 : assert_se(sd_id128_randomize(&id) >= 0);
102 1 : assert_se(id128_write_fd(fd, ID128_PLAIN, id, false) >= 0);
103 :
104 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
105 1 : assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
106 :
107 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
108 1 : assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
109 1 : assert_se(sd_id128_equal(id, id2));
110 :
111 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
112 1 : assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
113 1 : assert_se(sd_id128_equal(id, id2));
114 :
115 : /* Third, write plain without trailing newline */
116 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
117 1 : assert_se(ftruncate(fd, 0) >= 0);
118 :
119 1 : assert_se(sd_id128_randomize(&id) >= 0);
120 1 : assert_se(write(fd, sd_id128_to_string(id, t), 32) == 32);
121 :
122 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
123 1 : assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
124 :
125 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
126 1 : assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
127 1 : assert_se(sd_id128_equal(id, id2));
128 :
129 : /* Third, write UUID without trailing newline */
130 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
131 1 : assert_se(ftruncate(fd, 0) >= 0);
132 :
133 1 : assert_se(sd_id128_randomize(&id) >= 0);
134 1 : assert_se(write(fd, id128_to_uuid_string(id, q), 36) == 36);
135 :
136 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
137 1 : assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
138 :
139 1 : assert_se(lseek(fd, 0, SEEK_SET) == 0);
140 1 : assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
141 1 : assert_se(sd_id128_equal(id, id2));
142 :
143 1 : 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 1 : if (r == -EOPNOTSUPP)
145 0 : log_info("khash not supported on this kernel, skipping sd_id128_get_machine_app_specific() checks");
146 : else {
147 1 : assert_se(r >= 0);
148 1 : 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 1 : assert_se(sd_id128_equal(id, id2));
150 1 : 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 1 : assert_se(!sd_id128_equal(id, id2));
152 : }
153 :
154 : /* Query the invocation ID */
155 1 : r = sd_id128_get_invocation(&id);
156 1 : if (r < 0)
157 1 : 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 1 : return 0;
162 : }
|