inform_unittest.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // Copyright (C) 2014-2016 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 <asiolink/io_address.h>
  8. #include <cc/data.h>
  9. #include <dhcp/dhcp4.h>
  10. #include <dhcp/tests/iface_mgr_test_config.h>
  11. #include <dhcp4/tests/dhcp4_test_utils.h>
  12. #include <dhcp4/tests/dhcp4_client.h>
  13. #include <stats/stats_mgr.h>
  14. using namespace isc;
  15. using namespace isc::asiolink;
  16. using namespace isc::data;
  17. using namespace isc::dhcp;
  18. using namespace isc::dhcp::test;
  19. namespace {
  20. /// @brief Set of JSON configurations used throughout the Inform tests.
  21. ///
  22. /// - Configuration 0:
  23. /// - Used for testing direct traffic
  24. /// - 1 subnet: 10.0.0.0/24
  25. /// - 1 pool: 10.0.0.10-10.0.0.100
  26. /// - Router option present: 10.0.0.200 and 10.0.0.201
  27. /// - Domain Name Server option present: 10.0.0.202, 10.0.0.203.
  28. /// - Log Servers option present: 192.0.2.200 and 192.0.2.201
  29. /// - Quotes Servers option present: 192.0.2.202, 192.0.2.203.
  30. ///
  31. /// - Configuration 1:
  32. /// - Use for testing relayed messages
  33. /// - 1 subnet: 192.0.2.0/24
  34. /// - Router option present: 192.0.2.200 and 192.0.2.201
  35. /// - Domain Name Server option present: 192.0.2.202, 192.0.2.203.
  36. /// - Log Servers option present: 192.0.2.200 and 192.0.2.201
  37. /// - Quotes Servers option present: 192.0.2.202, 192.0.2.203.
  38. const char* INFORM_CONFIGS[] = {
  39. // Configuration 0
  40. "{ \"interfaces-config\": {"
  41. " \"interfaces\": [ \"*\" ]"
  42. "},"
  43. "\"valid-lifetime\": 600,"
  44. "\"subnet4\": [ { "
  45. " \"subnet\": \"10.0.0.0/24\", "
  46. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  47. " \"option-data\": [ {"
  48. " \"name\": \"routers\","
  49. " \"data\": \"10.0.0.200,10.0.0.201\""
  50. " },"
  51. " {"
  52. " \"name\": \"domain-name-servers\","
  53. " \"data\": \"10.0.0.202,10.0.0.203\""
  54. " },"
  55. " {"
  56. " \"name\": \"log-servers\","
  57. " \"data\": \"10.0.0.202,10.0.0.203\""
  58. " },"
  59. " {"
  60. " \"name\": \"cookie-servers\","
  61. " \"data\": \"10.0.0.200,10.0.0.201\""
  62. " } ]"
  63. " } ]"
  64. "}",
  65. // Configuration 1
  66. "{ \"interfaces-config\": {"
  67. " \"interfaces\": [ \"*\" ]"
  68. "},"
  69. "\"valid-lifetime\": 600,"
  70. "\"subnet4\": [ { "
  71. " \"subnet\": \"192.0.2.0/24\", "
  72. " \"option-data\": [ {"
  73. " \"name\": \"routers\","
  74. " \"data\": \"192.0.2.200,192.0.2.201\""
  75. " },"
  76. " {"
  77. " \"name\": \"domain-name-servers\","
  78. " \"data\": \"192.0.2.202,192.0.2.203\""
  79. " },"
  80. " {"
  81. " \"name\": \"log-servers\","
  82. " \"data\": \"10.0.0.200,10.0.0.201\""
  83. " },"
  84. " {"
  85. " \"name\": \"cookie-servers\","
  86. " \"data\": \"10.0.0.202,10.0.0.203\""
  87. " } ]"
  88. " } ]"
  89. "}"
  90. };
  91. /// @brief Test fixture class for testing DHCPINFORM.
  92. class InformTest : public Dhcpv4SrvTest {
  93. public:
  94. /// @brief Constructor.
  95. ///
  96. /// Sets up fake interfaces.
  97. InformTest()
  98. : Dhcpv4SrvTest(),
  99. iface_mgr_test_config_(true) {
  100. IfaceMgr::instance().openSockets4();
  101. // Let's wipe all existing statistics.
  102. isc::stats::StatsMgr::instance().removeAll();
  103. }
  104. /// @brief Desctructor.
  105. ///
  106. /// Cleans up statistics after the test.
  107. ~InformTest() {
  108. // Let's wipe all existing statistics.
  109. isc::stats::StatsMgr::instance().removeAll();
  110. }
  111. /// @brief Interface Manager's fake configuration control.
  112. IfaceMgrTestConfig iface_mgr_test_config_;
  113. };
  114. // Test that directly connected client's DHCPINFORM message is processed and
  115. // DHCPACK message is sent back.
  116. TEST_F(InformTest, directClientBroadcast) {
  117. Dhcp4Client client;
  118. // Configure DHCP server.
  119. configure(INFORM_CONFIGS[0], *client.getServer());
  120. // Request some configuration when DHCPINFORM is sent.
  121. client.requestOptions(DHO_LOG_SERVERS, DHO_COOKIE_SERVERS);
  122. // Preconfigure the client with the IP address.
  123. client.createLease(IOAddress("10.0.0.56"), 600);
  124. // Send DHCPINFORM message to the server.
  125. ASSERT_NO_THROW(client.doInform());
  126. // Make sure that the server responded.
  127. ASSERT_TRUE(client.getContext().response_);
  128. Pkt4Ptr resp = client.getContext().response_;
  129. // Make sure that the server has responded with DHCPACK.
  130. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  131. // Response should have been unicast to the ciaddr.
  132. EXPECT_EQ(IOAddress("10.0.0.56"), resp->getLocalAddr());
  133. // Response must not be relayed.
  134. EXPECT_FALSE(resp->isRelayed());
  135. // Make sure that the server id is present.
  136. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  137. // Make sure that the Routers option has been received.
  138. ASSERT_EQ(2, client.config_.routers_.size());
  139. EXPECT_EQ("10.0.0.200", client.config_.routers_[0].toText());
  140. EXPECT_EQ("10.0.0.201", client.config_.routers_[1].toText());
  141. // Make sure that the DNS Servers option has been received.
  142. ASSERT_EQ(2, client.config_.dns_servers_.size());
  143. EXPECT_EQ("10.0.0.202", client.config_.dns_servers_[0].toText());
  144. EXPECT_EQ("10.0.0.203", client.config_.dns_servers_[1].toText());
  145. // Make sure that the Log Servers option has been received.
  146. ASSERT_EQ(2, client.config_.quotes_servers_.size());
  147. EXPECT_EQ("10.0.0.200", client.config_.quotes_servers_[0].toText());
  148. EXPECT_EQ("10.0.0.201", client.config_.quotes_servers_[1].toText());
  149. // Make sure that the Quotes Servers option has been received.
  150. ASSERT_EQ(2, client.config_.log_servers_.size());
  151. EXPECT_EQ("10.0.0.202", client.config_.log_servers_[0].toText());
  152. EXPECT_EQ("10.0.0.203", client.config_.log_servers_[1].toText());
  153. // Check that we can send another DHCPINFORM message using
  154. // different ciaddr and we will get the configuration.
  155. client.createLease(IOAddress("10.0.0.12"), 600);
  156. // This time do not request Quotes Servers option and it should not
  157. // be returned.
  158. client.requestOptions(DHO_LOG_SERVERS);
  159. // Send DHCPINFORM.
  160. ASSERT_NO_THROW(client.doInform());
  161. // Make sure that the server responded.
  162. resp = client.getContext().response_;
  163. ASSERT_TRUE(resp);
  164. // Make sure that the server has responded with DHCPACK.
  165. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  166. // Response should have been unicast to the ciaddr.
  167. EXPECT_EQ(IOAddress("10.0.0.12"), resp->getLocalAddr());
  168. // Response must not be relayed.
  169. EXPECT_FALSE(resp->isRelayed());
  170. // Make sure that the server id is present.
  171. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  172. // Make sure that the Routers option has been received.
  173. ASSERT_EQ(2, client.config_.routers_.size());
  174. EXPECT_EQ("10.0.0.200", client.config_.routers_[0].toText());
  175. EXPECT_EQ("10.0.0.201", client.config_.routers_[1].toText());
  176. // Make sure that the DNS Servers option has been received.
  177. ASSERT_EQ(2, client.config_.dns_servers_.size());
  178. EXPECT_EQ("10.0.0.202", client.config_.dns_servers_[0].toText());
  179. EXPECT_EQ("10.0.0.203", client.config_.dns_servers_[1].toText());
  180. // Make sure that the Quotes Servers option hasn't been received.
  181. ASSERT_TRUE(client.config_.quotes_servers_.empty());
  182. }
  183. // This test checks that the server drops DHCPINFORM message when the
  184. // source address and ciaddr is 0.
  185. TEST_F(InformTest, directClientBroadcastNoAddress) {
  186. Dhcp4Client client;
  187. // Configure DHCP server.
  188. configure(INFORM_CONFIGS[0], *client.getServer());
  189. // Request some configuration when DHCPINFORM is sent.
  190. client.requestOptions(DHO_LOG_SERVERS, DHO_COOKIE_SERVERS);
  191. // Send DHCPINFORM message to the server.
  192. ASSERT_NO_THROW(client.doInform());
  193. // Make sure that the server dropped the message.
  194. ASSERT_FALSE(client.getContext().response_);
  195. }
  196. // Test that client's DHCPINFORM message sent to a unicast address
  197. // is received and processed by the server and that the DHCPACK is
  198. // is sent.
  199. TEST_F(InformTest, directClientUnicast) {
  200. Dhcp4Client client;
  201. // Configure DHCP server.
  202. configure(INFORM_CONFIGS[0], *client.getServer());
  203. // Preconfigure the client with the IP address.
  204. client.createLease(IOAddress("10.0.0.56"), 600);
  205. // Set remote address to unicast.
  206. client.setDestAddress(IOAddress("10.0.0.1"));
  207. // Send DHCPINFORM message to the server.
  208. ASSERT_NO_THROW(client.doInform());
  209. // Make sure that the server responded.
  210. ASSERT_TRUE(client.getContext().response_);
  211. Pkt4Ptr resp = client.getContext().response_;
  212. // Make sure that the server has responded with DHCPACK.
  213. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  214. // Response should have been unicast to the ciaddr.
  215. EXPECT_EQ(IOAddress("10.0.0.56"), resp->getLocalAddr());
  216. // Response must not be relayed.
  217. EXPECT_FALSE(resp->isRelayed());
  218. // Make sure that the server id is present.
  219. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  220. // Make sure that the Routers option has been received.
  221. ASSERT_EQ(2, client.config_.routers_.size());
  222. EXPECT_EQ("10.0.0.200", client.config_.routers_[0].toText());
  223. EXPECT_EQ("10.0.0.201", client.config_.routers_[1].toText());
  224. // Make sure that the DNS Servers option has been received.
  225. ASSERT_EQ(2, client.config_.dns_servers_.size());
  226. EXPECT_EQ("10.0.0.202", client.config_.dns_servers_[0].toText());
  227. EXPECT_EQ("10.0.0.203", client.config_.dns_servers_[1].toText());
  228. }
  229. // This test checks that the server responds to the source address of the
  230. // packet received from the directly connected client if the client didn't
  231. // set the ciaddr.
  232. TEST_F(InformTest, directClientNoCiaddr) {
  233. Dhcp4Client client;
  234. // Configure DHCP server.
  235. configure(INFORM_CONFIGS[0], *client.getServer());
  236. // Preconfigure the client with the IP address.
  237. client.createLease(IOAddress("10.0.0.56"), 600);
  238. // Send DHCPINFORM message (with ciaddr not set) to the server.
  239. ASSERT_NO_THROW(client.doInform(false));
  240. // Make sure that the server responded.
  241. ASSERT_TRUE(client.getContext().response_);
  242. Pkt4Ptr resp = client.getContext().response_;
  243. // Make sure that the server has responded with DHCPACK.
  244. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  245. // Response should have been unicast to the ciaddr.
  246. EXPECT_EQ(IOAddress("10.0.0.56"), resp->getLocalAddr());
  247. // Response must not be relayed.
  248. EXPECT_FALSE(resp->isRelayed());
  249. EXPECT_EQ(DHCP4_CLIENT_PORT, resp->getLocalPort());
  250. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getRemotePort());
  251. // Make sure that the server id is present.
  252. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  253. // Make sure that the Routers option has been received.
  254. ASSERT_EQ(2, client.config_.routers_.size());
  255. EXPECT_EQ("10.0.0.200", client.config_.routers_[0].toText());
  256. EXPECT_EQ("10.0.0.201", client.config_.routers_[1].toText());
  257. // Make sure that the DNS Servers option has been received.
  258. ASSERT_EQ(2, client.config_.dns_servers_.size());
  259. EXPECT_EQ("10.0.0.202", client.config_.dns_servers_[0].toText());
  260. EXPECT_EQ("10.0.0.203", client.config_.dns_servers_[1].toText());
  261. }
  262. // This test checks that the server receiving DHCPINFORM via relay, unicasts the
  263. // DHCPACK to the client (ciaddr).
  264. TEST_F(InformTest, relayedClient) {
  265. Dhcp4Client client;
  266. // Configure DHCP server.
  267. configure(INFORM_CONFIGS[1], *client.getServer());
  268. // Message is relayed.
  269. client.useRelay();
  270. // Request some configuration when DHCPINFORM is sent.
  271. client.requestOptions(DHO_LOG_SERVERS, DHO_COOKIE_SERVERS);
  272. // Preconfigure the client with the IP address.
  273. client.createLease(IOAddress("192.0.2.56"), 600);
  274. // Send DHCPINFORM message to the server.
  275. ASSERT_NO_THROW(client.doInform());
  276. // Make sure that the server responded.
  277. ASSERT_TRUE(client.getContext().response_);
  278. Pkt4Ptr resp = client.getContext().response_;
  279. // Make sure that the server has responded with DHCPACK.
  280. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  281. // Response should have been unicast to the ciaddr.
  282. EXPECT_EQ(IOAddress("192.0.2.56"), resp->getLocalAddr());
  283. // Response is unicast to the client, so it must not be relayed.
  284. EXPECT_FALSE(resp->isRelayed());
  285. EXPECT_EQ(DHCP4_CLIENT_PORT, resp->getLocalPort());
  286. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getRemotePort());
  287. // Make sure that the server id is present.
  288. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  289. // Make sure that the Routers option has been received.
  290. ASSERT_EQ(2, client.config_.routers_.size());
  291. EXPECT_EQ("192.0.2.200", client.config_.routers_[0].toText());
  292. EXPECT_EQ("192.0.2.201", client.config_.routers_[1].toText());
  293. // Make sure that the DNS Servers option has been received.
  294. ASSERT_EQ(2, client.config_.dns_servers_.size());
  295. EXPECT_EQ("192.0.2.202", client.config_.dns_servers_[0].toText());
  296. EXPECT_EQ("192.0.2.203", client.config_.dns_servers_[1].toText());
  297. // Make sure that the Quotes Servers option has been received.
  298. ASSERT_EQ(2, client.config_.quotes_servers_.size());
  299. EXPECT_EQ("10.0.0.202", client.config_.quotes_servers_[0].toText());
  300. EXPECT_EQ("10.0.0.203", client.config_.quotes_servers_[1].toText());
  301. // Make sure that the Log Servers option has been received.
  302. ASSERT_EQ(2, client.config_.log_servers_.size());
  303. EXPECT_EQ("10.0.0.200", client.config_.log_servers_[0].toText());
  304. EXPECT_EQ("10.0.0.201", client.config_.log_servers_[1].toText());
  305. }
  306. // This test checks that the server can respond to the DHCPINFORM message
  307. // received via relay when the ciaddr is not set.
  308. TEST_F(InformTest, relayedClientNoCiaddr) {
  309. Dhcp4Client client;
  310. // Configure DHCP server.
  311. configure(INFORM_CONFIGS[1], *client.getServer());
  312. // Message is relayed.
  313. client.useRelay();
  314. // Send DHCPINFORM message to the server.
  315. ASSERT_NO_THROW(client.doInform());
  316. // Make sure that the server responded.
  317. ASSERT_TRUE(client.getContext().response_);
  318. Pkt4Ptr resp = client.getContext().response_;
  319. // Make sure that the server has responded with DHCPACK.
  320. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  321. // Response should go through a relay as there is no ciaddr.
  322. EXPECT_EQ(IOAddress("192.0.2.2"), resp->getLocalAddr());
  323. EXPECT_EQ(IOAddress("192.0.2.2"), resp->getGiaddr());
  324. EXPECT_EQ(1, resp->getHops());
  325. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  326. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getRemotePort());
  327. // In the case when the client didn't set the ciaddr and the message
  328. // was received via relay the server sets the Broadcast flag to help
  329. // the relay forwarding the message (without yiaddr) to the client.
  330. EXPECT_EQ(BOOTP_BROADCAST, resp->getFlags() & BOOTP_BROADCAST);
  331. // Make sure that the server id is present.
  332. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  333. // Make sure that the Routers option has been received.
  334. ASSERT_EQ(2, client.config_.routers_.size());
  335. EXPECT_EQ("192.0.2.200", client.config_.routers_[0].toText());
  336. EXPECT_EQ("192.0.2.201", client.config_.routers_[1].toText());
  337. // Make sure that the DNS Servers option has been received.
  338. ASSERT_EQ(2, client.config_.dns_servers_.size());
  339. EXPECT_EQ("192.0.2.202", client.config_.dns_servers_[0].toText());
  340. EXPECT_EQ("192.0.2.203", client.config_.dns_servers_[1].toText());
  341. }
  342. /// This test verifies that after a client completes its INFORM exchange,
  343. /// appropriate statistics are updated.
  344. TEST_F(InformTest, statisticsInform) {
  345. Dhcp4Client client;
  346. // Configure DHCP server.
  347. configure(INFORM_CONFIGS[0], *client.getServer());
  348. // Request some configuration when DHCPINFORM is sent.
  349. client.requestOptions(DHO_LOG_SERVERS, DHO_COOKIE_SERVERS);
  350. // Preconfigure the client with the IP address.
  351. client.createLease(IOAddress("10.0.0.56"), 600);
  352. // Send DHCPINFORM message to the server.
  353. ASSERT_NO_THROW(client.doInform());
  354. // Make sure that the server responded.
  355. ASSERT_TRUE(client.getContext().response_);
  356. Pkt4Ptr resp = client.getContext().response_;
  357. // Make sure that the server has responded with DHCPACK.
  358. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  359. // Ok, let's check the statistics.
  360. using namespace isc::stats;
  361. StatsMgr& mgr = StatsMgr::instance();
  362. ObservationPtr pkt4_received = mgr.getObservation("pkt4-received");
  363. ObservationPtr pkt4_inform_received = mgr.getObservation("pkt4-inform-received");
  364. ObservationPtr pkt4_ack_sent = mgr.getObservation("pkt4-ack-sent");
  365. ObservationPtr pkt4_sent = mgr.getObservation("pkt4-sent");
  366. // All expected statistics must be present.
  367. ASSERT_TRUE(pkt4_received);
  368. ASSERT_TRUE(pkt4_inform_received);
  369. ASSERT_TRUE(pkt4_ack_sent);
  370. ASSERT_TRUE(pkt4_sent);
  371. // And they must have expected values.
  372. EXPECT_EQ(1, pkt4_received->getInteger().first);
  373. EXPECT_EQ(1, pkt4_inform_received->getInteger().first);
  374. EXPECT_EQ(1, pkt4_ack_sent->getInteger().first);
  375. EXPECT_EQ(1, pkt4_sent->getInteger().first);
  376. // Let the client send iform 4 times, which should make the server
  377. // to send 4 acks.
  378. ASSERT_NO_THROW(client.doInform());
  379. ASSERT_NO_THROW(client.doInform());
  380. ASSERT_NO_THROW(client.doInform());
  381. ASSERT_NO_THROW(client.doInform());
  382. // Let's see if the stats are properly updated.
  383. EXPECT_EQ(5, pkt4_received->getInteger().first);
  384. EXPECT_EQ(5, pkt4_inform_received->getInteger().first);
  385. EXPECT_EQ(5, pkt4_ack_sent->getInteger().first);
  386. EXPECT_EQ(5, pkt4_sent->getInteger().first);
  387. }
  388. } // end of anonymous namespace