hooks_unittest.cc 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  1. // Copyright (C) 2013-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 <dhcp/dhcp6.h>
  9. #include <dhcp/duid.h>
  10. #include <dhcp6/json_config_parser.h>
  11. #include <dhcp/dhcp6.h>
  12. #include <dhcpsrv/cfgmgr.h>
  13. #include <dhcpsrv/lease_mgr.h>
  14. #include <dhcpsrv/lease_mgr_factory.h>
  15. #include <dhcpsrv/utils.h>
  16. #include <util/buffer.h>
  17. #include <util/range_utilities.h>
  18. #include <hooks/server_hooks.h>
  19. #include <hooks/callout_manager.h>
  20. #include <dhcp6/tests/dhcp6_test_utils.h>
  21. #include <dhcp6/tests/dhcp6_client.h>
  22. #include <dhcp/tests/iface_mgr_test_config.h>
  23. #include <dhcp/tests/pkt_captures.h>
  24. #include <cc/command_interpreter.h>
  25. #include <dhcp6/tests/marker_file.h>
  26. #include <dhcp6/tests/test_libraries.h>
  27. #include <boost/scoped_ptr.hpp>
  28. #include <gtest/gtest.h>
  29. #include <unistd.h>
  30. #include <fstream>
  31. #include <iostream>
  32. #include <sstream>
  33. using namespace isc;
  34. using namespace isc::data;
  35. using namespace isc::dhcp::test;
  36. using namespace isc::asiolink;
  37. using namespace isc::dhcp;
  38. using namespace isc::util;
  39. using namespace isc::hooks;
  40. using namespace std;
  41. // namespace has to be named, because friends are defined in Dhcpv6Srv class
  42. // Maybe it should be isc::test?
  43. namespace {
  44. // Checks if hooks are implemented properly.
  45. TEST_F(Dhcpv6SrvTest, Hooks) {
  46. NakedDhcpv6Srv srv(0);
  47. // check if appropriate hooks are registered
  48. int hook_index_buffer6_receive = -1;
  49. int hook_index_buffer6_send = -1;
  50. int hook_index_lease6_renew = -1;
  51. int hook_index_lease6_release = -1;
  52. int hook_index_lease6_rebind = -1;
  53. int hook_index_lease6_decline = -1;
  54. int hook_index_pkt6_received = -1;
  55. int hook_index_select_subnet = -1;
  56. int hook_index_pkt6_send = -1;
  57. int hook_index_host6_identifier = -1;
  58. // check if appropriate indexes are set
  59. EXPECT_NO_THROW(hook_index_buffer6_receive = ServerHooks::getServerHooks()
  60. .getIndex("buffer6_receive"));
  61. EXPECT_NO_THROW(hook_index_buffer6_send = ServerHooks::getServerHooks()
  62. .getIndex("buffer6_send"));
  63. EXPECT_NO_THROW(hook_index_lease6_renew = ServerHooks::getServerHooks()
  64. .getIndex("lease6_renew"));
  65. EXPECT_NO_THROW(hook_index_lease6_release = ServerHooks::getServerHooks()
  66. .getIndex("lease6_release"));
  67. EXPECT_NO_THROW(hook_index_lease6_rebind = ServerHooks::getServerHooks()
  68. .getIndex("lease6_rebind"));
  69. EXPECT_NO_THROW(hook_index_lease6_decline = ServerHooks::getServerHooks()
  70. .getIndex("lease6_decline"));
  71. EXPECT_NO_THROW(hook_index_pkt6_received = ServerHooks::getServerHooks()
  72. .getIndex("pkt6_receive"));
  73. EXPECT_NO_THROW(hook_index_select_subnet = ServerHooks::getServerHooks()
  74. .getIndex("subnet6_select"));
  75. EXPECT_NO_THROW(hook_index_pkt6_send = ServerHooks::getServerHooks()
  76. .getIndex("pkt6_send"));
  77. EXPECT_NO_THROW(hook_index_host6_identifier = ServerHooks::getServerHooks()
  78. .getIndex("host6_identifier"));
  79. EXPECT_TRUE(hook_index_pkt6_received > 0);
  80. EXPECT_TRUE(hook_index_select_subnet > 0);
  81. EXPECT_TRUE(hook_index_pkt6_send > 0);
  82. EXPECT_TRUE(hook_index_buffer6_receive > 0);
  83. EXPECT_TRUE(hook_index_buffer6_send > 0);
  84. EXPECT_TRUE(hook_index_lease6_renew > 0);
  85. EXPECT_TRUE(hook_index_lease6_release > 0);
  86. EXPECT_TRUE(hook_index_lease6_rebind > 0);
  87. EXPECT_TRUE(hook_index_lease6_decline > 0);
  88. EXPECT_TRUE(hook_index_host6_identifier > 0);
  89. }
  90. /// @brief a class dedicated to Hooks testing in DHCPv6 server
  91. ///
  92. /// This class has a number of static members, because each non-static
  93. /// method has implicit 'this' parameter, so it does not match callout
  94. /// signature and couldn't be registered. Furthermore, static methods
  95. /// can't modify non-static members (for obvious reasons), so many
  96. /// fields are declared static. It is still better to keep them as
  97. /// one class rather than unrelated collection of global objects.
  98. class HooksDhcpv6SrvTest : public Dhcpv6SrvTest {
  99. public:
  100. /// @brief creates Dhcpv6Srv and prepares buffers for callouts
  101. HooksDhcpv6SrvTest() {
  102. // Allocate new DHCPv6 Server
  103. srv_.reset(new NakedDhcpv6Srv(0));
  104. // Clear static buffers
  105. resetCalloutBuffers();
  106. // Reset the hook system in its original state
  107. HooksManager::unloadLibraries();
  108. }
  109. /// @brief destructor (deletes Dhcpv6Srv)
  110. ~HooksDhcpv6SrvTest() {
  111. // Clear shared manager
  112. HooksManager::getSharedCalloutManager().reset();
  113. }
  114. /// @brief creates an option with specified option code
  115. ///
  116. /// This method is static, because it is used from callouts
  117. /// that do not have a pointer to HooksDhcpv6SSrvTest object
  118. ///
  119. /// @param option_code code of option to be created
  120. ///
  121. /// @return pointer to create option object
  122. static OptionPtr createOption(uint16_t option_code) {
  123. uint8_t payload[] = {
  124. 0xa, 0xb, 0xc, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14
  125. };
  126. OptionBuffer tmp(payload, payload + sizeof(payload));
  127. return OptionPtr(new Option(Option::V6, option_code, tmp));
  128. }
  129. /// test callback that stores received callout name and pkt6 value
  130. /// @param callout_handle handle passed by the hooks framework
  131. /// @return always 0
  132. static int
  133. pkt6_receive_callout(CalloutHandle& callout_handle) {
  134. callback_name_ = string("pkt6_receive");
  135. callout_handle.getArgument("query6", callback_qry_pkt6_);
  136. callback_argument_names_ = callout_handle.getArgumentNames();
  137. if (callback_qry_pkt6_) {
  138. callback_qry_options_copy_ = callback_qry_pkt6_->isCopyRetrievedOptions();
  139. }
  140. return (0);
  141. }
  142. /// test callback that changes client-id value
  143. /// @param callout_handle handle passed by the hooks framework
  144. /// @return always 0
  145. static int
  146. pkt6_receive_change_clientid(CalloutHandle& callout_handle) {
  147. Pkt6Ptr pkt;
  148. callout_handle.getArgument("query6", pkt);
  149. // Get rid of the old client-id
  150. pkt->delOption(D6O_CLIENTID);
  151. // Add a new option
  152. pkt->addOption(createOption(D6O_CLIENTID));
  153. // Carry on as usual
  154. return pkt6_receive_callout(callout_handle);
  155. }
  156. /// Test callback that deletes client-id
  157. /// @param callout_handle handle passed by the hooks framework
  158. /// @return always 0
  159. static int
  160. pkt6_receive_delete_clientid(CalloutHandle& callout_handle) {
  161. Pkt6Ptr pkt;
  162. callout_handle.getArgument("query6", pkt);
  163. // Get rid of the old client-id
  164. pkt->delOption(D6O_CLIENTID);
  165. // Carry on as usual
  166. return pkt6_receive_callout(callout_handle);
  167. }
  168. /// Test callback that sets skip flag
  169. /// @param callout_handle handle passed by the hooks framework
  170. /// @return always 0
  171. static int
  172. pkt6_receive_skip(CalloutHandle& callout_handle) {
  173. Pkt6Ptr pkt;
  174. callout_handle.getArgument("query6", pkt);
  175. callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
  176. // Carry on as usual
  177. return pkt6_receive_callout(callout_handle);
  178. }
  179. /// Test callback that stores received callout name and pkt6 value
  180. /// @param callout_handle handle passed by the hooks framework
  181. /// @return always 0
  182. static int
  183. buffer6_receive_callout(CalloutHandle& callout_handle) {
  184. callback_name_ = string("buffer6_receive");
  185. callout_handle.getArgument("query6", callback_qry_pkt6_);
  186. callback_argument_names_ = callout_handle.getArgumentNames();
  187. if (callback_qry_pkt6_) {
  188. callback_qry_options_copy_ = callback_qry_pkt6_->isCopyRetrievedOptions();
  189. }
  190. return (0);
  191. }
  192. /// Test callback that changes first byte of client-id value
  193. /// @param callout_handle handle passed by the hooks framework
  194. /// @return always 0
  195. static int
  196. buffer6_receive_change_clientid(CalloutHandle& callout_handle) {
  197. Pkt6Ptr pkt;
  198. callout_handle.getArgument("query6", pkt);
  199. // If there is at least one option with data
  200. if (pkt->data_.size() > Pkt6::DHCPV6_PKT_HDR_LEN + Option::OPTION6_HDR_LEN) {
  201. // Offset of the first byte of the first option. Let's set this byte
  202. // to some new value that we could later check
  203. pkt->data_[Pkt6::DHCPV6_PKT_HDR_LEN + Option::OPTION6_HDR_LEN] = 0xff;
  204. }
  205. // Carry on as usual
  206. return buffer6_receive_callout(callout_handle);
  207. }
  208. /// Test callback that deletes client-id
  209. /// @param callout_handle handle passed by the hooks framework
  210. /// @return always 0
  211. static int
  212. buffer6_receive_delete_clientid(CalloutHandle& callout_handle) {
  213. Pkt6Ptr pkt;
  214. callout_handle.getArgument("query6", pkt);
  215. // this is modified SOLICIT (with missing mandatory client-id)
  216. uint8_t data[] = {
  217. 1, // type 1 = SOLICIT
  218. 0xca, 0xfe, 0x01, // trans-id = 0xcafe01
  219. 0, 3, // option type 3 (IA_NA)
  220. 0, 12, // option length 12
  221. 0, 0, 0, 1, // iaid = 1
  222. 0, 0, 0, 0, // T1 = 0
  223. 0, 0, 0, 0 // T2 = 0
  224. };
  225. OptionBuffer modifiedMsg(data, data + sizeof(data));
  226. pkt->data_ = modifiedMsg;
  227. // carry on as usual
  228. return buffer6_receive_callout(callout_handle);
  229. }
  230. /// Test callback that sets skip flag
  231. /// @param callout_handle handle passed by the hooks framework
  232. /// @return always 0
  233. static int
  234. buffer6_receive_skip(CalloutHandle& callout_handle) {
  235. callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
  236. // Carry on as usual
  237. return buffer6_receive_callout(callout_handle);
  238. }
  239. /// Test callback that stores received callout name and pkt6 value
  240. /// @param callout_handle handle passed by the hooks framework
  241. /// @return always 0
  242. static int
  243. pkt6_send_callout(CalloutHandle& callout_handle) {
  244. callback_name_ = string("pkt6_send");
  245. callout_handle.getArgument("response6", callback_resp_pkt6_);
  246. callout_handle.getArgument("query6", callback_qry_pkt6_);
  247. callback_argument_names_ = callout_handle.getArgumentNames();
  248. if (callback_qry_pkt6_) {
  249. callback_qry_options_copy_ = callback_qry_pkt6_->isCopyRetrievedOptions();
  250. }
  251. if (callback_resp_pkt6_) {
  252. callback_resp_options_copy_ = callback_resp_pkt6_->isCopyRetrievedOptions();
  253. }
  254. return (0);
  255. }
  256. // Test callback that changes server-id
  257. /// @param callout_handle handle passed by the hooks framework
  258. /// @return always 0
  259. static int
  260. pkt6_send_change_serverid(CalloutHandle& callout_handle) {
  261. Pkt6Ptr pkt;
  262. callout_handle.getArgument("response6", pkt);
  263. // Get rid of the old server-id
  264. pkt->delOption(D6O_SERVERID);
  265. // Add a new option
  266. pkt->addOption(createOption(D6O_SERVERID));
  267. // Carry on as usual
  268. return pkt6_send_callout(callout_handle);
  269. }
  270. /// Test callback that deletes server-id
  271. /// @param callout_handle handle passed by the hooks framework
  272. /// @return always 0
  273. static int
  274. pkt6_send_delete_serverid(CalloutHandle& callout_handle) {
  275. Pkt6Ptr pkt;
  276. callout_handle.getArgument("response6", pkt);
  277. // Get rid of the old client-id
  278. pkt->delOption(D6O_SERVERID);
  279. // Carry on as usual
  280. return pkt6_send_callout(callout_handle);
  281. }
  282. /// Test callback that sets skip flag
  283. /// @param callout_handle handle passed by the hooks framework
  284. /// @return always 0
  285. static int
  286. pkt6_send_skip(CalloutHandle& callout_handle) {
  287. Pkt6Ptr pkt;
  288. callout_handle.getArgument("response6", pkt);
  289. callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
  290. // carry on as usual
  291. return pkt6_send_callout(callout_handle);
  292. }
  293. /// @brief Test callback that stores response packet.
  294. /// @param callout_handle handle passed by the hooks framework.
  295. /// @return always 0
  296. static int
  297. buffer6_send_callout(CalloutHandle& callout_handle) {
  298. callback_name_ = string("buffer6_send");
  299. callback_argument_names_ = callout_handle.getArgumentNames();
  300. callout_handle.getArgument("response6", callback_resp_pkt6_);
  301. if (callback_resp_pkt6_) {
  302. callback_resp_options_copy_ = callback_resp_pkt6_->isCopyRetrievedOptions();
  303. }
  304. return (0);
  305. }
  306. /// Test callback that stores received callout name and subnet6 values
  307. /// @param callout_handle handle passed by the hooks framework
  308. /// @return always 0
  309. static int
  310. subnet6_select_callout(CalloutHandle& callout_handle) {
  311. callback_name_ = string("subnet6_select");
  312. callout_handle.getArgument("query6", callback_qry_pkt6_);
  313. callout_handle.getArgument("subnet6", callback_subnet6_);
  314. callout_handle.getArgument("subnet6collection", callback_subnet6collection_);
  315. callback_argument_names_ = callout_handle.getArgumentNames();
  316. if (callback_qry_pkt6_) {
  317. callback_qry_options_copy_ = callback_qry_pkt6_->isCopyRetrievedOptions();
  318. }
  319. return (0);
  320. }
  321. /// Test callback that picks the other subnet if possible.
  322. /// @param callout_handle handle passed by the hooks framework
  323. /// @return always 0
  324. static int
  325. subnet6_select_different_subnet_callout(CalloutHandle& callout_handle) {
  326. // Call the basic callout to record all passed values
  327. subnet6_select_callout(callout_handle);
  328. const Subnet6Collection* subnets;
  329. Subnet6Ptr subnet;
  330. callout_handle.getArgument("subnet6", subnet);
  331. callout_handle.getArgument("subnet6collection", subnets);
  332. // Let's change to a different subnet
  333. if (subnets->size() > 1) {
  334. subnet = (*subnets)[1]; // Let's pick the other subnet
  335. callout_handle.setArgument("subnet6", subnet);
  336. }
  337. return (0);
  338. }
  339. /// Test callback that stores received callout name and pkt6 value
  340. /// @param callout_handle handle passed by the hooks framework
  341. /// @return always 0
  342. static int
  343. lease6_renew_callout(CalloutHandle& callout_handle) {
  344. callback_name_ = string("lease6_renew");
  345. callout_handle.getArgument("query6", callback_qry_pkt6_);
  346. callout_handle.getArgument("lease6", callback_lease6_);
  347. callout_handle.getArgument("ia_na", callback_ia_na_);
  348. callback_argument_names_ = callout_handle.getArgumentNames();
  349. if (callback_qry_pkt6_) {
  350. callback_qry_options_copy_ = callback_qry_pkt6_->isCopyRetrievedOptions();
  351. }
  352. return (0);
  353. }
  354. /// The following values are used by the callout to override
  355. /// renewed lease parameters
  356. static const uint32_t override_iaid_;
  357. static const uint32_t override_t1_;
  358. static const uint32_t override_t2_;
  359. static const uint32_t override_preferred_;
  360. static const uint32_t override_valid_;
  361. /// Test callback that overrides received lease. It updates
  362. /// T1, T2, preferred and valid lifetimes
  363. /// @param callout_handle handle passed by the hooks framework
  364. /// @return always 0
  365. static int
  366. lease6_renew_update_callout(CalloutHandle& callout_handle) {
  367. callback_name_ = string("lease6_renew");
  368. callout_handle.getArgument("query6", callback_qry_pkt6_);
  369. callout_handle.getArgument("lease6", callback_lease6_);
  370. callout_handle.getArgument("ia_na", callback_ia_na_);
  371. // Should be an ASSERT but it is not allowed here
  372. EXPECT_TRUE(callback_lease6_);
  373. // Let's override some values in the lease
  374. callback_lease6_->iaid_ = override_iaid_;
  375. callback_lease6_->t1_ = override_t1_;
  376. callback_lease6_->t2_ = override_t2_;
  377. callback_lease6_->preferred_lft_ = override_preferred_;
  378. callback_lease6_->valid_lft_ = override_valid_;
  379. // Should be an ASSERT but it is not allowed here
  380. EXPECT_TRUE(callback_ia_na_);
  381. // Override the values to be sent to the client as well
  382. callback_ia_na_->setIAID(override_iaid_);
  383. callback_ia_na_->setT1(override_t1_);
  384. callback_ia_na_->setT2(override_t2_);
  385. callback_argument_names_ = callout_handle.getArgumentNames();
  386. return (0);
  387. }
  388. /// Test callback that sets the skip flag
  389. /// @param callout_handle handle passed by the hooks framework
  390. /// @return always 0
  391. static int
  392. lease6_renew_skip_callout(CalloutHandle& callout_handle) {
  393. callback_name_ = string("lease6_renew");
  394. callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
  395. return (0);
  396. }
  397. /// Test callback that stores received callout name and pkt6 value
  398. /// @param callout_handle handle passed by the hooks framework
  399. /// @return always 0
  400. static int
  401. lease6_rebind_callout(CalloutHandle& callout_handle) {
  402. callback_name_ = string("lease6_rebind");
  403. callout_handle.getArgument("query6", callback_qry_pkt6_);
  404. callout_handle.getArgument("lease6", callback_lease6_);
  405. callout_handle.getArgument("ia_na", callback_ia_na_);
  406. callback_argument_names_ = callout_handle.getArgumentNames();
  407. if (callback_qry_pkt6_) {
  408. callback_qry_options_copy_ = callback_qry_pkt6_->isCopyRetrievedOptions();
  409. }
  410. return (0);
  411. }
  412. /// Test callback that overrides received lease. It updates
  413. /// T1, T2, preferred and valid lifetimes
  414. /// @param callout_handle handle passed by the hooks framework
  415. /// @return always 0
  416. static int
  417. lease6_rebind_update_callout(CalloutHandle& callout_handle) {
  418. callback_name_ = string("lease6_rebind");
  419. callout_handle.getArgument("query6", callback_qry_pkt6_);
  420. callout_handle.getArgument("lease6", callback_lease6_);
  421. callout_handle.getArgument("ia_na", callback_ia_na_);
  422. // Should be an ASSERT but it is not allowed here
  423. EXPECT_TRUE(callback_lease6_);
  424. // Let's override some values in the lease
  425. callback_lease6_->iaid_ = override_iaid_;
  426. callback_lease6_->t1_ = override_t1_;
  427. callback_lease6_->t2_ = override_t2_;
  428. callback_lease6_->preferred_lft_ = override_preferred_;
  429. callback_lease6_->valid_lft_ = override_valid_;
  430. // Should be an ASSERT but it is not allowed here
  431. EXPECT_TRUE(callback_ia_na_);
  432. // Override the values to be sent to the client as well
  433. callback_ia_na_->setIAID(override_iaid_);
  434. callback_ia_na_->setT1(override_t1_);
  435. callback_ia_na_->setT2(override_t2_);
  436. callback_argument_names_ = callout_handle.getArgumentNames();
  437. return (0);
  438. }
  439. /// Lease6_rebind callout that sets status to SKIP
  440. ///
  441. /// @param callout_handle handle passed by the hooks framework
  442. /// @return always 0
  443. static int
  444. lease6_rebind_skip_callout(CalloutHandle& callout_handle) {
  445. callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
  446. return (lease6_rebind_callout(callout_handle));
  447. }
  448. /// Lease6_rebind callout that sets status to DROP
  449. ///
  450. /// @param callout_handle handle passed by the hooks framework
  451. /// @return always 0
  452. static int
  453. lease6_rebind_drop_callout(CalloutHandle& callout_handle) {
  454. callout_handle.setStatus(CalloutHandle::NEXT_STEP_DROP);
  455. return (lease6_rebind_callout(callout_handle));
  456. }
  457. /// Test callback that stores received callout name passed parameters
  458. /// @param callout_handle handle passed by the hooks framework
  459. /// @return always 0
  460. static int
  461. lease6_release_callout(CalloutHandle& callout_handle) {
  462. callback_name_ = string("lease6_release");
  463. callout_handle.getArgument("query6", callback_qry_pkt6_);
  464. callout_handle.getArgument("lease6", callback_lease6_);
  465. callback_argument_names_ = callout_handle.getArgumentNames();
  466. if (callback_qry_pkt6_) {
  467. callback_qry_options_copy_ = callback_qry_pkt6_->isCopyRetrievedOptions();
  468. }
  469. return (0);
  470. }
  471. /// Test callback that sets the skip flag
  472. /// @param callout_handle handle passed by the hooks framework
  473. /// @return always 0
  474. static int
  475. lease6_release_skip_callout(CalloutHandle& callout_handle) {
  476. callback_name_ = string("lease6_release");
  477. callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
  478. return (0);
  479. }
  480. /// Lease6_decline test callback
  481. ///
  482. /// Stores all parameters in callback_* fields.
  483. ///
  484. /// @param callout_handle handle passed by the hooks framework
  485. /// @return always 0
  486. static int
  487. lease6_decline_callout(CalloutHandle& callout_handle) {
  488. callback_name_ = string("lease6_decline");
  489. callout_handle.getArgument("query6", callback_qry_pkt6_);
  490. callout_handle.getArgument("lease6", callback_lease6_);
  491. if (callback_qry_pkt6_) {
  492. callback_qry_options_copy_ = callback_qry_pkt6_->isCopyRetrievedOptions();
  493. }
  494. return (0);
  495. }
  496. /// Lease6_decline callout that sets status to SKIP
  497. ///
  498. /// @param callout_handle handle passed by the hooks framework
  499. /// @return always 0
  500. static int
  501. lease6_decline_skip_callout(CalloutHandle& callout_handle) {
  502. callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
  503. return (lease6_decline_callout(callout_handle));
  504. }
  505. /// Lease6_decline callout that sets status to DROP
  506. ///
  507. /// @param callout_handle handle passed by the hooks framework
  508. /// @return always 0
  509. static int
  510. lease6_decline_drop_callout(CalloutHandle& callout_handle) {
  511. callout_handle.setStatus(CalloutHandle::NEXT_STEP_DROP);
  512. return (lease6_decline_callout(callout_handle));
  513. }
  514. /// @brief Test host6_identifier by setting identifier to "foo"
  515. ///
  516. /// @param callout_handle handle passed by the hooks framework
  517. /// @return always 0
  518. static int
  519. host6_identifier_foo_callout(CalloutHandle& handle) {
  520. callback_name_ = string("host6_identifier");
  521. // Make sure the query6 parameter is passed.
  522. handle.getArgument("query6", callback_qry_pkt6_);
  523. // Make sure id_type parameter is passed.
  524. Host::IdentifierType type = Host::IDENT_FLEX;
  525. handle.getArgument("id_type", type);
  526. // Make sure id_value parameter is passed.
  527. std::vector<uint8_t> id_test;
  528. handle.getArgument("id_value", id_test);
  529. // Ok, now set the identifier.
  530. std::vector<uint8_t> id = { 0x66, 0x6f, 0x6f }; // foo
  531. handle.setArgument("id_value", id);
  532. handle.setArgument("id_type", Host::IDENT_FLEX);
  533. return (0);
  534. }
  535. /// @brief Test host4_identifier callout by setting identifier to hwaddr
  536. ///
  537. /// This callout always returns fixed HWADDR: 00:01:02:03:04:05
  538. ///
  539. /// @param callout_handle handle passed by the hooks framework
  540. /// @return always 0
  541. static int
  542. host6_identifier_hwaddr_callout(CalloutHandle& handle) {
  543. callback_name_ = string("host6_identifier");
  544. // Make sure the query6 parameter is passed.
  545. handle.getArgument("query6", callback_qry_pkt6_);
  546. // Make sure id_type parameter is passed.
  547. Host::IdentifierType type = Host::IDENT_FLEX;
  548. handle.getArgument("id_type", type);
  549. // Make sure id_value parameter is passed.
  550. std::vector<uint8_t> id_test;
  551. handle.getArgument("id_value", id_test);
  552. // Ok, now set the identifier to 00:01:02:03:04:05
  553. std::vector<uint8_t> id = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
  554. handle.setArgument("id_value", id);
  555. handle.setArgument("id_type", Host::IDENT_HWADDR);
  556. return (0);
  557. }
  558. /// Resets buffers used to store data received by callouts
  559. void resetCalloutBuffers() {
  560. callback_name_ = string("");
  561. callback_qry_pkt6_.reset();
  562. callback_resp_pkt6_.reset();
  563. callback_subnet6_.reset();
  564. callback_lease6_.reset();
  565. callback_ia_na_.reset();
  566. callback_subnet6collection_ = NULL;
  567. callback_argument_names_.clear();
  568. callback_qry_options_copy_ = false;
  569. callback_resp_options_copy_ = false;
  570. }
  571. /// Pointer to Dhcpv6Srv that is used in tests
  572. boost::scoped_ptr<NakedDhcpv6Srv> srv_;
  573. // The following fields are used in testing pkt6_receive_callout
  574. /// String name of the received callout
  575. static string callback_name_;
  576. /// Client's query Pkt6 structure returned in the callout
  577. static Pkt6Ptr callback_qry_pkt6_;
  578. /// Server's response Pkt6 structure returned in the callout
  579. static Pkt6Ptr callback_resp_pkt6_;
  580. /// Pointer to lease6
  581. static Lease6Ptr callback_lease6_;
  582. /// Pointer to IA_NA option being renewed or rebound
  583. static boost::shared_ptr<Option6IA> callback_ia_na_;
  584. /// Pointer to a subnet received by callout
  585. static Subnet6Ptr callback_subnet6_;
  586. /// A list of all available subnets (received by callout)
  587. static const Subnet6Collection* callback_subnet6collection_;
  588. /// A list of all received arguments
  589. static vector<string> callback_argument_names_;
  590. /// Flag indicating if copying retrieved options was enabled for
  591. /// a query during callout execution.
  592. static bool callback_qry_options_copy_;
  593. /// Flag indicating if copying retrieved options was enabled for
  594. /// a response during callout execution.
  595. static bool callback_resp_options_copy_;
  596. };
  597. // The following parameters are used by callouts to override
  598. // renewed lease parameters
  599. const uint32_t HooksDhcpv6SrvTest::override_iaid_ = 1000;
  600. const uint32_t HooksDhcpv6SrvTest::override_t1_ = 1001;
  601. const uint32_t HooksDhcpv6SrvTest::override_t2_ = 1002;
  602. const uint32_t HooksDhcpv6SrvTest::override_preferred_ = 1003;
  603. const uint32_t HooksDhcpv6SrvTest::override_valid_ = 1004;
  604. // The following fields are used in testing pkt6_receive_callout.
  605. // See fields description in the class for details
  606. string HooksDhcpv6SrvTest::callback_name_;
  607. Pkt6Ptr HooksDhcpv6SrvTest::callback_qry_pkt6_;
  608. Pkt6Ptr HooksDhcpv6SrvTest::callback_resp_pkt6_;
  609. Subnet6Ptr HooksDhcpv6SrvTest::callback_subnet6_;
  610. const Subnet6Collection* HooksDhcpv6SrvTest::callback_subnet6collection_;
  611. vector<string> HooksDhcpv6SrvTest::callback_argument_names_;
  612. Lease6Ptr HooksDhcpv6SrvTest::callback_lease6_;
  613. boost::shared_ptr<Option6IA> HooksDhcpv6SrvTest::callback_ia_na_;
  614. bool HooksDhcpv6SrvTest::callback_qry_options_copy_;
  615. bool HooksDhcpv6SrvTest::callback_resp_options_copy_;
  616. /// @brief Fixture class used to do basic library load/unload tests
  617. class LoadUnloadDhcpv6SrvTest : public ::testing::Test {
  618. public:
  619. /// @brief Pointer to the tested server object
  620. boost::shared_ptr<NakedDhcpv6Srv> server_;
  621. LoadUnloadDhcpv6SrvTest() {
  622. reset();
  623. }
  624. /// @brief Destructor
  625. ~LoadUnloadDhcpv6SrvTest() {
  626. server_.reset();
  627. reset();
  628. };
  629. /// @brief Reset hooks data
  630. ///
  631. /// Resets the data for the hooks-related portion of the test by ensuring
  632. /// that no libraries are loaded and that any marker files are deleted.
  633. void reset() {
  634. // Unload any previously-loaded libraries.
  635. HooksManager::unloadLibraries();
  636. // Get rid of any marker files.
  637. static_cast<void>(remove(LOAD_MARKER_FILE));
  638. static_cast<void>(remove(UNLOAD_MARKER_FILE));
  639. CfgMgr::instance().clear();
  640. }
  641. };
  642. // Checks if callouts installed on pkt6_receive are indeed called and the
  643. // all necessary parameters are passed.
  644. //
  645. // Note that the test name does not follow test naming convention,
  646. // but the proper hook name is "buffer6_receive".
  647. TEST_F(HooksDhcpv6SrvTest, simpleBuffer6Receive) {
  648. // Install pkt6_receive_callout
  649. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  650. "buffer6_receive", buffer6_receive_callout));
  651. // Let's create a simple SOLICIT
  652. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  653. // Simulate that we have received that traffic
  654. srv_->fakeReceive(sol);
  655. // Server will now process to run its normal loop, but instead of calling
  656. // IfaceMgr::receive6(), it will read all packets from the list set by
  657. // fakeReceive()
  658. // In particular, it should call registered buffer6_receive callback.
  659. srv_->run();
  660. // Check that the callback called is indeed the one we installed
  661. EXPECT_EQ("buffer6_receive", callback_name_);
  662. // Check that pkt6 argument passing was successful and returned proper value
  663. EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
  664. // Check that all expected parameters are there
  665. vector<string> expected_argument_names;
  666. expected_argument_names.push_back(string("query6"));
  667. EXPECT_TRUE(expected_argument_names == callback_argument_names_);
  668. // Pkt passed to a callout must be configured to copy retrieved options.
  669. EXPECT_TRUE(callback_qry_options_copy_);
  670. }
  671. // Checks if callouts installed on buffer6_receive is able to change
  672. // the values and the parameters are indeed used by the server.
  673. TEST_F(HooksDhcpv6SrvTest, valueChangeBuffer6Receive) {
  674. // Install pkt6_receive_callout
  675. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  676. "buffer6_receive", buffer6_receive_change_clientid));
  677. // Let's create a simple SOLICIT
  678. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  679. // Simulate that we have received that traffic
  680. srv_->fakeReceive(sol);
  681. // Server will now process to run its normal loop, but instead of calling
  682. // IfaceMgr::receive6(), it will read all packets from the list set by
  683. // fakeReceive()
  684. // In particular, it should call registered pkt6_receive callback.
  685. srv_->run();
  686. // Check that the server did send a response
  687. ASSERT_EQ(1, srv_->fake_sent_.size());
  688. // Make sure that we received a response
  689. Pkt6Ptr adv = srv_->fake_sent_.front();
  690. ASSERT_TRUE(adv);
  691. // Get client-id...
  692. OptionPtr clientid = adv->getOption(D6O_CLIENTID);
  693. ASSERT_TRUE(clientid);
  694. // ... and check if it is the modified value
  695. EXPECT_EQ(0xff, clientid->getData()[0]);
  696. }
  697. // Checks if callouts installed on buffer6_receive is able to delete
  698. // existing options and that change impacts server processing (mandatory
  699. // client-id option is deleted, so the packet is expected to be dropped)
  700. TEST_F(HooksDhcpv6SrvTest, deleteClientIdBuffer6Receive) {
  701. // Install pkt6_receive_callout
  702. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  703. "buffer6_receive", buffer6_receive_delete_clientid));
  704. // Let's create a simple SOLICIT
  705. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  706. // Simulate that we have received that traffic
  707. srv_->fakeReceive(sol);
  708. // Server will now process to run its normal loop, but instead of calling
  709. // IfaceMgr::receive6(), it will read all packets from the list set by
  710. // fakeReceive()
  711. // In particular, it should call registered pkt6_receive callback.
  712. srv_->run();
  713. // Check that the server dropped the packet and did not send a response
  714. ASSERT_EQ(0, srv_->fake_sent_.size());
  715. }
  716. // Checks if callouts installed on buffer6_received is able to set skip flag that
  717. // will cause the server to not process the packet (drop), even though it is valid.
  718. TEST_F(HooksDhcpv6SrvTest, skipBuffer6Receive) {
  719. // Install pkt6_receive_callout
  720. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  721. "buffer6_receive", buffer6_receive_skip));
  722. // Let's create a simple SOLICIT
  723. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  724. // Simulate that we have received that traffic
  725. srv_->fakeReceive(sol);
  726. // Server will now process to run its normal loop, but instead of calling
  727. // IfaceMgr::receive6(), it will read all packets from the list set by
  728. // fakeReceive()
  729. // In particular, it should call registered pkt6_receive callback.
  730. srv_->run();
  731. // Check that the server dropped the packet and did not produce any response
  732. ASSERT_EQ(0, srv_->fake_sent_.size());
  733. }
  734. // Checks if callouts installed on pkt6_receive are indeed called and the
  735. // all necessary parameters are passed.
  736. //
  737. // Note that the test name does not follow test naming convention,
  738. // but the proper hook name is "pkt6_receive".
  739. TEST_F(HooksDhcpv6SrvTest, simplePkt6Receive) {
  740. // Install pkt6_receive_callout
  741. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  742. "pkt6_receive", pkt6_receive_callout));
  743. // Let's create a simple SOLICIT
  744. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  745. // Simulate that we have received that traffic
  746. srv_->fakeReceive(sol);
  747. // Server will now process to run its normal loop, but instead of calling
  748. // IfaceMgr::receive6(), it will read all packets from the list set by
  749. // fakeReceive()
  750. // In particular, it should call registered pkt6_receive callback.
  751. srv_->run();
  752. // Check that the callback called is indeed the one we installed
  753. EXPECT_EQ("pkt6_receive", callback_name_);
  754. // Check that pkt6 argument passing was successful and returned proper value
  755. EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
  756. // Check that all expected parameters are there
  757. vector<string> expected_argument_names;
  758. expected_argument_names.push_back(string("query6"));
  759. EXPECT_TRUE(expected_argument_names == callback_argument_names_);
  760. // Pkt passed to a callout must be configured to copy retrieved options.
  761. EXPECT_TRUE(callback_qry_options_copy_);
  762. }
  763. // Checks if callouts installed on pkt6_received is able to change
  764. // the values and the parameters are indeed used by the server.
  765. TEST_F(HooksDhcpv6SrvTest, valueChangePkt6Receive) {
  766. // Install pkt6_receive_callout
  767. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  768. "pkt6_receive", pkt6_receive_change_clientid));
  769. // Let's create a simple SOLICIT
  770. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  771. // Simulate that we have received that traffic
  772. srv_->fakeReceive(sol);
  773. // Server will now process to run its normal loop, but instead of calling
  774. // IfaceMgr::receive6(), it will read all packets from the list set by
  775. // fakeReceive()
  776. // In particular, it should call registered pkt6_receive callback.
  777. srv_->run();
  778. // Check that the server did send a response
  779. ASSERT_EQ(1, srv_->fake_sent_.size());
  780. // Make sure that we received a response
  781. Pkt6Ptr adv = srv_->fake_sent_.front();
  782. ASSERT_TRUE(adv);
  783. // Get client-id...
  784. OptionPtr clientid = adv->getOption(D6O_CLIENTID);
  785. // ... and check if it is the modified value
  786. OptionPtr expected = createOption(D6O_CLIENTID);
  787. EXPECT_TRUE(clientid->equals(expected));
  788. }
  789. // Checks if callouts installed on pkt6_received is able to delete
  790. // existing options and that change impacts server processing (mandatory
  791. // client-id option is deleted, so the packet is expected to be dropped)
  792. TEST_F(HooksDhcpv6SrvTest, deleteClientIdPkt6Receive) {
  793. // Install pkt6_receive_callout
  794. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  795. "pkt6_receive", pkt6_receive_delete_clientid));
  796. // Let's create a simple SOLICIT
  797. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  798. // Simulate that we have received that traffic
  799. srv_->fakeReceive(sol);
  800. // Server will now process to run its normal loop, but instead of calling
  801. // IfaceMgr::receive6(), it will read all packets from the list set by
  802. // fakeReceive()
  803. // In particular, it should call registered pkt6_receive callback.
  804. srv_->run();
  805. // Check that the server dropped the packet and did not send a response
  806. ASSERT_EQ(0, srv_->fake_sent_.size());
  807. }
  808. // Checks if callouts installed on pkt6_received is able to set skip flag that
  809. // will cause the server to not process the packet (drop), even though it is valid.
  810. TEST_F(HooksDhcpv6SrvTest, skipPkt6Receive) {
  811. // Install pkt6_receive_callout
  812. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  813. "pkt6_receive", pkt6_receive_skip));
  814. // Let's create a simple SOLICIT
  815. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  816. // Simulate that we have received that traffic
  817. srv_->fakeReceive(sol);
  818. // Server will now process to run its normal loop, but instead of calling
  819. // IfaceMgr::receive6(), it will read all packets from the list set by
  820. // fakeReceive()
  821. // In particular, it should call registered pkt6_receive callback.
  822. srv_->run();
  823. // Check that the server dropped the packet and did not produce any response
  824. ASSERT_EQ(0, srv_->fake_sent_.size());
  825. }
  826. // Checks if callouts installed on pkt6_send are indeed called and the
  827. // all necessary parameters are passed.
  828. TEST_F(HooksDhcpv6SrvTest, simplePkt6Send) {
  829. // Install pkt6_receive_callout
  830. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  831. "pkt6_send", pkt6_send_callout));
  832. // Let's create a simple SOLICIT
  833. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  834. // Simulate that we have received that traffic
  835. srv_->fakeReceive(sol);
  836. // Server will now process to run its normal loop, but instead of calling
  837. // IfaceMgr::receive6(), it will read all packets from the list set by
  838. // fakeReceive()
  839. // In particular, it should call registered pkt6_receive callback.
  840. srv_->run();
  841. // Check that the callback called is indeed the one we installed
  842. EXPECT_EQ("pkt6_send", callback_name_);
  843. // Check that there is one packet sent
  844. ASSERT_EQ(1, srv_->fake_sent_.size());
  845. Pkt6Ptr adv = srv_->fake_sent_.front();
  846. // Check that pkt6 argument passing was successful and returned proper
  847. // values
  848. EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
  849. EXPECT_TRUE(callback_resp_pkt6_.get() == adv.get());
  850. // Check that all expected parameters are there
  851. vector<string> expected_argument_names;
  852. expected_argument_names.push_back(string("query6"));
  853. expected_argument_names.push_back(string("response6"));
  854. EXPECT_TRUE(expected_argument_names == callback_argument_names_);
  855. // Pkt passed to a callout must be configured to copy retrieved options.
  856. EXPECT_TRUE(callback_qry_options_copy_);
  857. EXPECT_TRUE(callback_resp_options_copy_);
  858. }
  859. // Checks if callouts installed on pkt6_send is able to change
  860. // the values and the packet sent contains those changes
  861. TEST_F(HooksDhcpv6SrvTest, valueChangePkt6Send) {
  862. // Install pkt6_receive_callout
  863. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  864. "pkt6_send", pkt6_send_change_serverid));
  865. // Let's create a simple SOLICIT
  866. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  867. // Simulate that we have received that traffic
  868. srv_->fakeReceive(sol);
  869. // Server will now process to run its normal loop, but instead of calling
  870. // IfaceMgr::receive6(), it will read all packets from the list set by
  871. // fakeReceive()
  872. // In particular, it should call registered pkt6_receive callback.
  873. srv_->run();
  874. // Check that the server did send a response
  875. ASSERT_EQ(1, srv_->fake_sent_.size());
  876. // Make sure that we received a response
  877. Pkt6Ptr adv = srv_->fake_sent_.front();
  878. ASSERT_TRUE(adv);
  879. // Get client-id...
  880. OptionPtr clientid = adv->getOption(D6O_SERVERID);
  881. // ... and check if it is the modified value
  882. OptionPtr expected = createOption(D6O_SERVERID);
  883. EXPECT_TRUE(clientid->equals(expected));
  884. }
  885. // Checks if callouts installed on pkt6_send is able to delete
  886. // existing options and that server applies those changes. In particular,
  887. // we are trying to send a packet without server-id. The packet should
  888. // be sent
  889. TEST_F(HooksDhcpv6SrvTest, deleteServerIdPkt6Send) {
  890. // Install pkt6_receive_callout
  891. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  892. "pkt6_send", pkt6_send_delete_serverid));
  893. // Let's create a simple SOLICIT
  894. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  895. // Simulate that we have received that traffic
  896. srv_->fakeReceive(sol);
  897. // Server will now process to run its normal loop, but instead of calling
  898. // IfaceMgr::receive6(), it will read all packets from the list set by
  899. // fakeReceive()
  900. // In particular, it should call registered pkt6_receive callback.
  901. srv_->run();
  902. // Check that the server indeed sent a malformed ADVERTISE
  903. ASSERT_EQ(1, srv_->fake_sent_.size());
  904. // Get that ADVERTISE
  905. Pkt6Ptr adv = srv_->fake_sent_.front();
  906. ASSERT_TRUE(adv);
  907. // Make sure that it does not have server-id
  908. EXPECT_FALSE(adv->getOption(D6O_SERVERID));
  909. }
  910. // Checks if callouts installed on pkt6_skip is able to set skip flag that
  911. // will cause the server to not process the packet (drop), even though it is valid.
  912. TEST_F(HooksDhcpv6SrvTest, skipPkt6Send) {
  913. // Install pkt6_receive_callout
  914. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  915. "pkt6_send", pkt6_send_skip));
  916. // Let's create a simple REQUEST
  917. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  918. // Simulate that we have received that traffic
  919. srv_->fakeReceive(sol);
  920. // Server will now process to run its normal loop, but instead of calling
  921. // IfaceMgr::receive6(), it will read all packets from the list set by
  922. // fakeReceive()
  923. // In particular, it should call registered pkt6_receive callback.
  924. srv_->run();
  925. // Check that the server send the packet
  926. ASSERT_EQ(1, srv_->fake_sent_.size());
  927. // But the sent packet should have 0 length (we told the server to
  928. // skip pack(), but did not do packing outselves)
  929. Pkt6Ptr sent = srv_->fake_sent_.front();
  930. // The actual size of sent packet should be 0
  931. EXPECT_EQ(0, sent->getBuffer().getLength());
  932. }
  933. // Checks if callouts installed on buffer6_send are indeed called and the
  934. // all necessary parameters are passed.
  935. TEST_F(HooksDhcpv6SrvTest, simpleBuffer6Send) {
  936. // Install pkt6_receive_callout
  937. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  938. "buffer6_send", buffer6_send_callout));
  939. // Let's create a simple SOLICIT
  940. Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
  941. // Simulate that we have received that traffic
  942. srv_->fakeReceive(sol);
  943. // Server will now process to run its normal loop, but instead of calling
  944. // IfaceMgr::receive6(), it will read all packets from the list set by
  945. // fakeReceive()
  946. // In particular, it should call registered pkt6_receive callback.
  947. srv_->run();
  948. // Check that the callback called is indeed the one we installed
  949. EXPECT_EQ("buffer6_send", callback_name_);
  950. // Check that there is one packet sent
  951. ASSERT_EQ(1, srv_->fake_sent_.size());
  952. Pkt6Ptr adv = srv_->fake_sent_.front();
  953. // Check that pkt6 argument passing was successful and returned proper
  954. // values
  955. EXPECT_TRUE(callback_resp_pkt6_.get() == adv.get());
  956. // Check that all expected parameters are there
  957. vector<string> expected_argument_names;
  958. expected_argument_names.push_back(string("response6"));
  959. EXPECT_TRUE(expected_argument_names == callback_argument_names_);
  960. // Pkt passed to a callout must be configured to copy retrieved options.
  961. EXPECT_TRUE(callback_resp_options_copy_);
  962. }
  963. // This test checks if subnet6_select callout is triggered and reports
  964. // valid parameters
  965. TEST_F(HooksDhcpv6SrvTest, subnet6Select) {
  966. // Configure 2 subnets, both directly reachable over local interface
  967. // (let's not complicate the matter with relays)
  968. string config = "{ \"interfaces-config\": {"
  969. " \"interfaces\": [ \"*\" ]"
  970. "},"
  971. "\"preferred-lifetime\": 3000,"
  972. "\"rebind-timer\": 2000, "
  973. "\"renew-timer\": 1000, "
  974. "\"subnet6\": [ { "
  975. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  976. " \"subnet\": \"2001:db8:1::/48\", "
  977. " \"interface\": \"" + valid_iface_ + "\" "
  978. " }, {"
  979. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  980. " \"subnet\": \"2001:db8:2::/48\" "
  981. " } ],"
  982. "\"valid-lifetime\": 4000 }";
  983. ConstElementPtr json;
  984. EXPECT_NO_THROW(json = parseDHCP6(config));
  985. ConstElementPtr status;
  986. // Configure the server and make sure the config is accepted
  987. EXPECT_NO_THROW(status = configureDhcp6Server(*srv_, json));
  988. ASSERT_TRUE(status);
  989. comment_ = isc::config::parseAnswer(rcode_, status);
  990. ASSERT_EQ(0, rcode_);
  991. CfgMgr::instance().commit();
  992. // Install pkt6_receive_callout
  993. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  994. "subnet6_select", subnet6_select_callout));
  995. // Prepare solicit packet. Server should select first subnet for it
  996. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  997. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  998. sol->setIface(valid_iface_);
  999. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1000. OptionPtr clientid = generateClientId();
  1001. sol->addOption(clientid);
  1002. // Pass it to the server and get an advertise
  1003. Pkt6Ptr adv = srv_->processSolicit(sol);
  1004. // Check if we get response at all
  1005. ASSERT_TRUE(adv);
  1006. // Check that the callback called is indeed the one we installed
  1007. EXPECT_EQ("subnet6_select", callback_name_);
  1008. // Check that pkt6 argument passing was successful and returned proper value
  1009. EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
  1010. const Subnet6Collection* exp_subnets =
  1011. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  1012. // The server is supposed to pick the first subnet, because of matching
  1013. // interface. Check that the value is reported properly.
  1014. ASSERT_TRUE(callback_subnet6_);
  1015. EXPECT_EQ(callback_subnet6_.get(), exp_subnets->front().get());
  1016. // Server is supposed to report two subnets
  1017. ASSERT_EQ(exp_subnets->size(), callback_subnet6collection_->size());
  1018. // Compare that the available subnets are reported as expected
  1019. EXPECT_TRUE((*exp_subnets)[0].get() == (*callback_subnet6collection_)[0].get());
  1020. EXPECT_TRUE((*exp_subnets)[1].get() == (*callback_subnet6collection_)[1].get());
  1021. // Pkt passed to a callout must be configured to copy retrieved options.
  1022. EXPECT_TRUE(callback_qry_options_copy_);
  1023. }
  1024. // This test checks if callout installed on subnet6_select hook point can pick
  1025. // a different subnet.
  1026. TEST_F(HooksDhcpv6SrvTest, subnet6SselectChange) {
  1027. // Configure 2 subnets, both directly reachable over local interface
  1028. // (let's not complicate the matter with relays)
  1029. string config = "{ \"interfaces-config\": {"
  1030. " \"interfaces\": [ \"*\" ]"
  1031. "},"
  1032. "\"preferred-lifetime\": 3000,"
  1033. "\"rebind-timer\": 2000, "
  1034. "\"renew-timer\": 1000, "
  1035. "\"subnet6\": [ { "
  1036. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1037. " \"subnet\": \"2001:db8:1::/48\", "
  1038. " \"interface\": \"" + valid_iface_ + "\" "
  1039. " }, {"
  1040. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  1041. " \"subnet\": \"2001:db8:2::/48\" "
  1042. " } ],"
  1043. "\"valid-lifetime\": 4000 }";
  1044. ConstElementPtr json;
  1045. EXPECT_NO_THROW(json = parseDHCP6(config));
  1046. ConstElementPtr status;
  1047. // Configure the server and make sure the config is accepted
  1048. EXPECT_NO_THROW(status = configureDhcp6Server(*srv_, json));
  1049. ASSERT_TRUE(status);
  1050. comment_ = isc::config::parseAnswer(rcode_, status);
  1051. ASSERT_EQ(0, rcode_);
  1052. CfgMgr::instance().commit();
  1053. // Install pkt6_receive_callout
  1054. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1055. "subnet6_select", subnet6_select_different_subnet_callout));
  1056. // Prepare solicit packet. Server should select first subnet for it
  1057. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1058. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  1059. sol->setIface(valid_iface_);
  1060. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1061. OptionPtr clientid = generateClientId();
  1062. sol->addOption(clientid);
  1063. // Pass it to the server and get an advertise
  1064. Pkt6Ptr adv = srv_->processSolicit(sol);
  1065. // Check if we get response at all
  1066. ASSERT_TRUE(adv);
  1067. // The response should have an address from second pool, so let's check it
  1068. OptionPtr tmp = adv->getOption(D6O_IA_NA);
  1069. ASSERT_TRUE(tmp);
  1070. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1071. ASSERT_TRUE(ia);
  1072. tmp = ia->getOption(D6O_IAADDR);
  1073. ASSERT_TRUE(tmp);
  1074. boost::shared_ptr<Option6IAAddr> addr_opt =
  1075. boost::dynamic_pointer_cast<Option6IAAddr>(tmp);
  1076. ASSERT_TRUE(addr_opt);
  1077. // Get all subnets and use second subnet for verification
  1078. const Subnet6Collection* subnets =
  1079. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  1080. ASSERT_EQ(2, subnets->size());
  1081. // Advertised address must belong to the second pool (in subnet's range,
  1082. // in dynamic pool)
  1083. EXPECT_TRUE((*subnets)[1]->inRange(addr_opt->getAddress()));
  1084. EXPECT_TRUE((*subnets)[1]->inPool(Lease::TYPE_NA, addr_opt->getAddress()));
  1085. }
  1086. // This test verifies that incoming (positive) RENEW can be handled properly,
  1087. // and the lease6_renew callouts are triggered.
  1088. TEST_F(HooksDhcpv6SrvTest, basicLease6Renew) {
  1089. NakedDhcpv6Srv srv(0);
  1090. // Install pkt6_receive_callout
  1091. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1092. "lease6_renew", lease6_renew_callout));
  1093. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1094. const uint32_t iaid = 234;
  1095. // Generate client-id also duid_
  1096. OptionPtr clientid = generateClientId();
  1097. // Check that the address we are about to use is indeed in pool
  1098. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1099. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1100. // value on purpose. They should be updated during RENEW.
  1101. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1102. 501, 502, 503, 504, subnet_->getID(),
  1103. HWAddrPtr(), 0));
  1104. lease->cltt_ = 1234;
  1105. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1106. // Check that the lease is really in the database
  1107. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1108. addr);
  1109. ASSERT_TRUE(l);
  1110. // Check that T1, T2, preferred, valid and cltt really set and not using
  1111. // previous (500, 501, etc.) values
  1112. EXPECT_NE(l->t1_, subnet_->getT1());
  1113. EXPECT_NE(l->t2_, subnet_->getT2());
  1114. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  1115. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  1116. EXPECT_NE(l->cltt_, time(NULL));
  1117. // Let's create a RENEW
  1118. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  1119. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1120. req->setIface("eth0");
  1121. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1122. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1123. ia->addOption(renewed_addr_opt);
  1124. req->addOption(ia);
  1125. req->addOption(clientid);
  1126. // Server-id is mandatory in RENEW
  1127. req->addOption(srv.getServerID());
  1128. // Pass it to the server and hope for a REPLY
  1129. Pkt6Ptr reply = srv.processRenew(req);
  1130. ASSERT_TRUE(reply);
  1131. // Check that the callback called is indeed the one we installed
  1132. EXPECT_EQ("lease6_renew", callback_name_);
  1133. // Check that appropriate parameters are passed to the callouts
  1134. EXPECT_TRUE(callback_qry_pkt6_);
  1135. EXPECT_TRUE(callback_lease6_);
  1136. EXPECT_TRUE(callback_ia_na_);
  1137. // Check if all expected parameters were really received
  1138. vector<string> expected_argument_names;
  1139. expected_argument_names.push_back("query6");
  1140. expected_argument_names.push_back("lease6");
  1141. expected_argument_names.push_back("ia_na");
  1142. sort(callback_argument_names_.begin(), callback_argument_names_.end());
  1143. sort(expected_argument_names.begin(), expected_argument_names.end());
  1144. EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
  1145. // Check if we get response at all
  1146. checkResponse(reply, DHCPV6_REPLY, 1234);
  1147. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1148. ASSERT_TRUE(tmp);
  1149. // Check that IA_NA was returned and that there's an address included
  1150. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 234, subnet_->getT1(),
  1151. subnet_->getT2());
  1152. ASSERT_TRUE(addr_opt);
  1153. // Check that the lease is really in the database
  1154. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  1155. ASSERT_TRUE(l);
  1156. // Check that the lease has been returned
  1157. ASSERT_TRUE(callback_lease6_);
  1158. // Check that the returned lease6 in callout is the same as the one in the
  1159. // database
  1160. EXPECT_TRUE(*callback_lease6_ == *l);
  1161. // Pkt passed to a callout must be configured to copy retrieved options.
  1162. EXPECT_TRUE(callback_qry_options_copy_);
  1163. }
  1164. // This test verifies that incoming (positive) RENEW can be handled properly,
  1165. // and the lease6_renew callouts are able to change the lease being updated.
  1166. TEST_F(HooksDhcpv6SrvTest, leaseUpdateLease6Renew) {
  1167. NakedDhcpv6Srv srv(0);
  1168. // Install pkt6_receive_callout
  1169. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1170. "lease6_renew", lease6_renew_update_callout));
  1171. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1172. const uint32_t iaid = 234;
  1173. // Generate client-id also duid_
  1174. OptionPtr clientid = generateClientId();
  1175. // Check that the address we are about to use is indeed in pool
  1176. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1177. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1178. // value on purpose. They should be updated during RENEW.
  1179. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1180. 501, 502, 503, 504, subnet_->getID(),
  1181. HWAddrPtr(), 0));
  1182. lease->cltt_ = 1234;
  1183. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1184. // Check that the lease is really in the database
  1185. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1186. addr);
  1187. ASSERT_TRUE(l);
  1188. // Check that T1, T2, preferred, valid and cltt really set and not using
  1189. // previous (500, 501, etc.) values
  1190. EXPECT_NE(l->t1_, subnet_->getT1());
  1191. EXPECT_NE(l->t2_, subnet_->getT2());
  1192. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  1193. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  1194. EXPECT_NE(l->cltt_, time(NULL));
  1195. // Let's create a RENEW
  1196. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  1197. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1198. req->setIface("eth0");
  1199. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1200. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1201. ia->addOption(renewed_addr_opt);
  1202. req->addOption(ia);
  1203. req->addOption(clientid);
  1204. // Server-id is mandatory in RENEW
  1205. req->addOption(srv.getServerID());
  1206. // Pass it to the server and hope for a REPLY
  1207. Pkt6Ptr reply = srv.processRenew(req);
  1208. ASSERT_TRUE(reply);
  1209. // Check if we get response at all
  1210. checkResponse(reply, DHCPV6_REPLY, 1234);
  1211. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1212. ASSERT_TRUE(tmp);
  1213. // Check that IA_NA was returned and that there's an address included
  1214. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 1000, 1001, 1002);
  1215. ASSERT_TRUE(addr_opt);
  1216. // Check that the lease is really in the database
  1217. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  1218. ASSERT_TRUE(l);
  1219. // Check that we chose the distinct override values
  1220. ASSERT_NE(override_t1_, subnet_->getT1());
  1221. ASSERT_NE(override_t2_, subnet_->getT2());
  1222. ASSERT_NE(override_preferred_, subnet_->getPreferred());
  1223. EXPECT_NE(override_valid_, subnet_->getValid());
  1224. // Check that T1, T2, preferred, valid were overridden the the callout
  1225. EXPECT_EQ(override_t1_, l->t1_);
  1226. EXPECT_EQ(override_t2_, l->t2_);
  1227. EXPECT_EQ(override_preferred_, l->preferred_lft_);
  1228. EXPECT_EQ(override_valid_, l->valid_lft_);
  1229. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  1230. int32_t cltt = static_cast<int32_t>(l->cltt_);
  1231. int32_t expected = static_cast<int32_t>(time(NULL));
  1232. // Equality or difference by 1 between cltt and expected is ok.
  1233. EXPECT_GE(1, abs(cltt - expected));
  1234. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr_opt->getAddress()));
  1235. }
  1236. // This test verifies that incoming (positive) RENEW can be handled properly,
  1237. // and the lease6_renew callouts are able to set the skip flag that will
  1238. // reject the renewal
  1239. TEST_F(HooksDhcpv6SrvTest, skipLease6Renew) {
  1240. NakedDhcpv6Srv srv(0);
  1241. // Install pkt6_receive_callout
  1242. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1243. "lease6_renew", lease6_renew_skip_callout));
  1244. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1245. const uint32_t iaid = 234;
  1246. // Generate client-id also duid_
  1247. OptionPtr clientid = generateClientId();
  1248. // Check that the address we are about to use is indeed in pool
  1249. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1250. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1251. // value on purpose. They should be updated during RENEW.
  1252. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1253. 501, 502, 503, 504, subnet_->getID(),
  1254. HWAddrPtr(), 0));
  1255. lease->cltt_ = 1234;
  1256. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1257. // Check that the lease is really in the database
  1258. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1259. addr);
  1260. ASSERT_TRUE(l);
  1261. // Check that T1, T2, preferred, valid and cltt really set and not using
  1262. // previous (500, 501, etc.) values
  1263. EXPECT_NE(l->t1_, subnet_->getT1());
  1264. EXPECT_NE(l->t2_, subnet_->getT2());
  1265. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  1266. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  1267. EXPECT_NE(l->cltt_, time(NULL));
  1268. // Let's create a RENEW
  1269. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  1270. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1271. req->setIface("eth0");
  1272. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1273. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1274. ia->addOption(renewed_addr_opt);
  1275. req->addOption(ia);
  1276. req->addOption(clientid);
  1277. // Server-id is mandatory in RENEW
  1278. req->addOption(srv.getServerID());
  1279. // Pass it to the server and hope for a REPLY
  1280. Pkt6Ptr reply = srv.processRenew(req);
  1281. ASSERT_TRUE(reply);
  1282. // Check that our callback was called
  1283. EXPECT_EQ("lease6_renew", callback_name_);
  1284. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1285. // Check that the old values are still there and they were not
  1286. // updated by the renewal
  1287. EXPECT_NE(l->t1_, subnet_->getT1());
  1288. EXPECT_NE(l->t2_, subnet_->getT2());
  1289. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  1290. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  1291. EXPECT_NE(l->cltt_, time(NULL));
  1292. }
  1293. // This test verifies that incoming (positive) RELEASE can be handled properly,
  1294. // that a REPLY is generated, that the response has status code and that the
  1295. // lease is indeed removed from the database.
  1296. //
  1297. // expected:
  1298. // - returned REPLY message has copy of client-id
  1299. // - returned REPLY message has server-id
  1300. // - returned REPLY message has IA that does not include an IAADDR
  1301. // - lease is actually removed from LeaseMgr
  1302. TEST_F(HooksDhcpv6SrvTest, basicLease6Release) {
  1303. NakedDhcpv6Srv srv(0);
  1304. // Install pkt6_receive_callout
  1305. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1306. "lease6_release", lease6_release_callout));
  1307. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1308. const uint32_t iaid = 234;
  1309. // Generate client-id also duid_
  1310. OptionPtr clientid = generateClientId();
  1311. // Check that the address we are about to use is indeed in pool
  1312. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1313. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1314. // value on purpose. They should be updated during RENEW.
  1315. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1316. 501, 502, 503, 504, subnet_->getID(),
  1317. HWAddrPtr(), 0));
  1318. lease->cltt_ = 1234;
  1319. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1320. // Check that the lease is really in the database
  1321. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1322. addr);
  1323. ASSERT_TRUE(l);
  1324. // Let's create a RELEASE
  1325. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  1326. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1327. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1328. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1329. ia->addOption(released_addr_opt);
  1330. req->addOption(ia);
  1331. req->addOption(clientid);
  1332. // Server-id is mandatory in RELEASE
  1333. req->addOption(srv.getServerID());
  1334. // Pass it to the server and hope for a REPLY
  1335. Pkt6Ptr reply = srv.processRelease(req);
  1336. ASSERT_TRUE(reply);
  1337. // Check that the callback called is indeed the one we installed
  1338. EXPECT_EQ("lease6_release", callback_name_);
  1339. // Check that appropriate parameters are passed to the callouts
  1340. EXPECT_TRUE(callback_qry_pkt6_);
  1341. EXPECT_TRUE(callback_lease6_);
  1342. // Check if all expected parameters were really received
  1343. vector<string> expected_argument_names;
  1344. expected_argument_names.push_back("query6");
  1345. expected_argument_names.push_back("lease6");
  1346. sort(callback_argument_names_.begin(), callback_argument_names_.end());
  1347. sort(expected_argument_names.begin(), expected_argument_names.end());
  1348. EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
  1349. // Check that the lease is really gone in the database
  1350. // get lease by address
  1351. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1352. ASSERT_FALSE(l);
  1353. // Get lease by subnetid/duid/iaid combination
  1354. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, *duid_, iaid,
  1355. subnet_->getID());
  1356. ASSERT_FALSE(l);
  1357. // Pkt passed to a callout must be configured to copy retrieved options.
  1358. EXPECT_TRUE(callback_qry_options_copy_);
  1359. }
  1360. // This is a variant of the previous test that tests that callouts are
  1361. // properly invoked for the prefix release case.
  1362. TEST_F(HooksDhcpv6SrvTest, basicLease6ReleasePD) {
  1363. NakedDhcpv6Srv srv(0);
  1364. // Install pkt6_receive_callout
  1365. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1366. "lease6_release", lease6_release_callout));
  1367. const IOAddress prefix("2001:db8:1:2:1::");
  1368. const uint32_t iaid = 234;
  1369. // Generate client-id also duid_
  1370. OptionPtr clientid = generateClientId();
  1371. // Check that the prefix we are about to use is indeed in pool
  1372. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_PD, prefix));
  1373. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1374. // value on purpose. They should be updated during RENEW.
  1375. Lease6Ptr lease(new Lease6(Lease::TYPE_PD, prefix, duid_, iaid,
  1376. 501, 502, 503, 504, subnet_->getID(),
  1377. HWAddrPtr(), 80));
  1378. lease->cltt_ = 1234;
  1379. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1380. // Check that the lease is really in the database
  1381. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_PD,
  1382. prefix);
  1383. ASSERT_TRUE(l);
  1384. // Let's create a RELEASE
  1385. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  1386. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1387. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_PD, iaid, 1500, 3000);
  1388. OptionPtr released_addr_opt(new Option6IAPrefix(D6O_IAPREFIX, prefix, 80,
  1389. 300, 500));
  1390. ia->addOption(released_addr_opt);
  1391. req->addOption(ia);
  1392. req->addOption(clientid);
  1393. // Server-id is mandatory in RELEASE
  1394. req->addOption(srv.getServerID());
  1395. // Pass it to the server and hope for a REPLY
  1396. Pkt6Ptr reply = srv.processRelease(req);
  1397. ASSERT_TRUE(reply);
  1398. // Check that the callback called is indeed the one we installed
  1399. EXPECT_EQ("lease6_release", callback_name_);
  1400. // Check that appropriate parameters are passed to the callouts
  1401. EXPECT_TRUE(callback_qry_pkt6_);
  1402. EXPECT_TRUE(callback_lease6_);
  1403. // Check if all expected parameters were really received
  1404. vector<string> expected_argument_names;
  1405. expected_argument_names.push_back("query6");
  1406. expected_argument_names.push_back("lease6");
  1407. sort(callback_argument_names_.begin(), callback_argument_names_.end());
  1408. sort(expected_argument_names.begin(), expected_argument_names.end());
  1409. EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
  1410. // Check that the lease is really gone in the database
  1411. // get lease by address
  1412. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_PD, prefix);
  1413. ASSERT_FALSE(l);
  1414. // Get lease by subnetid/duid/iaid combination
  1415. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_PD, *duid_, iaid,
  1416. subnet_->getID());
  1417. ASSERT_FALSE(l);
  1418. // Pkt passed to a callout must be configured to copy retrieved options.
  1419. EXPECT_TRUE(callback_qry_options_copy_);
  1420. }
  1421. // This test verifies that incoming (positive) RELEASE can be handled properly,
  1422. // that a REPLY is generated, that the response has status code and that the
  1423. // lease is indeed removed from the database.
  1424. //
  1425. // expected:
  1426. // - returned REPLY message has copy of client-id
  1427. // - returned REPLY message has server-id
  1428. // - returned REPLY message has IA that does not include an IAADDR
  1429. // - lease is actually removed from LeaseMgr
  1430. TEST_F(HooksDhcpv6SrvTest, skipLease6Release) {
  1431. NakedDhcpv6Srv srv(0);
  1432. // Install pkt6_receive_callout
  1433. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1434. "lease6_release", lease6_release_skip_callout));
  1435. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1436. const uint32_t iaid = 234;
  1437. // Generate client-id also duid_
  1438. OptionPtr clientid = generateClientId();
  1439. // Check that the address we are about to use is indeed in pool
  1440. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1441. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1442. // value on purpose. They should be updated during RENEW.
  1443. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1444. 501, 502, 503, 504, subnet_->getID(),
  1445. HWAddrPtr(), 0));
  1446. lease->cltt_ = 1234;
  1447. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1448. // Check that the lease is really in the database
  1449. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1450. addr);
  1451. ASSERT_TRUE(l);
  1452. // Let's create a RELEASE
  1453. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  1454. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1455. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1456. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1457. ia->addOption(released_addr_opt);
  1458. req->addOption(ia);
  1459. req->addOption(clientid);
  1460. // Server-id is mandatory in RELEASE
  1461. req->addOption(srv.getServerID());
  1462. // Pass it to the server and hope for a REPLY
  1463. Pkt6Ptr reply = srv.processRelease(req);
  1464. ASSERT_TRUE(reply);
  1465. // Check that the callback called is indeed the one we installed
  1466. EXPECT_EQ("lease6_release", callback_name_);
  1467. // Check that the lease is still there
  1468. // get lease by address
  1469. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1470. addr);
  1471. ASSERT_TRUE(l);
  1472. // Get lease by subnetid/duid/iaid combination
  1473. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, *duid_, iaid,
  1474. subnet_->getID());
  1475. ASSERT_TRUE(l);
  1476. }
  1477. // This test verifies that incoming (positive) REBIND can be handled properly,
  1478. // and the lease6_rebind callouts are triggered.
  1479. TEST_F(HooksDhcpv6SrvTest, basicLease6Rebind) {
  1480. NakedDhcpv6Srv srv(0);
  1481. // Install pkt6_receive_callout
  1482. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1483. "lease6_rebind", lease6_rebind_callout));
  1484. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1485. const uint32_t iaid = 234;
  1486. // Generate client-id also duid_
  1487. OptionPtr clientid = generateClientId();
  1488. // Check that the address we are about to use is indeed in pool
  1489. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1490. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1491. // value on purpose. They should be updated during REBIND.
  1492. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1493. 501, 502, 503, 504, subnet_->getID(),
  1494. HWAddrPtr(), 0));
  1495. lease->cltt_ = 1234;
  1496. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1497. // Check that the lease is really in the database
  1498. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1499. addr);
  1500. ASSERT_TRUE(l);
  1501. // Check that T1, T2, preferred, valid and cltt really set and not using
  1502. // previous (500, 501, etc.) values
  1503. EXPECT_NE(l->t1_, subnet_->getT1());
  1504. EXPECT_NE(l->t2_, subnet_->getT2());
  1505. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  1506. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  1507. EXPECT_NE(l->cltt_, time(NULL));
  1508. // Let's create a REBIND
  1509. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REBIND, 1234));
  1510. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1511. req->setIface("eth0");
  1512. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1513. OptionPtr rebound_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1514. ia->addOption(rebound_addr_opt);
  1515. req->addOption(ia);
  1516. req->addOption(clientid);
  1517. // Pass it to the server and hope for a REPLY
  1518. Pkt6Ptr reply = srv.processRebind(req);
  1519. ASSERT_TRUE(reply);
  1520. // Check that the callback called is indeed the one we installed
  1521. EXPECT_EQ("lease6_rebind", callback_name_);
  1522. // Check that appropriate parameters are passed to the callouts
  1523. EXPECT_TRUE(callback_qry_pkt6_);
  1524. EXPECT_TRUE(callback_lease6_);
  1525. EXPECT_TRUE(callback_ia_na_);
  1526. // Check if all expected parameters were really received
  1527. vector<string> expected_argument_names;
  1528. expected_argument_names.push_back("query6");
  1529. expected_argument_names.push_back("lease6");
  1530. expected_argument_names.push_back("ia_na");
  1531. sort(callback_argument_names_.begin(), callback_argument_names_.end());
  1532. sort(expected_argument_names.begin(), expected_argument_names.end());
  1533. EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
  1534. // Check if we get response at all
  1535. checkResponse(reply, DHCPV6_REPLY, 1234);
  1536. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1537. ASSERT_TRUE(tmp);
  1538. // Check that IA_NA was returned and that there's an address included
  1539. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 234, subnet_->getT1(),
  1540. subnet_->getT2());
  1541. ASSERT_TRUE(addr_opt);
  1542. // Check that the lease is really in the database
  1543. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  1544. ASSERT_TRUE(l);
  1545. // Check that the lease has been returned
  1546. ASSERT_TRUE(callback_lease6_);
  1547. // Check that the returned lease6 in callout is the same as the one in the
  1548. // database
  1549. EXPECT_TRUE(*callback_lease6_ == *l);
  1550. }
  1551. // This test verifies that incoming (positive) REBIND can be handled properly,
  1552. // and the lease6_rebind callouts are able to change the lease being updated.
  1553. TEST_F(HooksDhcpv6SrvTest, leaseUpdateLease6Rebind) {
  1554. NakedDhcpv6Srv srv(0);
  1555. // Install pkt6_receive_callout
  1556. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1557. "lease6_rebind", lease6_rebind_update_callout));
  1558. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1559. const uint32_t iaid = 234;
  1560. // Generate client-id also duid_
  1561. OptionPtr clientid = generateClientId();
  1562. // Check that the address we are about to use is indeed in pool
  1563. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1564. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1565. // value on purpose. They should be updated during REBIND.
  1566. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1567. 501, 502, 503, 504, subnet_->getID(),
  1568. HWAddrPtr(), 0));
  1569. lease->cltt_ = 1234;
  1570. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1571. // Check that the lease is really in the database
  1572. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1573. addr);
  1574. ASSERT_TRUE(l);
  1575. // Check that T1, T2, preferred, valid and cltt really set and not using
  1576. // previous (500, 501, etc.) values
  1577. EXPECT_NE(l->t1_, subnet_->getT1());
  1578. EXPECT_NE(l->t2_, subnet_->getT2());
  1579. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  1580. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  1581. EXPECT_NE(l->cltt_, time(NULL));
  1582. // Let's create a REBIND
  1583. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REBIND, 1234));
  1584. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1585. req->setIface("eth0");
  1586. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1587. OptionPtr rebound_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1588. ia->addOption(rebound_addr_opt);
  1589. req->addOption(ia);
  1590. req->addOption(clientid);
  1591. // Pass it to the server and hope for a REPLY
  1592. Pkt6Ptr reply = srv.processRebind(req);
  1593. ASSERT_TRUE(reply);
  1594. // Check if we get response at all
  1595. checkResponse(reply, DHCPV6_REPLY, 1234);
  1596. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1597. ASSERT_TRUE(tmp);
  1598. // Check that IA_NA was returned and that there's an address included
  1599. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 1000, 1001, 1002);
  1600. ASSERT_TRUE(addr_opt);
  1601. // Check that the lease is really in the database
  1602. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  1603. ASSERT_TRUE(l);
  1604. // Check that we chose the distinct override values
  1605. ASSERT_NE(override_t1_, subnet_->getT1());
  1606. ASSERT_NE(override_t2_, subnet_->getT2());
  1607. ASSERT_NE(override_preferred_, subnet_->getPreferred());
  1608. EXPECT_NE(override_valid_, subnet_->getValid());
  1609. // Check that T1, T2, preferred, valid were overridden the the callout
  1610. EXPECT_EQ(override_t1_, l->t1_);
  1611. EXPECT_EQ(override_t2_, l->t2_);
  1612. EXPECT_EQ(override_preferred_, l->preferred_lft_);
  1613. EXPECT_EQ(override_valid_, l->valid_lft_);
  1614. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  1615. int32_t cltt = static_cast<int32_t>(l->cltt_);
  1616. int32_t expected = static_cast<int32_t>(time(NULL));
  1617. // Equality or difference by 1 between cltt and expected is ok.
  1618. EXPECT_GE(1, abs(cltt - expected));
  1619. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr_opt->getAddress()));
  1620. }
  1621. // This test verifies that incoming (positive) REBIND can be handled properly,
  1622. // and the lease6_rebind callouts are able to set the skip flag that will
  1623. // reject the rebinding
  1624. TEST_F(HooksDhcpv6SrvTest, skipLease6Rebind) {
  1625. NakedDhcpv6Srv srv(0);
  1626. // Install pkt6_receive_callout
  1627. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1628. "lease6_rebind", lease6_rebind_skip_callout));
  1629. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1630. const uint32_t iaid = 234;
  1631. // Generate client-id also duid_
  1632. OptionPtr clientid = generateClientId();
  1633. // Check that the address we are about to use is indeed in pool
  1634. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1635. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1636. // value on purpose. They should be updated during REBIND.
  1637. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1638. 501, 502, 503, 504, subnet_->getID(),
  1639. HWAddrPtr(), 0));
  1640. lease->cltt_ = 1234;
  1641. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1642. // Check that the lease is really in the database
  1643. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1644. addr);
  1645. ASSERT_TRUE(l);
  1646. // Check that T1, T2, preferred, valid and cltt really set and not using
  1647. // previous (500, 501, etc.) values
  1648. EXPECT_NE(l->t1_, subnet_->getT1());
  1649. EXPECT_NE(l->t2_, subnet_->getT2());
  1650. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  1651. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  1652. EXPECT_NE(l->cltt_, time(NULL));
  1653. // Let's create a REBIND
  1654. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REBIND, 1234));
  1655. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1656. req->setIface("eth0");
  1657. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1658. OptionPtr rebound_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1659. ia->addOption(rebound_addr_opt);
  1660. req->addOption(ia);
  1661. req->addOption(clientid);
  1662. // Pass it to the server and hope for a REPLY
  1663. Pkt6Ptr reply = srv.processRebind(req);
  1664. ASSERT_TRUE(reply);
  1665. // Check that our callback was called
  1666. EXPECT_EQ("lease6_rebind", callback_name_);
  1667. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1668. // Check that the old values are still there and they were not
  1669. // updated by the rebinding
  1670. EXPECT_NE(l->t1_, subnet_->getT1());
  1671. EXPECT_NE(l->t2_, subnet_->getT2());
  1672. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  1673. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  1674. EXPECT_NE(l->cltt_, time(NULL));
  1675. }
  1676. // This test checks that the basic decline hook (lease6_decline) is
  1677. // triggered properly.
  1678. TEST_F(HooksDhcpv6SrvTest, basicLease6Decline) {
  1679. IfaceMgrTestConfig test_config(true);
  1680. // Libraries will be reloaded later
  1681. HooksManager::getSharedCalloutManager().reset(new CalloutManager(0));
  1682. // Install lease6_decline callout
  1683. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1684. "lease6_decline", lease6_decline_callout));
  1685. // Get an address and decline it. DUIDs, IAID match and we send valid
  1686. // address, so the decline procedure should be successful.
  1687. Dhcp6Client client;
  1688. acquireAndDecline(client, "01:02:03:04:05:06", 1234, "01:02:03:04:05:06",
  1689. 1234, VALID_ADDR, SHOULD_PASS);
  1690. // Check that the proper callback was called.
  1691. EXPECT_EQ("lease6_decline", callback_name_);
  1692. // And valid parameters were passed.
  1693. ASSERT_TRUE(callback_qry_pkt6_);
  1694. ASSERT_TRUE(callback_lease6_);
  1695. // Test sanity check - it was a decline, right?
  1696. EXPECT_EQ(DHCPV6_DECLINE, callback_qry_pkt6_->getType());
  1697. // Get the address from this decline.
  1698. OptionPtr ia = callback_qry_pkt6_->getOption(D6O_IA_NA);
  1699. ASSERT_TRUE(ia);
  1700. boost::shared_ptr<Option6IAAddr> addr_opt =
  1701. boost::dynamic_pointer_cast<Option6IAAddr>(ia->getOption(D6O_IAADDR));
  1702. ASSERT_TRUE(addr_opt);
  1703. IOAddress addr(addr_opt->getAddress());
  1704. // Now get a lease from the database.
  1705. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1706. addr);
  1707. ASSERT_TRUE(from_mgr);
  1708. // Now check that it's indeed declined.
  1709. EXPECT_EQ(Lease::STATE_DECLINED, from_mgr->state_);
  1710. // And that the parameters passed to callout are consistent with the database
  1711. EXPECT_EQ(addr, from_mgr->addr_);
  1712. EXPECT_EQ(addr, callback_lease6_->addr_);
  1713. // Pkt passed to a callout must be configured to copy retrieved options.
  1714. EXPECT_TRUE(callback_qry_options_copy_);
  1715. }
  1716. // Test that the lease6_decline hook point can handle SKIP status.
  1717. TEST_F(HooksDhcpv6SrvTest, lease6DeclineSkip) {
  1718. IfaceMgrTestConfig test_config(true);
  1719. // Libraries will be reloaded later
  1720. HooksManager::getSharedCalloutManager().reset(new CalloutManager(0));
  1721. // Install lease6_decline callout. It will set the status to skip
  1722. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1723. "lease6_decline", lease6_decline_skip_callout));
  1724. // Get an address and decline it. DUIDs, IAID match and we send valid
  1725. // address, so the decline procedure should be successful.
  1726. Dhcp6Client client;
  1727. acquireAndDecline(client, "01:02:03:04:05:06", 1234, "01:02:03:04:05:06",
  1728. 1234, VALID_ADDR, SHOULD_FAIL);
  1729. // Check that the proper callback was called.
  1730. EXPECT_EQ("lease6_decline", callback_name_);
  1731. // And valid parameters were passed.
  1732. ASSERT_TRUE(callback_qry_pkt6_);
  1733. ASSERT_TRUE(callback_lease6_);
  1734. // Test sanity check - it was a decline, right?
  1735. EXPECT_EQ(DHCPV6_DECLINE, callback_qry_pkt6_->getType());
  1736. // Get the address from this decline.
  1737. OptionPtr ia = callback_qry_pkt6_->getOption(D6O_IA_NA);
  1738. ASSERT_TRUE(ia);
  1739. boost::shared_ptr<Option6IAAddr> addr_opt =
  1740. boost::dynamic_pointer_cast<Option6IAAddr>(ia->getOption(D6O_IAADDR));
  1741. ASSERT_TRUE(addr_opt);
  1742. IOAddress addr(addr_opt->getAddress());
  1743. // Now get a lease from the database.
  1744. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1745. addr);
  1746. ASSERT_TRUE(from_mgr);
  1747. // Now check that it's NOT declined.
  1748. EXPECT_EQ(Lease::STATE_DEFAULT, from_mgr->state_);
  1749. // And that the parameters passed to callout are consistent with the database
  1750. EXPECT_EQ(addr, from_mgr->addr_);
  1751. EXPECT_EQ(addr, callback_lease6_->addr_);
  1752. }
  1753. // Test that the lease6_decline hook point can handle DROP status.
  1754. TEST_F(HooksDhcpv6SrvTest, lease6DeclineDrop) {
  1755. IfaceMgrTestConfig test_config(true);
  1756. // Libraries will be reloaded later
  1757. HooksManager::getSharedCalloutManager().reset(new CalloutManager(0));
  1758. // Install lease6_decline callout. It will set the status to skip
  1759. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1760. "lease6_decline", lease6_decline_drop_callout));
  1761. // Get an address and decline it. DUIDs, IAID match and we send valid
  1762. // address, so it would work, but the callout sets status to DROP, so
  1763. // the server should not update the lease and should not send back any
  1764. // packets.
  1765. Dhcp6Client client;
  1766. acquireAndDecline(client, "01:02:03:04:05:06", 1234, "01:02:03:04:05:06",
  1767. 1234, VALID_ADDR, SHOULD_FAIL);
  1768. // Check that the proper callback was called.
  1769. EXPECT_EQ("lease6_decline", callback_name_);
  1770. // And valid parameters were passed.
  1771. ASSERT_TRUE(callback_qry_pkt6_);
  1772. ASSERT_TRUE(callback_lease6_);
  1773. // Test sanity check - it was a decline, right?
  1774. EXPECT_EQ(DHCPV6_DECLINE, callback_qry_pkt6_->getType());
  1775. // Get the address from this decline.
  1776. OptionPtr ia = callback_qry_pkt6_->getOption(D6O_IA_NA);
  1777. ASSERT_TRUE(ia);
  1778. boost::shared_ptr<Option6IAAddr> addr_opt =
  1779. boost::dynamic_pointer_cast<Option6IAAddr>(ia->getOption(D6O_IAADDR));
  1780. ASSERT_TRUE(addr_opt);
  1781. IOAddress addr(addr_opt->getAddress());
  1782. // Now get a lease from the database.
  1783. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1784. addr);
  1785. ASSERT_TRUE(from_mgr);
  1786. // Now check that it's NOT declined.
  1787. EXPECT_EQ(Lease::STATE_DEFAULT, from_mgr->state_);
  1788. }
  1789. // Checks if callout installed on host6_identifier can generate an
  1790. // identifier and whether that identifier is actually used.
  1791. TEST_F(HooksDhcpv6SrvTest, host6Identifier) {
  1792. // Configure 2 subnets, both directly reachable over local interface
  1793. // (let's not complicate the matter with relays)
  1794. string config = "{ \"interfaces-config\": {\n"
  1795. " \"interfaces\": [ \"*\" ]\n"
  1796. "},\n"
  1797. "\"preferred-lifetime\": 3000,\n"
  1798. "\"rebind-timer\": 2000,\n"
  1799. "\"renew-timer\": 1000,\n"
  1800. "\"host-reservation-identifiers\": [ \"flex-id\" ],\n"
  1801. "\"subnet6\": [ {\n"
  1802. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],\n"
  1803. " \"subnet\": \"2001:db8::/48\", \n"
  1804. " \"interface\": \"" + valid_iface_ + "\",\n"
  1805. " \"reservations\": [\n"
  1806. " {\n"
  1807. " \"flex-id\": \"'foo'\",\n"
  1808. " \"ip-addresses\": [ \"2001:db8::f00\" ]\n"
  1809. " }\n"
  1810. " ]\n"
  1811. " } ]\n,"
  1812. "\"valid-lifetime\": 4000 }";
  1813. ConstElementPtr json;
  1814. EXPECT_NO_THROW(json = parseDHCP6(config));
  1815. ConstElementPtr status;
  1816. // Configure the server and make sure the config is accepted
  1817. EXPECT_NO_THROW(status = configureDhcp6Server(*srv_, json));
  1818. ASSERT_TRUE(status);
  1819. comment_ = isc::config::parseAnswer(rcode_, status);
  1820. ASSERT_EQ(0, rcode_);
  1821. CfgMgr::instance().commit();
  1822. // Install host6_identifier_foo_callout
  1823. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1824. "host6_identifier", host6_identifier_foo_callout));
  1825. // Prepare solicit packet. Server should select first subnet for it
  1826. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1827. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  1828. sol->setIface(valid_iface_);
  1829. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1830. OptionPtr clientid = generateClientId();
  1831. sol->addOption(clientid);
  1832. // Pass it to the server and get an advertise
  1833. Pkt6Ptr adv = srv_->processSolicit(sol);
  1834. // Check if we get response at all
  1835. ASSERT_TRUE(adv);
  1836. // Check that the callback called is indeed the one we installed
  1837. EXPECT_EQ("host6_identifier", callback_name_);
  1838. // Check that pkt6 argument passing was successful and returned proper value
  1839. EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
  1840. // Now check if we got the reserved address
  1841. OptionPtr tmp = adv->getOption(D6O_IA_NA);
  1842. ASSERT_TRUE(tmp);
  1843. // Check that IA_NA was returned and that there's an address included
  1844. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(adv, 234, 1000, 2000);
  1845. ASSERT_TRUE(addr_opt);
  1846. ASSERT_EQ("2001:db8::f00", addr_opt->getAddress().toText());
  1847. }
  1848. // Checks if callout installed on host6_identifier can generate an identifier
  1849. // other type. This particular callout always returns hwaddr.
  1850. TEST_F(HooksDhcpv6SrvTest, host6Identifier_hwaddr) {
  1851. // Configure 2 subnets, both directly reachable over local interface
  1852. // (let's not complicate the matter with relays)
  1853. string config = "{ \"interfaces-config\": {\n"
  1854. " \"interfaces\": [ \"*\" ]\n"
  1855. "},\n"
  1856. "\"preferred-lifetime\": 3000,\n"
  1857. "\"rebind-timer\": 2000,\n"
  1858. "\"renew-timer\": 1000,\n"
  1859. "\"host-reservation-identifiers\": [ \"flex-id\" ],\n"
  1860. "\"subnet6\": [ {\n"
  1861. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],\n"
  1862. " \"subnet\": \"2001:db8::/48\", \n"
  1863. " \"interface\": \"" + valid_iface_ + "\",\n"
  1864. " \"reservations\": [\n"
  1865. " {\n"
  1866. " \"hw-address\": \"00:01:02:03:04:05\",\n"
  1867. " \"ip-addresses\": [ \"2001:db8::f00\" ]\n"
  1868. " }\n"
  1869. " ]\n"
  1870. " } ]\n,"
  1871. "\"valid-lifetime\": 4000 }";
  1872. ConstElementPtr json;
  1873. EXPECT_NO_THROW(json = parseDHCP6(config));
  1874. ConstElementPtr status;
  1875. // Configure the server and make sure the config is accepted
  1876. EXPECT_NO_THROW(status = configureDhcp6Server(*srv_, json));
  1877. ASSERT_TRUE(status);
  1878. comment_ = isc::config::parseAnswer(rcode_, status);
  1879. ASSERT_EQ(0, rcode_);
  1880. CfgMgr::instance().commit();
  1881. // Install host6_identifier_foo_callout
  1882. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1883. "host6_identifier", host6_identifier_hwaddr_callout));
  1884. // Prepare solicit packet. Server should select first subnet for it
  1885. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1886. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  1887. sol->setIface(valid_iface_);
  1888. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1889. OptionPtr clientid = generateClientId();
  1890. sol->addOption(clientid);
  1891. // Pass it to the server and get an advertise
  1892. Pkt6Ptr adv = srv_->processSolicit(sol);
  1893. // Check if we get response at all
  1894. ASSERT_TRUE(adv);
  1895. // Check that the callback called is indeed the one we installed
  1896. EXPECT_EQ("host6_identifier", callback_name_);
  1897. // Check that pkt6 argument passing was successful and returned proper value
  1898. EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
  1899. // Now check if we got the reserved address
  1900. OptionPtr tmp = adv->getOption(D6O_IA_NA);
  1901. ASSERT_TRUE(tmp);
  1902. // Check that IA_NA was returned and that there's an address included
  1903. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(adv, 234, 1000, 2000);
  1904. ASSERT_TRUE(addr_opt);
  1905. ASSERT_EQ("2001:db8::f00", addr_opt->getAddress().toText());
  1906. }
  1907. // Verifies that libraries are unloaded by server destruction
  1908. // The callout libraries write their library index number to a marker
  1909. // file upon load and unload, making it simple to test whether or not
  1910. // the load and unload callouts have been invoked.
  1911. TEST_F(LoadUnloadDhcpv6SrvTest, unloadLibraries) {
  1912. ASSERT_NO_THROW(server_.reset(new NakedDhcpv6Srv(0)));
  1913. // Ensure no marker files to start with.
  1914. ASSERT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  1915. ASSERT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  1916. // Load the test libraries
  1917. HookLibsCollection libraries;
  1918. libraries.push_back(make_pair(std::string(CALLOUT_LIBRARY_1),
  1919. ConstElementPtr()));
  1920. libraries.push_back(make_pair(std::string(CALLOUT_LIBRARY_2),
  1921. ConstElementPtr()));
  1922. ASSERT_TRUE(HooksManager::loadLibraries(libraries));
  1923. // Verify that they load functions created the LOAD_MARKER_FILE
  1924. // and that it's contents are correct: "12" - the first library
  1925. // appends "1" to the file, the second appends "2"). Also
  1926. // check that the unload marker file does not yet exist.
  1927. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  1928. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  1929. // Destroy the server, instance which should unload the libraries.
  1930. server_.reset();
  1931. // Check that the libraries were unloaded. The libraries are
  1932. // unloaded in the reverse order to which they are loaded, and
  1933. // this should be reflected in the unload file.
  1934. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  1935. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  1936. }
  1937. } // end of anonymous namespace