1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
/*****************************************************************************
* h2frame_test.c: HTTP/2 frame formatting tests
*****************************************************************************
* Copyright (C) 2015 Rémi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#undef NDEBUG
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "h2frame.h"
#include <vlc_common.h>
#include "conn.h"
#define CTX ((void *)(uintptr_t)0x44556677)
static unsigned settings;
/* Callbacks */
void vlc_http_dbg(void *stream, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stream, fmt, ap);
va_end(ap);
}
static void vlc_h2_setting(void *ctx, uint_fast16_t id, uint_fast32_t value)
{
assert(ctx == CTX);
settings++;
fprintf(stderr, "* Setting %s: %"PRIuFAST32"\n",
vlc_h2_setting_name(id), value);
switch (id)
{
case VLC_H2_SETTING_HEADER_TABLE_SIZE:
assert(value == VLC_H2_MAX_HEADER_TABLE);
break;
case VLC_H2_SETTING_ENABLE_PUSH:
assert(value == 0);
break;
case VLC_H2_SETTING_MAX_CONCURRENT_STREAMS:
assert(value == VLC_H2_MAX_STREAMS);
break;
case VLC_H2_SETTING_INITIAL_WINDOW_SIZE:
assert(value == VLC_H2_INIT_WINDOW);
break;
case VLC_H2_SETTING_MAX_FRAME_SIZE:
assert(value == VLC_H2_MAX_FRAME);
break;
case VLC_H2_SETTING_MAX_HEADER_LIST_SIZE:
assert(value == VLC_H2_MAX_HEADER_LIST);
break;
default:
assert(!"Known setting");
break;
}
}
static unsigned settings_acked;
static int vlc_h2_settings_done(void *ctx)
{
assert(ctx == CTX);
settings_acked++;
(void) ctx;
return 0;
}
#define PING_VALUE UINT64_C(0x1122334455667788)
static unsigned pings;
static int vlc_h2_ping(void *ctx, uint_fast64_t opaque)
{
assert(ctx == CTX);
assert(opaque == PING_VALUE);
pings++;
return 0;
}
static uint_fast32_t local_error;
static uint_fast32_t remote_error;
static void vlc_h2_error(void *ctx, uint_fast32_t code)
{
assert(ctx == CTX);
local_error = code;
}
static int vlc_h2_reset(void *ctx, uint_fast32_t last_seq, uint_fast32_t code)
{
assert(ctx == CTX);
assert(last_seq == 0);
remote_error = code;
return 0;
}
static void vlc_h2_window_status(void *ctx, uint32_t *rcwd)
{
assert(ctx == CTX);
*rcwd = (1u << 31) - 1;
}
#define STREAM_ID 0x76543210
char stream_cookie; /* dummy unique value */
static void *vlc_h2_stream_lookup(void *ctx, uint_fast32_t id)
{
assert(ctx == CTX);
if (id != STREAM_ID)
return NULL;
return &stream_cookie;
}
static const char *const resp_hdrv[][2] = {
{ ":status", "200" },
{ "cache-control", "private" },
{ "date", "Mon, 21 Oct 2013 20:13:22 GMT" },
{ "location", "https://www.example.com" },
{ "content-encoding", "gzip" },
{ "set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; "
"version=1" },
};
static const unsigned resp_hdrc = sizeof (resp_hdrv) / sizeof (resp_hdrv[0]);
static unsigned stream_header_tables;
static void vlc_h2_stream_headers(void *ctx, unsigned count,
const char *const hdrs[][2])
{
assert(ctx == &stream_cookie);
assert(count == resp_hdrc);
for (unsigned i = 0; i < count; i++)
{
assert(!strcmp(hdrs[i][0], resp_hdrv[i][0]));
assert(!strcmp(hdrs[i][1], resp_hdrv[i][1]));
}
stream_header_tables++;
}
#define MESSAGE "Hello world!"
static unsigned stream_blocks;
static int vlc_h2_stream_data(void *ctx, struct vlc_h2_frame *f)
{
size_t len;
const uint8_t *buf = vlc_h2_frame_data_get(f, &len);
assert(ctx == &stream_cookie);
assert(len == sizeof (MESSAGE));
assert(!memcmp(buf, MESSAGE, len));
stream_blocks++;
free(f);
return 0;
}
static unsigned stream_ends;
static void vlc_h2_stream_end(void *ctx)
{
assert(ctx == &stream_cookie);
stream_ends++;
}
static int vlc_h2_stream_error(void *ctx, uint_fast32_t id, uint_fast32_t code)
{
assert(ctx == CTX);
assert(id == STREAM_ID + 2);
assert(code == VLC_H2_REFUSED_STREAM);
return 0;
}
static int vlc_h2_stream_reset(void *ctx, uint_fast32_t code)
{
assert(ctx == &stream_cookie);
assert(code == VLC_H2_CANCEL);
return 0;
}
/* Frame formatting */
static struct vlc_h2_frame *resize(struct vlc_h2_frame *f, size_t size)
{ /* NOTE: increasing size would require realloc() */
f->data[0] = size >> 16;
f->data[1] = size >> 8;
f->data[2] = size;
return f;
}
static struct vlc_h2_frame *retype(struct vlc_h2_frame *f, unsigned char type)
{
f->data[3] = type;
return f;
}
static struct vlc_h2_frame *reflag(struct vlc_h2_frame *f, unsigned char flags)
{
f->data[4] = flags;
return f;
}
static struct vlc_h2_frame *globalize(struct vlc_h2_frame *f)
{
memset(f->data + 5, 0, 4);
return f;
}
static struct vlc_h2_frame *localize(struct vlc_h2_frame *f)
{
f->data[5] = (STREAM_ID >> 24) & 0xff;
f->data[6] = (STREAM_ID >> 16) & 0xff;
f->data[7] = (STREAM_ID >> 8) & 0xff;
f->data[8] = STREAM_ID & 0xff;
return f;
}
static struct vlc_h2_frame *response(bool eos)
{
/* Use ridiculously small MTU to test headers fragmentation */
return vlc_h2_frame_headers(STREAM_ID, 16, eos, resp_hdrc, resp_hdrv);
}
static struct vlc_h2_frame *data(bool eos)
{
return vlc_h2_frame_data(STREAM_ID, MESSAGE, sizeof (MESSAGE), eos);
}
static struct vlc_h2_frame *priority(void)
{
return localize(resize(retype(data(false), 0x2), 5));
}
static struct vlc_h2_frame *rst_stream(void)
{
return vlc_h2_frame_rst_stream(STREAM_ID, VLC_H2_CANCEL);
}
static struct vlc_h2_frame *ping(void)
{
return vlc_h2_frame_ping(PING_VALUE);
}
static struct vlc_h2_frame *goaway(void)
{
return vlc_h2_frame_goaway(0, VLC_H2_NO_ERROR);
}
static struct vlc_h2_frame *unknown(void)
{
return retype(ping(), 200);
}
/* Test harness */
static unsigned test_raw_seqv(struct vlc_h2_parser *p, va_list ap)
{
struct vlc_h2_frame *f;
unsigned i = 0;
assert(p != NULL);
while ((f = va_arg(ap, struct vlc_h2_frame *)) != NULL)
if (vlc_h2_parse(p, f) == 0)
i++;
return i;
}
static unsigned test_raw_seq(struct vlc_h2_parser *p, ...)
{
va_list ap;
unsigned i;
assert(p != NULL);
va_start(ap, p);
i = test_raw_seqv(p, ap);
va_end(ap);
return i;
}
static const struct vlc_h2_parser_cbs vlc_h2_frame_test_callbacks =
{
vlc_h2_setting,
vlc_h2_settings_done,
vlc_h2_ping,
vlc_h2_error,
vlc_h2_reset,
vlc_h2_window_status,
vlc_h2_stream_lookup,
vlc_h2_stream_error,
vlc_h2_stream_headers,
vlc_h2_stream_data,
vlc_h2_stream_end,
vlc_h2_stream_reset,
};
static unsigned test_seq(void *ctx, ...)
{
struct vlc_h2_parser *p;
va_list ap;
unsigned i;
settings = settings_acked = 0;
pings = 0;
local_error = remote_error = -1;
stream_header_tables = stream_blocks = stream_ends = 0;
p = vlc_h2_parse_init(ctx, &vlc_h2_frame_test_callbacks);
assert(p != NULL);
i = test_raw_seq(p, vlc_h2_frame_settings(), vlc_h2_frame_settings_ack(),
NULL);
assert(i == 2);
assert(settings >= 1);
assert(settings_acked == 1);
va_start(ap, ctx);
i = test_raw_seqv(p, ap);
va_end(ap);
assert(test_raw_seq(p, goaway(), NULL) == 1);
assert(remote_error == VLC_H2_NO_ERROR);
vlc_h2_parse_destroy(p);
return i;
}
static unsigned test_bad_seq(void *ctx, ...)
{
struct vlc_h2_parser *p;
va_list ap;
unsigned i;
p = vlc_h2_parse_init(ctx, &vlc_h2_frame_test_callbacks);
assert(p != NULL);
i = test_raw_seq(p, vlc_h2_frame_settings(), vlc_h2_frame_settings_ack(),
NULL);
assert(i == 2);
va_start(ap, ctx);
i = test_raw_seqv(p, ap);
va_end(ap);
i = test_raw_seq(p, goaway(), NULL);
assert(i == 0); /* in failed state */
vlc_h2_parse_destroy(p);
return i;
}
static void test_preface_fail(void)
{
struct vlc_h2_parser *p;
p = vlc_h2_parse_init(CTX, &vlc_h2_frame_test_callbacks);
assert(p != NULL);
assert(test_raw_seq(p, ping(), ping(), NULL) == 0);
vlc_h2_parse_destroy(p);
}
static void test_header_block_fail(void)
{
struct vlc_h2_frame *hf = response(true);
struct vlc_h2_frame *pf = ping();
/* Check what happens if non-CONTINUATION frame after HEADERS */
assert(hf != NULL && hf->next != NULL && pf != NULL);
pf->next = hf->next;
hf->next = pf;
assert(test_bad_seq(CTX, hf, NULL) == 0);
}
int main(void)
{
int ret;
ret = test_seq(CTX, NULL);
assert(ret == 0);
ret = test_seq(CTX, ping(), vlc_h2_frame_pong(42), ping(), NULL);
assert(ret == 3);
assert(pings == 2);
assert(stream_header_tables == 0);
assert(stream_blocks == 0);
assert(stream_ends == 0);
ret = test_seq(CTX, response(true), NULL);
assert(ret == 1);
assert(pings == 0);
assert(stream_header_tables == 1);
assert(stream_blocks == 0);
assert(stream_ends == 1);
ret = test_seq(CTX, response(false), data(true), ping(),
response(false), data(false), data(true),
response(false), data(false), priority(), unknown(),
NULL);
assert(ret == 10);
assert(pings == 1);
assert(stream_header_tables == 3);
assert(stream_blocks == 4);
assert(stream_ends == 2);
ret = test_seq(CTX, rst_stream(),
vlc_h2_frame_window_update(0, 0x1000),
vlc_h2_frame_headers(STREAM_ID + 2,
VLC_H2_DEFAULT_MAX_FRAME, true,
resp_hdrc, resp_hdrv),
NULL);
assert(ret == 3);
assert(pings == 0);
assert(stream_header_tables == 0);
assert(stream_blocks == 0);
assert(stream_ends == 0);
test_preface_fail();
test_header_block_fail();
test_bad_seq(CTX, globalize(response(true)), NULL);
test_bad_seq(CTX, resize(reflag(response(true), 0x08), 0), NULL);
test_bad_seq(CTX, resize(reflag(response(true), 0x20), 4), NULL);
test_bad_seq(CTX, globalize(data(true)), NULL);
test_bad_seq(CTX, globalize(priority()), NULL);
test_bad_seq(CTX, globalize(rst_stream()), NULL);
test_bad_seq(CTX, localize(vlc_h2_frame_settings()), NULL);
test_bad_seq(CTX, resize(vlc_h2_frame_settings(), 5), NULL);
test_bad_seq(CTX, resize(ping(), 7), NULL);
test_bad_seq(CTX, localize(ping()), NULL);
test_bad_seq(CTX, localize(goaway()), NULL);
test_bad_seq(CTX, resize(goaway(), 7), NULL);
/* TODO: PUSH_PROMISE, PRIORITY, padding, unknown, invalid stuff... */
/* Dummy API test */
assert(vlc_h2_frame_data(1, NULL, 1 << 28, false) == NULL);
for (unsigned i = 0; i < 65536; i++)
assert(strlen(vlc_h2_setting_name(i)) > 0);
for (unsigned i = 0; i < 100; i++) /* lets not try all 4 billions */
assert(strlen(vlc_h2_strerror(i)) > 0);
return 0;
}
|