utils_module_tests.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * utils module tests
  3. * Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/bitfield.h"
  11. #include "utils/ext_password.h"
  12. #include "utils/trace.h"
  13. #include "utils/base64.h"
  14. struct printf_test_data {
  15. u8 *data;
  16. size_t len;
  17. char *encoded;
  18. };
  19. static const struct printf_test_data printf_tests[] = {
  20. { (u8 *) "abcde", 5, "abcde" },
  21. { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
  22. { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
  23. { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
  24. { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
  25. "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
  26. { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
  27. "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
  28. { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
  29. "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
  30. { NULL, 0, NULL }
  31. };
  32. static int printf_encode_decode_tests(void)
  33. {
  34. int i;
  35. size_t binlen;
  36. char buf[100];
  37. u8 bin[100];
  38. int errors = 0;
  39. wpa_printf(MSG_INFO, "printf encode/decode tests");
  40. for (i = 0; printf_tests[i].data; i++) {
  41. const struct printf_test_data *test = &printf_tests[i];
  42. printf_encode(buf, sizeof(buf), test->data, test->len);
  43. wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
  44. binlen = printf_decode(bin, sizeof(bin), buf);
  45. if (binlen != test->len ||
  46. os_memcmp(bin, test->data, binlen) != 0) {
  47. wpa_hexdump(MSG_ERROR, "Error in decoding#1",
  48. bin, binlen);
  49. errors++;
  50. }
  51. binlen = printf_decode(bin, sizeof(bin), test->encoded);
  52. if (binlen != test->len ||
  53. os_memcmp(bin, test->data, binlen) != 0) {
  54. wpa_hexdump(MSG_ERROR, "Error in decoding#2",
  55. bin, binlen);
  56. errors++;
  57. }
  58. }
  59. buf[5] = 'A';
  60. printf_encode(buf, 5, (const u8 *) "abcde", 5);
  61. if (buf[5] != 'A') {
  62. wpa_printf(MSG_ERROR, "Error in bounds checking#1");
  63. errors++;
  64. }
  65. for (i = 5; i < 10; i++) {
  66. buf[i] = 'A';
  67. printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
  68. if (buf[i] != 'A') {
  69. wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
  70. i);
  71. errors++;
  72. }
  73. }
  74. if (printf_decode(bin, 3, "abcde") != 2)
  75. errors++;
  76. if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
  77. errors++;
  78. if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
  79. errors++;
  80. if (errors) {
  81. wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. static int bitfield_tests(void)
  87. {
  88. struct bitfield *bf;
  89. int i;
  90. int errors = 0;
  91. wpa_printf(MSG_INFO, "bitfield tests");
  92. bf = bitfield_alloc(123);
  93. if (bf == NULL)
  94. return -1;
  95. for (i = 0; i < 123; i++) {
  96. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  97. errors++;
  98. if (i > 0 && bitfield_is_set(bf, i - 1))
  99. errors++;
  100. bitfield_set(bf, i);
  101. if (!bitfield_is_set(bf, i))
  102. errors++;
  103. bitfield_clear(bf, i);
  104. if (bitfield_is_set(bf, i))
  105. errors++;
  106. }
  107. for (i = 123; i < 200; i++) {
  108. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  109. errors++;
  110. if (i > 0 && bitfield_is_set(bf, i - 1))
  111. errors++;
  112. bitfield_set(bf, i);
  113. if (bitfield_is_set(bf, i))
  114. errors++;
  115. bitfield_clear(bf, i);
  116. if (bitfield_is_set(bf, i))
  117. errors++;
  118. }
  119. for (i = 0; i < 123; i++) {
  120. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  121. errors++;
  122. bitfield_set(bf, i);
  123. if (!bitfield_is_set(bf, i))
  124. errors++;
  125. }
  126. for (i = 0; i < 123; i++) {
  127. if (!bitfield_is_set(bf, i))
  128. errors++;
  129. bitfield_clear(bf, i);
  130. if (bitfield_is_set(bf, i))
  131. errors++;
  132. }
  133. for (i = 0; i < 123; i++) {
  134. if (bitfield_get_first_zero(bf) != i)
  135. errors++;
  136. bitfield_set(bf, i);
  137. }
  138. if (bitfield_get_first_zero(bf) != -1)
  139. errors++;
  140. for (i = 0; i < 123; i++) {
  141. if (!bitfield_is_set(bf, i))
  142. errors++;
  143. bitfield_clear(bf, i);
  144. if (bitfield_get_first_zero(bf) != i)
  145. errors++;
  146. bitfield_set(bf, i);
  147. }
  148. if (bitfield_get_first_zero(bf) != -1)
  149. errors++;
  150. bitfield_free(bf);
  151. bf = bitfield_alloc(8);
  152. if (bf == NULL)
  153. return -1;
  154. if (bitfield_get_first_zero(bf) != 0)
  155. errors++;
  156. for (i = 0; i < 8; i++)
  157. bitfield_set(bf, i);
  158. if (bitfield_get_first_zero(bf) != -1)
  159. errors++;
  160. bitfield_free(bf);
  161. if (errors) {
  162. wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
  163. return -1;
  164. }
  165. return 0;
  166. }
  167. static int int_array_tests(void)
  168. {
  169. int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
  170. int test2[] = { 1, -1, 0 };
  171. int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
  172. int test3_res[] = { -1, 1, 2, 3, 4, 0 };
  173. int errors = 0;
  174. int len;
  175. wpa_printf(MSG_INFO, "int_array tests");
  176. if (int_array_len(test1) != 6 ||
  177. int_array_len(test2) != 2)
  178. errors++;
  179. int_array_sort_unique(test3);
  180. len = int_array_len(test3_res);
  181. if (int_array_len(test3) != len)
  182. errors++;
  183. else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
  184. errors++;
  185. if (errors) {
  186. wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. static int ext_password_tests(void)
  192. {
  193. struct ext_password_data *data;
  194. int ret = 0;
  195. struct wpabuf *pw;
  196. wpa_printf(MSG_INFO, "ext_password tests");
  197. data = ext_password_init("unknown", "foo");
  198. if (data != NULL)
  199. return -1;
  200. data = ext_password_init("test", NULL);
  201. if (data == NULL)
  202. return -1;
  203. pw = ext_password_get(data, "foo");
  204. if (pw != NULL)
  205. ret = -1;
  206. ext_password_free(pw);
  207. ext_password_deinit(data);
  208. pw = ext_password_get(NULL, "foo");
  209. if (pw != NULL)
  210. ret = -1;
  211. ext_password_free(pw);
  212. return ret;
  213. }
  214. static int trace_tests(void)
  215. {
  216. wpa_printf(MSG_INFO, "trace tests");
  217. wpa_trace_show("test backtrace");
  218. wpa_trace_dump_funcname("test funcname", trace_tests);
  219. return 0;
  220. }
  221. static int base64_tests(void)
  222. {
  223. int errors = 0;
  224. unsigned char *res;
  225. size_t res_len;
  226. wpa_printf(MSG_INFO, "base64 tests");
  227. res = base64_encode((const unsigned char *) "", ~0, &res_len);
  228. if (res) {
  229. errors++;
  230. os_free(res);
  231. }
  232. res = base64_encode((const unsigned char *) "=", 1, &res_len);
  233. if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
  234. res[2] != '=' || res[3] != '=' || res[4] != '\n')
  235. errors++;
  236. os_free(res);
  237. res = base64_encode((const unsigned char *) "=", 1, NULL);
  238. if (!res || res[0] != 'P' || res[1] != 'Q' ||
  239. res[2] != '=' || res[3] != '=' || res[4] != '\n')
  240. errors++;
  241. os_free(res);
  242. res = base64_decode((const unsigned char *) "", 0, &res_len);
  243. if (res) {
  244. errors++;
  245. os_free(res);
  246. }
  247. res = base64_decode((const unsigned char *) "a", 1, &res_len);
  248. if (res) {
  249. errors++;
  250. os_free(res);
  251. }
  252. res = base64_decode((const unsigned char *) "====", 4, &res_len);
  253. if (res) {
  254. errors++;
  255. os_free(res);
  256. }
  257. res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
  258. if (!res || res_len != 1 || res[0] != '=')
  259. errors++;
  260. os_free(res);
  261. res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
  262. if (!res || res_len != 1 || res[0] != '=')
  263. errors++;
  264. os_free(res);
  265. if (errors) {
  266. wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
  267. return -1;
  268. }
  269. return 0;
  270. }
  271. static int common_tests(void)
  272. {
  273. char buf[3];
  274. u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
  275. u8 bin[3];
  276. int errors = 0;
  277. struct wpa_freq_range_list ranges;
  278. wpa_printf(MSG_INFO, "common tests");
  279. if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
  280. errors++;
  281. if (wpa_scnprintf(buf, 0, "hello") != 0 ||
  282. wpa_scnprintf(buf, 3, "hello") != 2)
  283. errors++;
  284. if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
  285. wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
  286. errors++;
  287. if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
  288. merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
  289. errors++;
  290. if (dup_binstr(NULL, 0) != NULL)
  291. errors++;
  292. if (freq_range_list_includes(NULL, 0) != 0)
  293. errors++;
  294. os_memset(&ranges, 0, sizeof(ranges));
  295. if (freq_range_list_parse(&ranges, "") != 0 ||
  296. freq_range_list_includes(&ranges, 0) != 0 ||
  297. freq_range_list_str(&ranges) != NULL)
  298. errors++;
  299. if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
  300. utf8_unescape("a", 1, NULL, 0) != 0 ||
  301. utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
  302. utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
  303. utf8_unescape("abc", 3, buf, 3) != 3)
  304. errors++;
  305. if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
  306. errors++;
  307. if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
  308. errors++;
  309. if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
  310. utf8_escape("a", 1, NULL, 0) != 0 ||
  311. utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
  312. utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
  313. utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
  314. utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
  315. utf8_escape("abc", 3, buf, 3) != 3)
  316. errors++;
  317. if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
  318. errors++;
  319. if (errors) {
  320. wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. int utils_module_tests(void)
  326. {
  327. int ret = 0;
  328. wpa_printf(MSG_INFO, "utils module tests");
  329. if (printf_encode_decode_tests() < 0 ||
  330. ext_password_tests() < 0 ||
  331. trace_tests() < 0 ||
  332. bitfield_tests() < 0 ||
  333. base64_tests() < 0 ||
  334. common_tests() < 0 ||
  335. int_array_tests() < 0)
  336. ret = -1;
  337. return ret;
  338. }