Home | History | Annotate | Line # | Download | only in test
      1 /*
      2  * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (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 <stdio.h>
     11 #include <string.h>
     12 
     13 #include <openssl/opensslconf.h>
     14 #include "internal/quic_srt_gen.h"
     15 
     16 #include "testutil.h"
     17 #include "testutil/output.h"
     18 
     19 struct test_case {
     20     const unsigned char *key;
     21     size_t key_len;
     22     QUIC_CONN_ID dcid;
     23     QUIC_STATELESS_RESET_TOKEN expected;
     24 };
     25 
     26 static const unsigned char key_1[] = { 0x01, 0x02, 0x03 };
     27 
     28 static const unsigned char key_2[] = {
     29     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
     30     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
     31 };
     32 
     33 static const struct test_case tests[] = {
     34     { key_1, sizeof(key_1), { 2, { 0x55, 0x66 } },
     35         { { 0x02, 0x9e, 0x8f, 0x3d, 0x1e, 0xa9, 0x06, 0x23, 0xb2, 0x43, 0xd2, 0x19, 0x59, 0x8a, 0xa1, 0x66 } } },
     36     { key_2, sizeof(key_2), { 0, { 0 } },
     37         { { 0x93, 0x10, 0x2f, 0xc7, 0xaf, 0x9d, 0x9b, 0x28, 0x3f, 0x84, 0x95, 0x6b, 0xa3, 0xdc, 0x07, 0x6b } } },
     38     { key_2, sizeof(key_2),
     39         { 20, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
     40         { { 0x9a, 0x98, 0x98, 0x61, 0xbe, 0xfd, 0xe3, 0x05, 0x45, 0xac, 0x66, 0xcf, 0x3b, 0x58, 0xfb, 0xab } } }
     41 };
     42 
     43 static int test_srt_gen(int idx)
     44 {
     45     int testresult = 0;
     46     const struct test_case *t = &tests[idx];
     47     QUIC_SRT_GEN *srt_gen = NULL;
     48     QUIC_STATELESS_RESET_TOKEN token;
     49     size_t i;
     50 
     51     if (!TEST_ptr(srt_gen = ossl_quic_srt_gen_new(NULL, NULL,
     52                       t->key, t->key_len)))
     53         goto err;
     54 
     55     for (i = 0; i < 2; ++i) {
     56         memset(&token, 0xff, sizeof(token));
     57 
     58         if (!TEST_true(ossl_quic_srt_gen_calculate_token(srt_gen, &t->dcid,
     59                 &token)))
     60             goto err;
     61 
     62         if (!TEST_mem_eq(token.token, sizeof(token.token),
     63                 &t->expected, sizeof(t->expected)))
     64             goto err;
     65     }
     66 
     67     testresult = 1;
     68 err:
     69     ossl_quic_srt_gen_free(srt_gen);
     70     return testresult;
     71 }
     72 
     73 int setup_tests(void)
     74 {
     75     ADD_ALL_TESTS(test_srt_gen, OSSL_NELEM(tests));
     76     return 1;
     77 }
     78