portconfig.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #ifndef __ISC_TESTUTILS_PORTCONFIG_H
  15. #define __ISC_TESTUTILS_PORTCONFIG_H
  16. #include <gtest/gtest.h>
  17. #include <cc/data.h>
  18. #include <server_common/portconfig.h>
  19. namespace isc {
  20. namespace testutils {
  21. /**
  22. * \brief Bits of tests for server port configuration.
  23. *
  24. * These are bits of tests that can be reused by server classes to check if
  25. * configuration of the listening addresses work.
  26. *
  27. * You can put any of these functions into a TEST_F test and pass the server
  28. * to it.
  29. *
  30. * \todo There's quite a lot of common code in the basic server handling.
  31. * We should refactor it, so both Resolver server and Auth server have
  32. * a common base class. When this is done, the common parts would be put
  33. * there and the tests would be at the base class, not here.
  34. */
  35. namespace portconfig {
  36. /**
  37. * \brief Check setting of the listening addresses directly (as a list) works.
  38. *
  39. * \param server The server to test against.
  40. */
  41. template<class Server>
  42. void
  43. listenAddresses(Server& server) {
  44. using namespace isc::server_common::portconfig;
  45. // In this test we assume the address list is originally empty.
  46. EXPECT_TRUE(server.getListenAddresses().empty());
  47. // Try putting there some addresses
  48. AddressList addresses;
  49. addresses.push_back(AddressPair("127.0.0.1", 53210));
  50. addresses.push_back(AddressPair("::1", 53210));
  51. server.setListenAddresses(addresses);
  52. EXPECT_EQ(2, server.getListenAddresses().size());
  53. EXPECT_EQ("::1", server.getListenAddresses()[1].first);
  54. // Is it independent from what we do with the vector later?
  55. addresses.clear();
  56. EXPECT_EQ(2, server.getListenAddresses().size());
  57. // If we set to an empty list next, the server configuration should
  58. // become empty, too.
  59. server.setListenAddresses(addresses);
  60. EXPECT_TRUE(server.getListenAddresses().empty());
  61. }
  62. /**
  63. * \brief Check setting of the addresses by config value.
  64. *
  65. * This passes an listen_on element to the server's updateConfig function.
  66. * It tries little bit of switching around. It tries both setting a presumably
  67. * valid addresses and then setting something that cant be bound, rolling back
  68. * back to original.
  69. *
  70. * \param server The server object to test against.
  71. */
  72. template<class Server>
  73. void
  74. listenAddressConfig(Server& server) {
  75. using namespace isc::data;
  76. // Try putting there some address
  77. ElementPtr config(Element::fromJSON("{"
  78. "\"listen_on\": ["
  79. " {"
  80. " \"address\": \"127.0.0.1\","
  81. " \"port\": 53210"
  82. " }"
  83. "]"
  84. "}"));
  85. ConstElementPtr result(server.updateConfig(config));
  86. EXPECT_EQ(result->toWire(), isc::config::createAnswer()->toWire());
  87. ASSERT_EQ(1, server.getListenAddresses().size());
  88. EXPECT_EQ("127.0.0.1", server.getListenAddresses()[0].first);
  89. EXPECT_EQ(53210, server.getListenAddresses()[0].second);
  90. // This address is rejected by the test socket requestor
  91. config = Element::fromJSON("{"
  92. "\"listen_on\": ["
  93. " {"
  94. " \"address\": \"192.0.2.2\","
  95. " \"port\": 53210"
  96. " }"
  97. "]"
  98. "}");
  99. result = server.updateConfig(config);
  100. EXPECT_FALSE(result->equals(*isc::config::createAnswer()));
  101. ASSERT_EQ(1, server.getListenAddresses().size());
  102. EXPECT_EQ("127.0.0.1", server.getListenAddresses()[0].first);
  103. EXPECT_EQ(53210, server.getListenAddresses()[0].second);
  104. }
  105. /**
  106. * \brief Check that given config is rejected.
  107. *
  108. * Try if given config is considered invalid by the server and is rejected.
  109. * The value is converted from JSON to the data elements and passed to server's
  110. * updateConfig method. It should not crash, but return a negative answer.
  111. *
  112. * It is used internally by invalidListenAddressConfig, but you can use it
  113. * to test any other invalid configs.
  114. *
  115. * \todo It might be better to put it to some other namespace, as this is more
  116. * generic. But right now it is used only here, so until something else
  117. * needs it, it might as well stay here.
  118. * \param server The server to test against.
  119. * \param JSON Config to use.
  120. * \param name It is used in the output if the test fails.
  121. */
  122. template<class Server>
  123. void
  124. configRejected(Server& server, const std::string& JSON,
  125. const std::string& name)
  126. {
  127. SCOPED_TRACE(name);
  128. using namespace isc::data;
  129. ElementPtr config(Element::fromJSON(JSON));
  130. EXPECT_FALSE(server.updateConfig(config)->
  131. equals(*isc::config::createAnswer())) <<
  132. "Accepted invalid config " << JSON;
  133. }
  134. /**
  135. * \brief Check some invalid address configs.
  136. *
  137. * It tries a series of invalid listen_on configs against the server and checks
  138. * it is rejected.
  139. * \param server The server to check against.
  140. */
  141. template<class Server>
  142. void
  143. invalidListenAddressConfig(Server& server) {
  144. configRejected(server, "{"
  145. "\"listen_on\": \"error\""
  146. "}", "Wrong element type");
  147. configRejected(server, "{"
  148. "\"listen_on\": [{}]"
  149. "}", "Empty address element");
  150. configRejected(server, "{"
  151. "\"listen_on\": [{"
  152. " \"port\": 1.5,"
  153. " \"address\": \"192.0.2.1\""
  154. "}]}", "Float port");
  155. configRejected(server, "{"
  156. "\"listen_on\": [{"
  157. " \"port\": -5,"
  158. " \"address\": \"192.0.2.1\""
  159. "}]}", "Negative port");
  160. configRejected(server, "{"
  161. "\"listen_on\": [{"
  162. " \"port\": 1000000,"
  163. " \"address\": \"192.0.2.1\""
  164. "}]}", "Huge port");
  165. configRejected(server, "{"
  166. "\"listen_on\": [{"
  167. " \"port\": 53,"
  168. " \"address\": \"bad_address\""
  169. "}]}", "Bad address");
  170. }
  171. }
  172. }
  173. }
  174. #endif // __ISC_TESTUTILS_PORTCONFIG_H