]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-id128.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-id128.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
87d2c1ff
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2011 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
87d2c1ff
LP
10 (at your option) any later version.
11
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
5430f7f2 15 Lesser General Public License for more details.
87d2c1ff 16
5430f7f2 17 You should have received a copy of the GNU Lesser General Public License
87d2c1ff
LP
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
21#include <string.h>
22
07630cea
LP
23#include "sd-daemon.h"
24#include "sd-id128.h"
81527be1 25
b5efdb8a 26#include "alloc-util.h"
65548c58
LP
27#include "fd-util.h"
28#include "fileio.h"
29#include "id128-util.h"
87d2c1ff 30#include "macro.h"
07630cea
LP
31#include "string-util.h"
32#include "util.h"
87d2c1ff
LP
33
34#define ID128_WALDI SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10)
aa96c6cb
LP
35#define STR_WALDI "0102030405060708090a0b0c0d0e0f10"
36#define UUID_WALDI "01020304-0506-0708-090a-0b0c0d0e0f10"
87d2c1ff
LP
37
38int main(int argc, char *argv[]) {
39 sd_id128_t id, id2;
910fd145 40 char t[33], q[37];
aa96c6cb 41 _cleanup_free_ char *b = NULL;
65548c58 42 _cleanup_close_ int fd = -1;
b3415f5d 43 int r;
87d2c1ff
LP
44
45 assert_se(sd_id128_randomize(&id) == 0);
46 printf("random: %s\n", sd_id128_to_string(id, t));
47
48 assert_se(sd_id128_from_string(t, &id2) == 0);
49 assert_se(sd_id128_equal(id, id2));
50
143bfdaf
HHPF
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));
87d2c1ff 54
143bfdaf
HHPF
55 assert_se(sd_id128_get_boot(&id) == 0);
56 printf("boot: %s\n", sd_id128_to_string(id, t));
57 }
87d2c1ff
LP
58
59 printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t));
aa96c6cb
LP
60 assert_se(streq(t, STR_WALDI));
61
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));
65
910fd145
LP
66 printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI, q));
67 assert_se(streq(q, UUID_WALDI));
68
69 b = mfree(b);
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));
73
74 assert_se(sd_id128_from_string(STR_WALDI, &id) >= 0);
75 assert_se(sd_id128_equal(id, ID128_WALDI));
76
aa96c6cb
LP
77 assert_se(sd_id128_from_string(UUID_WALDI, &id) >= 0);
78 assert_se(sd_id128_equal(id, ID128_WALDI));
79
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);
85
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"));
eaff7bd8 93
65548c58
LP
94 fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
95 assert_se(fd >= 0);
96
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);
100
101 assert_se(lseek(fd, 0, SEEK_SET) == 0);
102 assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
103
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));
107
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));
111
112 /* Second, write as plain */
113 assert_se(lseek(fd, 0, SEEK_SET) == 0);
114 assert_se(ftruncate(fd, 0) >= 0);
115
116 assert_se(sd_id128_randomize(&id) >= 0);
117 assert_se(id128_write_fd(fd, ID128_PLAIN, id, false) >= 0);
118
119 assert_se(lseek(fd, 0, SEEK_SET) == 0);
120 assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
121
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));
125
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));
129
130 /* Third, write plain without trailing newline */
131 assert_se(lseek(fd, 0, SEEK_SET) == 0);
132 assert_se(ftruncate(fd, 0) >= 0);
133
134 assert_se(sd_id128_randomize(&id) >= 0);
135 assert_se(write(fd, sd_id128_to_string(id, t), 32) == 32);
136
137 assert_se(lseek(fd, 0, SEEK_SET) == 0);
138 assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
139
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));
143
144 /* Third, write UUID without trailing newline */
145 assert_se(lseek(fd, 0, SEEK_SET) == 0);
146 assert_se(ftruncate(fd, 0) >= 0);
147
148 assert_se(sd_id128_randomize(&id) >= 0);
ba8868f5 149 assert_se(write(fd, id128_to_uuid_string(id, q), 36) == 36);
65548c58
LP
150
151 assert_se(lseek(fd, 0, SEEK_SET) == 0);
152 assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
153
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));
157
2e914f34
MP
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");
161 } else {
162 assert_se(r >= 0);
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));
167 }
70fc4f57 168
b3415f5d
LP
169 /* Query the invocation ID */
170 r = sd_id128_get_invocation(&id);
171 if (r < 0)
172 log_warning_errno(r, "Failed to get invocation ID, ignoring: %m");
173 else
174 log_info("Invocation ID: " SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id));
175
87d2c1ff
LP
176 return 0;
177}