trace_api_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2022-2023 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. # define OSSL_START "xyz-"
  60. # define OSSL_HELLO "Hello World\n"
  61. /* OSSL_STR80 must have length OSSL_TRACE_STRING_MAX */
  62. # define OSSL_STR80 "1234567890123456789012345678901234567890123456789012345678901234567890123456789\n"
  63. # define OSSL_STR81 (OSSL_STR80"x")
  64. # define OSSL_CTRL "A\xfe\nB"
  65. # define OSSL_MASKED "A \nB"
  66. # define OSSL_BYE "Good Bye Universe\n"
  67. # define OSSL_END "-abc"
  68. # define trace_string(text, full, str) \
  69. OSSL_trace_string(trc_out, text, full, (unsigned char *)(str), strlen(str))
  70. static int put_trace_output(void)
  71. {
  72. int res = 1;
  73. OSSL_TRACE_BEGIN(HTTP) {
  74. res = TEST_int_eq(BIO_printf(trc_out, OSSL_HELLO), strlen(OSSL_HELLO));
  75. res += TEST_int_eq(trace_string(0, 0, OSSL_STR80), strlen(OSSL_STR80));
  76. res += TEST_int_eq(trace_string(0, 0, OSSL_STR81), strlen(OSSL_STR80));
  77. res += TEST_int_eq(trace_string(1, 1, OSSL_CTRL), strlen(OSSL_CTRL));
  78. res += TEST_int_eq(trace_string(0, 1, OSSL_MASKED), strlen(OSSL_MASKED)
  79. + 1); /* newline added */
  80. res += TEST_int_eq(BIO_printf(trc_out, OSSL_BYE), strlen(OSSL_BYE));
  81. res = res == 6;
  82. /* not using '&&' but '+' to catch potentially multiple test failures */
  83. } OSSL_TRACE_END(HTTP);
  84. return res;
  85. }
  86. static int test_trace_channel(void)
  87. {
  88. static const char expected[] =
  89. OSSL_START"\n" OSSL_HELLO
  90. OSSL_STR80 "[len 81 limited to 80]: "OSSL_STR80
  91. OSSL_CTRL OSSL_MASKED"\n" OSSL_BYE OSSL_END"\n";
  92. static const size_t expected_len = sizeof(expected) - 1;
  93. BIO *bio = NULL;
  94. char *p_buf = NULL;
  95. long len = 0;
  96. int ret = 0;
  97. bio = BIO_new(BIO_s_mem());
  98. if (!TEST_ptr(bio))
  99. goto end;
  100. if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_HTTP, bio), 1)) {
  101. BIO_free(bio);
  102. goto end;
  103. }
  104. if (!TEST_true(OSSL_trace_enabled(OSSL_TRACE_CATEGORY_HTTP)))
  105. goto end;
  106. if (!TEST_int_eq(OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_HTTP,
  107. OSSL_START), 1))
  108. goto end;
  109. if (!TEST_int_eq(OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_HTTP,
  110. OSSL_END), 1))
  111. goto end;
  112. ret = put_trace_output();
  113. len = BIO_get_mem_data(bio, &p_buf);
  114. if (!TEST_strn2_eq(p_buf, len, expected, expected_len))
  115. ret = 0;
  116. ret = TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_HTTP, NULL), 1)
  117. && ret;
  118. end:
  119. return ret;
  120. }
  121. static int trace_cb_failure;
  122. static int trace_cb_called;
  123. static size_t trace_cb(const char *buffer, size_t count,
  124. int category, int cmd, void *data)
  125. {
  126. trace_cb_called = 1;
  127. if (!TEST_true(category == OSSL_TRACE_CATEGORY_TRACE))
  128. trace_cb_failure = 1;
  129. return count;
  130. }
  131. static int test_trace_callback(void)
  132. {
  133. int ret = 0;
  134. if (!TEST_true(OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_TRACE, trace_cb,
  135. NULL)))
  136. goto end;
  137. put_trace_output();
  138. if (!TEST_false(trace_cb_failure) || !TEST_true(trace_cb_called))
  139. goto end;
  140. ret = 1;
  141. end:
  142. return ret;
  143. }
  144. #endif
  145. OPT_TEST_DECLARE_USAGE("\n")
  146. int setup_tests(void)
  147. {
  148. if (!test_skip_common_options()) {
  149. TEST_error("Error parsing test options\n");
  150. return 0;
  151. }
  152. ADD_TEST(test_trace_categories);
  153. #ifndef OPENSSL_NO_TRACE
  154. ADD_TEST(test_trace_channel);
  155. ADD_TEST(test_trace_callback);
  156. #endif
  157. return 1;
  158. }
  159. void cleanup_tests(void)
  160. {
  161. }