dhcp4_test_utils.h 17 KB

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