Ju Pan | 2feb459 | 2019-09-16 20:56:38 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2019, 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 "fw/forwarder.hpp" |
| 27 | #include "common/global.hpp" |
| 28 | |
| 29 | #include "tests/test-common.hpp" |
| 30 | #include "tests/daemon/global-io-fixture.hpp" |
| 31 | #include "tests/daemon/face/dummy-face.hpp" |
| 32 | #include "choose-strategy.hpp" |
| 33 | #include "dummy-strategy.hpp" |
| 34 | |
| 35 | namespace nfd { |
| 36 | namespace tests { |
| 37 | |
| 38 | class StrategyNewNextHopFixture : public GlobalIoTimeFixture |
| 39 | { |
| 40 | protected: |
| 41 | template<typename ...Args> |
| 42 | shared_ptr<DummyFace> |
| 43 | addFace(Args&&... args) |
| 44 | { |
| 45 | auto face = make_shared<DummyFace>(std::forward<Args>(args)...); |
| 46 | faceTable.add(face); |
| 47 | return face; |
| 48 | } |
| 49 | |
| 50 | protected: |
| 51 | FaceTable faceTable; |
| 52 | Forwarder forwarder{faceTable}; |
| 53 | }; |
| 54 | |
| 55 | BOOST_AUTO_TEST_SUITE(Fw) |
| 56 | BOOST_FIXTURE_TEST_SUITE(TestStrategyNewNextHop, StrategyNewNextHopFixture) |
| 57 | |
| 58 | BOOST_AUTO_TEST_CASE(AfterNewNextHop1) |
| 59 | { |
| 60 | auto face1 = addFace(); |
| 61 | auto face2 = addFace(); |
| 62 | auto face3 = addFace(); |
| 63 | |
| 64 | DummyStrategy& strategy = choose<DummyStrategy>(forwarder, "/A", DummyStrategy::getStrategyName()); |
| 65 | StrategyChoice& sc = forwarder.getStrategyChoice(); |
| 66 | sc.insert(Name("/A/B"), strategy.getStrategyName()); |
| 67 | sc.insert(Name("/A/B/C"), strategy.getStrategyName()); |
| 68 | |
| 69 | strategy.enableNewNextHopTrigger(true); |
| 70 | |
| 71 | Fib& fib = forwarder.getFib(); |
| 72 | Pit& pit = forwarder.getPit(); |
| 73 | |
| 74 | // fib: "/", "/A/B" |
| 75 | fib::Entry* entry = fib.insert("/").first; |
| 76 | fib.addOrUpdateNextHop(*entry, *face1, 0); |
| 77 | entry = fib.insert("/A/B").first; |
| 78 | fib.addOrUpdateNextHop(*entry, *face1, 0); |
| 79 | |
| 80 | // pit: "/A", "/A/B/C" |
| 81 | auto interest1 = makeInterest("/A"); |
| 82 | shared_ptr<pit::Entry> pit1 = pit.insert(*interest1).first; |
| 83 | pit1->insertOrUpdateInRecord(*face3, *interest1); |
| 84 | auto interest2 = makeInterest("/A/B/C"); |
| 85 | shared_ptr<pit::Entry> pit2 = pit.insert(*interest2).first; |
| 86 | pit2->insertOrUpdateInRecord(*face3, *interest2); |
| 87 | // new nexthop for "/" |
| 88 | entry = fib.insert("/").first; |
| 89 | fib.addOrUpdateNextHop(*entry, *face2, 0); |
| 90 | |
| 91 | // /A --> triggered, /A/B --> not triggered, /A/B/C --> not triggered |
| 92 | BOOST_REQUIRE_EQUAL(strategy.afterNewNextHopCalls.size(), 1); |
| 93 | BOOST_CHECK_EQUAL(strategy.afterNewNextHopCalls[0], Name("/A")); |
| 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE(AfterNewNextHop2) |
| 97 | { |
| 98 | auto face1 = addFace(); |
| 99 | auto face2 = addFace(); |
| 100 | auto face3 = addFace(); |
| 101 | |
| 102 | DummyStrategy& strategy = choose<DummyStrategy>(forwarder, "/A", DummyStrategy::getStrategyName()); |
| 103 | StrategyChoice& sc = forwarder.getStrategyChoice(); |
| 104 | sc.insert(Name("/A/B"), strategy.getStrategyName()); |
| 105 | sc.insert(Name("/A/B/C"), strategy.getStrategyName()); |
| 106 | |
| 107 | strategy.enableNewNextHopTrigger(true); |
| 108 | |
| 109 | Fib& fib = forwarder.getFib(); |
| 110 | Pit& pit = forwarder.getPit(); |
| 111 | |
| 112 | // fib: "/", "/A/B" |
| 113 | fib::Entry* entry = fib.insert("/").first; |
| 114 | fib.addOrUpdateNextHop(*entry, *face1, 0); |
| 115 | entry = fib.insert("/A/B").first; |
| 116 | fib.addOrUpdateNextHop(*entry, *face1, 0); |
| 117 | |
| 118 | // pit: "/A", "/A/B/C" |
| 119 | auto interest1 = makeInterest("/A"); |
| 120 | shared_ptr<pit::Entry> pit1 = pit.insert(*interest1).first; |
| 121 | pit1->insertOrUpdateInRecord(*face3, *interest1); |
| 122 | auto interest2 = makeInterest("/A/B"); |
| 123 | shared_ptr<pit::Entry> pit2 = pit.insert(*interest2).first; |
| 124 | pit2->insertOrUpdateInRecord(*face3, *interest2); |
| 125 | // new nexthop for "/" |
| 126 | entry = fib.insert("/").first; |
| 127 | fib.addOrUpdateNextHop(*entry, *face2, 0); |
| 128 | |
| 129 | // /A --> triggered, /A/B --> not triggered, /A/B/C --> not triggered |
| 130 | BOOST_REQUIRE_EQUAL(strategy.afterNewNextHopCalls.size(), 1); |
| 131 | BOOST_CHECK_EQUAL(strategy.afterNewNextHopCalls[0], Name("/A")); |
| 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(DisableTrigger) |
| 135 | { |
| 136 | auto face1 = addFace(); |
| 137 | auto face2 = addFace(); |
| 138 | auto face3 = addFace(); |
| 139 | |
| 140 | DummyStrategy& strategy = choose<DummyStrategy>(forwarder, "/A", DummyStrategy::getStrategyName()); |
| 141 | strategy.enableNewNextHopTrigger(false); |
| 142 | |
| 143 | Fib& fib = forwarder.getFib(); |
| 144 | Pit& pit = forwarder.getPit(); |
| 145 | |
| 146 | fib::Entry* entry = fib.insert("/").first; |
| 147 | fib.addOrUpdateNextHop(*entry, *face1, 0); |
| 148 | |
| 149 | auto interest1 = makeInterest("/A"); |
| 150 | shared_ptr<pit::Entry> pit1 = pit.insert(*interest1).first; |
| 151 | pit1->insertOrUpdateInRecord(*face3, *interest1); |
| 152 | // new nexthop for "/A" |
| 153 | entry = fib.insert("/A").first; |
| 154 | fib.addOrUpdateNextHop(*entry, *face2, 0); |
| 155 | |
| 156 | BOOST_CHECK_EQUAL(strategy.afterNewNextHopCalls.size(), 0); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyNewNextHop |
| 160 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 161 | |
| 162 | } // namespace tests |
| 163 | } // namespace nfd |