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