common_module_tests.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * common 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 "ieee802_11_common.h"
  11. #include "ieee802_11_defs.h"
  12. #include "gas.h"
  13. #include "wpa_common.h"
  14. struct ieee802_11_parse_test_data {
  15. u8 *data;
  16. size_t len;
  17. ParseRes result;
  18. int count;
  19. };
  20. static const struct ieee802_11_parse_test_data parse_tests[] = {
  21. { (u8 *) "", 0, ParseOK, 0 },
  22. { (u8 *) " ", 1, ParseFailed, 0 },
  23. { (u8 *) "\xff\x00", 2, ParseUnknown, 1 },
  24. { (u8 *) "\xff\x01", 2, ParseFailed, 0 },
  25. { (u8 *) "\xdd\x03\x01\x02\x03", 5, ParseUnknown, 1 },
  26. { (u8 *) "\xdd\x04\x01\x02\x03\x04", 6, ParseUnknown, 1 },
  27. { (u8 *) "\xdd\x04\x00\x50\xf2\x02", 6, ParseUnknown, 1 },
  28. { (u8 *) "\xdd\x05\x00\x50\xf2\x02\x02", 7, ParseOK, 1 },
  29. { (u8 *) "\xdd\x05\x00\x50\xf2\x02\xff", 7, ParseUnknown, 1 },
  30. { (u8 *) "\xdd\x04\x00\x50\xf2\xff", 6, ParseUnknown, 1 },
  31. { (u8 *) "\xdd\x04\x50\x6f\x9a\xff", 6, ParseUnknown, 1 },
  32. { (u8 *) "\xdd\x04\x00\x90\x4c\x33", 6, ParseOK, 1 },
  33. { (u8 *) "\xdd\x04\x00\x90\x4c\xff\xdd\x04\x00\x90\x4c\x33", 12,
  34. ParseUnknown, 2 },
  35. { (u8 *) "\x10\x01\x00\x21\x00", 5, ParseOK, 2 },
  36. { (u8 *) "\x24\x00", 2, ParseOK, 1 },
  37. { (u8 *) "\x38\x00", 2, ParseOK, 1 },
  38. { (u8 *) "\x54\x00", 2, ParseOK, 1 },
  39. { (u8 *) "\x5a\x00", 2, ParseOK, 1 },
  40. { (u8 *) "\x65\x00", 2, ParseOK, 1 },
  41. { (u8 *) "\x65\x12\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11",
  42. 20, ParseOK, 1 },
  43. { (u8 *) "\x6e\x00", 2, ParseOK, 1 },
  44. { (u8 *) "\xc7\x00", 2, ParseOK, 1 },
  45. { (u8 *) "\xc7\x01\x00", 3, ParseOK, 1 },
  46. { (u8 *) "\x03\x00\x2a\x00\x36\x00\x37\x00\x38\x00\x2d\x00\x3d\x00\xbf\x00\xc0\x00",
  47. 18, ParseOK, 9 },
  48. { (u8 *) "\x8b\x00", 2, ParseOK, 1 },
  49. { (u8 *) "\xdd\x04\x00\x90\x4c\x04", 6, ParseUnknown, 1 },
  50. { NULL, 0, ParseOK, 0 }
  51. };
  52. static int ieee802_11_parse_tests(void)
  53. {
  54. int i, ret = 0;
  55. wpa_printf(MSG_INFO, "ieee802_11_parse tests");
  56. for (i = 0; parse_tests[i].data; i++) {
  57. const struct ieee802_11_parse_test_data *test;
  58. struct ieee802_11_elems elems;
  59. ParseRes res;
  60. test = &parse_tests[i];
  61. res = ieee802_11_parse_elems(test->data, test->len, &elems, 1);
  62. if (res != test->result ||
  63. ieee802_11_ie_count(test->data, test->len) != test->count) {
  64. wpa_printf(MSG_ERROR, "ieee802_11_parse test %d failed",
  65. i);
  66. ret = -1;
  67. }
  68. }
  69. if (ieee802_11_vendor_ie_concat((const u8 *) "\x00\x01", 2, 0) != NULL)
  70. {
  71. wpa_printf(MSG_ERROR,
  72. "ieee802_11_vendor_ie_concat test failed");
  73. ret = -1;
  74. }
  75. return ret;
  76. }
  77. struct rsn_ie_parse_test_data {
  78. u8 *data;
  79. size_t len;
  80. int result;
  81. };
  82. static const struct rsn_ie_parse_test_data rsn_parse_tests[] = {
  83. { (u8 *) "", 0, -1 },
  84. { (u8 *) "\x30\x00", 2, -1 },
  85. { (u8 *) "\x30\x02\x01\x00", 4, 0 },
  86. { (u8 *) "\x30\x02\x00\x00", 4, -2 },
  87. { (u8 *) "\x30\x02\x02\x00", 4, -2 },
  88. { (u8 *) "\x30\x02\x00\x01", 4, -2 },
  89. { (u8 *) "\x30\x02\x00\x00\x00", 5, -2 },
  90. { (u8 *) "\x30\x03\x01\x00\x00", 5, -3 },
  91. { (u8 *) "\x30\x06\x01\x00\x00\x00\x00\x00", 8, -1 },
  92. { (u8 *) "\x30\x06\x01\x00\x00\x0f\xac\x04", 8, 0 },
  93. { (u8 *) "\x30\x07\x01\x00\x00\x0f\xac\x04\x00", 9, -5 },
  94. { (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x00", 10, -4 },
  95. { (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x01", 10, -4 },
  96. { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04",
  97. 14, 0 },
  98. { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x00\x01\x00\x0f\xac\x04",
  99. 14, -4 },
  100. { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x06",
  101. 14, -1 },
  102. { (u8 *) "\x30\x10\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x08",
  103. 18, 0 },
  104. { (u8 *) "\x30\x0d\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00",
  105. 15, -7 },
  106. { (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x00",
  107. 16, -6 },
  108. { (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x01",
  109. 16, -6 },
  110. { (u8 *) "\x30\x12\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01",
  111. 20, 0 },
  112. { (u8 *) "\x30\x16\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x01\x00\x0f\xac\x02",
  113. 24, 0 },
  114. { (u8 *) "\x30\x13\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00",
  115. 21, 0 },
  116. { (u8 *) "\x30\x14\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00",
  117. 22, 0 },
  118. { (u8 *) "\x30\x16\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x00",
  119. 24, 0 },
  120. { (u8 *) "\x30\x16\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x01",
  121. 24, -9 },
  122. { (u8 *) "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x00\x00\x00\x00\x00",
  123. 28, -10 },
  124. { (u8 *) "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x00\x00\x0f\xac\x06",
  125. 28, 0 },
  126. { (u8 *) "\x30\x1c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x00\x00\x0f\xac\x06\x01\x02",
  127. 30, 0 },
  128. { NULL, 0, 0 }
  129. };
  130. static int rsn_ie_parse_tests(void)
  131. {
  132. int i, ret = 0;
  133. wpa_printf(MSG_INFO, "rsn_ie_parse tests");
  134. for (i = 0; rsn_parse_tests[i].data; i++) {
  135. const struct rsn_ie_parse_test_data *test;
  136. struct wpa_ie_data data;
  137. test = &rsn_parse_tests[i];
  138. if (wpa_parse_wpa_ie_rsn(test->data, test->len, &data) !=
  139. test->result) {
  140. wpa_printf(MSG_ERROR, "rsn_ie_parse test %d failed", i);
  141. ret = -1;
  142. }
  143. }
  144. return ret;
  145. }
  146. static int gas_tests(void)
  147. {
  148. struct wpabuf *buf;
  149. wpa_printf(MSG_INFO, "gas tests");
  150. gas_anqp_set_len(NULL);
  151. buf = wpabuf_alloc(1);
  152. if (buf == NULL)
  153. return -1;
  154. gas_anqp_set_len(buf);
  155. wpabuf_free(buf);
  156. buf = wpabuf_alloc(20);
  157. if (buf == NULL)
  158. return -1;
  159. wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
  160. wpabuf_put_u8(buf, WLAN_PA_GAS_INITIAL_REQ);
  161. wpabuf_put_u8(buf, 0);
  162. wpabuf_put_be32(buf, 0);
  163. wpabuf_put_u8(buf, 0);
  164. gas_anqp_set_len(buf);
  165. wpabuf_free(buf);
  166. return 0;
  167. }
  168. int common_module_tests(void)
  169. {
  170. int ret = 0;
  171. wpa_printf(MSG_INFO, "common module tests");
  172. if (ieee802_11_parse_tests() < 0 ||
  173. gas_tests() < 0 ||
  174. rsn_ie_parse_tests() < 0)
  175. ret = -1;
  176. return ret;
  177. }