Home | History | Annotate | Line # | Download | only in test
      1 /*
      2  * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
      3  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
      4  *
      5  * Licensed under the Apache License 2.0 (the "License").  You may not use
      6  * this file except in compliance with the License.  You can obtain a copy
      7  * in the file LICENSE in the source distribution or at
      8  * https://www.openssl.org/source/license.html
      9  */
     10 
     11 #include <string.h>
     12 #include <openssl/params.h>
     13 #include "testutil.h"
     14 
     15 /* On machines that dont support <inttypes.h> just disable the tests */
     16 #if !defined(OPENSSL_NO_INTTYPES_H)
     17 
     18 #ifdef OPENSSL_SYS_VMS
     19 #define strtoumax strtoull
     20 #define strtoimax strtoll
     21 #endif
     22 
     23 typedef struct {
     24     OSSL_PARAM *param;
     25     int32_t i32;
     26     int64_t i64;
     27     uint32_t u32;
     28     uint64_t u64;
     29     double d;
     30     int valid_i32, valid_i64, valid_u32, valid_u64, valid_d;
     31     void *ref, *datum;
     32     size_t size;
     33 } PARAM_CONVERSION;
     34 
     35 static int param_conversion_load_stanza(PARAM_CONVERSION *pc, const STANZA *s)
     36 {
     37 
     38     static int32_t datum_i32, ref_i32;
     39     static int64_t datum_i64, ref_i64;
     40     static uint32_t datum_u32, ref_u32;
     41     static uint64_t datum_u64, ref_u64;
     42     static double datum_d, ref_d;
     43     static OSSL_PARAM params[] = {
     44         OSSL_PARAM_int32("int32", &datum_i32),
     45         OSSL_PARAM_int64("int64", &datum_i64),
     46         OSSL_PARAM_uint32("uint32", &datum_u32),
     47         OSSL_PARAM_uint64("uint64", &datum_u64),
     48         OSSL_PARAM_double("double", &datum_d),
     49         OSSL_PARAM_END
     50     };
     51     int def_i32 = 0, def_i64 = 0, def_u32 = 0, def_u64 = 0, def_d = 0;
     52     const PAIR *pp = s->pairs;
     53     const char *type = NULL;
     54     char *p;
     55     int i;
     56 
     57     memset(pc, 0, sizeof(*pc));
     58 
     59     for (i = 0; i < s->numpairs; i++, pp++) {
     60         p = "";
     61         if (OPENSSL_strcasecmp(pp->key, "type") == 0) {
     62             if (type != NULL) {
     63                 TEST_info("Line %d: multiple type lines", s->curr);
     64                 return 0;
     65             }
     66             pc->param = OSSL_PARAM_locate(params, type = pp->value);
     67             if (pc->param == NULL) {
     68                 TEST_info("Line %d: unknown type line", s->curr);
     69                 return 0;
     70             }
     71         } else if (OPENSSL_strcasecmp(pp->key, "int32") == 0) {
     72             if (def_i32++) {
     73                 TEST_info("Line %d: multiple int32 lines", s->curr);
     74                 return 0;
     75             }
     76             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
     77                 pc->valid_i32 = 1;
     78                 pc->i32 = (int32_t)strtoimax(pp->value, &p, 10);
     79             }
     80         } else if (OPENSSL_strcasecmp(pp->key, "int64") == 0) {
     81             if (def_i64++) {
     82                 TEST_info("Line %d: multiple int64 lines", s->curr);
     83                 return 0;
     84             }
     85             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
     86                 pc->valid_i64 = 1;
     87                 pc->i64 = (int64_t)strtoimax(pp->value, &p, 10);
     88             }
     89         } else if (OPENSSL_strcasecmp(pp->key, "uint32") == 0) {
     90             if (def_u32++) {
     91                 TEST_info("Line %d: multiple uint32 lines", s->curr);
     92                 return 0;
     93             }
     94             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
     95                 pc->valid_u32 = 1;
     96                 pc->u32 = (uint32_t)strtoumax(pp->value, &p, 10);
     97             }
     98         } else if (OPENSSL_strcasecmp(pp->key, "uint64") == 0) {
     99             if (def_u64++) {
    100                 TEST_info("Line %d: multiple uint64 lines", s->curr);
    101                 return 0;
    102             }
    103             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
    104                 pc->valid_u64 = 1;
    105                 pc->u64 = (uint64_t)strtoumax(pp->value, &p, 10);
    106             }
    107         } else if (OPENSSL_strcasecmp(pp->key, "double") == 0) {
    108             if (def_d++) {
    109                 TEST_info("Line %d: multiple double lines", s->curr);
    110                 return 0;
    111             }
    112             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
    113                 pc->valid_d = 1;
    114                 pc->d = strtod(pp->value, &p);
    115             }
    116         } else {
    117             TEST_info("Line %d: unknown keyword %s", s->curr, pp->key);
    118             return 0;
    119         }
    120         if (*p != '\0') {
    121             TEST_info("Line %d: extra characters at end '%s' for %s",
    122                 s->curr, p, pp->key);
    123             return 0;
    124         }
    125     }
    126 
    127     if (!TEST_ptr(type)) {
    128         TEST_info("Line %d: type not found", s->curr);
    129         return 0;
    130     }
    131 
    132     if (OPENSSL_strcasecmp(type, "int32") == 0) {
    133         if (!TEST_true(def_i32) || !TEST_true(pc->valid_i32)) {
    134             TEST_note("errant int32 on line %d", s->curr);
    135             return 0;
    136         }
    137         datum_i32 = ref_i32 = pc->i32;
    138         pc->datum = &datum_i32;
    139         pc->ref = &ref_i32;
    140         pc->size = sizeof(ref_i32);
    141     } else if (OPENSSL_strcasecmp(type, "int64") == 0) {
    142         if (!TEST_true(def_i64) || !TEST_true(pc->valid_i64)) {
    143             TEST_note("errant int64 on line %d", s->curr);
    144             return 0;
    145         }
    146         datum_i64 = ref_i64 = pc->i64;
    147         pc->datum = &datum_i64;
    148         pc->ref = &ref_i64;
    149         pc->size = sizeof(ref_i64);
    150     } else if (OPENSSL_strcasecmp(type, "uint32") == 0) {
    151         if (!TEST_true(def_u32) || !TEST_true(pc->valid_u32)) {
    152             TEST_note("errant uint32 on line %d", s->curr);
    153             return 0;
    154         }
    155         datum_u32 = ref_u32 = pc->u32;
    156         pc->datum = &datum_u32;
    157         pc->ref = &ref_u32;
    158         pc->size = sizeof(ref_u32);
    159     } else if (OPENSSL_strcasecmp(type, "uint64") == 0) {
    160         if (!TEST_true(def_u64) || !TEST_true(pc->valid_u64)) {
    161             TEST_note("errant uint64 on line %d", s->curr);
    162             return 0;
    163         }
    164         datum_u64 = ref_u64 = pc->u64;
    165         pc->datum = &datum_u64;
    166         pc->ref = &ref_u64;
    167         pc->size = sizeof(ref_u64);
    168     } else if (OPENSSL_strcasecmp(type, "double") == 0) {
    169         if (!TEST_true(def_d) || !TEST_true(pc->valid_d)) {
    170             TEST_note("errant double on line %d", s->curr);
    171             return 0;
    172         }
    173         datum_d = ref_d = pc->d;
    174         pc->datum = &datum_d;
    175         pc->ref = &ref_d;
    176         pc->size = sizeof(ref_d);
    177     } else {
    178         TEST_error("type unknown at line %d", s->curr);
    179         return 0;
    180     }
    181     return 1;
    182 }
    183 
    184 static int param_conversion_test(const PARAM_CONVERSION *pc, int line)
    185 {
    186     int32_t i32;
    187     int64_t i64;
    188     uint32_t u32;
    189     uint64_t u64;
    190     double d;
    191 
    192     if (!pc->valid_i32) {
    193         if (!TEST_false(OSSL_PARAM_get_int32(pc->param, &i32))
    194             || !TEST_ulong_ne(ERR_get_error(), 0)) {
    195             TEST_note("unexpected valid conversion to int32 on line %d", line);
    196             return 0;
    197         }
    198     } else {
    199         if (!TEST_true(OSSL_PARAM_get_int32(pc->param, &i32))
    200             || !TEST_true(i32 == pc->i32)) {
    201             TEST_note("unexpected conversion to int32 on line %d", line);
    202             return 0;
    203         }
    204         memset(pc->datum, 44, pc->size);
    205         if (!TEST_true(OSSL_PARAM_set_int32(pc->param, i32))
    206             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
    207             TEST_note("unexpected valid conversion from int32 on line %d",
    208                 line);
    209             return 0;
    210         }
    211     }
    212 
    213     if (!pc->valid_i64) {
    214         if (!TEST_false(OSSL_PARAM_get_int64(pc->param, &i64))
    215             || !TEST_ulong_ne(ERR_get_error(), 0)) {
    216             TEST_note("unexpected valid conversion to int64 on line %d", line);
    217             return 0;
    218         }
    219     } else {
    220         if (!TEST_true(OSSL_PARAM_get_int64(pc->param, &i64))
    221             || !TEST_true(i64 == pc->i64)) {
    222             TEST_note("unexpected conversion to int64 on line %d", line);
    223             return 0;
    224         }
    225         memset(pc->datum, 44, pc->size);
    226         if (!TEST_true(OSSL_PARAM_set_int64(pc->param, i64))
    227             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
    228             TEST_note("unexpected valid conversion from int64 on line %d",
    229                 line);
    230             return 0;
    231         }
    232     }
    233 
    234     if (!pc->valid_u32) {
    235         if (!TEST_false(OSSL_PARAM_get_uint32(pc->param, &u32))
    236             || !TEST_ulong_ne(ERR_get_error(), 0)) {
    237             TEST_note("unexpected valid conversion to uint32 on line %d", line);
    238             return 0;
    239         }
    240     } else {
    241         if (!TEST_true(OSSL_PARAM_get_uint32(pc->param, &u32))
    242             || !TEST_true(u32 == pc->u32)) {
    243             TEST_note("unexpected conversion to uint32 on line %d", line);
    244             return 0;
    245         }
    246         memset(pc->datum, 44, pc->size);
    247         if (!TEST_true(OSSL_PARAM_set_uint32(pc->param, u32))
    248             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
    249             TEST_note("unexpected valid conversion from uint32 on line %d",
    250                 line);
    251             return 0;
    252         }
    253     }
    254 
    255     if (!pc->valid_u64) {
    256         if (!TEST_false(OSSL_PARAM_get_uint64(pc->param, &u64))
    257             || !TEST_ulong_ne(ERR_get_error(), 0)) {
    258             TEST_note("unexpected valid conversion to uint64 on line %d", line);
    259             return 0;
    260         }
    261     } else {
    262         if (!TEST_true(OSSL_PARAM_get_uint64(pc->param, &u64))
    263             || !TEST_true(u64 == pc->u64)) {
    264             TEST_note("unexpected conversion to uint64 on line %d", line);
    265             return 0;
    266         }
    267         memset(pc->datum, 44, pc->size);
    268         if (!TEST_true(OSSL_PARAM_set_uint64(pc->param, u64))
    269             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
    270             TEST_note("unexpected valid conversion from uint64 on line %d",
    271                 line);
    272             return 0;
    273         }
    274     }
    275 
    276     if (!pc->valid_d) {
    277         if (!TEST_false(OSSL_PARAM_get_double(pc->param, &d))
    278             || !TEST_ulong_ne(ERR_get_error(), 0)) {
    279             TEST_note("unexpected valid conversion to double on line %d", line);
    280             return 0;
    281         }
    282     } else {
    283         if (!TEST_true(OSSL_PARAM_get_double(pc->param, &d))) {
    284             TEST_note("unable to convert to double on line %d", line);
    285             return 0;
    286         }
    287         /*
    288          * Check for not a number (NaN) without using the libm functions.
    289          * When d is a NaN, the standard requires d == d to be false.
    290          * It's less clear if d != d should be true even though it generally is.
    291          * Hence we use the equality test and a not.
    292          */
    293         if (!(d == d)) {
    294             /*
    295              * We've encountered a NaN so check it's really meant to be a NaN.
    296              * We ignore the case where the two values are both different NaN,
    297              * that's not resolvable without knowing the underlying format
    298              * or using libm functions.
    299              */
    300             if (!TEST_false(pc->d == pc->d)) {
    301                 TEST_note("unexpected NaN on line %d", line);
    302                 return 0;
    303             }
    304         } else if (!TEST_true(d == pc->d)) {
    305             TEST_note("unexpected conversion to double on line %d", line);
    306             return 0;
    307         }
    308         memset(pc->datum, 44, pc->size);
    309         if (!TEST_true(OSSL_PARAM_set_double(pc->param, d))
    310             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
    311             TEST_note("unexpected valid conversion from double on line %d",
    312                 line);
    313             return 0;
    314         }
    315     }
    316 
    317     return 1;
    318 }
    319 
    320 static int run_param_file_tests(int i)
    321 {
    322     STANZA *s;
    323     PARAM_CONVERSION pc;
    324     const char *testfile = test_get_argument(i);
    325     int res = 1;
    326 
    327     if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
    328         return 0;
    329     if (!test_start_file(s, testfile)) {
    330         OPENSSL_free(s);
    331         return 0;
    332     }
    333 
    334     while (!BIO_eof(s->fp)) {
    335         if (!test_readstanza(s)) {
    336             res = 0;
    337             goto end;
    338         }
    339         if (s->numpairs != 0)
    340             if (!param_conversion_load_stanza(&pc, s)
    341                 || !param_conversion_test(&pc, s->curr))
    342                 res = 0;
    343         test_clearstanza(s);
    344     }
    345 end:
    346     test_end_file(s);
    347     OPENSSL_free(s);
    348     return res;
    349 }
    350 
    351 #endif /* OPENSSL_NO_INTTYPES_H */
    352 
    353 OPT_TEST_DECLARE_USAGE("file...\n")
    354 
    355 int setup_tests(void)
    356 {
    357     size_t n;
    358 
    359     if (!test_skip_common_options()) {
    360         TEST_error("Error parsing test options\n");
    361         return 0;
    362     }
    363 
    364     n = test_get_argument_count();
    365     if (n == 0)
    366         return 0;
    367 
    368 #if !defined(OPENSSL_NO_INTTYPES_H)
    369     ADD_ALL_TESTS(run_param_file_tests, n);
    370 #endif /* OPENSSL_NO_INTTYPES_H */
    371 
    372     return 1;
    373 }
    374