test-ie.c (24608B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <assert.h> 30 #include <stdlib.h> 31 #include <errno.h> 32 #include <ell/ell.h> 33 34 #include "src/ie.h" 35 36 struct test_data { 37 unsigned int num_ie; 38 unsigned int len; 39 const unsigned char *buf; 40 }; 41 42 struct ie { 43 unsigned char type; 44 unsigned char len; 45 unsigned char value[]; 46 } __attribute__ ((packed)); 47 48 static void ie_test_reader(const void *data) 49 { 50 const struct test_data *test = data; 51 struct ie_tlv_iter iter; 52 struct ie *ie; 53 unsigned int count = 0, pos = 0; 54 char *str; 55 56 ie_tlv_iter_init(&iter, test->buf, test->len); 57 58 while (ie_tlv_iter_next(&iter)) { 59 ie = (struct ie *)&test->buf[pos]; 60 str = l_util_hexstring(&test->buf[pos + 2], ie->len); 61 printf("IE %d [%d/%d/%s]\n", count, ie->type, ie->len, str); 62 l_free(str); 63 64 assert(iter.tag == test->buf[pos++]); 65 assert(iter.len == test->buf[pos++]); 66 assert(!memcmp(iter.data, test->buf + pos, iter.len)); 67 pos += ie->len; 68 69 count++; 70 } 71 72 assert(count == test->num_ie); 73 } 74 75 static const unsigned char beacon_frame[] = { 76 /* IEEE 802.11 Beacon frame */ 77 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 78 0xff, 0xff, 0xc8, 0xd7, 0x19, 0x39, 0xbe, 0x77, 79 0xc8, 0xd7, 0x19, 0x39, 0xbe, 0x77, 0x50, 0xa2, 80 81 /* IEEE 802.11 wireless LAN management frame 82 * - Fixed parameters (12 bytes) 83 */ 84 0x87, 0x81, 0x31, 0xe6, 0x29, 0x02, 0x00, 0x00, 85 0x64, 0x00, 0x11, 0x00, 86 87 /* - Tagged parameters (TLV format, 252 bytes). 88 * This starts at byte position 36 89 */ 90 0x00, 0x0c, 0x57, 0x65, 0x73, 0x31, 0x4f, 0x70, 91 0x65, 0x6e, 0x57, 0x4c, 0x41, 0x4e, 0x01, 0x08, 92 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c, 93 0x05, 0x04, 0x00, 0x01, 0x00, 0x00, 0x30, 0x14, 94 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 95 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 96 0xac, 0x02, 0x0c, 0x00, 0x0b, 0x05, 0x02, 0x00, 97 0x02, 0x00, 0x00, 0x2d, 0x1a, 0x6f, 0x08, 0x17, 98 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 101 0x16, 0x24, 0x0d, 0x16, 0x00, 0x00, 0x00, 0x00, 102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 104 0x0e, 0x14, 0x00, 0x0a, 0x00, 0x2c, 0x01, 0xc8, 105 0x00, 0x14, 0x00, 0x05, 0x00, 0x19, 0x00, 0x7f, 106 0x08, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 107 0x40, 0xbf, 0x0c, 0x32, 0x58, 0x82, 0x0f, 0xea, 108 0xff, 0x00, 0x00, 0xea, 0xff, 0x00, 0x00, 0xc0, 109 0x05, 0x01, 0x2a, 0x00, 0x00, 0x00, 0xc3, 0x04, 110 0x02, 0x02, 0x02, 0x02, 0xdd, 0x31, 0x00, 0x50, 111 0xf2, 0x04, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10, 112 0x44, 0x00, 0x01, 0x02, 0x10, 0x47, 0x00, 0x10, 113 0x98, 0x42, 0x13, 0x05, 0x23, 0x6e, 0xde, 0x3a, 114 0xfa, 0x13, 0x0a, 0x79, 0x44, 0x0f, 0xab, 0x43, 115 0x10, 0x3c, 0x00, 0x01, 0x03, 0x10, 0x49, 0x00, 116 0x06, 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20, 0xdd, 117 0x09, 0x00, 0x10, 0x18, 0x02, 0x02, 0x00, 0x1c, 118 0x00, 0x00, 0xdd, 0x18, 0x00, 0x50, 0xf2, 0x02, 119 0x01, 0x01, 0x80, 0x00, 0x03, 0xa4, 0x00, 0x00, 120 0x27, 0xa4, 0x00, 0x00, 0x42, 0x43, 0x5e, 0x00, 121 0x62, 0x32, 0x2f, 0x00, 0x01, 0x9a, 0xc1, 0xc8, 122 }; 123 124 static struct test_data beacon_frame_data = { 125 .buf = beacon_frame + 36, 126 .num_ie = 15, 127 .len = 252, 128 }; 129 130 static int create_ie(int ie_count, struct ie_tlv_builder *builder, 131 int type, int len, unsigned char *value) 132 { 133 int total_len = 0; 134 char *str; 135 136 str = l_util_hexstring(value, len); 137 printf("IE %d [%d/%d/%s]\n", ie_count, type, len, str); 138 l_free(str); 139 assert(ie_tlv_builder_next(builder, type)); 140 total_len += 1; 141 assert(ie_tlv_builder_set_length(builder, len)); 142 total_len += 1; 143 memcpy(ie_tlv_builder_get_data(builder), value, len); 144 total_len += len; 145 146 return total_len; 147 } 148 149 #define ie(type, len, value...) \ 150 do { \ 151 unsigned char buf[] = { value }; \ 152 final_len += create_ie(ie_count, &builder, type, len, buf); \ 153 ie_count++; \ 154 } while (0) 155 156 static void ie_test_writer(const void *data) 157 { 158 struct test_data *test = (struct test_data *)data; 159 size_t expected_len = test->len; 160 struct ie_tlv_builder builder; 161 size_t final_len = 0; 162 size_t builder_len; 163 const unsigned char *expected_buf = test->buf; 164 unsigned int ie_count = 0; 165 char *str; 166 167 assert(ie_tlv_builder_init(&builder, NULL, 0)); 168 169 test->buf = builder.buf; 170 test->len = builder.max; 171 172 ie(IE_TYPE_SSID, 0x0c, 173 0x57, 0x65, 0x73, 0x31, 0x4f, 0x70, 0x65, 0x6e, 174 0x57, 0x4c, 0x41, 0x4e); 175 ie(IE_TYPE_SUPPORTED_RATES, 0x08, 176 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c); 177 ie(IE_TYPE_TIM, 0x04, 0x00, 0x01, 0x00, 0x00); 178 ie(IE_TYPE_RSN, 0x14, 179 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 180 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 181 0xac, 0x02, 0x0c, 0x00); 182 ie(IE_TYPE_BSS_LOAD, 0x05, 0x02, 0x00, 0x02, 0x00, 0x00); 183 ie(IE_TYPE_HT_CAPABILITIES, 0x1a, 184 0x6f, 0x08, 0x17, 0xff, 0xff, 0xff, 0x00, 0x00, 185 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 186 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 187 0x00, 0x00); 188 ie(IE_TYPE_HT_OPERATION, 0x16, 189 0x24, 0x0d, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 192 ie(IE_TYPE_OVERLAPPING_BSS_SCAN_PARAMETERS, 0x0e, 193 0x14, 0x00, 0x0a, 0x00, 0x2c, 0x01, 0xc8, 0x00, 194 0x14, 0x00, 0x05, 0x00, 0x19, 0x00); 195 ie(IE_TYPE_EXTENDED_CAPABILITIES, 0x08, 196 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x40); 197 ie(191 /* IEEE 802.11ac/D3.1 VHT Capabilities */, 0x0c, 198 0x32, 0x58, 0x82, 0x0f, 0xea, 0xff, 0x00, 0x00, 199 0xea, 0xff, 0x00, 0x00); 200 ie(192 /* IEEE 802.11ac/D3.1 VHT Operation */, 0x05, 201 0x01, 0x2a, 0x00, 0x00, 0x00); 202 ie(195 /* unknown */, 0x04, 0x02, 0x02, 0x02, 0x02); 203 ie(IE_TYPE_VENDOR_SPECIFIC, 0x31, 204 0x00, 0x50, 0xf2, /* OUI (Microsoft) */ 205 0x04, /* WPS type */ 206 0x10, 0x4a, 0x00, 0x01, 0x10, /* version */ 207 0x10, 0x44, 0x00, 0x01, 0x02, /* wps state */ 208 0x10, 0x47, /* UUID E */ 209 0x00, 0x10, /* len */ 210 0x98, 0x42, 0x13, 0x05, 0x23, 0x6e, 0xde, 0x3a, 211 0xfa, 0x13, 0x0a, 0x79, 0x44, 0x0f, 0xab, 0x43, /* enrollee */ 212 0x10, 0x3c, 0x00, 0x01, 0x03, /* RF bands: 2.5 & 5 GHz */ 213 0x10, 0x49, 0x00, 0x06, 0x00, 0x37, 0x2a, 0x00, 214 0x01, 0x20 /* vendor extension */ ); 215 ie(IE_TYPE_VENDOR_SPECIFIC, 0x09, 216 0x00, 0x10, 0x18, /* OUI (Broadcom) */ 217 0x02, /* type */ 218 0x02, 0x00, 0x1c, 0x00, 0x00 /* data */ ); 219 ie(IE_TYPE_VENDOR_SPECIFIC, 0x18, 220 0x00, 0x50, 0xf2, /* OUI (Microsoft) */ 221 0x02, /* WMM/WME type */ 222 0x01, 0x01, 0x80, 0x00, 0x03, 0xa4, 0x00, 0x00, 223 0x27, 0xa4, 0x00, 0x00, 0x42, 0x43, 0x5e, 0x00, 224 0x62, 0x32, 0x2f, 0x00, 0x01, 0x9a, 0xc1, 0xc8 /* data */ ); 225 226 ie_tlv_builder_finalize(&builder, &builder_len); 227 228 assert(final_len == builder_len); 229 assert(expected_len == final_len); 230 assert(ie_count == test->num_ie); 231 232 str = l_util_hexstring(test->buf, final_len); 233 printf("IE buf %s\n", str); 234 l_free(str); 235 236 if (memcmp(test->buf, expected_buf, final_len)) { 237 unsigned int i; 238 239 str = l_util_hexstring(&beacon_frame[36], final_len); 240 printf("Expecting buf %s\n", str); 241 l_free(str); 242 243 for (i = 0; i < final_len; i++) { 244 if (expected_buf[i] != test->buf[i]) { 245 printf("1st difference at pos %d " 246 "expecting 0x%02x got 0x%02x\n", 247 i, expected_buf[i], test->buf[i]); 248 break; 249 } 250 } 251 252 assert(!memcmp(test->buf, expected_buf, final_len)); 253 } 254 255 printf("Wrote %d IE total len %zu\n", ie_count, expected_len); 256 } 257 258 static void ie_test_writer_invalid_tag(const void *data) 259 { 260 struct ie_tlv_builder builder; 261 262 assert(ie_tlv_builder_init(&builder, NULL, 0)); 263 assert(!ie_tlv_builder_next(&builder, 512)); 264 } 265 266 static void ie_test_writer_invalid_len(const void *data) 267 { 268 struct ie_tlv_builder builder; 269 270 assert(ie_tlv_builder_init(&builder, NULL, 0)); 271 assert(ie_tlv_builder_next(&builder, 255)); 272 assert(!ie_tlv_builder_set_length(&builder, MAX_BUILDER_SIZE)); 273 } 274 275 static void ie_test_reader_extended(const void *data) 276 { 277 struct ie_tlv_iter iter; 278 static const uint8_t test_buf[] = { 279 0xff, 0x05, 0x0a, 0xff, 0x01, 0x02, 0x03, 280 }; 281 282 ie_tlv_iter_init(&iter, test_buf, L_ARRAY_SIZE(test_buf)); 283 284 assert(ie_tlv_iter_next(&iter)); 285 286 assert(ie_tlv_iter_get_tag(&iter) == IE_TYPE_EXTENDED_REQUEST); 287 assert(ie_tlv_iter_get_length(&iter) == 4); 288 assert(ie_tlv_iter_get_data(&iter) == test_buf + 3); 289 290 assert(!ie_tlv_iter_next(&iter)); 291 } 292 293 static void ie_test_writer_extended(const void *data) 294 { 295 struct ie_tlv_builder builder; 296 size_t builder_len; 297 static const uint8_t expected[] = { 298 0xff, 0x05, 0x0a, 0xff, 0x01, 0x02, 0x03, 299 }; 300 301 assert(ie_tlv_builder_init(&builder, NULL, 0)); 302 303 assert(ie_tlv_builder_next(&builder, IE_TYPE_EXTENDED_REQUEST)); 304 assert(ie_tlv_builder_set_length(&builder, 4)); 305 306 memcpy(ie_tlv_builder_get_data(&builder), expected + 3, 4); 307 308 ie_tlv_builder_finalize(&builder, &builder_len); 309 310 assert(builder_len == L_ARRAY_SIZE(expected)); 311 assert(!memcmp(builder.buf, expected, builder_len)); 312 } 313 314 struct ie_rsne_info_test { 315 const unsigned char *data; 316 size_t data_len; 317 enum ie_rsn_cipher_suite group_cipher; 318 uint16_t pairwise_ciphers; 319 uint32_t akm_suites; 320 bool preauthentication:1; 321 bool no_pairwise:1; 322 uint8_t ptksa_replay_counter:2; 323 uint8_t gtksa_replay_counter:2; 324 bool mfpr:1; 325 bool mfpc:1; 326 bool peerkey_enabled:1; 327 bool spp_a_msdu_capable:1; 328 bool spp_a_msdu_required:1; 329 bool pbac:1; 330 bool extended_key_id:1; 331 uint8_t num_pmkids; 332 uint8_t pmkids[232]; 333 enum ie_rsn_cipher_suite group_management_cipher; 334 }; 335 336 static const unsigned char rsne_data_1[] = { 337 0x30, 0x14, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 338 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x02, 0x00, 0x00, 339 }; 340 341 static const struct ie_rsne_info_test ie_rsne_info_test_1 = { 342 .data = rsne_data_1, 343 .data_len = sizeof(rsne_data_1), 344 .group_cipher = IE_RSN_CIPHER_SUITE_CCMP, 345 .pairwise_ciphers = IE_RSN_CIPHER_SUITE_CCMP, 346 .akm_suites = IE_RSN_AKM_SUITE_PSK, 347 }; 348 349 /* 802.11, Section 8.4.2.27.1; first example */ 350 static const unsigned char rsne_data_2[] = { 351 0x30, 0x14, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 352 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x01, 0x00, 0x00, 353 }; 354 355 static const struct ie_rsne_info_test ie_rsne_info_test_2 = { 356 .data = rsne_data_2, 357 .data_len = sizeof(rsne_data_2), 358 .group_cipher = IE_RSN_CIPHER_SUITE_CCMP, 359 .pairwise_ciphers = IE_RSN_CIPHER_SUITE_CCMP, 360 .akm_suites = IE_RSN_AKM_SUITE_8021X, 361 }; 362 363 /* 802.11, Section 8.4.2.27.1; second example */ 364 static const unsigned char rsne_data_3[] = { 365 0x30, 0x14, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 366 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x01, 0x01, 0x00, 367 }; 368 369 static const struct ie_rsne_info_test ie_rsne_info_test_3 = { 370 .data = rsne_data_3, 371 .data_len = sizeof(rsne_data_3), 372 .group_cipher = IE_RSN_CIPHER_SUITE_CCMP, 373 .pairwise_ciphers = IE_RSN_CIPHER_SUITE_CCMP, 374 .akm_suites = IE_RSN_AKM_SUITE_8021X, 375 .preauthentication = true, 376 }; 377 378 /* 802.11, Section 8.4.2.27.1; third example */ 379 static const unsigned char rsne_data_4[] = { 380 0x30, 0x14, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x01, 0x01, 0x00, 0x00, 0x0f, 381 0xac, 0x00, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x01, 0x00, 0x00 382 }; 383 384 static const struct ie_rsne_info_test ie_rsne_info_test_4 = { 385 .data = rsne_data_4, 386 .data_len = sizeof(rsne_data_4), 387 .group_cipher = IE_RSN_CIPHER_SUITE_WEP40, 388 .pairwise_ciphers = IE_RSN_CIPHER_SUITE_USE_GROUP_CIPHER, 389 .akm_suites = IE_RSN_AKM_SUITE_8021X, 390 }; 391 392 /* 802.11, Section 8.4.2.27.1; fourth example */ 393 static const unsigned char rsne_data_5[] = { 394 0x30, 0x26, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 395 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x01, 0x01, 0x00, 0x01, 0x00, 396 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 397 0x0d, 0x0e, 0x0f, 0x10, 398 }; 399 400 static const struct ie_rsne_info_test ie_rsne_info_test_5 = { 401 .data = rsne_data_5, 402 .data_len = sizeof(rsne_data_5), 403 .group_cipher = IE_RSN_CIPHER_SUITE_CCMP, 404 .pairwise_ciphers = IE_RSN_CIPHER_SUITE_CCMP, 405 .akm_suites = IE_RSN_AKM_SUITE_8021X, 406 .preauthentication = true, 407 .num_pmkids = 1, 408 .pmkids = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 409 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, }, 410 }; 411 412 /* 802.11, Section 8.4.2.27.1; last example */ 413 static const unsigned char rsne_data_6[] = { 414 0x30, 0x1a, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 415 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x01, 0x80, 0x00, 0x00, 0x00, 416 0x00, 0x0f, 0xac, 0x06, 417 }; 418 419 static const struct ie_rsne_info_test ie_rsne_info_test_6 = { 420 .data = rsne_data_6, 421 .data_len = sizeof(rsne_data_6), 422 .group_cipher = IE_RSN_CIPHER_SUITE_CCMP, 423 .pairwise_ciphers = IE_RSN_CIPHER_SUITE_CCMP, 424 .akm_suites = IE_RSN_AKM_SUITE_8021X, 425 .mfpc = true, /* Management frame protection is enabled, not required */ 426 .group_management_cipher = IE_RSN_CIPHER_SUITE_BIP_CMAC, 427 }; 428 429 static void ie_test_rsne_info(const void *data) 430 { 431 const struct ie_rsne_info_test *test = data; 432 int r; 433 struct ie_rsn_info info; 434 435 r = ie_parse_rsne_from_data(test->data, test->data_len, &info); 436 assert(r == 0); 437 438 assert(test->group_cipher == info.group_cipher); 439 assert(test->pairwise_ciphers == info.pairwise_ciphers); 440 assert(test->akm_suites == info.akm_suites); 441 442 assert(test->preauthentication == info.preauthentication); 443 assert(test->no_pairwise == info.no_pairwise); 444 assert(test->ptksa_replay_counter == info.ptksa_replay_counter); 445 assert(test->gtksa_replay_counter == info.gtksa_replay_counter); 446 assert(test->mfpr == info.mfpr); 447 assert(test->mfpc == info.mfpc); 448 assert(test->peerkey_enabled == info.peerkey_enabled); 449 assert(test->spp_a_msdu_capable == info.spp_a_msdu_capable); 450 assert(test->spp_a_msdu_required == info.spp_a_msdu_required); 451 assert(test->pbac == info.pbac); 452 assert(test->extended_key_id == info.extended_key_id); 453 454 assert(test->num_pmkids == info.num_pmkids); 455 assert(!memcmp(test->pmkids, info.pmkids, 16 * test->num_pmkids)); 456 457 assert(test->group_management_cipher == info.group_management_cipher); 458 } 459 460 static void ie_test_rsne_build_compact_info(const void *data) 461 { 462 const struct ie_rsne_info_test *test = data; 463 int r; 464 struct ie_rsn_info info; 465 uint8_t buf[256]; 466 467 r = ie_parse_rsne_from_data(test->data, test->data_len, &info); 468 assert(r == 0); 469 470 r = ie_build_rsne(&info, buf); 471 assert(r); 472 473 assert(!memcmp(test->data, buf, test->data_len)); 474 } 475 476 struct ie_wpa_info_test { 477 const unsigned char *data; 478 size_t data_len; 479 enum ie_rsn_cipher_suite group_cipher; 480 uint16_t pairwise_ciphers; 481 uint32_t akm_suites; 482 }; 483 484 static const unsigned char wpa_data_1[] = { 485 0xdd, 0x1a, 0x00, 0x50, 0xf2, 0x01, 0x01, 0x00, 0x00, 0x50, 0xf2, 0x02, 486 0x02, 0x00, 0x00, 0x50, 0xf2, 0x04, 0x00, 0x50, 0xf2, 0x02, 0x01, 0x00, 487 0x00, 0x50, 0xf2, 0x02 488 }; 489 490 static const struct ie_wpa_info_test ie_wpa_info_test_1 = { 491 .data = wpa_data_1, 492 .data_len = sizeof(wpa_data_1), 493 .group_cipher = IE_RSN_CIPHER_SUITE_TKIP, 494 .pairwise_ciphers = IE_RSN_CIPHER_SUITE_TKIP | IE_RSN_CIPHER_SUITE_CCMP, 495 }; 496 497 static void ie_test_wpa_info(const void *data) 498 { 499 const struct ie_wpa_info_test *test = data; 500 int r; 501 struct ie_rsn_info info; 502 503 r = ie_parse_wpa_from_data(test->data, test->data_len, &info); 504 assert(r == 0); 505 506 assert(test->group_cipher == info.group_cipher); 507 assert(test->pairwise_ciphers == info.pairwise_ciphers); 508 } 509 510 /* Wrong OUI type*/ 511 static const unsigned char wpa_data_2[] = { 512 0xdd, 0x1a, 0x00, 0x50, 0xf2, 0x02, 0x01, 0x00, 0x00, 0x50, 0xf2, 0x02, 513 0x02, 0x00, 0x00, 0x50, 0xf2, 0x04, 0x00, 0x50, 0xf2, 0x02, 0x01, 0x00, 514 0x00, 0x50, 0xf2, 0x02 515 }; 516 517 static const struct ie_wpa_info_test ie_wpa_info_test_2 = { 518 .data = wpa_data_2, 519 .data_len = sizeof(wpa_data_2), 520 }; 521 522 /* Wrong version */ 523 static const unsigned char wpa_data_3[] = { 524 0xdd, 0x1a, 0x00, 0x50, 0xf2, 0x01, 0x02, 0x00, 0x00, 0x50, 0xf2, 0x02, 525 0x02, 0x00, 0x00, 0x50, 0xf2, 0x04, 0x00, 0x50, 0xf2, 0x02, 0x01, 0x00, 526 0x00, 0x50, 0xf2, 0x02 527 }; 528 529 static const struct ie_wpa_info_test ie_wpa_info_test_3 = { 530 .data = wpa_data_3, 531 .data_len = sizeof(wpa_data_3), 532 }; 533 534 /* Unsupported OUI */ 535 static const unsigned char wpa_data_4[] = { 536 0xdd, 0x1a, 0x00, 0x0f, 0xac, 0x01, 0x01, 0x00, 0x00, 0x50, 0xf2, 0x02, 537 0x02, 0x00, 0x00, 0x50, 0xf2, 0x04, 0x00, 0x50, 0xf2, 0x02, 0x01, 0x00, 538 0x00, 0x50, 0xf2, 0x02 539 }; 540 541 static const struct ie_wpa_info_test ie_wpa_info_test_4 = { 542 .data = wpa_data_4, 543 .data_len = sizeof(wpa_data_4), 544 }; 545 546 static void ie_test_wpa_info_fail(const void *data) 547 { 548 const struct ie_wpa_info_test *test = data; 549 int r; 550 struct ie_rsn_info info; 551 552 r = ie_parse_wpa_from_data(test->data, test->data_len, &info); 553 assert(r != 0); 554 } 555 556 static const struct ie_wpa_info_test ie_wpa_info_test_5 = { 557 .data = wpa_data_1, 558 .data_len = sizeof(wpa_data_1), 559 .group_cipher = IE_RSN_CIPHER_SUITE_TKIP, 560 .pairwise_ciphers = IE_RSN_CIPHER_SUITE_CCMP | IE_RSN_CIPHER_SUITE_CCMP, 561 .akm_suites = IE_RSN_AKM_SUITE_PSK, 562 }; 563 564 static void ie_test_wpa_build_compact_info(const void *data) 565 { 566 const struct ie_wpa_info_test *test = data; 567 int r; 568 struct ie_rsn_info info; 569 uint8_t buf[256]; 570 571 r = ie_parse_wpa_from_data(test->data, test->data_len, &info); 572 assert(r == 0); 573 574 r = ie_build_wpa(&info, buf); 575 assert(r); 576 577 assert(!memcmp(test->data, buf, test->data_len)); 578 } 579 580 struct ie_tlv_concat_test { 581 const unsigned char *ies; 582 unsigned int len; 583 const unsigned char *expected_data; 584 ssize_t expected_len; 585 }; 586 587 static unsigned char wsc_ie_single[] = { 588 0xdd, 0xc7, 0x00, 0x50, 0xf2, 0x04, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10, 589 0x44, 0x00, 0x01, 0x02, 0x10, 0x41, 0x00, 0x01, 0x01, 0x10, 0x12, 0x00, 590 0x02, 0x00, 0x04, 0x10, 0x53, 0x00, 0x02, 0x26, 0x88, 0x10, 0x3b, 0x00, 591 0x01, 0x03, 0x10, 0x47, 0x00, 0x10, 0xc4, 0x4a, 0xad, 0x8d, 0x25, 0x2f, 592 0x52, 0xc6, 0xf9, 0x6b, 0x38, 0x5d, 0xcb, 0x23, 0x31, 0xae, 0x10, 0x21, 593 0x00, 0x15, 0x41, 0x53, 0x55, 0x53, 0x54, 0x65, 0x4b, 0x20, 0x43, 0x6f, 594 0x6d, 0x70, 0x75, 0x74, 0x65, 0x72, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x10, 595 0x23, 0x00, 0x1c, 0x57, 0x69, 0x2d, 0x46, 0x69, 0x20, 0x50, 0x72, 0x6f, 596 0x74, 0x65, 0x63, 0x74, 0x65, 0x64, 0x20, 0x53, 0x65, 0x74, 0x75, 0x70, 597 0x20, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x10, 0x24, 0x00, 0x08, 0x52, 598 0x54, 0x2d, 0x41, 0x43, 0x36, 0x38, 0x55, 0x10, 0x42, 0x00, 0x11, 0x31, 599 0x30, 0x3a, 0x63, 0x33, 0x3a, 0x37, 0x62, 0x3a, 0x35, 0x34, 0x3a, 0x37, 600 0x34, 0x3a, 0x64, 0x30, 0x10, 0x54, 0x00, 0x08, 0x00, 0x06, 0x00, 0x50, 601 0xf2, 0x04, 0x00, 0x01, 0x10, 0x11, 0x00, 0x08, 0x52, 0x54, 0x2d, 0x41, 602 0x43, 0x36, 0x38, 0x55, 0x10, 0x08, 0x00, 0x02, 0x20, 0x08, 0x10, 0x3c, 603 0x00, 0x01, 0x03, 0x10, 0x49, 0x00, 0x0e, 0x00, 0x37, 0x2a, 0x00, 0x01, 604 0x20, 0x01, 0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 605 }; 606 607 static unsigned char wsc_ie_split[] = { 608 0xdd, 0x3A, 0x00, 0x50, 0xf2, 0x04, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10, 609 0x44, 0x00, 0x01, 0x02, 0x10, 0x41, 0x00, 0x01, 0x01, 0x10, 0x12, 0x00, 610 0x02, 0x00, 0x04, 0x10, 0x53, 0x00, 0x02, 0x26, 0x88, 0x10, 0x3b, 0x00, 611 0x01, 0x03, 0x10, 0x47, 0x00, 0x10, 0xc4, 0x4a, 0xad, 0x8d, 0x25, 0x2f, 612 0x52, 0xc6, 0xf9, 0x6b, 0x38, 0x5d, 0xcb, 0x23, 0x31, 0xae, 0x10, 0x21, 613 0xdd, 0x3A, 0x00, 0x50, 0xf2, 0x04, 0x00, 0x15, 0x41, 0x53, 0x55, 0x53, 614 0x54, 0x65, 0x4b, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x75, 0x74, 0x65, 0x72, 615 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x10, 0x23, 0x00, 0x1c, 0x57, 0x69, 0x2d, 616 0x46, 0x69, 0x20, 0x50, 0x72, 0x6f, 0x74, 0x65, 0x63, 0x74, 0x65, 0x64, 617 0x20, 0x53, 0x65, 0x74, 0x75, 0x70, 0x20, 0x52, 0x6f, 0x75, 0x74, 0x65, 618 0xdd, 0x3A, 0x00, 0x50, 0xf2, 0x04, 0x72, 0x10, 0x24, 0x00, 0x08, 0x52, 619 0x54, 0x2d, 0x41, 0x43, 0x36, 0x38, 0x55, 0x10, 0x42, 0x00, 0x11, 0x31, 620 0x30, 0x3a, 0x63, 0x33, 0x3a, 0x37, 0x62, 0x3a, 0x35, 0x34, 0x3a, 0x37, 621 0x34, 0x3a, 0x64, 0x30, 0x10, 0x54, 0x00, 0x08, 0x00, 0x06, 0x00, 0x50, 622 0xf2, 0x04, 0x00, 0x01, 0x10, 0x11, 0x00, 0x08, 0x52, 0x54, 0x2d, 0x41, 623 0xdd, 0x25, 0x00, 0x50, 0xf2, 0x04, 0x43, 0x36, 0x38, 0x55, 0x10, 0x08, 624 0x00, 0x02, 0x20, 0x08, 0x10, 0x3c, 0x00, 0x01, 0x03, 0x10, 0x49, 0x00, 625 0x0e, 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20, 0x01, 0x06, 0xff, 0xff, 0xff, 626 0xff, 0xff, 0xff, 627 }; 628 629 struct ie_tlv_concat_test ie_tlv_concat_test_data_1 = { 630 .ies = wsc_ie_single, 631 .len = sizeof(wsc_ie_single), 632 .expected_data = wsc_ie_single + 6, 633 .expected_len = sizeof(wsc_ie_single) - 6, 634 }; 635 636 struct ie_tlv_concat_test ie_tlv_concat_test_data_2 = { 637 .ies = beacon_frame + 36, 638 .len = 252, 639 .expected_data = beacon_frame + 206, 640 .expected_len = 45, 641 }; 642 643 struct ie_tlv_concat_test ie_tlv_concat_test_data_3 = { 644 .ies = wsc_ie_split, 645 .len = sizeof(wsc_ie_split), 646 .expected_data = wsc_ie_single + 6, 647 .expected_len = sizeof(wsc_ie_single) - 6, 648 }; 649 650 static void ie_test_concat_wsc(const void *data) 651 { 652 const struct ie_tlv_concat_test *test = data; 653 void *res; 654 ssize_t len; 655 656 res = ie_tlv_extract_wsc_payload(test->ies, test->len, &len); 657 658 assert(len == test->expected_len); 659 660 if (len > 0) 661 assert(!memcmp(res, test->expected_data, len)); 662 else 663 assert(!res); 664 665 l_free(res); 666 } 667 668 static void ie_test_encapsulate_wsc(const void *data) 669 { 670 const struct ie_tlv_concat_test *test = data; 671 void *extracted; 672 void *packed; 673 ssize_t extracted_len; 674 size_t packed_len; 675 676 extracted = ie_tlv_extract_wsc_payload(test->ies, test->len, 677 &extracted_len); 678 679 assert(extracted_len == test->expected_len); 680 681 if (extracted_len > 0) 682 assert(!memcmp(extracted, test->expected_data, extracted_len)); 683 else 684 assert(!extracted); 685 686 packed = ie_tlv_encapsulate_wsc_payload(extracted, extracted_len, 687 &packed_len); 688 689 assert(packed); 690 assert(packed_len == test->len); 691 assert(!memcmp(packed, test->ies, test->len)); 692 693 l_free(extracted); 694 l_free(packed); 695 } 696 697 int main(int argc, char *argv[]) 698 { 699 l_test_init(&argc, &argv); 700 701 l_test_add("/ie/reader/static", ie_test_reader, &beacon_frame_data); 702 703 l_test_add("/ie/writer/invalid-tag", ie_test_writer_invalid_tag, NULL); 704 l_test_add("/ie/writer/invalid-len", ie_test_writer_invalid_len, NULL); 705 706 l_test_add("/ie/writer/create", ie_test_writer, &beacon_frame_data); 707 708 l_test_add("/ie/reader/extended", ie_test_reader_extended, NULL); 709 l_test_add("/ie/writer/extended", ie_test_writer_extended, NULL); 710 711 l_test_add("/ie/RSN Info Parser/Test Case 1", 712 ie_test_rsne_info, &ie_rsne_info_test_1); 713 l_test_add("/ie/RSN Info Parser/Test Case 2", 714 ie_test_rsne_info, &ie_rsne_info_test_2); 715 l_test_add("/ie/RSN Info Parser/Test Case 3", 716 ie_test_rsne_info, &ie_rsne_info_test_3); 717 l_test_add("/ie/RSN Info Parser/Test Case 4", 718 ie_test_rsne_info, &ie_rsne_info_test_4); 719 l_test_add("/ie/RSN Info Parser/Test Case 5", 720 ie_test_rsne_info, &ie_rsne_info_test_5); 721 l_test_add("/ie/RSN Info Parser/Test Case 6", 722 ie_test_rsne_info, &ie_rsne_info_test_6); 723 724 l_test_add("/ie/RSN Info Builder/Compact Test 1", 725 ie_test_rsne_build_compact_info, 726 &ie_rsne_info_test_3); 727 l_test_add("/ie/RSN Info Builder/Compact Test 2", 728 ie_test_rsne_build_compact_info, 729 &ie_rsne_info_test_4); 730 l_test_add("/ie/RSN Info Builder/Compact Test 3", 731 ie_test_rsne_build_compact_info, 732 &ie_rsne_info_test_5); 733 734 l_test_add("/ie/WPA Info Parser/Test Case 1", 735 ie_test_wpa_info, &ie_wpa_info_test_1); 736 l_test_add("/ie/WPA Info Parser/Test Case 2", 737 ie_test_wpa_info_fail, &ie_wpa_info_test_2); 738 l_test_add("/ie/WPA Info Parser/Test Case 3", 739 ie_test_wpa_info_fail, &ie_wpa_info_test_3); 740 l_test_add("/ie/WPA Info Parser/Test Case 4", 741 ie_test_wpa_info_fail, &ie_wpa_info_test_4); 742 743 l_test_add("/ie/WPA Info Builder/Compact Test 1", 744 ie_test_wpa_build_compact_info, 745 &ie_wpa_info_test_5); 746 747 l_test_add("/ie/Concatenation/WSC/Test Case 1", 748 ie_test_concat_wsc, &ie_tlv_concat_test_data_1); 749 l_test_add("/ie/Concatenation/WSC/Test Case 2", 750 ie_test_concat_wsc, &ie_tlv_concat_test_data_2); 751 l_test_add("/ie/Concatenation/WSC/Test Case 3", 752 ie_test_concat_wsc, &ie_tlv_concat_test_data_3); 753 754 l_test_add("/ie/Encapsulation/WSC/Test Case 1", 755 ie_test_encapsulate_wsc, 756 &ie_tlv_concat_test_data_1); 757 758 return l_test_run(); 759 }