dora_unittest.cc 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. // Copyright (C) 2014-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 <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 <dhcpsrv/cfgmgr.h>
  12. #include <dhcpsrv/host.h>
  13. #include <dhcpsrv/host_mgr.h>
  14. #include <dhcpsrv/subnet_id.h>
  15. #include <dhcpsrv/testutils/cql_schema.h>
  16. #include <dhcpsrv/testutils/mysql_schema.h>
  17. #include <dhcpsrv/testutils/pgsql_schema.h>
  18. #include <dhcp4/tests/dhcp4_test_utils.h>
  19. #include <dhcp4/tests/dhcp4_client.h>
  20. #include <boost/shared_ptr.hpp>
  21. #include <stats/stats_mgr.h>
  22. using namespace isc;
  23. using namespace isc::asiolink;
  24. using namespace isc::data;
  25. using namespace isc::dhcp;
  26. using namespace isc::dhcp::test;
  27. namespace {
  28. /// @brief Set of JSON configurations used throughout the DORA tests.
  29. ///
  30. /// - Configuration 0:
  31. /// - Used for testing direct traffic
  32. /// - 1 subnet: 10.0.0.0/24
  33. /// - 1 pool: 10.0.0.10-10.0.0.100
  34. /// - Router option present: 10.0.0.200 and 10.0.0.201
  35. /// - Domain Name Server option present: 10.0.0.202, 10.0.0.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. ///
  39. /// - Configuration 1:
  40. /// - Use for testing relayed messages
  41. /// - 1 subnet: 192.0.2.0/24
  42. /// - Router option present: 192.0.2.200 and 192.0.2.201
  43. /// - Domain Name Server option present: 192.0.2.202, 192.0.2.203.
  44. /// - Log Servers option present: 192.0.2.200 and 192.0.2.201
  45. /// - Quotes Servers option present: 192.0.2.202, 192.0.2.203.
  46. ///
  47. /// - Configuration 2:
  48. /// - Use for testing simple scenarios with host reservations
  49. /// - 1 subnet: 10.0.0.0/24
  50. /// - One reservation for the client using MAC address:
  51. /// aa:bb:cc:dd:ee:ff, reserved address 10.0.0.7
  52. ///
  53. /// - Configuration 3:
  54. /// - Use for testing match-client-id flag
  55. /// - 1 subnet: 10.0.0.0/24
  56. /// - 1 pool: 10.0.0.10-10.0.0.100
  57. /// - match-client-id flag is set to false, thus the server
  58. /// uses HW address for lease lookup, rather than client id
  59. ///
  60. /// - Configuration 4:
  61. /// - Used for testing host reservations where circuit-id takes precedence
  62. /// over hw-address, and the hw-address takes precedence over duid.
  63. /// - 1 subnet: 10.0.0.0/24
  64. /// - 3 reservations for this subnet:
  65. /// - IP address 10.0.0.7 for HW address aa:bb:cc:dd:ee:ff
  66. /// - IP address 10.0.0.8 for DUID 01:02:03:04:05
  67. /// - IP address 10.0.0.9 for circuit-id 'charter950'
  68. /// - IP address 10.0.0.1 for client-id
  69. ///
  70. /// - Configuration 5:
  71. /// - The same as configuration 4, but using the following order of
  72. /// host-reservation-identifiers: duid, circuit-id, hw-address,
  73. /// client-id.
  74. ///
  75. /// - Configuration 6:
  76. /// - This configuration provides reservations for next-server,
  77. /// server-hostname and boot-file-name value.
  78. /// - 1 subnet: 10.0.0.0/24
  79. /// - 1 reservation for this subnet:
  80. /// - Client's HW address: aa:bb:cc:dd:ee:ff
  81. /// - next-server = 10.0.0.7
  82. /// - server name = "some-name.example.org"
  83. /// - boot-file-name = "bootfile.efi"
  84. ///
  85. /// - Configuration 7:
  86. /// - Simple configuration with a single subnet and single pool
  87. /// - Using MySQL lease database backend to store leases
  88. ///
  89. /// - Configuration 8:
  90. /// - Simple configuration with a single subnet and single pool
  91. /// - Using PostgreSQL lease database backend to store leases
  92. ///
  93. /// - Configuration 9:
  94. /// - Simple configuration with a single subnet and single pool
  95. /// - Using Cassandra lease database backend to store leases
  96. const char* DORA_CONFIGS[] = {
  97. // Configuration 0
  98. "{ \"interfaces-config\": {"
  99. " \"interfaces\": [ \"*\" ]"
  100. "},"
  101. "\"valid-lifetime\": 600,"
  102. "\"subnet4\": [ { "
  103. " \"subnet\": \"10.0.0.0/24\", "
  104. " \"id\": 1,"
  105. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  106. " \"option-data\": [ {"
  107. " \"name\": \"routers\","
  108. " \"data\": \"10.0.0.200,10.0.0.201\""
  109. " },"
  110. " {"
  111. " \"name\": \"domain-name-servers\","
  112. " \"data\": \"10.0.0.202,10.0.0.203\""
  113. " },"
  114. " {"
  115. " \"name\": \"log-servers\","
  116. " \"data\": \"10.0.0.200,10.0.0.201\""
  117. " },"
  118. " {"
  119. " \"name\": \"cookie-servers\","
  120. " \"data\": \"10.0.0.202,10.0.0.203\""
  121. " } ]"
  122. " } ]"
  123. "}",
  124. // Configuration 1
  125. "{ \"interfaces-config\": {"
  126. " \"interfaces\": [ \"*\" ]"
  127. "},"
  128. "\"valid-lifetime\": 600,"
  129. "\"subnet4\": [ { "
  130. " \"subnet\": \"192.0.2.0/24\", "
  131. " \"option-data\": [ {"
  132. " \"name\": \"routers\","
  133. " \"data\": \"192.0.2.200,192.0.2.201\""
  134. " },"
  135. " {"
  136. " \"name\": \"domain-name-servers\","
  137. " \"data\": \"192.0.2.202,192.0.2.203\""
  138. " },"
  139. " {"
  140. " \"name\": \"log-servers\","
  141. " \"data\": \"10.0.0.200,10.0.0.201\""
  142. " },"
  143. " {"
  144. " \"name\": \"cookie-servers\","
  145. " \"data\": \"10.0.0.202,10.0.0.203\""
  146. " } ]"
  147. " } ]"
  148. "}",
  149. // Configuration 2
  150. "{ \"interfaces-config\": {"
  151. " \"interfaces\": [ \"*\" ]"
  152. "},"
  153. "\"valid-lifetime\": 600,"
  154. "\"subnet4\": [ { "
  155. " \"subnet\": \"10.0.0.0/24\", "
  156. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  157. " \"reservations\": [ "
  158. " {"
  159. " \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
  160. " \"ip-address\": \"10.0.0.7\""
  161. " },"
  162. " {"
  163. " \"duid\": \"01:02:03:04:05\","
  164. " \"ip-address\": \"10.0.0.8\""
  165. " },"
  166. " {"
  167. " \"circuit-id\": \"'charter950'\","
  168. " \"ip-address\": \"10.0.0.9\""
  169. " },"
  170. " {"
  171. " \"client-id\": \"01:11:22:33:44:55:66\","
  172. " \"ip-address\": \"10.0.0.1\""
  173. " }"
  174. " ]"
  175. "} ]"
  176. "}",
  177. // Configuration 3
  178. "{ \"interfaces-config\": {"
  179. " \"interfaces\": [ \"*\" ]"
  180. "},"
  181. "\"valid-lifetime\": 600,"
  182. "\"match-client-id\": false,"
  183. "\"subnet4\": [ { "
  184. " \"subnet\": \"10.0.0.0/24\", "
  185. " \"id\": 1,"
  186. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  187. " \"option-data\": [ {"
  188. " \"name\": \"routers\","
  189. " \"data\": \"10.0.0.200,10.0.0.201\""
  190. " } ]"
  191. " } ]"
  192. "}",
  193. // Configuration 4
  194. "{ \"interfaces-config\": {"
  195. " \"interfaces\": [ \"*\" ]"
  196. "},"
  197. "\"host-reservation-identifiers\": [ \"circuit-id\", \"hw-address\","
  198. " \"duid\", \"client-id\" ],"
  199. "\"valid-lifetime\": 600,"
  200. "\"subnet4\": [ { "
  201. " \"subnet\": \"10.0.0.0/24\", "
  202. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  203. " \"reservations\": [ "
  204. " {"
  205. " \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
  206. " \"ip-address\": \"10.0.0.7\""
  207. " },"
  208. " {"
  209. " \"duid\": \"01:02:03:04:05\","
  210. " \"ip-address\": \"10.0.0.8\""
  211. " },"
  212. " {"
  213. " \"circuit-id\": \"'charter950'\","
  214. " \"ip-address\": \"10.0.0.9\""
  215. " },"
  216. " {"
  217. " \"client-id\": \"01:11:22:33:44:55:66\","
  218. " \"ip-address\": \"10.0.0.1\""
  219. " }"
  220. " ]"
  221. "} ]"
  222. "}",
  223. // Configuration 5
  224. "{ \"interfaces-config\": {"
  225. " \"interfaces\": [ \"*\" ]"
  226. "},"
  227. "\"host-reservation-identifiers\": [ \"duid\", \"client-id\","
  228. " \"circuit-id\", \"hw-address\" ],"
  229. "\"valid-lifetime\": 600,"
  230. "\"subnet4\": [ { "
  231. " \"subnet\": \"10.0.0.0/24\", "
  232. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  233. " \"reservations\": [ "
  234. " {"
  235. " \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
  236. " \"ip-address\": \"10.0.0.7\""
  237. " },"
  238. " {"
  239. " \"duid\": \"01:02:03:04:05\","
  240. " \"ip-address\": \"10.0.0.8\""
  241. " },"
  242. " {"
  243. " \"circuit-id\": \"'charter950'\","
  244. " \"ip-address\": \"10.0.0.9\""
  245. " },"
  246. " {"
  247. " \"client-id\": \"01:11:22:33:44:55:66\","
  248. " \"ip-address\": \"10.0.0.1\""
  249. " }"
  250. " ]"
  251. "} ]"
  252. "}",
  253. // Configuration 6
  254. "{ \"interfaces-config\": {"
  255. " \"interfaces\": [ \"*\" ]"
  256. "},"
  257. "\"valid-lifetime\": 600,"
  258. "\"next-server\": \"10.0.0.1\","
  259. "\"subnet4\": [ { "
  260. " \"subnet\": \"10.0.0.0/24\", "
  261. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  262. " \"reservations\": [ "
  263. " {"
  264. " \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
  265. " \"next-server\": \"10.0.0.7\","
  266. " \"server-hostname\": \"some-name.example.org\","
  267. " \"boot-file-name\": \"bootfile.efi\""
  268. " }"
  269. " ]"
  270. "} ]"
  271. "}",
  272. // Configuration 7
  273. "{ \"interfaces-config\": {"
  274. " \"interfaces\": [ \"*\" ]"
  275. "},"
  276. "\"lease-database\": {"
  277. " \"type\": \"mysql\","
  278. " \"name\": \"keatest\","
  279. " \"user\": \"keatest\","
  280. " \"password\": \"keatest\""
  281. "},"
  282. "\"valid-lifetime\": 600,"
  283. "\"subnet4\": [ { "
  284. " \"subnet\": \"10.0.0.0/24\", "
  285. " \"id\": 1,"
  286. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]"
  287. " } ]"
  288. "}",
  289. // Configuration 8
  290. "{ \"interfaces-config\": {"
  291. " \"interfaces\": [ \"*\" ]"
  292. "},"
  293. "\"lease-database\": {"
  294. " \"type\": \"postgresql\","
  295. " \"name\": \"keatest\","
  296. " \"user\": \"keatest\","
  297. " \"password\": \"keatest\""
  298. "},"
  299. "\"valid-lifetime\": 600,"
  300. "\"subnet4\": [ { "
  301. " \"subnet\": \"10.0.0.0/24\", "
  302. " \"id\": 1,"
  303. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]"
  304. " } ]"
  305. "}",
  306. // Configuration 9
  307. "{ \"interfaces-config\": {"
  308. " \"interfaces\": [ \"*\" ]"
  309. "},"
  310. "\"lease-database\": {"
  311. " \"type\": \"cql\","
  312. " \"name\": \"keatest\","
  313. " \"user\": \"keatest\","
  314. " \"password\": \"keatest\""
  315. "},"
  316. "\"valid-lifetime\": 600,"
  317. "\"subnet4\": [ { "
  318. " \"subnet\": \"10.0.0.0/24\", "
  319. " \"id\": 1,"
  320. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]"
  321. " } ]"
  322. "}"
  323. };
  324. /// @brief Test fixture class for testing 4-way (DORA) exchanges.
  325. ///
  326. /// @todo Currently there is a limit number of test cases covered here.
  327. /// In the future it is planned that the tests from the
  328. /// dhcp4_srv_unittest.cc will be migrated here and will use the
  329. /// @c Dhcp4Client class.
  330. class DORATest : public Dhcpv4SrvTest {
  331. public:
  332. /// @brief Constructor.
  333. ///
  334. /// Sets up fake interfaces.
  335. DORATest()
  336. : Dhcpv4SrvTest(),
  337. iface_mgr_test_config_(true) {
  338. IfaceMgr::instance().openSockets4();
  339. // Let's wipe all existing statistics.
  340. isc::stats::StatsMgr::instance().removeAll();
  341. }
  342. /// @brief Destructor.
  343. ///
  344. /// Cleans up statistics after the test.
  345. ~DORATest() {
  346. // Let's wipe all existing statistics.
  347. isc::stats::StatsMgr::instance().removeAll();
  348. }
  349. /// @brief Test that server returns the same lease for the client which is
  350. /// sometimes using client identifier, sometimes not.
  351. ///
  352. /// This test checks the server's behavior in the following scenario:
  353. /// - Client identifies itself to the server using HW address, and may use
  354. /// client identifier.
  355. /// - Client performs the 4-way exchange and obtains a lease from the server.
  356. /// - If the client identifier was in use when the client has acquired the lease,
  357. /// the client uses null client identifier in the next exchange with the server.
  358. /// - If the client identifier was not in use when the client has acquired the
  359. /// lease, the client uses client identifier in the next exchange with the
  360. /// server.
  361. /// - When the client contacts the server for the second time using the
  362. /// DHCPDISCOVER the server determines (using HW address) that the client
  363. /// already has a lease and returns this lease to the client.
  364. /// - The client renews the existing lease.
  365. ///
  366. /// @param clientid_a Client identifier when the client initially allocates
  367. /// the lease. An empty value means "no client identifier".
  368. /// @param clientid_b Client identifier when the client sends the DHCPDISCOVER
  369. /// and then DHCPREQUEST to renew lease.
  370. void oneAllocationOverlapTest(const std::string& clientid_a,
  371. const std::string& clientid_b);
  372. /// @brief Test that the client using the same hardware address but
  373. /// multiple client identifiers will obtain multiple leases.
  374. ///
  375. /// This reflects the scenario of the OS installation over the network
  376. /// when BIOS, installer and the host request DHCPv4 lease assignment
  377. /// using the same MAC address/interface but generating different
  378. /// client identifiers.
  379. ///
  380. /// @param config_index Index of the configuration within the
  381. /// @c DORA_CONFIGS array.
  382. void testMultiStageBoot(const unsigned int config_index);
  383. /// @brief Interface Manager's fake configuration control.
  384. IfaceMgrTestConfig iface_mgr_test_config_;
  385. };
  386. /// This test verifies that the client in the SELECTING state can get
  387. /// an address when it doesn't request any specific address in the
  388. /// DHCPDISCOVER message.
  389. TEST_F(DORATest, selectingDoNotRequestAddress) {
  390. Dhcp4Client client(Dhcp4Client::SELECTING);
  391. // Configure DHCP server.
  392. configure(DORA_CONFIGS[0], *client.getServer());
  393. // Perform 4-way exchange with the server but to not request any
  394. // specific address in the DHCPDISCOVER message.
  395. ASSERT_NO_THROW(client.doDORA());
  396. // Make sure that the server responded.
  397. ASSERT_TRUE(client.getContext().response_);
  398. Pkt4Ptr resp = client.getContext().response_;
  399. // Make sure that the server has responded with DHCPACK.
  400. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  401. // Response must not be relayed.
  402. EXPECT_FALSE(resp->isRelayed());
  403. // Make sure that the server id is present.
  404. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  405. // Make sure that the client has got the lease with the requested address.
  406. ASSERT_NE(client.config_.lease_.addr_.toText(), "0.0.0.0");
  407. }
  408. /// This test verifies that multiple clients may use the DHCPv4 server
  409. /// and obtain unique leases.
  410. TEST_F(DORATest, selectingMultipleClients) {
  411. Dhcp4Client client(Dhcp4Client::SELECTING);
  412. // Configure DHCP server.
  413. configure(DORA_CONFIGS[0], *client.getServer());
  414. // Get the first lease.
  415. ASSERT_NO_THROW(client.doDORA());
  416. // Make sure that the server responded.
  417. Pkt4Ptr resp = client.getContext().response_;
  418. ASSERT_TRUE(resp);
  419. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  420. // Store the lease.
  421. Lease4 lease1 = client.config_.lease_;
  422. // Get the lease for a different client.
  423. client.modifyHWAddr();
  424. ASSERT_NO_THROW(client.doDORA());
  425. // Make sure that the server responded.
  426. resp = client.getContext().response_;
  427. ASSERT_TRUE(resp);
  428. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  429. // Store the lease.
  430. Lease4 lease2 = client.config_.lease_;
  431. // Get the lease for a different client.
  432. client.modifyHWAddr();
  433. ASSERT_NO_THROW(client.doDORA());
  434. // Make sure that the server responded.
  435. resp = client.getContext().response_;
  436. ASSERT_TRUE(resp);
  437. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  438. // Store the lease.
  439. Lease4 lease3 = client.config_.lease_;
  440. // Make sure that unique addresses have been assigned.
  441. EXPECT_NE(lease1.addr_, lease2.addr_);
  442. EXPECT_NE(lease2.addr_, lease3.addr_);
  443. EXPECT_NE(lease1.addr_, lease3.addr_);
  444. }
  445. // This test verifies that the client in a SELECTING state can request
  446. // a specific address and that this address will be assigned when
  447. // available. It also tests that if the client requests an address which
  448. // is in use the client will get a different address.
  449. TEST_F(DORATest, selectingRequestAddress) {
  450. Dhcp4Client client(Dhcp4Client::SELECTING);
  451. // Configure DHCP server.
  452. configure(DORA_CONFIGS[0], *client.getServer());
  453. // Perform 4-way exchange with the server.
  454. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  455. IOAddress>(new IOAddress("10.0.0.50"))));
  456. // Make sure that the server responded.
  457. ASSERT_TRUE(client.getContext().response_);
  458. Pkt4Ptr resp = client.getContext().response_;
  459. // Make sure that the server has responded with DHCPACK.
  460. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  461. // Response must not be relayed.
  462. EXPECT_FALSE(resp->isRelayed());
  463. // Make sure that the server id is present.
  464. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  465. // Make sure that the client has got the lease with the requested address.
  466. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  467. // Simulate different client requesting the same address.
  468. client.modifyHWAddr();
  469. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  470. IOAddress>(new IOAddress("10.0.0.50"))));
  471. resp = client.getContext().response_;
  472. // Make sure that the server responded.
  473. ASSERT_TRUE(resp);
  474. // Make sure that the server has responded with DHCPACK.
  475. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  476. // Response must not be relayed.
  477. EXPECT_FALSE(resp->isRelayed());
  478. // Make sure that the server id is present.
  479. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  480. // Make sure that the client has got some address.
  481. EXPECT_NE(client.config_.lease_.addr_.toText(), "0.0.0.0");
  482. // Make sure that the client has got a different address than requested
  483. // as the requested one is already in use.
  484. EXPECT_NE(client.config_.lease_.addr_.toText(), "10.0.0.50");
  485. }
  486. // This test verifies that the client will get the address that it has
  487. // been allocated when the client requests a different address.
  488. TEST_F(DORATest, selectingRequestNonMatchingAddress) {
  489. Dhcp4Client client(Dhcp4Client::SELECTING);
  490. // Configure DHCP server.
  491. configure(DORA_CONFIGS[0], *client.getServer());
  492. // Perform 4-way exchange with the server.
  493. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  494. IOAddress>(new IOAddress("10.0.0.50"))));
  495. // Make sure that the server responded.
  496. ASSERT_TRUE(client.getContext().response_);
  497. Pkt4Ptr resp = client.getContext().response_;
  498. // Make sure that the server has responded with DHCPACK.
  499. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  500. // Response must not be relayed.
  501. EXPECT_FALSE(resp->isRelayed());
  502. // Make sure that the server id is present.
  503. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  504. // Make sure that the client has got the lease with the requested address.
  505. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  506. // Let's request a different address. The server should respond with
  507. // the one that the client already has allocated.
  508. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  509. IOAddress>(new IOAddress("10.0.0.80"))));
  510. // Make sure that the server responded.
  511. ASSERT_TRUE(client.getContext().response_);
  512. resp = client.getContext().response_;
  513. // Make sure that the server has responded with DHCPACK.
  514. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  515. // Response must not be relayed.
  516. EXPECT_FALSE(resp->isRelayed());
  517. // Make sure that the server id is present.
  518. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  519. // Make sure that the client has got the lease with the address that
  520. // the client has recorded in the lease database.
  521. EXPECT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  522. }
  523. // Test that the client in the INIT-REBOOT state can request the IP
  524. // address it has and the address is returned. Also, check that if
  525. // if the client requests invalid address the server sends a DHCPNAK.
  526. TEST_F(DORATest, initRebootRequest) {
  527. Dhcp4Client client(Dhcp4Client::SELECTING);
  528. // Configure DHCP server.
  529. configure(DORA_CONFIGS[0], *client.getServer());
  530. client.includeClientId("11:22");
  531. // Obtain a lease from the server using the 4-way exchange.
  532. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  533. IOAddress>(new IOAddress("10.0.0.50"))));
  534. // Make sure that the server responded.
  535. ASSERT_TRUE(client.getContext().response_);
  536. Pkt4Ptr resp = client.getContext().response_;
  537. // Make sure that the server has responded with DHCPACK.
  538. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  539. // Response must not be relayed.
  540. EXPECT_FALSE(resp->isRelayed());
  541. // Make sure that the server id is present.
  542. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  543. // Make sure that the client has got the lease with the requested address.
  544. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  545. // Client has a lease in the database. Let's transition the client
  546. // to the INIT_REBOOT state so as the client can request the cached
  547. // lease using the DHCPREQUEST message.
  548. client.setState(Dhcp4Client::INIT_REBOOT);
  549. ASSERT_NO_THROW(client.doRequest());
  550. // Make sure that the server responded.
  551. ASSERT_TRUE(client.getContext().response_);
  552. resp = client.getContext().response_;
  553. // Make sure that the server has responded with DHCPACK.
  554. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  555. // Response must not be relayed.
  556. EXPECT_FALSE(resp->isRelayed());
  557. // Make sure that the server id is present.
  558. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  559. // Make sure that the client has got the lease with the requested address.
  560. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  561. // Try to request a different address than the client has. The server
  562. // should respond with DHCPNAK.
  563. client.config_.lease_.addr_ = IOAddress("10.0.0.30");
  564. ASSERT_NO_THROW(client.doRequest());
  565. // Make sure that the server responded.
  566. ASSERT_TRUE(client.getContext().response_);
  567. resp = client.getContext().response_;
  568. EXPECT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  569. // Change client identifier. The server should treat the request
  570. // as a request from unknown client and ignore it.
  571. client.includeClientId("12:34");
  572. ASSERT_NO_THROW(client.doRequest());
  573. ASSERT_FALSE(client.getContext().response_);
  574. // Now let's fix the IP address. The client identifier is still
  575. // invalid so the message should be dropped.
  576. client.config_.lease_.addr_ = IOAddress("10.0.0.50");
  577. ASSERT_NO_THROW(client.doRequest());
  578. ASSERT_FALSE(client.getContext().response_);
  579. // Restore original client identifier.
  580. client.includeClientId("11:22");
  581. // Try to request from a different HW address. This should be successful
  582. // because the client identifier matches.
  583. client.modifyHWAddr();
  584. ASSERT_NO_THROW(client.doRequest());
  585. // Make sure that the server responded.
  586. ASSERT_TRUE(client.getContext().response_);
  587. resp = client.getContext().response_;
  588. // Make sure that the server has responded with DHCPACK.
  589. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  590. // Make sure that the client has got the lease with the requested address.
  591. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  592. }
  593. // Check that the ciaddr returned by the server is correct for DHCPOFFER and
  594. // DHCPNAK according to RFC2131, section 4.3.1.
  595. TEST_F(DORATest, ciaddr) {
  596. Dhcp4Client client(Dhcp4Client::SELECTING);
  597. // Configure DHCP server.
  598. configure(DORA_CONFIGS[0], *client.getServer());
  599. // Force ciaddr of Discover message to be non-zero.
  600. client.ciaddr_.specify(IOAddress("10.0.0.50"));
  601. // Obtain a lease from the server using the 4-way exchange.
  602. ASSERT_NO_THROW(client.doDiscover(boost::shared_ptr<
  603. IOAddress>(new IOAddress("10.0.0.50"))));
  604. // Make sure that the server responded.
  605. ASSERT_TRUE(client.getContext().response_);
  606. Pkt4Ptr resp = client.getContext().response_;
  607. // Make sure that the server has responded with DHCPOFFER.
  608. ASSERT_EQ(DHCPOFFER, static_cast<int>(resp->getType()));
  609. // Make sure ciaddr is not set for DHCPOFFER.
  610. EXPECT_EQ("0.0.0.0", resp->getCiaddr().toText());
  611. // Obtain a lease from the server using the 4-way exchange.
  612. ASSERT_NO_THROW(client.doRequest());
  613. // Make sure that the server responded.
  614. ASSERT_TRUE(client.getContext().response_);
  615. resp = client.getContext().response_;
  616. // Make sure that the server has responded with DHCPACK.
  617. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  618. // Let's transition the client to Renewing state.
  619. client.setState(Dhcp4Client::RENEWING);
  620. // Set the unicast destination address to indicate that it is a renewal.
  621. client.setDestAddress(IOAddress("10.0.0.1"));
  622. ASSERT_NO_THROW(client.doRequest());
  623. // The client is sending invalid ciaddr so the server should send a NAK.
  624. resp = client.getContext().response_;
  625. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  626. // For DHCPACK the ciaddr may be 0 or may be set to the ciaddr value
  627. // from the client's message. Kea sets it to the latter.
  628. EXPECT_EQ("10.0.0.50", resp->getCiaddr().toText());
  629. // Replace the address held by the client. The client will request
  630. // the assignment of this address but the server has a different
  631. // address for this client.
  632. client.ciaddr_.specify(IOAddress("192.168.0.30"));
  633. ASSERT_NO_THROW(client.doRequest());
  634. // The client is sending invalid ciaddr so the server should send a NAK.
  635. resp = client.getContext().response_;
  636. ASSERT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  637. // For DHCPNAK the ciaddr is always 0 (should not be copied) from the
  638. // client's message.
  639. EXPECT_EQ("0.0.0.0", resp->getCiaddr().toText());
  640. }
  641. void
  642. DORATest::oneAllocationOverlapTest(const std::string& clientid_a,
  643. const std::string& clientid_b) {
  644. // Allocate a lease by client using the 4-way exchange.
  645. Dhcp4Client client(Dhcp4Client::SELECTING);
  646. client.includeClientId(clientid_a);
  647. client.setHWAddress("01:02:03:04:05:06");
  648. configure(DORA_CONFIGS[0], *client.getServer());
  649. ASSERT_NO_THROW(client.doDORA());
  650. // Make sure that the server responded.
  651. ASSERT_TRUE(client.getContext().response_);
  652. Pkt4Ptr resp = client.getContext().response_;
  653. // Make sure that the server has responded with DHCPACK.
  654. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  655. Lease4Ptr lease_a = LeaseMgrFactory::instance().getLease4(client.config_.lease_.addr_);
  656. ASSERT_TRUE(lease_a);
  657. // Remember the allocated address.
  658. IOAddress yiaddr = lease_a->addr_;
  659. // Change client identifier. If parameters clientid_a and clientid_b
  660. // are specified correctly, this removes the client identifier from
  661. // client's requests if the lease has been acquired with the client
  662. // identifier, or adds the client identifier otherwise.
  663. client.includeClientId(clientid_b);
  664. // Check if the server will offer the same address.
  665. ASSERT_NO_THROW(client.doDiscover());
  666. resp = client.getContext().response_;
  667. ASSERT_TRUE(resp);
  668. EXPECT_EQ(yiaddr, resp->getYiaddr());
  669. // Client should also be able to renew its address.
  670. client.setState(Dhcp4Client::RENEWING);
  671. ASSERT_NO_THROW(client.doRequest());
  672. ASSERT_TRUE(client.getContext().response_);
  673. resp = client.getContext().response_;
  674. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  675. ASSERT_EQ(yiaddr, client.config_.lease_.addr_);
  676. }
  677. // This test checks the server behavior in the following situation:
  678. // - Client A identifies itself to the server using client identifier
  679. // and the hardware address and requests allocation of the new lease.
  680. // - Server allocates the lease to the client.
  681. // - Client B has the same hardware address but is using a different
  682. // client identifier then Client A.
  683. // - Client B sends DHCPDISCOVER.
  684. // - Server should determine that the client B is not client A, because
  685. // it is using a different client identifier, even though they use the
  686. // same HW address. As a consequence, the server should offer a
  687. // different address to the client B.
  688. // - The client B performs the 4-way exchange again and the server
  689. // allocates a new address to the client, which should be different
  690. // than the address used by the client A.
  691. // - Client B is in the renewing state and it successfully renews its
  692. // address.
  693. // - Client A also renews its address successfully.
  694. TEST_F(DORATest, twoAllocationsOverlap) {
  695. // Allocate a lease by client A using the 4-way exchange.
  696. Dhcp4Client client_a(Dhcp4Client::SELECTING);
  697. client_a.includeClientId("12:34");
  698. client_a.setHWAddress("01:02:03:04:05:06");
  699. configure(DORA_CONFIGS[0], *client_a.getServer());
  700. ASSERT_NO_THROW(client_a.doDORA());
  701. // Make sure that the server responded.
  702. ASSERT_TRUE(client_a.getContext().response_);
  703. Pkt4Ptr resp_a = client_a.getContext().response_;
  704. // Make sure that the server has responded with DHCPACK.
  705. ASSERT_EQ(DHCPACK, static_cast<int>(resp_a->getType()));
  706. // Make sure that the lease has been recorded by the server.
  707. Lease4Ptr lease_a = LeaseMgrFactory::instance().getLease4(client_a.config_.lease_.addr_);
  708. ASSERT_TRUE(lease_a);
  709. // Create client B.
  710. Dhcp4Client client_b(client_a.getServer(), Dhcp4Client::SELECTING);
  711. client_b.setHWAddress("01:02:03:04:05:06");
  712. client_b.includeClientId("45:67");
  713. // Send DHCPDISCOVER and expect the response.
  714. ASSERT_NO_THROW(client_b.doDiscover());
  715. Pkt4Ptr resp_b = client_b.getContext().response_;
  716. // Make sure that the server has responded with DHCPOFFER.
  717. ASSERT_EQ(DHCPOFFER, static_cast<int>(resp_b->getType()));
  718. // The offered address should be different than the address which
  719. // was obtained by the client A.
  720. ASSERT_NE(resp_b->getYiaddr(), client_a.config_.lease_.addr_);
  721. // Make sure that the client A lease hasn't been modified.
  722. lease_a = LeaseMgrFactory::instance().getLease4(client_a.config_.lease_.addr_);
  723. ASSERT_TRUE(lease_a);
  724. // Now that we know that the server will avoid assigning the same
  725. // address that the client A has, use the 4-way exchange to actually
  726. // allocate some address.
  727. ASSERT_NO_THROW(client_b.doDORA());
  728. // Make sure that the server responded.
  729. ASSERT_TRUE(client_b.getContext().response_);
  730. resp_b = client_b.getContext().response_;
  731. // Make sure that the server has responded with DHCPACK.
  732. ASSERT_EQ(DHCPACK, static_cast<int>(resp_b->getType()));
  733. // Again, make sure the assigned addresses are different.
  734. ASSERT_NE(client_b.config_.lease_.addr_, client_a.config_.lease_.addr_);
  735. // Make sure that the client A still has a lease.
  736. lease_a = LeaseMgrFactory::instance().getLease4(client_a.config_.lease_.addr_);
  737. ASSERT_TRUE(lease_a);
  738. // Make sure that the client B has a lease.
  739. Lease4Ptr lease_b = LeaseMgrFactory::instance().getLease4(client_b.config_.lease_.addr_);
  740. ASSERT_TRUE(lease_b);
  741. // Client B should be able to renew its address.
  742. client_b.setState(Dhcp4Client::RENEWING);
  743. ASSERT_NO_THROW(client_b.doRequest());
  744. ASSERT_TRUE(client_b.getContext().response_);
  745. resp_b = client_b.getContext().response_;
  746. ASSERT_EQ(DHCPACK, static_cast<int>(resp_b->getType()));
  747. ASSERT_NE(client_b.config_.lease_.addr_, client_a.config_.lease_.addr_);
  748. // Client A should also be able to renew its address.
  749. client_a.setState(Dhcp4Client::RENEWING);
  750. ASSERT_NO_THROW(client_a.doRequest());
  751. ASSERT_TRUE(client_a.getContext().response_);
  752. resp_b = client_a.getContext().response_;
  753. ASSERT_EQ(DHCPACK, static_cast<int>(resp_b->getType()));
  754. ASSERT_NE(client_a.config_.lease_.addr_, client_b.config_.lease_.addr_);
  755. }
  756. // This test checks the server behavior in the following situation:
  757. // - Client A identifies itself to the server using the hardware address
  758. // and client identifier.
  759. // - Client A performs the 4-way exchange and obtains a lease from the server.
  760. // - Client B uses the same HW address as the client A, but it doesn't use
  761. // the client identifier.
  762. // - Client B sends the DHCPDISCOVER to the server.
  763. // The server determines that there is a lease for the client A using the
  764. // same HW address as the client B. Server discards the client's message and
  765. // doesn't offer the lease for the client B to prevent allocation of the
  766. // lease without a unique identifier.
  767. // - The client sends the DHCPREQUEST and the server sends the DHCPNAK for the
  768. // same reason.
  769. // - Client A renews its address successfully.
  770. TEST_F(DORATest, oneAllocationOverlap1) {
  771. oneAllocationOverlapTest("12:34", "");
  772. }
  773. // This test is similar to oneAllocationOverlap2 but this time the client A
  774. // uses no client identifier, and the client B uses the HW address and the
  775. // client identifier. The server behaves as previously.
  776. TEST_F(DORATest, oneAllocationOverlap2) {
  777. oneAllocationOverlapTest("", "12:34");
  778. }
  779. // This is a simple test for the host reservation. It creates a reservation
  780. // for an address for a single client, identified by the HW address. The
  781. // test verifies that the client using this HW address will obtain a
  782. // lease for the reserved address. It also checks that the client using
  783. // a different HW address will obtain an address from the dynamic pool.
  784. TEST_F(DORATest, reservation) {
  785. // Client A is a one which will have a reservation.
  786. Dhcp4Client clientA(Dhcp4Client::SELECTING);
  787. // Set explicit HW address so as it matches the reservation in the
  788. // configuration used below.
  789. clientA.setHWAddress("aa:bb:cc:dd:ee:ff");
  790. // Configure DHCP server.
  791. configure(DORA_CONFIGS[2], *clientA.getServer());
  792. // Client A performs 4-way exchange and should obtain a reserved
  793. // address.
  794. ASSERT_NO_THROW(clientA.doDORA(boost::shared_ptr<
  795. IOAddress>(new IOAddress("0.0.0.0"))));
  796. // Make sure that the server responded.
  797. ASSERT_TRUE(clientA.getContext().response_);
  798. Pkt4Ptr resp = clientA.getContext().response_;
  799. // Make sure that the server has responded with DHCPACK.
  800. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  801. // Make sure that the client has got the lease for the reserved address.
  802. ASSERT_EQ("10.0.0.7", clientA.config_.lease_.addr_.toText());
  803. // Client B uses the same server as Client A.
  804. Dhcp4Client clientB(clientA.getServer(), Dhcp4Client::SELECTING);
  805. // Client B has no reservation so it should get the lease from
  806. // the dynamic pool.
  807. ASSERT_NO_THROW(clientB.doDORA(boost::shared_ptr<
  808. IOAddress>(new IOAddress("0.0.0.0"))));
  809. // Make sure that the server responded.
  810. ASSERT_TRUE(clientB.getContext().response_);
  811. resp = clientB.getContext().response_;
  812. // Make sure that the server has responded with DHCPACK.
  813. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  814. // Obtain the subnet to which the returned address belongs.
  815. Subnet4Ptr subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->
  816. selectSubnet(clientB.config_.lease_.addr_);
  817. ASSERT_TRUE(subnet);
  818. // Make sure that the address has been allocated from the dynamic pool.
  819. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, clientB.config_.lease_.addr_));
  820. }
  821. // This test checks that it is possible to make a reservation by
  822. // DUID carried in the Client Identifier option.
  823. TEST_F(DORATest, reservationByDUID) {
  824. Dhcp4Client client(Dhcp4Client::SELECTING);
  825. // Use relay agent.
  826. client.useRelay(true, IOAddress("10.0.0.1"), IOAddress("10.0.0.2"));
  827. // Modify HW address so as the server doesn't assign reserved
  828. // address by HW address.
  829. client.modifyHWAddr();
  830. // Specify DUID for which address 10.0.0.8 is reserved.
  831. // The value specified as client id includes:
  832. // - FF is a client identifier type for DUID,
  833. // - 45454545 - represents 4 bytes for IAID
  834. // - 01:02:03:04:05 - is an actual DUID for which there is a
  835. // reservation.
  836. client.includeClientId("FF:45:45:45:45:01:02:03:04:05");
  837. // Configure DHCP server.
  838. configure(DORA_CONFIGS[2], *client.getServer());
  839. // Client A performs 4-way exchange and should obtain a reserved
  840. // address.
  841. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  842. IOAddress>(new IOAddress("0.0.0.0"))));
  843. // Make sure that the server responded.
  844. ASSERT_TRUE(client.getContext().response_);
  845. Pkt4Ptr resp = client.getContext().response_;
  846. // Make sure that the server has responded with DHCPACK.
  847. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  848. // Make sure that the client has got the lease for the reserved address.
  849. ASSERT_EQ("10.0.0.8", client.config_.lease_.addr_.toText());
  850. }
  851. // This test checks that it is possible to make a reservation by
  852. // circuit-id inserted by the relay agent.
  853. TEST_F(DORATest, reservationByCircuitId) {
  854. Dhcp4Client client(Dhcp4Client::SELECTING);
  855. // Use relay agent so as the circuit-id can be inserted.
  856. client.useRelay(true, IOAddress("10.0.0.1"), IOAddress("10.0.0.2"));
  857. // Specify circuit-id.
  858. client.setCircuitId("charter950");
  859. // Configure DHCP server.
  860. configure(DORA_CONFIGS[2], *client.getServer());
  861. // Client A performs 4-way exchange and should obtain a reserved
  862. // address.
  863. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  864. IOAddress>(new IOAddress("0.0.0.0"))));
  865. // Make sure that the server responded.
  866. ASSERT_TRUE(client.getContext().response_);
  867. Pkt4Ptr resp = client.getContext().response_;
  868. // Make sure that the server has responded with DHCPACK.
  869. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  870. // Make sure that the client has got the lease for the reserved address.
  871. ASSERT_EQ("10.0.0.9", client.config_.lease_.addr_.toText());
  872. }
  873. // This test checks that it is possible to make a reservation by
  874. // client-id.
  875. TEST_F(DORATest, reservationByClientId) {
  876. Dhcp4Client client(Dhcp4Client::SELECTING);
  877. // Use relay agent to make sure that the desired subnet is
  878. // selected for our client.
  879. client.useRelay(true, IOAddress("10.0.0.20"), IOAddress("10.0.0.21"));
  880. // Specify client identifier.
  881. client.includeClientId("01:11:22:33:44:55:66");
  882. // Configure DHCP server.
  883. configure(DORA_CONFIGS[2], *client.getServer());
  884. // Client A performs 4-way exchange and should obtain a reserved
  885. // address.
  886. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  887. IOAddress>(new IOAddress("0.0.0.0"))));
  888. // Make sure that the server responded.
  889. ASSERT_TRUE(client.getContext().response_);
  890. Pkt4Ptr resp = client.getContext().response_;
  891. // Make sure that the server has responded with DHCPACK.
  892. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  893. // Make sure that the client has got the lease for the reserved address.
  894. ASSERT_EQ("10.0.0.1", client.config_.lease_.addr_.toText());
  895. }
  896. // This test verifies that order in which host identifiers are used to
  897. // retrieve host reservations can be controlled.
  898. TEST_F(DORATest, hostIdentifiersOrder) {
  899. Dhcp4Client client(Dhcp4Client::SELECTING);
  900. client.setHWAddress("aa:bb:cc:dd:ee:ff");
  901. // Use relay agent so as the circuit-id can be inserted.
  902. client.useRelay(true, IOAddress("10.0.0.1"), IOAddress("10.0.0.2"));
  903. // Specify DUID for which address 10.0.0.8 is reserved.
  904. // The value specified as client id includes:
  905. // - FF is a client identifier type for DUID,
  906. // - 45454545 - represents 4 bytes for IAID
  907. // - 01:02:03:04:05 - is an actual DUID for which there is a
  908. // reservation.
  909. client.includeClientId("FF:45:45:45:45:01:02:03:04:05");
  910. // Specify circuit-id.
  911. client.setCircuitId("charter950");
  912. // Configure DHCP server.
  913. configure(DORA_CONFIGS[2], *client.getServer());
  914. // Perform 4-way exchange to obtain reserved address.
  915. // The client has in fact two reserved addresses, but the one assigned
  916. // should be by hw-address.
  917. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  918. IOAddress>(new IOAddress("0.0.0.0"))));
  919. // Make sure that the server responded.
  920. ASSERT_TRUE(client.getContext().response_);
  921. Pkt4Ptr resp = client.getContext().response_;
  922. // Make sure that the server has responded with DHCPACK.
  923. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  924. // Make sure that the client has got the lease for the reserved address.
  925. ASSERT_EQ("10.0.0.7", client.config_.lease_.addr_.toText());
  926. // Reconfigure the server to change the preference order of the
  927. // host identifiers. The 'circuit-id' should now take precedence over
  928. // the hw-address, duid and client-id.
  929. configure(DORA_CONFIGS[4], *client.getServer());
  930. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  931. IOAddress>(new IOAddress("0.0.0.0"))));
  932. // Make sure that the server responded.
  933. ASSERT_TRUE(client.getContext().response_);
  934. resp = client.getContext().response_;
  935. // Make sure that the server has responded with DHCPACK.
  936. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  937. // Make sure that the client has got the lease for the reserved address.
  938. ASSERT_EQ("10.0.0.9", client.config_.lease_.addr_.toText());
  939. // Reconfigure the server to change the preference order of the
  940. // host identifiers. The 'duid' should now take precedence over
  941. // the client-id, hw-address and circuit-id
  942. configure(DORA_CONFIGS[5], *client.getServer());
  943. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  944. IOAddress>(new IOAddress("0.0.0.0"))));
  945. // Make sure that the server responded.
  946. ASSERT_TRUE(client.getContext().response_);
  947. resp = client.getContext().response_;
  948. // Make sure that the server has responded with DHCPACK.
  949. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  950. // Make sure that the client has got the lease for the reserved address.
  951. ASSERT_EQ("10.0.0.8", client.config_.lease_.addr_.toText());
  952. // Replace the client identifier with the one for which address
  953. // 10.0.0.1 is reserved. Because the DUID is a special type of
  954. // client identifier, this change effectively removes the association
  955. // of the client with the DUID for which address 10.0.0.8 is reserved.
  956. // The next identifier type to be used by the server (after DUID) is
  957. // client-id and thus the server should assign address 10.0.0.1.
  958. client.includeClientId("01:11:22:33:44:55:66");
  959. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  960. IOAddress>(new IOAddress("0.0.0.0"))));
  961. // Make sure that the server responded.
  962. ASSERT_TRUE(client.getContext().response_);
  963. resp = client.getContext().response_;
  964. // Make sure that the server has responded with DHCPACK.
  965. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  966. // Make sure that the client has got the lease for the reserved address.
  967. ASSERT_EQ("10.0.0.1", client.config_.lease_.addr_.toText());
  968. }
  969. // This test checks that setting the match-client-id value to false causes
  970. // the server to ignore changing client identifier when the client is
  971. // using consistent HW address.
  972. TEST_F(DORATest, ignoreChangingClientId) {
  973. Dhcp4Client client(Dhcp4Client::SELECTING);
  974. // Configure DHCP server.
  975. configure(DORA_CONFIGS[3], *client.getServer());
  976. client.includeClientId("12:12");
  977. // Obtain the lease using 4-way exchange.
  978. ASSERT_NO_THROW(client.doDORA());
  979. // Make sure that the server responded.
  980. ASSERT_TRUE(client.getContext().response_);
  981. Pkt4Ptr resp = client.getContext().response_;
  982. // Make sure that the server has responded with DHCPACK.
  983. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  984. EXPECT_FALSE(client.config_.lease_.client_id_);
  985. // Remember address which the client has obtained.
  986. IOAddress leased_address = client.config_.lease_.addr_;
  987. // Modify client id. Because we have set the configuration flag which
  988. // forces the server to lookup leases using the HW address, the
  989. // client id modification should not matter and the client should
  990. // obtain the same lease.
  991. client.includeClientId("14:14");
  992. // Obtain the lease using 4-way exchange.
  993. ASSERT_NO_THROW(client.doDORA());
  994. // Make sure that the server responded.
  995. ASSERT_TRUE(client.getContext().response_);
  996. resp = client.getContext().response_;
  997. // Make sure that the server has responded with DHCPACK.
  998. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  999. // Make sure that the server assigned the same address, even though the
  1000. // client id has changed.
  1001. EXPECT_EQ(leased_address, client.config_.lease_.addr_);
  1002. // Check that the client id is not present in the lease.
  1003. EXPECT_FALSE(client.config_.lease_.client_id_);
  1004. }
  1005. // This test checks that the match-client-id parameter doesn't have
  1006. // effect on the lease lookup using the HW address.
  1007. TEST_F(DORATest, changingHWAddress) {
  1008. Dhcp4Client client(Dhcp4Client::SELECTING);
  1009. // Configure DHCP server.
  1010. configure(DORA_CONFIGS[3], *client.getServer());
  1011. client.includeClientId("12:12");
  1012. client.setHWAddress("00:01:02:03:04:05");
  1013. // Obtain the lease using 4-way exchange.
  1014. ASSERT_NO_THROW(client.doDORA());
  1015. // Make sure that the server responded.
  1016. ASSERT_TRUE(client.getContext().response_);
  1017. Pkt4Ptr resp = client.getContext().response_;
  1018. // Make sure that the server has responded with DHCPACK.
  1019. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1020. // Check that the client id is not present in the lease.
  1021. EXPECT_FALSE(client.config_.lease_.client_id_);
  1022. // Remember address which the client has obtained.
  1023. IOAddress leased_address = client.config_.lease_.addr_;
  1024. // Modify HW address but leave client id in place. The value of the
  1025. // match-client-id set to false must not have any effect on the
  1026. // case when the HW address is changing. In such case the server will
  1027. // allocate the new address for the client.
  1028. client.setHWAddress("01:01:01:01:01:01");
  1029. // Obtain a lease.
  1030. ASSERT_NO_THROW(client.doDORA());
  1031. // Make sure that the server responded.
  1032. ASSERT_TRUE(client.getContext().response_);
  1033. resp = client.getContext().response_;
  1034. // Make sure that the server has responded with DHCPACK.
  1035. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1036. // Client must assign different address because the client id is
  1037. // ignored and the HW address was changed.
  1038. EXPECT_NE(client.config_.lease_.addr_, leased_address);
  1039. // Check that the client id is not present in the lease.
  1040. EXPECT_FALSE(client.config_.lease_.client_id_);
  1041. }
  1042. // This test verifies that the server assigns reserved values for the
  1043. // siaddr, sname and file fields carried within DHCPv4 message.
  1044. TEST_F(DORATest, messageFieldsReservations) {
  1045. // Client has a reservation.
  1046. Dhcp4Client client(Dhcp4Client::SELECTING);
  1047. // Set explicit HW address so as it matches the reservation in the
  1048. // configuration used below.
  1049. client.setHWAddress("aa:bb:cc:dd:ee:ff");
  1050. // Configure DHCP server.
  1051. configure(DORA_CONFIGS[6], *client.getServer());
  1052. // Client performs 4-way exchange and should obtain a reserved
  1053. // address and fixed fields.
  1054. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  1055. IOAddress>(new IOAddress("0.0.0.0"))));
  1056. // Make sure that the server responded.
  1057. ASSERT_TRUE(client.getContext().response_);
  1058. Pkt4Ptr resp = client.getContext().response_;
  1059. // Make sure that the server has responded with DHCPACK.
  1060. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1061. // Check that the reserved values have been assigned.
  1062. EXPECT_EQ("10.0.0.7", client.config_.siaddr_.toText());
  1063. EXPECT_EQ("some-name.example.org", client.config_.sname_);
  1064. EXPECT_EQ("bootfile.efi", client.config_.boot_file_name_);
  1065. }
  1066. // This test checks the following scenario:
  1067. // 1. Client A performs 4-way exchange and obtains a lease from the dynamic pool.
  1068. // 2. Reservation is created for the client A using an address out of the dynamic
  1069. // pool.
  1070. // 3. Client A renews the lease.
  1071. // 4. Server responds with DHCPNAK to indicate that the client should stop using
  1072. // an address for which it has a lease. Server doesn't want to renew an
  1073. // address for which the client doesn't have a reservation, while it has
  1074. // a reservation for a different address.
  1075. // 5. Client A receives a DHCPNAK and returns to the DHCP server discovery.
  1076. // 6. Client A performs a 4-way exchange with a server and the server allocates
  1077. // a reserved address to the Client A.
  1078. // 7. Client A renews the allocated address and the server returns a DHCPACK.
  1079. // 8. Reservation for the Client A is removed.
  1080. // 9. Client A renews the (previously reserved) lease and the server returns
  1081. // DHCPNAK because the address in use is neither reserved nor belongs to
  1082. // the dynamic pool.
  1083. // 10. Client A returns to the DHCP server discovery.
  1084. // 11. Client A uses 4-way exchange to obtain a lease from the dynamic pool.
  1085. // 12. The new address that the Client A is using is reserved for Client B.
  1086. // Client A still holds this address.
  1087. // 13. Client B uses 4-way exchange to obtain a new lease.
  1088. // 14. The server determines that the Client B has a reservation for the
  1089. // address which is in use by Client A and offers an address different
  1090. // than reserved.
  1091. // 15. Client B requests the allocation of the offered address and the server
  1092. // allocates this address.
  1093. // 16. Client A renews the lease.
  1094. // 17. The server determines that the address that Client A is using is reserved
  1095. // for Client B. The server returns DHCPNAK to the Client A.
  1096. // 18. Client B uses 4-way exchange to obtain the reserved lease but the lease
  1097. // for the Client A hasn't been removed yet. Client B is assigned the same
  1098. // address it has been using.
  1099. // 19. Client A uses 4-way exchange to allocate a new lease.
  1100. // 20. The server allocates a new lease from the dynamic pool but it avoids
  1101. // allocating the address reserved for the Client B.
  1102. // 21. Client B uses 4-way exchange to obtain a new lease.
  1103. // 22. The server finally allocates a reserved address to the Client B.
  1104. TEST_F(DORATest, reservationsWithConflicts) {
  1105. Dhcp4Client client(Dhcp4Client::SELECTING);
  1106. // Configure DHCP server.
  1107. configure(DORA_CONFIGS[0], *client.getServer());
  1108. // Client A performs 4-way exchange and obtains a lease from the
  1109. // dynamic pool.
  1110. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  1111. IOAddress>(new IOAddress("10.0.0.50"))));
  1112. // Make sure that the server responded.
  1113. ASSERT_TRUE(client.getContext().response_);
  1114. Pkt4Ptr resp = client.getContext().response_;
  1115. // Make sure that the server has responded with DHCPACK.
  1116. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1117. // Make sure that the client has got the lease with the requested address.
  1118. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  1119. configure(DORA_CONFIGS[0], false);
  1120. // Reservation is created for the client A using an address out of the
  1121. // dynamic pool.
  1122. HostPtr host(new Host(&client.getHWAddress()->hwaddr_[0],
  1123. client.getHWAddress()->hwaddr_.size(),
  1124. Host::IDENT_HWADDR, SubnetID(1),
  1125. SubnetID(0), IOAddress("10.0.0.9")));
  1126. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  1127. CfgMgr::instance().commit();
  1128. // Let's transition the client to Renewing state.
  1129. client.setState(Dhcp4Client::RENEWING);
  1130. // Set the unicast destination address to indicate that it is a renewal.
  1131. client.setDestAddress(IOAddress("10.0.0.1"));
  1132. ASSERT_NO_THROW(client.doRequest());
  1133. // Client should get the DHCPNAK from the server because the client has
  1134. // a reservation for a different address that it is trying to renew.
  1135. resp = client.getContext().response_;
  1136. ASSERT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  1137. // A conforming client would go back to the server discovery.
  1138. client.setState(Dhcp4Client::SELECTING);
  1139. // Obtain a lease from the server using the 4-way exchange.
  1140. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  1141. IOAddress>(new IOAddress("0.0.0.0"))));
  1142. // Make sure that the server responded.
  1143. ASSERT_TRUE(client.getContext().response_);
  1144. resp = client.getContext().response_;
  1145. // Make sure that the server has responded with DHCPACK with a reserved
  1146. // address
  1147. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1148. ASSERT_EQ("10.0.0.9", client.config_.lease_.addr_.toText());
  1149. // Client A renews the allocated address.
  1150. client.setState(Dhcp4Client::RENEWING);
  1151. // Set the unicast destination address to indicate that it is a renewal.
  1152. client.setDestAddress(IOAddress("10.0.0.1"));
  1153. ASSERT_NO_THROW(client.doRequest());
  1154. // Make sure the server responded and renewed the client's address.
  1155. resp = client.getContext().response_;
  1156. ASSERT_EQ("10.0.0.9", client.config_.lease_.addr_.toText());
  1157. // By reconfiguring the server, we remove the existing reservations.
  1158. configure(DORA_CONFIGS[0]);
  1159. // Try to renew the existing lease again.
  1160. ASSERT_NO_THROW(client.doRequest());
  1161. // The reservation has been removed, so the server should respond with
  1162. // a DHCPNAK because the address that the client is using doesn't belong
  1163. // to a dynamic pool.
  1164. resp = client.getContext().response_;
  1165. ASSERT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  1166. // A conforming client would go back to the server discovery.
  1167. client.setState(Dhcp4Client::SELECTING);
  1168. // Obtain a lease from the server using the 4-way exchange.
  1169. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  1170. IOAddress>(new IOAddress("0.0.0.0"))));
  1171. // Make sure that the server responded.
  1172. ASSERT_TRUE(client.getContext().response_);
  1173. resp = client.getContext().response_;
  1174. // Make sure that the server has responded with DHCPACK.
  1175. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1176. // Obtain the subnet to which the returned address belongs.
  1177. Subnet4Ptr subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->
  1178. selectSubnet(client.config_.lease_.addr_);
  1179. ASSERT_TRUE(subnet);
  1180. // Make sure that the address has been allocated from the dynamic pool.
  1181. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, client.config_.lease_.addr_));
  1182. // Remember the address allocated in the dynamic pool.
  1183. IOAddress in_pool_addr = client.config_.lease_.addr_;
  1184. // Create Client B.
  1185. Dhcp4Client clientB(client.getServer());
  1186. clientB.modifyHWAddr();
  1187. // Create reservation for the Client B, for the address that the
  1188. // Client A is using.
  1189. configure(DORA_CONFIGS[0], false);
  1190. host.reset(new Host(&clientB.getHWAddress()->hwaddr_[0],
  1191. clientB.getHWAddress()->hwaddr_.size(),
  1192. Host::IDENT_HWADDR, SubnetID(1),
  1193. SubnetID(0), in_pool_addr));
  1194. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  1195. CfgMgr::instance().commit();
  1196. // Client B performs a DHCPDISCOVER.
  1197. clientB.setState(Dhcp4Client::SELECTING);
  1198. // The server determines that the address reserved for Client B is
  1199. // in use by Client A so it offers a different address.
  1200. ASSERT_NO_THROW(clientB.doDORA(boost::shared_ptr<
  1201. IOAddress>(new IOAddress("0.0.0.0"))));
  1202. ASSERT_TRUE(clientB.getContext().response_);
  1203. ASSERT_EQ(DHCPACK, static_cast<int>(clientB.getContext().response_->getType()));
  1204. IOAddress client_b_addr = clientB.config_.lease_.addr_;
  1205. ASSERT_NE(client_b_addr, in_pool_addr);
  1206. // Client A renews the lease.
  1207. client.setState(Dhcp4Client::RENEWING);
  1208. // Set the unicast destination address to indicate that it is a renewal.
  1209. client.setDestAddress(IOAddress(in_pool_addr));
  1210. ASSERT_NO_THROW(client.doRequest());
  1211. // Client A should get a DHCPNAK because it is using an address reserved
  1212. // for Client B.
  1213. resp = client.getContext().response_;
  1214. ASSERT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  1215. // Client B performs 4-way exchange but still gets an address from the
  1216. // dynamic pool, because Client A hasn't obtained a new lease, so it is
  1217. // still using an address reserved for Client B.
  1218. clientB.setState(Dhcp4Client::SELECTING);
  1219. // Obtain a lease from the server using the 4-way exchange.
  1220. ASSERT_NO_THROW(clientB.doDORA(boost::shared_ptr<
  1221. IOAddress>(new IOAddress("0.0.0.0"))));
  1222. // Make sure that the server responded.
  1223. ASSERT_TRUE(clientB.getContext().response_);
  1224. ASSERT_EQ(DHCPACK, static_cast<int>(clientB.getContext().response_->getType()));
  1225. ASSERT_NE(clientB.config_.lease_.addr_, in_pool_addr);
  1226. ASSERT_EQ(client_b_addr, clientB.config_.lease_.addr_);
  1227. // Client B renews its lease.
  1228. clientB.setState(Dhcp4Client::RENEWING);
  1229. clientB.setDestAddress(IOAddress("10.0.0.1"));
  1230. ASSERT_NO_THROW(clientB.doRequest());
  1231. // The server should renew the client's B lease because the address
  1232. // reserved for client B is still in use by the client A.
  1233. ASSERT_TRUE(clientB.getContext().response_);
  1234. EXPECT_EQ(DHCPACK, static_cast<int>(clientB.getContext().response_->getType()));
  1235. ASSERT_NE(clientB.config_.lease_.addr_, in_pool_addr);
  1236. ASSERT_EQ(client_b_addr, clientB.config_.lease_.addr_);
  1237. // Client A performs 4-way exchange.
  1238. client.setState(Dhcp4Client::SELECTING);
  1239. // Revert to the broadcast address for the selecting client.
  1240. client.setDestAddress(IOAddress::IPV4_BCAST_ADDRESS());
  1241. // Obtain a lease from the server using the 4-way exchange.
  1242. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  1243. IOAddress>(new IOAddress("0.0.0.0"))));
  1244. // Make sure that the server responded.
  1245. ASSERT_TRUE(client.getContext().response_);
  1246. resp = client.getContext().response_;
  1247. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1248. // The server should have assigned a different address than the one
  1249. // reserved for the Client B.
  1250. ASSERT_NE(client.config_.lease_.addr_, in_pool_addr);
  1251. subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->
  1252. selectSubnet(client.config_.lease_.addr_);
  1253. ASSERT_TRUE(subnet);
  1254. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, client.config_.lease_.addr_));
  1255. // Client B renews again.
  1256. ASSERT_NO_THROW(clientB.doRequest());
  1257. // The client B should now receive the DHCPNAK from the server because
  1258. // the reserved address is now available and the client should
  1259. // revert to the DHCPDISCOVER to obtain it.
  1260. ASSERT_TRUE(clientB.getContext().response_);
  1261. EXPECT_EQ(DHCPNAK, static_cast<int>(clientB.getContext().response_->getType()));
  1262. // Client B performs 4-way exchange and obtains a lease for the
  1263. // reserved address.
  1264. clientB.setState(Dhcp4Client::SELECTING);
  1265. ASSERT_NO_THROW(clientB.doDORA(boost::shared_ptr<
  1266. IOAddress>(new IOAddress("0.0.0.0"))));
  1267. // Make sure that the server responded.
  1268. ASSERT_TRUE(clientB.getContext().response_);
  1269. resp = clientB.getContext().response_;
  1270. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1271. ASSERT_EQ(in_pool_addr, clientB.config_.lease_.addr_);
  1272. }
  1273. /// This test verifies that after a client completes its DORA exchange,
  1274. /// appropriate statistics are updated.
  1275. TEST_F(DORATest, statisticsDORA) {
  1276. Dhcp4Client client(Dhcp4Client::SELECTING);
  1277. // Configure DHCP server.
  1278. configure(DORA_CONFIGS[0], *client.getServer());
  1279. // Perform 4-way exchange with the server but to not request any
  1280. // specific address in the DHCPDISCOVER message.
  1281. ASSERT_NO_THROW(client.doDORA());
  1282. // Make sure that the server responded.
  1283. ASSERT_TRUE(client.getContext().response_);
  1284. Pkt4Ptr resp = client.getContext().response_;
  1285. // Make sure that the server has responded with DHCPACK.
  1286. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1287. // Ok, let's check the statistics.
  1288. using namespace isc::stats;
  1289. StatsMgr& mgr = StatsMgr::instance();
  1290. ObservationPtr pkt4_received = mgr.getObservation("pkt4-received");
  1291. ObservationPtr pkt4_discover_received = mgr.getObservation("pkt4-discover-received");
  1292. ObservationPtr pkt4_offer_sent = mgr.getObservation("pkt4-offer-sent");
  1293. ObservationPtr pkt4_request_received = mgr.getObservation("pkt4-request-received");
  1294. ObservationPtr pkt4_ack_sent = mgr.getObservation("pkt4-ack-sent");
  1295. ObservationPtr pkt4_sent = mgr.getObservation("pkt4-sent");
  1296. // All expected statistics must be present.
  1297. ASSERT_TRUE(pkt4_received);
  1298. ASSERT_TRUE(pkt4_discover_received);
  1299. ASSERT_TRUE(pkt4_offer_sent);
  1300. ASSERT_TRUE(pkt4_request_received);
  1301. ASSERT_TRUE(pkt4_ack_sent);
  1302. ASSERT_TRUE(pkt4_sent);
  1303. // They also must have expected values.
  1304. EXPECT_EQ(2, pkt4_received->getInteger().first);
  1305. EXPECT_EQ(1, pkt4_discover_received->getInteger().first);
  1306. EXPECT_EQ(1, pkt4_offer_sent->getInteger().first);
  1307. EXPECT_EQ(1, pkt4_request_received->getInteger().first);
  1308. EXPECT_EQ(1, pkt4_ack_sent->getInteger().first);
  1309. EXPECT_EQ(2, pkt4_sent->getInteger().first);
  1310. // Let the client send request 3 times, which should make the server
  1311. // to send 3 acks.
  1312. client.setState(Dhcp4Client::RENEWING);
  1313. ASSERT_NO_THROW(client.doRequest());
  1314. ASSERT_NO_THROW(client.doRequest());
  1315. ASSERT_NO_THROW(client.doRequest());
  1316. // Let's see if the stats are properly updated.
  1317. EXPECT_EQ(5, pkt4_received->getInteger().first);
  1318. EXPECT_EQ(1, pkt4_discover_received->getInteger().first);
  1319. EXPECT_EQ(1, pkt4_offer_sent->getInteger().first);
  1320. EXPECT_EQ(4, pkt4_request_received->getInteger().first);
  1321. EXPECT_EQ(4, pkt4_ack_sent->getInteger().first);
  1322. EXPECT_EQ(5, pkt4_sent->getInteger().first);
  1323. }
  1324. // This test verifies that after a client completes an exchange that result
  1325. // in NAK, appropriate statistics are updated.
  1326. TEST_F(DORATest, statisticsNAK) {
  1327. Dhcp4Client client(Dhcp4Client::SELECTING);
  1328. // Configure DHCP server.
  1329. configure(DORA_CONFIGS[0], *client.getServer());
  1330. // Obtain a lease from the server using the 4-way exchange.
  1331. // Get a lease.
  1332. client.doDORA();
  1333. // Wipe all stats.
  1334. isc::stats::StatsMgr::instance().removeAll();
  1335. client.setState(Dhcp4Client::INIT_REBOOT);
  1336. client.config_.lease_.addr_ = IOAddress("10.0.0.30");
  1337. ASSERT_NO_THROW(client.doRequest());
  1338. // Make sure that the server responded.
  1339. ASSERT_TRUE(client.getContext().response_);
  1340. Pkt4Ptr resp = client.getContext().response_;
  1341. EXPECT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  1342. using namespace isc::stats;
  1343. StatsMgr& mgr = StatsMgr::instance();
  1344. ObservationPtr pkt4_received = mgr.getObservation("pkt4-received");
  1345. ObservationPtr pkt4_request_received = mgr.getObservation("pkt4-request-received");
  1346. ObservationPtr pkt4_ack_sent = mgr.getObservation("pkt4-ack-sent");
  1347. ObservationPtr pkt4_nak_sent = mgr.getObservation("pkt4-nak-sent");
  1348. ObservationPtr pkt4_sent = mgr.getObservation("pkt4-sent");
  1349. // All expected statistics must be present.
  1350. ASSERT_TRUE(pkt4_received);
  1351. ASSERT_TRUE(pkt4_request_received);
  1352. ASSERT_FALSE(pkt4_ack_sent); // No acks were sent, no such statistic expected.
  1353. ASSERT_TRUE(pkt4_nak_sent);
  1354. ASSERT_TRUE(pkt4_sent);
  1355. // They also must have expected values.
  1356. EXPECT_EQ(1, pkt4_received->getInteger().first);
  1357. EXPECT_EQ(1, pkt4_request_received->getInteger().first);
  1358. EXPECT_EQ(1, pkt4_nak_sent->getInteger().first);
  1359. EXPECT_EQ(1, pkt4_sent->getInteger().first);
  1360. }
  1361. void
  1362. DORATest::testMultiStageBoot(const unsigned int config_index) {
  1363. Dhcp4Client client(Dhcp4Client::SELECTING);
  1364. // Configure DHCP server.
  1365. ASSERT_NO_THROW(configure(DORA_CONFIGS[config_index],
  1366. *client.getServer()));
  1367. // Stage 1: get the first lease for our client. In PXE boot, it would be
  1368. // a stage when the BIOS requests a lease.
  1369. // Include client id apart from the MAC address.
  1370. client.includeClientId("10:21:32:AB:CD:EF");
  1371. ASSERT_NO_THROW(client.doDORA());
  1372. // Make sure that the server responded.
  1373. ASSERT_TRUE(client.getContext().response_);
  1374. Pkt4Ptr resp = client.getContext().response_;
  1375. // Make sure that the server has responded with DHCPACK.
  1376. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1377. // Make sure that the client has got the lease which belongs
  1378. // to a pool.
  1379. IOAddress leased_address1 = client.config_.lease_.addr_;
  1380. Subnet4Ptr subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->
  1381. selectSubnet(leased_address1);
  1382. ASSERT_TRUE(subnet);
  1383. // Make sure that the address has been allocated from the dynamic pool.
  1384. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, leased_address1));
  1385. // Stage 2: the client with a given MAC address has a lease in the
  1386. // lease database. The installer comes up and uses the same MAC address
  1387. // but generates a different client id. The server should treat the
  1388. // client with modified client identifier as a different client and
  1389. // create a new lease for it.
  1390. // Modify client identifier.
  1391. client.includeClientId("11:54:45:AB:AA:FE");
  1392. ASSERT_NO_THROW(client.doDORA());
  1393. // Make sure that the server responded.
  1394. ASSERT_TRUE(client.getContext().response_);
  1395. resp = client.getContext().response_;
  1396. // Make sure that the server has responded with DHCPACK.
  1397. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1398. // Make sure that the client has got the lease which belongs
  1399. // to a pool.
  1400. IOAddress leased_address2 = client.config_.lease_.addr_;
  1401. // Make sure that the address has been allocated from the dynamic pool.
  1402. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, leased_address2));
  1403. // The client should have got a new lease.
  1404. ASSERT_NE(leased_address1, leased_address2);
  1405. // Modify client identifier again.
  1406. client.includeClientId("22:34:AC:BE:44:54");
  1407. ASSERT_NO_THROW(client.doDORA());
  1408. // Make sure that the server responded.
  1409. ASSERT_TRUE(client.getContext().response_);
  1410. resp = client.getContext().response_;
  1411. // Make sure that the server has responded with DHCPACK.
  1412. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1413. // Make sure that the client has got the lease which belongs
  1414. // to a pool.
  1415. IOAddress leased_address3 = client.config_.lease_.addr_;
  1416. // Make sure that the address has been allocated from the dynamic pool.
  1417. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, leased_address3));
  1418. // The client should have got a new lease.
  1419. ASSERT_NE(leased_address1, leased_address3);
  1420. ASSERT_NE(leased_address2, leased_address3);
  1421. }
  1422. // Test that the client using the same hardware address but multiple
  1423. // client identifiers will obtain multiple leases.
  1424. TEST_F(DORATest, multiStageBoot) {
  1425. // DORA_CONFIGS[0] to be used for server configuration.
  1426. testMultiStageBoot(0);
  1427. }
  1428. // Starting tests which require MySQL backend availability. Those tests
  1429. // will not be executed if Kea has been compiled without the
  1430. // --with-dhcp-mysql.
  1431. #ifdef HAVE_MYSQL
  1432. /// @brief Test fixture class for the test utilizing MySQL database backend.
  1433. class DORAMySQLTest : public DORATest {
  1434. public:
  1435. /// @brief Constructor.
  1436. ///
  1437. /// Recreates MySQL schema for a test.
  1438. DORAMySQLTest() : DORATest() {
  1439. destroyMySQLSchema();
  1440. createMySQLSchema();
  1441. }
  1442. /// @brief Destructor.
  1443. ///
  1444. /// Destroys MySQL schema.
  1445. virtual ~DORAMySQLTest() {
  1446. destroyMySQLSchema();
  1447. }
  1448. };
  1449. // Test that the client using the same hardware address but multiple
  1450. // client identifiers will obtain multiple leases (MySQL lease database).
  1451. TEST_F(DORAMySQLTest, multiStageBoot) {
  1452. // DORA_CONFIGS[7] to be used for server configuration.
  1453. testMultiStageBoot(7);
  1454. }
  1455. #endif
  1456. // Starting tests which require MySQL backend availability. Those tests
  1457. // will not be executed if Kea has been compiled without the
  1458. // --with-dhcp-pgsql.
  1459. #ifdef HAVE_PGSQL
  1460. /// @brief Test fixture class for the test utilizing PostgreSQL database backend.
  1461. class DORAPgSQLTest : public DORATest {
  1462. public:
  1463. /// @brief Constructor.
  1464. ///
  1465. /// Recreates PgSQL schema for a test.
  1466. DORAPgSQLTest() : DORATest() {
  1467. destroyPgSQLSchema();
  1468. createPgSQLSchema();
  1469. }
  1470. /// @brief Destructor.
  1471. ///
  1472. /// Destroys PgSQL schema.
  1473. virtual ~DORAPgSQLTest() {
  1474. destroyPgSQLSchema();
  1475. }
  1476. };
  1477. // Test that the client using the same hardware address but multiple
  1478. // client identifiers will obtain multiple leases (PostgreSQL lease database).
  1479. TEST_F(DORAPgSQLTest, multiStageBoot) {
  1480. // DORA_CONFIGS[8] to be used for server configuration.
  1481. testMultiStageBoot(8);
  1482. }
  1483. #endif
  1484. #ifdef HAVE_CQL
  1485. /// @brief Test fixture class for the test utilizing Cassandra database backend.
  1486. class DORACQLTest : public DORATest {
  1487. public:
  1488. /// @brief Constructor.
  1489. ///
  1490. /// Recreates CQL schema for a test.
  1491. DORACQLTest() : DORATest() {
  1492. destroyCqlSchema(false, true);
  1493. createCqlSchema(false, true);
  1494. }
  1495. /// @brief Destructor.
  1496. ///
  1497. /// Destroys CQL schema.
  1498. virtual ~DORACQLTest() {
  1499. destroyCqlSchema(false, true);
  1500. }
  1501. };
  1502. // Test that the client using the same hardware address but multiple
  1503. // client identifiers will obtain multiple leases (CQL lease database).
  1504. TEST_F(DORACQLTest, multiStageBoot) {
  1505. // DORA_CONFIGS[9] to be used for server configuration.
  1506. testMultiStageBoot(9);
  1507. }
  1508. #endif
  1509. } // end of anonymous namespace