test-uuid.c 6.6 KB

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