quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

unit1656.c (4397B)


      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 test_spec {
     31   const char *input;
     32   const char *exp_output;
     33   CURLcode exp_result;
     34 };
     35 
     36 static bool do_test(const struct test_spec *spec, size_t i,
     37                     struct dynbuf *dbuf)
     38 {
     39   CURLcode result;
     40   const char *in = spec->input;
     41 
     42   curlx_dyn_reset(dbuf);
     43   result = Curl_x509_GTime2str(dbuf, in, in + strlen(in));
     44   if(result != spec->exp_result) {
     45     curl_mfprintf(stderr, "test %zu: expect result %d, got %d\n",
     46                   i, spec->exp_result, result);
     47     return FALSE;
     48   }
     49   else if(!result && strcmp(spec->exp_output, curlx_dyn_ptr(dbuf))) {
     50     curl_mfprintf(stderr,
     51                   "test %zu: input '%s', expected output '%s', got '%s'\n",
     52                   i, in, spec->exp_output, curlx_dyn_ptr(dbuf));
     53     return FALSE;
     54   }
     55 
     56   return TRUE;
     57 }
     58 
     59 static CURLcode test_unit1656(char *arg)
     60 {
     61   UNITTEST_BEGIN_SIMPLE
     62 
     63   static const struct test_spec test_specs[] = {
     64   { "190321134340", "1903-21-13 43:40:00", CURLE_OK },
     65   { "", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
     66   { "WTF", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
     67   { "0WTF", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
     68   { "19032113434", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
     69   { "19032113434WTF", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
     70   { "190321134340.", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
     71   { "190321134340.1", "1903-21-13 43:40:00.1", CURLE_OK },
     72   { "19032113434017.0", "1903-21-13 43:40:17", CURLE_OK },
     73   { "19032113434017.01", "1903-21-13 43:40:17.01", CURLE_OK },
     74   { "19032113434003.001", "1903-21-13 43:40:03.001", CURLE_OK },
     75   { "19032113434003.090", "1903-21-13 43:40:03.09", CURLE_OK },
     76   { "190321134340Z", "1903-21-13 43:40:00 GMT", CURLE_OK },
     77   { "19032113434017.0Z", "1903-21-13 43:40:17 GMT", CURLE_OK },
     78   { "19032113434017.01Z", "1903-21-13 43:40:17.01 GMT", CURLE_OK },
     79   { "19032113434003.001Z", "1903-21-13 43:40:03.001 GMT", CURLE_OK },
     80   { "19032113434003.090Z", "1903-21-13 43:40:03.09 GMT", CURLE_OK },
     81   { "190321134340CET", "1903-21-13 43:40:00 CET", CURLE_OK },
     82   { "19032113434017.0CET", "1903-21-13 43:40:17 CET", CURLE_OK },
     83   { "19032113434017.01CET", "1903-21-13 43:40:17.01 CET", CURLE_OK },
     84   { "190321134340+02:30", "1903-21-13 43:40:00 UTC+02:30", CURLE_OK },
     85   { "19032113434017.0+02:30", "1903-21-13 43:40:17 UTC+02:30", CURLE_OK },
     86   { "19032113434017.01+02:30", "1903-21-13 43:40:17.01 UTC+02:30", CURLE_OK },
     87   { "190321134340-3", "1903-21-13 43:40:00 UTC-3", CURLE_OK },
     88   { "19032113434017.0-04", "1903-21-13 43:40:17 UTC-04", CURLE_OK },
     89   { "19032113434017.01-01:10", "1903-21-13 43:40:17.01 UTC-01:10", CURLE_OK },
     90   };
     91 
     92   size_t i;
     93   struct dynbuf dbuf;
     94   bool all_ok = TRUE;
     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(test_specs); ++i) {
    104     if(!do_test(&test_specs[i], i, &dbuf))
    105       all_ok = FALSE;
    106   }
    107   fail_unless(all_ok, "some tests of Curl_x509_GTime2str() 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_unit1656(char *arg)
    118 {
    119   UNITTEST_BEGIN_SIMPLE
    120   puts("not tested since Curl_x509_GTime2str() is not built in");
    121   UNITTEST_END_SIMPLE
    122 }
    123 
    124 #endif