3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2011 Intel Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "lib/bluetooth.h"
33 struct uuid_test_data {
37 unsigned char *binary;
40 unsigned char *binary128;
43 static unsigned char uuid_base_binary[] = {
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
45 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
47 static struct uuid_test_data uuid_base = {
51 .str128 = "00000000-0000-1000-8000-00805f9b34fb",
52 .binary128 = uuid_base_binary,
55 static unsigned char uuid_sixteen_binary[] = {
56 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x10, 0x00,
57 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
59 static struct uuid_test_data uuid_sixteen1 = {
63 .str128 = "00001234-0000-1000-8000-00805F9B34FB",
64 .binary128 = uuid_sixteen_binary,
67 static struct uuid_test_data uuid_sixteen2 = {
71 .str128 = "00001234-0000-1000-8000-00805F9B34FB",
72 .binary128 = uuid_sixteen_binary,
75 static unsigned char uuid_32_binary[] = {
76 0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x10, 0x00,
77 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
79 static struct uuid_test_data uuid_32_1 = {
83 .str128 = "12345678-0000-1000-8000-00805F9B34FB",
84 .binary128 = uuid_32_binary,
87 static struct uuid_test_data uuid_32_2 = {
91 .str128 = "12345678-0000-1000-8000-00805F9B34FB",
92 .binary128 = uuid_32_binary,
95 static unsigned char uuid_128_binary[] = {
96 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
97 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
99 static struct uuid_test_data uuid_128 = {
100 .str = "F0000000-0000-1000-8000-00805f9b34fb",
101 .binary = uuid_128_binary,
103 .str128 = "F0000000-0000-1000-8000-00805f9b34fb",
104 .binary128 = uuid_128_binary,
107 static void test_uuid(gconstpointer data)
109 const struct uuid_test_data *test_data = data;
112 g_assert(bt_string_to_uuid(&uuid, test_data->str) == 0);
113 g_assert(uuid.type == test_data->type);
117 g_assert(uuid.value.u16 == test_data->val16);
120 g_assert(uuid.value.u32 == test_data->val32);
124 * No matter the system type: 128-bit UUID should use
125 * big-endian (human readable format).
127 g_assert(memcmp(&uuid.value.u128, test_data->binary, 16) == 0);
135 static void test_str(gconstpointer data)
137 const struct uuid_test_data *test_data = data;
142 if (g_str_has_prefix(test_data->str, "0x") == TRUE)
143 str = test_data->str + 2;
145 str = test_data->str;
147 g_assert(bt_string_to_uuid(&uuid, test_data->str) == 0);
149 bt_uuid_to_string(&uuid, buf, sizeof(buf));
150 g_assert(strcasecmp(buf, str) == 0);
152 switch (test_data->type) {
154 bt_uuid16_create(&uuid, test_data->val16);
157 bt_uuid32_create(&uuid, test_data->val32);
163 bt_uuid_to_string(&uuid, buf, sizeof(buf));
164 g_assert(strcasecmp(buf, str) == 0);
167 static void test_cmp(gconstpointer data)
169 const struct uuid_test_data *test_data = data;
170 bt_uuid_t uuid1, uuid2;
172 g_assert(bt_string_to_uuid(&uuid1, test_data->str) == 0);
173 g_assert(bt_string_to_uuid(&uuid2, test_data->str128) == 0);
175 g_assert(bt_uuid_cmp(&uuid1, &uuid2) == 0);
178 static const struct uuid_test_data compress[] = {
180 .str = "00001234-0000-1000-8000-00805f9b34fb",
184 .str = "0000FFFF-0000-1000-8000-00805f9b34fb",
188 .str = "0000FFFF-0000-1000-8000-00805F9B34FB",
192 .str = "F0000000-0000-1000-8000-00805f9b34fb",
194 .binary = uuid_128_binary,
198 static const char *malformed[] = {
210 "00001234-0000-1000-8000-00805F9B34F",
211 "00001234-0000-1000-8000 00805F9B34FB",
212 "00001234-0000-1000-8000-00805F9B34FBC",
213 "00001234-0000-1000-800G-00805F9B34FB",
217 static void test_malformed(gconstpointer data)
219 const char *str = data;
222 g_assert(bt_string_to_uuid(&uuid, str) != 0);
225 int main(int argc, char *argv[])
229 g_test_init(&argc, &argv, NULL);
231 g_test_add_data_func("/uuid/base", &uuid_base, test_uuid);
232 g_test_add_data_func("/uuid/base/str", &uuid_base, test_str);
233 g_test_add_data_func("/uuid/base/cmp", &uuid_base, test_cmp);
235 g_test_add_data_func("/uuid/sixteen1", &uuid_sixteen1, test_uuid);
236 g_test_add_data_func("/uuid/sixteen1/str", &uuid_sixteen1, test_str);
237 g_test_add_data_func("/uuid/sixteen1/cmp", &uuid_sixteen1, test_cmp);
239 g_test_add_data_func("/uuid/sixteen2", &uuid_sixteen2, test_uuid);
240 g_test_add_data_func("/uuid/sixteen2/str", &uuid_sixteen2, test_str);
241 g_test_add_data_func("/uuid/sixteen2/cmp", &uuid_sixteen2, test_cmp);
243 g_test_add_data_func("/uuid/thirtytwo1", &uuid_32_1, test_uuid);
244 g_test_add_data_func("/uuid/thirtytwo1/str", &uuid_32_1, test_str);
245 g_test_add_data_func("/uuid/thirtytwo1/cmp", &uuid_32_1, test_cmp);
247 g_test_add_data_func("/uuid/thirtytwo2", &uuid_32_2, test_uuid);
248 g_test_add_data_func("/uuid/thritytwo2/str", &uuid_32_2, test_str);
249 g_test_add_data_func("/uuid/thirtytwo2/cmp", &uuid_32_2, test_cmp);
251 g_test_add_data_func("/uuid/onetwentyeight", &uuid_128, test_uuid);
252 g_test_add_data_func("/uuid/onetwentyeight/str", &uuid_128, test_str);
253 g_test_add_data_func("/uuid/onetwentyeight/cmp", &uuid_128, test_cmp);
255 for (i = 0; malformed[i]; i++) {
258 testpath = g_strdup_printf("/uuid/malformed/%s", malformed[i]);
259 g_test_add_data_func(testpath, malformed[i], test_malformed);
263 for (i = 0; i < (sizeof(compress) / sizeof(compress[0])); i++) {
266 testpath = g_strdup_printf("/uuid/compress/%s",
268 g_test_add_data_func(testpath, compress + i, test_uuid);