blob: aeed38f5b96f116145226f8aceb139275b362f63 [file] [log] [blame]
Junxiao Shidbe71732014-02-21 22:23:28 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04002/*
3 * Copyright (c) 2014-2019, 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"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
31#include "tests/daemon/fw/dummy-strategy.hpp"
32#include "tests/daemon/fw/choose-strategy.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070033
34namespace nfd {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000035namespace measurements {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036namespace tests {
Junxiao Shidbe71732014-02-21 22:23:28 -070037
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000038using namespace nfd::tests;
Junxiao Shidbe71732014-02-21 22:23:28 -070039
Junxiao Shi1c93cae2014-12-09 22:52:17 -070040class MeasurementsAccessorTestStrategy : public DummyStrategy
Junxiao Shidbe71732014-02-21 22:23:28 -070041{
42public:
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000043 static void
44 registerAs(const Name& strategyName)
45 {
46 registerAsImpl<MeasurementsAccessorTestStrategy>(strategyName);
47 }
48
Junxiao Shi7bb01512014-03-05 21:34:09 -070049 MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi1c93cae2014-12-09 22:52:17 -070050 : DummyStrategy(forwarder, name)
Junxiao Shidbe71732014-02-21 22:23:28 -070051 {
52 }
Junxiao Shic041ca32014-02-25 20:01:15 -070053
Junxiao Shidbe71732014-02-21 22:23:28 -070054 MeasurementsAccessor&
Junxiao Shi35353962015-01-08 09:13:47 -070055 getMeasurementsAccessor()
Junxiao Shidbe71732014-02-21 22:23:28 -070056 {
57 return this->getMeasurements();
58 }
59};
60
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040061class MeasurementsAccessorFixture : public GlobalIoFixture
Junxiao Shidbe71732014-02-21 22:23:28 -070062{
Junxiao Shi35353962015-01-08 09:13:47 -070063protected:
64 MeasurementsAccessorFixture()
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000065 : measurements(forwarder.getMeasurements())
Junxiao Shi35353962015-01-08 09:13:47 -070066 {
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000067 const Name strategyP("/measurements-accessor-test-strategy-P/%FD%01");
68 const Name strategyQ("/measurements-accessor-test-strategy-Q/%FD%01");
69 MeasurementsAccessorTestStrategy::registerAs(strategyP);
70 MeasurementsAccessorTestStrategy::registerAs(strategyQ);
71
72 accessor1 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/", strategyP)
73 .getMeasurementsAccessor();
74 accessor2 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/A", strategyQ)
75 .getMeasurementsAccessor();
76 accessor3 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/A/B", strategyP)
77 .getMeasurementsAccessor();
78
79 // Despite accessor1 and accessor3 are associated with the same strategy name,
80 // they are different strategy instances and thus cannot access each other's measurements.
Junxiao Shi35353962015-01-08 09:13:47 -070081 }
82
83protected:
Junxiao Shic041ca32014-02-25 20:01:15 -070084 Forwarder forwarder;
Junxiao Shi35353962015-01-08 09:13:47 -070085 Measurements& measurements;
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000086 MeasurementsAccessor* accessor1;
87 MeasurementsAccessor* accessor2;
88 MeasurementsAccessor* accessor3;
Junxiao Shi35353962015-01-08 09:13:47 -070089};
Junxiao Shic041ca32014-02-25 20:01:15 -070090
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000091BOOST_AUTO_TEST_SUITE(Table)
92BOOST_FIXTURE_TEST_SUITE(TestMeasurementsAccessor, MeasurementsAccessorFixture)
Junxiao Shic041ca32014-02-25 20:01:15 -070093
Junxiao Shi35353962015-01-08 09:13:47 -070094BOOST_AUTO_TEST_CASE(Get)
95{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000096 BOOST_CHECK(accessor1->get("/" ) != nullptr);
97 BOOST_CHECK(accessor1->get("/A" ) == nullptr);
98 BOOST_CHECK(accessor1->get("/A/B" ) == nullptr);
99 BOOST_CHECK(accessor1->get("/A/B/C") == nullptr);
100 BOOST_CHECK(accessor1->get("/A/D" ) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700101
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000102 BOOST_CHECK(accessor2->get("/" ) == nullptr);
103 BOOST_CHECK(accessor2->get("/A" ) != nullptr);
104 BOOST_CHECK(accessor2->get("/A/B" ) == nullptr);
105 BOOST_CHECK(accessor2->get("/A/B/C") == nullptr);
106 BOOST_CHECK(accessor2->get("/A/D" ) != nullptr);
107
108 BOOST_CHECK(accessor3->get("/" ) == nullptr);
109 BOOST_CHECK(accessor3->get("/A" ) == nullptr);
110 BOOST_CHECK(accessor3->get("/A/B" ) != nullptr);
111 BOOST_CHECK(accessor3->get("/A/B/C") != nullptr);
112 BOOST_CHECK(accessor3->get("/A/D" ) == nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700113}
Junxiao Shic041ca32014-02-25 20:01:15 -0700114
Junxiao Shi35353962015-01-08 09:13:47 -0700115BOOST_AUTO_TEST_CASE(GetParent)
116{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000117 Entry& entryRoot = measurements.get("/");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000118 BOOST_CHECK(accessor1->getParent(entryRoot) == nullptr);
119 BOOST_CHECK(accessor2->getParent(entryRoot) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700120
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000121 Entry& entryA = measurements.get("/A");
122 BOOST_CHECK(accessor2->getParent(entryA) == nullptr);
123 Entry& entryAD = measurements.get("/A/D");
124 BOOST_CHECK(accessor2->getParent(entryAD) != nullptr);
125 // whether accessor1 and accessor3 can getParent(entryA) and getParent(entryAD) is undefined,
126 // because they shouldn't have obtained those entries in the first place
Junxiao Shi35353962015-01-08 09:13:47 -0700127}
Junxiao Shic041ca32014-02-25 20:01:15 -0700128
Junxiao Shi35353962015-01-08 09:13:47 -0700129BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
130{
131 shared_ptr<Interest> interest = makeInterest("/A/B/C");
132 shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first;
133
134 measurements.get("/");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000135 BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") != nullptr);
136 BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) != nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700137
138 measurements.get("/A");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000139 BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") == nullptr);
140 BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) == nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700141}
142
143BOOST_AUTO_TEST_CASE(FindExactMatch)
144{
145 measurements.get("/");
146 measurements.get("/A");
147 measurements.get("/A/B");
148 measurements.get("/A/B/C");
149 measurements.get("/A/D");
150
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000151 BOOST_CHECK(accessor1->findExactMatch("/" ) != nullptr);
152 BOOST_CHECK(accessor1->findExactMatch("/A" ) == nullptr);
153 BOOST_CHECK(accessor1->findExactMatch("/A/B" ) == nullptr);
154 BOOST_CHECK(accessor1->findExactMatch("/A/B/C") == nullptr);
155 BOOST_CHECK(accessor1->findExactMatch("/A/D" ) == nullptr);
156 BOOST_CHECK(accessor1->findExactMatch("/A/E" ) == nullptr);
157 BOOST_CHECK(accessor1->findExactMatch("/F" ) == nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700158
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000159 BOOST_CHECK(accessor2->findExactMatch("/" ) == nullptr);
160 BOOST_CHECK(accessor2->findExactMatch("/A" ) != nullptr);
161 BOOST_CHECK(accessor2->findExactMatch("/A/B" ) == nullptr);
162 BOOST_CHECK(accessor2->findExactMatch("/A/B/C") == nullptr);
163 BOOST_CHECK(accessor2->findExactMatch("/A/D" ) != nullptr);
164 BOOST_CHECK(accessor2->findExactMatch("/A/E" ) == nullptr);
165 BOOST_CHECK(accessor2->findExactMatch("/F" ) == nullptr);
166
167 BOOST_CHECK(accessor3->findExactMatch("/" ) == nullptr);
168 BOOST_CHECK(accessor3->findExactMatch("/A" ) == nullptr);
169 BOOST_CHECK(accessor3->findExactMatch("/A/B" ) != nullptr);
170 BOOST_CHECK(accessor3->findExactMatch("/A/B/C") != nullptr);
171 BOOST_CHECK(accessor3->findExactMatch("/A/D" ) == nullptr);
172 BOOST_CHECK(accessor3->findExactMatch("/A/E" ) == nullptr);
173 BOOST_CHECK(accessor3->findExactMatch("/F" ) == nullptr);
Junxiao Shidbe71732014-02-21 22:23:28 -0700174}
175
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000176BOOST_AUTO_TEST_SUITE_END() // TestMeasurementsAccessor
177BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shidbe71732014-02-21 22:23:28 -0700178
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700179} // namespace tests
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000180} // namespace measurements
Junxiao Shidbe71732014-02-21 22:23:28 -0700181} // namespace nfd