susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "core/network-interface-predicate.hpp" |
| 27 | #include "core/network-interface.hpp" |
| 28 | |
| 29 | #include "tests/test-common.hpp" |
| 30 | |
| 31 | #include <boost/property_tree/info_parser.hpp> |
| 32 | #include <sstream> |
| 33 | |
| 34 | namespace nfd { |
| 35 | namespace tests { |
| 36 | |
| 37 | class NetworkInterfacePredicateFixture : public BaseFixture |
| 38 | { |
| 39 | protected: |
| 40 | NetworkInterfacePredicateFixture() |
| 41 | { |
| 42 | using namespace boost::asio::ip; |
| 43 | interfaces.push_back( |
| 44 | NetworkInterfaceInfo{0, "eth0", |
| 45 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 46 | {address_v4::from_string("129.82.100.1")}, |
| 47 | {}, |
| 48 | address_v4::from_string("129.82.255.255"), |
| 49 | IFF_UP}); |
| 50 | interfaces.push_back( |
| 51 | NetworkInterfaceInfo{1, "eth1", |
| 52 | ethernet::Address::fromString("3e:15:c2:8b:65:01"), |
| 53 | {address_v4::from_string("192.168.2.1")}, |
| 54 | {}, |
| 55 | address_v4::from_string("192.168.2.255"), |
| 56 | IFF_UP}); |
| 57 | interfaces.push_back( |
| 58 | NetworkInterfaceInfo{2, "eth2", |
| 59 | ethernet::Address::fromString("3e:15:c2:8b:65:02"), |
| 60 | {address_v4::from_string("198.51.100.1")}, |
| 61 | {address_v6::from_string("2001:db8::1")}, |
| 62 | address_v4::from_string("198.51.100.255"), |
| 63 | IFF_MULTICAST | IFF_BROADCAST | IFF_UP}); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | parseConfig(const std::string& config) |
| 68 | { |
| 69 | std::istringstream input(config); |
| 70 | boost::property_tree::ptree ptree; |
| 71 | boost::property_tree::read_info(input, ptree); |
| 72 | |
| 73 | for (const auto& i : ptree) { |
| 74 | if (i.first == "whitelist") { |
| 75 | predicate.parseWhitelist(i.second); |
| 76 | } |
| 77 | else if (i.first == "blacklist") { |
| 78 | predicate.parseBlacklist(i.second); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | protected: |
| 84 | NetworkInterfacePredicate predicate; |
| 85 | std::vector<NetworkInterfaceInfo> interfaces; |
| 86 | }; |
| 87 | |
| 88 | BOOST_FIXTURE_TEST_SUITE(TestNetworkInterfacePredicate, NetworkInterfacePredicateFixture) |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(Default) |
| 91 | { |
| 92 | parseConfig(""); |
| 93 | |
| 94 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), true); |
| 95 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), true); |
| 96 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), true); |
| 97 | } |
| 98 | |
| 99 | BOOST_AUTO_TEST_CASE(EmptyWhitelist) |
| 100 | { |
| 101 | parseConfig("whitelist\n" |
| 102 | "{\n" |
| 103 | "}"); |
| 104 | |
| 105 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), false); |
| 106 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), false); |
| 107 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), false); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(WildcardBlacklist) |
| 111 | { |
| 112 | parseConfig("blacklist\n" |
| 113 | "{\n" |
| 114 | " *\n" |
| 115 | "}"); |
| 116 | |
| 117 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), false); |
| 118 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), false); |
| 119 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), false); |
| 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(IfnameWhitelist) |
| 123 | { |
| 124 | parseConfig("whitelist\n" |
| 125 | "{\n" |
| 126 | " ifname eth0\n" |
| 127 | " ifname eth1\n" |
| 128 | "}"); |
| 129 | |
| 130 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), true); |
| 131 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), true); |
| 132 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), false); |
| 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_CASE(IfnameBlacklist) |
| 136 | { |
| 137 | parseConfig("blacklist\n" |
| 138 | "{\n" |
| 139 | " ifname eth0\n" |
| 140 | " ifname eth1\n" |
| 141 | "}"); |
| 142 | |
| 143 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), false); |
| 144 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), false); |
| 145 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), true); |
| 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_CASE(IfnameMalformed) |
| 149 | { |
| 150 | BOOST_CHECK_THROW( |
| 151 | parseConfig("whitelist\n" |
| 152 | "{\n" |
| 153 | " ifname\n" |
| 154 | "}"), |
| 155 | ConfigFile::Error); |
| 156 | } |
| 157 | |
| 158 | BOOST_AUTO_TEST_CASE(EtherWhitelist) |
| 159 | { |
| 160 | parseConfig("whitelist\n" |
| 161 | "{\n" |
| 162 | " ether 3e:15:c2:8b:65:00\n" |
| 163 | " ether 3e:15:c2:8b:65:01\n" |
| 164 | "}"); |
| 165 | |
| 166 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), true); |
| 167 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), true); |
| 168 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), false); |
| 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_CASE(EtherBlacklist) |
| 172 | { |
| 173 | parseConfig("blacklist\n" |
| 174 | "{\n" |
| 175 | " ether 3e:15:c2:8b:65:00\n" |
| 176 | " ether 3e:15:c2:8b:65:01\n" |
| 177 | "}"); |
| 178 | |
| 179 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), false); |
| 180 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), false); |
| 181 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), true); |
| 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(EtherMalformed) |
| 185 | { |
| 186 | BOOST_CHECK_THROW( |
| 187 | parseConfig("blacklist\n" |
| 188 | "{\n" |
| 189 | " ether foo\n" |
| 190 | "}"), |
| 191 | ConfigFile::Error); |
| 192 | } |
| 193 | |
| 194 | BOOST_AUTO_TEST_CASE(SubnetWhitelist) |
| 195 | { |
| 196 | parseConfig("whitelist\n" |
| 197 | "{\n" |
| 198 | " subnet 192.168.0.0/16\n" |
| 199 | "}"); |
| 200 | |
| 201 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), false); |
| 202 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), true); |
| 203 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), false); |
| 204 | } |
| 205 | |
| 206 | BOOST_AUTO_TEST_CASE(SubnetBlacklist) |
| 207 | { |
| 208 | parseConfig("blacklist\n" |
| 209 | "{\n" |
| 210 | " subnet 192.168.0.0/16\n" |
| 211 | "}"); |
| 212 | |
| 213 | BOOST_CHECK_EQUAL(predicate(interfaces[0]), true); |
| 214 | BOOST_CHECK_EQUAL(predicate(interfaces[1]), false); |
| 215 | BOOST_CHECK_EQUAL(predicate(interfaces[2]), true); |
| 216 | } |
| 217 | |
| 218 | BOOST_AUTO_TEST_CASE(SubnetMalformed) |
| 219 | { |
| 220 | BOOST_CHECK_THROW( |
| 221 | parseConfig("blacklist\n" |
| 222 | "{\n" |
| 223 | " subnet 266.0.0.91/\n" |
| 224 | "}"), |
| 225 | ConfigFile::Error); |
| 226 | } |
| 227 | |
| 228 | BOOST_AUTO_TEST_SUITE_END() // TestNetworkInterfacePredicate |
| 229 | |
| 230 | } // namespace tests |
| 231 | } // namespace nfd |