trace_api_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/trace.h>
  10. #include "testutil.h"
  11. static int test_trace_categories(void)
  12. {
  13. int cat_num;
  14. for (cat_num = -1; cat_num <= OSSL_TRACE_CATEGORY_NUM + 1; ++cat_num) {
  15. const char *cat_name = OSSL_trace_get_category_name(cat_num);
  16. int is_cat_name_eq = 0;
  17. int ret_cat_num;
  18. int expected_ret;
  19. switch (cat_num) {
  20. #define CASE(name) \
  21. case OSSL_TRACE_CATEGORY_##name: \
  22. is_cat_name_eq = TEST_str_eq(cat_name, #name); \
  23. break
  24. CASE(ALL);
  25. CASE(TRACE);
  26. CASE(INIT);
  27. CASE(TLS);
  28. CASE(TLS_CIPHER);
  29. CASE(CONF);
  30. #ifndef OPENSSL_NO_ENGINE
  31. CASE(ENGINE_TABLE);
  32. CASE(ENGINE_REF_COUNT);
  33. #endif
  34. CASE(PKCS5V2);
  35. CASE(PKCS12_KEYGEN);
  36. CASE(PKCS12_DECRYPT);
  37. CASE(X509V3_POLICY);
  38. CASE(BN_CTX);
  39. CASE(CMP);
  40. CASE(STORE);
  41. CASE(DECODER);
  42. CASE(ENCODER);
  43. CASE(REF_COUNT);
  44. CASE(HTTP);
  45. #undef CASE
  46. default:
  47. is_cat_name_eq = TEST_ptr_null(cat_name);
  48. break;
  49. }
  50. if (!TEST_true(is_cat_name_eq))
  51. return 0;
  52. ret_cat_num =
  53. OSSL_trace_get_category_num(cat_name);
  54. expected_ret = cat_name != NULL ? cat_num : -1;
  55. if (!TEST_int_eq(expected_ret, ret_cat_num))
  56. return 0;
  57. }
  58. return 1;
  59. }
  60. #ifndef OPENSSL_NO_TRACE
  61. static void put_trace_output(void)
  62. {
  63. OSSL_TRACE_BEGIN(TLS) {
  64. BIO_printf(trc_out, "Hello World\n");
  65. BIO_printf(trc_out, "Good Bye Universe\n");
  66. } OSSL_TRACE_END(TLS);
  67. }
  68. static int test_trace_channel(void)
  69. {
  70. static const char expected[] = "xyz-\nHello World\nGood Bye Universe\n-abc\n";
  71. static const char expected_len = sizeof(expected) - 1;
  72. BIO *bio = NULL;
  73. char *p_buf = NULL;
  74. long len = 0;
  75. int ret = 0;
  76. bio = BIO_new(BIO_s_mem());
  77. if (!TEST_ptr(bio))
  78. goto end;
  79. if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_TLS, bio), 1))
  80. goto end;
  81. if (!TEST_true(OSSL_trace_enabled(OSSL_TRACE_CATEGORY_TLS)))
  82. goto end;
  83. if (!TEST_int_eq(OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_TLS, "xyz-"), 1))
  84. goto end;
  85. if (!TEST_int_eq(OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_TLS, "-abc"), 1))
  86. goto end;
  87. put_trace_output();
  88. len = BIO_get_mem_data(bio, &p_buf);
  89. if (!TEST_strn2_eq(p_buf, len, expected, expected_len))
  90. goto end;
  91. if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_TLS, NULL), 1))
  92. goto end;
  93. bio = NULL;
  94. ret = 1;
  95. end:
  96. BIO_free(bio);
  97. return ret;
  98. }
  99. #endif
  100. OPT_TEST_DECLARE_USAGE("\n")
  101. int setup_tests(void)
  102. {
  103. if (!test_skip_common_options()) {
  104. TEST_error("Error parsing test options\n");
  105. return 0;
  106. }
  107. ADD_TEST(test_trace_categories);
  108. #ifndef OPENSSL_NO_TRACE
  109. ADD_TEST(test_trace_channel);
  110. #endif
  111. return 1;
  112. }
  113. void cleanup_tests(void)
  114. {
  115. }