dhcp4_test_utils.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // Copyright (C) 2013-2015 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. /// @file dhcp4_test_utils.h
  15. ///
  16. /// @brief This file contains utility classes used for DHCPv4 server testing
  17. #ifndef DHCP4_TEST_UTILS_H
  18. #define DHCP4_TEST_UTILS_H
  19. #include <gtest/gtest.h>
  20. #include <dhcp/iface_mgr.h>
  21. #include <dhcp/option4_addrlst.h>
  22. #include <dhcp/pkt4.h>
  23. #include <dhcp/pkt_filter.h>
  24. #include <dhcp/pkt_filter_inet.h>
  25. #include <dhcpsrv/subnet.h>
  26. #include <dhcpsrv/lease.h>
  27. #include <dhcpsrv/lease_mgr_factory.h>
  28. #include <dhcp4/dhcp4_srv.h>
  29. #include <asiolink/io_address.h>
  30. #include <cc/command_interpreter.h>
  31. #include <list>
  32. #include <boost/shared_ptr.hpp>
  33. namespace isc {
  34. namespace dhcp {
  35. namespace test {
  36. /// @brief Dummy Packet Filtering class.
  37. ///
  38. /// This class reports capability to respond directly to the client which
  39. /// doesn't have address configured yet.
  40. ///
  41. /// All packet and socket handling functions do nothing because they are not
  42. /// used in unit tests.
  43. class PktFilterTest : public PktFilter {
  44. public:
  45. /// @brief Constructor.
  46. ///
  47. /// Sets the 'direct response' capability to true.
  48. PktFilterTest()
  49. : direct_resp_supported_(true) {
  50. }
  51. /// @brief Reports 'direct response' capability.
  52. ///
  53. /// @return always true.
  54. virtual bool isDirectResponseSupported() const {
  55. return (direct_resp_supported_);
  56. }
  57. /// Does nothing.
  58. virtual SocketInfo openSocket(Iface&,
  59. const isc::asiolink::IOAddress& addr,
  60. const uint16_t port, const bool, const bool) {
  61. return (SocketInfo(addr, port, 0));
  62. }
  63. /// Does nothing.
  64. virtual Pkt4Ptr receive(Iface&, const SocketInfo&) {
  65. return Pkt4Ptr();
  66. }
  67. /// Does nothing.
  68. virtual int send(const Iface&, uint16_t, const Pkt4Ptr&) {
  69. return (0);
  70. }
  71. /// @brief Holds a boolean value which indicates whether direct response
  72. /// capability is supported (true) or not (false).
  73. bool direct_resp_supported_;
  74. };
  75. typedef boost::shared_ptr<PktFilterTest> PktFilterTestPtr;
  76. /// Forward definition for Dhcp4Client defined in dhcp4_client.h
  77. /// dhcp4_client.h includes dhcp_test_utils.h (this file), so to avoid
  78. /// circular dependencies, we need a forward class declaration.
  79. class Dhcp4Client;
  80. /// @brief "Naked" DHCPv4 server, exposes internal fields
  81. class NakedDhcpv4Srv: public Dhcpv4Srv {
  82. public:
  83. /// @brief Constructor.
  84. ///
  85. /// This constructor disables default modes of operation used by the
  86. /// Dhcpv4Srv class:
  87. /// - Send/receive broadcast messages through sockets on interfaces
  88. /// which support broadcast traffic.
  89. /// - Direct DHCPv4 traffic - communication with clients which do not
  90. /// have IP address assigned yet.
  91. ///
  92. /// Enabling these modes requires root privileges so they must be
  93. /// disabled for unit testing.
  94. ///
  95. /// Note, that disabling broadcast options on sockets does not impact
  96. /// the operation of these tests because they use local loopback
  97. /// interface which doesn't have broadcast capability anyway. It rather
  98. /// prevents setting broadcast options on other (broadcast capable)
  99. /// sockets which are opened on other interfaces in Dhcpv4Srv constructor.
  100. ///
  101. /// The Direct DHCPv4 Traffic capability can be disabled here because
  102. /// it is tested with PktFilterLPFTest unittest. The tests which belong
  103. /// to PktFilterLPFTest can be enabled on demand when root privileges can
  104. /// be guaranteed.
  105. ///
  106. /// @param port port number to listen on; the default value 0 indicates
  107. /// that sockets should not be opened.
  108. NakedDhcpv4Srv(uint16_t port = 0)
  109. : Dhcpv4Srv(port, false, false) {
  110. // Create a default lease database backend.
  111. std::string dbconfig = "type=memfile universe=4 persist=false";
  112. isc::dhcp::LeaseMgrFactory::create(dbconfig);
  113. // Create fixed server id.
  114. server_id_.reset(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER,
  115. asiolink::IOAddress("192.0.3.1")));
  116. }
  117. /// @brief Returns fixed server identifier assigned to the naked server
  118. /// instance.
  119. OptionPtr getServerID() const {
  120. return (server_id_);
  121. }
  122. /// @brief fakes packet reception
  123. /// @param timeout ignored
  124. ///
  125. /// The method receives all packets queued in receive queue, one after
  126. /// another. Once the queue is empty, it initiates the shutdown procedure.
  127. ///
  128. /// See fake_received_ field for description
  129. virtual Pkt4Ptr receivePacket(int /*timeout*/) {
  130. // If there is anything prepared as fake incoming traffic, use it
  131. if (!fake_received_.empty()) {
  132. Pkt4Ptr pkt = fake_received_.front();
  133. fake_received_.pop_front();
  134. return (pkt);
  135. }
  136. // If not, just trigger shutdown and return immediately
  137. shutdown();
  138. return (Pkt4Ptr());
  139. }
  140. /// @brief fake packet sending
  141. ///
  142. /// Pretend to send a packet, but instead just store it in fake_send_ list
  143. /// where test can later inspect server's response.
  144. virtual void sendPacket(const Pkt4Ptr& pkt) {
  145. fake_sent_.push_back(pkt);
  146. }
  147. /// @brief adds a packet to fake receive queue
  148. ///
  149. /// See fake_received_ field for description
  150. void fakeReceive(const Pkt4Ptr& pkt) {
  151. fake_received_.push_back(pkt);
  152. }
  153. virtual ~NakedDhcpv4Srv() {
  154. }
  155. /// @brief Dummy server identifier option used by various tests.
  156. OptionPtr server_id_;
  157. /// @brief packets we pretend to receive
  158. ///
  159. /// Instead of setting up sockets on interfaces that change between OSes, it
  160. /// is much easier to fake packet reception. This is a list of packets that
  161. /// we pretend to have received. You can schedule new packets to be received
  162. /// using fakeReceive() and NakedDhcpv4Srv::receivePacket() methods.
  163. std::list<Pkt4Ptr> fake_received_;
  164. std::list<Pkt4Ptr> fake_sent_;
  165. using Dhcpv4Srv::adjustIfaceData;
  166. using Dhcpv4Srv::appendServerID;
  167. using Dhcpv4Srv::processDiscover;
  168. using Dhcpv4Srv::processRequest;
  169. using Dhcpv4Srv::processRelease;
  170. using Dhcpv4Srv::processDecline;
  171. using Dhcpv4Srv::processInform;
  172. using Dhcpv4Srv::processClientName;
  173. using Dhcpv4Srv::createNameChangeRequests;
  174. using Dhcpv4Srv::acceptServerId;
  175. using Dhcpv4Srv::sanityCheck;
  176. using Dhcpv4Srv::srvidToString;
  177. using Dhcpv4Srv::unpackOptions;
  178. using Dhcpv4Srv::classifyPacket;
  179. using Dhcpv4Srv::accept;
  180. using Dhcpv4Srv::acceptMessageType;
  181. using Dhcpv4Srv::selectSubnet;
  182. using Dhcpv4Srv::VENDOR_CLASS_PREFIX;
  183. using Dhcpv4Srv::shutdown_;
  184. using Dhcpv4Srv::alloc_engine_;
  185. };
  186. // We need to pass one reference to the Dhcp4Client, which is defined in
  187. // dhcp4_client.h. That header includes this file. To avoid circular
  188. // dependencies, we use forward declaration here.
  189. class Dhcp4Client;
  190. class Dhcpv4SrvTest : public ::testing::Test {
  191. public:
  192. enum ExpectedResult {
  193. SHOULD_PASS, // pass = accept decline, move lease to declined state.
  194. SHOULD_FAIL // fail = reject the decline
  195. };
  196. /// @brief Constructor
  197. ///
  198. /// Initializes common objects used in many tests.
  199. /// Also sets up initial configuration in CfgMgr.
  200. Dhcpv4SrvTest();
  201. /// @brief destructor
  202. virtual ~Dhcpv4SrvTest();
  203. /// @brief Add 'Parameter Request List' option to the packet.
  204. ///
  205. /// This function adds PRL option comprising the following option codes:
  206. /// - 5 - Name Server
  207. /// - 15 - Domain Name
  208. /// - 7 - Log Server
  209. /// - 8 - Quotes Server
  210. /// - 9 - LPR Server
  211. ///
  212. /// @param pkt packet to add PRL option to.
  213. void addPrlOption(Pkt4Ptr& pkt);
  214. /// @brief Configures options being requested in the PRL option.
  215. ///
  216. /// The lpr-servers option is NOT configured here although it is
  217. /// added to the 'Parameter Request List' option in the
  218. /// \ref addPrlOption. When requested option is not configured
  219. /// the server should not return it in its response. The goal
  220. /// of not configuring the requested option is to verify that
  221. /// the server will not return it.
  222. void configureRequestedOptions();
  223. /// @brief checks that the response matches request
  224. /// @param q query (client's message)
  225. /// @param a answer (server's message)
  226. void messageCheck(const Pkt4Ptr& q, const Pkt4Ptr& a);
  227. /// @brief Check that certain basic (always added when lease is acquired)
  228. /// are present in a message.
  229. ///
  230. /// @param pkt A message to be checked.
  231. /// @return Assertion result which indicates whether test passed or failed.
  232. ::testing::AssertionResult basicOptionsPresent(const Pkt4Ptr& pkt);
  233. /// @brief Check that certain basic (always added when lease is acquired)
  234. /// are not present.
  235. ///
  236. /// @param pkt A packet to be checked.
  237. /// @return Assertion result which indicates whether test passed or failed.
  238. ::testing::AssertionResult noBasicOptions(const Pkt4Ptr& pkt);
  239. /// @brief Check that certain requested options are present in the message.
  240. ///
  241. /// @param pkt A message to be checked.
  242. /// @return Assertion result which indicates whether test passed or failed.
  243. ::testing::AssertionResult requestedOptionsPresent(const Pkt4Ptr& pkt);
  244. /// @brief Check that certain options (requested with PRL option)
  245. /// are not present.
  246. ///
  247. /// @param pkt A packet to be checked.
  248. /// @return Assertion result which indicates whether test passed or failed.
  249. ::testing::AssertionResult noRequestedOptions(const Pkt4Ptr& pkt);
  250. /// @brief generates client-id option
  251. ///
  252. /// Generate client-id option of specified length
  253. /// Ids with different lengths are sufficient to generate
  254. /// unique ids. If more fine grained control is required,
  255. /// tests generate client-ids on their own.
  256. /// Sets client_id_ field.
  257. /// @param size size of the client-id to be generated
  258. OptionPtr generateClientId(size_t size = 4);
  259. /// @brief generate hardware address
  260. ///
  261. /// @param size size of the generated MAC address
  262. /// @param pointer to Hardware Address object
  263. HWAddrPtr generateHWAddr(size_t size = 6);
  264. /// Check that address was returned from proper range, that its lease
  265. /// lifetime is correct, that T1 and T2 are returned properly
  266. /// @param rsp response to be checked
  267. /// @param subnet subnet that should be used to verify assigned address
  268. /// and options
  269. /// @param t1_present check that t1 must be present (true) or must not be
  270. /// present (false)
  271. /// @param t2_present check that t2 must be present (true) or must not be
  272. /// present (false)
  273. void checkAddressParams(const Pkt4Ptr& rsp, const SubnetPtr subnet,
  274. bool t1_present = false,
  275. bool t2_present = false);
  276. /// @brief Basic checks for generated response (message type and trans-id).
  277. ///
  278. /// @param rsp response packet to be validated
  279. /// @param expected_message_type expected message type
  280. /// @param expected_transid expected transaction-id
  281. void checkResponse(const Pkt4Ptr& rsp, int expected_message_type,
  282. uint32_t expected_transid);
  283. /// @brief Checks if the lease sent to client is present in the database
  284. ///
  285. /// @param rsp response packet to be validated
  286. /// @param client_id expected client-identifier (or NULL)
  287. /// @param HWAddr expected hardware address (not used now)
  288. /// @param expected_addr expected address
  289. Lease4Ptr checkLease(const Pkt4Ptr& rsp, const OptionPtr& client_id,
  290. const HWAddrPtr&,
  291. const isc::asiolink::IOAddress& expected_addr);
  292. /// @brief Checks if server response (OFFER, ACK, NAK) includes proper server-id
  293. /// @param rsp response packet to be validated
  294. /// @param expected_srvid expected value of server-id
  295. void checkServerId(const Pkt4Ptr& rsp, const OptionPtr& expected_srvid);
  296. /// @brief Checks if server response (OFFER, ACK, NAK) includes proper client-id
  297. ///
  298. /// This method follows values reported by CfgMgr in echoClientId() method.
  299. /// Depending on its configuration, the client-id is either mandatory or
  300. /// forbidden to appear in the response.
  301. ///
  302. /// @param rsp response packet to be validated
  303. /// @param expected_clientid expected value of client-id
  304. void checkClientId(const Pkt4Ptr& rsp, const OptionPtr& expected_clientid);
  305. /// @brief Create packet from output buffer of another packet.
  306. ///
  307. /// This function creates a packet using an output buffer from another
  308. /// packet. This imitates reception of a packet from the wire. The
  309. /// unpack function is then called to parse packet contents and to
  310. /// create a collection of the options carried by this packet.
  311. ///
  312. /// This function is useful for unit tests which verify that the received
  313. /// packet is parsed correctly. In those cases it is usually inappropriate
  314. /// to create an instance of the packet, add options, set packet
  315. /// fields and use such packet as an input to processDiscover or
  316. /// processRequest function. This is because, such a packet has certain
  317. /// options already initialized and there is no way to verify that they
  318. /// have been initialized when packet instance was created or wire buffer
  319. /// processing. By creating a packet from the buffer we guarantee that the
  320. /// new packet is entirely initialized during wire data parsing.
  321. ///
  322. /// @param src_pkt A source packet, to be copied.
  323. /// @param [out] dst_pkt A destination packet.
  324. ///
  325. /// @return assertion result indicating if a function completed with
  326. /// success or failure.
  327. static ::testing::AssertionResult
  328. createPacketFromBuffer(const isc::dhcp::Pkt4Ptr& src_pkt,
  329. isc::dhcp::Pkt4Ptr& dst_pkt);
  330. /// @brief Tests if Discover or Request message is processed correctly
  331. ///
  332. /// This test verifies that the Parameter Request List option is handled
  333. /// correctly, i.e. it checks that certain options are present in the
  334. /// server's response when they are requested and that they are not present
  335. /// when they are not requested or NAK occurs.
  336. ///
  337. /// @todo We need an additional test for PRL option using real traffic
  338. /// capture.
  339. ///
  340. /// @param msg_type DHCPDISCOVER or DHCPREQUEST
  341. void testDiscoverRequest(const uint8_t msg_type);
  342. /// @brief Runs DHCPv4 configuration from the JSON string.
  343. ///
  344. /// @param config String holding server configuration in JSON format.
  345. /// @param commit A boolean flag indicating if the new configuration
  346. /// should be committed (if true), or not (if false).
  347. void configure(const std::string& config, const bool commit = true);
  348. /// @brief Configure specified DHCP server using JSON string.
  349. ///
  350. /// @param config String holding server configuration in JSON format.
  351. /// @param srv Instance of the server to be configured.
  352. /// @param commit A boolean flag indicating if the new configuration
  353. /// should be committed (if true), or not (if false).
  354. void configure(const std::string& config, NakedDhcpv4Srv& srv,
  355. const bool commit = true);
  356. /// @brief Pretents a packet of specified type was received.
  357. ///
  358. /// Instantiates fake network interfaces, configures passed Dhcpv4Srv,
  359. /// then creates a message of specified type and sends it to the
  360. /// server and then checks whether expected statstics were set
  361. /// appropriately.
  362. ///
  363. /// @param srv the DHCPv4 server to be used
  364. /// @param config JSON configuration to be used
  365. /// @param pkt_type type of the packet to be faked
  366. /// @param stat_name name of the expected statistic
  367. void pretendReceivingPkt(NakedDhcpv4Srv& srv, const std::string& config,
  368. uint8_t pkt_type, const std::string& stat_name);
  369. /// @brief Create @c Dhcpv4Exchange from client's query.
  370. Dhcpv4Exchange createExchange(const Pkt4Ptr& query);
  371. /// @brief Performs 4-way exchange to obtain new lease.
  372. ///
  373. /// This is used as a preparatory step for Decline operation.
  374. ///
  375. /// @param client Client to be used to obtain a lease.
  376. void acquireLease(Dhcp4Client& client);
  377. /// @brief Tests if the acquired lease is or is not declined.
  378. ///
  379. /// @param client Dhcp4Client instance
  380. /// @param hw_address_1 HW Address to be used to acquire the lease.
  381. /// @param client_id_1 Client id to be used to acquire the lease.
  382. /// @param hw_address_2 HW Address to be used to decline the lease.
  383. /// @param client_id_2 Client id to be used to decline the lease.
  384. /// @param expected_result SHOULD_PASS if the lease is expected to
  385. /// be successfully declined, or SHOULD_FAIL if the lease is expected
  386. /// to not be declined.
  387. void acquireAndDecline(Dhcp4Client& client,
  388. const std::string& hw_address_1,
  389. const std::string& client_id_1,
  390. const std::string& hw_address_2,
  391. const std::string& client_id_2,
  392. ExpectedResult expected_result);
  393. /// @brief This function cleans up after the test.
  394. virtual void TearDown();
  395. /// @brief A subnet used in most tests
  396. Subnet4Ptr subnet_;
  397. /// @brief A pool used in most tests
  398. Pool4Ptr pool_;
  399. /// @brief A client-id used in most tests
  400. ClientIdPtr client_id_;
  401. int rcode_;
  402. isc::data::ConstElementPtr comment_;
  403. /// @brief Server object under test.
  404. NakedDhcpv4Srv srv_;
  405. };
  406. }; // end of isc::dhcp::test namespace
  407. }; // end of isc::dhcp namespace
  408. }; // end of isc namespace
  409. #endif // DHCP4_TEST_UTILS_H