blob: 292c5a6be6c03e6c1a694f1e0e1f0d0b1048b34a [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 Shi80f9fcd2016-07-23 02:48:36 +000034namespace measurements {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
Junxiao Shidbe71732014-02-21 22:23:28 -070036
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000037using namespace nfd::tests;
Junxiao Shidbe71732014-02-21 22:23:28 -070038
Junxiao Shi1c93cae2014-12-09 22:52:17 -070039class MeasurementsAccessorTestStrategy : public DummyStrategy
Junxiao Shidbe71732014-02-21 22:23:28 -070040{
41public:
Junxiao Shi7bb01512014-03-05 21:34:09 -070042 MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi1c93cae2014-12-09 22:52:17 -070043 : DummyStrategy(forwarder, name)
Junxiao Shidbe71732014-02-21 22:23:28 -070044 {
45 }
Junxiao Shic041ca32014-02-25 20:01:15 -070046
Junxiao Shidbe71732014-02-21 22:23:28 -070047 virtual
48 ~MeasurementsAccessorTestStrategy()
Junxiao Shidbe71732014-02-21 22:23:28 -070049 {
50 }
51
Junxiao Shidbe71732014-02-21 22:23:28 -070052public: // accessors
53 MeasurementsAccessor&
Junxiao Shi35353962015-01-08 09:13:47 -070054 getMeasurementsAccessor()
Junxiao Shidbe71732014-02-21 22:23:28 -070055 {
56 return this->getMeasurements();
57 }
58};
59
Junxiao Shi35353962015-01-08 09:13:47 -070060class MeasurementsAccessorFixture : public BaseFixture
Junxiao Shidbe71732014-02-21 22:23:28 -070061{
Junxiao Shi35353962015-01-08 09:13:47 -070062protected:
63 MeasurementsAccessorFixture()
Junxiao Shia49a1ab2016-07-15 18:24:36 +000064 : strategy1(install<MeasurementsAccessorTestStrategy>(forwarder, "ndn:/strategy1"))
65 , strategy2(install<MeasurementsAccessorTestStrategy>(forwarder, "ndn:/strategy2"))
Junxiao Shi35353962015-01-08 09:13:47 -070066 , measurements(forwarder.getMeasurements())
Junxiao Shia49a1ab2016-07-15 18:24:36 +000067 , accessor1(strategy1.getMeasurementsAccessor())
68 , accessor2(strategy2.getMeasurementsAccessor())
Junxiao Shi35353962015-01-08 09:13:47 -070069 {
70 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
Junxiao Shia49a1ab2016-07-15 18:24:36 +000071 strategyChoice.insert("/" , strategy1.getName());
72 strategyChoice.insert("/A" , strategy2.getName());
73 strategyChoice.insert("/A/B", strategy1.getName());
Junxiao Shi35353962015-01-08 09:13:47 -070074 }
75
76protected:
Junxiao Shic041ca32014-02-25 20:01:15 -070077 Forwarder forwarder;
Junxiao Shia49a1ab2016-07-15 18:24:36 +000078 MeasurementsAccessorTestStrategy& strategy1;
79 MeasurementsAccessorTestStrategy& strategy2;
Junxiao Shi35353962015-01-08 09:13:47 -070080 Measurements& measurements;
81 MeasurementsAccessor& accessor1;
82 MeasurementsAccessor& accessor2;
83};
Junxiao Shic041ca32014-02-25 20:01:15 -070084
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000085BOOST_AUTO_TEST_SUITE(Table)
86BOOST_FIXTURE_TEST_SUITE(TestMeasurementsAccessor, MeasurementsAccessorFixture)
Junxiao Shic041ca32014-02-25 20:01:15 -070087
Junxiao Shi35353962015-01-08 09:13:47 -070088BOOST_AUTO_TEST_CASE(Get)
89{
90 BOOST_CHECK(accessor1.get("/" ) != nullptr);
91 BOOST_CHECK(accessor1.get("/A" ) == nullptr);
92 BOOST_CHECK(accessor1.get("/A/B" ) != nullptr);
93 BOOST_CHECK(accessor1.get("/A/B/C") != nullptr);
94 BOOST_CHECK(accessor1.get("/A/D" ) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Junxiao Shi35353962015-01-08 09:13:47 -070096 BOOST_CHECK(accessor2.get("/" ) == nullptr);
97 BOOST_CHECK(accessor2.get("/A" ) != nullptr);
98 BOOST_CHECK(accessor2.get("/A/B" ) == nullptr);
99 BOOST_CHECK(accessor2.get("/A/B/C") == nullptr);
100 BOOST_CHECK(accessor2.get("/A/D" ) != nullptr);
101}
Junxiao Shic041ca32014-02-25 20:01:15 -0700102
Junxiao Shi35353962015-01-08 09:13:47 -0700103BOOST_AUTO_TEST_CASE(GetParent)
104{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000105 Entry& entryRoot = measurements.get("/");
106 BOOST_CHECK(accessor1.getParent(entryRoot) == nullptr);
107 BOOST_CHECK(accessor2.getParent(entryRoot) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700108
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000109 Entry& entryABC = measurements.get("/A/B/C");
110 BOOST_CHECK(accessor1.getParent(entryABC) != nullptr);
111 BOOST_CHECK(accessor2.getParent(entryABC) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700112
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000113 Entry& entryAB = measurements.get("/A/B");
114 BOOST_CHECK(accessor1.getParent(entryAB) == nullptr);
115 // whether accessor2.getParent(entryAB) can return an Entry is undefined,
Junxiao Shi35353962015-01-08 09:13:47 -0700116 // because strategy2 shouldn't obtain entryAB in the first place
117}
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Junxiao Shi35353962015-01-08 09:13:47 -0700119BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
120{
121 shared_ptr<Interest> interest = makeInterest("/A/B/C");
122 shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first;
123
124 measurements.get("/");
125 BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") != nullptr);
126 BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) != nullptr);
127
128 measurements.get("/A");
129 BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") == nullptr);
130 BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) == nullptr);
131}
132
133BOOST_AUTO_TEST_CASE(FindExactMatch)
134{
135 measurements.get("/");
136 measurements.get("/A");
137 measurements.get("/A/B");
138 measurements.get("/A/B/C");
139 measurements.get("/A/D");
140
141 BOOST_CHECK(accessor1.findExactMatch("/" ) != nullptr);
142 BOOST_CHECK(accessor1.findExactMatch("/A" ) == nullptr);
143 BOOST_CHECK(accessor1.findExactMatch("/A/B" ) != nullptr);
144 BOOST_CHECK(accessor1.findExactMatch("/A/B/C") != nullptr);
145 BOOST_CHECK(accessor1.findExactMatch("/A/D" ) == nullptr);
146 BOOST_CHECK(accessor1.findExactMatch("/A/E" ) == nullptr);
147 BOOST_CHECK(accessor1.findExactMatch("/F" ) == nullptr);
148
149 BOOST_CHECK(accessor2.findExactMatch("/" ) == nullptr);
150 BOOST_CHECK(accessor2.findExactMatch("/A" ) != nullptr);
151 BOOST_CHECK(accessor2.findExactMatch("/A/B" ) == nullptr);
152 BOOST_CHECK(accessor2.findExactMatch("/A/B/C") == nullptr);
153 BOOST_CHECK(accessor2.findExactMatch("/A/D" ) != nullptr);
154 BOOST_CHECK(accessor2.findExactMatch("/A/E" ) == nullptr);
155 BOOST_CHECK(accessor2.findExactMatch("/F" ) == nullptr);
Junxiao Shidbe71732014-02-21 22:23:28 -0700156}
157
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000158BOOST_AUTO_TEST_SUITE_END() // TestMeasurementsAccessor
159BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shidbe71732014-02-21 22:23:28 -0700160
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700161} // namespace tests
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000162} // namespace measurements
Junxiao Shidbe71732014-02-21 22:23:28 -0700163} // namespace nfd