packettest.c revision 1.1.1.1
1/* 2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10#include "../ssl/packet_locl.h" 11 12#define BUF_LEN 255 13 14static int test_PACKET_remaining(unsigned char buf[BUF_LEN]) 15{ 16 PACKET pkt; 17 18 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 19 || PACKET_remaining(&pkt) != BUF_LEN 20 || !PACKET_forward(&pkt, BUF_LEN - 1) 21 || PACKET_remaining(&pkt) != 1 22 || !PACKET_forward(&pkt, 1) 23 || PACKET_remaining(&pkt) != 0) { 24 fprintf(stderr, "test_PACKET_remaining() failed\n"); 25 return 0; 26 } 27 28 return 1; 29} 30 31static int test_PACKET_end(unsigned char buf[BUF_LEN]) 32{ 33 PACKET pkt; 34 35 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 36 || PACKET_remaining(&pkt) != BUF_LEN 37 || PACKET_end(&pkt) != buf + BUF_LEN 38 || !PACKET_forward(&pkt, BUF_LEN - 1) 39 || PACKET_end(&pkt) != buf + BUF_LEN 40 || !PACKET_forward(&pkt, 1) 41 || PACKET_end(&pkt) != buf + BUF_LEN) { 42 fprintf(stderr, "test_PACKET_end() failed\n"); 43 return 0; 44 } 45 46 return 1; 47} 48 49static int test_PACKET_get_1(unsigned char buf[BUF_LEN]) 50{ 51 unsigned int i; 52 PACKET pkt; 53 54 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 55 || !PACKET_get_1(&pkt, &i) 56 || i != 0x02 57 || !PACKET_forward(&pkt, BUF_LEN - 2) 58 || !PACKET_get_1(&pkt, &i) 59 || i != 0xfe 60 || PACKET_get_1(&pkt, &i)) { 61 fprintf(stderr, "test_PACKET_get_1() failed\n"); 62 return 0; 63 } 64 65 return 1; 66} 67 68static int test_PACKET_get_4(unsigned char buf[BUF_LEN]) 69{ 70 unsigned long i; 71 PACKET pkt; 72 73 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 74 || !PACKET_get_4(&pkt, &i) 75 || i != 0x08060402UL 76 || !PACKET_forward(&pkt, BUF_LEN - 8) 77 || !PACKET_get_4(&pkt, &i) 78 || i != 0xfefcfaf8UL 79 || PACKET_get_4(&pkt, &i)) { 80 fprintf(stderr, "test_PACKET_get_4() failed\n"); 81 return 0; 82 } 83 84 return 1; 85} 86 87static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN]) 88{ 89 unsigned int i; 90 PACKET pkt; 91 92 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 93 || !PACKET_get_net_2(&pkt, &i) 94 || i != 0x0204 95 || !PACKET_forward(&pkt, BUF_LEN - 4) 96 || !PACKET_get_net_2(&pkt, &i) 97 || i != 0xfcfe 98 || PACKET_get_net_2(&pkt, &i)) { 99 fprintf(stderr, "test_PACKET_get_net_2() failed\n"); 100 return 0; 101 } 102 103 return 1; 104} 105 106static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN]) 107{ 108 unsigned long i; 109 PACKET pkt; 110 111 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 112 || !PACKET_get_net_3(&pkt, &i) 113 || i != 0x020406UL 114 || !PACKET_forward(&pkt, BUF_LEN - 6) 115 || !PACKET_get_net_3(&pkt, &i) 116 || i != 0xfafcfeUL 117 || PACKET_get_net_3(&pkt, &i)) { 118 fprintf(stderr, "test_PACKET_get_net_3() failed\n"); 119 return 0; 120 } 121 122 return 1; 123} 124 125static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN]) 126{ 127 unsigned long i; 128 PACKET pkt; 129 130 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 131 || !PACKET_get_net_4(&pkt, &i) 132 || i != 0x02040608UL 133 || !PACKET_forward(&pkt, BUF_LEN - 8) 134 || !PACKET_get_net_4(&pkt, &i) 135 || i != 0xf8fafcfeUL 136 || PACKET_get_net_4(&pkt, &i)) { 137 fprintf(stderr, "test_PACKET_get_net_4() failed\n"); 138 return 0; 139 } 140 141 return 1; 142} 143 144static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN]) 145{ 146 PACKET pkt, subpkt; 147 unsigned long i; 148 149 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 150 || !PACKET_get_sub_packet(&pkt, &subpkt, 4) 151 || !PACKET_get_net_4(&subpkt, &i) 152 || i != 0x02040608UL 153 || PACKET_remaining(&subpkt) 154 || !PACKET_forward(&pkt, BUF_LEN - 8) 155 || !PACKET_get_sub_packet(&pkt, &subpkt, 4) 156 || !PACKET_get_net_4(&subpkt, &i) 157 || i != 0xf8fafcfeUL 158 || PACKET_remaining(&subpkt) 159 || PACKET_get_sub_packet(&pkt, &subpkt, 4)) { 160 fprintf(stderr, "test_PACKET_get_sub_packet() failed\n"); 161 return 0; 162 } 163 164 return 1; 165} 166 167static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN]) 168{ 169 const unsigned char *bytes; 170 PACKET pkt; 171 172 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 173 || !PACKET_get_bytes(&pkt, &bytes, 4) 174 || bytes[0] != 2 || bytes[1] != 4 175 || bytes[2] != 6 || bytes[3] != 8 176 || PACKET_remaining(&pkt) != BUF_LEN -4 177 || !PACKET_forward(&pkt, BUF_LEN - 8) 178 || !PACKET_get_bytes(&pkt, &bytes, 4) 179 || bytes[0] != 0xf8 || bytes[1] != 0xfa 180 || bytes[2] != 0xfc || bytes[3] != 0xfe 181 || PACKET_remaining(&pkt)) { 182 fprintf(stderr, "test_PACKET_get_bytes() failed\n"); 183 return 0; 184 } 185 186 return 1; 187} 188 189static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN]) 190{ 191 unsigned char bytes[4]; 192 PACKET pkt; 193 194 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 195 || !PACKET_copy_bytes(&pkt, bytes, 4) 196 || bytes[0] != 2 || bytes[1] != 4 197 || bytes[2] != 6 || bytes[3] != 8 198 || PACKET_remaining(&pkt) != BUF_LEN - 4 199 || !PACKET_forward(&pkt, BUF_LEN - 8) 200 || !PACKET_copy_bytes(&pkt, bytes, 4) 201 || bytes[0] != 0xf8 || bytes[1] != 0xfa 202 || bytes[2] != 0xfc || bytes[3] != 0xfe 203 || PACKET_remaining(&pkt)) { 204 fprintf(stderr, "test_PACKET_copy_bytes() failed\n"); 205 return 0; 206 } 207 208 return 1; 209} 210 211static int test_PACKET_copy_all(unsigned char buf[BUF_LEN]) 212{ 213 unsigned char tmp[BUF_LEN]; 214 PACKET pkt; 215 size_t len; 216 217 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 218 || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len) 219 || len != BUF_LEN 220 || memcmp(buf, tmp, BUF_LEN) != 0 221 || PACKET_remaining(&pkt) != BUF_LEN 222 || PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)) { 223 fprintf(stderr, "test_PACKET_copy_bytes() failed\n"); 224 return 0; 225 } 226 227 return 1; 228} 229 230static int test_PACKET_memdup(unsigned char buf[BUF_LEN]) 231{ 232 unsigned char *data = NULL; 233 size_t len; 234 PACKET pkt; 235 236 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 237 || !PACKET_memdup(&pkt, &data, &len) 238 || len != BUF_LEN 239 || memcmp(data, PACKET_data(&pkt), len) 240 || !PACKET_forward(&pkt, 10) 241 || !PACKET_memdup(&pkt, &data, &len) 242 || len != BUF_LEN - 10 243 || memcmp(data, PACKET_data(&pkt), len)) { 244 fprintf(stderr, "test_PACKET_memdup() failed\n"); 245 OPENSSL_free(data); 246 return 0; 247 } 248 249 OPENSSL_free(data); 250 return 1; 251} 252 253static int test_PACKET_strndup() 254{ 255 char buf[10], buf2[10]; 256 char *data = NULL; 257 PACKET pkt; 258 259 memset(buf, 'x', 10); 260 memset(buf2, 'y', 10); 261 buf2[5] = '\0'; 262 263 if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10) 264 || !PACKET_strndup(&pkt, &data) 265 || strlen(data) != 10 266 || strncmp(data, buf, 10) 267 || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10) 268 || !PACKET_strndup(&pkt, &data) 269 || strlen(data) != 5 270 || strcmp(data, buf2)) { 271 fprintf(stderr, "test_PACKET_strndup failed\n"); 272 OPENSSL_free(data); 273 return 0; 274 } 275 276 OPENSSL_free(data); 277 return 1; 278} 279 280static int test_PACKET_contains_zero_byte() 281{ 282 char buf[10], buf2[10]; 283 PACKET pkt; 284 285 memset(buf, 'x', 10); 286 memset(buf2, 'y', 10); 287 buf2[5] = '\0'; 288 289 if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10) 290 || PACKET_contains_zero_byte(&pkt) 291 || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10) 292 || !PACKET_contains_zero_byte(&pkt)) { 293 fprintf(stderr, "test_PACKET_contains_zero_byte failed\n"); 294 return 0; 295 } 296 297 return 1; 298} 299 300static int test_PACKET_forward(unsigned char buf[BUF_LEN]) 301{ 302 const unsigned char *byte; 303 PACKET pkt; 304 305 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 306 || !PACKET_forward(&pkt, 1) 307 || !PACKET_get_bytes(&pkt, &byte, 1) 308 || byte[0] != 4 309 || !PACKET_forward(&pkt, BUF_LEN - 3) 310 || !PACKET_get_bytes(&pkt, &byte, 1) 311 || byte[0] != 0xfe) { 312 fprintf(stderr, "test_PACKET_forward() failed\n"); 313 return 0; 314 } 315 316 return 1; 317} 318 319static int test_PACKET_buf_init() 320{ 321 unsigned char buf[BUF_LEN]; 322 PACKET pkt; 323 324 /* Also tests PACKET_remaining() */ 325 if ( !PACKET_buf_init(&pkt, buf, 4) 326 || PACKET_remaining(&pkt) != 4 327 || !PACKET_buf_init(&pkt, buf, BUF_LEN) 328 || PACKET_remaining(&pkt) != BUF_LEN 329 || PACKET_buf_init(&pkt, buf, -1)) { 330 fprintf(stderr, "test_PACKET_buf_init() failed\n"); 331 return 0; 332 } 333 334 return 1; 335} 336 337static int test_PACKET_null_init() 338{ 339 PACKET pkt; 340 341 PACKET_null_init(&pkt); 342 if ( PACKET_remaining(&pkt) != 0 343 || PACKET_forward(&pkt, 1)) { 344 fprintf(stderr, "test_PACKET_null_init() failed\n"); 345 return 0; 346 } 347 348 return 1; 349} 350 351static int test_PACKET_equal(unsigned char buf[BUF_LEN]) 352{ 353 PACKET pkt; 354 355 if ( !PACKET_buf_init(&pkt, buf, 4) 356 || !PACKET_equal(&pkt, buf, 4) 357 || PACKET_equal(&pkt, buf + 1, 4) 358 || !PACKET_buf_init(&pkt, buf, BUF_LEN) 359 || !PACKET_equal(&pkt, buf, BUF_LEN) 360 || PACKET_equal(&pkt, buf, BUF_LEN - 1) 361 || PACKET_equal(&pkt, buf, BUF_LEN + 1) 362 || PACKET_equal(&pkt, buf, 0)) { 363 fprintf(stderr, "test_PACKET_equal() failed\n"); 364 return 0; 365 } 366 367 return 1; 368} 369 370static int test_PACKET_get_length_prefixed_1() 371{ 372 unsigned char buf[BUF_LEN]; 373 const size_t len = 16; 374 unsigned int i; 375 PACKET pkt, short_pkt, subpkt; 376 377 buf[0] = len; 378 for (i = 1; i < BUF_LEN; i++) { 379 buf[i] = (i * 2) & 0xff; 380 } 381 382 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 383 || !PACKET_buf_init(&short_pkt, buf, len) 384 || !PACKET_get_length_prefixed_1(&pkt, &subpkt) 385 || PACKET_remaining(&subpkt) != len 386 || !PACKET_get_net_2(&subpkt, &i) 387 || i != 0x0204 388 || PACKET_get_length_prefixed_1(&short_pkt, &subpkt) 389 || PACKET_remaining(&short_pkt) != len) { 390 fprintf(stderr, "test_PACKET_get_length_prefixed_1() failed\n"); 391 return 0; 392 } 393 394 return 1; 395} 396 397static int test_PACKET_get_length_prefixed_2() 398{ 399 unsigned char buf[1024]; 400 const size_t len = 516; /* 0x0204 */ 401 unsigned int i; 402 PACKET pkt, short_pkt, subpkt; 403 404 for (i = 1; i <= 1024; i++) { 405 buf[i-1] = (i * 2) & 0xff; 406 } 407 408 if ( !PACKET_buf_init(&pkt, buf, 1024) 409 || !PACKET_buf_init(&short_pkt, buf, len) 410 || !PACKET_get_length_prefixed_2(&pkt, &subpkt) 411 || PACKET_remaining(&subpkt) != len 412 || !PACKET_get_net_2(&subpkt, &i) 413 || i != 0x0608 414 || PACKET_get_length_prefixed_2(&short_pkt, &subpkt) 415 || PACKET_remaining(&short_pkt) != len) { 416 fprintf(stderr, "test_PACKET_get_length_prefixed_2() failed\n"); 417 return 0; 418 } 419 420 return 1; 421} 422 423static int test_PACKET_get_length_prefixed_3() 424{ 425 unsigned char buf[1024]; 426 const size_t len = 516; /* 0x000204 */ 427 unsigned int i; 428 PACKET pkt, short_pkt, subpkt; 429 430 for (i = 0; i < 1024; i++) { 431 buf[i] = (i * 2) & 0xff; 432 } 433 434 if ( !PACKET_buf_init(&pkt, buf, 1024) 435 || !PACKET_buf_init(&short_pkt, buf, len) 436 || !PACKET_get_length_prefixed_3(&pkt, &subpkt) 437 || PACKET_remaining(&subpkt) != len 438 || !PACKET_get_net_2(&subpkt, &i) 439 || i != 0x0608 440 || PACKET_get_length_prefixed_3(&short_pkt, &subpkt) 441 || PACKET_remaining(&short_pkt) != len) { 442 fprintf(stderr, "test_PACKET_get_length_prefixed_3() failed\n"); 443 return 0; 444 } 445 446 return 1; 447} 448 449static int test_PACKET_as_length_prefixed_1() 450{ 451 unsigned char buf[BUF_LEN]; 452 const size_t len = 16; 453 unsigned int i; 454 PACKET pkt, exact_pkt, subpkt; 455 456 buf[0] = len; 457 for (i = 1; i < BUF_LEN; i++) { 458 buf[i] = (i * 2) & 0xff; 459 } 460 461 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) 462 || !PACKET_buf_init(&exact_pkt, buf, len + 1) 463 || PACKET_as_length_prefixed_1(&pkt, &subpkt) 464 || PACKET_remaining(&pkt) != BUF_LEN 465 || !PACKET_as_length_prefixed_1(&exact_pkt, &subpkt) 466 || PACKET_remaining(&exact_pkt) != 0 467 || PACKET_remaining(&subpkt) != len) { 468 fprintf(stderr, "test_PACKET_as_length_prefixed_1() failed\n"); 469 return 0; 470 } 471 472 return 1; 473} 474 475static int test_PACKET_as_length_prefixed_2() 476{ 477 unsigned char buf[1024]; 478 const size_t len = 516; /* 0x0204 */ 479 unsigned int i; 480 PACKET pkt, exact_pkt, subpkt; 481 482 for (i = 1; i <= 1024; i++) { 483 buf[i-1] = (i * 2) & 0xff; 484 } 485 486 if ( !PACKET_buf_init(&pkt, buf, 1024) 487 || !PACKET_buf_init(&exact_pkt, buf, len + 2) 488 || PACKET_as_length_prefixed_2(&pkt, &subpkt) 489 || PACKET_remaining(&pkt) != 1024 490 || !PACKET_as_length_prefixed_2(&exact_pkt, &subpkt) 491 || PACKET_remaining(&exact_pkt) != 0 492 || PACKET_remaining(&subpkt) != len) { 493 fprintf(stderr, "test_PACKET_as_length_prefixed_2() failed\n"); 494 return 0; 495 } 496 497 return 1; 498} 499 500int main(int argc, char **argv) 501{ 502 unsigned char buf[BUF_LEN]; 503 unsigned int i; 504 505 for (i=1; i<=BUF_LEN; i++) { 506 buf[i-1] = (i * 2) & 0xff; 507 } 508 i = 0; 509 510 if ( !test_PACKET_buf_init() 511 || !test_PACKET_null_init() 512 || !test_PACKET_remaining(buf) 513 || !test_PACKET_end(buf) 514 || !test_PACKET_equal(buf) 515 || !test_PACKET_get_1(buf) 516 || !test_PACKET_get_4(buf) 517 || !test_PACKET_get_net_2(buf) 518 || !test_PACKET_get_net_3(buf) 519 || !test_PACKET_get_net_4(buf) 520 || !test_PACKET_get_sub_packet(buf) 521 || !test_PACKET_get_bytes(buf) 522 || !test_PACKET_copy_bytes(buf) 523 || !test_PACKET_copy_all(buf) 524 || !test_PACKET_memdup(buf) 525 || !test_PACKET_strndup() 526 || !test_PACKET_contains_zero_byte() 527 || !test_PACKET_forward(buf) 528 || !test_PACKET_get_length_prefixed_1() 529 || !test_PACKET_get_length_prefixed_2() 530 || !test_PACKET_get_length_prefixed_3() 531 || !test_PACKET_as_length_prefixed_1() 532 || !test_PACKET_as_length_prefixed_2()) { 533 return 1; 534 } 535 printf("PASS\n"); 536 return 0; 537} 538