cmp_asn_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. 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;
  37. ASN1_INTEGER *asn1integer = ASN1_INTEGER_new();
  38. if (!TEST_ptr(asn1integer))
  39. return 0;
  40. ASN1_INTEGER_set(asn1integer, 77);
  41. res = TEST_int_eq(77, ossl_cmp_asn1_get_int(asn1integer));
  42. ASN1_INTEGER_free(asn1integer);
  43. return res;
  44. }
  45. static int test_cmp_asn1_get_int(void)
  46. {
  47. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  48. fixture->expected = 1;
  49. EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down);
  50. return result;
  51. }
  52. static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE *
  53. fixture)
  54. {
  55. if (!TEST_int_eq(fixture->expected,
  56. ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string,
  57. fixture->src_string)))
  58. return 0;
  59. if (fixture->expected != 0)
  60. return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string,
  61. fixture->src_string));
  62. return 1;
  63. }
  64. static int test_ASN1_OCTET_STRING_set(void)
  65. {
  66. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  67. fixture->expected = 1;
  68. if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new())
  69. || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
  70. || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
  71. sizeof(rand_data)))) {
  72. tear_down(fixture);
  73. fixture = NULL;
  74. }
  75. EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
  76. return result;
  77. }
  78. static int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
  79. {
  80. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  81. fixture->expected = 1;
  82. if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
  83. || !(fixture->tgt_string = fixture->src_string)
  84. || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
  85. sizeof(rand_data)))) {
  86. tear_down(fixture);
  87. fixture = NULL;
  88. }
  89. EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
  90. return result;
  91. }
  92. void cleanup_tests(void)
  93. {
  94. return;
  95. }
  96. int setup_tests(void)
  97. {
  98. RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
  99. /* ASN.1 related tests */
  100. ADD_TEST(test_cmp_asn1_get_int);
  101. ADD_TEST(test_ASN1_OCTET_STRING_set);
  102. ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src);
  103. return 1;
  104. }