ip_check_unittest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <string.h>
  17. #include <gtest/gtest.h>
  18. #include <acl/ip_check.h>
  19. #include "sockaddr.h"
  20. using namespace isc::acl;
  21. using namespace isc::acl::internal;
  22. using namespace std;
  23. namespace {
  24. const size_t IPV4_SIZE = 4;
  25. const size_t IPV6_SIZE = 16;
  26. // Simple struct holding either an IPV4 or IPV6 address. This is the "Context"
  27. // used for the tests.
  28. //
  29. // The structure is also used for converting an IPV4 address to a four-byte
  30. // array.
  31. struct GeneralAddress {
  32. int family; // Family of the address
  33. vector<uint8_t> addr; // Address type. Size indicates what it holds
  34. // Convert uint32_t address in host-byte order to a uint8_t vector in
  35. // network-byte order.
  36. vector<uint8_t> convertUint32(uint32_t address) {
  37. BOOST_STATIC_ASSERT(sizeof(uint32_t) == IPV4_SIZE);
  38. vector<uint8_t> result(IPV4_SIZE);
  39. // Address is in network-byte order, so copy to the array. The
  40. // MS byte is at the lowest address.
  41. result[3] = address & 0xff;
  42. result[2] = (address >> 8) & 0xff;
  43. result[1] = (address >> 16) & 0xff;
  44. result[0] = (address >> 24) & 0xff;
  45. return (result);
  46. }
  47. // Convenience constructor for V4 address. As it is not marked as explicit,
  48. // it allows the automatic promotion of a uint32_t to a GeneralAddress data
  49. // type in calls to matches().
  50. GeneralAddress(uint32_t address) : family(AF_INET), addr()
  51. {
  52. addr = convertUint32(address);
  53. }
  54. // Convenience constructor for V6 address. As it is not marked as explicit,
  55. // it allows the automatic promotion of a vector<uint8_t> to a
  56. // GeneralAddress data type in calls to matches().
  57. GeneralAddress(const vector<uint8_t>& address) : family(AF_INET6),
  58. addr(address)
  59. {
  60. if (address.size() != IPV6_SIZE) {
  61. isc_throw(isc::InvalidParameter, "vector passed to GeneralAddress "
  62. "constructor is " << address.size() << " bytes long - it "
  63. "should be " << IPV6_SIZE << " bytes instead");
  64. }
  65. }
  66. // A couple of convenience methods for checking equality with different
  67. // representations of an address.
  68. // Check that the IPV4 address is the same as that given.
  69. bool equals(uint32_t address) {
  70. if (family == AF_INET) {
  71. const vector<uint8_t> byte_address = convertUint32(address);
  72. return (equal(byte_address.begin(), byte_address.end(),
  73. addr.begin()));
  74. }
  75. return (false);
  76. }
  77. // Check that the array is equal to that given.
  78. bool equals(const vector<uint8_t>& byte_address) {
  79. if (addr.size() == byte_address.size()) {
  80. return (equal(byte_address.begin(), byte_address.end(),
  81. addr.begin()));
  82. }
  83. return (false);
  84. }
  85. };
  86. } // Unnamed namespace
  87. // Provide a specialisation of the IPCheck::matches() method for the
  88. // GeneralAddress class.
  89. namespace isc {
  90. namespace acl {
  91. template <>
  92. bool IPCheck<GeneralAddress>::matches(const GeneralAddress& address) const {
  93. return (compare(&address.addr[0], address.family));
  94. }
  95. } // namespace acl
  96. } // namespace isc
  97. namespace {
  98. /// *** Free Function Tests ***
  99. // Test the createMask() function.
  100. TEST(IPFunctionCheck, CreateMask) {
  101. // Invalid arguments should throw.
  102. EXPECT_THROW(createMask(9), isc::OutOfRange);
  103. // Check on all possible 8-bit values.
  104. uint16_t expected = 0xff00;
  105. for (size_t i = 0; i <= 8; ++i, expected >>= 1) {
  106. EXPECT_EQ(static_cast<uint8_t>(expected & 0xff), createMask(i));
  107. }
  108. }
  109. // Test the splitIPAddress() function.
  110. TEST(IPFunctionCheck, SplitIPAddress) {
  111. pair<string, uint32_t> result;
  112. result = splitIPAddress("192.0.2.1");
  113. EXPECT_EQ(string("192.0.2.1"), result.first);
  114. EXPECT_EQ(-1, result.second);
  115. result = splitIPAddress("192.0.2.1/24");
  116. EXPECT_EQ(string("192.0.2.1"), result.first);
  117. EXPECT_EQ(24, result.second);
  118. result = splitIPAddress("2001:db8::/128");
  119. EXPECT_EQ(string("2001:db8::"), result.first);
  120. EXPECT_EQ(128, result.second);
  121. result = splitIPAddress("192.0.2.1/0");
  122. EXPECT_EQ(string("192.0.2.1"), result.first);
  123. EXPECT_EQ(0, result.second);
  124. EXPECT_THROW(splitIPAddress("192.0.2.43/27 "), isc::InvalidParameter);
  125. EXPECT_THROW(splitIPAddress("192.0.2.43/-1"), isc::InvalidParameter);
  126. EXPECT_THROW(splitIPAddress("192.0.2.43//1"), isc::InvalidParameter);
  127. EXPECT_THROW(splitIPAddress("192.0.2.43/1/"), isc::InvalidParameter);
  128. EXPECT_THROW(splitIPAddress("/192.0.2.43/1"), isc::InvalidParameter);
  129. EXPECT_THROW(splitIPAddress("2001:db8::/xxxx"), isc::InvalidParameter);
  130. EXPECT_THROW(splitIPAddress("2001:db8::/32/s"), isc::InvalidParameter);
  131. EXPECT_THROW(splitIPAddress("1/"), isc::InvalidParameter);
  132. EXPECT_THROW(splitIPAddress("/1"), isc::InvalidParameter);
  133. EXPECT_THROW(splitIPAddress(" 1/ "), isc::InvalidParameter);
  134. }
  135. TEST(IPAddress, constructIPv4) {
  136. IPAddress ipaddr(tests::getSockAddr("192.0.2.1"));
  137. const uint8_t expected_data[4] = { 192, 0, 2, 1 };
  138. EXPECT_EQ(AF_INET, ipaddr.getFamily());
  139. EXPECT_EQ(4, ipaddr.getLength());
  140. EXPECT_EQ(0, memcmp(expected_data, ipaddr.getData(), 4));
  141. }
  142. TEST(IPAddress, constructIPv6) {
  143. IPAddress ipaddr(tests::getSockAddr("2001:db8:1234:abcd::53"));
  144. const uint8_t expected_data[16] = { 0x20, 0x01, 0x0d, 0xb8, 0x12, 0x34, 0xab,
  145. 0xcd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. 0x00, 0x53 };
  147. EXPECT_EQ(AF_INET6, ipaddr.getFamily());
  148. EXPECT_EQ(16, ipaddr.getLength());
  149. EXPECT_EQ(0, memcmp(expected_data, ipaddr.getData(), 16));
  150. }
  151. TEST(IPAddress, badConstruct) {
  152. struct sockaddr sa;
  153. sa.sa_family = AF_UNSPEC;
  154. EXPECT_THROW(IPAddress ipaddr(sa), isc::BadValue);
  155. }
  156. // *** IPv4 Tests ***
  157. TEST(IPCheck, V4StringConstructor) {
  158. // Constructor with no prefix length given (32 is assumed).
  159. IPCheck<GeneralAddress> acl1("192.0.2.255");
  160. EXPECT_EQ(32, acl1.getPrefixlen());
  161. EXPECT_EQ(AF_INET, acl1.getFamily());
  162. vector<uint8_t> stored1 = acl1.getAddress();
  163. EXPECT_EQ(IPV4_SIZE, stored1.size());
  164. GeneralAddress expected1(0xc00002ff);
  165. EXPECT_TRUE(expected1.equals(stored1));
  166. // Constructor with valid mask given
  167. IPCheck<GeneralAddress> acl2("192.0.2.0/24");
  168. EXPECT_EQ(24, acl2.getPrefixlen());
  169. EXPECT_EQ(AF_INET, acl2.getFamily());
  170. vector<uint8_t> stored2 = acl2.getAddress();
  171. EXPECT_EQ(IPV4_SIZE, stored2.size());
  172. GeneralAddress expected2(0xc0000200);
  173. EXPECT_TRUE(expected2.equals(stored2));
  174. // More valid masks
  175. IPCheck<GeneralAddress> acl3("192.0.2.1/0");
  176. EXPECT_EQ(0, acl3.getPrefixlen());
  177. EXPECT_EQ(AF_INET, acl3.getFamily());
  178. vector<uint8_t> stored3 = acl3.getAddress();
  179. EXPECT_EQ(IPV4_SIZE, stored3.size());
  180. GeneralAddress expected3(0xc0000201);
  181. EXPECT_TRUE(expected3.equals(stored3));
  182. IPCheck<GeneralAddress> acl4("192.0.2.2/32");
  183. EXPECT_EQ(32, acl4.getPrefixlen());
  184. EXPECT_EQ(AF_INET, acl4.getFamily());
  185. vector<uint8_t> stored4 = acl4.getAddress();
  186. EXPECT_EQ(IPV4_SIZE, stored4.size());
  187. GeneralAddress expected4(0xc0000202);
  188. EXPECT_TRUE(expected4.equals(stored4));
  189. // Any match
  190. IPCheck<GeneralAddress> acl5("any4");
  191. EXPECT_EQ(0, acl5.getPrefixlen());
  192. EXPECT_EQ(AF_INET, acl5.getFamily());
  193. vector<uint8_t> stored5 = acl5.getAddress();
  194. EXPECT_EQ(IPV4_SIZE, stored5.size());
  195. GeneralAddress expected5(0);
  196. EXPECT_TRUE(expected5.equals(stored5));
  197. // Invalid prefix lengths
  198. EXPECT_THROW(IPCheck<GeneralAddress>("192.0.2.0/33"), isc::OutOfRange);
  199. // ... and invalid strings
  200. EXPECT_THROW(IPCheck<GeneralAddress>("192.0.2.0/-1"),
  201. isc::InvalidParameter);
  202. EXPECT_THROW(IPCheck<GeneralAddress>("192.0.2.0/24/3"),
  203. isc::InvalidParameter);
  204. EXPECT_THROW(IPCheck<GeneralAddress>("192.0.2.0/ww"),
  205. isc::InvalidParameter);
  206. EXPECT_THROW(IPCheck<GeneralAddress>("aa.255.255.0/ww"),
  207. isc::InvalidParameter);
  208. }
  209. TEST(IPCheck, V4CopyConstructor) {
  210. IPCheck<GeneralAddress> acl1("192.0.2.1/24");
  211. IPCheck<GeneralAddress> acl2(acl1);
  212. EXPECT_EQ(acl1.getPrefixlen(), acl2.getPrefixlen());
  213. EXPECT_EQ(acl1.getFamily(), acl2.getFamily());
  214. vector<uint8_t> net1 = acl1.getMask();
  215. vector<uint8_t> net2 = acl2.getMask();
  216. EXPECT_EQ(net1.size(), net2.size());
  217. EXPECT_TRUE(equal(net1.begin(), net1.end(), net2.begin()));
  218. net1 = acl1.getAddress();
  219. net2 = acl2.getAddress();
  220. EXPECT_EQ(net1.size(), net2.size());
  221. EXPECT_TRUE(equal(net1.begin(), net1.end(), net2.begin()));
  222. }
  223. TEST(IPCheck, V4AssignmentOperator) {
  224. IPCheck<GeneralAddress> acl1("192.0.2.0/24");
  225. IPCheck<GeneralAddress> acl2("192.0.2.128/25");
  226. acl2 = acl1;
  227. EXPECT_EQ(acl1.getPrefixlen(), acl2.getPrefixlen());
  228. EXPECT_EQ(acl1.getFamily(), acl2.getFamily());
  229. vector<uint8_t> net1 = acl1.getMask();
  230. vector<uint8_t> net2 = acl2.getMask();
  231. EXPECT_EQ(net1.size(), net2.size());
  232. EXPECT_TRUE(equal(net1.begin(), net1.end(), net2.begin()));
  233. net1 = acl1.getAddress();
  234. net2 = acl2.getAddress();
  235. EXPECT_EQ(net1.size(), net2.size());
  236. EXPECT_TRUE(equal(net1.begin(), net1.end(), net2.begin()));
  237. }
  238. // Check that the comparison works - note that "matches" just calls the
  239. // internal compare() code. (Also note that the argument to matches() will be
  240. // automatically converted to the GeneralAddress data type used for the tests
  241. // because of its constructor taking a uint32_t argument.
  242. TEST(IPCheck, V4Compare) {
  243. // Exact address - match if given address matches stored address.
  244. IPCheck<GeneralAddress> acl1("192.0.2.255/32");
  245. EXPECT_TRUE(acl1.matches(0xc00002ff));
  246. EXPECT_FALSE(acl1.matches(0xc00002fe));
  247. EXPECT_FALSE(acl1.matches(0x13457f13));
  248. IPCheck<GeneralAddress> acl2("192.0.2.255/27");
  249. EXPECT_TRUE(acl2.matches(0xc00002ff));
  250. EXPECT_TRUE(acl2.matches(0xc00002fe));
  251. EXPECT_TRUE(acl2.matches(0xc00002ee));
  252. EXPECT_FALSE(acl2.matches(0xc00002de));
  253. EXPECT_FALSE(acl2.matches(0xd00002fe));
  254. EXPECT_FALSE(acl2.matches(0x13457f13));
  255. // Match if "any4" is specified
  256. IPCheck<GeneralAddress> acl3("any4");
  257. EXPECT_TRUE(acl3.matches(0xc00002ff));
  258. EXPECT_TRUE(acl3.matches(0xc00002fe));
  259. EXPECT_TRUE(acl3.matches(0xc00002ee));
  260. EXPECT_TRUE(acl3.matches(0xc00002de));
  261. EXPECT_TRUE(acl3.matches(0xd00002fe));
  262. EXPECT_TRUE(acl3.matches(0x13457f13));
  263. IPCheck<GeneralAddress> acl4("0.0.0.0/0");
  264. EXPECT_TRUE(acl4.matches(0xc00002ff));
  265. EXPECT_TRUE(acl4.matches(0xc00002fe));
  266. EXPECT_TRUE(acl4.matches(0xc00002ee));
  267. EXPECT_TRUE(acl4.matches(0xc00002de));
  268. EXPECT_TRUE(acl4.matches(0xd00002fe));
  269. EXPECT_TRUE(acl4.matches(0x13457f13));
  270. IPCheck<GeneralAddress> acl5("192.0.2.255/0");
  271. EXPECT_TRUE(acl5.matches(0xc00002ff));
  272. EXPECT_TRUE(acl5.matches(0xc00002fe));
  273. EXPECT_TRUE(acl5.matches(0xc00002ee));
  274. EXPECT_TRUE(acl5.matches(0xc00002de));
  275. EXPECT_TRUE(acl5.matches(0xd00002fe));
  276. EXPECT_TRUE(acl5.matches(0x13457f13));
  277. }
  278. // *** IPV6 Tests ***
  279. // Some constants used in the tests
  280. const char* V6ADDR_1_STRING = "2001:0db8:1122:3344:5566:7788:99aa:bbcc";
  281. const uint8_t V6ADDR_1[] = {
  282. 0x20, 0x01, 0x0d, 0xb8, 0x11, 0x22, 0x33, 0x44,
  283. 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc
  284. };
  285. const char* V6ADDR_2_STRING = "2001:0db8::dead:beef";
  286. const uint8_t V6ADDR_2[] = {
  287. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  288. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  289. };
  290. // Identical to V6ADDR_2 to 48 bits
  291. const uint8_t V6ADDR_2_48[] = {
  292. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0xff, 0x66,
  293. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  294. };
  295. // Identical to V6ADDR_2 to 49 bits
  296. const uint8_t V6ADDR_2_49[] = {
  297. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x7f, 0x66,
  298. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  299. };
  300. // Identical to V6ADDR_2 to 50 bits
  301. const uint8_t V6ADDR_2_50[] = {
  302. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x3f, 0x66,
  303. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  304. };
  305. // Identical to V6ADDR_2 to 51 bits
  306. const uint8_t V6ADDR_2_51[] = {
  307. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x1f, 0x66,
  308. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  309. };
  310. // Identical to V6ADDR_2 to 51 bits
  311. const uint8_t V6ADDR_2_52[] = {
  312. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x0f, 0x66,
  313. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef
  314. };
  315. // Identical to V6ADDR_2 to 127 bits
  316. const uint8_t V6ADDR_2_127[] = {
  317. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  318. 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xee
  319. };
  320. const uint8_t V6ADDR_3[] = {
  321. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  322. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  323. };
  324. const uint8_t V6ADDR_4[] = {
  325. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  326. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  327. };
  328. TEST(IPCheck, V6StringConstructor) {
  329. IPCheck<GeneralAddress> acl1(V6ADDR_1_STRING);
  330. vector<uint8_t> address = acl1.getAddress();
  331. EXPECT_EQ(128, acl1.getPrefixlen());
  332. EXPECT_EQ(AF_INET6, acl1.getFamily());
  333. EXPECT_EQ(IPV6_SIZE, address.size());
  334. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_1));
  335. IPCheck<GeneralAddress> acl2(string(V6ADDR_2_STRING) + string("/51"));
  336. address = acl2.getAddress();
  337. EXPECT_EQ(IPV6_SIZE, address.size());
  338. EXPECT_EQ(51, acl2.getPrefixlen());
  339. EXPECT_EQ(AF_INET6, acl2.getFamily());
  340. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_2));
  341. IPCheck<GeneralAddress> acl3(string(V6ADDR_2_STRING) + string("/127"));
  342. address = acl3.getAddress();
  343. EXPECT_EQ(IPV6_SIZE, address.size());
  344. EXPECT_EQ(127, acl3.getPrefixlen());
  345. EXPECT_EQ(AF_INET6, acl3.getFamily());
  346. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_2));
  347. IPCheck<GeneralAddress> acl4("::1");
  348. address = acl4.getAddress();
  349. EXPECT_EQ(IPV6_SIZE, address.size());
  350. EXPECT_EQ(128, acl4.getPrefixlen());
  351. EXPECT_EQ(AF_INET6, acl4.getFamily());
  352. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_3));
  353. // Any match. In these cases, the address should all be zeroes.
  354. IPCheck<GeneralAddress> acl5("any6");
  355. address = acl5.getAddress();
  356. EXPECT_EQ(IPV6_SIZE, address.size());
  357. EXPECT_EQ(0, acl5.getPrefixlen());
  358. EXPECT_EQ(AF_INET6, acl5.getFamily());
  359. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_4));
  360. IPCheck<GeneralAddress> acl6("::/0");
  361. address = acl6.getAddress();
  362. EXPECT_EQ(0, acl6.getPrefixlen());
  363. EXPECT_EQ(AF_INET6, acl6.getFamily());
  364. EXPECT_TRUE(equal(address.begin(), address.end(), V6ADDR_4));
  365. // Some invalid strings
  366. EXPECT_THROW(IPCheck<GeneralAddress>("::1/129"), isc::OutOfRange);
  367. EXPECT_THROW(IPCheck<GeneralAddress>("::1/24/3"), isc::InvalidParameter);
  368. EXPECT_THROW(IPCheck<GeneralAddress>(":::1/24"), isc::InvalidParameter);
  369. EXPECT_THROW(IPCheck<GeneralAddress>("2001:0db8::abcd/ww"),
  370. isc::InvalidParameter);
  371. EXPECT_THROW(IPCheck<GeneralAddress>("2xx1:0db8::abcd/32"),
  372. isc::InvalidParameter);
  373. }
  374. TEST(IPCheck, V6CopyConstructor) {
  375. IPCheck<GeneralAddress> acl1(string(V6ADDR_2_STRING) + string("/52"));
  376. IPCheck<GeneralAddress> acl2(acl1);
  377. vector<uint8_t> acl1_address = acl1.getAddress();
  378. vector<uint8_t> acl2_address = acl1.getAddress();
  379. EXPECT_EQ(sizeof(V6ADDR_1), acl1_address.size());
  380. EXPECT_EQ(acl1_address.size(), acl2_address.size());
  381. EXPECT_TRUE(equal(acl1_address.begin(), acl1_address.end(),
  382. acl2_address.begin()));
  383. EXPECT_EQ(acl1.getPrefixlen(), acl2.getPrefixlen());
  384. vector<uint8_t> acl1_mask = acl1.getMask();
  385. vector<uint8_t> acl2_mask = acl1.getMask();
  386. EXPECT_EQ(sizeof(V6ADDR_1), acl1_mask.size());
  387. EXPECT_EQ(acl1_mask.size(), acl2_mask.size());
  388. EXPECT_TRUE(equal(acl1_mask.begin(), acl1_mask.end(),
  389. acl2_mask.begin()));
  390. }
  391. TEST(IPCheck, V6AssignmentOperator) {
  392. IPCheck<GeneralAddress> acl1(string(V6ADDR_2_STRING) + string("/52"));
  393. IPCheck<GeneralAddress> acl2(string(V6ADDR_1_STRING) + string("/48"));
  394. acl2 = acl1;
  395. vector<uint8_t> acl1_address = acl1.getAddress();
  396. vector<uint8_t> acl2_address = acl2.getAddress();
  397. EXPECT_EQ(sizeof(V6ADDR_1), acl1_address.size());
  398. EXPECT_EQ(acl1_address.size(), acl2_address.size());
  399. EXPECT_TRUE(equal(acl1_address.begin(), acl1_address.end(),
  400. acl2_address.begin()));
  401. EXPECT_EQ(acl1.getPrefixlen(), acl2.getPrefixlen());
  402. vector<uint8_t> acl1_mask = acl1.getMask();
  403. vector<uint8_t> acl2_mask = acl2.getMask();
  404. EXPECT_EQ(sizeof(V6ADDR_1), acl1_mask.size());
  405. EXPECT_EQ(acl1_mask.size(), acl2_mask.size());
  406. EXPECT_TRUE(equal(acl1_mask.begin(), acl1_mask.end(),
  407. acl2_mask.begin()));
  408. }
  409. TEST(IPCheck, V6Compare) {
  410. // Set up some data.
  411. vector<uint8_t> v6addr_2(V6ADDR_2, V6ADDR_2 + IPV6_SIZE);
  412. vector<uint8_t> v6addr_2_48(V6ADDR_2_48, V6ADDR_2_48 + IPV6_SIZE);
  413. vector<uint8_t> v6addr_2_49(V6ADDR_2_49, V6ADDR_2_49 + IPV6_SIZE);
  414. vector<uint8_t> v6addr_2_50(V6ADDR_2_50, V6ADDR_2_50 + IPV6_SIZE);
  415. vector<uint8_t> v6addr_2_51(V6ADDR_2_51, V6ADDR_2_51 + IPV6_SIZE);
  416. vector<uint8_t> v6addr_2_52(V6ADDR_2_52, V6ADDR_2_52 + IPV6_SIZE);
  417. vector<uint8_t> v6addr_2_127(V6ADDR_2_127, V6ADDR_2_127 + IPV6_SIZE);
  418. vector<uint8_t> v6addr_3(V6ADDR_3, V6ADDR_3 + IPV6_SIZE);
  419. // Exact address - match if given address matches stored address.
  420. IPCheck<GeneralAddress> acl1(string(V6ADDR_2_STRING) + string("/128"));
  421. EXPECT_TRUE(acl1.matches(v6addr_2));
  422. EXPECT_FALSE(acl1.matches(v6addr_2_127));
  423. EXPECT_FALSE(acl1.matches(v6addr_2_52));
  424. EXPECT_FALSE(acl1.matches(v6addr_2_51));
  425. EXPECT_FALSE(acl1.matches(v6addr_2_50));
  426. EXPECT_FALSE(acl1.matches(v6addr_2_49));
  427. EXPECT_FALSE(acl1.matches(v6addr_2_48));
  428. EXPECT_FALSE(acl1.matches(v6addr_3));
  429. // Match to various prefixes.
  430. IPCheck<GeneralAddress> acl2(string(V6ADDR_2_STRING) + string("/127"));
  431. EXPECT_TRUE(acl2.matches(v6addr_2));
  432. EXPECT_TRUE(acl2.matches(v6addr_2_127));
  433. EXPECT_FALSE(acl2.matches(v6addr_2_52));
  434. EXPECT_FALSE(acl2.matches(v6addr_2_51));
  435. EXPECT_FALSE(acl2.matches(v6addr_2_50));
  436. EXPECT_FALSE(acl2.matches(v6addr_2_49));
  437. EXPECT_FALSE(acl2.matches(v6addr_2_48));
  438. EXPECT_FALSE(acl2.matches(v6addr_3));
  439. IPCheck<GeneralAddress> acl3(string(V6ADDR_2_STRING) + string("/52"));
  440. EXPECT_TRUE(acl3.matches(v6addr_2));
  441. EXPECT_TRUE(acl3.matches(v6addr_2_127));
  442. EXPECT_TRUE(acl3.matches(v6addr_2_52));
  443. EXPECT_FALSE(acl3.matches(v6addr_2_51));
  444. EXPECT_FALSE(acl3.matches(v6addr_2_50));
  445. EXPECT_FALSE(acl3.matches(v6addr_2_49));
  446. EXPECT_FALSE(acl3.matches(v6addr_2_48));
  447. EXPECT_FALSE(acl3.matches(v6addr_3));
  448. IPCheck<GeneralAddress> acl4(string(V6ADDR_2_STRING) + string("/51"));
  449. EXPECT_TRUE(acl4.matches(v6addr_2));
  450. EXPECT_TRUE(acl4.matches(v6addr_2_127));
  451. EXPECT_TRUE(acl4.matches(v6addr_2_52));
  452. EXPECT_TRUE(acl4.matches(v6addr_2_51));
  453. EXPECT_FALSE(acl4.matches(v6addr_2_50));
  454. EXPECT_FALSE(acl4.matches(v6addr_2_49));
  455. EXPECT_FALSE(acl4.matches(v6addr_2_48));
  456. EXPECT_FALSE(acl4.matches(v6addr_3));
  457. IPCheck<GeneralAddress> acl5(string(V6ADDR_2_STRING) + string("/50"));
  458. EXPECT_TRUE(acl5.matches(v6addr_2));
  459. EXPECT_TRUE(acl5.matches(v6addr_2_127));
  460. EXPECT_TRUE(acl5.matches(v6addr_2_52));
  461. EXPECT_TRUE(acl5.matches(v6addr_2_51));
  462. EXPECT_TRUE(acl5.matches(v6addr_2_50));
  463. EXPECT_FALSE(acl5.matches(v6addr_2_49));
  464. EXPECT_FALSE(acl5.matches(v6addr_2_48));
  465. EXPECT_FALSE(acl5.matches(v6addr_3));
  466. IPCheck<GeneralAddress> acl6(string(V6ADDR_2_STRING) + string("/0"));
  467. EXPECT_TRUE(acl6.matches(v6addr_2));
  468. EXPECT_TRUE(acl6.matches(v6addr_2_127));
  469. EXPECT_TRUE(acl6.matches(v6addr_2_52));
  470. EXPECT_TRUE(acl6.matches(v6addr_2_51));
  471. EXPECT_TRUE(acl6.matches(v6addr_2_50));
  472. EXPECT_TRUE(acl6.matches(v6addr_2_49));
  473. EXPECT_TRUE(acl6.matches(v6addr_2_48));
  474. EXPECT_TRUE(acl6.matches(v6addr_3));
  475. // Match on any address
  476. IPCheck<GeneralAddress> acl7("any6");
  477. EXPECT_TRUE(acl7.matches(v6addr_2));
  478. EXPECT_TRUE(acl7.matches(v6addr_2_127));
  479. EXPECT_TRUE(acl7.matches(v6addr_2_52));
  480. EXPECT_TRUE(acl7.matches(v6addr_2_51));
  481. EXPECT_TRUE(acl7.matches(v6addr_2_50));
  482. EXPECT_TRUE(acl7.matches(v6addr_2_49));
  483. EXPECT_TRUE(acl7.matches(v6addr_2_48));
  484. }
  485. // *** Mixed-mode tests - mainly to check that no exception is thrown ***
  486. TEST(IPCheck, MixedMode) {
  487. // ACL has a V4 address specified, check against a V6 address.
  488. IPCheck<GeneralAddress> acl1("192.0.2.255/24");
  489. GeneralAddress test1(vector<uint8_t>(V6ADDR_1, V6ADDR_1 + IPV6_SIZE));
  490. EXPECT_NO_THROW(acl1.matches(test1));
  491. EXPECT_FALSE(acl1.matches(test1));
  492. // Now the reverse - the ACL is specified with a V6 address.
  493. IPCheck<GeneralAddress> acl2(V6ADDR_2_STRING);
  494. GeneralAddress test2(0x12345678);
  495. EXPECT_FALSE(acl2.matches(test2));
  496. // Ensure only a V4 address matches "any4".
  497. IPCheck<GeneralAddress> acl3("any4");
  498. EXPECT_FALSE(acl3.matches(test1));
  499. EXPECT_TRUE(acl3.matches(test2));
  500. // ... and check the reverse
  501. IPCheck<GeneralAddress> acl4("any6");
  502. EXPECT_TRUE(acl4.matches(test1));
  503. EXPECT_FALSE(acl4.matches(test2));
  504. // Check where the bit pattern of an IPv4 address matches that of an IPv6
  505. // one.
  506. IPCheck<GeneralAddress> acl5("2001:db8::/32");
  507. GeneralAddress test5(0x20010db8);
  508. EXPECT_FALSE(acl5.matches(test5));
  509. // ... and where the reverse is true. (2001:db8 corresponds to 32.1.13.184).
  510. IPCheck<GeneralAddress> acl6("32.1.13.184");
  511. GeneralAddress test6(vector<uint8_t>(V6ADDR_1, V6ADDR_1 + IPV6_SIZE));
  512. EXPECT_FALSE(acl6.matches(test6));
  513. }
  514. } // Unnamed namespace