infrequest_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <dhcp/tests/iface_mgr_test_config.h>
  8. #include <dhcp6/tests/dhcp6_test_utils.h>
  9. #include <dhcp6/tests/dhcp6_client.h>
  10. #include <dhcp/option6_addrlst.h>
  11. #include <dhcp/option6_client_fqdn.h>
  12. #include <stats/stats_mgr.h>
  13. using namespace isc;
  14. using namespace isc::dhcp;
  15. using namespace isc::dhcp::test;
  16. namespace {
  17. /// @brief Set of JSON configurations used by the Information-Request unit tests.
  18. ///
  19. /// - Configuration 0:
  20. /// - one subnet used on eth0 interface
  21. /// - with address and prefix pools
  22. /// - dns-servers option
  23. /// - Configuration 1:
  24. /// - one subnet used on eth0 interface
  25. /// - no addresses or prefixes
  26. /// - domain-search option
  27. /// - Configuration 2:
  28. /// - one subnet used on eth0 interface
  29. /// - dns-servers option for subnet
  30. /// - sip-servers defined in global scope
  31. /// - Configuration 3:
  32. /// - nis-server, nis-domain specified in global scope
  33. /// - no subnets defined
  34. const char* CONFIGS[] = {
  35. // Configuration 0
  36. "{ \"interfaces-config\": {"
  37. " \"interfaces\": [ \"*\" ]"
  38. "},"
  39. "\"preferred-lifetime\": 3000,"
  40. "\"rebind-timer\": 2000, "
  41. "\"renew-timer\": 1000, "
  42. "\"subnet6\": [ { "
  43. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  44. " \"pd-pools\": ["
  45. " { \"prefix\": \"2001:db8:3::\", "
  46. " \"prefix-len\": 48, "
  47. " \"delegated-len\": 64"
  48. " } ],"
  49. " \"option-data\": [ {"
  50. " \"name\": \"dns-servers\","
  51. " \"data\": \"2001:db8::1, 2001:db8::2\""
  52. " } ],"
  53. " \"subnet\": \"2001:db8::/32\", "
  54. " \"interface\": \"eth0\""
  55. " } ],"
  56. "\"valid-lifetime\": 4000 }",
  57. // Configuration 1
  58. "{ \"interfaces-config\": {"
  59. " \"interfaces\": [ \"*\" ]"
  60. "},"
  61. "\"preferred-lifetime\": 3000,"
  62. "\"rebind-timer\": 2000, "
  63. "\"renew-timer\": 1000, "
  64. "\"subnet6\": [ { "
  65. " \"option-data\": [ {"
  66. " \"name\": \"sip-server-addr\","
  67. " \"data\": \"2001:db8::abcd\""
  68. " } ],"
  69. " \"subnet\": \"2001:db8::/32\", "
  70. " \"interface\": \"eth0\""
  71. " } ],"
  72. "\"valid-lifetime\": 4000 }",
  73. // Configuration 2
  74. "{ \"interfaces-config\": {"
  75. " \"interfaces\": [ \"*\" ]"
  76. "},"
  77. "\"preferred-lifetime\": 3000,"
  78. "\"rebind-timer\": 2000, "
  79. "\"renew-timer\": 1000, "
  80. " \"option-data\": [ {"
  81. " \"name\": \"sip-server-addr\","
  82. " \"data\": \"2001:db8::1\""
  83. " } ],"
  84. "\"subnet6\": [ { "
  85. " \"subnet\": \"2001:db8::/32\", "
  86. " \"interface\": \"eth0\","
  87. " \"option-data\": [ {"
  88. " \"name\": \"dns-servers\","
  89. " \"data\": \"2001:db8::2\""
  90. " } ]"
  91. " } ],"
  92. "\"valid-lifetime\": 4000 }",
  93. // Configuration 3
  94. "{ \"interfaces-config\": {"
  95. " \"interfaces\": [ \"*\" ]"
  96. "},"
  97. "\"option-data\": [ {"
  98. " \"name\": \"nis-servers\","
  99. " \"data\": \"2001:db8::1, 2001:db8::2\""
  100. " } ]"
  101. "}"
  102. };
  103. /// @brief Test fixture class for testing 4-way exchange: Solicit-Advertise,
  104. /// Request-Reply.
  105. class InfRequestTest : public Dhcpv6SrvTest {
  106. public:
  107. /// @brief Constructor.
  108. ///
  109. /// Sets up fake interfaces.
  110. InfRequestTest()
  111. : Dhcpv6SrvTest(),
  112. iface_mgr_test_config_(true) {
  113. // Let's wipe all existing statistics.
  114. isc::stats::StatsMgr::instance().removeAll();
  115. }
  116. /// @brief Destructor.
  117. ///
  118. /// Removes any statistics that may have been set.
  119. ~InfRequestTest() {
  120. // Let's wipe all existing statistics.
  121. isc::stats::StatsMgr::instance().removeAll();
  122. }
  123. /// @brief Interface Manager's fake configuration control.
  124. IfaceMgrTestConfig iface_mgr_test_config_;
  125. };
  126. /// Check that server processes correctly an incoming inf-request in a
  127. /// typical subnet that has also address and prefix pools.
  128. TEST_F(InfRequestTest, infRequestBasic) {
  129. Dhcp6Client client;
  130. // Configure client to request IA_PD.
  131. configure(CONFIGS[0], *client.getServer());
  132. // Make sure we ended-up having expected number of subnets configured.
  133. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  134. getCfgSubnets6()->getAll();
  135. ASSERT_EQ(1, subnets->size());
  136. // Perform 2-way exchange (Inf-request/reply)
  137. client.requestOption(D6O_NAME_SERVERS);
  138. ASSERT_NO_THROW(client.doInfRequest());
  139. // Confirm that there's a response
  140. Pkt6Ptr response = client.getContext().response_;
  141. ASSERT_TRUE(response);
  142. // Check that it contains our client-id
  143. OptionPtr client_id = response->getOption(D6O_CLIENTID);
  144. ASSERT_TRUE(client_id);
  145. EXPECT_TRUE(compareOptions(client_id, client.getClientId()));
  146. // Check that it contains proper server-id
  147. OptionPtr server_id = response->getOption(D6O_SERVERID);
  148. ASSERT_TRUE(server_id);
  149. EXPECT_TRUE(compareOptions(server_id, client.getServer()->getServerID()));
  150. // Check that we received requested DNS servers option
  151. Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
  152. (response->getOption(D6O_NAME_SERVERS));
  153. ASSERT_TRUE(dns);
  154. Option6AddrLst::AddressContainer addrs = dns->getAddresses();
  155. ASSERT_EQ(2, addrs.size());
  156. EXPECT_EQ("2001:db8::1", addrs[0].toText());
  157. EXPECT_EQ("2001:db8::2", addrs[1].toText());
  158. }
  159. /// Check that server processes correctly an incoming inf-request
  160. /// that does not hold client-id. It's so called anonymous inf-request.
  161. /// Uncommon, but certainly valid behavior.
  162. TEST_F(InfRequestTest, infRequestAnonymous) {
  163. Dhcp6Client client;
  164. // Configure client to request IA_PD.
  165. configure(CONFIGS[0], *client.getServer());
  166. // Make sure we ended-up having expected number of subnets configured.
  167. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  168. getCfgSubnets6()->getAll();
  169. ASSERT_EQ(1, subnets->size());
  170. // Perform 2-way exchange (Inf-request/reply)
  171. client.requestOption(D6O_NAME_SERVERS);
  172. client.useClientId(false);
  173. ASSERT_NO_THROW(client.doInfRequest());
  174. // Confirm that there's a response
  175. Pkt6Ptr response = client.getContext().response_;
  176. ASSERT_TRUE(response);
  177. // Check that we received the requested DNS servers option
  178. Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
  179. (response->getOption(D6O_NAME_SERVERS));
  180. ASSERT_TRUE(dns);
  181. Option6AddrLst::AddressContainer addrs = dns->getAddresses();
  182. ASSERT_EQ(2, addrs.size());
  183. EXPECT_EQ("2001:db8::1", addrs[0].toText());
  184. EXPECT_EQ("2001:db8::2", addrs[1].toText());
  185. }
  186. /// Check that server processes correctly an incoming inf-request
  187. /// if there is a subnet without any addresses or prefixes configured.
  188. TEST_F(InfRequestTest, infRequestStateless) {
  189. Dhcp6Client client;
  190. // Configure client to request IA_PD.
  191. configure(CONFIGS[1], *client.getServer());
  192. // Make sure we ended-up having expected number of subnets configured.
  193. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  194. getCfgSubnets6()->getAll();
  195. ASSERT_EQ(1, subnets->size());
  196. // Perform 2-way exchange (Inf-request/reply)
  197. client.requestOption(D6O_SIP_SERVERS_ADDR);
  198. ASSERT_NO_THROW(client.doInfRequest());
  199. // Confirm that there's a response
  200. Pkt6Ptr response = client.getContext().response_;
  201. ASSERT_TRUE(response);
  202. // Check that we received the requested SIP servers option
  203. Option6AddrLstPtr sip = boost::dynamic_pointer_cast<Option6AddrLst>
  204. (response->getOption(D6O_SIP_SERVERS_ADDR));
  205. ASSERT_TRUE(sip);
  206. Option6AddrLst::AddressContainer addrs = sip->getAddresses();
  207. ASSERT_EQ(1, addrs.size());
  208. EXPECT_EQ("2001:db8::abcd", addrs[0].toText());
  209. }
  210. /// Check that server processes correctly an incoming inf-request
  211. /// if there are options defined at both global and subnet scope.
  212. TEST_F(InfRequestTest, infRequestSubnetAndGlobal) {
  213. Dhcp6Client client;
  214. // Configure client to request IA_PD.
  215. configure(CONFIGS[2], *client.getServer());
  216. // Make sure we ended-up having expected number of subnets configured.
  217. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  218. getCfgSubnets6()->getAll();
  219. ASSERT_EQ(1, subnets->size());
  220. // Perform 2-way exchange (Inf-request/reply)
  221. client.requestOption(D6O_SIP_SERVERS_ADDR);
  222. client.requestOption(D6O_NAME_SERVERS);
  223. ASSERT_NO_THROW(client.doInfRequest());
  224. // Confirm that there's a response
  225. Pkt6Ptr response = client.getContext().response_;
  226. ASSERT_TRUE(response);
  227. // Check that we received the requested sip servers option
  228. Option6AddrLstPtr sip = boost::dynamic_pointer_cast<Option6AddrLst>
  229. (response->getOption(D6O_SIP_SERVERS_ADDR));
  230. ASSERT_TRUE(sip);
  231. Option6AddrLst::AddressContainer addrs = sip->getAddresses();
  232. ASSERT_EQ(1, addrs.size());
  233. EXPECT_EQ("2001:db8::1", addrs[0].toText());
  234. // Check that we received the requested dns servers option
  235. Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
  236. (response->getOption(D6O_NAME_SERVERS));
  237. ASSERT_TRUE(dns);
  238. addrs = dns->getAddresses();
  239. ASSERT_EQ(1, addrs.size());
  240. EXPECT_EQ("2001:db8::2", addrs[0].toText());
  241. }
  242. /// Check that server processes correctly an incoming inf-request
  243. /// if there are options defined at global scope only (no subnets).
  244. TEST_F(InfRequestTest, infRequestNoSubnets) {
  245. Dhcp6Client client;
  246. // Configure client to request IA_PD.
  247. configure(CONFIGS[3], *client.getServer());
  248. // Make sure we ended-up having expected number of subnets configured.
  249. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  250. getCfgSubnets6()->getAll();
  251. ASSERT_EQ(0, subnets->size());
  252. // Perform 2-way exchange (Inf-request/reply)
  253. client.requestOption(D6O_NIS_SERVERS);
  254. ASSERT_NO_THROW(client.doInfRequest());
  255. // Confirm that there's a response
  256. Pkt6Ptr response = client.getContext().response_;
  257. ASSERT_TRUE(response);
  258. // Check that we received the requested sip servers option
  259. Option6AddrLstPtr nis = boost::dynamic_pointer_cast<Option6AddrLst>
  260. (response->getOption(D6O_NIS_SERVERS));
  261. ASSERT_TRUE(nis);
  262. Option6AddrLst::AddressContainer addrs = nis->getAddresses();
  263. ASSERT_EQ(2, addrs.size());
  264. EXPECT_EQ("2001:db8::1", addrs[0].toText());
  265. EXPECT_EQ("2001:db8::2", addrs[1].toText());
  266. }
  267. /// Check that server processes correctly an incoming inf-request in a
  268. /// typical subnet that has also address and prefix pools.
  269. TEST_F(InfRequestTest, infRequestStats) {
  270. Dhcp6Client client;
  271. // Configure client to request IA_PD.
  272. configure(CONFIGS[0], *client.getServer());
  273. // Make sure we ended-up having expected number of subnets configured.
  274. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  275. getCfgSubnets6()->getAll();
  276. ASSERT_EQ(1, subnets->size());
  277. // Ok, let's check the statistics. None should be present.
  278. using namespace isc::stats;
  279. StatsMgr& mgr = StatsMgr::instance();
  280. ObservationPtr pkt6_rcvd = mgr.getObservation("pkt6-received");
  281. ObservationPtr pkt6_infreq_rcvd = mgr.getObservation("pkt6-infrequest-received");
  282. ObservationPtr pkt6_reply_sent = mgr.getObservation("pkt6-reply-sent");
  283. ObservationPtr pkt6_sent = mgr.getObservation("pkt6-sent");
  284. EXPECT_FALSE(pkt6_rcvd);
  285. EXPECT_FALSE(pkt6_infreq_rcvd);
  286. EXPECT_FALSE(pkt6_reply_sent);
  287. EXPECT_FALSE(pkt6_sent);
  288. // Perform 2-way exchange (Inf-request/reply)
  289. client.requestOption(D6O_NAME_SERVERS);
  290. ASSERT_NO_THROW(client.doInfRequest());
  291. // Confirm that there's a response
  292. Pkt6Ptr response = client.getContext().response_;
  293. ASSERT_TRUE(response);
  294. pkt6_rcvd = mgr.getObservation("pkt6-received");
  295. pkt6_infreq_rcvd = mgr.getObservation("pkt6-infrequest-received");
  296. pkt6_reply_sent = mgr.getObservation("pkt6-reply-sent");
  297. pkt6_sent = mgr.getObservation("pkt6-sent");
  298. ASSERT_TRUE(pkt6_rcvd);
  299. ASSERT_TRUE(pkt6_infreq_rcvd);
  300. ASSERT_TRUE(pkt6_reply_sent);
  301. ASSERT_TRUE(pkt6_sent);
  302. // They also must have expected values.
  303. EXPECT_EQ(1, pkt6_rcvd->getInteger().first);
  304. EXPECT_EQ(1, pkt6_infreq_rcvd->getInteger().first);
  305. EXPECT_EQ(1, pkt6_reply_sent->getInteger().first);
  306. EXPECT_EQ(1, pkt6_sent->getInteger().first);
  307. }
  308. } // end of anonymous namespace