cmp_asn_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2007-2023 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. static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
  13. typedef struct test_fixture {
  14. const char *test_case_name;
  15. int expected;
  16. ASN1_OCTET_STRING *src_string;
  17. ASN1_OCTET_STRING *tgt_string;
  18. } CMP_ASN_TEST_FIXTURE;
  19. static CMP_ASN_TEST_FIXTURE *set_up(const char *const test_case_name)
  20. {
  21. CMP_ASN_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_ASN_TEST_FIXTURE *fixture)
  28. {
  29. ASN1_OCTET_STRING_free(fixture->src_string);
  30. if (fixture->tgt_string != fixture->src_string)
  31. ASN1_OCTET_STRING_free(fixture->tgt_string);
  32. OPENSSL_free(fixture);
  33. }
  34. static int execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE *fixture)
  35. {
  36. int res = 0;
  37. ASN1_INTEGER *asn1integer = ASN1_INTEGER_new();
  38. const int good_int = 77;
  39. const int64_t max_int = INT_MAX;
  40. if (!TEST_ptr(asn1integer))
  41. return res;
  42. if (!TEST_true(ASN1_INTEGER_set(asn1integer, good_int))) {
  43. ASN1_INTEGER_free(asn1integer);
  44. return 0;
  45. }
  46. res = TEST_int_eq(good_int, ossl_cmp_asn1_get_int(asn1integer));
  47. if (res == 0)
  48. goto err;
  49. res = 0;
  50. if (!TEST_true(ASN1_INTEGER_set_int64(asn1integer, max_int + 1)))
  51. goto err;
  52. res = TEST_int_eq(-2, ossl_cmp_asn1_get_int(asn1integer));
  53. err:
  54. ASN1_INTEGER_free(asn1integer);
  55. return res;
  56. }
  57. static int test_cmp_asn1_get_int(void)
  58. {
  59. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  60. fixture->expected = 1;
  61. EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down);
  62. return result;
  63. }
  64. static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE *
  65. fixture)
  66. {
  67. if (!TEST_int_eq(fixture->expected,
  68. ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string,
  69. fixture->src_string)))
  70. return 0;
  71. if (fixture->expected != 0)
  72. return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string,
  73. fixture->src_string));
  74. return 1;
  75. }
  76. static int test_ASN1_OCTET_STRING_set(void)
  77. {
  78. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  79. fixture->expected = 1;
  80. if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new())
  81. || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
  82. || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
  83. sizeof(rand_data)))) {
  84. tear_down(fixture);
  85. fixture = NULL;
  86. }
  87. EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
  88. return result;
  89. }
  90. static int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
  91. {
  92. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  93. fixture->expected = 1;
  94. if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
  95. || !(fixture->tgt_string = fixture->src_string)
  96. || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
  97. sizeof(rand_data)))) {
  98. tear_down(fixture);
  99. fixture = NULL;
  100. }
  101. EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
  102. return result;
  103. }
  104. void cleanup_tests(void)
  105. {
  106. return;
  107. }
  108. int setup_tests(void)
  109. {
  110. RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
  111. /* ASN.1 related tests */
  112. ADD_TEST(test_cmp_asn1_get_int);
  113. ADD_TEST(test_ASN1_OCTET_STRING_set);
  114. ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src);
  115. return 1;
  116. }