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