trace_api_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. CASE(ENGINE_TABLE);
  31. CASE(ENGINE_REF_COUNT);
  32. CASE(PKCS5V2);
  33. CASE(PKCS12_KEYGEN);
  34. CASE(PKCS12_DECRYPT);
  35. CASE(X509V3_POLICY);
  36. CASE(BN_CTX);
  37. CASE(CMP);
  38. CASE(STORE);
  39. CASE(DECODER);
  40. CASE(ENCODER);
  41. CASE(REF_COUNT);
  42. CASE(HTTP);
  43. #undef CASE
  44. default:
  45. is_cat_name_eq = TEST_ptr_null(cat_name);
  46. break;
  47. }
  48. if (!TEST_true(is_cat_name_eq))
  49. return 0;
  50. ret_cat_num =
  51. OSSL_trace_get_category_num(cat_name);
  52. expected_ret = cat_name != NULL ? cat_num : -1;
  53. if (!TEST_int_eq(expected_ret, ret_cat_num))
  54. return 0;
  55. }
  56. return 1;
  57. }
  58. #ifndef OPENSSL_NO_TRACE
  59. static void put_trace_output(void)
  60. {
  61. OSSL_TRACE_BEGIN(HTTP) {
  62. BIO_printf(trc_out, "Hello World\n");
  63. BIO_printf(trc_out, "Good Bye Universe\n");
  64. } OSSL_TRACE_END(HTTP);
  65. }
  66. static int test_trace_channel(void)
  67. {
  68. static const char expected[] = "xyz-\nHello World\nGood Bye Universe\n-abc\n";
  69. static const char expected_len = sizeof(expected) - 1;
  70. BIO *bio = NULL;
  71. char *p_buf = NULL;
  72. long len = 0;
  73. int ret = 0;
  74. bio = BIO_new(BIO_s_mem());
  75. if (!TEST_ptr(bio))
  76. goto end;
  77. if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_HTTP, bio), 1))
  78. goto end;
  79. if (!TEST_true(OSSL_trace_enabled(OSSL_TRACE_CATEGORY_HTTP)))
  80. goto end;
  81. if (!TEST_int_eq(OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_HTTP, "xyz-"), 1))
  82. goto end;
  83. if (!TEST_int_eq(OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_HTTP, "-abc"), 1))
  84. goto end;
  85. put_trace_output();
  86. len = BIO_get_mem_data(bio, &p_buf);
  87. if (!TEST_strn2_eq(p_buf, len, expected, expected_len))
  88. goto end;
  89. if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_HTTP, NULL), 1))
  90. goto end;
  91. bio = NULL;
  92. ret = 1;
  93. end:
  94. BIO_free(bio);
  95. return ret;
  96. }
  97. static int trace_cb_failure;
  98. static int trace_cb_called;
  99. static size_t trace_cb(const char *buffer, size_t count,
  100. int category, int cmd, void *data)
  101. {
  102. trace_cb_called = 1;
  103. if (!TEST_true(category == OSSL_TRACE_CATEGORY_TRACE))
  104. trace_cb_failure = 1;
  105. return count;
  106. }
  107. static int test_trace_callback(void)
  108. {
  109. int ret = 0;
  110. if (!TEST_true(OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_TRACE, trace_cb,
  111. NULL)))
  112. goto end;
  113. put_trace_output();
  114. if (!TEST_false(trace_cb_failure) || !TEST_true(trace_cb_called))
  115. goto end;
  116. ret = 1;
  117. end:
  118. return ret;
  119. }
  120. #endif
  121. OPT_TEST_DECLARE_USAGE("\n")
  122. int setup_tests(void)
  123. {
  124. if (!test_skip_common_options()) {
  125. TEST_error("Error parsing test options\n");
  126. return 0;
  127. }
  128. ADD_TEST(test_trace_categories);
  129. #ifndef OPENSSL_NO_TRACE
  130. ADD_TEST(test_trace_channel);
  131. ADD_TEST(test_trace_callback);
  132. #endif
  133. return 1;
  134. }
  135. void cleanup_tests(void)
  136. {
  137. }