unit1656.c 4.3 KB

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