2
0

cmp_asn_test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2007-2019 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 "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. ASN1_INTEGER *asn1integer = ASN1_INTEGER_new();
  37. ASN1_INTEGER_set(asn1integer, 77);
  38. if (!TEST_int_eq(77, ossl_cmp_asn1_get_int(asn1integer)))
  39. return 0;
  40. ASN1_INTEGER_free(asn1integer);
  41. return 1;
  42. }
  43. static int test_cmp_asn1_get_int(void)
  44. {
  45. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  46. fixture->expected = 1;
  47. EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down);
  48. return result;
  49. }
  50. static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE *
  51. fixture)
  52. {
  53. if (!TEST_int_eq(fixture->expected,
  54. ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string,
  55. fixture->src_string)))
  56. return 0;
  57. if (fixture->expected != 0)
  58. return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string,
  59. fixture->src_string));
  60. return 1;
  61. }
  62. static int test_ASN1_OCTET_STRING_set(void)
  63. {
  64. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  65. fixture->expected = 1;
  66. if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new())
  67. || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
  68. || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
  69. sizeof(rand_data)))) {
  70. tear_down(fixture);
  71. fixture = NULL;
  72. }
  73. EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
  74. return result;
  75. }
  76. static int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
  77. {
  78. SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
  79. fixture->expected = 1;
  80. if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
  81. || !(fixture->tgt_string = fixture->src_string)
  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. void cleanup_tests(void)
  91. {
  92. return;
  93. }
  94. int setup_tests(void)
  95. {
  96. RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
  97. /* ASN.1 related tests */
  98. ADD_TEST(test_cmp_asn1_get_int);
  99. ADD_TEST(test_ASN1_OCTET_STRING_set);
  100. ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src);
  101. /*
  102. * TODO make sure that total number of tests (here currently 24) is shown,
  103. * also for other cmp_*text.c. Currently the test drivers always show 1.
  104. */
  105. return 1;
  106. }