test-log.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2019 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <assert.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include "log.h"
  39. #define MAX_LINE_LEN 512
  40. static int openlog_called;
  41. static int openlog_facility;
  42. static char vsyslog_buf[MAX_LINE_LEN];
  43. static int vsyslog_priority;
  44. static int vsyslog_called;
  45. static int closelog_called;
  46. void
  47. openlog(const char *ident, int option, int facility)
  48. {
  49. openlog_called = 1;
  50. openlog_facility = facility;
  51. }
  52. void
  53. vsyslog(int priority, const char *format, va_list ap)
  54. {
  55. va_list ap_copy;
  56. int res;
  57. vsyslog_called = 1;
  58. vsyslog_priority = priority;
  59. va_copy(ap_copy, ap);
  60. res = vsnprintf(vsyslog_buf, MAX_LINE_LEN, format, ap_copy);
  61. assert(res < MAX_LINE_LEN && res != -1);
  62. va_end(ap_copy);
  63. }
  64. void
  65. closelog(void)
  66. {
  67. closelog_called = 1;
  68. }
  69. static void
  70. log_check(int priority, const char *msg, int should_be_called, int expected_priority)
  71. {
  72. vsyslog_called = 0;
  73. vsyslog_priority = -1;
  74. vsyslog_buf[0] = '\0';
  75. log(priority, "%s", msg);
  76. if (should_be_called) {
  77. assert(vsyslog_called);
  78. assert(vsyslog_priority == expected_priority);
  79. assert(strcmp(vsyslog_buf, msg) == 0);
  80. } else {
  81. assert(!vsyslog_called);
  82. }
  83. }
  84. int
  85. main(void)
  86. {
  87. openlog_called = 0;
  88. assert(log_init("test", LOG_TARGET_SYSLOG, LOG_DAEMON) == 0);
  89. assert(openlog_called);
  90. assert(openlog_facility == LOG_DAEMON);
  91. log_check(LOG_INFO, "test log info", 1, LOG_INFO);
  92. log_check(LOG_ERR, "test log err", 1, LOG_ERR);
  93. log_check(LOG_DEBUG, "test log debug", 0, LOG_DEBUG);
  94. log_set_debug(1);
  95. log_check(LOG_DEBUG, "test log debug", 1, LOG_DEBUG);
  96. log_set_debug(0);
  97. log_check(LOG_DEBUG, "test log debug", 0, LOG_DEBUG);
  98. log_set_debug(1);
  99. log_set_priority_bump(1);
  100. log_check(LOG_ERR, "test log err", 1, LOG_ERR);
  101. log_check(LOG_DEBUG, "test log debug", 1, LOG_INFO);
  102. log_set_priority_bump(0);
  103. log_check(LOG_ERR, "test log err", 1, LOG_ERR);
  104. log_check(LOG_DEBUG, "test log debug", 1, LOG_DEBUG);
  105. closelog_called = 0;
  106. openlog_called = 0;
  107. log_set_target(LOG_TARGET_SYSLOG, LOG_USER);
  108. assert(openlog_called);
  109. assert(closelog_called);
  110. assert(openlog_facility == LOG_USER);
  111. closelog_called = 0;
  112. log_close();
  113. assert(closelog_called);
  114. return (0);
  115. }