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