test_crypto_gen.c revision b8fa6195
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 25392f7a3SLiteSpeed Tech#include <assert.h> 35392f7a3SLiteSpeed Tech#include <stdio.h> 45392f7a3SLiteSpeed Tech#include <stdlib.h> 55392f7a3SLiteSpeed Tech#include <string.h> 65392f7a3SLiteSpeed Tech#include <sys/queue.h> 75392f7a3SLiteSpeed Tech 85392f7a3SLiteSpeed Tech#include "lsquic.h" 95392f7a3SLiteSpeed Tech#include "lsquic_types.h" 105392f7a3SLiteSpeed Tech#include "lsquic_parse.h" 115392f7a3SLiteSpeed Tech 125392f7a3SLiteSpeed Tech 135392f7a3SLiteSpeed Techstruct test { 145392f7a3SLiteSpeed Tech int lineno; 155392f7a3SLiteSpeed Tech const struct parse_funcs * 165392f7a3SLiteSpeed Tech pf; 175392f7a3SLiteSpeed Tech uint64_t offset; 185392f7a3SLiteSpeed Tech size_t avail; /* Space to write stream frame to */ 195392f7a3SLiteSpeed Tech size_t min_sz; /* Minimum size needed to generate CRYPTO 205392f7a3SLiteSpeed Tech * frame. Any sizes smaller than this 215392f7a3SLiteSpeed Tech * should fail. 225392f7a3SLiteSpeed Tech */ 235392f7a3SLiteSpeed Tech size_t data_sz; 245392f7a3SLiteSpeed Tech char data[0x100]; 255392f7a3SLiteSpeed Tech 265392f7a3SLiteSpeed Tech /* Output. This is how we expect the resulting frame to look. 275392f7a3SLiteSpeed Tech */ 285392f7a3SLiteSpeed Tech int len; /* Return value of pf_gen_crypto_frame() */ 295392f7a3SLiteSpeed Tech char out[0x100]; 305392f7a3SLiteSpeed Tech}; 315392f7a3SLiteSpeed Tech 325392f7a3SLiteSpeed Tech 335392f7a3SLiteSpeed Techstatic const struct test tests[] = { 345392f7a3SLiteSpeed Tech 355392f7a3SLiteSpeed Tech { .lineno = __LINE__, 36fb73393fSDmitri Tikhonov .pf = select_pf_by_ver(LSQVER_ID27), 375392f7a3SLiteSpeed Tech .offset = 0, 385392f7a3SLiteSpeed Tech .data_sz = 10, 395392f7a3SLiteSpeed Tech .data = "0123456789", 405392f7a3SLiteSpeed Tech .avail = 0x100, 415392f7a3SLiteSpeed Tech .out = 425392f7a3SLiteSpeed Tech { /* Type */ 0x06, 435392f7a3SLiteSpeed Tech /* Offset */ 0x00, 445392f7a3SLiteSpeed Tech /* Size */ 0x0A, 455392f7a3SLiteSpeed Tech /* Data */ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 465392f7a3SLiteSpeed Tech }, 475392f7a3SLiteSpeed Tech .len = 1 + 1 + 1 + 10, 485392f7a3SLiteSpeed Tech .min_sz = 1 + 1 + 1 + 1, 495392f7a3SLiteSpeed Tech }, 505392f7a3SLiteSpeed Tech 515392f7a3SLiteSpeed Tech { .lineno = __LINE__, 52fb73393fSDmitri Tikhonov .pf = select_pf_by_ver(LSQVER_ID27), 535392f7a3SLiteSpeed Tech .offset = 500, 545392f7a3SLiteSpeed Tech .data_sz = 10, 555392f7a3SLiteSpeed Tech .data = "0123456789", 565392f7a3SLiteSpeed Tech .avail = 0x100, 575392f7a3SLiteSpeed Tech .out = 585392f7a3SLiteSpeed Tech { /* Type */ 0x06, 595392f7a3SLiteSpeed Tech /* Offset */ 0x41, 0xF4, 605392f7a3SLiteSpeed Tech /* Size */ 0x0A, 615392f7a3SLiteSpeed Tech /* Data */ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 625392f7a3SLiteSpeed Tech }, 635392f7a3SLiteSpeed Tech .len = 1 + 2 + 1 + 10, 645392f7a3SLiteSpeed Tech .min_sz = 1 + 2 + 1 + 1, 655392f7a3SLiteSpeed Tech }, 665392f7a3SLiteSpeed Tech 675392f7a3SLiteSpeed Tech}; 685392f7a3SLiteSpeed Tech 695392f7a3SLiteSpeed Tech 705392f7a3SLiteSpeed Techstruct test_ctx { 715392f7a3SLiteSpeed Tech const struct test *test; 725392f7a3SLiteSpeed Tech unsigned off; 735392f7a3SLiteSpeed Tech}; 745392f7a3SLiteSpeed Tech 755392f7a3SLiteSpeed Tech 765392f7a3SLiteSpeed Techstatic size_t 77b8fa6195SDmitri Tikhonovcrypto_read (void *ctx, void *buf, size_t len, int *fin) 785392f7a3SLiteSpeed Tech{ 795392f7a3SLiteSpeed Tech struct test_ctx *test_ctx = ctx; 805392f7a3SLiteSpeed Tech if (test_ctx->test->data_sz - test_ctx->off < len) 815392f7a3SLiteSpeed Tech len = test_ctx->test->data_sz - test_ctx->off; 825392f7a3SLiteSpeed Tech memcpy(buf, test_ctx->test->data, len); 835392f7a3SLiteSpeed Tech test_ctx->off += len; 845392f7a3SLiteSpeed Tech return len; 855392f7a3SLiteSpeed Tech} 865392f7a3SLiteSpeed Tech 875392f7a3SLiteSpeed Tech 885392f7a3SLiteSpeed Techstatic void 895392f7a3SLiteSpeed Techinit_ctx (struct test_ctx *test_ctx, const struct test *test) 905392f7a3SLiteSpeed Tech{ 915392f7a3SLiteSpeed Tech test_ctx->test = test; 925392f7a3SLiteSpeed Tech test_ctx->off = 0; 935392f7a3SLiteSpeed Tech} 945392f7a3SLiteSpeed Tech 955392f7a3SLiteSpeed Tech 965392f7a3SLiteSpeed Techstatic void 975392f7a3SLiteSpeed Techrun_test (int i) 985392f7a3SLiteSpeed Tech{ 995392f7a3SLiteSpeed Tech const struct test *const test = &tests[i]; 1005392f7a3SLiteSpeed Tech 1015392f7a3SLiteSpeed Tech int len; 1025392f7a3SLiteSpeed Tech size_t min; 1035392f7a3SLiteSpeed Tech struct test_ctx test_ctx; 1045392f7a3SLiteSpeed Tech unsigned char out[0x100]; 1055392f7a3SLiteSpeed Tech 1065392f7a3SLiteSpeed Tech if (test->len > 0) 1075392f7a3SLiteSpeed Tech { 1085392f7a3SLiteSpeed Tech /* Test that all sizes under specified min fail to produce a frame */ 1095392f7a3SLiteSpeed Tech for (min = 0; min < test->min_sz; ++min) 1105392f7a3SLiteSpeed Tech { 1115392f7a3SLiteSpeed Tech init_ctx(&test_ctx, test); 112b8fa6195SDmitri Tikhonov len = test->pf->pf_gen_crypto_frame(out, min, 0, test->offset, 0, 1135392f7a3SLiteSpeed Tech test->data_sz, crypto_read, &test_ctx); 1145392f7a3SLiteSpeed Tech assert(-1 == len); 1155392f7a3SLiteSpeed Tech } 1165392f7a3SLiteSpeed Tech 1175392f7a3SLiteSpeed Tech /* Test that it succeeds now: */ 1185392f7a3SLiteSpeed Tech init_ctx(&test_ctx, test); 119b8fa6195SDmitri Tikhonov len = test->pf->pf_gen_crypto_frame(out, min, 0, test->offset, 0, 1205392f7a3SLiteSpeed Tech test->data_sz, crypto_read, &test_ctx); 1215392f7a3SLiteSpeed Tech assert(len == (int) min); 1225392f7a3SLiteSpeed Tech } 1235392f7a3SLiteSpeed Tech 1245392f7a3SLiteSpeed Tech init_ctx(&test_ctx, test); 125b8fa6195SDmitri Tikhonov len = test->pf->pf_gen_crypto_frame(out, test->avail, 0, test->offset, 0, 1265392f7a3SLiteSpeed Tech test->data_sz, crypto_read, &test_ctx); 1275392f7a3SLiteSpeed Tech 1285392f7a3SLiteSpeed Tech if (test->len > 0) { 1295392f7a3SLiteSpeed Tech assert(test->len == len); 1305392f7a3SLiteSpeed Tech assert(0 == memcmp(test->out, out, test->len)); 1315392f7a3SLiteSpeed Tech } 1325392f7a3SLiteSpeed Tech else 1335392f7a3SLiteSpeed Tech { 1345392f7a3SLiteSpeed Tech assert(len < 0); 1355392f7a3SLiteSpeed Tech } 1365392f7a3SLiteSpeed Tech} 1375392f7a3SLiteSpeed Tech 1385392f7a3SLiteSpeed Tech 1395392f7a3SLiteSpeed Techint 1405392f7a3SLiteSpeed Techmain (void) 1415392f7a3SLiteSpeed Tech{ 1425392f7a3SLiteSpeed Tech unsigned i; 1435392f7a3SLiteSpeed Tech for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) 1445392f7a3SLiteSpeed Tech run_test(i); 1455392f7a3SLiteSpeed Tech return 0; 1465392f7a3SLiteSpeed Tech} 147