dhcp4_test_utils.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <config/ccsession.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. /// @brief "Naked" DHCPv4 server, exposes internal fields
  77. class NakedDhcpv4Srv: public Dhcpv4Srv {
  78. public:
  79. /// @brief Constructor.
  80. ///
  81. /// This constructor disables default modes of operation used by the
  82. /// Dhcpv4Srv class:
  83. /// - Send/receive broadcast messages through sockets on interfaces
  84. /// which support broadcast traffic.
  85. /// - Direct DHCPv4 traffic - communication with clients which do not
  86. /// have IP address assigned yet.
  87. ///
  88. /// Enabling these modes requires root privileges so they must be
  89. /// disabled for unit testing.
  90. ///
  91. /// Note, that disabling broadcast options on sockets does not impact
  92. /// the operation of these tests because they use local loopback
  93. /// interface which doesn't have broadcast capability anyway. It rather
  94. /// prevents setting broadcast options on other (broadcast capable)
  95. /// sockets which are opened on other interfaces in Dhcpv4Srv constructor.
  96. ///
  97. /// The Direct DHCPv4 Traffic capability can be disabled here because
  98. /// it is tested with PktFilterLPFTest unittest. The tests which belong
  99. /// to PktFilterLPFTest can be enabled on demand when root privileges can
  100. /// be guaranteed.
  101. ///
  102. /// @param port port number to listen on; the default value 0 indicates
  103. /// that sockets should not be opened.
  104. NakedDhcpv4Srv(uint16_t port = 0)
  105. : Dhcpv4Srv(port, false, false) {
  106. // Create a default lease database backend.
  107. std::string dbconfig = "type=memfile universe=4 persist=false";
  108. isc::dhcp::LeaseMgrFactory::create(dbconfig);
  109. // Create fixed server id.
  110. server_id_.reset(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER,
  111. asiolink::IOAddress("192.0.3.1")));
  112. }
  113. /// @brief Returns fixed server identifier assigned to the naked server
  114. /// instance.
  115. OptionPtr getServerID() const {
  116. return (server_id_);
  117. }
  118. /// @brief fakes packet reception
  119. /// @param timeout ignored
  120. ///
  121. /// The method receives all packets queued in receive queue, one after
  122. /// another. Once the queue is empty, it initiates the shutdown procedure.
  123. ///
  124. /// See fake_received_ field for description
  125. virtual Pkt4Ptr receivePacket(int /*timeout*/) {
  126. // If there is anything prepared as fake incoming traffic, use it
  127. if (!fake_received_.empty()) {
  128. Pkt4Ptr pkt = fake_received_.front();
  129. fake_received_.pop_front();
  130. return (pkt);
  131. }
  132. // If not, just trigger shutdown and return immediately
  133. shutdown();
  134. return (Pkt4Ptr());
  135. }
  136. /// @brief fake packet sending
  137. ///
  138. /// Pretend to send a packet, but instead just store it in fake_send_ list
  139. /// where test can later inspect server's response.
  140. virtual void sendPacket(const Pkt4Ptr& pkt) {
  141. fake_sent_.push_back(pkt);
  142. }
  143. /// @brief adds a packet to fake receive queue
  144. ///
  145. /// See fake_received_ field for description
  146. void fakeReceive(const Pkt4Ptr& pkt) {
  147. fake_received_.push_back(pkt);
  148. }
  149. virtual ~NakedDhcpv4Srv() {
  150. }
  151. /// @brief Dummy server identifier option used by various tests.
  152. OptionPtr server_id_;
  153. /// @brief packets we pretend to receive
  154. ///
  155. /// Instead of setting up sockets on interfaces that change between OSes, it
  156. /// is much easier to fake packet reception. This is a list of packets that
  157. /// we pretend to have received. You can schedule new packets to be received
  158. /// using fakeReceive() and NakedDhcpv4Srv::receivePacket() methods.
  159. std::list<Pkt4Ptr> fake_received_;
  160. std::list<Pkt4Ptr> fake_sent_;
  161. using Dhcpv4Srv::adjustIfaceData;
  162. using Dhcpv4Srv::appendServerID;
  163. using Dhcpv4Srv::processDiscover;
  164. using Dhcpv4Srv::processRequest;
  165. using Dhcpv4Srv::processRelease;
  166. using Dhcpv4Srv::processDecline;
  167. using Dhcpv4Srv::processInform;
  168. using Dhcpv4Srv::processClientName;
  169. using Dhcpv4Srv::computeDhcid;
  170. using Dhcpv4Srv::createNameChangeRequests;
  171. using Dhcpv4Srv::acceptServerId;
  172. using Dhcpv4Srv::sanityCheck;
  173. using Dhcpv4Srv::srvidToString;
  174. using Dhcpv4Srv::unpackOptions;
  175. using Dhcpv4Srv::classifyPacket;
  176. using Dhcpv4Srv::accept;
  177. using Dhcpv4Srv::acceptMessageType;
  178. using Dhcpv4Srv::selectSubnet;
  179. using Dhcpv4Srv::VENDOR_CLASS_PREFIX;
  180. using Dhcpv4Srv::shutdown_;
  181. using Dhcpv4Srv::alloc_engine_;
  182. };
  183. class Dhcpv4SrvTest : public ::testing::Test {
  184. public:
  185. /// @brief Constructor
  186. ///
  187. /// Initializes common objects used in many tests.
  188. /// Also sets up initial configuration in CfgMgr.
  189. Dhcpv4SrvTest();
  190. /// @brief destructor
  191. virtual ~Dhcpv4SrvTest();
  192. /// @brief Add 'Parameter Request List' option to the packet.
  193. ///
  194. /// This function adds PRL option comprising the following option codes:
  195. /// - 5 - Name Server
  196. /// - 15 - Domain Name
  197. /// - 7 - Log Server
  198. /// - 8 - Quotes Server
  199. /// - 9 - LPR Server
  200. ///
  201. /// @param pkt packet to add PRL option to.
  202. void addPrlOption(Pkt4Ptr& pkt);
  203. /// @brief Configures options being requested in the PRL option.
  204. ///
  205. /// The lpr-servers option is NOT configured here although it is
  206. /// added to the 'Parameter Request List' option in the
  207. /// \ref addPrlOption. When requested option is not configured
  208. /// the server should not return it in its response. The goal
  209. /// of not configuring the requested option is to verify that
  210. /// the server will not return it.
  211. void configureRequestedOptions();
  212. /// @brief checks that the response matches request
  213. /// @param q query (client's message)
  214. /// @param a answer (server's message)
  215. void messageCheck(const Pkt4Ptr& q, const Pkt4Ptr& a);
  216. /// @brief Check that certain basic (always added when lease is acquired)
  217. /// are present in a message.
  218. ///
  219. /// @param pkt A message to be checked.
  220. /// @return Assertion result which indicates whether test passed or failed.
  221. ::testing::AssertionResult basicOptionsPresent(const Pkt4Ptr& pkt);
  222. /// @brief Check that certain basic (always added when lease is acquired)
  223. /// are not present.
  224. ///
  225. /// @param pkt A packet to be checked.
  226. /// @return Assertion result which indicates whether test passed or failed.
  227. ::testing::AssertionResult noBasicOptions(const Pkt4Ptr& pkt);
  228. /// @brief Check that certain requested options are present in the message.
  229. ///
  230. /// @param pkt A message to be checked.
  231. /// @return Assertion result which indicates whether test passed or failed.
  232. ::testing::AssertionResult requestedOptionsPresent(const Pkt4Ptr& pkt);
  233. /// @brief Check that certain options (requested with PRL option)
  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 noRequestedOptions(const Pkt4Ptr& pkt);
  239. /// @brief generates client-id option
  240. ///
  241. /// Generate client-id option of specified length
  242. /// Ids with different lengths are sufficient to generate
  243. /// unique ids. If more fine grained control is required,
  244. /// tests generate client-ids on their own.
  245. /// Sets client_id_ field.
  246. /// @param size size of the client-id to be generated
  247. OptionPtr generateClientId(size_t size = 4);
  248. /// @brief generate hardware address
  249. ///
  250. /// @param size size of the generated MAC address
  251. /// @param pointer to Hardware Address object
  252. HWAddrPtr generateHWAddr(size_t size = 6);
  253. /// Check that address was returned from proper range, that its lease
  254. /// lifetime is correct, that T1 and T2 are returned properly
  255. /// @param rsp response to be checked
  256. /// @param subnet subnet that should be used to verify assigned address
  257. /// and options
  258. /// @param t1_present check that t1 must be present (true) or must not be
  259. /// present (false)
  260. /// @param t2_present check that t2 must be present (true) or must not be
  261. /// present (false)
  262. void checkAddressParams(const Pkt4Ptr& rsp, const SubnetPtr subnet,
  263. bool t1_present = false,
  264. bool t2_present = false);
  265. /// @brief Basic checks for generated response (message type and trans-id).
  266. ///
  267. /// @param rsp response packet to be validated
  268. /// @param expected_message_type expected message type
  269. /// @param expected_transid expected transaction-id
  270. void checkResponse(const Pkt4Ptr& rsp, int expected_message_type,
  271. uint32_t expected_transid);
  272. /// @brief Checks if the lease sent to client is present in the database
  273. ///
  274. /// @param rsp response packet to be validated
  275. /// @param client_id expected client-identifier (or NULL)
  276. /// @param HWAddr expected hardware address (not used now)
  277. /// @param expected_addr expected address
  278. Lease4Ptr checkLease(const Pkt4Ptr& rsp, const OptionPtr& client_id,
  279. const HWAddrPtr&,
  280. const isc::asiolink::IOAddress& expected_addr);
  281. /// @brief Checks if server response (OFFER, ACK, NAK) includes proper server-id
  282. /// @param rsp response packet to be validated
  283. /// @param expected_srvid expected value of server-id
  284. void checkServerId(const Pkt4Ptr& rsp, const OptionPtr& expected_srvid);
  285. /// @brief Checks if server response (OFFER, ACK, NAK) includes proper client-id
  286. ///
  287. /// This method follows values reported by CfgMgr in echoClientId() method.
  288. /// Depending on its configuration, the client-id is either mandatory or
  289. /// forbidden to appear in the response.
  290. ///
  291. /// @param rsp response packet to be validated
  292. /// @param expected_clientid expected value of client-id
  293. void checkClientId(const Pkt4Ptr& rsp, const OptionPtr& expected_clientid);
  294. /// @brief Create packet from output buffer of another packet.
  295. ///
  296. /// This function creates a packet using an output buffer from another
  297. /// packet. This imitates reception of a packet from the wire. The
  298. /// unpack function is then called to parse packet contents and to
  299. /// create a collection of the options carried by this packet.
  300. ///
  301. /// This function is useful for unit tests which verify that the received
  302. /// packet is parsed correctly. In those cases it is usually inappropriate
  303. /// to create an instance of the packet, add options, set packet
  304. /// fields and use such packet as an input to processDiscover or
  305. /// processRequest function. This is because, such a packet has certain
  306. /// options already initialized and there is no way to verify that they
  307. /// have been initialized when packet instance was created or wire buffer
  308. /// processing. By creating a packet from the buffer we guarantee that the
  309. /// new packet is entirely initialized during wire data parsing.
  310. ///
  311. /// @param src_pkt A source packet, to be copied.
  312. /// @param [out] dst_pkt A destination packet.
  313. ///
  314. /// @return assertion result indicating if a function completed with
  315. /// success or failure.
  316. static ::testing::AssertionResult
  317. createPacketFromBuffer(const isc::dhcp::Pkt4Ptr& src_pkt,
  318. isc::dhcp::Pkt4Ptr& dst_pkt);
  319. /// @brief Tests if Discover or Request message is processed correctly
  320. ///
  321. /// This test verifies that the Parameter Request List option is handled
  322. /// correctly, i.e. it checks that certain options are present in the
  323. /// server's response when they are requested and that they are not present
  324. /// when they are not requested or NAK occurs.
  325. ///
  326. /// @todo We need an additional test for PRL option using real traffic
  327. /// capture.
  328. ///
  329. /// @param msg_type DHCPDISCOVER or DHCPREQUEST
  330. void testDiscoverRequest(const uint8_t msg_type);
  331. /// @brief Runs DHCPv4 configuration from the JSON string.
  332. ///
  333. /// @param config String holding server configuration in JSON format.
  334. /// @param commit A boolean flag indicating if the new configuration
  335. /// should be committed (if true), or not (if false).
  336. void configure(const std::string& config, const bool commit = true);
  337. /// @brief Configure specified DHCP server using JSON string.
  338. ///
  339. /// @param config String holding server configuration in JSON format.
  340. /// @param srv Instance of the server to be configured.
  341. /// @param commit A boolean flag indicating if the new configuration
  342. /// should be committed (if true), or not (if false).
  343. void configure(const std::string& config, NakedDhcpv4Srv& srv,
  344. const bool commit = true);
  345. /// @brief Create @c Dhcpv4Exchange from client's query.
  346. Dhcpv4Exchange createExchange(const Pkt4Ptr& query);
  347. /// @brief This function cleans up after the test.
  348. virtual void TearDown();
  349. /// @brief A subnet used in most tests
  350. Subnet4Ptr subnet_;
  351. /// @brief A pool used in most tests
  352. Pool4Ptr pool_;
  353. /// @brief A client-id used in most tests
  354. ClientIdPtr client_id_;
  355. int rcode_;
  356. isc::data::ConstElementPtr comment_;
  357. /// @brief Server object under test.
  358. NakedDhcpv4Srv srv_;
  359. };
  360. }; // end of isc::dhcp::test namespace
  361. }; // end of isc::dhcp namespace
  362. }; // end of isc namespace
  363. #endif // DHCP4_TEST_UTILS_H