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