dhcp6_srv_unittest.cc 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. // Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <config/ccsession.h>
  17. #include <dhcp/dhcp6.h>
  18. #include <dhcp/duid.h>
  19. #include <dhcp/option.h>
  20. #include <dhcp/option_custom.h>
  21. #include <dhcp/option6_addrlst.h>
  22. #include <dhcp/option6_ia.h>
  23. #include <dhcp/option6_iaaddr.h>
  24. #include <dhcp/option_int_array.h>
  25. #include <dhcp6/config_parser.h>
  26. #include <dhcp6/dhcp6_srv.h>
  27. #include <dhcpsrv/cfgmgr.h>
  28. #include <dhcpsrv/lease_mgr.h>
  29. #include <dhcpsrv/lease_mgr_factory.h>
  30. #include <dhcpsrv/utils.h>
  31. #include <util/buffer.h>
  32. #include <util/range_utilities.h>
  33. #include <boost/scoped_ptr.hpp>
  34. #include <gtest/gtest.h>
  35. #include <fstream>
  36. #include <iostream>
  37. #include <sstream>
  38. using namespace isc;
  39. using namespace isc::asiolink;
  40. using namespace isc::config;
  41. using namespace isc::data;
  42. using namespace isc::dhcp;
  43. using namespace isc::util;
  44. using namespace std;
  45. // namespace has to be named, because friends are defined in Dhcpv6Srv class
  46. // Maybe it should be isc::test?
  47. namespace {
  48. class NakedDhcpv6Srv: public Dhcpv6Srv {
  49. // "naked" Interface Manager, exposes internal members
  50. public:
  51. NakedDhcpv6Srv(uint16_t port):Dhcpv6Srv(port) { }
  52. using Dhcpv6Srv::processSolicit;
  53. using Dhcpv6Srv::processRequest;
  54. using Dhcpv6Srv::processRenew;
  55. using Dhcpv6Srv::createStatusCode;
  56. using Dhcpv6Srv::selectSubnet;
  57. using Dhcpv6Srv::sanityCheck;
  58. };
  59. class Dhcpv6SrvTest : public ::testing::Test {
  60. public:
  61. // these are empty for now, but let's keep them around
  62. Dhcpv6SrvTest() : rcode_(-1) {
  63. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 48, 1000,
  64. 2000, 3000, 4000));
  65. pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  66. subnet_->addPool(pool_);
  67. CfgMgr::instance().deleteSubnets6();
  68. CfgMgr::instance().addSubnet6(subnet_);
  69. }
  70. // Generate IA_NA option with specified parameters
  71. boost::shared_ptr<Option6IA> generateIA(uint32_t iaid, uint32_t t1, uint32_t t2) {
  72. boost::shared_ptr<Option6IA> ia =
  73. boost::shared_ptr<Option6IA>(new Option6IA(D6O_IA_NA, iaid));
  74. ia->setT1(t1);
  75. ia->setT2(t2);
  76. return (ia);
  77. }
  78. // Generate client-id option
  79. OptionPtr generateClientId(size_t duid_size = 32) {
  80. OptionBuffer clnt_duid(duid_size);
  81. for (int i = 0; i < duid_size; i++) {
  82. clnt_duid[i] = 100 + i;
  83. }
  84. duid_ = DuidPtr(new DUID(clnt_duid));
  85. return (OptionPtr(new Option(Option::V6, D6O_CLIENTID,
  86. clnt_duid.begin(),
  87. clnt_duid.begin() + duid_size)));
  88. }
  89. // Checks if server response (ADVERTISE or REPLY) includes proper server-id.
  90. void checkServerId(const Pkt6Ptr& rsp, const OptionPtr& expected_srvid) {
  91. // check that server included its server-id
  92. OptionPtr tmp = rsp->getOption(D6O_SERVERID);
  93. EXPECT_EQ(tmp->getType(), expected_srvid->getType() );
  94. ASSERT_EQ(tmp->len(), expected_srvid->len() );
  95. EXPECT_TRUE(tmp->getData() == expected_srvid->getData());
  96. }
  97. // Checks if server response (ADVERTISE or REPLY) includes proper client-id.
  98. void checkClientId(const Pkt6Ptr& rsp, const OptionPtr& expected_clientid) {
  99. // check that server included our own client-id
  100. OptionPtr tmp = rsp->getOption(D6O_CLIENTID);
  101. ASSERT_TRUE(tmp);
  102. EXPECT_EQ(expected_clientid->getType(), tmp->getType());
  103. ASSERT_EQ(expected_clientid->len(), tmp->len());
  104. // check that returned client-id is valid
  105. EXPECT_TRUE(expected_clientid->getData() == tmp->getData());
  106. }
  107. // Checks that server response (ADVERTISE or REPLY) contains proper IA_NA option
  108. // It returns IAADDR option for each chaining with checkIAAddr method.
  109. boost::shared_ptr<Option6IAAddr> checkIA_NA(const Pkt6Ptr& rsp, uint32_t expected_iaid,
  110. uint32_t expected_t1, uint32_t expected_t2) {
  111. OptionPtr tmp = rsp->getOption(D6O_IA_NA);
  112. // Can't use ASSERT_TRUE() in method that returns something
  113. if (!tmp) {
  114. ADD_FAILURE() << "IA_NA option not present in response";
  115. return (boost::shared_ptr<Option6IAAddr>());
  116. }
  117. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  118. EXPECT_EQ(expected_iaid, ia->getIAID() );
  119. EXPECT_EQ(expected_t1, ia->getT1());
  120. EXPECT_EQ(expected_t2, ia->getT2());
  121. tmp = ia->getOption(D6O_IAADDR);
  122. boost::shared_ptr<Option6IAAddr> addr = boost::dynamic_pointer_cast<Option6IAAddr>(tmp);
  123. return (addr);
  124. }
  125. // Checks that server rejected IA_NA, i.e. that it has no addresses and
  126. // that expected status code really appears there.
  127. // Status code indicates type of error encountered (in theory it can also
  128. // indicate success, but servers typically don't send success status
  129. // as this is the default result and it saves bandwidth)
  130. void checkRejectedIA_NA(const boost::shared_ptr<Option6IA>& ia,
  131. uint16_t expected_status_code) {
  132. // Make sure there is no address assigned.
  133. EXPECT_FALSE(ia->getOption(D6O_IAADDR));
  134. // T1, T2 should be zeroed
  135. EXPECT_EQ(0, ia->getT1());
  136. EXPECT_EQ(0, ia->getT2());
  137. boost::shared_ptr<OptionCustom> status =
  138. boost::dynamic_pointer_cast<OptionCustom>(ia->getOption(D6O_STATUS_CODE));
  139. EXPECT_TRUE(status);
  140. if (status) {
  141. // We don't have dedicated class for status code, so let's just interpret
  142. // first 2 bytes as status. Remainder of the status code option content is
  143. // just a text explanation what went wrong.
  144. EXPECT_EQ(static_cast<uint16_t>(expected_status_code),
  145. status->readInteger<uint16_t>(0));
  146. }
  147. }
  148. // Check that generated IAADDR option contains expected address.
  149. void checkIAAddr(const boost::shared_ptr<Option6IAAddr>& addr,
  150. const IOAddress& expected_addr,
  151. uint32_t expected_preferred, uint32_t expected_valid) {
  152. // Check that the assigned address is indeed from the configured pool.
  153. // Note that when comparing addresses, we compare the textual
  154. // representation. IOAddress does not support being streamed to
  155. // an ostream, which means it can't be used in EXPECT_EQ.
  156. EXPECT_TRUE(subnet_->inPool(addr->getAddress()));
  157. EXPECT_EQ(expected_addr.toText(), addr->getAddress().toText());
  158. EXPECT_EQ(addr->getPreferred(), subnet_->getPreferred());
  159. EXPECT_EQ(addr->getValid(), subnet_->getValid());
  160. }
  161. // Basic checks for generated response (message type and transaction-id).
  162. void checkResponse(const Pkt6Ptr& rsp, uint8_t expected_message_type,
  163. uint32_t expected_transid) {
  164. ASSERT_TRUE(rsp);
  165. EXPECT_EQ(expected_message_type, rsp->getType());
  166. EXPECT_EQ(expected_transid, rsp->getTransid());
  167. }
  168. // Checks if the lease sent to client is present in the database
  169. Lease6Ptr checkLease(const DuidPtr& duid, const OptionPtr& ia_na,
  170. boost::shared_ptr<Option6IAAddr> addr) {
  171. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(ia_na);
  172. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(addr->getAddress());
  173. if (!lease) {
  174. cout << "Lease for " << addr->getAddress().toText()
  175. << " not found in the database backend.";
  176. return (Lease6Ptr());
  177. }
  178. EXPECT_EQ(addr->getAddress().toText(), lease->addr_.toText());
  179. EXPECT_TRUE(*lease->duid_ == *duid);
  180. EXPECT_EQ(ia->getIAID(), lease->iaid_);
  181. EXPECT_EQ(subnet_->getID(), lease->subnet_id_);
  182. return (lease);
  183. }
  184. ~Dhcpv6SrvTest() {
  185. CfgMgr::instance().deleteSubnets6();
  186. };
  187. // A subnet used in most tests
  188. Subnet6Ptr subnet_;
  189. // A pool used in most tests
  190. Pool6Ptr pool_;
  191. // A DUID used in most tests (typically as client-id)
  192. DuidPtr duid_;
  193. int rcode_;
  194. ConstElementPtr comment_;
  195. };
  196. // Test verifies that the Dhcpv6_srv class can be instantiated. It checks a mode
  197. // without open sockets and with sockets opened on a high port (to not require
  198. // root privileges).
  199. TEST_F(Dhcpv6SrvTest, basic) {
  200. // srv has stubbed interface detection. It will read
  201. // interfaces.txt instead. It will pretend to have detected
  202. // fe80::1234 link-local address on eth0 interface. Obviously
  203. // an attempt to bind this socket will fail.
  204. boost::scoped_ptr<Dhcpv6Srv> srv;
  205. ASSERT_NO_THROW( {
  206. // Skip opening any sockets
  207. srv.reset(new Dhcpv6Srv(0));
  208. });
  209. srv.reset();
  210. ASSERT_NO_THROW({
  211. // open an unpriviledged port
  212. srv.reset(new Dhcpv6Srv(DHCP6_SERVER_PORT + 10000));
  213. });
  214. }
  215. // Test checks that DUID is generated properly
  216. TEST_F(Dhcpv6SrvTest, DUID) {
  217. boost::scoped_ptr<Dhcpv6Srv> srv;
  218. ASSERT_NO_THROW( {
  219. srv.reset(new Dhcpv6Srv(0));
  220. });
  221. OptionPtr srvid = srv->getServerID();
  222. ASSERT_TRUE(srvid);
  223. EXPECT_EQ(D6O_SERVERID, srvid->getType());
  224. OutputBuffer buf(32);
  225. srvid->pack(buf);
  226. // length of the actual DUID
  227. size_t len = buf.getLength() - srvid->getHeaderLen();
  228. InputBuffer data(buf.getData(), buf.getLength());
  229. // ignore first four bytes (standard DHCPv6 header)
  230. data.readUint32();
  231. uint16_t duid_type = data.readUint16();
  232. cout << "Duid-type=" << duid_type << endl;
  233. switch(duid_type) {
  234. case DUID::DUID_LLT: {
  235. // DUID must contain at least 6 bytes long MAC
  236. // + 8 bytes of fixed header
  237. EXPECT_GE(14, len);
  238. uint16_t hw_type = data.readUint16();
  239. // there's no real way to find out "correct"
  240. // hardware type
  241. EXPECT_GT(hw_type, 0);
  242. // check that timer is counted since 1.1.2000,
  243. // not from 1.1.1970.
  244. uint32_t seconds = data.readUint32();
  245. EXPECT_LE(seconds, DUID_TIME_EPOCH);
  246. // this test will start failing after 2030.
  247. // Hopefully we will be at BIND12 by then.
  248. // MAC must not be zeros
  249. vector<uint8_t> mac(len-8);
  250. vector<uint8_t> zeros(len-8, 0);
  251. data.readVector(mac, len-8);
  252. EXPECT_TRUE(mac != zeros);
  253. break;
  254. }
  255. case DUID::DUID_EN: {
  256. // there's not much we can check. Just simple
  257. // check if it is not all zeros
  258. vector<uint8_t> content(len-2);
  259. data.readVector(content, len-2);
  260. EXPECT_FALSE(isRangeZero(content.begin(), content.end()));
  261. break;
  262. }
  263. case DUID::DUID_LL: {
  264. // not supported yet
  265. cout << "Test not implemented for DUID-LL." << endl;
  266. // No failure here. There's really no way for test LL DUID. It doesn't
  267. // even make sense to check if that Link Layer is actually present on
  268. // a physical interface. RFC3315 says a server should write its DUID
  269. // and keep it despite hardware changes.
  270. break;
  271. }
  272. case DUID::DUID_UUID: // not supported yet
  273. default:
  274. ADD_FAILURE() << "Not supported duid type=" << duid_type << endl;
  275. break;
  276. }
  277. }
  278. // This test checks if Option Request Option (ORO) is parsed correctly
  279. // and the requested options are actually assigned.
  280. TEST_F(Dhcpv6SrvTest, advertiseOptions) {
  281. ConstElementPtr x;
  282. string config = "{ \"interface\": [ \"all\" ],"
  283. "\"preferred-lifetime\": 3000,"
  284. "\"rebind-timer\": 2000, "
  285. "\"renew-timer\": 1000, "
  286. "\"subnet6\": [ { "
  287. " \"pool\": [ \"2001:db8:1::/64\" ],"
  288. " \"subnet\": \"2001:db8:1::/48\", "
  289. " \"option-data\": [ {"
  290. " \"name\": \"OPTION_DNS_SERVERS\","
  291. " \"code\": 23,"
  292. " \"data\": \"2001 0DB8 1234 FFFF 0000 0000 0000 0001"
  293. "2001 0DB8 1234 FFFF 0000 0000 0000 0002\""
  294. " },"
  295. " {"
  296. " \"name\": \"OPTION_FOO\","
  297. " \"code\": 1000,"
  298. " \"data\": \"1234\""
  299. " } ]"
  300. " } ],"
  301. "\"valid-lifetime\": 4000 }";
  302. ElementPtr json = Element::fromJSON(config);
  303. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  304. ASSERT_NO_THROW(srv.reset(new NakedDhcpv6Srv(0)));
  305. EXPECT_NO_THROW(x = configureDhcp6Server(*srv, json));
  306. ASSERT_TRUE(x);
  307. comment_ = parseAnswer(rcode_, x);
  308. ASSERT_EQ(0, rcode_);
  309. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  310. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  311. sol->addOption(generateIA(234, 1500, 3000));
  312. OptionPtr clientid = generateClientId();
  313. sol->addOption(clientid);
  314. // Pass it to the server and get an advertise
  315. boost::shared_ptr<Pkt6> adv = srv->processSolicit(sol);
  316. // check if we get response at all
  317. ASSERT_TRUE(adv);
  318. // We have not requested option with code 1000 so it should not
  319. // be included in the response.
  320. ASSERT_FALSE(adv->getOption(1000));
  321. ASSERT_FALSE(adv->getOption(D6O_NAME_SERVERS));
  322. // Let's now request option with code 1000.
  323. // We expect that server will include this option in its reply.
  324. boost::shared_ptr<OptionIntArray<uint16_t> >
  325. option_oro(new OptionIntArray<uint16_t>(Option::V6, D6O_ORO));
  326. // Create vector with two option codes.
  327. std::vector<uint16_t> codes(2);
  328. codes[0] = 1000;
  329. codes[1] = D6O_NAME_SERVERS;
  330. // Pass this code to option.
  331. option_oro->setValues(codes);
  332. // Append ORO to SOLICIT message.
  333. sol->addOption(option_oro);
  334. // Need to process SOLICIT again after requesting new option.
  335. adv = srv->processSolicit(sol);
  336. ASSERT_TRUE(adv);
  337. OptionPtr tmp = adv->getOption(D6O_NAME_SERVERS);
  338. ASSERT_TRUE(tmp);
  339. boost::shared_ptr<Option6AddrLst> reply_nameservers =
  340. boost::dynamic_pointer_cast<Option6AddrLst>(tmp);
  341. ASSERT_TRUE(reply_nameservers);
  342. Option6AddrLst::AddressContainer addrs = reply_nameservers->getAddresses();
  343. ASSERT_EQ(2, addrs.size());
  344. EXPECT_TRUE(addrs[0] == IOAddress("2001:db8:1234:FFFF::1"));
  345. EXPECT_TRUE(addrs[1] == IOAddress("2001:db8:1234:FFFF::2"));
  346. // There is a dummy option with code 1000 we requested from a server.
  347. // Expect that this option is in server's response.
  348. tmp = adv->getOption(1000);
  349. ASSERT_TRUE(tmp);
  350. // Check that the option contains valid data (from configuration).
  351. std::vector<uint8_t> data = tmp->getData();
  352. ASSERT_EQ(2, data.size());
  353. const uint8_t foo_expected[] = {
  354. 0x12, 0x34
  355. };
  356. EXPECT_EQ(0, memcmp(&data[0], foo_expected, 2));
  357. // more checks to be implemented
  358. }
  359. // There are no dedicated tests for Dhcpv6Srv::handleIA_NA and Dhcpv6Srv::assignLeases
  360. // as they are indirectly tested in Solicit and Request tests.
  361. // This test verifies that incoming SOLICIT can be handled properly, that an
  362. // ADVERTISE is generated, that the response has an address and that address
  363. // really belongs to the configured pool.
  364. //
  365. // This test sends a SOLICIT without any hint in IA_NA.
  366. //
  367. // constructed very simple SOLICIT message with:
  368. // - client-id option (mandatory)
  369. // - IA option (a request for address, without any addresses)
  370. //
  371. // expected returned ADVERTISE message:
  372. // - copy of client-id
  373. // - server-id
  374. // - IA that includes IAADDR
  375. TEST_F(Dhcpv6SrvTest, SolicitBasic) {
  376. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  377. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  378. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  379. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  380. sol->addOption(generateIA(234, 1500, 3000));
  381. OptionPtr clientid = generateClientId();
  382. sol->addOption(clientid);
  383. // Pass it to the server and get an advertise
  384. Pkt6Ptr reply = srv->processSolicit(sol);
  385. // check if we get response at all
  386. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  387. // check that IA_NA was returned and that there's an address included
  388. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  389. subnet_->getT2());
  390. // Check that the assigned address is indeed from the configured pool
  391. checkIAAddr(addr, addr->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  392. // check DUIDs
  393. checkServerId(reply, srv->getServerID());
  394. checkClientId(reply, clientid);
  395. }
  396. // This test verifies that incoming SOLICIT can be handled properly, that an
  397. // ADVERTISE is generated, that the response has an address and that address
  398. // really belongs to the configured pool.
  399. //
  400. // This test sends a SOLICIT with IA_NA that contains a valid hint.
  401. //
  402. // constructed very simple SOLICIT message with:
  403. // - client-id option (mandatory)
  404. // - IA option (a request for address, with an address that belongs to the
  405. // configured pool, i.e. is valid as hint)
  406. //
  407. // expected returned ADVERTISE message:
  408. // - copy of client-id
  409. // - server-id
  410. // - IA that includes IAADDR
  411. TEST_F(Dhcpv6SrvTest, SolicitHint) {
  412. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  413. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  414. // Let's create a SOLICIT
  415. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  416. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  417. boost::shared_ptr<Option6IA> ia = generateIA(234, 1500, 3000);
  418. // with a valid hint
  419. IOAddress hint("2001:db8:1:1::dead:beef");
  420. ASSERT_TRUE(subnet_->inPool(hint));
  421. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  422. ia->addOption(hint_opt);
  423. sol->addOption(ia);
  424. OptionPtr clientid = generateClientId();
  425. sol->addOption(clientid);
  426. // Pass it to the server and get an advertise
  427. Pkt6Ptr reply = srv->processSolicit(sol);
  428. // check if we get response at all
  429. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  430. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  431. ASSERT_TRUE(tmp);
  432. // check that IA_NA was returned and that there's an address included
  433. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  434. subnet_->getT2());
  435. // check that we've got the address we requested
  436. checkIAAddr(addr, hint, subnet_->getPreferred(), subnet_->getValid());
  437. // check DUIDs
  438. checkServerId(reply, srv->getServerID());
  439. checkClientId(reply, clientid);
  440. }
  441. // This test verifies that incoming SOLICIT can be handled properly, that an
  442. // ADVERTISE is generated, that the response has an address and that address
  443. // really belongs to the configured pool.
  444. //
  445. // This test sends a SOLICIT with IA_NA that contains an invalid hint.
  446. //
  447. // constructed very simple SOLICIT message with:
  448. // - client-id option (mandatory)
  449. // - IA option (a request for address, with an address that does not
  450. // belong to the configured pool, i.e. is valid as hint)
  451. //
  452. // expected returned ADVERTISE message:
  453. // - copy of client-id
  454. // - server-id
  455. // - IA that includes IAADDR
  456. TEST_F(Dhcpv6SrvTest, SolicitInvalidHint) {
  457. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  458. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  459. // Let's create a SOLICIT
  460. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  461. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  462. boost::shared_ptr<Option6IA> ia = generateIA(234, 1500, 3000);
  463. IOAddress hint("2001:db8:1::cafe:babe");
  464. ASSERT_FALSE(subnet_->inPool(hint));
  465. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  466. ia->addOption(hint_opt);
  467. sol->addOption(ia);
  468. OptionPtr clientid = generateClientId();
  469. sol->addOption(clientid);
  470. // Pass it to the server and get an advertise
  471. Pkt6Ptr reply = srv->processSolicit(sol);
  472. // check if we get response at all
  473. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  474. // check that IA_NA was returned and that there's an address included
  475. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  476. subnet_->getT2());
  477. // Check that the assigned address is indeed from the configured pool
  478. checkIAAddr(addr, addr->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  479. EXPECT_TRUE(subnet_->inPool(addr->getAddress()));
  480. // check DUIDs
  481. checkServerId(reply, srv->getServerID());
  482. checkClientId(reply, clientid);
  483. }
  484. /// @todo: Add a test that client sends hint that is in pool, but currently
  485. /// being used by a different client.
  486. // This test checks that the server is offering different addresses to different
  487. // clients in ADVERTISEs. Please note that ADVERTISE is not a guarantee that such
  488. // and address will be assigned. Had the pool was very small and contained only
  489. // 2 addresses, the third client would get the same advertise as the first one
  490. // and this is a correct behavior. It is REQUEST that will fail for the third
  491. // client. ADVERTISE is basically saying "if you send me a request, you will
  492. // probably get an address like this" (there are no guarantees).
  493. TEST_F(Dhcpv6SrvTest, ManySolicits) {
  494. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  495. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  496. Pkt6Ptr sol1 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  497. Pkt6Ptr sol2 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 2345));
  498. Pkt6Ptr sol3 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 3456));
  499. sol1->setRemoteAddr(IOAddress("fe80::abcd"));
  500. sol2->setRemoteAddr(IOAddress("fe80::1223"));
  501. sol3->setRemoteAddr(IOAddress("fe80::3467"));
  502. sol1->addOption(generateIA(1, 1500, 3000));
  503. sol2->addOption(generateIA(2, 1500, 3000));
  504. sol3->addOption(generateIA(3, 1500, 3000));
  505. // different client-id sizes
  506. OptionPtr clientid1 = generateClientId(12);
  507. OptionPtr clientid2 = generateClientId(14);
  508. OptionPtr clientid3 = generateClientId(16);
  509. sol1->addOption(clientid1);
  510. sol2->addOption(clientid2);
  511. sol3->addOption(clientid3);
  512. // Pass it to the server and get an advertise
  513. Pkt6Ptr reply1 = srv->processSolicit(sol1);
  514. Pkt6Ptr reply2 = srv->processSolicit(sol2);
  515. Pkt6Ptr reply3 = srv->processSolicit(sol3);
  516. // check if we get response at all
  517. checkResponse(reply1, DHCPV6_ADVERTISE, 1234);
  518. checkResponse(reply2, DHCPV6_ADVERTISE, 2345);
  519. checkResponse(reply3, DHCPV6_ADVERTISE, 3456);
  520. // check that IA_NA was returned and that there's an address included
  521. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  522. subnet_->getT2());
  523. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  524. subnet_->getT2());
  525. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  526. subnet_->getT2());
  527. // Check that the assigned address is indeed from the configured pool
  528. checkIAAddr(addr1, addr1->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  529. checkIAAddr(addr2, addr2->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  530. checkIAAddr(addr3, addr3->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  531. // check DUIDs
  532. checkServerId(reply1, srv->getServerID());
  533. checkServerId(reply2, srv->getServerID());
  534. checkServerId(reply3, srv->getServerID());
  535. checkClientId(reply1, clientid1);
  536. checkClientId(reply2, clientid2);
  537. checkClientId(reply3, clientid3);
  538. // Finally check that the addresses offered are different
  539. EXPECT_NE(addr1->getAddress().toText(), addr2->getAddress().toText());
  540. EXPECT_NE(addr2->getAddress().toText(), addr3->getAddress().toText());
  541. EXPECT_NE(addr3->getAddress().toText(), addr1->getAddress().toText());
  542. cout << "Offered address to client1=" << addr1->getAddress().toText() << endl;
  543. cout << "Offered address to client2=" << addr2->getAddress().toText() << endl;
  544. cout << "Offered address to client3=" << addr3->getAddress().toText() << endl;
  545. }
  546. // This test verifies that incoming REQUEST can be handled properly, that a
  547. // REPLY is generated, that the response has an address and that address
  548. // really belongs to the configured pool.
  549. //
  550. // This test sends a REQUEST with IA_NA that contains a valid hint.
  551. //
  552. // constructed very simple REQUEST message with:
  553. // - client-id option (mandatory)
  554. // - IA option (a request for address, with an address that belongs to the
  555. // configured pool, i.e. is valid as hint)
  556. //
  557. // expected returned REPLY message:
  558. // - copy of client-id
  559. // - server-id
  560. // - IA that includes IAADDR
  561. TEST_F(Dhcpv6SrvTest, RequestBasic) {
  562. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  563. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  564. // Let's create a REQUEST
  565. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  566. req->setRemoteAddr(IOAddress("fe80::abcd"));
  567. boost::shared_ptr<Option6IA> ia = generateIA(234, 1500, 3000);
  568. // with a valid hint
  569. IOAddress hint("2001:db8:1:1::dead:beef");
  570. ASSERT_TRUE(subnet_->inPool(hint));
  571. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  572. ia->addOption(hint_opt);
  573. req->addOption(ia);
  574. OptionPtr clientid = generateClientId();
  575. req->addOption(clientid);
  576. // server-id is mandatory in REQUEST
  577. req->addOption(srv->getServerID());
  578. // Pass it to the server and hope for a REPLY
  579. Pkt6Ptr reply = srv->processRequest(req);
  580. // check if we get response at all
  581. checkResponse(reply, DHCPV6_REPLY, 1234);
  582. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  583. ASSERT_TRUE(tmp);
  584. // check that IA_NA was returned and that there's an address included
  585. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  586. subnet_->getT2());
  587. // check that we've got the address we requested
  588. checkIAAddr(addr, hint, subnet_->getPreferred(), subnet_->getValid());
  589. // check DUIDs
  590. checkServerId(reply, srv->getServerID());
  591. checkClientId(reply, clientid);
  592. // check that the lease is really in the database
  593. Lease6Ptr l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr);
  594. EXPECT_TRUE(l);
  595. LeaseMgrFactory::instance().deleteLease(addr->getAddress());
  596. }
  597. // This test checks that the server is offering different addresses to different
  598. // clients in REQUEST. Please note that ADVERTISE is not a guarantee that such
  599. // and address will be assigned. Had the pool was very small and contained only
  600. // 2 addresses, the third client would get the same advertise as the first one
  601. // and this is a correct behavior. It is REQUEST that will fail for the third
  602. // client. ADVERTISE is basically saying "if you send me a request, you will
  603. // probably get an address like this" (there are no guarantees).
  604. TEST_F(Dhcpv6SrvTest, ManyRequests) {
  605. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  606. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  607. Pkt6Ptr req1 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  608. Pkt6Ptr req2 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 2345));
  609. Pkt6Ptr req3 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 3456));
  610. req1->setRemoteAddr(IOAddress("fe80::abcd"));
  611. req2->setRemoteAddr(IOAddress("fe80::1223"));
  612. req3->setRemoteAddr(IOAddress("fe80::3467"));
  613. req1->addOption(generateIA(1, 1500, 3000));
  614. req2->addOption(generateIA(2, 1500, 3000));
  615. req3->addOption(generateIA(3, 1500, 3000));
  616. // different client-id sizes
  617. OptionPtr clientid1 = generateClientId(12);
  618. OptionPtr clientid2 = generateClientId(14);
  619. OptionPtr clientid3 = generateClientId(16);
  620. req1->addOption(clientid1);
  621. req2->addOption(clientid2);
  622. req3->addOption(clientid3);
  623. // server-id is mandatory in REQUEST
  624. req1->addOption(srv->getServerID());
  625. req2->addOption(srv->getServerID());
  626. req3->addOption(srv->getServerID());
  627. // Pass it to the server and get an advertise
  628. Pkt6Ptr reply1 = srv->processRequest(req1);
  629. Pkt6Ptr reply2 = srv->processRequest(req2);
  630. Pkt6Ptr reply3 = srv->processRequest(req3);
  631. // check if we get response at all
  632. checkResponse(reply1, DHCPV6_REPLY, 1234);
  633. checkResponse(reply2, DHCPV6_REPLY, 2345);
  634. checkResponse(reply3, DHCPV6_REPLY, 3456);
  635. // check that IA_NA was returned and that there's an address included
  636. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  637. subnet_->getT2());
  638. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  639. subnet_->getT2());
  640. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  641. subnet_->getT2());
  642. // Check that the assigned address is indeed from the configured pool
  643. checkIAAddr(addr1, addr1->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  644. checkIAAddr(addr2, addr2->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  645. checkIAAddr(addr3, addr3->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  646. // check DUIDs
  647. checkServerId(reply1, srv->getServerID());
  648. checkServerId(reply2, srv->getServerID());
  649. checkServerId(reply3, srv->getServerID());
  650. checkClientId(reply1, clientid1);
  651. checkClientId(reply2, clientid2);
  652. checkClientId(reply3, clientid3);
  653. // Finally check that the addresses offered are different
  654. EXPECT_NE(addr1->getAddress().toText(), addr2->getAddress().toText());
  655. EXPECT_NE(addr2->getAddress().toText(), addr3->getAddress().toText());
  656. EXPECT_NE(addr3->getAddress().toText(), addr1->getAddress().toText());
  657. cout << "Assigned address to client1=" << addr1->getAddress().toText() << endl;
  658. cout << "Assigned address to client2=" << addr2->getAddress().toText() << endl;
  659. cout << "Assigned address to client3=" << addr3->getAddress().toText() << endl;
  660. }
  661. // This test verifies that incoming (positive) RENEW can be handled properly, that a
  662. // REPLY is generated, that the response has an address and that address
  663. // really belongs to the configured pool and that lease is actually renewed.
  664. //
  665. // expected:
  666. // - returned REPLY message has copy of client-id
  667. // - returned REPLY message has server-id
  668. // - returned REPLY message has IA that includes IAADDR
  669. // - lease is actually renewed in LeaseMgr
  670. TEST_F(Dhcpv6SrvTest, RenewBasic) {
  671. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  672. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  673. const IOAddress addr("2001:db8:1:1::cafe:babe");
  674. const uint32_t iaid = 234;
  675. // Generate client-id also duid_
  676. OptionPtr clientid = generateClientId();
  677. // Check that the address we are about to use is indeed in pool
  678. ASSERT_TRUE(subnet_->inPool(addr));
  679. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  680. // value on purpose. They should be updated during RENEW.
  681. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid_, iaid,
  682. 501, 502, 503, 504, subnet_->getID(), 0));
  683. lease->cltt_ = 1234;
  684. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  685. // Check that the lease is really in the database
  686. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(addr);
  687. ASSERT_TRUE(l);
  688. // Check that T1, T2, preferred, valid and cltt really set and not using
  689. // previous (500, 501, etc.) values
  690. EXPECT_NE(l->t1_, subnet_->getT1());
  691. EXPECT_NE(l->t2_, subnet_->getT2());
  692. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  693. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  694. EXPECT_NE(l->cltt_, time(NULL));
  695. // Let's create a RENEW
  696. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  697. req->setRemoteAddr(IOAddress("fe80::abcd"));
  698. boost::shared_ptr<Option6IA> ia = generateIA(iaid, 1500, 3000);
  699. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  700. ia->addOption(renewed_addr_opt);
  701. req->addOption(ia);
  702. req->addOption(clientid);
  703. // Server-id is mandatory in RENEW
  704. req->addOption(srv->getServerID());
  705. // Pass it to the server and hope for a REPLY
  706. Pkt6Ptr reply = srv->processRenew(req);
  707. // Check if we get response at all
  708. checkResponse(reply, DHCPV6_REPLY, 1234);
  709. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  710. ASSERT_TRUE(tmp);
  711. // Check that IA_NA was returned and that there's an address included
  712. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 234, subnet_->getT1(),
  713. subnet_->getT2());
  714. // Check that we've got the address we requested
  715. checkIAAddr(addr_opt, addr, subnet_->getPreferred(), subnet_->getValid());
  716. // Check DUIDs
  717. checkServerId(reply, srv->getServerID());
  718. checkClientId(reply, clientid);
  719. // Check that the lease is really in the database
  720. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  721. ASSERT_TRUE(l);
  722. // Check that T1, T2, preferred, valid and cltt were really updated
  723. EXPECT_EQ(l->t1_, subnet_->getT1());
  724. EXPECT_EQ(l->t2_, subnet_->getT2());
  725. EXPECT_EQ(l->preferred_lft_, subnet_->getPreferred());
  726. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  727. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  728. int32_t cltt = static_cast<int32_t>(l->cltt_);
  729. int32_t expected = static_cast<int32_t>(time(NULL));
  730. // equality or difference by 1 between cltt and expected is ok.
  731. EXPECT_GE(1, abs(cltt - expected));
  732. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr_opt->getAddress()));
  733. }
  734. // This test verifies that incoming (invalid) RENEW can be handled properly.
  735. //
  736. // This test checks 3 scenarios:
  737. // 1. there is no such lease at all
  738. // 2. there is such a lease, but it is assigned to a different IAID
  739. // 3. there is such a lease, but it belongs to a different client
  740. //
  741. // expected:
  742. // - returned REPLY message has copy of client-id
  743. // - returned REPLY message has server-id
  744. // - returned REPLY message has IA that includes STATUS-CODE
  745. // - No lease in LeaseMgr
  746. TEST_F(Dhcpv6SrvTest, RenewReject) {
  747. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  748. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  749. const IOAddress addr("2001:db8:1:1::dead");
  750. const uint32_t transid = 1234;
  751. const uint32_t valid_iaid = 234;
  752. const uint32_t bogus_iaid = 456;
  753. // Quick sanity check that the address we're about to use is ok
  754. ASSERT_TRUE(subnet_->inPool(addr));
  755. // GenerateClientId() also sets duid_
  756. OptionPtr clientid = generateClientId();
  757. // Check that the lease is NOT in the database
  758. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(addr);
  759. ASSERT_FALSE(l);
  760. // Let's create a RENEW
  761. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, transid));
  762. req->setRemoteAddr(IOAddress("fe80::abcd"));
  763. boost::shared_ptr<Option6IA> ia = generateIA(bogus_iaid, 1500, 3000);
  764. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  765. ia->addOption(renewed_addr_opt);
  766. req->addOption(ia);
  767. req->addOption(clientid);
  768. // Server-id is mandatory in RENEW
  769. req->addOption(srv->getServerID());
  770. // Case 1: No lease known to server
  771. // Pass it to the server and hope for a REPLY
  772. Pkt6Ptr reply = srv->processRenew(req);
  773. // Check if we get response at all
  774. checkResponse(reply, DHCPV6_REPLY, transid);
  775. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  776. ASSERT_TRUE(tmp);
  777. // Check that IA_NA was returned and that there's an address included
  778. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  779. ASSERT_TRUE(ia);
  780. checkRejectedIA_NA(ia, STATUS_NoAddrsAvail);
  781. // Check that there is no lease added
  782. l = LeaseMgrFactory::instance().getLease6(addr);
  783. ASSERT_FALSE(l);
  784. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  785. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  786. // value on purpose. They should be updated during RENEW.
  787. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid_, valid_iaid,
  788. 501, 502, 503, 504, subnet_->getID(), 0));
  789. lease->cltt_ = 123; // Let's use it as an indicator that the lease
  790. // was NOT updated.
  791. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  792. // Pass it to the server and hope for a REPLY
  793. reply = srv->processRenew(req);
  794. checkResponse(reply, DHCPV6_REPLY, transid);
  795. tmp = reply->getOption(D6O_IA_NA);
  796. ASSERT_TRUE(tmp);
  797. // Check that IA_NA was returned and that there's an address included
  798. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  799. ASSERT_TRUE(ia);
  800. checkRejectedIA_NA(ia, STATUS_NoAddrsAvail);
  801. // There is a iaid mis-match, so server should respond that there is
  802. // no such address to renew.
  803. // CASE 3: Lease belongs to a client with different client-id
  804. req->delOption(D6O_CLIENTID);
  805. ia = boost::dynamic_pointer_cast<Option6IA>(req->getOption(D6O_IA_NA));
  806. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  807. req->addOption(generateClientId(13)); // generate different DUID
  808. // (with length 13)
  809. reply = srv->processRenew(req);
  810. checkResponse(reply, DHCPV6_REPLY, transid);
  811. tmp = reply->getOption(D6O_IA_NA);
  812. ASSERT_TRUE(tmp);
  813. // Check that IA_NA was returned and that there's an address included
  814. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  815. ASSERT_TRUE(ia);
  816. checkRejectedIA_NA(ia, STATUS_NoAddrsAvail);
  817. lease = LeaseMgrFactory::instance().getLease6(addr);
  818. ASSERT_TRUE(lease);
  819. // Verify that the lease was not updated.
  820. EXPECT_EQ(123, lease->cltt_);
  821. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  822. }
  823. // This test verifies if the status code option is generated properly.
  824. TEST_F(Dhcpv6SrvTest, StatusCode) {
  825. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  826. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  827. // a dummy content for client-id
  828. uint8_t expected[] = {
  829. 0x0, 0xD, // option code = 13
  830. 0x0, 0x7, // option length = 7
  831. 0x0, 0x3, // status code = 3
  832. 0x41, 0x42, 0x43, 0x44, 0x45 // string value ABCDE
  833. };
  834. // Create the option.
  835. OptionPtr status = srv->createStatusCode(3, "ABCDE");
  836. // Allocate an output buffer. We will store the option
  837. // in wire format here.
  838. OutputBuffer buf(sizeof(expected));
  839. // Prepare the wire format.
  840. ASSERT_NO_THROW(status->pack(buf));
  841. // Check that the option buffer has valid length (option header + data).
  842. ASSERT_EQ(sizeof(expected), buf.getLength());
  843. // Verify the contents of the option.
  844. EXPECT_EQ(0, memcmp(expected, buf.getData(), sizeof(expected)));
  845. }
  846. // This test verifies if the sanityCheck() really checks options presence.
  847. TEST_F(Dhcpv6SrvTest, sanityCheck) {
  848. boost::scoped_ptr<NakedDhcpv6Srv> srv;
  849. ASSERT_NO_THROW( srv.reset(new NakedDhcpv6Srv(0)) );
  850. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  851. // check that the packets originating from local addresses can be
  852. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  853. // client-id is optional for information-request, so
  854. EXPECT_NO_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL));
  855. // empty packet, no client-id, no server-id
  856. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN),
  857. RFCViolation);
  858. // This doesn't make much sense, but let's check it for completeness
  859. EXPECT_NO_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::FORBIDDEN, Dhcpv6Srv::FORBIDDEN));
  860. OptionPtr clientid = generateClientId();
  861. pkt->addOption(clientid);
  862. // client-id is mandatory, server-id is forbidden (as in SOLICIT or REBIND)
  863. EXPECT_NO_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN));
  864. pkt->addOption(srv->getServerID());
  865. // both client-id and server-id are mandatory (as in REQUEST, RENEW, RELEASE, DECLINE)
  866. EXPECT_NO_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY));
  867. // sane section ends here, let's do some negative tests as well
  868. pkt->addOption(clientid);
  869. pkt->addOption(clientid);
  870. // with more than one client-id it should throw, no matter what
  871. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  872. RFCViolation);
  873. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  874. RFCViolation);
  875. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  876. RFCViolation);
  877. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  878. RFCViolation);
  879. pkt->delOption(D6O_CLIENTID);
  880. pkt->delOption(D6O_CLIENTID);
  881. // again we have only one client-id
  882. // let's try different type of insanity - several server-ids
  883. pkt->addOption(srv->getServerID());
  884. pkt->addOption(srv->getServerID());
  885. // with more than one server-id it should throw, no matter what
  886. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  887. RFCViolation);
  888. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  889. RFCViolation);
  890. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  891. RFCViolation);
  892. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  893. RFCViolation);
  894. }
  895. } // end of anonymous namespace