test_censoring.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <zf_log.c>
  2. #include <zf_test.h>
  3. static unsigned g_logged = 0;
  4. static void mock_output_callback(const zf_log_message *msg, void *arg)
  5. {
  6. (void)msg; (void)arg;
  7. ++g_logged;
  8. }
  9. static unsigned was_logged()
  10. {
  11. const unsigned logged = g_logged;
  12. g_logged = 0;
  13. return logged;
  14. }
  15. static void some_function_0()
  16. {
  17. ++g_logged;
  18. }
  19. static void some_function_1(const unsigned d)
  20. {
  21. assert(0 < d);
  22. g_logged += d;
  23. }
  24. #if TEST_LOG_SECRETS
  25. #define EXPECTED_LINES(n) TEST_VERIFY_EQUAL(was_logged(), (n))
  26. #else
  27. #define EXPECTED_LINES(n) TEST_VERIFY_EQUAL(was_logged(), 0)
  28. #endif
  29. static void test_censoring()
  30. {
  31. const char name[] = "Orion";
  32. const char address[] = "Space";
  33. const char cipher[] = "Secret";
  34. const zf_log_spec spec = {ZF_LOG_GLOBAL_FORMAT, ZF_LOG_GLOBAL_OUTPUT};
  35. #if ZF_LOG_SECRETS
  36. ZF_LOGI("Customer name: %s", name);
  37. ZF_LOGI("Customer address: %s", address);
  38. #endif
  39. EXPECTED_LINES(2);
  40. ZF_LOG_SECRET(ZF_LOGI("Customer name: %s", name));
  41. EXPECTED_LINES(1);
  42. ZF_LOG_SECRET(ZF_LOGI_MEM(cipher, sizeof(cipher), "Customer cipher:"));
  43. EXPECTED_LINES(2);
  44. ZF_LOG_SECRET(ZF_LOGI_AUX(&spec, "Customer address: %s", address));
  45. EXPECTED_LINES(1);
  46. ZF_LOG_SECRET(ZF_LOGI_MEM_AUX(&spec, cipher, sizeof(cipher), "Customer cipher:"));
  47. EXPECTED_LINES(2);
  48. ZF_LOG_SECRET(some_function_0());
  49. EXPECTED_LINES(1);
  50. ZF_LOG_SECRET(some_function_1(42));
  51. EXPECTED_LINES(42);
  52. ZF_LOGI("Must always log this");
  53. TEST_VERIFY_EQUAL(was_logged(), 1);
  54. }
  55. int main(int argc, char *argv[])
  56. {
  57. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, mock_output_callback);
  58. TEST_RUNNER_CREATE(argc, argv);
  59. TEST_EXECUTE(test_censoring());
  60. return TEST_RUNNER_EXIT_CODE();
  61. }