1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2011 Intel Corporation
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib.h>
#include "lib/bluetooth.h"
#include "lib/uuid.h"
#include "src/shared/tester.h"
struct uuid_test_data {
const char *str;
uint16_t val16;
uint32_t val32;
unsigned char *binary;
uint8_t type;
const char *str128;
unsigned char *binary128;
};
static unsigned char uuid_base_binary[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
static struct uuid_test_data uuid_base = {
.str = "0000",
.val16 = 0x0000,
.type = BT_UUID16,
.str128 = "00000000-0000-1000-8000-00805f9b34fb",
.binary128 = uuid_base_binary,
};
static unsigned char uuid_sixteen_binary[] = {
0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
static struct uuid_test_data uuid_sixteen1 = {
.str = "0x1234",
.val16 = 0x1234,
.type = BT_UUID16,
.str128 = "00001234-0000-1000-8000-00805F9B34FB",
.binary128 = uuid_sixteen_binary,
};
static struct uuid_test_data uuid_sixteen2 = {
.str = "1234",
.val16 = 0x1234,
.type = BT_UUID16,
.str128 = "00001234-0000-1000-8000-00805F9B34FB",
.binary128 = uuid_sixteen_binary,
};
static unsigned char uuid_32_binary[] = {
0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
static struct uuid_test_data uuid_32_1 = {
.str = "0x12345678",
.val32 = 0x12345678,
.type = BT_UUID32,
.str128 = "12345678-0000-1000-8000-00805F9B34FB",
.binary128 = uuid_32_binary,
};
static struct uuid_test_data uuid_32_2 = {
.str = "12345678",
.val32 = 0x12345678,
.type = BT_UUID32,
.str128 = "12345678-0000-1000-8000-00805F9B34FB",
.binary128 = uuid_32_binary,
};
static unsigned char uuid_128_binary[] = {
0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
static struct uuid_test_data uuid_128 = {
.str = "F0000000-0000-1000-8000-00805f9b34fb",
.binary = uuid_128_binary,
.type = BT_UUID128,
.str128 = "F0000000-0000-1000-8000-00805f9b34fb",
.binary128 = uuid_128_binary,
};
static void test_uuid(gconstpointer data)
{
const struct uuid_test_data *test_data = data;
bt_uuid_t uuid;
g_assert(bt_string_to_uuid(&uuid, test_data->str) == 0);
g_assert(uuid.type == test_data->type);
switch (uuid.type) {
case BT_UUID16:
g_assert(uuid.value.u16 == test_data->val16);
break;
case BT_UUID32:
g_assert(uuid.value.u32 == test_data->val32);
break;
case BT_UUID128:
/*
* No matter the system type: 128-bit UUID should use
* big-endian (human readable format).
*/
g_assert(memcmp(&uuid.value.u128, test_data->binary, 16) == 0);
break;
case BT_UUID_UNSPEC:
default:
tester_test_passed();
return;
}
tester_test_passed();
}
static void test_str(gconstpointer data)
{
const struct uuid_test_data *test_data = data;
const char *str;
char buf[128];
bt_uuid_t uuid;
if (g_str_has_prefix(test_data->str, "0x") == TRUE)
str = test_data->str + 2;
else
str = test_data->str;
g_assert(bt_string_to_uuid(&uuid, test_data->str) == 0);
bt_uuid_to_string(&uuid, buf, sizeof(buf));
g_assert(bt_uuid_strcmp(buf, str) == 0);
switch (test_data->type) {
case BT_UUID16:
bt_uuid16_create(&uuid, test_data->val16);
break;
case BT_UUID32:
bt_uuid32_create(&uuid, test_data->val32);
break;
default:
tester_test_passed();
return;
}
bt_uuid_to_string(&uuid, buf, sizeof(buf));
g_assert(bt_uuid_strcmp(buf, str) == 0);
tester_test_passed();
}
static void test_cmp(gconstpointer data)
{
const struct uuid_test_data *test_data = data;
bt_uuid_t uuid1, uuid2;
g_assert(bt_string_to_uuid(&uuid1, test_data->str) == 0);
g_assert(bt_string_to_uuid(&uuid2, test_data->str128) == 0);
g_assert(bt_uuid_cmp(&uuid1, &uuid2) == 0);
tester_test_passed();
}
static const struct uuid_test_data compress[] = {
{
.str = "00001234-0000-1000-8000-00805f9b34fb",
.type = BT_UUID16,
.val16 = 0x1234,
}, {
.str = "0000FFFF-0000-1000-8000-00805f9b34fb",
.type = BT_UUID16,
.val16 = 0xFFFF,
}, {
.str = "0000FFFF-0000-1000-8000-00805F9B34FB",
.type = BT_UUID16,
.val16 = 0xFFFF,
}, {
.str = "F0000000-0000-1000-8000-00805f9b34fb",
.type = BT_UUID128,
.binary = uuid_128_binary,
},
};
static const char *malformed[] = {
"0",
"01",
"012",
"xxxx",
"xxxxx",
"0xxxxx",
"0123456",
"012g4567",
"012345678",
"0x234567u9",
"01234567890",
"00001234-0000-1000-8000-00805F9B34F",
"00001234-0000-1000-8000 00805F9B34FB",
"00001234-0000-1000-8000-00805F9B34FBC",
"00001234-0000-1000-800G-00805F9B34FB",
NULL,
};
static void test_malformed(gconstpointer data)
{
const char *str = data;
bt_uuid_t uuid;
g_assert(bt_string_to_uuid(&uuid, str) != 0);
tester_test_passed();
}
int main(int argc, char *argv[])
{
size_t i;
tester_init(&argc, &argv);
tester_add("/uuid/base", &uuid_base, NULL, test_uuid, NULL);
tester_add("/uuid/base/str", &uuid_base, NULL, test_str, NULL);
tester_add("/uuid/base/cmp", &uuid_base, NULL, test_cmp, NULL);
tester_add("/uuid/sixteen1", &uuid_sixteen1, NULL, test_uuid, NULL);
tester_add("/uuid/sixteen1/str", &uuid_sixteen1, NULL, test_str, NULL);
tester_add("/uuid/sixteen1/cmp", &uuid_sixteen1, NULL, test_cmp, NULL);
tester_add("/uuid/sixteen2", &uuid_sixteen2, NULL, test_uuid, NULL);
tester_add("/uuid/sixteen2/str", &uuid_sixteen2, NULL, test_str, NULL);
tester_add("/uuid/sixteen2/cmp", &uuid_sixteen2, NULL, test_cmp, NULL);
tester_add("/uuid/thirtytwo1", &uuid_32_1, NULL, test_uuid, NULL);
tester_add("/uuid/thirtytwo1/str", &uuid_32_1, NULL, test_str, NULL);
tester_add("/uuid/thirtytwo1/cmp", &uuid_32_1, NULL, test_cmp, NULL);
tester_add("/uuid/thirtytwo2", &uuid_32_2, NULL, test_uuid, NULL);
tester_add("/uuid/thritytwo2/str", &uuid_32_2, NULL, test_str, NULL);
tester_add("/uuid/thirtytwo2/cmp", &uuid_32_2, NULL, test_cmp, NULL);
tester_add("/uuid/onetwentyeight", &uuid_128, NULL, test_uuid, NULL);
tester_add("/uuid/onetwentyeight/str", &uuid_128, NULL, test_str, NULL);
tester_add("/uuid/onetwentyeight/cmp", &uuid_128, NULL, test_cmp, NULL);
for (i = 0; malformed[i]; i++) {
char *testpath;
testpath = g_strdup_printf("/uuid/malformed/%s", malformed[i]);
tester_add(testpath, malformed[i], NULL, test_malformed, NULL);
g_free(testpath);
}
for (i = 0; i < (sizeof(compress) / sizeof(compress[0])); i++) {
char *testpath;
testpath = g_strdup_printf("/uuid/compress/%s",
compress[i].str);
tester_add(testpath, compress + i, NULL, test_uuid, NULL);
g_free(testpath);
}
return tester_run();
}
|