ip_check_unittest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // Copyright (C) 2011 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 <gtest/gtest.h>
  15. #include <acl/ip_check.h>
  16. #include <boost/scoped_ptr.hpp>
  17. using namespace isc::acl;
  18. using namespace std;
  19. // Simple struct holding either an IPV4 or IPV6 address. This is the "Context"
  20. // used for the tests.
  21. struct GeneralAddress {
  22. bool isv4; // true if it holds a v4 address
  23. uint32_t v4addr;
  24. uint8_t v6addr[16];
  25. GeneralAddress()
  26. {}
  27. // Convenience constructor for V4 address. As it is not marked as
  28. // explicit, it allows the automatic promotion of a uint32_t to a
  29. // GeneralAddress data type in calls to matches().
  30. GeneralAddress(uint32_t address) : isv4(true), v4addr(address)
  31. {
  32. fill(v6addr, v6addr + sizeof(v6addr), 0); // Explicitly zero
  33. }
  34. // Convenience constructor for V6 address. As it is not marked as
  35. // explicit, it allows the automatic promotion of a vector<uint8_t> to a
  36. // GeneralAddress data type in calls to matches().
  37. GeneralAddress(const vector<uint8_t>& address) : isv4(false), v4addr(0) {
  38. if (address.size() != sizeof(v6addr)) {
  39. isc_throw(isc::InvalidParameter, "vector passed to GeneralAddress "
  40. "constructor is " << address.size() << " bytes long - it "
  41. "should be " << sizeof(v6addr) << " instead");
  42. }
  43. copy(address.begin(), address.end(), v6addr);
  44. }
  45. };
  46. // Provide specializations of the Ipv4check and Ipv6Check matches() methods for
  47. // the GeneralAddress structure.
  48. namespace isc {
  49. namespace acl {
  50. template <>
  51. bool Ipv4Check<GeneralAddress>::matches(const GeneralAddress& addr) const {
  52. return (addr.isv4 ? compare(addr.v4addr) : false);
  53. }
  54. template <>
  55. bool Ipv6Check<GeneralAddress>::matches(const GeneralAddress& addr) const {
  56. return (addr.isv4 ? false : compare(addr.v6addr));
  57. }
  58. } // namespace acl
  59. } // namespace isc
  60. /// *** Free Function Tests ***
  61. // Test the createNetmask() function.
  62. TEST(IpFunctionCheck, CreateNetmask) {
  63. // 8-bit tests: invalid arguments should throw.
  64. EXPECT_THROW(createNetmask<uint8_t>(9), isc::OutOfRange);
  65. // Check on all possible 8-bit values. Use a signed type to generate a
  66. // variable with the most significant bits set (right-shifting will
  67. // introduce additional bits).
  68. int8_t expected8 = 0x80;
  69. for (size_t i = 1; i <= 8; ++i, expected8 >>= 1) {
  70. EXPECT_EQ(static_cast<uint8_t>(expected8), createNetmask<uint8_t>(i));
  71. }
  72. EXPECT_EQ(0, createNetmask<uint8_t>(0));
  73. // Do the same for 32 bits.
  74. EXPECT_THROW(createNetmask<int32_t>(33), isc::OutOfRange);
  75. int32_t expected32 = 0x80000000;
  76. for (size_t i = 1; i <= 32; ++i, expected32 >>= 1) {
  77. EXPECT_EQ(static_cast<uint32_t>(expected32),
  78. createNetmask<uint32_t>(i));
  79. }
  80. EXPECT_EQ(0, createNetmask<uint32_t>(0));
  81. }
  82. // Test the splitIpAddress() function.
  83. TEST(IpFunctionCheck, SplitIpAddress) {
  84. pair<string, uint32_t> result;
  85. result = splitIpAddress("192.0.2.1/24", 32);
  86. EXPECT_EQ(string("192.0.2.1"), result.first);
  87. EXPECT_EQ(24, result.second);
  88. result = splitIpAddress("192.0.2.2/32", 32);
  89. EXPECT_EQ(string("192.0.2.2"), result.first);
  90. EXPECT_EQ(32, result.second);
  91. result = splitIpAddress("2001:db8::/128", 128);
  92. EXPECT_EQ(string("2001:db8::"), result.first);
  93. EXPECT_EQ(128, result.second);
  94. EXPECT_THROW(splitIpAddress("2001:db8::/129", 128), isc::OutOfRange);
  95. EXPECT_THROW(splitIpAddress("2001:db8::/xxxx", 32), isc::InvalidParameter);
  96. EXPECT_THROW(splitIpAddress("2001:db8::/32/s", 32), isc::InvalidParameter);
  97. }
  98. // *** IPV4 Tests ***
  99. // Check that a default constructor can be instantiated.
  100. TEST(Ipv4Check, V4DefaultConstructor) {
  101. Ipv4Check<GeneralAddress> acl1;
  102. // The test is needed to avoid the unused variable causing a warning or
  103. // getting optimised away.
  104. EXPECT_EQ(1, acl1.getAddress());
  105. }
  106. // Check that the constructor stores the elements correctly and, for the
  107. // address and mask, in network-byte order.
  108. TEST(Ipv4Check, V4ConstructorAddress) {
  109. // Address is presented in network byte order in constructor, so not
  110. // conversion is needed for this test.
  111. Ipv4Check<GeneralAddress> acl1(0x12345678);
  112. EXPECT_EQ(0x12345678, acl1.getAddress());
  113. }
  114. TEST(Ipv4Check, V4ConstructorMask) {
  115. // Valid values. Address of "1" is used as a placeholder
  116. Ipv4Check<GeneralAddress> acl1(1, 1);
  117. // The mask is stored in network byte order, so the pattern expected must
  118. // also be converted to network byte order for the comparison to succeed.
  119. uint32_t expected = htonl(0x80000000);
  120. EXPECT_EQ(expected, acl1.getNetmask());
  121. EXPECT_EQ(1, acl1.getMasksize());
  122. Ipv4Check<GeneralAddress> acl2(1, 24);
  123. expected = htonl(0xffffff00);
  124. EXPECT_EQ(expected, acl2.getNetmask());
  125. EXPECT_EQ(24, acl2.getMasksize());
  126. // ... and some invalid network masks
  127. EXPECT_THROW(Ipv4Check<GeneralAddress>(1, 0), isc::OutOfRange);
  128. EXPECT_THROW(Ipv4Check<GeneralAddress>(1, 33), isc::OutOfRange);
  129. }
  130. TEST(Ipv4Check, V4ConstructorInverse) {
  131. // Valid values. Address/mask of "1" is used as a placeholder
  132. Ipv4Check<GeneralAddress> acl1(1, 1);
  133. EXPECT_FALSE(acl1.getInverse());
  134. Ipv4Check<GeneralAddress> acl2(1, 1, true);
  135. EXPECT_TRUE(acl2.getInverse());
  136. Ipv4Check<GeneralAddress> acl3(1, 1, false);
  137. EXPECT_FALSE(acl3.getInverse());
  138. }
  139. TEST(Ipv4Check, V4StringConstructor) {
  140. Ipv4Check<GeneralAddress> acl1("192.0.2.255");
  141. uint32_t expected = htonl(0xc00002ff);
  142. EXPECT_EQ(expected, acl1.getAddress());
  143. EXPECT_EQ(32, acl1.getMasksize());
  144. Ipv4Check<GeneralAddress> acl2("192.0.2.0/24");
  145. expected = htonl(0xc0000200);
  146. EXPECT_EQ(expected, acl2.getAddress());
  147. EXPECT_EQ(24, acl2.getMasksize());
  148. EXPECT_THROW(Ipv4Check<GeneralAddress>("192.0.2.0/0"), isc::OutOfRange);
  149. EXPECT_THROW(Ipv4Check<GeneralAddress>("192.0.2.0/33"), isc::OutOfRange);
  150. EXPECT_THROW(Ipv4Check<GeneralAddress>("192.0.2.0/24/3"),
  151. isc::InvalidParameter);
  152. EXPECT_THROW(Ipv4Check<GeneralAddress>("192.0.2.0/ww"),
  153. isc::InvalidParameter);
  154. EXPECT_THROW(Ipv4Check<GeneralAddress>("aa.255.255.0/ww"),
  155. isc::InvalidParameter);
  156. }
  157. TEST(Ipv4Check, V4CopyConstructor) {
  158. Ipv4Check<GeneralAddress> acl1("192.0.2.1/24", true);
  159. Ipv4Check<GeneralAddress> acl2(acl1);
  160. EXPECT_EQ(acl1.getAddress(), acl2.getAddress());
  161. EXPECT_EQ(acl1.getMasksize(), acl2.getMasksize());
  162. EXPECT_EQ(acl1.getNetmask(), acl2.getNetmask());
  163. EXPECT_EQ(acl1.getInverse(), acl2.getInverse());
  164. }
  165. TEST(Ipv4Check, V4AssignmentOperator) {
  166. Ipv4Check<GeneralAddress> acl1("192.0.2.0/24", true);
  167. Ipv4Check<GeneralAddress> acl2("192.0.2.128/25", false);
  168. acl2 = acl1;
  169. EXPECT_EQ(acl1.getAddress(), acl2.getAddress());
  170. EXPECT_EQ(acl1.getMasksize(), acl2.getMasksize());
  171. EXPECT_EQ(acl1.getNetmask(), acl2.getNetmask());
  172. EXPECT_EQ(acl1.getInverse(), acl2.getInverse());
  173. }
  174. // Check that the comparison works - note that "matches" just calls the
  175. // internal compare() code. (Also note that the argument to matches() will be
  176. // automatically converted to the GeneralAddress data type used for the tests
  177. // because of its constructor taking a uint32_t argument.
  178. //
  179. // As before, note that addresses passed to the class are expected to be in
  180. // network-byte order. Therefore for the comparisons to work as expected, we
  181. // must convert the values to network-byte order first.
  182. TEST(Ipv4Check, V4Compare) {
  183. // Exact address - match if given address matches stored address.
  184. Ipv4Check<GeneralAddress> acl1(htonl(0x23457f13), 32);
  185. EXPECT_TRUE(acl1.matches(htonl(0x23457f13)));
  186. EXPECT_FALSE(acl1.matches(htonl(0x23457f12)));
  187. EXPECT_FALSE(acl1.matches(htonl(0x13457f13)));
  188. // Exact address - match if address does not match stored address
  189. Ipv4Check<GeneralAddress> acl2(htonl(0x23457f13), 32, true);
  190. EXPECT_FALSE(acl2.matches(htonl(0x23457f13)));
  191. EXPECT_TRUE(acl2.matches(htonl(0x23457f12)));
  192. EXPECT_TRUE(acl2.matches(htonl(0x13457f13)));
  193. // Match if the address matches a mask
  194. Ipv4Check<GeneralAddress> acl3(htonl(0x23450000), 16);
  195. EXPECT_TRUE(acl3.matches(htonl(0x23450000)));
  196. EXPECT_TRUE(acl3.matches(htonl(0x23450001)));
  197. EXPECT_TRUE(acl3.matches(htonl(0x2345ffff)));
  198. EXPECT_FALSE(acl3.matches(htonl(0x23460000)));
  199. EXPECT_FALSE(acl3.matches(htonl(0x2346ffff)));
  200. // Match if the address does not match a mask
  201. Ipv4Check<GeneralAddress> acl4(htonl(0x23450000), 16, true);
  202. EXPECT_FALSE(acl4.matches(htonl(0x23450000)));
  203. EXPECT_FALSE(acl4.matches(htonl(0x23450001)));
  204. EXPECT_FALSE(acl4.matches(htonl(0x2345ffff)));
  205. EXPECT_TRUE(acl4.matches(htonl(0x23460000)));
  206. EXPECT_TRUE(acl4.matches(htonl(0x2346ffff)));
  207. }
  208. // *** IPV6 Tests ***
  209. // Some constants used in the tests
  210. static const char* V6ADDR_1_STRING = "2001:0db8:1122:3344:5566:7788:99aa:bbcc";
  211. static const uint8_t V6ADDR_1[] = {
  212. 0x20, 0x01, 0x0d, 0xb8, 0x11, 0x22, 0x33, 0x44,
  213. 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc
  214. };
  215. static const char* V6ADDR_2_STRING = "2001:0db8::dead:beef";
  216. static const uint8_t V6ADDR_2[] = {
  217. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  218. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  219. };
  220. // Identical to V6ADDR_2 to 48 bits
  221. static const uint8_t V6ADDR_2_48[] = {
  222. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x55, 0x66,
  223. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  224. };
  225. // Identical to V6ADDR_2 to 52 bits
  226. static const uint8_t V6ADDR_2_52[] = {
  227. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x05, 0x66,
  228. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  229. };
  230. static const char* V6ADDR_3_STRING = "::1";
  231. static const uint8_t V6ADDR_3[] = {
  232. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  233. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  234. };
  235. // Mask with MS bit set
  236. static const uint8_t MASK_1[] = {
  237. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  238. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  239. };
  240. static const uint8_t MASK_8[] = {
  241. 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  242. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  243. };
  244. static const uint8_t MASK_48[] = {
  245. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
  246. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  247. };
  248. static const uint8_t MASK_51[] = {
  249. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00,
  250. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  251. };
  252. static const uint8_t MASK_128[] = {
  253. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  254. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  255. };
  256. // Check that a default constructor can be instantiated.
  257. TEST(Ipv6Check, V6DefaultConstructor) {
  258. Ipv6Check<GeneralAddress> acl1;
  259. // The test is needed to avoid the unused variable causing a warning or
  260. // getting optimised away.
  261. EXPECT_EQ(0, acl1.getMasksize());
  262. }
  263. TEST(Ipv6Check, V6ConstructorAddress) {
  264. Ipv6Check<GeneralAddress> acl1(V6ADDR_1);
  265. vector<uint8_t> stored = acl1.getAddress();
  266. EXPECT_EQ(sizeof(V6ADDR_1), stored.size());
  267. EXPECT_TRUE(equal(stored.begin(), stored.end(), V6ADDR_1));
  268. }
  269. TEST(Ipv6Check, V6ConstructorMask) {
  270. // Valid masks...
  271. Ipv6Check<GeneralAddress> acl1(V6ADDR_1, 1);
  272. vector<uint8_t> stored = acl1.getNetmask();
  273. EXPECT_EQ(sizeof(MASK_1), stored.size());
  274. EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_1));
  275. Ipv6Check<GeneralAddress> acl2(V6ADDR_1, 8);
  276. stored = acl2.getNetmask();
  277. EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_8));
  278. Ipv6Check<GeneralAddress> acl3(V6ADDR_1, 48);
  279. stored = acl3.getNetmask();
  280. EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_48));
  281. Ipv6Check<GeneralAddress> acl4(V6ADDR_1, 51);
  282. stored = acl4.getNetmask();
  283. EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_51));
  284. Ipv6Check<GeneralAddress> acl5(V6ADDR_1, 128);
  285. stored = acl5.getNetmask();
  286. EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_128));
  287. // ... and some invalid network masks
  288. EXPECT_THROW(Ipv6Check<GeneralAddress>(V6ADDR_1, 0), isc::OutOfRange);
  289. EXPECT_THROW(Ipv6Check<GeneralAddress>(V6ADDR_1, 129), isc::OutOfRange);
  290. }
  291. TEST(Ipv6Check, V6ConstructorInverse) {
  292. // Valid values. Address/mask of "1" is used as a placeholder
  293. Ipv6Check<GeneralAddress> acl1(V6ADDR_1, 1);
  294. EXPECT_FALSE(acl1.getInverse());
  295. Ipv6Check<GeneralAddress> acl2(V6ADDR_1, 1, true);
  296. EXPECT_TRUE(acl2.getInverse());
  297. Ipv6Check<GeneralAddress> acl3(V6ADDR_1, 1, false);
  298. EXPECT_FALSE(acl3.getInverse());
  299. }
  300. TEST(Ipv6Check, V6StringConstructor) {
  301. Ipv6Check<GeneralAddress> acl1(V6ADDR_1_STRING);
  302. vector<uint8_t> address = acl1.getAddress();
  303. EXPECT_EQ(128, acl1.getMasksize());
  304. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_1));
  305. Ipv6Check<GeneralAddress> acl2(string(V6ADDR_2_STRING) + string("/48"));
  306. address = acl2.getAddress();
  307. EXPECT_EQ(48, acl2.getMasksize());
  308. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_2));
  309. Ipv6Check<GeneralAddress> acl3("::1");
  310. address = acl3.getAddress();
  311. EXPECT_EQ(128, acl3.getMasksize());
  312. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_3));
  313. EXPECT_THROW(Ipv6Check<GeneralAddress>("::1/0"), isc::OutOfRange);
  314. EXPECT_THROW(Ipv6Check<GeneralAddress>("::1/129"), isc::OutOfRange);
  315. EXPECT_THROW(Ipv6Check<GeneralAddress>("::1/24/3"), isc::InvalidParameter);
  316. EXPECT_THROW(Ipv6Check<GeneralAddress>("2001:0db8::abcd/ww"),
  317. isc::InvalidParameter);
  318. EXPECT_THROW(Ipv6Check<GeneralAddress>("2xx1:0db8::abcd/32"),
  319. isc::InvalidParameter);
  320. }
  321. TEST(Ipv6Check, V6CopyConstructor) {
  322. Ipv6Check<GeneralAddress> acl1(string(V6ADDR_2_STRING) + string("/52"));
  323. Ipv6Check<GeneralAddress> acl2(acl1);
  324. vector<uint8_t> acl1_address = acl1.getAddress();
  325. vector<uint8_t> acl2_address = acl1.getAddress();
  326. EXPECT_EQ(sizeof(V6ADDR_1), acl1_address.size());
  327. EXPECT_EQ(acl1_address.size(), acl2_address.size());
  328. EXPECT_TRUE(equal(acl1_address.begin(), acl1_address.end(),
  329. acl2_address.begin()));
  330. EXPECT_EQ(acl1.getMasksize(), acl2.getMasksize());
  331. vector<uint8_t> acl1_netmask = acl1.getNetmask();
  332. vector<uint8_t> acl2_netmask = acl1.getNetmask();
  333. EXPECT_EQ(sizeof(V6ADDR_1), acl1_netmask.size());
  334. EXPECT_EQ(acl1_netmask.size(), acl2_netmask.size());
  335. EXPECT_TRUE(equal(acl1_netmask.begin(), acl1_netmask.end(),
  336. acl2_netmask.begin()));
  337. EXPECT_EQ(acl1.getInverse(), acl2.getInverse());
  338. }
  339. TEST(Ipv6Check, V6AssignmentOperator) {
  340. Ipv6Check<GeneralAddress> acl1(string(V6ADDR_2_STRING) + string("/52"));
  341. Ipv6Check<GeneralAddress> acl2(string(V6ADDR_1_STRING) + string("/48"));
  342. acl2 = acl1;
  343. vector<uint8_t> acl1_address = acl1.getAddress();
  344. vector<uint8_t> acl2_address = acl2.getAddress();
  345. EXPECT_EQ(sizeof(V6ADDR_1), acl1_address.size());
  346. EXPECT_EQ(acl1_address.size(), acl2_address.size());
  347. EXPECT_TRUE(equal(acl1_address.begin(), acl1_address.end(),
  348. acl2_address.begin()));
  349. EXPECT_EQ(acl1.getMasksize(), acl2.getMasksize());
  350. vector<uint8_t> acl1_netmask = acl1.getNetmask();
  351. vector<uint8_t> acl2_netmask = acl2.getNetmask();
  352. EXPECT_EQ(sizeof(V6ADDR_1), acl1_netmask.size());
  353. EXPECT_EQ(acl1_netmask.size(), acl2_netmask.size());
  354. EXPECT_TRUE(equal(acl1_netmask.begin(), acl1_netmask.end(),
  355. acl2_netmask.begin()));
  356. EXPECT_EQ(acl1.getInverse(), acl2.getInverse());
  357. }
  358. TEST(Ipv6Check, V6Compare) {
  359. // Set up some data.
  360. vector<uint8_t> v6addr_2(V6ADDR_2, V6ADDR_2 + sizeof(V6ADDR_2));
  361. vector<uint8_t> v6addr_2_48(V6ADDR_2_48, V6ADDR_2_48 + sizeof(V6ADDR_2_48));
  362. vector<uint8_t> v6addr_2_52(V6ADDR_2_52, V6ADDR_2_52 + sizeof(V6ADDR_2_52));
  363. vector<uint8_t> v6addr_3(V6ADDR_3, V6ADDR_3 + sizeof(V6ADDR_3));
  364. // Exact address - match if given address matches stored address.
  365. Ipv6Check<GeneralAddress> acl1(string(V6ADDR_2_STRING) + string("/128"));
  366. EXPECT_TRUE(acl1.matches(v6addr_2));
  367. EXPECT_FALSE(acl1.matches(v6addr_2_52));
  368. EXPECT_FALSE(acl1.matches(v6addr_2_48));
  369. EXPECT_FALSE(acl1.matches(v6addr_3));
  370. // Exact address - match if address does not match stored address
  371. Ipv6Check<GeneralAddress> acl2(string(V6ADDR_2_STRING) + string("/128"),
  372. true);
  373. EXPECT_FALSE(acl2.matches(v6addr_2));
  374. EXPECT_TRUE(acl2.matches(v6addr_2_52));
  375. EXPECT_TRUE(acl2.matches(v6addr_2_48));
  376. EXPECT_TRUE(acl2.matches(v6addr_3));
  377. // Match if the address matches a mask
  378. Ipv6Check<GeneralAddress> acl3(string(V6ADDR_2_STRING) + string("/52"));
  379. EXPECT_TRUE(acl3.matches(v6addr_2));
  380. EXPECT_TRUE(acl3.matches(v6addr_2_52));
  381. EXPECT_FALSE(acl3.matches(v6addr_2_48));
  382. EXPECT_FALSE(acl3.matches(v6addr_3));
  383. // Match if the address does not match a mask
  384. Ipv6Check<GeneralAddress> acl4(string(V6ADDR_2_STRING) + string("/52"),
  385. true);
  386. EXPECT_FALSE(acl4.matches(v6addr_2));
  387. EXPECT_FALSE(acl4.matches(v6addr_2_52));
  388. EXPECT_TRUE(acl4.matches(v6addr_2_48));
  389. EXPECT_TRUE(acl4.matches(v6addr_3));
  390. }
  391. // *** IP Tests ***
  392. TEST(IpCheck, V4Test) {
  393. IpCheck<GeneralAddress> acl("192.168.132.255/16");
  394. //c0 a8 84 ff
  395. GeneralAddress test;
  396. test.isv4 = true;
  397. test.v4addr = htonl(0xc0a884ff);
  398. EXPECT_TRUE(acl.matches(test));
  399. test.v4addr = htonl(0xc0a884ee); // Correct to 24 bits
  400. EXPECT_TRUE(acl.matches(test));
  401. test.v4addr = htonl(0xc0a8ffee); // Correct to 16 bits
  402. EXPECT_TRUE(acl.matches(test));
  403. test.v4addr = htonl(0xc000ffee); // Incorrect
  404. EXPECT_FALSE(acl.matches(test));
  405. test.isv4 = false;
  406. test.v4addr = htonl(0xc0a884ff); // Correct IPV4 address
  407. EXPECT_FALSE(acl.matches(test));
  408. // Quick check for negative match
  409. IpCheck<GeneralAddress> acl2("192.168.132.255/16", true);
  410. test.isv4 = true;
  411. test.v4addr = htonl(0xc0a884ff);
  412. EXPECT_FALSE(acl2.matches(test));
  413. }
  414. TEST(IpCheck, V6Test) {
  415. IpCheck<GeneralAddress> acl(string(V6ADDR_2_STRING) + string("/52"));
  416. GeneralAddress test;
  417. test.isv4 = false;
  418. copy(V6ADDR_2, V6ADDR_2 + sizeof(V6ADDR_2), test.v6addr);
  419. EXPECT_TRUE(acl.matches(test));
  420. copy(V6ADDR_2_52, V6ADDR_2_52 + sizeof(V6ADDR_2_52), test.v6addr);
  421. EXPECT_TRUE(acl.matches(test));
  422. copy(V6ADDR_2_48, V6ADDR_2_48 + sizeof(V6ADDR_2_48), test.v6addr);
  423. EXPECT_FALSE(acl.matches(test));
  424. test.isv4 = true;
  425. copy(V6ADDR_2, V6ADDR_2 + sizeof(V6ADDR_2), test.v6addr);
  426. // Correct V6 address
  427. EXPECT_FALSE(acl.matches(test));
  428. // Quick check for negative match
  429. IpCheck<GeneralAddress> acl2(string(V6ADDR_2_STRING) + string("/52"), true);
  430. test.isv4 = false;
  431. copy(V6ADDR_2, V6ADDR_2 + sizeof(V6ADDR_2), test.v6addr);
  432. EXPECT_FALSE(acl2.matches(test));
  433. }
  434. // Check copy constructor and assignment operator for V4 address
  435. TEST(IpCheck, V4Copying) {
  436. IpCheck<GeneralAddress> acl("192.168.132.255/16");
  437. GeneralAddress test;
  438. test.isv4 = true;
  439. test.v4addr = htonl(0xc0a884ff);
  440. IpCheck<GeneralAddress> acl1(acl);
  441. EXPECT_TRUE(acl1.matches(test));
  442. IpCheck<GeneralAddress> acl2("192.255.132.255/32");
  443. EXPECT_FALSE(acl2.matches(test));
  444. acl2 = acl;
  445. EXPECT_TRUE(acl2.matches(test));
  446. }
  447. // Check copy constructor and assignment operator for V6 address
  448. TEST(IpCheck, V6Copying) {
  449. IpCheck<GeneralAddress> acl(string(V6ADDR_2_STRING) + string("/52"));
  450. GeneralAddress test;
  451. test.isv4 = false;
  452. copy(V6ADDR_2, V6ADDR_2 + sizeof(V6ADDR_2), test.v6addr);
  453. IpCheck<GeneralAddress> acl1(acl);
  454. EXPECT_TRUE(acl1.matches(test));
  455. IpCheck<GeneralAddress> acl2(V6ADDR_3_STRING);
  456. EXPECT_FALSE(acl2.matches(test));
  457. acl2 = acl;
  458. EXPECT_TRUE(acl2.matches(test));
  459. }