client_class_def_unittest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <dhcpsrv/client_class_def.h>
  8. #include <dhcpsrv/cfgmgr.h>
  9. #include <dhcp/option_space.h>
  10. #include <testutils/test_to_element.h>
  11. #include <exceptions/exceptions.h>
  12. #include <boost/scoped_ptr.hpp>
  13. #include <asiolink/io_address.h>
  14. #include <gtest/gtest.h>
  15. /// @file client_class_def_unittest.cc Unit tests for client class storage
  16. /// classes.
  17. using namespace std;
  18. using namespace isc::dhcp;
  19. using namespace isc::util;
  20. using namespace isc::asiolink;
  21. using namespace isc::test;
  22. using namespace isc;
  23. namespace {
  24. // Tests basic construction of ClientClassDef
  25. TEST(ClientClassDef, construction) {
  26. boost::scoped_ptr<ClientClassDef> cclass;
  27. std::string name = "class1";
  28. ExpressionPtr expr;
  29. CfgOptionPtr cfg_option;
  30. // Classes cannot have blank names
  31. ASSERT_THROW(cclass.reset(new ClientClassDef("", expr, cfg_option)),
  32. BadValue);
  33. // Verify we can create a class with a name, expression, and no cfg_option
  34. ASSERT_NO_THROW(cclass.reset(new ClientClassDef(name, expr)));
  35. EXPECT_EQ(name, cclass->getName());
  36. ASSERT_FALSE(cclass->getMatchExpr());
  37. // Verify we get an empty collection of cfg_option
  38. cfg_option = cclass->getCfgOption();
  39. ASSERT_TRUE(cfg_option);
  40. EXPECT_TRUE(cfg_option->empty());
  41. }
  42. // Tests options operations. Note we just do the basics
  43. // as CfgOption is heavily tested elsewhere.
  44. TEST(ClientClassDef, cfgOptionBasics) {
  45. boost::scoped_ptr<ClientClassDef> cclass;
  46. std::string name = "class1";
  47. ExpressionPtr expr;
  48. CfgOptionPtr test_options;
  49. CfgOptionPtr class_options;
  50. OptionPtr opt;
  51. // First construct the class with empty option pointer
  52. ASSERT_NO_THROW(cclass.reset(new ClientClassDef(name, expr, test_options)));
  53. // We should get back a collection with no entries,
  54. // not an empty collection pointer
  55. class_options = cclass->getCfgOption();
  56. ASSERT_TRUE(class_options);
  57. // Create an option container and add some options
  58. OptionPtr option;
  59. test_options.reset(new CfgOption());
  60. option.reset(new Option(Option::V4, 17, OptionBuffer(10, 0xFF)));
  61. ASSERT_NO_THROW(test_options->add(option, false, DHCP4_OPTION_SPACE));
  62. option.reset(new Option(Option::V6, 101, OptionBuffer(10, 0xFF)));
  63. ASSERT_NO_THROW(test_options->add(option, false, "isc"));
  64. option.reset(new Option(Option::V6, 100, OptionBuffer(10, 0xFF)));
  65. ASSERT_NO_THROW(test_options->add(option, false, DHCP6_OPTION_SPACE));
  66. // Now remake the client class with cfg_option
  67. ASSERT_NO_THROW(cclass.reset(new ClientClassDef(name, expr, test_options)));
  68. class_options = cclass->getCfgOption();
  69. ASSERT_TRUE(class_options);
  70. // Now make sure we can find all the options
  71. OptionDescriptor opt_desc = class_options->get(DHCP4_OPTION_SPACE,17);
  72. ASSERT_TRUE(opt_desc.option_);
  73. EXPECT_EQ(17, opt_desc.option_->getType());
  74. opt_desc = class_options->get("isc",101);
  75. ASSERT_TRUE(opt_desc.option_);
  76. EXPECT_EQ(101, opt_desc.option_->getType());
  77. opt_desc = class_options->get(DHCP6_OPTION_SPACE,100);
  78. ASSERT_TRUE(opt_desc.option_);
  79. EXPECT_EQ(100, opt_desc.option_->getType());
  80. }
  81. // Verifies copy constructor and equality tools (methods/operators)
  82. TEST(ClientClassDef, copyAndEquality) {
  83. boost::scoped_ptr<ClientClassDef> cclass;
  84. ExpressionPtr expr;
  85. CfgOptionPtr test_options;
  86. OptionPtr opt;
  87. // Make an expression
  88. expr.reset(new Expression());
  89. TokenPtr token(new TokenString("boo"));
  90. expr->push_back(token);
  91. // Create an option container with an option
  92. OptionPtr option;
  93. test_options.reset(new CfgOption());
  94. option.reset(new Option(Option::V4, 17, OptionBuffer(10, 0xFF)));
  95. ASSERT_NO_THROW(test_options->add(option, false, DHCP4_OPTION_SPACE));
  96. // Now remake the client class with cfg_option
  97. ASSERT_NO_THROW(cclass.reset(new ClientClassDef("class_one", expr,
  98. test_options)));
  99. // Now lets make a copy of it.
  100. boost::scoped_ptr<ClientClassDef> cclass2;
  101. ASSERT_NO_THROW(cclass2.reset(new ClientClassDef(*cclass)));
  102. // The allocated Expression pointers should not match
  103. EXPECT_TRUE(cclass->getMatchExpr().get() !=
  104. cclass2->getMatchExpr().get());
  105. // The allocated CfgOption pointers should not match
  106. EXPECT_TRUE(cclass->getCfgOption().get() !=
  107. cclass2->getCfgOption().get());
  108. // Verify the equality tools reflect that the classes are equal.
  109. EXPECT_TRUE(cclass->equals(*cclass2));
  110. EXPECT_TRUE(*cclass == *cclass2);
  111. EXPECT_FALSE(*cclass != *cclass2);
  112. // Make a class that differs from the first class only by name and
  113. // verify that the equality tools reflect that the classes are not equal.
  114. ASSERT_NO_THROW(cclass2.reset(new ClientClassDef("class_two", expr,
  115. test_options)));
  116. EXPECT_FALSE(cclass->equals(*cclass2));
  117. EXPECT_FALSE(*cclass == *cclass2);
  118. EXPECT_TRUE(*cclass != *cclass2);
  119. // Make a class with the same name and options, but no expression
  120. // verify that the equality tools reflect that the classes are not equal.
  121. expr.reset();
  122. ASSERT_NO_THROW(cclass2.reset(new ClientClassDef("class_one", expr,
  123. test_options)));
  124. EXPECT_FALSE(cclass->equals(*cclass2));
  125. EXPECT_FALSE(*cclass == *cclass2);
  126. EXPECT_TRUE(*cclass != *cclass2);
  127. // Make a class with the same name and options, but different expression,
  128. // verify that the equality tools reflect that the classes are not equal.
  129. expr.reset(new Expression());
  130. token.reset(new TokenString("yah"));
  131. expr->push_back(token);
  132. ASSERT_NO_THROW(cclass2.reset(new ClientClassDef("class_one", expr,
  133. test_options)));
  134. EXPECT_FALSE(cclass->equals(*cclass2));
  135. EXPECT_FALSE(*cclass == *cclass2);
  136. EXPECT_TRUE(*cclass != *cclass2);
  137. // Make a class with same name and expression, but no options
  138. // verify that the equality tools reflect that the classes are not equal.
  139. test_options.reset(new CfgOption());
  140. ASSERT_NO_THROW(cclass2.reset(new ClientClassDef("class_one", expr,
  141. test_options)));
  142. EXPECT_FALSE(cclass->equals(*cclass2));
  143. EXPECT_FALSE(*cclass == *cclass2);
  144. EXPECT_TRUE(*cclass != *cclass2);
  145. // Make a class that with same name and expression, but different options
  146. // verify that the equality tools reflect that the classes are not equal.
  147. option.reset(new Option(Option::V4, 20, OptionBuffer(10, 0xFF)));
  148. ASSERT_NO_THROW(test_options->add(option, false, DHCP4_OPTION_SPACE));
  149. ASSERT_NO_THROW(cclass2.reset(new ClientClassDef("class_one", expr,
  150. test_options)));
  151. EXPECT_FALSE(cclass->equals(*cclass2));
  152. EXPECT_FALSE(*cclass == *cclass2);
  153. EXPECT_TRUE(*cclass != *cclass2);
  154. }
  155. // Tests the basic operation of ClientClassDictionary
  156. // This includes adding, finding, and removing classes
  157. TEST(ClientClassDictionary, basics) {
  158. ClientClassDictionaryPtr dictionary;
  159. ClientClassDefPtr cclass;
  160. ExpressionPtr expr;
  161. CfgOptionPtr cfg_option;
  162. // Verify constructor doesn't throw
  163. ASSERT_NO_THROW(dictionary.reset(new ClientClassDictionary()));
  164. // Verify we can fetch a pointer the map of classes and
  165. // that we start with no classes defined
  166. const ClientClassDefMapPtr classes = dictionary->getClasses();
  167. ASSERT_TRUE(classes);
  168. EXPECT_EQ(0, classes->size());
  169. // Verify that we can add classes with both addClass variants
  170. // First addClass(name, expression, cfg_option)
  171. ASSERT_NO_THROW(dictionary->addClass("cc1", expr, "", cfg_option));
  172. ASSERT_NO_THROW(dictionary->addClass("cc2", expr, "", cfg_option));
  173. // Verify duplicate add attempt throws
  174. ASSERT_THROW(dictionary->addClass("cc2", expr, "", cfg_option),
  175. DuplicateClientClassDef);
  176. // Verify that you cannot add a class with no name.
  177. ASSERT_THROW(dictionary->addClass("", expr, "", cfg_option), BadValue);
  178. // Now with addClass(class pointer)
  179. ASSERT_NO_THROW(cclass.reset(new ClientClassDef("cc3", expr, cfg_option)));
  180. ASSERT_NO_THROW(dictionary->addClass(cclass));
  181. // Verify duplicate add attempt throws
  182. ASSERT_THROW(dictionary->addClass(cclass), DuplicateClientClassDef);
  183. // Verify that you cannot add empty class pointer
  184. cclass.reset();
  185. ASSERT_THROW(dictionary->addClass(cclass), BadValue);
  186. // Map should show 3 entries.
  187. EXPECT_EQ(3, classes->size());
  188. // Verify we can find them all.
  189. ASSERT_NO_THROW(cclass = dictionary->findClass("cc1"));
  190. ASSERT_TRUE(cclass);
  191. EXPECT_EQ("cc1", cclass->getName());
  192. ASSERT_NO_THROW(cclass = dictionary->findClass("cc2"));
  193. ASSERT_TRUE(cclass);
  194. EXPECT_EQ("cc2", cclass->getName());
  195. ASSERT_NO_THROW(cclass = dictionary->findClass("cc3"));
  196. ASSERT_TRUE(cclass);
  197. EXPECT_EQ("cc3", cclass->getName());
  198. // Verify the looking for non-existing returns empty pointer
  199. ASSERT_NO_THROW(cclass = dictionary->findClass("bogus"));
  200. EXPECT_FALSE(cclass);
  201. // Verify that we can remove a class
  202. ASSERT_NO_THROW(dictionary->removeClass("cc3"));
  203. EXPECT_EQ(2, classes->size());
  204. // Shouldn't be able to find anymore
  205. ASSERT_NO_THROW(cclass = dictionary->findClass("cc3"));
  206. EXPECT_FALSE(cclass);
  207. // Verify that we can attempt to remove a non-existing class
  208. // without harm.
  209. ASSERT_NO_THROW(dictionary->removeClass("cc3"));
  210. EXPECT_EQ(2, classes->size());
  211. }
  212. // Verifies copy constructor and equality tools (methods/operators)
  213. TEST(ClientClassDictionary, copyAndEquality) {
  214. ClientClassDictionaryPtr dictionary;
  215. ClientClassDictionaryPtr dictionary2;
  216. ClientClassDefPtr cclass;
  217. ExpressionPtr expr;
  218. CfgOptionPtr options;
  219. dictionary.reset(new ClientClassDictionary());
  220. ASSERT_NO_THROW(dictionary->addClass("one", expr, "", options));
  221. ASSERT_NO_THROW(dictionary->addClass("two", expr, "", options));
  222. ASSERT_NO_THROW(dictionary->addClass("three", expr, "", options));
  223. // Copy constructor should succeed.
  224. ASSERT_NO_THROW(dictionary2.reset(new ClientClassDictionary(*dictionary)));
  225. // Allocated class map pointers should not be equal
  226. EXPECT_NE(dictionary->getClasses().get(), dictionary2->getClasses().get());
  227. // Equality tools should reflect that the dictionaries are equal.
  228. EXPECT_TRUE(dictionary->equals(*dictionary2));
  229. EXPECT_TRUE(*dictionary == *dictionary2);
  230. EXPECT_FALSE(*dictionary != *dictionary2);
  231. // Remove a class from dictionary2.
  232. ASSERT_NO_THROW(dictionary2->removeClass("two"));
  233. // Equality tools should reflect that the dictionaries are not equal.
  234. EXPECT_FALSE(dictionary->equals(*dictionary2));
  235. EXPECT_FALSE(*dictionary == *dictionary2);
  236. EXPECT_TRUE(*dictionary != *dictionary2);
  237. // Create an empty dictionary.
  238. dictionary2.reset(new ClientClassDictionary());
  239. // Equality tools should reflect that the dictionaries are not equal.
  240. EXPECT_FALSE(dictionary->equals(*dictionary2));
  241. EXPECT_FALSE(*dictionary == *dictionary2);
  242. EXPECT_TRUE(*dictionary != *dictionary2);
  243. }
  244. // Tests the default constructor regarding fixed fields
  245. TEST(ClientClassDef, fixedFieldsDefaults) {
  246. boost::scoped_ptr<ClientClassDef> cclass;
  247. std::string name = "class1";
  248. ExpressionPtr expr;
  249. CfgOptionPtr cfg_option;
  250. // Classes cannot have blank names
  251. ASSERT_THROW(cclass.reset(new ClientClassDef("", expr, cfg_option)),
  252. BadValue);
  253. // Verify we can create a class with a name, expression, and no cfg_option
  254. ASSERT_NO_THROW(cclass.reset(new ClientClassDef(name, expr)));
  255. // Let's checks that it doesn't return any nonsense
  256. string empty;
  257. ASSERT_EQ(IOAddress("0.0.0.0"), cclass->getNextServer());
  258. EXPECT_EQ(empty, cclass->getSname());
  259. EXPECT_EQ(empty, cclass->getFilename());
  260. }
  261. // Tests basic operations of fixed fields
  262. TEST(ClientClassDef, fixedFieldsBasics) {
  263. boost::scoped_ptr<ClientClassDef> cclass;
  264. std::string name = "class1";
  265. ExpressionPtr expr;
  266. CfgOptionPtr cfg_option;
  267. // Classes cannot have blank names
  268. ASSERT_THROW(cclass.reset(new ClientClassDef("", expr, cfg_option)),
  269. BadValue);
  270. // Verify we can create a class with a name, expression, and no cfg_option
  271. ASSERT_NO_THROW(cclass.reset(new ClientClassDef(name, expr)));
  272. string sname = "This is a very long string that can be a server name";
  273. string filename = "this-is-a-slightly-longish-name-of-a-file.txt";
  274. cclass->setNextServer(IOAddress("1.2.3.4"));
  275. cclass->setSname(sname);
  276. cclass->setFilename(filename);
  277. // Let's checks that it doesn't return any nonsense
  278. ASSERT_EQ(IOAddress("1.2.3.4"), cclass->getNextServer());
  279. EXPECT_EQ(sname, cclass->getSname());
  280. EXPECT_EQ(filename, cclass->getFilename());
  281. }
  282. // Verifies the unparse method of option class definitions
  283. TEST(ClientClassDef, unparseDef) {
  284. CfgMgr::instance().setFamily(AF_INET);
  285. boost::scoped_ptr<ClientClassDef> cclass;
  286. // Get a client class definition and fill it
  287. std::string name = "class1";
  288. ExpressionPtr expr;
  289. ASSERT_NO_THROW(cclass.reset(new ClientClassDef(name, expr)));
  290. std::string test = "option[12].text == 'foo'";
  291. cclass->setTest(test);
  292. std::string next_server = "1.2.3.4";
  293. cclass->setNextServer(IOAddress(next_server));
  294. std::string sname = "my-server.example.com";
  295. cclass->setSname(sname);
  296. std::string filename = "/boot/kernel";
  297. cclass->setFilename(filename);
  298. // Unparse it
  299. std::string expected = "{\n"
  300. "\"name\": \"" + name + "\",\n"
  301. "\"test\": \"" + test + "\",\n"
  302. "\"next-server\": \"" + next_server + "\",\n"
  303. "\"server-hostname\": \"" + sname + "\",\n"
  304. "\"boot-file-name\": \"" + filename + "\",\n"
  305. "\"option-data\": [ ] }\n";
  306. runToElementTest<ClientClassDef>(expected, *cclass);
  307. }
  308. // Verifies the unparse method of client class dictionaries
  309. TEST(ClientClassDictionary, unparseDict) {
  310. CfgMgr::instance().setFamily(AF_INET);
  311. ClientClassDictionaryPtr dictionary;
  312. ExpressionPtr expr;
  313. CfgOptionPtr options;
  314. // Get a client class dictionary and fill it
  315. dictionary.reset(new ClientClassDictionary());
  316. ASSERT_NO_THROW(dictionary->addClass("one", expr, "", options));
  317. ASSERT_NO_THROW(dictionary->addClass("two", expr, "", options));
  318. ASSERT_NO_THROW(dictionary->addClass("three", expr, "", options));
  319. // Unparse it
  320. auto add_defaults =
  321. [](std::string name) {
  322. return ("{\n"
  323. "\"name\": \"" + name + "\",\n"
  324. "\"next-server\": \"0.0.0.0\",\n"
  325. "\"server-hostname\": \"\",\n"
  326. "\"boot-file-name\": \"\",\n"
  327. "\"option-data\": [ ] }");
  328. };
  329. std::string expected = "[\n" +
  330. add_defaults("one") + ",\n" +
  331. add_defaults("two") + ",\n" +
  332. add_defaults("three") + "]\n";
  333. runToElementTest<ClientClassDictionary>(expected, *dictionary);
  334. }
  335. } // end of anonymous namespace