]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-id128.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "sd-daemon.h"
26 #include "alloc-util.h"
29 #include "id128-util.h"
31 #include "string-util.h"
34 #define ID128_WALDI SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10)
35 #define STR_WALDI "0102030405060708090a0b0c0d0e0f10"
36 #define UUID_WALDI "01020304-0506-0708-090a-0b0c0d0e0f10"
38 int main(int argc
, char *argv
[]) {
41 _cleanup_free_
char *b
= NULL
;
42 _cleanup_close_
int fd
= -1;
45 assert_se(sd_id128_randomize(&id
) == 0);
46 printf("random: %s\n", sd_id128_to_string(id
, t
));
48 assert_se(sd_id128_from_string(t
, &id2
) == 0);
49 assert_se(sd_id128_equal(id
, id2
));
51 if (sd_booted() > 0) {
52 assert_se(sd_id128_get_machine(&id
) == 0);
53 printf("machine: %s\n", sd_id128_to_string(id
, t
));
55 assert_se(sd_id128_get_boot(&id
) == 0);
56 printf("boot: %s\n", sd_id128_to_string(id
, t
));
59 printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI
, t
));
60 assert_se(streq(t
, STR_WALDI
));
62 assert_se(asprintf(&b
, SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(ID128_WALDI
)) == 32);
63 printf("waldi2: %s\n", b
);
64 assert_se(streq(t
, b
));
66 printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI
, q
));
67 assert_se(streq(q
, UUID_WALDI
));
70 assert_se(asprintf(&b
, ID128_UUID_FORMAT_STR
, SD_ID128_FORMAT_VAL(ID128_WALDI
)) == 36);
71 printf("waldi4: %s\n", b
);
72 assert_se(streq(q
, b
));
74 assert_se(sd_id128_from_string(STR_WALDI
, &id
) >= 0);
75 assert_se(sd_id128_equal(id
, ID128_WALDI
));
77 assert_se(sd_id128_from_string(UUID_WALDI
, &id
) >= 0);
78 assert_se(sd_id128_equal(id
, ID128_WALDI
));
80 assert_se(sd_id128_from_string("", &id
) < 0);
81 assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f101", &id
) < 0);
82 assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f10-", &id
) < 0);
83 assert_se(sd_id128_from_string("01020304-0506-0708-090a0b0c0d0e0f10", &id
) < 0);
84 assert_se(sd_id128_from_string("010203040506-0708-090a-0b0c0d0e0f10", &id
) < 0);
86 assert_se(id128_is_valid(STR_WALDI
));
87 assert_se(id128_is_valid(UUID_WALDI
));
88 assert_se(!id128_is_valid(""));
89 assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f101"));
90 assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f10-"));
91 assert_se(!id128_is_valid("01020304-0506-0708-090a0b0c0d0e0f10"));
92 assert_se(!id128_is_valid("010203040506-0708-090a-0b0c0d0e0f10"));
94 fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
97 /* First, write as UUID */
98 assert_se(sd_id128_randomize(&id
) >= 0);
99 assert_se(id128_write_fd(fd
, ID128_UUID
, id
, false) >= 0);
101 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
102 assert_se(id128_read_fd(fd
, ID128_PLAIN
, &id2
) == -EINVAL
);
104 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
105 assert_se(id128_read_fd(fd
, ID128_UUID
, &id2
) >= 0);
106 assert_se(sd_id128_equal(id
, id2
));
108 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
109 assert_se(id128_read_fd(fd
, ID128_ANY
, &id2
) >= 0);
110 assert_se(sd_id128_equal(id
, id2
));
112 /* Second, write as plain */
113 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
114 assert_se(ftruncate(fd
, 0) >= 0);
116 assert_se(sd_id128_randomize(&id
) >= 0);
117 assert_se(id128_write_fd(fd
, ID128_PLAIN
, id
, false) >= 0);
119 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
120 assert_se(id128_read_fd(fd
, ID128_UUID
, &id2
) == -EINVAL
);
122 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
123 assert_se(id128_read_fd(fd
, ID128_PLAIN
, &id2
) >= 0);
124 assert_se(sd_id128_equal(id
, id2
));
126 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
127 assert_se(id128_read_fd(fd
, ID128_ANY
, &id2
) >= 0);
128 assert_se(sd_id128_equal(id
, id2
));
130 /* Third, write plain without trailing newline */
131 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
132 assert_se(ftruncate(fd
, 0) >= 0);
134 assert_se(sd_id128_randomize(&id
) >= 0);
135 assert_se(write(fd
, sd_id128_to_string(id
, t
), 32) == 32);
137 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
138 assert_se(id128_read_fd(fd
, ID128_UUID
, &id2
) == -EINVAL
);
140 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
141 assert_se(id128_read_fd(fd
, ID128_PLAIN
, &id2
) >= 0);
142 assert_se(sd_id128_equal(id
, id2
));
144 /* Third, write UUID without trailing newline */
145 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
146 assert_se(ftruncate(fd
, 0) >= 0);
148 assert_se(sd_id128_randomize(&id
) >= 0);
149 assert_se(write(fd
, id128_to_uuid_string(id
, q
), 36) == 36);
151 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
152 assert_se(id128_read_fd(fd
, ID128_PLAIN
, &id2
) == -EINVAL
);
154 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
155 assert_se(id128_read_fd(fd
, ID128_UUID
, &id2
) >= 0);
156 assert_se(sd_id128_equal(id
, id2
));
158 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
);
159 if (r
== -EAFNOSUPPORT
) {
160 log_info("khash not supported on this kernel, skipping sd_id128_get_machine_app_specific() checks");
163 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);
164 assert_se(sd_id128_equal(id
, id2
));
165 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);
166 assert_se(!sd_id128_equal(id
, id2
));
169 /* Query the invocation ID */
170 r
= sd_id128_get_invocation(&id
);
172 log_warning_errno(r
, "Failed to get invocation ID, ignoring: %m");
174 log_info("Invocation ID: " SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(id
));