log_formatter_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "config.h"
  15. #include <gtest/gtest.h>
  16. #include <util/unittests/resource.h>
  17. #include <log/log_formatter.h>
  18. #include <log/logger_level.h>
  19. #include <vector>
  20. #include <string>
  21. using namespace std;
  22. namespace {
  23. class FormatterTest : public ::testing::Test {
  24. protected:
  25. typedef pair<isc::log::Severity, string> Output;
  26. typedef isc::log::Formatter<FormatterTest> Formatter;
  27. vector<Output> outputs;
  28. public:
  29. void output(const isc::log::Severity& prefix, const string& message) {
  30. outputs.push_back(Output(prefix, message));
  31. }
  32. // Just shortcut for new string
  33. string* s(const char* text) {
  34. return (new string(text));
  35. }
  36. };
  37. // Create an inactive formatter and check it doesn't produce any output
  38. TEST_F(FormatterTest, inactive) {
  39. Formatter();
  40. EXPECT_EQ(0, outputs.size());
  41. }
  42. // Create an active formatter and check it produces output. Does no arg
  43. // substitution yet
  44. TEST_F(FormatterTest, active) {
  45. Formatter(isc::log::INFO, s("Text of message"), this);
  46. ASSERT_EQ(1, outputs.size());
  47. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  48. EXPECT_EQ("Text of message", outputs[0].second);
  49. }
  50. // No output even when we have an arg on the inactive formatter
  51. TEST_F(FormatterTest, inactiveArg) {
  52. Formatter().arg("Hello");
  53. EXPECT_EQ(0, outputs.size());
  54. }
  55. // Create an active formatter and replace a placeholder with string
  56. TEST_F(FormatterTest, stringArg) {
  57. {
  58. SCOPED_TRACE("C++ string");
  59. Formatter(isc::log::INFO, s("Hello %1"), this).arg(string("World"));
  60. ASSERT_EQ(1, outputs.size());
  61. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  62. EXPECT_EQ("Hello World", outputs[0].second);
  63. }
  64. {
  65. SCOPED_TRACE("C++ string");
  66. Formatter(isc::log::INFO, s("Hello %1"), this).arg(string("Internet"));
  67. ASSERT_EQ(2, outputs.size());
  68. EXPECT_EQ(isc::log::INFO, outputs[1].first);
  69. EXPECT_EQ("Hello Internet", outputs[1].second);
  70. }
  71. }
  72. // Can convert to string
  73. TEST_F(FormatterTest, intArg) {
  74. Formatter(isc::log::INFO, s("The answer is %1"), this).arg(42);
  75. ASSERT_EQ(1, outputs.size());
  76. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  77. EXPECT_EQ("The answer is 42", outputs[0].second);
  78. }
  79. // Can use multiple arguments at different places
  80. TEST_F(FormatterTest, multiArg) {
  81. Formatter(isc::log::INFO, s("The %2 are %1"), this).arg("switched").
  82. arg("arguments");
  83. ASSERT_EQ(1, outputs.size());
  84. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  85. EXPECT_EQ("The arguments are switched", outputs[0].second);
  86. }
  87. #ifdef ENABLE_LOGGER_CHECKS
  88. TEST_F(FormatterTest, mismatchedPlaceholders) {
  89. // Throws MismatchedPlaceholders exception if the placeholder is missing
  90. // for a supplied argument.
  91. EXPECT_THROW(Formatter(isc::log::INFO, s("Missing the second %1"), this).
  92. arg("argument").arg("missing"),
  93. isc::log::MismatchedPlaceholders);
  94. #ifdef EXPECT_DEATH
  95. // Likewise, if there's a redundant placeholder (or missing argument), the
  96. // check detects it and aborts the program. Due to the restriction of the
  97. // current implementation, it doesn't throw.
  98. EXPECT_DEATH({
  99. isc::util::unittests::dontCreateCoreDumps();
  100. Formatter(isc::log::INFO, s("Too many arguments in %1 %2"), this).
  101. arg("only one");
  102. }, ".*");
  103. // Mixed case of above two: the exception will be thrown due to the missing
  104. // placeholder, but before even it's caught the program will be aborted
  105. // due to the unused placeholder as a result of the exception.
  106. EXPECT_DEATH({
  107. isc::util::unittests::dontCreateCoreDumps();
  108. Formatter(isc::log::INFO, s("Missing the first %2"), this).
  109. arg("missing").arg("argument");
  110. }, ".*");
  111. #endif /* EXPECT_DEATH */
  112. }
  113. #else
  114. // If logger checks are not enabled, nothing is thrown
  115. TEST_F(FormatterTest, mismatchedPlaceholders) {
  116. Formatter(isc::log::INFO, s("Missing the second %1"), this).
  117. arg("argument").arg("missing");
  118. ASSERT_EQ(1, outputs.size());
  119. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  120. EXPECT_EQ("Missing the second argument "
  121. "@@Missing placeholder %2 for 'missing'@@", outputs[0].second);
  122. EXPECT_NO_THROW(Formatter(isc::log::INFO,
  123. s("Too many arguments in %1 %2"), this).
  124. arg("only one"));
  125. ASSERT_EQ(2, outputs.size());
  126. EXPECT_EQ(isc::log::INFO, outputs[1].first);
  127. EXPECT_EQ("Too many arguments in only one %2 "
  128. "@@Excess logger placeholders still exist@@",
  129. outputs[1].second);
  130. EXPECT_NO_THROW(Formatter(isc::log::INFO, s("Missing the first %2"), this).
  131. arg("missing").arg("argument"));
  132. ASSERT_EQ(3, outputs.size());
  133. EXPECT_EQ(isc::log::INFO, outputs[2].first);
  134. EXPECT_EQ("Missing the first argument "
  135. "@@Missing placeholder %1 for 'missing'@@", outputs[2].second);
  136. }
  137. #endif /* ENABLE_LOGGER_CHECKS */
  138. // Can replace multiple placeholders
  139. TEST_F(FormatterTest, multiPlaceholder) {
  140. Formatter(isc::log::INFO, s("The %1 is the %1"), this).
  141. arg("first rule of tautology club");
  142. ASSERT_EQ(1, outputs.size());
  143. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  144. EXPECT_EQ("The first rule of tautology club is "
  145. "the first rule of tautology club", outputs[0].second);
  146. }
  147. // Test we can cope with replacement containing the placeholder
  148. TEST_F(FormatterTest, noRecurse) {
  149. // If we recurse, this will probably eat all the memory and crash
  150. Formatter(isc::log::INFO, s("%1"), this).arg("%1 %1");
  151. ASSERT_EQ(1, outputs.size());
  152. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  153. EXPECT_EQ("%1 %1", outputs[0].second);
  154. }
  155. }