unit1657.c (3514B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9 * 10 * This software is licensed as described in the file COPYING, which 11 * you should have received as part of this distribution. The terms 12 * are also available at https://curl.se/docs/copyright.html. 13 * 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 * copies of the Software, and permit persons to whom the Software is 16 * furnished to do so, under the terms of the COPYING file. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 #include "unitcheck.h" 25 26 #include "vtls/x509asn1.h" 27 28 #if defined(USE_GNUTLS) || defined(USE_SCHANNEL) || defined(USE_MBEDTLS) 29 30 struct test1657_spec { 31 CURLcode (*setbuf)(const struct test1657_spec *spec, struct dynbuf *buf); 32 size_t n; 33 CURLcode exp_result; 34 }; 35 36 static CURLcode make1657_nested(const struct test1657_spec *spec, 37 struct dynbuf *buf) 38 { 39 CURLcode r; 40 size_t i; 41 unsigned char open_undef[] = { 0x32, 0x80 }; 42 unsigned char close_undef[] = { 0x00, 0x00 }; 43 44 for(i = 0; i < spec->n; ++i) { 45 r = curlx_dyn_addn(buf, open_undef, sizeof(open_undef)); 46 if(r) 47 return r; 48 } 49 for(i = 0; i < spec->n; ++i) { 50 r = curlx_dyn_addn(buf, close_undef, sizeof(close_undef)); 51 if(r) 52 return r; 53 } 54 return CURLE_OK; 55 } 56 57 static const struct test1657_spec test1657_specs[] = { 58 { make1657_nested, 3, CURLE_OK }, 59 { make1657_nested, 16, CURLE_OK }, 60 { make1657_nested, 17, CURLE_BAD_FUNCTION_ARGUMENT }, 61 { make1657_nested, 1024, CURLE_BAD_FUNCTION_ARGUMENT }, 62 }; 63 64 static bool do_test1657(const struct test1657_spec *spec, size_t i, 65 struct dynbuf *buf) 66 { 67 CURLcode result; 68 struct Curl_asn1Element elem; 69 const char *in; 70 71 memset(&elem, 0, sizeof(elem)); 72 curlx_dyn_reset(buf); 73 result = spec->setbuf(spec, buf); 74 if(result) { 75 curl_mfprintf(stderr, "test %zu: error setting buf %d\n", i, result); 76 return FALSE; 77 } 78 in = curlx_dyn_ptr(buf); 79 result = Curl_x509_getASN1Element(&elem, in, in + curlx_dyn_len(buf)); 80 if(result != spec->exp_result) { 81 curl_mfprintf(stderr, "test %zu: expect result %d, got %d\n", 82 i, spec->exp_result, result); 83 return FALSE; 84 } 85 return TRUE; 86 } 87 88 static CURLcode test_unit1657(char *arg) 89 { 90 UNITTEST_BEGIN_SIMPLE 91 92 size_t i; 93 bool all_ok = TRUE; 94 struct dynbuf dbuf; 95 96 curlx_dyn_init(&dbuf, 32*1024); 97 98 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 99 curl_mfprintf(stderr, "curl_global_init() failed\n"); 100 return TEST_ERR_MAJOR_BAD; 101 } 102 103 for(i = 0; i < CURL_ARRAYSIZE(test1657_specs); ++i) { 104 if(!do_test1657(&test1657_specs[i], i, &dbuf)) 105 all_ok = FALSE; 106 } 107 fail_unless(all_ok, "some tests of Curl_x509_getASN1Element() fails"); 108 109 curlx_dyn_free(&dbuf); 110 curl_global_cleanup(); 111 112 UNITTEST_END_SIMPLE 113 } 114 115 #else 116 117 static CURLcode test_unit1657(char *arg) 118 { 119 UNITTEST_BEGIN_SIMPLE 120 puts("not tested since Curl_x509_getASN1Element() is not built in"); 121 UNITTEST_END_SIMPLE 122 } 123 124 #endif