Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  * Copyright Nokia 2007-2019
      4      1.1  christos  * Copyright Siemens AG 2015-2019
      5      1.1  christos  *
      6      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      7      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      8      1.1  christos  * in the file LICENSE in the source distribution or at
      9      1.1  christos  * https://www.openssl.org/source/license.html
     10      1.1  christos  */
     11      1.1  christos 
     12      1.1  christos #include "helpers/cmp_testlib.h"
     13      1.1  christos 
     14      1.1  christos static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
     15      1.1  christos 
     16      1.1  christos typedef struct test_fixture {
     17      1.1  christos     const char *test_case_name;
     18      1.1  christos     int expected;
     19      1.1  christos     ASN1_OCTET_STRING *src_string;
     20      1.1  christos     ASN1_OCTET_STRING *tgt_string;
     21      1.1  christos 
     22      1.1  christos } CMP_ASN_TEST_FIXTURE;
     23      1.1  christos 
     24      1.1  christos static CMP_ASN_TEST_FIXTURE *set_up(const char *const test_case_name)
     25      1.1  christos {
     26      1.1  christos     CMP_ASN_TEST_FIXTURE *fixture;
     27      1.1  christos 
     28      1.1  christos     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
     29      1.1  christos         return NULL;
     30      1.1  christos     fixture->test_case_name = test_case_name;
     31      1.1  christos     return fixture;
     32      1.1  christos }
     33      1.1  christos 
     34      1.1  christos static void tear_down(CMP_ASN_TEST_FIXTURE *fixture)
     35      1.1  christos {
     36      1.1  christos     ASN1_OCTET_STRING_free(fixture->src_string);
     37      1.1  christos     if (fixture->tgt_string != fixture->src_string)
     38      1.1  christos         ASN1_OCTET_STRING_free(fixture->tgt_string);
     39      1.1  christos 
     40      1.1  christos     OPENSSL_free(fixture);
     41      1.1  christos }
     42      1.1  christos 
     43      1.1  christos static int execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE *fixture)
     44      1.1  christos {
     45      1.1  christos     int res = 0;
     46      1.1  christos     ASN1_INTEGER *asn1integer = ASN1_INTEGER_new();
     47      1.1  christos     const int good_int = 77;
     48      1.1  christos     const int64_t max_int = INT_MAX;
     49      1.1  christos 
     50      1.1  christos     if (!TEST_ptr(asn1integer))
     51      1.1  christos         return res;
     52      1.1  christos 
     53      1.1  christos     if (!TEST_true(ASN1_INTEGER_set(asn1integer, good_int))) {
     54      1.1  christos         ASN1_INTEGER_free(asn1integer);
     55      1.1  christos         return 0;
     56      1.1  christos     }
     57      1.1  christos     res = TEST_int_eq(good_int, ossl_cmp_asn1_get_int(asn1integer));
     58      1.1  christos     if (res == 0)
     59      1.1  christos         goto err;
     60      1.1  christos 
     61      1.1  christos     res = 0;
     62      1.1  christos     if (!TEST_true(ASN1_INTEGER_set_int64(asn1integer, max_int + 1)))
     63      1.1  christos         goto err;
     64      1.1  christos     res = TEST_int_eq(-2, ossl_cmp_asn1_get_int(asn1integer));
     65      1.1  christos 
     66  1.1.1.2  christos err:
     67      1.1  christos     ASN1_INTEGER_free(asn1integer);
     68      1.1  christos     return res;
     69      1.1  christos }
     70      1.1  christos 
     71      1.1  christos static int test_cmp_asn1_get_int(void)
     72      1.1  christos {
     73      1.1  christos     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
     74      1.1  christos     fixture->expected = 1;
     75      1.1  christos     EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down);
     76      1.1  christos     return result;
     77      1.1  christos }
     78      1.1  christos 
     79      1.1  christos static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE *
     80  1.1.1.2  christos         fixture)
     81      1.1  christos {
     82      1.1  christos     if (!TEST_int_eq(fixture->expected,
     83  1.1.1.2  christos             ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string,
     84  1.1.1.2  christos                 fixture->src_string)))
     85      1.1  christos         return 0;
     86      1.1  christos     if (fixture->expected != 0)
     87  1.1.1.2  christos         return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string, fixture->src_string));
     88      1.1  christos     return 1;
     89      1.1  christos }
     90      1.1  christos 
     91      1.1  christos static int test_ASN1_OCTET_STRING_set(void)
     92      1.1  christos {
     93      1.1  christos     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
     94      1.1  christos     fixture->expected = 1;
     95      1.1  christos     if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new())
     96  1.1.1.2  christos         || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
     97  1.1.1.2  christos         || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
     98  1.1.1.2  christos             sizeof(rand_data)))) {
     99      1.1  christos         tear_down(fixture);
    100      1.1  christos         fixture = NULL;
    101      1.1  christos     }
    102      1.1  christos     EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
    103      1.1  christos     return result;
    104      1.1  christos }
    105      1.1  christos 
    106      1.1  christos static int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
    107      1.1  christos {
    108      1.1  christos     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
    109      1.1  christos     fixture->expected = 1;
    110      1.1  christos     if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
    111  1.1.1.2  christos         || !(fixture->tgt_string = fixture->src_string)
    112  1.1.1.2  christos         || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
    113  1.1.1.2  christos             sizeof(rand_data)))) {
    114      1.1  christos         tear_down(fixture);
    115      1.1  christos         fixture = NULL;
    116      1.1  christos     }
    117      1.1  christos     EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
    118      1.1  christos     return result;
    119      1.1  christos }
    120      1.1  christos 
    121      1.1  christos void cleanup_tests(void)
    122      1.1  christos {
    123      1.1  christos     return;
    124      1.1  christos }
    125      1.1  christos 
    126      1.1  christos int setup_tests(void)
    127      1.1  christos {
    128      1.1  christos     RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
    129      1.1  christos     /* ASN.1 related tests */
    130      1.1  christos     ADD_TEST(test_cmp_asn1_get_int);
    131      1.1  christos     ADD_TEST(test_ASN1_OCTET_STRING_set);
    132      1.1  christos     ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src);
    133      1.1  christos     return 1;
    134      1.1  christos }
    135