xref: /curl/tests/unit/unit1656.c (revision 27959ecc)
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 "curlcheck.h"
25 
26 #include "vtls/x509asn1.h"
27 
unit_setup(void)28 static CURLcode unit_setup(void)
29 {
30   return CURLE_OK;
31 }
32 
unit_stop(void)33 static void unit_stop(void)
34 {
35 
36 }
37 
38 #if defined(USE_GNUTLS) || defined(USE_SCHANNEL) || defined(USE_SECTRANSP) || \
39   defined(USE_MBEDTLS)
40 
41 #ifndef ARRAYSIZE
42 #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
43 #endif
44 
45 struct test_spec {
46   const char *input;
47   const char *exp_output;
48   CURLcode exp_result;
49 };
50 
51 static struct test_spec test_specs[] = {
52   { "190321134340", "1903-21-13 43:40:00", CURLE_OK },
53   { "", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
54   { "WTF", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
55   { "0WTF", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
56   { "19032113434", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
57   { "19032113434WTF", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
58   { "190321134340.", NULL, CURLE_BAD_FUNCTION_ARGUMENT },
59   { "190321134340.1", "1903-21-13 43:40:00.1", CURLE_OK },
60   { "19032113434017.0", "1903-21-13 43:40:17", CURLE_OK },
61   { "19032113434017.01", "1903-21-13 43:40:17.01", CURLE_OK },
62   { "19032113434003.001", "1903-21-13 43:40:03.001", CURLE_OK },
63   { "19032113434003.090", "1903-21-13 43:40:03.09", CURLE_OK },
64   { "190321134340Z", "1903-21-13 43:40:00 GMT", CURLE_OK },
65   { "19032113434017.0Z", "1903-21-13 43:40:17 GMT", CURLE_OK },
66   { "19032113434017.01Z", "1903-21-13 43:40:17.01 GMT", CURLE_OK },
67   { "19032113434003.001Z", "1903-21-13 43:40:03.001 GMT", CURLE_OK },
68   { "19032113434003.090Z", "1903-21-13 43:40:03.09 GMT", CURLE_OK },
69   { "190321134340CET", "1903-21-13 43:40:00 CET", CURLE_OK },
70   { "19032113434017.0CET", "1903-21-13 43:40:17 CET", CURLE_OK },
71   { "19032113434017.01CET", "1903-21-13 43:40:17.01 CET", CURLE_OK },
72   { "190321134340+02:30", "1903-21-13 43:40:00 UTC+02:30", CURLE_OK },
73   { "19032113434017.0+02:30", "1903-21-13 43:40:17 UTC+02:30", CURLE_OK },
74   { "19032113434017.01+02:30", "1903-21-13 43:40:17.01 UTC+02:30", CURLE_OK },
75   { "190321134340-3", "1903-21-13 43:40:00 UTC-3", CURLE_OK },
76   { "19032113434017.0-04", "1903-21-13 43:40:17 UTC-04", CURLE_OK },
77   { "19032113434017.01-01:10", "1903-21-13 43:40:17.01 UTC-01:10", CURLE_OK },
78 };
79 
do_test(struct test_spec * spec,size_t i,struct dynbuf * dbuf)80 static bool do_test(struct test_spec *spec, size_t i, struct dynbuf *dbuf)
81 {
82   CURLcode result;
83   const char *in = spec->input;
84 
85   Curl_dyn_reset(dbuf);
86   result = Curl_x509_GTime2str(dbuf, in, in + strlen(in));
87   if(result != spec->exp_result) {
88     fprintf(stderr, "test %zu: expect result %d, got %d\n",
89             i, spec->exp_result, result);
90     return FALSE;
91   }
92   else if(!result && strcmp(spec->exp_output, Curl_dyn_ptr(dbuf))) {
93     fprintf(stderr, "test %zu: input '%s', expected output '%s', got '%s'\n",
94             i, in, spec->exp_output, Curl_dyn_ptr(dbuf));
95     return FALSE;
96   }
97 
98   return TRUE;
99 }
100 
101 UNITTEST_START
102 {
103   size_t i;
104   struct dynbuf dbuf;
105   bool all_ok = TRUE;
106 
107   Curl_dyn_init(&dbuf, 32*1024);
108 
109   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
110     fprintf(stderr, "curl_global_init() failed\n");
111     return TEST_ERR_MAJOR_BAD;
112   }
113 
114   for(i = 0; i < ARRAYSIZE(test_specs); ++i) {
115     if(!do_test(&test_specs[i], i, &dbuf))
116       all_ok = FALSE;
117   }
118   fail_unless(all_ok, "some tests of Curl_x509_GTime2str() fails");
119 
120   Curl_dyn_free(&dbuf);
121   curl_global_cleanup();
122 }
123 UNITTEST_STOP
124 
125 #else
126 
127 UNITTEST_START
128 {
129   puts("not tested since Curl_x509_GTime2str() is not built-in");
130 }
131 UNITTEST_STOP
132 
133 #endif
134