statistics_unittest.cc.pre 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2010 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 <string>
  16. #include <gtest/gtest.h>
  17. #include <boost/bind.hpp>
  18. #include <dns/opcode.h>
  19. #include <dns/rcode.h>
  20. #include <cc/data.h>
  21. #include <cc/session.h>
  22. #include <auth/statistics.h>
  23. #include <auth/statistics_items.h>
  24. #include <dns/tests/unittest_util.h>
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <netdb.h>
  30. using namespace std;
  31. using namespace isc::cc;
  32. using namespace isc::dns;
  33. using namespace isc::data;
  34. using namespace isc::auth::statistics;
  35. namespace {
  36. // ### STATISTICS ITEMS DEFINITION ###
  37. class CountersTest : public ::testing::Test {
  38. protected:
  39. CountersTest() : counters() {}
  40. ~CountersTest() {}
  41. Counters counters;
  42. };
  43. void
  44. flatten(std::map<std::string, int>& flat_map, const std::string& prefix,
  45. const isc::data::ConstElementPtr map_element) {
  46. std::map<std::string, ConstElementPtr> map = map_element->mapValue();
  47. for (std::map<std::string, ConstElementPtr>::const_iterator
  48. i = map.begin(), e = map.end();
  49. i != e;
  50. ++i)
  51. {
  52. switch (i->second->getType()) {
  53. case isc::data::Element::map:
  54. flatten(flat_map, i->first + ".", i->second);
  55. break;
  56. case isc::data::Element::integer:
  57. flat_map[prefix + i->first] = i->second->intValue();
  58. break;
  59. default:
  60. EXPECT_FALSE("Element Parse Error");
  61. }
  62. }
  63. }
  64. // Test if the values of the counters are all zero except for the items
  65. // specified in except_for.
  66. void
  67. checkCountersAllZeroExcept(const isc::data::ConstElementPtr counters,
  68. const std::set<std::string>& except_for) {
  69. std::map<std::string, int> stats_map;
  70. flatten(stats_map, "", counters);
  71. for (std::map<std::string, int>::const_iterator
  72. i = stats_map.begin(), e = stats_map.end();
  73. i != e;
  74. ++i)
  75. {
  76. int expect = 0;
  77. if (except_for.count(i->first) != 0) {
  78. expect = 1;
  79. }
  80. EXPECT_EQ(expect, i->second) << "Expected counter "
  81. << i->first << " = " << expect << ", actual: "
  82. << i->second;
  83. }
  84. }
  85. TEST_F(CountersTest, incrementNormalQuery) {
  86. Message response(Message::RENDER);
  87. QRAttributes qrattrs;
  88. std::set<std::string> expect_nonzero;
  89. expect_nonzero.clear();
  90. checkCountersAllZeroExcept(counters.get()->get("zones")->get("_SERVER_"),
  91. expect_nonzero);
  92. qrattrs.setQueryIPVersion(AF_INET6);
  93. qrattrs.setQueryTransportProtocol(IPPROTO_UDP);
  94. qrattrs.setQueryOpCode(Opcode::QUERY_CODE);
  95. qrattrs.setQueryEDNS(true, false);
  96. qrattrs.setQueryDO(true);
  97. response.setRcode(Rcode::REFUSED());
  98. response.addQuestion(Question(Name("example.com"),
  99. RRClass::IN(), RRType::AAAA()));
  100. counters.inc(qrattrs, response, true);
  101. expect_nonzero.clear();
  102. expect_nonzero.insert("opcode.query");
  103. expect_nonzero.insert("request.v6");
  104. expect_nonzero.insert("request.udp");
  105. expect_nonzero.insert("request.edns0");
  106. expect_nonzero.insert("request.dnssec_ok");
  107. expect_nonzero.insert("responses");
  108. expect_nonzero.insert("qrynoauthans");
  109. expect_nonzero.insert("rcode.refused");
  110. expect_nonzero.insert("authqryrej");
  111. checkCountersAllZeroExcept(counters.get()->get("zones")->get("_SERVER_"),
  112. expect_nonzero);
  113. }
  114. int
  115. countTreeElements(const struct CounterTypeTree* tree) {
  116. int count = 0;
  117. for (int i = 0; tree[i].name != NULL; ++i) {
  118. if (tree[i].sub_tree == NULL) {
  119. ++count;
  120. } else {
  121. count += countTreeElements(tree[i].sub_tree);
  122. }
  123. }
  124. return (count);
  125. }
  126. TEST(StatisticsItemsTest, QRItemNamesCheck) {
  127. EXPECT_EQ(QR_COUNTER_TYPES, countTreeElements(QRCounterTree));
  128. }
  129. }