blob: 98a39c34502ef2f6c9abdad50cb1145b389dd34a [file] [log] [blame]
Junxiao Shidbe71732014-02-21 22:23:28 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi35353962015-01-08 09:13:47 -07003 * Copyright (c) 2014-2015, 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.
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 Shidbe71732014-02-21 22:23:28 -070031
32namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Junxiao Shidbe71732014-02-21 22:23:28 -070034
Junxiao Shi35353962015-01-08 09:13:47 -070035using measurements::Entry;
Junxiao Shidbe71732014-02-21 22:23:28 -070036
Junxiao Shi1c93cae2014-12-09 22:52:17 -070037class MeasurementsAccessorTestStrategy : public DummyStrategy
Junxiao Shidbe71732014-02-21 22:23:28 -070038{
39public:
Junxiao Shi7bb01512014-03-05 21:34:09 -070040 MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi1c93cae2014-12-09 22:52:17 -070041 : DummyStrategy(forwarder, name)
Junxiao Shidbe71732014-02-21 22:23:28 -070042 {
43 }
Junxiao Shic041ca32014-02-25 20:01:15 -070044
Junxiao Shidbe71732014-02-21 22:23:28 -070045 virtual
46 ~MeasurementsAccessorTestStrategy()
Junxiao Shidbe71732014-02-21 22:23:28 -070047 {
48 }
49
Junxiao Shidbe71732014-02-21 22:23:28 -070050public: // accessors
51 MeasurementsAccessor&
Junxiao Shi35353962015-01-08 09:13:47 -070052 getMeasurementsAccessor()
Junxiao Shidbe71732014-02-21 22:23:28 -070053 {
54 return this->getMeasurements();
55 }
56};
57
Junxiao Shi35353962015-01-08 09:13:47 -070058class MeasurementsAccessorFixture : public BaseFixture
Junxiao Shidbe71732014-02-21 22:23:28 -070059{
Junxiao Shi35353962015-01-08 09:13:47 -070060protected:
61 MeasurementsAccessorFixture()
62 : strategy1(make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy1"))
63 , strategy2(make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy2"))
64 , measurements(forwarder.getMeasurements())
65 , accessor1(strategy1->getMeasurementsAccessor())
66 , accessor2(strategy2->getMeasurementsAccessor())
67 {
68 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
69 strategyChoice.install(strategy1);
70 strategyChoice.install(strategy2);
71 strategyChoice.insert("/" , strategy1->getName());
72 strategyChoice.insert("/A" , strategy2->getName());
73 strategyChoice.insert("/A/B", strategy1->getName());
74 }
75
76protected:
Junxiao Shic041ca32014-02-25 20:01:15 -070077 Forwarder forwarder;
Junxiao Shi35353962015-01-08 09:13:47 -070078 shared_ptr<MeasurementsAccessorTestStrategy> strategy1;
79 shared_ptr<MeasurementsAccessorTestStrategy> strategy2;
80 Measurements& measurements;
81 MeasurementsAccessor& accessor1;
82 MeasurementsAccessor& accessor2;
83};
Junxiao Shic041ca32014-02-25 20:01:15 -070084
Junxiao Shi35353962015-01-08 09:13:47 -070085BOOST_FIXTURE_TEST_SUITE(TableMeasurementsAccessor, MeasurementsAccessorFixture)
Junxiao Shic041ca32014-02-25 20:01:15 -070086
Junxiao Shi35353962015-01-08 09:13:47 -070087BOOST_AUTO_TEST_CASE(Get)
88{
89 BOOST_CHECK(accessor1.get("/" ) != nullptr);
90 BOOST_CHECK(accessor1.get("/A" ) == nullptr);
91 BOOST_CHECK(accessor1.get("/A/B" ) != nullptr);
92 BOOST_CHECK(accessor1.get("/A/B/C") != nullptr);
93 BOOST_CHECK(accessor1.get("/A/D" ) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -070094
Junxiao Shi35353962015-01-08 09:13:47 -070095 BOOST_CHECK(accessor2.get("/" ) == nullptr);
96 BOOST_CHECK(accessor2.get("/A" ) != nullptr);
97 BOOST_CHECK(accessor2.get("/A/B" ) == nullptr);
98 BOOST_CHECK(accessor2.get("/A/B/C") == nullptr);
99 BOOST_CHECK(accessor2.get("/A/D" ) != nullptr);
100}
Junxiao Shic041ca32014-02-25 20:01:15 -0700101
Junxiao Shi35353962015-01-08 09:13:47 -0700102BOOST_AUTO_TEST_CASE(GetParent)
103{
104 shared_ptr<Entry> entryRoot = measurements.get("/");
105 BOOST_CHECK(accessor1.getParent(*entryRoot) == nullptr);
106 BOOST_CHECK(accessor2.getParent(*entryRoot) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700107
Junxiao Shi35353962015-01-08 09:13:47 -0700108 shared_ptr<Entry> entryABC = measurements.get("/A/B/C");
109 BOOST_CHECK(accessor1.getParent(*entryABC) != nullptr);
110 BOOST_CHECK(accessor2.getParent(*entryABC) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700111
Junxiao Shi35353962015-01-08 09:13:47 -0700112 shared_ptr<Entry> entryAB = measurements.get("/A/B");
113 BOOST_CHECK(accessor1.getParent(*entryAB) == nullptr);
114 // whether accessor2.getParent(*entryAB) can return an Entry is undefined,
115 // because strategy2 shouldn't obtain entryAB in the first place
116}
Junxiao Shic041ca32014-02-25 20:01:15 -0700117
Junxiao Shi35353962015-01-08 09:13:47 -0700118BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
119{
120 shared_ptr<Interest> interest = makeInterest("/A/B/C");
121 shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first;
122
123 measurements.get("/");
124 BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") != nullptr);
125 BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) != nullptr);
126
127 measurements.get("/A");
128 BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") == nullptr);
129 BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) == nullptr);
130}
131
132BOOST_AUTO_TEST_CASE(FindExactMatch)
133{
134 measurements.get("/");
135 measurements.get("/A");
136 measurements.get("/A/B");
137 measurements.get("/A/B/C");
138 measurements.get("/A/D");
139
140 BOOST_CHECK(accessor1.findExactMatch("/" ) != nullptr);
141 BOOST_CHECK(accessor1.findExactMatch("/A" ) == nullptr);
142 BOOST_CHECK(accessor1.findExactMatch("/A/B" ) != nullptr);
143 BOOST_CHECK(accessor1.findExactMatch("/A/B/C") != nullptr);
144 BOOST_CHECK(accessor1.findExactMatch("/A/D" ) == nullptr);
145 BOOST_CHECK(accessor1.findExactMatch("/A/E" ) == nullptr);
146 BOOST_CHECK(accessor1.findExactMatch("/F" ) == nullptr);
147
148 BOOST_CHECK(accessor2.findExactMatch("/" ) == nullptr);
149 BOOST_CHECK(accessor2.findExactMatch("/A" ) != nullptr);
150 BOOST_CHECK(accessor2.findExactMatch("/A/B" ) == nullptr);
151 BOOST_CHECK(accessor2.findExactMatch("/A/B/C") == nullptr);
152 BOOST_CHECK(accessor2.findExactMatch("/A/D" ) != nullptr);
153 BOOST_CHECK(accessor2.findExactMatch("/A/E" ) == nullptr);
154 BOOST_CHECK(accessor2.findExactMatch("/F" ) == nullptr);
Junxiao Shidbe71732014-02-21 22:23:28 -0700155}
156
157BOOST_AUTO_TEST_SUITE_END()
158
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700159} // namespace tests
Junxiao Shidbe71732014-02-21 22:23:28 -0700160} // namespace nfd