utils_module_tests.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 "common/ieee802_11_defs.h"
  11. #include "utils/bitfield.h"
  12. #include "utils/ext_password.h"
  13. #include "utils/trace.h"
  14. #include "utils/base64.h"
  15. #include "utils/ip_addr.h"
  16. struct printf_test_data {
  17. u8 *data;
  18. size_t len;
  19. char *encoded;
  20. };
  21. static const struct printf_test_data printf_tests[] = {
  22. { (u8 *) "abcde", 5, "abcde" },
  23. { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
  24. { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
  25. { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
  26. { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
  27. "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
  28. { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
  29. "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
  30. { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
  31. "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
  32. { NULL, 0, NULL }
  33. };
  34. static int printf_encode_decode_tests(void)
  35. {
  36. int i;
  37. size_t binlen;
  38. char buf[100];
  39. u8 bin[100];
  40. int errors = 0;
  41. int array[10];
  42. wpa_printf(MSG_INFO, "printf encode/decode tests");
  43. for (i = 0; printf_tests[i].data; i++) {
  44. const struct printf_test_data *test = &printf_tests[i];
  45. printf_encode(buf, sizeof(buf), test->data, test->len);
  46. wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
  47. binlen = printf_decode(bin, sizeof(bin), buf);
  48. if (binlen != test->len ||
  49. os_memcmp(bin, test->data, binlen) != 0) {
  50. wpa_hexdump(MSG_ERROR, "Error in decoding#1",
  51. bin, binlen);
  52. errors++;
  53. }
  54. binlen = printf_decode(bin, sizeof(bin), test->encoded);
  55. if (binlen != test->len ||
  56. os_memcmp(bin, test->data, binlen) != 0) {
  57. wpa_hexdump(MSG_ERROR, "Error in decoding#2",
  58. bin, binlen);
  59. errors++;
  60. }
  61. }
  62. buf[5] = 'A';
  63. printf_encode(buf, 5, (const u8 *) "abcde", 5);
  64. if (buf[5] != 'A') {
  65. wpa_printf(MSG_ERROR, "Error in bounds checking#1");
  66. errors++;
  67. }
  68. for (i = 5; i < 10; i++) {
  69. buf[i] = 'A';
  70. printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
  71. if (buf[i] != 'A') {
  72. wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
  73. i);
  74. errors++;
  75. }
  76. }
  77. if (printf_decode(bin, 3, "abcde") != 2)
  78. errors++;
  79. if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
  80. errors++;
  81. if (printf_decode(bin, 3, "\\xq") != 1 || bin[0] != 'q')
  82. errors++;
  83. if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
  84. errors++;
  85. array[0] = 10;
  86. array[1] = 10;
  87. array[2] = 5;
  88. array[3] = 10;
  89. array[4] = 5;
  90. array[5] = 0;
  91. if (int_array_len(array) != 5)
  92. errors++;
  93. int_array_sort_unique(array);
  94. if (int_array_len(array) != 2)
  95. errors++;
  96. if (errors) {
  97. wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. static int bitfield_tests(void)
  103. {
  104. struct bitfield *bf;
  105. int i;
  106. int errors = 0;
  107. wpa_printf(MSG_INFO, "bitfield tests");
  108. bf = bitfield_alloc(123);
  109. if (bf == NULL)
  110. return -1;
  111. for (i = 0; i < 123; i++) {
  112. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  113. errors++;
  114. if (i > 0 && bitfield_is_set(bf, i - 1))
  115. errors++;
  116. bitfield_set(bf, i);
  117. if (!bitfield_is_set(bf, i))
  118. errors++;
  119. bitfield_clear(bf, i);
  120. if (bitfield_is_set(bf, i))
  121. errors++;
  122. }
  123. for (i = 123; i < 200; i++) {
  124. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  125. errors++;
  126. if (i > 0 && bitfield_is_set(bf, i - 1))
  127. errors++;
  128. bitfield_set(bf, i);
  129. if (bitfield_is_set(bf, i))
  130. errors++;
  131. bitfield_clear(bf, i);
  132. if (bitfield_is_set(bf, i))
  133. errors++;
  134. }
  135. for (i = 0; i < 123; i++) {
  136. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  137. errors++;
  138. bitfield_set(bf, i);
  139. if (!bitfield_is_set(bf, i))
  140. errors++;
  141. }
  142. for (i = 0; i < 123; i++) {
  143. if (!bitfield_is_set(bf, i))
  144. errors++;
  145. bitfield_clear(bf, i);
  146. if (bitfield_is_set(bf, i))
  147. errors++;
  148. }
  149. for (i = 0; i < 123; i++) {
  150. if (bitfield_get_first_zero(bf) != i)
  151. errors++;
  152. bitfield_set(bf, i);
  153. }
  154. if (bitfield_get_first_zero(bf) != -1)
  155. errors++;
  156. for (i = 0; i < 123; i++) {
  157. if (!bitfield_is_set(bf, i))
  158. errors++;
  159. bitfield_clear(bf, i);
  160. if (bitfield_get_first_zero(bf) != i)
  161. errors++;
  162. bitfield_set(bf, i);
  163. }
  164. if (bitfield_get_first_zero(bf) != -1)
  165. errors++;
  166. bitfield_free(bf);
  167. bf = bitfield_alloc(8);
  168. if (bf == NULL)
  169. return -1;
  170. if (bitfield_get_first_zero(bf) != 0)
  171. errors++;
  172. for (i = 0; i < 8; i++)
  173. bitfield_set(bf, i);
  174. if (bitfield_get_first_zero(bf) != -1)
  175. errors++;
  176. bitfield_free(bf);
  177. if (errors) {
  178. wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. static int int_array_tests(void)
  184. {
  185. int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
  186. int test2[] = { 1, -1, 0 };
  187. int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
  188. int test3_res[] = { -1, 1, 2, 3, 4, 0 };
  189. int errors = 0;
  190. int len;
  191. wpa_printf(MSG_INFO, "int_array tests");
  192. if (int_array_len(test1) != 6 ||
  193. int_array_len(test2) != 2)
  194. errors++;
  195. int_array_sort_unique(test3);
  196. len = int_array_len(test3_res);
  197. if (int_array_len(test3) != len)
  198. errors++;
  199. else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
  200. errors++;
  201. if (errors) {
  202. wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. static int ext_password_tests(void)
  208. {
  209. struct ext_password_data *data;
  210. int ret = 0;
  211. struct wpabuf *pw;
  212. wpa_printf(MSG_INFO, "ext_password tests");
  213. data = ext_password_init("unknown", "foo");
  214. if (data != NULL)
  215. return -1;
  216. data = ext_password_init("test", NULL);
  217. if (data == NULL)
  218. return -1;
  219. pw = ext_password_get(data, "foo");
  220. if (pw != NULL)
  221. ret = -1;
  222. ext_password_free(pw);
  223. ext_password_deinit(data);
  224. pw = ext_password_get(NULL, "foo");
  225. if (pw != NULL)
  226. ret = -1;
  227. ext_password_free(pw);
  228. return ret;
  229. }
  230. static int trace_tests(void)
  231. {
  232. wpa_printf(MSG_INFO, "trace tests");
  233. wpa_trace_show("test backtrace");
  234. wpa_trace_dump_funcname("test funcname", trace_tests);
  235. return 0;
  236. }
  237. static int base64_tests(void)
  238. {
  239. int errors = 0;
  240. unsigned char *res;
  241. size_t res_len;
  242. wpa_printf(MSG_INFO, "base64 tests");
  243. res = base64_encode((const unsigned char *) "", ~0, &res_len);
  244. if (res) {
  245. errors++;
  246. os_free(res);
  247. }
  248. res = base64_encode((const unsigned char *) "=", 1, &res_len);
  249. if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
  250. res[2] != '=' || res[3] != '=' || res[4] != '\n')
  251. errors++;
  252. os_free(res);
  253. res = base64_encode((const unsigned char *) "=", 1, NULL);
  254. if (!res || res[0] != 'P' || res[1] != 'Q' ||
  255. res[2] != '=' || res[3] != '=' || res[4] != '\n')
  256. errors++;
  257. os_free(res);
  258. res = base64_decode((const unsigned char *) "", 0, &res_len);
  259. if (res) {
  260. errors++;
  261. os_free(res);
  262. }
  263. res = base64_decode((const unsigned char *) "a", 1, &res_len);
  264. if (res) {
  265. errors++;
  266. os_free(res);
  267. }
  268. res = base64_decode((const unsigned char *) "====", 4, &res_len);
  269. if (res) {
  270. errors++;
  271. os_free(res);
  272. }
  273. res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
  274. if (!res || res_len != 1 || res[0] != '=')
  275. errors++;
  276. os_free(res);
  277. res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
  278. if (!res || res_len != 1 || res[0] != '=')
  279. errors++;
  280. os_free(res);
  281. if (errors) {
  282. wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
  283. return -1;
  284. }
  285. return 0;
  286. }
  287. static int common_tests(void)
  288. {
  289. char buf[3], longbuf[100];
  290. u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
  291. u8 bin[3];
  292. int errors = 0;
  293. struct wpa_freq_range_list ranges;
  294. size_t len;
  295. const char *txt;
  296. u8 ssid[255];
  297. wpa_printf(MSG_INFO, "common tests");
  298. if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
  299. errors++;
  300. if (wpa_scnprintf(buf, 0, "hello") != 0 ||
  301. wpa_scnprintf(buf, 3, "hello") != 2)
  302. errors++;
  303. if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
  304. wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
  305. errors++;
  306. if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
  307. merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
  308. errors++;
  309. if (dup_binstr(NULL, 0) != NULL)
  310. errors++;
  311. if (freq_range_list_includes(NULL, 0) != 0)
  312. errors++;
  313. os_memset(&ranges, 0, sizeof(ranges));
  314. if (freq_range_list_parse(&ranges, "") != 0 ||
  315. freq_range_list_includes(&ranges, 0) != 0 ||
  316. freq_range_list_str(&ranges) != NULL)
  317. errors++;
  318. if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
  319. utf8_unescape("a", 1, NULL, 0) != 0 ||
  320. utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
  321. utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
  322. utf8_unescape("abc", 3, buf, 3) != 3)
  323. errors++;
  324. if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
  325. errors++;
  326. if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
  327. errors++;
  328. if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
  329. utf8_escape("a", 1, NULL, 0) != 0 ||
  330. utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
  331. utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
  332. utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
  333. utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
  334. utf8_escape("abc", 3, buf, 3) != 3)
  335. errors++;
  336. if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
  337. errors++;
  338. os_memset(ssid, 0, sizeof(ssid));
  339. txt = wpa_ssid_txt(ssid, sizeof(ssid));
  340. len = os_strlen(txt);
  341. /* Verify that SSID_MAX_LEN * 4 buffer limit is enforced. */
  342. if (len != SSID_MAX_LEN * 4) {
  343. wpa_printf(MSG_ERROR,
  344. "Unexpected wpa_ssid_txt() result with too long SSID");
  345. errors++;
  346. }
  347. if (wpa_snprintf_hex_sep(longbuf, 0, addr, ETH_ALEN, '-') != 0 ||
  348. wpa_snprintf_hex_sep(longbuf, 5, addr, ETH_ALEN, '-') != 3 ||
  349. os_strcmp(longbuf, "01-0") != 0)
  350. errors++;
  351. if (errors) {
  352. wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
  353. return -1;
  354. }
  355. return 0;
  356. }
  357. static int os_tests(void)
  358. {
  359. int errors = 0;
  360. void *ptr;
  361. os_time_t t;
  362. wpa_printf(MSG_INFO, "os tests");
  363. ptr = os_calloc((size_t) -1, (size_t) -1);
  364. if (ptr) {
  365. errors++;
  366. os_free(ptr);
  367. }
  368. ptr = os_calloc((size_t) 2, (size_t) -1);
  369. if (ptr) {
  370. errors++;
  371. os_free(ptr);
  372. }
  373. ptr = os_calloc((size_t) -1, (size_t) 2);
  374. if (ptr) {
  375. errors++;
  376. os_free(ptr);
  377. }
  378. ptr = os_realloc_array(NULL, (size_t) -1, (size_t) -1);
  379. if (ptr) {
  380. errors++;
  381. os_free(ptr);
  382. }
  383. os_sleep(1, 1);
  384. if (os_mktime(1969, 1, 1, 1, 1, 1, &t) == 0 ||
  385. os_mktime(1971, 0, 1, 1, 1, 1, &t) == 0 ||
  386. os_mktime(1971, 13, 1, 1, 1, 1, &t) == 0 ||
  387. os_mktime(1971, 1, 0, 1, 1, 1, &t) == 0 ||
  388. os_mktime(1971, 1, 32, 1, 1, 1, &t) == 0 ||
  389. os_mktime(1971, 1, 1, -1, 1, 1, &t) == 0 ||
  390. os_mktime(1971, 1, 1, 24, 1, 1, &t) == 0 ||
  391. os_mktime(1971, 1, 1, 1, -1, 1, &t) == 0 ||
  392. os_mktime(1971, 1, 1, 1, 60, 1, &t) == 0 ||
  393. os_mktime(1971, 1, 1, 1, 1, -1, &t) == 0 ||
  394. os_mktime(1971, 1, 1, 1, 1, 61, &t) == 0 ||
  395. os_mktime(1971, 1, 1, 1, 1, 1, &t) != 0 ||
  396. os_mktime(2020, 1, 2, 3, 4, 5, &t) != 0 ||
  397. os_mktime(2015, 12, 31, 23, 59, 59, &t) != 0)
  398. errors++;
  399. if (os_setenv("hwsim_test_env", "test value", 0) != 0 ||
  400. os_setenv("hwsim_test_env", "test value 2", 1) != 0 ||
  401. os_unsetenv("hwsim_test_env") != 0)
  402. errors++;
  403. if (os_file_exists("/this-file-does-not-exists-hwsim") != 0)
  404. errors++;
  405. if (errors) {
  406. wpa_printf(MSG_ERROR, "%d os test(s) failed", errors);
  407. return -1;
  408. }
  409. return 0;
  410. }
  411. static int wpabuf_tests(void)
  412. {
  413. int errors = 0;
  414. void *ptr;
  415. struct wpabuf *buf;
  416. wpa_printf(MSG_INFO, "wpabuf tests");
  417. ptr = os_malloc(100);
  418. if (ptr) {
  419. buf = wpabuf_alloc_ext_data(ptr, 100);
  420. if (buf) {
  421. if (wpabuf_resize(&buf, 100) < 0)
  422. errors++;
  423. else
  424. wpabuf_put(buf, 100);
  425. wpabuf_free(buf);
  426. } else {
  427. errors++;
  428. os_free(ptr);
  429. }
  430. } else {
  431. errors++;
  432. }
  433. buf = wpabuf_alloc(100);
  434. if (buf) {
  435. struct wpabuf *buf2;
  436. wpabuf_put(buf, 100);
  437. if (wpabuf_resize(&buf, 100) < 0)
  438. errors++;
  439. else
  440. wpabuf_put(buf, 100);
  441. buf2 = wpabuf_concat(buf, NULL);
  442. if (buf2 != buf)
  443. errors++;
  444. wpabuf_free(buf2);
  445. } else {
  446. errors++;
  447. }
  448. buf = NULL;
  449. buf = wpabuf_zeropad(buf, 10);
  450. if (buf != NULL)
  451. errors++;
  452. if (errors) {
  453. wpa_printf(MSG_ERROR, "%d wpabuf test(s) failed", errors);
  454. return -1;
  455. }
  456. return 0;
  457. }
  458. static int ip_addr_tests(void)
  459. {
  460. int errors = 0;
  461. struct hostapd_ip_addr addr;
  462. char buf[100];
  463. wpa_printf(MSG_INFO, "ip_addr tests");
  464. if (hostapd_parse_ip_addr("1.2.3.4", &addr) != 0 ||
  465. addr.af != AF_INET ||
  466. hostapd_ip_txt(NULL, buf, sizeof(buf)) != NULL ||
  467. hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
  468. hostapd_ip_txt(&addr, buf, 0) != NULL ||
  469. hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
  470. errors++;
  471. if (hostapd_parse_ip_addr("::", &addr) != 0 ||
  472. addr.af != AF_INET6 ||
  473. hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
  474. hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
  475. errors++;
  476. if (errors) {
  477. wpa_printf(MSG_ERROR, "%d ip_addr test(s) failed", errors);
  478. return -1;
  479. }
  480. return 0;
  481. }
  482. int utils_module_tests(void)
  483. {
  484. int ret = 0;
  485. wpa_printf(MSG_INFO, "utils module tests");
  486. if (printf_encode_decode_tests() < 0 ||
  487. ext_password_tests() < 0 ||
  488. trace_tests() < 0 ||
  489. bitfield_tests() < 0 ||
  490. base64_tests() < 0 ||
  491. common_tests() < 0 ||
  492. os_tests() < 0 ||
  493. wpabuf_tests() < 0 ||
  494. ip_addr_tests() < 0 ||
  495. int_array_tests() < 0)
  496. ret = -1;
  497. return ret;
  498. }