infrequest_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright (C) 2015 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 <dhcp/tests/iface_mgr_test_config.h>
  16. #include <dhcp6/tests/dhcp6_test_utils.h>
  17. #include <dhcp6/tests/dhcp6_client.h>
  18. #include <dhcp/option6_addrlst.h>
  19. #include <dhcp/option6_client_fqdn.h>
  20. using namespace isc;
  21. using namespace isc::dhcp;
  22. using namespace isc::dhcp::test;
  23. using namespace isc::test;
  24. namespace {
  25. /// @brief Set of JSON configurations used by the Information-Request unit tests.
  26. ///
  27. /// - Configuration 0:
  28. /// - one subnet used on eth0 interface
  29. /// - with address and prefix pools
  30. /// - dns-servers option
  31. /// - Configuation 1:
  32. /// - one subnet used on eth0 interface
  33. /// - no addresses or prefixes
  34. /// - domain-search option
  35. /// - Configuration 2:
  36. /// - one subnet used on eth0 interface
  37. /// - dns-servers option for subnet
  38. /// - sip-servers defined in global scope
  39. /// - Configuration 3:
  40. /// - nis-server, nis-domain specified in global scope
  41. /// - no subnets defined
  42. const char* CONFIGS[] = {
  43. // Configuration 0
  44. "{ \"interfaces\": [ \"*\" ],"
  45. "\"preferred-lifetime\": 3000,"
  46. "\"rebind-timer\": 2000, "
  47. "\"renew-timer\": 1000, "
  48. "\"subnet6\": [ { "
  49. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  50. " \"pd-pools\": ["
  51. " { \"prefix\": \"2001:db8:3::\", "
  52. " \"prefix-len\": 48, "
  53. " \"delegated-len\": 64"
  54. " } ],"
  55. " \"option-data\": [ {"
  56. " \"name\": \"dns-servers\","
  57. " \"data\": \"2001:db8::1, 2001:db8::2\""
  58. " } ],"
  59. " \"subnet\": \"2001:db8::/32\", "
  60. " \"interface\": \"eth0\""
  61. " } ],"
  62. "\"valid-lifetime\": 4000 }",
  63. // Configuration 1
  64. "{ \"interfaces\": [ \"*\" ],"
  65. "\"preferred-lifetime\": 3000,"
  66. "\"rebind-timer\": 2000, "
  67. "\"renew-timer\": 1000, "
  68. "\"subnet6\": [ { "
  69. " \"option-data\": [ {"
  70. " \"name\": \"sip-server-addr\","
  71. " \"data\": \"2001:db8::abcd\""
  72. " } ],"
  73. " \"subnet\": \"2001:db8::/32\", "
  74. " \"interface\": \"eth0\""
  75. " } ],"
  76. "\"valid-lifetime\": 4000 }",
  77. // Configuration 2
  78. "{ \"interfaces\": [ \"*\" ],"
  79. "\"preferred-lifetime\": 3000,"
  80. "\"rebind-timer\": 2000, "
  81. "\"renew-timer\": 1000, "
  82. " \"option-data\": [ {"
  83. " \"name\": \"sip-server-dns\","
  84. " \"data\": \"2001:db8::1\""
  85. " } ],"
  86. "\"subnet6\": [ { "
  87. " \"subnet\": \"2001:db8::/32\", "
  88. " \"interface\": \"eth0\","
  89. " \"option-data\": [ {"
  90. " \"name\": \"dns-servers\","
  91. " \"data\": \"2001:db8::2\""
  92. " } ]"
  93. " } ],"
  94. "\"valid-lifetime\": 4000 }",
  95. // Configuration 3
  96. "{ \"interfaces\": [ \"*\" ],"
  97. "\"option-data\": [ {"
  98. " \"name\": \"nis-servers\","
  99. " \"data\": \"2001:db8::1, 2001:db8::2\""
  100. " } ],"
  101. "\"subnet6\": [ ]"
  102. "}"
  103. };
  104. /// @brief Test fixture class for testing 4-way exchange: Solicit-Advertise,
  105. /// Request-Reply.
  106. class InfRequestTest : public Dhcpv6SrvTest {
  107. public:
  108. /// @brief Constructor.
  109. ///
  110. /// Sets up fake interfaces.
  111. InfRequestTest()
  112. : Dhcpv6SrvTest(),
  113. iface_mgr_test_config_(true) {
  114. }
  115. /// @brief Interface Manager's fake configuration control.
  116. IfaceMgrTestConfig iface_mgr_test_config_;
  117. };
  118. /// Check that server processes correctly an incoming inf-request in a
  119. /// typical subnet that has also address and prefix pools.
  120. TEST_F(InfRequestTest, infRequestBasic) {
  121. Dhcp6Client client;
  122. // Configure client to request IA_PD.
  123. configure(CONFIGS[0], *client.getServer());
  124. // Make sure we ended-up having expected number of subnets configured.
  125. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  126. getCfgSubnets6()->getAll();
  127. ASSERT_EQ(1, subnets->size());
  128. // Perform 2-way exchange (Inf-request/reply)
  129. client.requestOption(D6O_NAME_SERVERS);
  130. ASSERT_NO_THROW(client.doInfRequest());
  131. // Confirm that there's a response
  132. Pkt6Ptr response = client.getContext().response_;
  133. ASSERT_TRUE(response);
  134. // Check that it contains our client-id
  135. OptionPtr client_id = response->getOption(D6O_CLIENTID);
  136. ASSERT_TRUE(client_id);
  137. EXPECT_EQ(client_id, client.getClientId());
  138. // Check that it contains proper server-id
  139. OptionPtr server_id = response->getOption(D6O_SERVERID);
  140. ASSERT_TRUE(server_id);
  141. EXPECT_EQ(server_id, client.getServer()->getServerID());
  142. // Check that we received requested DNS servers option
  143. Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
  144. (response->getOption(D6O_NAME_SERVERS));
  145. ASSERT_TRUE(dns);
  146. Option6AddrLst::AddressContainer addrs = dns->getAddresses();
  147. ASSERT_EQ(2, addrs.size());
  148. EXPECT_EQ("2001:db8::1", addrs[0].toText());
  149. EXPECT_EQ("2001:db8::2", addrs[0].toText());
  150. }
  151. /// Check that server processes correctly an incoming inf-request
  152. /// that does not hold client-id. It's so called anonymous inf-request.
  153. /// Uncommon, but certainly valid behavior.
  154. TEST_F(InfRequestTest, infRequestAnonymous) {
  155. Dhcp6Client client;
  156. // Configure client to request IA_PD.
  157. configure(CONFIGS[0], *client.getServer());
  158. // Make sure we ended-up having expected number of subnets configured.
  159. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  160. getCfgSubnets6()->getAll();
  161. ASSERT_EQ(1, subnets->size());
  162. // Perform 2-way exchange (Inf-request/reply)
  163. client.requestOption(D6O_NAME_SERVERS);
  164. client.sendClientId(false);
  165. ASSERT_NO_THROW(client.doInfRequest());
  166. // Confirm that there's a response
  167. Pkt6Ptr response = client.getContext().response_;
  168. ASSERT_TRUE(response);
  169. // Check that we received the requested DNS servers option
  170. Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
  171. (response->getOption(D6O_NAME_SERVERS));
  172. ASSERT_TRUE(dns);
  173. Option6AddrLst::AddressContainer addrs = dns->getAddresses();
  174. ASSERT_EQ(2, addrs.size());
  175. EXPECT_EQ("2001:db8::1", addrs[0].toText());
  176. EXPECT_EQ("2001:db8::2", addrs[0].toText());
  177. }
  178. /// Check that server processes correctly an incoming inf-request
  179. /// if there is a subnet without any addresses or prefixes configured.
  180. TEST_F(InfRequestTest, infRequestStateless) {
  181. Dhcp6Client client;
  182. // Configure client to request IA_PD.
  183. configure(CONFIGS[1], *client.getServer());
  184. // Make sure we ended-up having expected number of subnets configured.
  185. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  186. getCfgSubnets6()->getAll();
  187. ASSERT_EQ(1, subnets->size());
  188. // Perform 2-way exchange (Inf-request/reply)
  189. client.requestOption(D6O_SIP_SERVERS_ADDR);
  190. ASSERT_NO_THROW(client.doInfRequest());
  191. // Confirm that there's a response
  192. Pkt6Ptr response = client.getContext().response_;
  193. ASSERT_TRUE(response);
  194. // Check that we received the requested SIP servers option
  195. Option6AddrLstPtr sip = boost::dynamic_pointer_cast<Option6AddrLst>
  196. (response->getOption(D6O_SIP_SERVERS_ADDR));
  197. ASSERT_TRUE(sip);
  198. Option6AddrLst::AddressContainer addrs = sip->getAddresses();
  199. ASSERT_EQ(1, addrs.size());
  200. EXPECT_EQ("2001:db8::abcd", addrs[0].toText());
  201. }
  202. /// Check that server processes correctly an incoming inf-request
  203. /// if there are options defined at both global and subnet scope.
  204. TEST_F(InfRequestTest, infRequestSubnetAndGlobal) {
  205. Dhcp6Client client;
  206. // Configure client to request IA_PD.
  207. configure(CONFIGS[2], *client.getServer());
  208. // Make sure we ended-up having expected number of subnets configured.
  209. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  210. getCfgSubnets6()->getAll();
  211. ASSERT_EQ(1, subnets->size());
  212. // Perform 2-way exchange (Inf-request/reply)
  213. client.requestOption(D6O_SIP_SERVERS_ADDR);
  214. client.requestOption(D6O_NAME_SERVERS);
  215. ASSERT_NO_THROW(client.doInfRequest());
  216. // Confirm that there's a response
  217. Pkt6Ptr response = client.getContext().response_;
  218. ASSERT_TRUE(response);
  219. // Check that we received the requested sip servers option
  220. Option6AddrLstPtr sip = boost::dynamic_pointer_cast<Option6AddrLst>
  221. (response->getOption(D6O_SIP_SERVERS_ADDR));
  222. ASSERT_TRUE(sip);
  223. Option6AddrLst::AddressContainer addrs = sip->getAddresses();
  224. ASSERT_EQ(1, addrs.size());
  225. EXPECT_EQ("2001:db8::1", addrs[0].toText());
  226. // Check that we received the requested dns servers option
  227. Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
  228. (response->getOption(D6O_NAME_SERVERS));
  229. ASSERT_TRUE(dns);
  230. addrs = sip->getAddresses();
  231. ASSERT_EQ(1, addrs.size());
  232. EXPECT_EQ("2001:db8::2", addrs[0].toText());
  233. }
  234. /// Check that server processes correctly an incoming inf-request
  235. /// if there are options defined at global scope only (no subnets).
  236. TEST_F(InfRequestTest, infRequestNoSubnets) {
  237. Dhcp6Client client;
  238. // Configure client to request IA_PD.
  239. configure(CONFIGS[3], *client.getServer());
  240. // Make sure we ended-up having expected number of subnets configured.
  241. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  242. getCfgSubnets6()->getAll();
  243. ASSERT_EQ(1, subnets->size());
  244. // Perform 2-way exchange (Inf-request/reply)
  245. client.requestOption(D6O_NIS_SERVERS);
  246. ASSERT_NO_THROW(client.doInfRequest());
  247. // Confirm that there's a response
  248. Pkt6Ptr response = client.getContext().response_;
  249. ASSERT_TRUE(response);
  250. // Check that we received the requested sip servers option
  251. Option6AddrLstPtr nis = boost::dynamic_pointer_cast<Option6AddrLst>
  252. (response->getOption(D6O_NIS_SERVERS));
  253. ASSERT_TRUE(nis);
  254. Option6AddrLst::AddressContainer addrs = nis->getAddresses();
  255. ASSERT_EQ(2, addrs.size());
  256. EXPECT_EQ("2001:db8::1", addrs[0].toText());
  257. EXPECT_EQ("2001:db8::2", addrs[0].toText());
  258. }
  259. } // end of anonymous namespace