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