statistics_unittest.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <boost/bind.hpp>
  17. #include <cc/data.h>
  18. #include <cc/session.h>
  19. #include <auth/statistics.h>
  20. #include <dns/tests/unittest_util.h>
  21. using isc::UnitTestUtil;
  22. using namespace std;
  23. using namespace isc::cc;
  24. using namespace isc::dns;
  25. using namespace isc::data;
  26. namespace {
  27. class AuthCountersTest : public ::testing::Test {
  28. private:
  29. class MockSession : public AbstractSession {
  30. public:
  31. MockSession() :
  32. // by default we return a simple "success" message.
  33. msg_(Element::fromJSON("{\"result\": [0, \"SUCCESS\"]}")),
  34. throw_session_error_(false), throw_session_timeout_(false)
  35. {}
  36. virtual void establish(const char* socket_file);
  37. virtual void disconnect();
  38. virtual int group_sendmsg(ConstElementPtr msg, string group,
  39. string instance, string to);
  40. virtual bool group_recvmsg(ConstElementPtr& envelope,
  41. ConstElementPtr& msg,
  42. bool nonblock, int seq);
  43. virtual void subscribe(string group, string instance);
  44. virtual void unsubscribe(string group, string instance);
  45. virtual void startRead(boost::function<void()> read_callback);
  46. virtual int reply(ConstElementPtr envelope, ConstElementPtr newmsg);
  47. virtual bool hasQueuedMsgs() const;
  48. virtual void setTimeout(size_t) {}
  49. virtual size_t getTimeout() const { return (0); };
  50. void setThrowSessionError(bool flag);
  51. void setThrowSessionTimeout(bool flag);
  52. void setMessage(ConstElementPtr msg) { msg_ = msg; }
  53. ConstElementPtr sent_msg;
  54. string msg_destination;
  55. private:
  56. ConstElementPtr msg_;
  57. bool throw_session_error_;
  58. bool throw_session_timeout_;
  59. };
  60. protected:
  61. AuthCountersTest() : counters() {
  62. counters.setStatisticsSession(&statistics_session_);
  63. }
  64. ~AuthCountersTest() {
  65. }
  66. MockSession statistics_session_;
  67. AuthCounters counters;
  68. // no need to be inherited from the original class here.
  69. class MockModuleSpec {
  70. public:
  71. bool validateStatistics(ConstElementPtr, const bool valid) const
  72. { return (valid); }
  73. };
  74. MockModuleSpec module_spec_;
  75. };
  76. void
  77. AuthCountersTest::MockSession::establish(const char*) {}
  78. void
  79. AuthCountersTest::MockSession::disconnect() {}
  80. void
  81. AuthCountersTest::MockSession::subscribe(string, string)
  82. {}
  83. void
  84. AuthCountersTest::MockSession::unsubscribe(string, string)
  85. {}
  86. void
  87. AuthCountersTest::MockSession::startRead(boost::function<void()>)
  88. {}
  89. int
  90. AuthCountersTest::MockSession::reply(ConstElementPtr, ConstElementPtr) {
  91. return (-1);
  92. }
  93. bool
  94. AuthCountersTest::MockSession::hasQueuedMsgs() const {
  95. return (false);
  96. }
  97. int
  98. AuthCountersTest::MockSession::group_sendmsg(ConstElementPtr msg,
  99. string group, string, string)
  100. {
  101. if (throw_session_error_) {
  102. isc_throw(SessionError, "Session Error");
  103. }
  104. sent_msg = msg;
  105. msg_destination = group;
  106. return (0);
  107. }
  108. bool
  109. AuthCountersTest::MockSession::group_recvmsg(ConstElementPtr&,
  110. ConstElementPtr& msg, bool, int)
  111. {
  112. if (throw_session_timeout_) {
  113. isc_throw(SessionTimeout, "Session Timeout");
  114. }
  115. msg = msg_;
  116. return (true);
  117. }
  118. void
  119. AuthCountersTest::MockSession::setThrowSessionError(bool flag) {
  120. throw_session_error_ = flag;
  121. }
  122. void
  123. AuthCountersTest::MockSession::setThrowSessionTimeout(bool flag) {
  124. throw_session_timeout_ = flag;
  125. }
  126. TEST_F(AuthCountersTest, incrementUDPCounter) {
  127. // The counter should be initialized to 0.
  128. EXPECT_EQ(0, counters.getCounter(AuthCounters::SERVER_UDP_QUERY));
  129. EXPECT_NO_THROW(counters.inc(AuthCounters::SERVER_UDP_QUERY));
  130. // After increment, the counter should be 1.
  131. EXPECT_EQ(1, counters.getCounter(AuthCounters::SERVER_UDP_QUERY));
  132. }
  133. TEST_F(AuthCountersTest, incrementTCPCounter) {
  134. // The counter should be initialized to 0.
  135. EXPECT_EQ(0, counters.getCounter(AuthCounters::SERVER_TCP_QUERY));
  136. EXPECT_NO_THROW(counters.inc(AuthCounters::SERVER_TCP_QUERY));
  137. // After increment, the counter should be 1.
  138. EXPECT_EQ(1, counters.getCounter(AuthCounters::SERVER_TCP_QUERY));
  139. }
  140. TEST_F(AuthCountersTest, incrementInvalidCounter) {
  141. // Expect to throw an isc::OutOfRange
  142. EXPECT_THROW(counters.inc(AuthCounters::SERVER_COUNTER_TYPES),
  143. isc::OutOfRange);
  144. }
  145. TEST_F(AuthCountersTest, submitStatisticsWithoutSession) {
  146. // Set statistics_session to NULL and call submitStatistics().
  147. // Expect to return false.
  148. counters.setStatisticsSession(NULL);
  149. EXPECT_FALSE(counters.submitStatistics());
  150. }
  151. TEST_F(AuthCountersTest, submitStatisticsWithException) {
  152. // Exception SessionError and SessionTimeout will be thrown
  153. // while sending statistics data.
  154. // Both expect to return false.
  155. statistics_session_.setThrowSessionError(true);
  156. EXPECT_FALSE(counters.submitStatistics());
  157. statistics_session_.setThrowSessionError(false);
  158. statistics_session_.setThrowSessionTimeout(true);
  159. EXPECT_FALSE(counters.submitStatistics());
  160. statistics_session_.setThrowSessionTimeout(false);
  161. }
  162. TEST_F(AuthCountersTest, submitStatisticsWithoutValidator) {
  163. // Submit statistics data.
  164. // Validate if it submits correct data.
  165. // Counters should be initialized to 0.
  166. EXPECT_EQ(0, counters.getCounter(AuthCounters::SERVER_UDP_QUERY));
  167. EXPECT_EQ(0, counters.getCounter(AuthCounters::SERVER_TCP_QUERY));
  168. // UDP query counter is set to 2.
  169. counters.inc(AuthCounters::SERVER_UDP_QUERY);
  170. counters.inc(AuthCounters::SERVER_UDP_QUERY);
  171. // TCP query counter is set to 1.
  172. counters.inc(AuthCounters::SERVER_TCP_QUERY);
  173. counters.submitStatistics();
  174. // Destination is "Stats".
  175. EXPECT_EQ("Stats", statistics_session_.msg_destination);
  176. // Command is "set".
  177. EXPECT_EQ("set", statistics_session_.sent_msg->get("command")
  178. ->get(0)->stringValue());
  179. EXPECT_EQ("Auth", statistics_session_.sent_msg->get("command")
  180. ->get(1)->get("owner")->stringValue());
  181. ConstElementPtr statistics_data = statistics_session_.sent_msg
  182. ->get("command")->get(1)
  183. ->get("data");
  184. // UDP query counter is 2 and TCP query counter is 1.
  185. EXPECT_EQ(2, statistics_data->get("queries.udp")->intValue());
  186. EXPECT_EQ(1, statistics_data->get("queries.tcp")->intValue());
  187. }
  188. TEST_F(AuthCountersTest, submitStatisticsWithValidator) {
  189. //a validator for the unittest
  190. AuthCounters::validator_type validator;
  191. ConstElementPtr el;
  192. // Submit statistics data with correct statistics validator.
  193. validator = boost::bind(
  194. &AuthCountersTest::MockModuleSpec::validateStatistics,
  195. &module_spec_, _1, true);
  196. EXPECT_TRUE(validator(el));
  197. // register validator to AuthCounters
  198. counters.registerStatisticsValidator(validator);
  199. // Counters should be initialized to 0.
  200. EXPECT_EQ(0, counters.getCounter(AuthCounters::SERVER_UDP_QUERY));
  201. EXPECT_EQ(0, counters.getCounter(AuthCounters::SERVER_TCP_QUERY));
  202. // UDP query counter is set to 2.
  203. counters.inc(AuthCounters::SERVER_UDP_QUERY);
  204. counters.inc(AuthCounters::SERVER_UDP_QUERY);
  205. // TCP query counter is set to 1.
  206. counters.inc(AuthCounters::SERVER_TCP_QUERY);
  207. // checks the value returned by submitStatistics
  208. EXPECT_TRUE(counters.submitStatistics());
  209. // Destination is "Stats".
  210. EXPECT_EQ("Stats", statistics_session_.msg_destination);
  211. // Command is "set".
  212. EXPECT_EQ("set", statistics_session_.sent_msg->get("command")
  213. ->get(0)->stringValue());
  214. EXPECT_EQ("Auth", statistics_session_.sent_msg->get("command")
  215. ->get(1)->get("owner")->stringValue());
  216. ConstElementPtr statistics_data = statistics_session_.sent_msg
  217. ->get("command")->get(1)
  218. ->get("data");
  219. // UDP query counter is 2 and TCP query counter is 1.
  220. EXPECT_EQ(2, statistics_data->get("queries.udp")->intValue());
  221. EXPECT_EQ(1, statistics_data->get("queries.tcp")->intValue());
  222. // Submit statistics data with incorrect statistics validator.
  223. validator = boost::bind(
  224. &AuthCountersTest::MockModuleSpec::validateStatistics,
  225. &module_spec_, _1, false);
  226. EXPECT_FALSE(validator(el));
  227. counters.registerStatisticsValidator(validator);
  228. // checks the value returned by submitStatistics
  229. EXPECT_FALSE(counters.submitStatistics());
  230. }
  231. }