utils_module_tests.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. #include "utils/eloop.h"
  17. #include "utils/module_tests.h"
  18. struct printf_test_data {
  19. u8 *data;
  20. size_t len;
  21. char *encoded;
  22. };
  23. static const struct printf_test_data printf_tests[] = {
  24. { (u8 *) "abcde", 5, "abcde" },
  25. { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
  26. { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
  27. { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
  28. { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
  29. "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
  30. { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
  31. "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
  32. { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
  33. "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
  34. { NULL, 0, NULL }
  35. };
  36. static int printf_encode_decode_tests(void)
  37. {
  38. int i;
  39. size_t binlen;
  40. char buf[100];
  41. u8 bin[100];
  42. int errors = 0;
  43. int array[10];
  44. wpa_printf(MSG_INFO, "printf encode/decode tests");
  45. for (i = 0; printf_tests[i].data; i++) {
  46. const struct printf_test_data *test = &printf_tests[i];
  47. printf_encode(buf, sizeof(buf), test->data, test->len);
  48. wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
  49. binlen = printf_decode(bin, sizeof(bin), buf);
  50. if (binlen != test->len ||
  51. os_memcmp(bin, test->data, binlen) != 0) {
  52. wpa_hexdump(MSG_ERROR, "Error in decoding#1",
  53. bin, binlen);
  54. errors++;
  55. }
  56. binlen = printf_decode(bin, sizeof(bin), test->encoded);
  57. if (binlen != test->len ||
  58. os_memcmp(bin, test->data, binlen) != 0) {
  59. wpa_hexdump(MSG_ERROR, "Error in decoding#2",
  60. bin, binlen);
  61. errors++;
  62. }
  63. }
  64. buf[5] = 'A';
  65. printf_encode(buf, 5, (const u8 *) "abcde", 5);
  66. if (buf[5] != 'A') {
  67. wpa_printf(MSG_ERROR, "Error in bounds checking#1");
  68. errors++;
  69. }
  70. for (i = 5; i < 10; i++) {
  71. buf[i] = 'A';
  72. printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
  73. if (buf[i] != 'A') {
  74. wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
  75. i);
  76. errors++;
  77. }
  78. }
  79. if (printf_decode(bin, 3, "abcde") != 2)
  80. errors++;
  81. if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
  82. errors++;
  83. if (printf_decode(bin, 3, "\\xq") != 1 || bin[0] != 'q')
  84. errors++;
  85. if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
  86. errors++;
  87. array[0] = 10;
  88. array[1] = 10;
  89. array[2] = 5;
  90. array[3] = 10;
  91. array[4] = 5;
  92. array[5] = 0;
  93. if (int_array_len(array) != 5)
  94. errors++;
  95. int_array_sort_unique(array);
  96. if (int_array_len(array) != 2)
  97. errors++;
  98. if (errors) {
  99. wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. static int bitfield_tests(void)
  105. {
  106. struct bitfield *bf;
  107. int i;
  108. int errors = 0;
  109. wpa_printf(MSG_INFO, "bitfield tests");
  110. bf = bitfield_alloc(123);
  111. if (bf == NULL)
  112. return -1;
  113. for (i = 0; i < 123; i++) {
  114. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  115. errors++;
  116. if (i > 0 && bitfield_is_set(bf, i - 1))
  117. errors++;
  118. bitfield_set(bf, i);
  119. if (!bitfield_is_set(bf, i))
  120. errors++;
  121. bitfield_clear(bf, i);
  122. if (bitfield_is_set(bf, i))
  123. errors++;
  124. }
  125. for (i = 123; i < 200; i++) {
  126. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  127. errors++;
  128. if (i > 0 && bitfield_is_set(bf, i - 1))
  129. errors++;
  130. bitfield_set(bf, i);
  131. if (bitfield_is_set(bf, i))
  132. errors++;
  133. bitfield_clear(bf, i);
  134. if (bitfield_is_set(bf, i))
  135. errors++;
  136. }
  137. for (i = 0; i < 123; i++) {
  138. if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
  139. errors++;
  140. bitfield_set(bf, i);
  141. if (!bitfield_is_set(bf, i))
  142. errors++;
  143. }
  144. for (i = 0; i < 123; i++) {
  145. if (!bitfield_is_set(bf, i))
  146. errors++;
  147. bitfield_clear(bf, i);
  148. if (bitfield_is_set(bf, i))
  149. errors++;
  150. }
  151. for (i = 0; i < 123; i++) {
  152. if (bitfield_get_first_zero(bf) != i)
  153. errors++;
  154. bitfield_set(bf, i);
  155. }
  156. if (bitfield_get_first_zero(bf) != -1)
  157. errors++;
  158. for (i = 0; i < 123; i++) {
  159. if (!bitfield_is_set(bf, i))
  160. errors++;
  161. bitfield_clear(bf, i);
  162. if (bitfield_get_first_zero(bf) != i)
  163. errors++;
  164. bitfield_set(bf, i);
  165. }
  166. if (bitfield_get_first_zero(bf) != -1)
  167. errors++;
  168. bitfield_free(bf);
  169. bf = bitfield_alloc(8);
  170. if (bf == NULL)
  171. return -1;
  172. if (bitfield_get_first_zero(bf) != 0)
  173. errors++;
  174. for (i = 0; i < 8; i++)
  175. bitfield_set(bf, i);
  176. if (bitfield_get_first_zero(bf) != -1)
  177. errors++;
  178. bitfield_free(bf);
  179. if (errors) {
  180. wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
  181. return -1;
  182. }
  183. return 0;
  184. }
  185. static int int_array_tests(void)
  186. {
  187. int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
  188. int test2[] = { 1, -1, 0 };
  189. int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
  190. int test3_res[] = { -1, 1, 2, 3, 4, 0 };
  191. int errors = 0;
  192. int len;
  193. wpa_printf(MSG_INFO, "int_array tests");
  194. if (int_array_len(test1) != 6 ||
  195. int_array_len(test2) != 2)
  196. errors++;
  197. int_array_sort_unique(test3);
  198. len = int_array_len(test3_res);
  199. if (int_array_len(test3) != len)
  200. errors++;
  201. else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
  202. errors++;
  203. if (errors) {
  204. wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. static int ext_password_tests(void)
  210. {
  211. struct ext_password_data *data;
  212. int ret = 0;
  213. struct wpabuf *pw;
  214. wpa_printf(MSG_INFO, "ext_password tests");
  215. data = ext_password_init("unknown", "foo");
  216. if (data != NULL)
  217. return -1;
  218. data = ext_password_init("test", NULL);
  219. if (data == NULL)
  220. return -1;
  221. pw = ext_password_get(data, "foo");
  222. if (pw != NULL)
  223. ret = -1;
  224. ext_password_free(pw);
  225. ext_password_deinit(data);
  226. pw = ext_password_get(NULL, "foo");
  227. if (pw != NULL)
  228. ret = -1;
  229. ext_password_free(pw);
  230. return ret;
  231. }
  232. static int trace_tests(void)
  233. {
  234. wpa_printf(MSG_INFO, "trace tests");
  235. wpa_trace_show("test backtrace");
  236. wpa_trace_dump_funcname("test funcname", trace_tests);
  237. return 0;
  238. }
  239. static int base64_tests(void)
  240. {
  241. int errors = 0;
  242. unsigned char *res;
  243. size_t res_len;
  244. wpa_printf(MSG_INFO, "base64 tests");
  245. res = base64_encode((const unsigned char *) "", ~0, &res_len);
  246. if (res) {
  247. errors++;
  248. os_free(res);
  249. }
  250. res = base64_encode((const unsigned char *) "=", 1, &res_len);
  251. if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
  252. res[2] != '=' || res[3] != '=' || res[4] != '\n')
  253. errors++;
  254. os_free(res);
  255. res = base64_encode((const unsigned char *) "=", 1, NULL);
  256. if (!res || res[0] != 'P' || res[1] != 'Q' ||
  257. res[2] != '=' || res[3] != '=' || res[4] != '\n')
  258. errors++;
  259. os_free(res);
  260. res = base64_decode((const unsigned char *) "", 0, &res_len);
  261. if (res) {
  262. errors++;
  263. os_free(res);
  264. }
  265. res = base64_decode((const unsigned char *) "a", 1, &res_len);
  266. if (res) {
  267. errors++;
  268. os_free(res);
  269. }
  270. res = base64_decode((const unsigned char *) "====", 4, &res_len);
  271. if (res) {
  272. errors++;
  273. os_free(res);
  274. }
  275. res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
  276. if (!res || res_len != 1 || res[0] != '=')
  277. errors++;
  278. os_free(res);
  279. res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
  280. if (!res || res_len != 1 || res[0] != '=')
  281. errors++;
  282. os_free(res);
  283. if (errors) {
  284. wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
  285. return -1;
  286. }
  287. return 0;
  288. }
  289. static int common_tests(void)
  290. {
  291. char buf[3], longbuf[100];
  292. u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
  293. u8 bin[3];
  294. int errors = 0;
  295. struct wpa_freq_range_list ranges;
  296. size_t len;
  297. const char *txt;
  298. u8 ssid[255];
  299. wpa_printf(MSG_INFO, "common tests");
  300. if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
  301. errors++;
  302. if (wpa_scnprintf(buf, 0, "hello") != 0 ||
  303. wpa_scnprintf(buf, 3, "hello") != 2)
  304. errors++;
  305. if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
  306. wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
  307. errors++;
  308. if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
  309. merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
  310. errors++;
  311. if (dup_binstr(NULL, 0) != NULL)
  312. errors++;
  313. if (freq_range_list_includes(NULL, 0) != 0)
  314. errors++;
  315. os_memset(&ranges, 0, sizeof(ranges));
  316. if (freq_range_list_parse(&ranges, "") != 0 ||
  317. freq_range_list_includes(&ranges, 0) != 0 ||
  318. freq_range_list_str(&ranges) != NULL)
  319. errors++;
  320. if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
  321. utf8_unescape("a", 1, NULL, 0) != 0 ||
  322. utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
  323. utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
  324. utf8_unescape("abc", 3, buf, 3) != 3)
  325. errors++;
  326. if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
  327. errors++;
  328. if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
  329. errors++;
  330. if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
  331. utf8_escape("a", 1, NULL, 0) != 0 ||
  332. utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
  333. utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
  334. utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
  335. utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
  336. utf8_escape("abc", 3, buf, 3) != 3)
  337. errors++;
  338. if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
  339. errors++;
  340. os_memset(ssid, 0, sizeof(ssid));
  341. txt = wpa_ssid_txt(ssid, sizeof(ssid));
  342. len = os_strlen(txt);
  343. /* Verify that SSID_MAX_LEN * 4 buffer limit is enforced. */
  344. if (len != SSID_MAX_LEN * 4) {
  345. wpa_printf(MSG_ERROR,
  346. "Unexpected wpa_ssid_txt() result with too long SSID");
  347. errors++;
  348. }
  349. if (wpa_snprintf_hex_sep(longbuf, 0, addr, ETH_ALEN, '-') != 0 ||
  350. wpa_snprintf_hex_sep(longbuf, 5, addr, ETH_ALEN, '-') != 3 ||
  351. os_strcmp(longbuf, "01-0") != 0)
  352. errors++;
  353. if (errors) {
  354. wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
  355. return -1;
  356. }
  357. return 0;
  358. }
  359. static int os_tests(void)
  360. {
  361. int errors = 0;
  362. void *ptr;
  363. os_time_t t;
  364. wpa_printf(MSG_INFO, "os tests");
  365. ptr = os_calloc((size_t) -1, (size_t) -1);
  366. if (ptr) {
  367. errors++;
  368. os_free(ptr);
  369. }
  370. ptr = os_calloc((size_t) 2, (size_t) -1);
  371. if (ptr) {
  372. errors++;
  373. os_free(ptr);
  374. }
  375. ptr = os_calloc((size_t) -1, (size_t) 2);
  376. if (ptr) {
  377. errors++;
  378. os_free(ptr);
  379. }
  380. ptr = os_realloc_array(NULL, (size_t) -1, (size_t) -1);
  381. if (ptr) {
  382. errors++;
  383. os_free(ptr);
  384. }
  385. os_sleep(1, 1);
  386. if (os_mktime(1969, 1, 1, 1, 1, 1, &t) == 0 ||
  387. os_mktime(1971, 0, 1, 1, 1, 1, &t) == 0 ||
  388. os_mktime(1971, 13, 1, 1, 1, 1, &t) == 0 ||
  389. os_mktime(1971, 1, 0, 1, 1, 1, &t) == 0 ||
  390. os_mktime(1971, 1, 32, 1, 1, 1, &t) == 0 ||
  391. os_mktime(1971, 1, 1, -1, 1, 1, &t) == 0 ||
  392. os_mktime(1971, 1, 1, 24, 1, 1, &t) == 0 ||
  393. os_mktime(1971, 1, 1, 1, -1, 1, &t) == 0 ||
  394. os_mktime(1971, 1, 1, 1, 60, 1, &t) == 0 ||
  395. os_mktime(1971, 1, 1, 1, 1, -1, &t) == 0 ||
  396. os_mktime(1971, 1, 1, 1, 1, 61, &t) == 0 ||
  397. os_mktime(1971, 1, 1, 1, 1, 1, &t) != 0 ||
  398. os_mktime(2020, 1, 2, 3, 4, 5, &t) != 0 ||
  399. os_mktime(2015, 12, 31, 23, 59, 59, &t) != 0)
  400. errors++;
  401. if (os_setenv("hwsim_test_env", "test value", 0) != 0 ||
  402. os_setenv("hwsim_test_env", "test value 2", 1) != 0 ||
  403. os_unsetenv("hwsim_test_env") != 0)
  404. errors++;
  405. if (os_file_exists("/this-file-does-not-exists-hwsim") != 0)
  406. errors++;
  407. if (errors) {
  408. wpa_printf(MSG_ERROR, "%d os test(s) failed", errors);
  409. return -1;
  410. }
  411. return 0;
  412. }
  413. static int wpabuf_tests(void)
  414. {
  415. int errors = 0;
  416. void *ptr;
  417. struct wpabuf *buf;
  418. wpa_printf(MSG_INFO, "wpabuf tests");
  419. ptr = os_malloc(100);
  420. if (ptr) {
  421. buf = wpabuf_alloc_ext_data(ptr, 100);
  422. if (buf) {
  423. if (wpabuf_resize(&buf, 100) < 0)
  424. errors++;
  425. else
  426. wpabuf_put(buf, 100);
  427. wpabuf_free(buf);
  428. } else {
  429. errors++;
  430. os_free(ptr);
  431. }
  432. } else {
  433. errors++;
  434. }
  435. buf = wpabuf_alloc(100);
  436. if (buf) {
  437. struct wpabuf *buf2;
  438. wpabuf_put(buf, 100);
  439. if (wpabuf_resize(&buf, 100) < 0)
  440. errors++;
  441. else
  442. wpabuf_put(buf, 100);
  443. buf2 = wpabuf_concat(buf, NULL);
  444. if (buf2 != buf)
  445. errors++;
  446. wpabuf_free(buf2);
  447. } else {
  448. errors++;
  449. }
  450. buf = NULL;
  451. buf = wpabuf_zeropad(buf, 10);
  452. if (buf != NULL)
  453. errors++;
  454. if (errors) {
  455. wpa_printf(MSG_ERROR, "%d wpabuf test(s) failed", errors);
  456. return -1;
  457. }
  458. return 0;
  459. }
  460. static int ip_addr_tests(void)
  461. {
  462. int errors = 0;
  463. struct hostapd_ip_addr addr;
  464. char buf[100];
  465. wpa_printf(MSG_INFO, "ip_addr tests");
  466. if (hostapd_parse_ip_addr("1.2.3.4", &addr) != 0 ||
  467. addr.af != AF_INET ||
  468. hostapd_ip_txt(NULL, buf, sizeof(buf)) != NULL ||
  469. hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
  470. hostapd_ip_txt(&addr, buf, 0) != NULL ||
  471. hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
  472. errors++;
  473. if (hostapd_parse_ip_addr("::", &addr) != 0 ||
  474. addr.af != AF_INET6 ||
  475. hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
  476. hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
  477. errors++;
  478. if (errors) {
  479. wpa_printf(MSG_ERROR, "%d ip_addr test(s) failed", errors);
  480. return -1;
  481. }
  482. return 0;
  483. }
  484. struct test_eloop {
  485. unsigned int magic;
  486. int close_in_timeout;
  487. int pipefd1[2];
  488. int pipefd2[2];
  489. };
  490. static void eloop_tests_start(int close_in_timeout);
  491. static void eloop_test_read_2(int sock, void *eloop_ctx, void *sock_ctx)
  492. {
  493. struct test_eloop *t = eloop_ctx;
  494. ssize_t res;
  495. char buf[10];
  496. wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
  497. if (t->magic != 0x12345678) {
  498. wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
  499. __func__, t->magic);
  500. }
  501. if (t->pipefd2[0] != sock) {
  502. wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
  503. __func__, sock, t->pipefd2[0]);
  504. }
  505. res = read(sock, buf, sizeof(buf));
  506. wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
  507. __func__, sock, (int) res);
  508. }
  509. static void eloop_test_read_2_wrong(int sock, void *eloop_ctx, void *sock_ctx)
  510. {
  511. struct test_eloop *t = eloop_ctx;
  512. wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
  513. if (t->magic != 0x12345678) {
  514. wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
  515. __func__, t->magic);
  516. }
  517. if (t->pipefd2[0] != sock) {
  518. wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
  519. __func__, sock, t->pipefd2[0]);
  520. }
  521. /*
  522. * This is expected to block due to the original socket with data having
  523. * been closed and no new data having been written to the new socket
  524. * with the same fd. To avoid blocking the process during test, skip the
  525. * read here.
  526. */
  527. wpa_printf(MSG_ERROR, "%s: FAIL - should not have called this function",
  528. __func__);
  529. }
  530. static void reopen_pipefd2(struct test_eloop *t)
  531. {
  532. if (t->pipefd2[0] < 0) {
  533. wpa_printf(MSG_INFO, "pipefd2 had been closed");
  534. } else {
  535. int res;
  536. wpa_printf(MSG_INFO, "close pipefd2");
  537. eloop_unregister_read_sock(t->pipefd2[0]);
  538. close(t->pipefd2[0]);
  539. t->pipefd2[0] = -1;
  540. close(t->pipefd2[1]);
  541. t->pipefd2[1] = -1;
  542. res = pipe(t->pipefd2);
  543. if (res < 0) {
  544. wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
  545. t->pipefd2[0] = -1;
  546. t->pipefd2[1] = -1;
  547. return;
  548. }
  549. wpa_printf(MSG_INFO,
  550. "re-register pipefd2 with new sockets %d,%d",
  551. t->pipefd2[0], t->pipefd2[1]);
  552. eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2_wrong,
  553. t, NULL);
  554. }
  555. }
  556. static void eloop_test_read_1(int sock, void *eloop_ctx, void *sock_ctx)
  557. {
  558. struct test_eloop *t = eloop_ctx;
  559. ssize_t res;
  560. char buf[10];
  561. wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
  562. if (t->magic != 0x12345678) {
  563. wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
  564. __func__, t->magic);
  565. }
  566. if (t->pipefd1[0] != sock) {
  567. wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
  568. __func__, sock, t->pipefd1[0]);
  569. }
  570. res = read(sock, buf, sizeof(buf));
  571. wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
  572. __func__, sock, (int) res);
  573. if (!t->close_in_timeout)
  574. reopen_pipefd2(t);
  575. }
  576. static void eloop_test_cb(void *eloop_data, void *user_ctx)
  577. {
  578. struct test_eloop *t = eloop_data;
  579. wpa_printf(MSG_INFO, "%s", __func__);
  580. if (t->magic != 0x12345678) {
  581. wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
  582. __func__, t->magic);
  583. }
  584. if (t->close_in_timeout)
  585. reopen_pipefd2(t);
  586. }
  587. static void eloop_test_timeout(void *eloop_data, void *user_ctx)
  588. {
  589. struct test_eloop *t = eloop_data;
  590. int next_run = 0;
  591. wpa_printf(MSG_INFO, "%s", __func__);
  592. if (t->magic != 0x12345678) {
  593. wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
  594. __func__, t->magic);
  595. }
  596. if (t->pipefd1[0] >= 0) {
  597. wpa_printf(MSG_INFO, "pipefd1 had not been closed");
  598. eloop_unregister_read_sock(t->pipefd1[0]);
  599. close(t->pipefd1[0]);
  600. t->pipefd1[0] = -1;
  601. close(t->pipefd1[1]);
  602. t->pipefd1[1] = -1;
  603. }
  604. if (t->pipefd2[0] >= 0) {
  605. wpa_printf(MSG_INFO, "pipefd2 had not been closed");
  606. eloop_unregister_read_sock(t->pipefd2[0]);
  607. close(t->pipefd2[0]);
  608. t->pipefd2[0] = -1;
  609. close(t->pipefd2[1]);
  610. t->pipefd2[1] = -1;
  611. }
  612. next_run = t->close_in_timeout;
  613. t->magic = 0;
  614. wpa_printf(MSG_INFO, "%s - free(%p)", __func__, t);
  615. os_free(t);
  616. if (next_run)
  617. eloop_tests_start(0);
  618. }
  619. static void eloop_tests_start(int close_in_timeout)
  620. {
  621. struct test_eloop *t;
  622. int res;
  623. t = os_zalloc(sizeof(*t));
  624. if (!t)
  625. return;
  626. t->magic = 0x12345678;
  627. t->close_in_timeout = close_in_timeout;
  628. wpa_printf(MSG_INFO, "starting eloop tests (%p) (close_in_timeout=%d)",
  629. t, close_in_timeout);
  630. res = pipe(t->pipefd1);
  631. if (res < 0) {
  632. wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
  633. os_free(t);
  634. return;
  635. }
  636. res = pipe(t->pipefd2);
  637. if (res < 0) {
  638. wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
  639. close(t->pipefd1[0]);
  640. close(t->pipefd1[1]);
  641. os_free(t);
  642. return;
  643. }
  644. wpa_printf(MSG_INFO, "pipe fds: %d,%d %d,%d",
  645. t->pipefd1[0], t->pipefd1[1],
  646. t->pipefd2[0], t->pipefd2[1]);
  647. eloop_register_read_sock(t->pipefd1[0], eloop_test_read_1, t, NULL);
  648. eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2, t, NULL);
  649. eloop_register_timeout(0, 0, eloop_test_cb, t, NULL);
  650. eloop_register_timeout(0, 200000, eloop_test_timeout, t, NULL);
  651. if (write(t->pipefd1[1], "HELLO", 5) < 0)
  652. wpa_printf(MSG_INFO, "write: %s", strerror(errno));
  653. if (write(t->pipefd2[1], "TEST", 4) < 0)
  654. wpa_printf(MSG_INFO, "write: %s", strerror(errno));
  655. os_sleep(0, 50000);
  656. wpa_printf(MSG_INFO, "waiting for eloop callbacks");
  657. }
  658. static void eloop_tests_run(void *eloop_data, void *user_ctx)
  659. {
  660. eloop_tests_start(1);
  661. }
  662. static int eloop_tests(void)
  663. {
  664. wpa_printf(MSG_INFO, "schedule eloop tests to be run");
  665. /*
  666. * Cannot return error from these without a significant design change,
  667. * so for now, run the tests from a scheduled timeout and require
  668. * separate verification of the results from the debug log.
  669. */
  670. eloop_register_timeout(0, 0, eloop_tests_run, NULL, NULL);
  671. return 0;
  672. }
  673. int utils_module_tests(void)
  674. {
  675. int ret = 0;
  676. wpa_printf(MSG_INFO, "utils module tests");
  677. if (printf_encode_decode_tests() < 0 ||
  678. ext_password_tests() < 0 ||
  679. trace_tests() < 0 ||
  680. bitfield_tests() < 0 ||
  681. base64_tests() < 0 ||
  682. common_tests() < 0 ||
  683. os_tests() < 0 ||
  684. wpabuf_tests() < 0 ||
  685. ip_addr_tests() < 0 ||
  686. eloop_tests() < 0 ||
  687. int_array_tests() < 0)
  688. ret = -1;
  689. return ret;
  690. }