log_formatter_unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // Test the .deactivate() method
  73. TEST_F(FormatterTest, deactivate) {
  74. Formatter(isc::log::INFO, s("Text of message"), this).deactivate();
  75. // If there was no .deactivate, it should have output it.
  76. // But not now.
  77. ASSERT_EQ(0, outputs.size());
  78. }
  79. // Can convert to string
  80. TEST_F(FormatterTest, intArg) {
  81. Formatter(isc::log::INFO, s("The answer is %1"), this).arg(42);
  82. ASSERT_EQ(1, outputs.size());
  83. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  84. EXPECT_EQ("The answer is 42", outputs[0].second);
  85. }
  86. // Can use multiple arguments at different places
  87. TEST_F(FormatterTest, multiArg) {
  88. Formatter(isc::log::INFO, s("The %2 are %1"), this).arg("switched").
  89. arg("arguments");
  90. ASSERT_EQ(1, outputs.size());
  91. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  92. EXPECT_EQ("The arguments are switched", outputs[0].second);
  93. }
  94. #ifdef ENABLE_LOGGER_CHECKS
  95. TEST_F(FormatterTest, mismatchedPlaceholders) {
  96. // Throws MismatchedPlaceholders exception if the placeholder is missing
  97. // for a supplied argument.
  98. EXPECT_THROW(Formatter(isc::log::INFO, s("Missing the second %1"), this).
  99. arg("argument").arg("missing"),
  100. isc::log::MismatchedPlaceholders);
  101. #ifdef EXPECT_DEATH
  102. // Likewise, if there's a redundant placeholder (or missing argument), the
  103. // check detects it and aborts the program. Due to the restriction of the
  104. // current implementation, it doesn't throw.
  105. EXPECT_DEATH({
  106. isc::util::unittests::dontCreateCoreDumps();
  107. Formatter(isc::log::INFO, s("Too many arguments in %1 %2"), this).
  108. arg("only one");
  109. }, ".*");
  110. #endif /* EXPECT_DEATH */
  111. // Mixed case of above two: the exception will be thrown due to the missing
  112. // placeholder. The other check is disabled due to that.
  113. EXPECT_THROW(Formatter(isc::log::INFO, s("Missing the first %2"), this).
  114. arg("missing").arg("argument"),
  115. isc::log::MismatchedPlaceholders);
  116. }
  117. #else
  118. // If logger checks are not enabled, nothing is thrown
  119. TEST_F(FormatterTest, mismatchedPlaceholders) {
  120. Formatter(isc::log::INFO, s("Missing the second %1"), this).
  121. arg("argument").arg("missing");
  122. ASSERT_EQ(1, outputs.size());
  123. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  124. EXPECT_EQ("Missing the second argument "
  125. "@@Missing placeholder %2 for 'missing'@@", outputs[0].second);
  126. EXPECT_NO_THROW(Formatter(isc::log::INFO,
  127. s("Too many arguments in %1 %2"), this).
  128. arg("only one"));
  129. ASSERT_EQ(2, outputs.size());
  130. EXPECT_EQ(isc::log::INFO, outputs[1].first);
  131. EXPECT_EQ("Too many arguments in only one %2 "
  132. "@@Excess logger placeholders still exist@@",
  133. outputs[1].second);
  134. EXPECT_NO_THROW(Formatter(isc::log::INFO, s("Missing the first %2"), this).
  135. arg("missing").arg("argument"));
  136. ASSERT_EQ(3, outputs.size());
  137. EXPECT_EQ(isc::log::INFO, outputs[2].first);
  138. EXPECT_EQ("Missing the first argument "
  139. "@@Missing placeholder %1 for 'missing'@@", outputs[2].second);
  140. }
  141. #endif /* ENABLE_LOGGER_CHECKS */
  142. // Can replace multiple placeholders
  143. TEST_F(FormatterTest, multiPlaceholder) {
  144. Formatter(isc::log::INFO, s("The %1 is the %1"), this).
  145. arg("first rule of tautology club");
  146. ASSERT_EQ(1, outputs.size());
  147. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  148. EXPECT_EQ("The first rule of tautology club is "
  149. "the first rule of tautology club", outputs[0].second);
  150. }
  151. // Test we can cope with replacement containing the placeholder
  152. TEST_F(FormatterTest, noRecurse) {
  153. // If we recurse, this will probably eat all the memory and crash
  154. Formatter(isc::log::INFO, s("%1"), this).arg("%1 %1");
  155. ASSERT_EQ(1, outputs.size());
  156. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  157. EXPECT_EQ("%1 %1", outputs[0].second);
  158. }
  159. }