Commit | Line | Data |
---|---|---|
c70636a7 | 1 | /* |
c70636a7 MJ |
2 | * Copyright 2019 Michael Jeanson <mjeanson@efficios.com> |
3 | * | |
9d16b343 | 4 | * SPDX-License-Identifier: GPL-2.0-only |
c70636a7 | 5 | * |
c70636a7 MJ |
6 | */ |
7 | ||
c9e313bc | 8 | #include "common/uuid.hpp" |
c70636a7 | 9 | |
28ab034a JG |
10 | #include <stdbool.h> |
11 | #include <stdio.h> | |
12 | #include <string.h> | |
e30c79d2 MJ |
13 | #include <tap/tap.h> |
14 | ||
c70636a7 MJ |
15 | #define NR_TESTS 21 |
16 | ||
17 | static const char valid_str_1[] = "3d260c88-75ea-47b8-a7e2-d6077c0378d9"; | |
18 | static const char valid_str_2[] = "611cf3a6-a68b-4515-834f-208bc2762592"; | |
19 | static const char valid_str_3[] = "1b4855cc-96de-4ae8-abe3-86449c2a43c4"; | |
20 | static const char valid_str_4[] = "8ADED5B9-ACD2-439F-A60C-897403AA2AB4"; | |
21 | static const char valid_str_5[] = "f109e0a2-C619-4d18-b760-20EA20E0F69A"; | |
22 | ||
28ab034a JG |
23 | static lttng_uuid valid_uuid_1 = { 0x3d, 0x26, 0x0c, 0x88, 0x75, 0xea, 0x47, 0xb8, |
24 | 0xa7, 0xe2, 0xd6, 0x07, 0x7c, 0x03, 0x78, 0xd9 }; | |
25 | static lttng_uuid valid_uuid_2 = { 0x61, 0x1c, 0xf3, 0xa6, 0xa6, 0x8b, 0x45, 0x15, | |
26 | 0x83, 0x4f, 0x20, 0x8b, 0xc2, 0x76, 0x25, 0x92 }; | |
27 | static lttng_uuid valid_uuid_3 = { 0x1b, 0x48, 0x55, 0xcc, 0x96, 0xde, 0x4a, 0xe8, | |
28 | 0xab, 0xe3, 0x86, 0x44, 0x9c, 0x2a, 0x43, 0xc4 }; | |
c70636a7 MJ |
29 | |
30 | static const char invalid_str_1[] = "1b485!cc-96de-4XX8-abe3-86449c2a43?4"; | |
31 | static const char invalid_str_2[] = "c2e6eddb&3955&4006&be3a&70bb63bd5f25"; | |
32 | static const char invalid_str_3[] = "81b1cb88-ff42-45b9-ba4d-964088ee45"; | |
33 | static const char invalid_str_4[] = "2d-6c6d756574-470e-9142-a4e6ad03f143"; | |
34 | static const char invalid_str_5[] = "4542ad19-9e4f-4931-8261-2101c3e089ae7"; | |
35 | static const char invalid_str_6[] = "XX0123"; | |
36 | ||
57b90af7 JG |
37 | /* For error.hpp */ |
38 | int lttng_opt_quiet = 1; | |
39 | int lttng_opt_verbose = 0; | |
40 | int lttng_opt_mi; | |
41 | ||
cd9adb8b | 42 | static void run_test_lttng_uuid_from_str() |
c70636a7 MJ |
43 | { |
44 | int ret; | |
45 | lttng_uuid uuid1; | |
46 | ||
47 | /* | |
48 | * Parse valid UUID strings, expect success. | |
49 | */ | |
50 | ret = lttng_uuid_from_str(valid_str_1, uuid1); | |
51 | ok(ret == 0, "lttng_uuid_from_str - Parse valid string '%s', expect success", valid_str_1); | |
52 | ||
53 | ret = lttng_uuid_from_str(valid_str_2, uuid1); | |
54 | ok(ret == 0, "lttng_uuid_from_str - Parse valid string '%s', expect success", valid_str_2); | |
55 | ||
56 | ret = lttng_uuid_from_str(valid_str_3, uuid1); | |
57 | ok(ret == 0, "lttng_uuid_from_str - Parse valid string '%s', expect success", valid_str_3); | |
58 | ||
59 | ret = lttng_uuid_from_str(valid_str_4, uuid1); | |
60 | ok(ret == 0, "lttng_uuid_from_str - Parse valid string '%s', expect success", valid_str_4); | |
61 | ||
62 | ret = lttng_uuid_from_str(valid_str_5, uuid1); | |
63 | ok(ret == 0, "lttng_uuid_from_str - Parse valid string '%s', expect success", valid_str_5); | |
64 | ||
65 | /* | |
66 | * Parse invalid UUID strings, expect failure. | |
67 | */ | |
68 | ret = lttng_uuid_from_str(invalid_str_1, uuid1); | |
28ab034a JG |
69 | ok(ret != 0, |
70 | "lttng_uuid_from_str - Parse invalid string '%s', expect failure", | |
71 | invalid_str_1); | |
c70636a7 MJ |
72 | |
73 | ret = lttng_uuid_from_str(invalid_str_2, uuid1); | |
28ab034a JG |
74 | ok(ret != 0, |
75 | "lttng_uuid_from_str - Parse invalid string '%s', expect failure", | |
76 | invalid_str_2); | |
c70636a7 MJ |
77 | |
78 | ret = lttng_uuid_from_str(invalid_str_3, uuid1); | |
28ab034a JG |
79 | ok(ret != 0, |
80 | "lttng_uuid_from_str - Parse invalid string '%s', expect failure", | |
81 | invalid_str_3); | |
c70636a7 MJ |
82 | |
83 | ret = lttng_uuid_from_str(invalid_str_4, uuid1); | |
28ab034a JG |
84 | ok(ret != 0, |
85 | "lttng_uuid_from_str - Parse invalid string '%s', expect failure", | |
86 | invalid_str_4); | |
c70636a7 MJ |
87 | |
88 | ret = lttng_uuid_from_str(invalid_str_5, uuid1); | |
28ab034a JG |
89 | ok(ret != 0, |
90 | "lttng_uuid_from_str - Parse invalid string '%s', expect failure", | |
91 | invalid_str_5); | |
c70636a7 MJ |
92 | |
93 | ret = lttng_uuid_from_str(invalid_str_6, uuid1); | |
28ab034a JG |
94 | ok(ret != 0, |
95 | "lttng_uuid_from_str - Parse invalid string '%s', expect failure", | |
96 | invalid_str_6); | |
c70636a7 MJ |
97 | } |
98 | ||
cd9adb8b | 99 | static void run_test_lttng_uuid_to_str() |
c70636a7 MJ |
100 | { |
101 | char uuid_str[LTTNG_UUID_STR_LEN]; | |
102 | ||
103 | lttng_uuid_to_str(valid_uuid_1, uuid_str); | |
28ab034a JG |
104 | ok(strcmp(uuid_str, valid_str_1) == 0, |
105 | "lttng_uuid_to_str - Convert UUID '%s' to string, expect success", | |
106 | valid_str_1); | |
c70636a7 MJ |
107 | |
108 | lttng_uuid_to_str(valid_uuid_2, uuid_str); | |
28ab034a JG |
109 | ok(strcmp(uuid_str, valid_str_2) == 0, |
110 | "lttng_uuid_to_str - Convert UUID '%s' to string, expect success", | |
111 | valid_str_2); | |
c70636a7 MJ |
112 | |
113 | lttng_uuid_to_str(valid_uuid_3, uuid_str); | |
28ab034a JG |
114 | ok(strcmp(uuid_str, valid_str_3) == 0, |
115 | "lttng_uuid_to_str - Convert UUID '%s' to string, expect success", | |
116 | valid_str_3); | |
c70636a7 MJ |
117 | } |
118 | ||
cd9adb8b | 119 | static void run_test_lttng_uuid_is_equal() |
c70636a7 MJ |
120 | { |
121 | int ret; | |
122 | lttng_uuid uuid1, uuid2; | |
123 | ||
b60adc34 JG |
124 | ret = lttng_uuid_from_str(valid_str_1, uuid1); |
125 | assert(ret == 0); | |
126 | ret = lttng_uuid_from_str(valid_str_1, uuid2); | |
127 | assert(ret == 0); | |
328c2fe7 | 128 | ret = uuid1 == uuid2; |
c70636a7 MJ |
129 | ok(ret == true, "lttng_uuid_is_equal - Compare same UUID, expect success"); |
130 | ||
b60adc34 JG |
131 | ret = lttng_uuid_from_str(valid_str_2, uuid2); |
132 | assert(ret == 0); | |
328c2fe7 | 133 | ret = uuid1 == uuid2; |
c70636a7 MJ |
134 | ok(ret == false, "lttng_uuid_is_equal - Compare different UUID, expect failure"); |
135 | } | |
136 | ||
cd9adb8b | 137 | static void run_test_lttng_uuid_copy() |
c70636a7 | 138 | { |
328c2fe7 | 139 | bool ret; |
c70636a7 MJ |
140 | lttng_uuid uuid1; |
141 | ||
328c2fe7 JG |
142 | uuid1 = valid_uuid_1; |
143 | ret = uuid1 == valid_uuid_1; | |
c70636a7 MJ |
144 | |
145 | ok(ret == true, "lttng_uuid_copy - Compare copied UUID with source, expect success"); | |
146 | } | |
147 | ||
cd9adb8b | 148 | static void run_test_lttng_uuid_generate() |
c70636a7 MJ |
149 | { |
150 | int ret; | |
151 | lttng_uuid uuid1, uuid2; | |
152 | ||
153 | lttng_uuid_generate(uuid1); | |
154 | lttng_uuid_generate(uuid2); | |
155 | ||
328c2fe7 | 156 | ok(uuid1 != uuid2, "lttng_uuid_generate - Generated UUIDs are different"); |
c70636a7 MJ |
157 | |
158 | /* | |
159 | * Set the two most significant bits (bits 6 and 7) of the | |
160 | * clock_seq_hi_and_reserved to zero and one, respectively. | |
161 | */ | |
162 | ret = uuid1[8] & (1 << 6); | |
163 | ok(ret == 0, "lttng_uuid_generate - bit 6 of clock_seq_hi_and_reserved is set to zero"); | |
164 | ||
165 | ret = uuid1[8] & (1 << 7); | |
166 | ok(ret != 0, "lttng_uuid_generate - bit 7 of clock_seq_hi_and_reserved is set to one"); | |
167 | ||
168 | /* | |
169 | * Set the four most significant bits (bits 12 through 15) of the | |
170 | * time_hi_and_version field to the 4-bit version number from | |
171 | * Section 4.1.3. | |
172 | */ | |
173 | ret = uuid1[6] >> 4; | |
174 | ok(ret == LTTNG_UUID_VER, "lttng_uuid_generate - Generated UUID version check"); | |
175 | } | |
176 | ||
cd9adb8b | 177 | static void run_test() |
c70636a7 MJ |
178 | { |
179 | plan_tests(NR_TESTS); | |
180 | ||
181 | run_test_lttng_uuid_from_str(); | |
182 | run_test_lttng_uuid_to_str(); | |
183 | run_test_lttng_uuid_is_equal(); | |
184 | run_test_lttng_uuid_copy(); | |
185 | run_test_lttng_uuid_generate(); | |
186 | } | |
187 | ||
cd9adb8b | 188 | int main() |
c70636a7 MJ |
189 | { |
190 | /* Run tap-formated tests */ | |
191 | run_test(); | |
192 | ||
193 | return exit_status(); | |
194 | } |