statistics_unittest.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <gtest/gtest.h>
  16. #include <cc/data.h>
  17. #include <cc/session.h>
  18. #include <auth/statistics.h>
  19. #include <dns/tests/unittest_util.h>
  20. using isc::UnitTestUtil;
  21. using namespace std;
  22. using namespace isc::cc;
  23. using namespace isc::dns;
  24. using namespace isc::data;
  25. namespace {
  26. class AuthCountersTest : public ::testing::Test {
  27. private:
  28. class MockSession : public AbstractSession {
  29. public:
  30. MockSession() :
  31. // by default we return a simple "success" message.
  32. msg_(Element::fromJSON("{\"result\": [0, \"SUCCESS\"]}")),
  33. throw_session_error_(false), throw_session_timeout_(false)
  34. {}
  35. virtual void establish(const char* socket_file);
  36. virtual void disconnect();
  37. virtual int group_sendmsg(ConstElementPtr msg, string group,
  38. string instance, string to);
  39. virtual bool group_recvmsg(ConstElementPtr& envelope,
  40. ConstElementPtr& msg,
  41. bool nonblock, int seq);
  42. virtual void subscribe(string group, string instance);
  43. virtual void unsubscribe(string group, string instance);
  44. virtual void startRead(boost::function<void()> read_callback);
  45. virtual int reply(ConstElementPtr envelope, ConstElementPtr newmsg);
  46. virtual bool hasQueuedMsgs() const;
  47. virtual void setTimeout(size_t) {}
  48. virtual size_t getTimeout() const { return (0); };
  49. void setThrowSessionError(bool flag);
  50. void setThrowSessionTimeout(bool flag);
  51. void setMessage(ConstElementPtr msg) { msg_ = msg; }
  52. ConstElementPtr sent_msg;
  53. string msg_destination;
  54. private:
  55. ConstElementPtr msg_;
  56. bool throw_session_error_;
  57. bool throw_session_timeout_;
  58. };
  59. protected:
  60. AuthCountersTest() : counters() {
  61. counters.setStatisticsSession(&statistics_session_);
  62. }
  63. ~AuthCountersTest() {
  64. }
  65. MockSession statistics_session_;
  66. AuthCounters counters;
  67. };
  68. void
  69. AuthCountersTest::MockSession::establish(const char*) {}
  70. void
  71. AuthCountersTest::MockSession::disconnect() {}
  72. void
  73. AuthCountersTest::MockSession::subscribe(string, string)
  74. {}
  75. void
  76. AuthCountersTest::MockSession::unsubscribe(string, string)
  77. {}
  78. void
  79. AuthCountersTest::MockSession::startRead(boost::function<void()>)
  80. {}
  81. int
  82. AuthCountersTest::MockSession::reply(ConstElementPtr, ConstElementPtr) {
  83. return (-1);
  84. }
  85. bool
  86. AuthCountersTest::MockSession::hasQueuedMsgs() const {
  87. return (false);
  88. }
  89. int
  90. AuthCountersTest::MockSession::group_sendmsg(ConstElementPtr msg,
  91. string group, string, string)
  92. {
  93. if (throw_session_error_) {
  94. isc_throw(SessionError, "Session Error");
  95. }
  96. sent_msg = msg;
  97. msg_destination = group;
  98. return (0);
  99. }
  100. bool
  101. AuthCountersTest::MockSession::group_recvmsg(ConstElementPtr&,
  102. ConstElementPtr& msg, bool, int)
  103. {
  104. if (throw_session_timeout_) {
  105. isc_throw(SessionTimeout, "Session Timeout");
  106. }
  107. msg = msg_;
  108. return (true);
  109. }
  110. void
  111. AuthCountersTest::MockSession::setThrowSessionError(bool flag) {
  112. throw_session_error_ = flag;
  113. }
  114. void
  115. AuthCountersTest::MockSession::setThrowSessionTimeout(bool flag) {
  116. throw_session_timeout_ = flag;
  117. }
  118. TEST_F(AuthCountersTest, incrementUDPCounter) {
  119. // The counter should be initialized to 0.
  120. EXPECT_EQ(0, counters.getCounter(AuthCounters::COUNTER_UDP_QUERY));
  121. EXPECT_NO_THROW(counters.inc(AuthCounters::COUNTER_UDP_QUERY));
  122. // After increment, the counter should be 1.
  123. EXPECT_EQ(1, counters.getCounter(AuthCounters::COUNTER_UDP_QUERY));
  124. }
  125. TEST_F(AuthCountersTest, incrementTCPCounter) {
  126. // The counter should be initialized to 0.
  127. EXPECT_EQ(0, counters.getCounter(AuthCounters::COUNTER_TCP_QUERY));
  128. EXPECT_NO_THROW(counters.inc(AuthCounters::COUNTER_TCP_QUERY));
  129. // After increment, the counter should be 1.
  130. EXPECT_EQ(1, counters.getCounter(AuthCounters::COUNTER_TCP_QUERY));
  131. }
  132. TEST_F(AuthCountersTest, incrementInvalidCounter) {
  133. // Expect to throw isc::InvalidParameter if the type of the counter is
  134. // invalid.
  135. EXPECT_THROW(counters.inc(AuthCounters::COUNTER_TYPES),
  136. std::out_of_range);
  137. }
  138. TEST_F(AuthCountersTest, submitStatisticsWithoutSession) {
  139. // Set statistics_session to NULL and call submitStatistics().
  140. // Expect to return false.
  141. counters.setStatisticsSession(NULL);
  142. EXPECT_FALSE(counters.submitStatistics());
  143. }
  144. TEST_F(AuthCountersTest, submitStatisticsWithException) {
  145. // Exception SessionError and SessionTimeout will be thrown
  146. // while sending statistics data.
  147. // Both expect to return false.
  148. statistics_session_.setThrowSessionError(true);
  149. EXPECT_FALSE(counters.submitStatistics());
  150. statistics_session_.setThrowSessionError(false);
  151. statistics_session_.setThrowSessionTimeout(true);
  152. EXPECT_FALSE(counters.submitStatistics());
  153. statistics_session_.setThrowSessionTimeout(false);
  154. }
  155. TEST_F(AuthCountersTest, submitStatistics) {
  156. // Submit statistics data.
  157. // Validate if it submits correct data.
  158. // Counters should be initialized to 0.
  159. EXPECT_EQ(0, counters.getCounter(AuthCounters::COUNTER_UDP_QUERY));
  160. EXPECT_EQ(0, counters.getCounter(AuthCounters::COUNTER_TCP_QUERY));
  161. // UDP query counter is set to 2.
  162. counters.inc(AuthCounters::COUNTER_UDP_QUERY);
  163. counters.inc(AuthCounters::COUNTER_UDP_QUERY);
  164. // TCP query counter is set to 1.
  165. counters.inc(AuthCounters::COUNTER_TCP_QUERY);
  166. counters.submitStatistics();
  167. // Destination is "Stats".
  168. EXPECT_EQ("Stats", statistics_session_.msg_destination);
  169. // Command is "set".
  170. EXPECT_EQ("set", statistics_session_.sent_msg->get("command")
  171. ->get(0)->stringValue());
  172. EXPECT_EQ("Auth", statistics_session_.sent_msg->get("command")
  173. ->get(1)->get("owner")->stringValue());
  174. ConstElementPtr statistics_data = statistics_session_.sent_msg
  175. ->get("command")->get(1)
  176. ->get("data");
  177. // UDP query counter is 2 and TCP query counter is 1.
  178. EXPECT_EQ(2, statistics_data->get("queries.udp")->intValue());
  179. EXPECT_EQ(1, statistics_data->get("queries.tcp")->intValue());
  180. }
  181. }