cmp_status_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include "helpers/cmp_testlib.h"
  12. typedef struct test_fixture {
  13. const char *test_case_name;
  14. int pkistatus;
  15. const char *str; /* Not freed by tear_down */
  16. const char *text; /* Not freed by tear_down */
  17. int pkifailure;
  18. } CMP_STATUS_TEST_FIXTURE;
  19. static CMP_STATUS_TEST_FIXTURE *set_up(const char *const test_case_name)
  20. {
  21. CMP_STATUS_TEST_FIXTURE *fixture;
  22. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  23. return NULL;
  24. fixture->test_case_name = test_case_name;
  25. return fixture;
  26. }
  27. static void tear_down(CMP_STATUS_TEST_FIXTURE *fixture)
  28. {
  29. OPENSSL_free(fixture);
  30. }
  31. /*
  32. * Tests PKIStatusInfo creation and get-functions
  33. */
  34. static int execute_PKISI_test(CMP_STATUS_TEST_FIXTURE *fixture)
  35. {
  36. OSSL_CMP_PKISI *si = NULL;
  37. int status;
  38. ASN1_UTF8STRING *statusString = NULL;
  39. int res = 0, i;
  40. if (!TEST_ptr(si = OSSL_CMP_STATUSINFO_new(fixture->pkistatus,
  41. fixture->pkifailure,
  42. fixture->text)))
  43. goto end;
  44. status = ossl_cmp_pkisi_get_status(si);
  45. if (!TEST_int_eq(fixture->pkistatus, status)
  46. || !TEST_str_eq(fixture->str, ossl_cmp_PKIStatus_to_string(status)))
  47. goto end;
  48. if (!TEST_ptr(statusString =
  49. sk_ASN1_UTF8STRING_value(ossl_cmp_pkisi_get0_statusString(si),
  50. 0))
  51. || !TEST_mem_eq(fixture->text, strlen(fixture->text),
  52. (char *)statusString->data, statusString->length))
  53. goto end;
  54. if (!TEST_int_eq(fixture->pkifailure,
  55. ossl_cmp_pkisi_get_pkifailureinfo(si)))
  56. goto end;
  57. for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++)
  58. if (!TEST_int_eq((fixture->pkifailure >> i) & 1,
  59. ossl_cmp_pkisi_check_pkifailureinfo(si, i)))
  60. goto end;
  61. res = 1;
  62. end:
  63. OSSL_CMP_PKISI_free(si);
  64. return res;
  65. }
  66. static int test_PKISI(void)
  67. {
  68. SETUP_TEST_FIXTURE(CMP_STATUS_TEST_FIXTURE, set_up);
  69. fixture->pkistatus = OSSL_CMP_PKISTATUS_revocationNotification;
  70. fixture->str = "PKIStatus: revocation notification - a revocation of the cert has occurred";
  71. fixture->text = "this is an additional text describing the failure";
  72. fixture->pkifailure = OSSL_CMP_CTX_FAILINFO_unsupportedVersion |
  73. OSSL_CMP_CTX_FAILINFO_badDataFormat;
  74. EXECUTE_TEST(execute_PKISI_test, tear_down);
  75. return result;
  76. }
  77. void cleanup_tests(void)
  78. {
  79. return;
  80. }
  81. int setup_tests(void)
  82. {
  83. /*-
  84. * this tests all of:
  85. * OSSL_CMP_STATUSINFO_new()
  86. * ossl_cmp_pkisi_get_status()
  87. * ossl_cmp_PKIStatus_to_string()
  88. * ossl_cmp_pkisi_get0_statusString()
  89. * ossl_cmp_pkisi_get_pkifailureinfo()
  90. * ossl_cmp_pkisi_check_pkifailureinfo()
  91. */
  92. ADD_TEST(test_PKISI);
  93. return 1;
  94. }