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