cfg_subnets6_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright (C) 2014 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 <dhcp/classify.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp/option.h>
  18. #include <dhcpsrv/cfg_subnets6.h>
  19. #include <dhcpsrv/subnet.h>
  20. #include <dhcpsrv/subnet_id.h>
  21. #include <dhcpsrv/subnet_selector.h>
  22. #include <gtest/gtest.h>
  23. #include <string>
  24. using namespace isc;
  25. using namespace isc::asiolink;
  26. using namespace isc::dhcp;
  27. namespace {
  28. /// @brief Generates interface id option.
  29. ///
  30. /// @param text Interface id in a textual format.
  31. OptionPtr
  32. generateInterfaceId(const std::string& text) {
  33. OptionBuffer buffer(text.begin(), text.end());
  34. return OptionPtr(new Option(Option::V6, D6O_INTERFACE_ID, buffer));
  35. }
  36. // This test checks that the subnet can be selected using a relay agent's
  37. // link address.
  38. TEST(CfgSubnets6Test, selectSubnetByRelayAddress) {
  39. CfgSubnets6 cfg;
  40. // Let's configure 3 subnets
  41. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  42. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  43. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  44. cfg.add(subnet1);
  45. cfg.add(subnet2);
  46. cfg.add(subnet3);
  47. // Make sure that none of the subnets is selected when there is no relay
  48. // information configured for them.
  49. SubnetSelector selector;
  50. selector.first_relay_linkaddr_ = IOAddress("2001:db8:ff::1");
  51. EXPECT_FALSE(cfg.selectSubnet(selector));
  52. selector.first_relay_linkaddr_ = IOAddress("2001:db8:ff::2");
  53. EXPECT_FALSE(cfg.selectSubnet(selector));
  54. selector.first_relay_linkaddr_ = IOAddress("2001:db8:ff::3");
  55. EXPECT_FALSE(cfg.selectSubnet(selector));
  56. // Now specify relay information.
  57. subnet1->setRelayInfo(IOAddress("2001:db8:ff::1"));
  58. subnet2->setRelayInfo(IOAddress("2001:db8:ff::2"));
  59. subnet3->setRelayInfo(IOAddress("2001:db8:ff::3"));
  60. // And try again. This time relay-info is there and should match.
  61. selector.first_relay_linkaddr_ = IOAddress("2001:db8:ff::1");
  62. EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
  63. selector.first_relay_linkaddr_ = IOAddress("2001:db8:ff::2");
  64. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  65. selector.first_relay_linkaddr_ = IOAddress("2001:db8:ff::3");
  66. EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
  67. }
  68. // This test checks that the subnet can be selected using an interface
  69. // name associated with a asubnet.
  70. TEST(CfgSubnets6Test, selectSubnetByInterfaceName) {
  71. CfgSubnets6 cfg;
  72. // Let's create 3 subnets.
  73. Subnet6Ptr subnet1(new Subnet6(IOAddress("2000::"), 48, 1, 2, 3, 4));
  74. Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 48, 1, 2, 3, 4));
  75. Subnet6Ptr subnet3(new Subnet6(IOAddress("4000::"), 48, 1, 2, 3, 4));
  76. subnet1->setIface("foo");
  77. subnet2->setIface("bar");
  78. subnet3->setIface("foobar");
  79. // Until subnets are added to the configuration, there should be nothing
  80. // returned.
  81. SubnetSelector selector;
  82. selector.iface_name_ = "foo";
  83. EXPECT_FALSE(cfg.selectSubnet(selector));
  84. // Add one of the subnets.
  85. cfg.add(subnet1);
  86. // The subnet should be now selected for the interface name "foo".
  87. EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
  88. // Check that the interface name is checked even when there is
  89. // only one subnet defined: there should be nothing returned when
  90. // other interface name is specified.
  91. selector.iface_name_ = "bar";
  92. EXPECT_FALSE(cfg.selectSubnet(selector));
  93. // Add other subnets.
  94. cfg.add(subnet2);
  95. cfg.add(subnet3);
  96. // When we specify correct interface names, the subnets should be returned.
  97. selector.iface_name_ = "foobar";
  98. EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
  99. selector.iface_name_ = "bar";
  100. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  101. // When specifying a non-existing interface the subnet should not be
  102. // returned.
  103. selector.iface_name_ = "xyzzy";
  104. EXPECT_FALSE(cfg.selectSubnet(selector));
  105. }
  106. // This test checks that the subnet can be selected using an Interface ID
  107. // option inserted by a relay.
  108. TEST(CfgSubnets6Test, selectSubnetByInterfaceId) {
  109. CfgSubnets6 cfg;
  110. // Create 3 subnets.
  111. Subnet6Ptr subnet1(new Subnet6(IOAddress("2000::"), 48, 1, 2, 3, 4));
  112. Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 48, 1, 2, 3, 4));
  113. Subnet6Ptr subnet3(new Subnet6(IOAddress("4000::"), 48, 1, 2, 3, 4));
  114. // Create Interface-id options used in subnets 1,2, and 3
  115. OptionPtr ifaceid1 = generateInterfaceId("relay1.eth0");
  116. OptionPtr ifaceid2 = generateInterfaceId("VL32");
  117. // That's a strange interface-id, but this is a real life example
  118. OptionPtr ifaceid3 = generateInterfaceId("ISAM144|299|ipv6|nt:vp:1:110");
  119. // Bogus interface-id.
  120. OptionPtr ifaceid_bogus = generateInterfaceId("non-existent");
  121. // Assign interface ids to the respective subnets.
  122. subnet1->setInterfaceId(ifaceid1);
  123. subnet2->setInterfaceId(ifaceid2);
  124. subnet3->setInterfaceId(ifaceid3);
  125. // There shouldn't be any subnet configured at this stage.
  126. SubnetSelector selector;
  127. selector.interface_id_ = ifaceid1;
  128. // Note that some link address must be specified to indicate that it is
  129. // a relayed message!
  130. selector.first_relay_linkaddr_ = IOAddress("5000::1");
  131. EXPECT_FALSE(cfg.selectSubnet(selector));
  132. // Add one of the subnets.
  133. cfg.add(subnet1);
  134. // If only one subnet has been specified, it should be returned when the
  135. // interface id matches. But, for a different interface id there should be
  136. // no match.
  137. EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
  138. selector.interface_id_ = ifaceid2;
  139. EXPECT_FALSE(cfg.selectSubnet(selector));
  140. // Add other subnets.
  141. cfg.add(subnet2);
  142. cfg.add(subnet3);
  143. // Now that we have all subnets added. we should be able to retrieve them
  144. // using appropriate interface ids.
  145. selector.interface_id_ = ifaceid3;
  146. EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
  147. selector.interface_id_ = ifaceid2;
  148. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  149. // For invalid interface id, there should be nothing returned.
  150. selector.interface_id_ = ifaceid_bogus;
  151. EXPECT_FALSE(cfg.selectSubnet(selector));
  152. }
  153. // Test that the client classes are considered when the subnet is selected by
  154. // the relay link address.
  155. TEST(CfgSubnets6Test, selectSubnetByRelayAddressAndClassify) {
  156. CfgSubnets6 cfg;
  157. // Let's configure 3 subnets
  158. Subnet6Ptr subnet1(new Subnet6(IOAddress("2000::"), 48, 1, 2, 3, 4));
  159. Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 48, 1, 2, 3, 4));
  160. Subnet6Ptr subnet3(new Subnet6(IOAddress("4000::"), 48, 1, 2, 3, 4));
  161. cfg.add(subnet1);
  162. cfg.add(subnet2);
  163. cfg.add(subnet3);
  164. // Let's sanity check that we can use that configuration.
  165. SubnetSelector selector;
  166. selector.first_relay_linkaddr_ = IOAddress("2000::123");
  167. EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
  168. selector.first_relay_linkaddr_ = IOAddress("3000::345");
  169. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  170. selector.first_relay_linkaddr_ = IOAddress("4000::567");
  171. EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
  172. // Client now belongs to bar class.
  173. selector.client_classes_.insert("bar");
  174. // There are no class restrictions defined, so everything should work
  175. // as before.
  176. selector.first_relay_linkaddr_ = IOAddress("2000::123");
  177. EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
  178. selector.first_relay_linkaddr_ = IOAddress("3000::345");
  179. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  180. selector.first_relay_linkaddr_ = IOAddress("4000::567");
  181. EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
  182. // Now let's add client class restrictions.
  183. subnet1->allowClientClass("foo"); // Serve here only clients from foo class
  184. subnet2->allowClientClass("bar"); // Serve here only clients from bar class
  185. subnet3->allowClientClass("baz"); // Serve here only clients from baz class
  186. // The same check as above should result in client being served only in
  187. // bar class, i.e. subnet2
  188. selector.first_relay_linkaddr_ = IOAddress("2000::123");
  189. EXPECT_FALSE(cfg.selectSubnet(selector));
  190. selector.first_relay_linkaddr_ = IOAddress("3000::345");
  191. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  192. selector.first_relay_linkaddr_ = IOAddress("4000::567");
  193. EXPECT_FALSE(cfg.selectSubnet(selector));
  194. // Now let's check that client with wrong class is not supported
  195. selector.client_classes_.clear();
  196. selector.client_classes_.insert("some_other_class");
  197. selector.first_relay_linkaddr_ = IOAddress("2000::123");
  198. EXPECT_FALSE(cfg.selectSubnet(selector));
  199. selector.first_relay_linkaddr_ = IOAddress("3000::345");
  200. EXPECT_FALSE(cfg.selectSubnet(selector));
  201. selector.first_relay_linkaddr_ = IOAddress("4000::567");
  202. EXPECT_FALSE(cfg.selectSubnet(selector));
  203. // Finally, let's check that client without any classes is not supported
  204. selector.client_classes_.clear();
  205. selector.first_relay_linkaddr_ = IOAddress("2000::123");
  206. EXPECT_FALSE(cfg.selectSubnet(selector));
  207. selector.first_relay_linkaddr_ = IOAddress("3000::345");
  208. EXPECT_FALSE(cfg.selectSubnet(selector));
  209. selector.first_relay_linkaddr_ = IOAddress("4000::567");
  210. EXPECT_FALSE(cfg.selectSubnet(selector));
  211. }
  212. // Test that client classes are considered when the subnet is selcted by the
  213. // interface name.
  214. TEST(CfgSubnets6Test, selectSubnetByInterfaceNameAndClaassify) {
  215. CfgSubnets6 cfg;
  216. Subnet6Ptr subnet1(new Subnet6(IOAddress("2000::"), 48, 1, 2, 3, 4));
  217. Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 48, 1, 2, 3, 4));
  218. Subnet6Ptr subnet3(new Subnet6(IOAddress("4000::"), 48, 1, 2, 3, 4));
  219. subnet1->setIface("foo");
  220. subnet2->setIface("bar");
  221. subnet3->setIface("foobar");
  222. cfg.add(subnet1);
  223. cfg.add(subnet2);
  224. cfg.add(subnet3);
  225. // Now we have only one subnet, any request will be served from it
  226. SubnetSelector selector;
  227. selector.client_classes_.insert("bar");
  228. selector.iface_name_ = "foo";
  229. EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
  230. selector.iface_name_ = "bar";
  231. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  232. selector.iface_name_ = "foobar";
  233. EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
  234. subnet1->allowClientClass("foo"); // Serve here only clients from foo class
  235. subnet2->allowClientClass("bar"); // Serve here only clients from bar class
  236. subnet3->allowClientClass("baz"); // Serve here only clients from baz class
  237. selector.iface_name_ = "foo";
  238. EXPECT_FALSE(cfg.selectSubnet(selector));
  239. selector.iface_name_ = "bar";
  240. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  241. selector.iface_name_ = "foobar";
  242. EXPECT_FALSE(cfg.selectSubnet(selector));
  243. }
  244. // Test that client classes are considered when the interface is selected by
  245. // the interface id.
  246. TEST(CfgSubnets6Test, selectSubnetByInterfaceIdAndClassify) {
  247. CfgSubnets6 cfg;
  248. Subnet6Ptr subnet1(new Subnet6(IOAddress("2000::"), 48, 1, 2, 3, 4));
  249. Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 48, 1, 2, 3, 4));
  250. Subnet6Ptr subnet3(new Subnet6(IOAddress("4000::"), 48, 1, 2, 3, 4));
  251. // interface-id options used in subnets 1,2, and 3
  252. OptionPtr ifaceid1 = generateInterfaceId("relay1.eth0");
  253. OptionPtr ifaceid2 = generateInterfaceId("VL32");
  254. // That's a strange interface-id, but this is a real life example
  255. OptionPtr ifaceid3 = generateInterfaceId("ISAM144|299|ipv6|nt:vp:1:110");
  256. // bogus interface-id
  257. OptionPtr ifaceid_bogus = generateInterfaceId("non-existent");
  258. subnet1->setInterfaceId(ifaceid1);
  259. subnet2->setInterfaceId(ifaceid2);
  260. subnet3->setInterfaceId(ifaceid3);
  261. cfg.add(subnet1);
  262. cfg.add(subnet2);
  263. cfg.add(subnet3);
  264. // If we have only a single subnet and the request came from a local
  265. // address, let's use that subnet
  266. SubnetSelector selector;
  267. selector.first_relay_linkaddr_ = IOAddress("5000::1");
  268. selector.client_classes_.insert("bar");
  269. selector.interface_id_ = ifaceid1;
  270. EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
  271. selector.interface_id_ = ifaceid2;
  272. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  273. selector.interface_id_ = ifaceid3;
  274. EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
  275. subnet1->allowClientClass("foo"); // Serve here only clients from foo class
  276. subnet2->allowClientClass("bar"); // Serve here only clients from bar class
  277. subnet3->allowClientClass("baz"); // Serve here only clients from baz class
  278. EXPECT_FALSE(cfg.selectSubnet(selector));
  279. selector.interface_id_ = ifaceid2;
  280. EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
  281. selector.interface_id_ = ifaceid3;
  282. EXPECT_FALSE(cfg.selectSubnet(selector));
  283. }
  284. // Checks that detection of duplicated subnet IDs works as expected. It should
  285. // not be possible to add two IPv6 subnets holding the same ID.
  286. TEST(CfgSubnets6, duplication) {
  287. CfgSubnets6 cfg;
  288. Subnet6Ptr subnet1(new Subnet6(IOAddress("2000::"), 48, 1, 2, 3, 4, 123));
  289. Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 48, 1, 2, 3, 4, 124));
  290. Subnet6Ptr subnet3(new Subnet6(IOAddress("4000::"), 48, 1, 2, 3, 4, 123));
  291. ASSERT_NO_THROW(cfg.add(subnet1));
  292. EXPECT_NO_THROW(cfg.add(subnet2));
  293. // Subnet 3 has the same ID as subnet 1. It shouldn't be able to add it.
  294. EXPECT_THROW(cfg.add(subnet3), isc::dhcp::DuplicateSubnetID);
  295. }
  296. } // end of anonymous namespace