blob: cab1dbba7229b64571a44f41d065c662f7f30afc [file] [log] [blame]
Junxiao Shidbe71732014-02-21 22:23:28 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shia49a1ab2016-07-15 18:24:36 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi35353962015-01-08 09:13:47 -07004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shie368d992014-12-02 23:44:31 -070024 */
Junxiao Shidbe71732014-02-21 22:23:28 -070025
26#include "table/measurements-accessor.hpp"
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070027#include "fw/strategy.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070028
Junxiao Shid9ee45c2014-02-27 15:38:11 -070029#include "tests/test-common.hpp"
Junxiao Shi1c93cae2014-12-09 22:52:17 -070030#include "../fw/dummy-strategy.hpp"
Junxiao Shia49a1ab2016-07-15 18:24:36 +000031#include "../fw/install-strategy.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070032
33namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Junxiao Shidbe71732014-02-21 22:23:28 -070035
Junxiao Shi35353962015-01-08 09:13:47 -070036using measurements::Entry;
Junxiao Shidbe71732014-02-21 22:23:28 -070037
Junxiao Shi1c93cae2014-12-09 22:52:17 -070038class MeasurementsAccessorTestStrategy : public DummyStrategy
Junxiao Shidbe71732014-02-21 22:23:28 -070039{
40public:
Junxiao Shi7bb01512014-03-05 21:34:09 -070041 MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi1c93cae2014-12-09 22:52:17 -070042 : DummyStrategy(forwarder, name)
Junxiao Shidbe71732014-02-21 22:23:28 -070043 {
44 }
Junxiao Shic041ca32014-02-25 20:01:15 -070045
Junxiao Shidbe71732014-02-21 22:23:28 -070046 virtual
47 ~MeasurementsAccessorTestStrategy()
Junxiao Shidbe71732014-02-21 22:23:28 -070048 {
49 }
50
Junxiao Shidbe71732014-02-21 22:23:28 -070051public: // accessors
52 MeasurementsAccessor&
Junxiao Shi35353962015-01-08 09:13:47 -070053 getMeasurementsAccessor()
Junxiao Shidbe71732014-02-21 22:23:28 -070054 {
55 return this->getMeasurements();
56 }
57};
58
Junxiao Shi35353962015-01-08 09:13:47 -070059class MeasurementsAccessorFixture : public BaseFixture
Junxiao Shidbe71732014-02-21 22:23:28 -070060{
Junxiao Shi35353962015-01-08 09:13:47 -070061protected:
62 MeasurementsAccessorFixture()
Junxiao Shia49a1ab2016-07-15 18:24:36 +000063 : strategy1(install<MeasurementsAccessorTestStrategy>(forwarder, "ndn:/strategy1"))
64 , strategy2(install<MeasurementsAccessorTestStrategy>(forwarder, "ndn:/strategy2"))
Junxiao Shi35353962015-01-08 09:13:47 -070065 , measurements(forwarder.getMeasurements())
Junxiao Shia49a1ab2016-07-15 18:24:36 +000066 , accessor1(strategy1.getMeasurementsAccessor())
67 , accessor2(strategy2.getMeasurementsAccessor())
Junxiao Shi35353962015-01-08 09:13:47 -070068 {
69 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
Junxiao Shia49a1ab2016-07-15 18:24:36 +000070 strategyChoice.insert("/" , strategy1.getName());
71 strategyChoice.insert("/A" , strategy2.getName());
72 strategyChoice.insert("/A/B", strategy1.getName());
Junxiao Shi35353962015-01-08 09:13:47 -070073 }
74
75protected:
Junxiao Shic041ca32014-02-25 20:01:15 -070076 Forwarder forwarder;
Junxiao Shia49a1ab2016-07-15 18:24:36 +000077 MeasurementsAccessorTestStrategy& strategy1;
78 MeasurementsAccessorTestStrategy& strategy2;
Junxiao Shi35353962015-01-08 09:13:47 -070079 Measurements& measurements;
80 MeasurementsAccessor& accessor1;
81 MeasurementsAccessor& accessor2;
82};
Junxiao Shic041ca32014-02-25 20:01:15 -070083
Junxiao Shi35353962015-01-08 09:13:47 -070084BOOST_FIXTURE_TEST_SUITE(TableMeasurementsAccessor, MeasurementsAccessorFixture)
Junxiao Shic041ca32014-02-25 20:01:15 -070085
Junxiao Shi35353962015-01-08 09:13:47 -070086BOOST_AUTO_TEST_CASE(Get)
87{
88 BOOST_CHECK(accessor1.get("/" ) != nullptr);
89 BOOST_CHECK(accessor1.get("/A" ) == nullptr);
90 BOOST_CHECK(accessor1.get("/A/B" ) != nullptr);
91 BOOST_CHECK(accessor1.get("/A/B/C") != nullptr);
92 BOOST_CHECK(accessor1.get("/A/D" ) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -070093
Junxiao Shi35353962015-01-08 09:13:47 -070094 BOOST_CHECK(accessor2.get("/" ) == nullptr);
95 BOOST_CHECK(accessor2.get("/A" ) != nullptr);
96 BOOST_CHECK(accessor2.get("/A/B" ) == nullptr);
97 BOOST_CHECK(accessor2.get("/A/B/C") == nullptr);
98 BOOST_CHECK(accessor2.get("/A/D" ) != nullptr);
99}
Junxiao Shic041ca32014-02-25 20:01:15 -0700100
Junxiao Shi35353962015-01-08 09:13:47 -0700101BOOST_AUTO_TEST_CASE(GetParent)
102{
103 shared_ptr<Entry> entryRoot = measurements.get("/");
104 BOOST_CHECK(accessor1.getParent(*entryRoot) == nullptr);
105 BOOST_CHECK(accessor2.getParent(*entryRoot) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700106
Junxiao Shi35353962015-01-08 09:13:47 -0700107 shared_ptr<Entry> entryABC = measurements.get("/A/B/C");
108 BOOST_CHECK(accessor1.getParent(*entryABC) != nullptr);
109 BOOST_CHECK(accessor2.getParent(*entryABC) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700110
Junxiao Shi35353962015-01-08 09:13:47 -0700111 shared_ptr<Entry> entryAB = measurements.get("/A/B");
112 BOOST_CHECK(accessor1.getParent(*entryAB) == nullptr);
113 // whether accessor2.getParent(*entryAB) can return an Entry is undefined,
114 // because strategy2 shouldn't obtain entryAB in the first place
115}
Junxiao Shic041ca32014-02-25 20:01:15 -0700116
Junxiao Shi35353962015-01-08 09:13:47 -0700117BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
118{
119 shared_ptr<Interest> interest = makeInterest("/A/B/C");
120 shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first;
121
122 measurements.get("/");
123 BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") != nullptr);
124 BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) != nullptr);
125
126 measurements.get("/A");
127 BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") == nullptr);
128 BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) == nullptr);
129}
130
131BOOST_AUTO_TEST_CASE(FindExactMatch)
132{
133 measurements.get("/");
134 measurements.get("/A");
135 measurements.get("/A/B");
136 measurements.get("/A/B/C");
137 measurements.get("/A/D");
138
139 BOOST_CHECK(accessor1.findExactMatch("/" ) != nullptr);
140 BOOST_CHECK(accessor1.findExactMatch("/A" ) == nullptr);
141 BOOST_CHECK(accessor1.findExactMatch("/A/B" ) != nullptr);
142 BOOST_CHECK(accessor1.findExactMatch("/A/B/C") != nullptr);
143 BOOST_CHECK(accessor1.findExactMatch("/A/D" ) == nullptr);
144 BOOST_CHECK(accessor1.findExactMatch("/A/E" ) == nullptr);
145 BOOST_CHECK(accessor1.findExactMatch("/F" ) == nullptr);
146
147 BOOST_CHECK(accessor2.findExactMatch("/" ) == nullptr);
148 BOOST_CHECK(accessor2.findExactMatch("/A" ) != nullptr);
149 BOOST_CHECK(accessor2.findExactMatch("/A/B" ) == nullptr);
150 BOOST_CHECK(accessor2.findExactMatch("/A/B/C") == nullptr);
151 BOOST_CHECK(accessor2.findExactMatch("/A/D" ) != nullptr);
152 BOOST_CHECK(accessor2.findExactMatch("/A/E" ) == nullptr);
153 BOOST_CHECK(accessor2.findExactMatch("/F" ) == nullptr);
Junxiao Shidbe71732014-02-21 22:23:28 -0700154}
155
156BOOST_AUTO_TEST_SUITE_END()
157
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700158} // namespace tests
Junxiao Shidbe71732014-02-21 22:23:28 -0700159} // namespace nfd