blob: 73ec41813b5c133f0b97eb944a95ccdf53d8deeb [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::tests {
Junxiao Shidbe71732014-02-21 22:23:28 -070035
Junxiao Shi1c93cae2014-12-09 22:52:17 -070036class MeasurementsAccessorTestStrategy : public DummyStrategy
Junxiao Shidbe71732014-02-21 22:23:28 -070037{
38public:
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000039 static void
40 registerAs(const Name& strategyName)
41 {
42 registerAsImpl<MeasurementsAccessorTestStrategy>(strategyName);
43 }
44
Junxiao Shi7bb01512014-03-05 21:34:09 -070045 MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi1c93cae2014-12-09 22:52:17 -070046 : DummyStrategy(forwarder, name)
Junxiao Shidbe71732014-02-21 22:23:28 -070047 {
48 }
Junxiao Shic041ca32014-02-25 20:01:15 -070049
Junxiao Shidbe71732014-02-21 22:23:28 -070050 MeasurementsAccessor&
Junxiao Shi35353962015-01-08 09:13:47 -070051 getMeasurementsAccessor()
Junxiao Shidbe71732014-02-21 22:23:28 -070052 {
53 return this->getMeasurements();
54 }
55};
56
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040057class MeasurementsAccessorFixture : public GlobalIoFixture
Junxiao Shidbe71732014-02-21 22:23:28 -070058{
Junxiao Shi35353962015-01-08 09:13:47 -070059protected:
60 MeasurementsAccessorFixture()
Junxiao Shi35353962015-01-08 09:13:47 -070061 {
Eric Newberry358414d2021-03-21 20:56:42 -070062 const auto strategyP = Name("/measurements-accessor-test-strategy-P").appendVersion(1);
63 const auto strategyQ = Name("/measurements-accessor-test-strategy-Q").appendVersion(1);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000064 MeasurementsAccessorTestStrategy::registerAs(strategyP);
65 MeasurementsAccessorTestStrategy::registerAs(strategyQ);
66
67 accessor1 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/", strategyP)
68 .getMeasurementsAccessor();
69 accessor2 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/A", strategyQ)
70 .getMeasurementsAccessor();
71 accessor3 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/A/B", strategyP)
72 .getMeasurementsAccessor();
73
74 // Despite accessor1 and accessor3 are associated with the same strategy name,
75 // they are different strategy instances and thus cannot access each other's measurements.
Junxiao Shi35353962015-01-08 09:13:47 -070076 }
77
78protected:
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040079 FaceTable faceTable;
80 Forwarder forwarder{faceTable};
81 Measurements& measurements{forwarder.getMeasurements()};
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000082 MeasurementsAccessor* accessor1;
83 MeasurementsAccessor* accessor2;
84 MeasurementsAccessor* accessor3;
Junxiao Shi35353962015-01-08 09:13:47 -070085};
Junxiao Shic041ca32014-02-25 20:01:15 -070086
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000087BOOST_AUTO_TEST_SUITE(Table)
88BOOST_FIXTURE_TEST_SUITE(TestMeasurementsAccessor, MeasurementsAccessorFixture)
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shi35353962015-01-08 09:13:47 -070090BOOST_AUTO_TEST_CASE(Get)
91{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000092 BOOST_CHECK(accessor1->get("/" ) != nullptr);
93 BOOST_CHECK(accessor1->get("/A" ) == nullptr);
94 BOOST_CHECK(accessor1->get("/A/B" ) == nullptr);
95 BOOST_CHECK(accessor1->get("/A/B/C") == nullptr);
96 BOOST_CHECK(accessor1->get("/A/D" ) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -070097
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000098 BOOST_CHECK(accessor2->get("/" ) == nullptr);
99 BOOST_CHECK(accessor2->get("/A" ) != nullptr);
100 BOOST_CHECK(accessor2->get("/A/B" ) == nullptr);
101 BOOST_CHECK(accessor2->get("/A/B/C") == nullptr);
102 BOOST_CHECK(accessor2->get("/A/D" ) != nullptr);
103
104 BOOST_CHECK(accessor3->get("/" ) == nullptr);
105 BOOST_CHECK(accessor3->get("/A" ) == nullptr);
106 BOOST_CHECK(accessor3->get("/A/B" ) != nullptr);
107 BOOST_CHECK(accessor3->get("/A/B/C") != nullptr);
108 BOOST_CHECK(accessor3->get("/A/D" ) == nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700109}
Junxiao Shic041ca32014-02-25 20:01:15 -0700110
Junxiao Shi35353962015-01-08 09:13:47 -0700111BOOST_AUTO_TEST_CASE(GetParent)
112{
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400113 auto& entryRoot = measurements.get("/");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000114 BOOST_CHECK(accessor1->getParent(entryRoot) == nullptr);
115 BOOST_CHECK(accessor2->getParent(entryRoot) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700116
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400117 auto& entryA = measurements.get("/A");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000118 BOOST_CHECK(accessor2->getParent(entryA) == nullptr);
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400119 auto& entryAD = measurements.get("/A/D");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000120 BOOST_CHECK(accessor2->getParent(entryAD) != nullptr);
121 // whether accessor1 and accessor3 can getParent(entryA) and getParent(entryAD) is undefined,
122 // because they shouldn't have obtained those entries in the first place
Junxiao Shi35353962015-01-08 09:13:47 -0700123}
Junxiao Shic041ca32014-02-25 20:01:15 -0700124
Junxiao Shi35353962015-01-08 09:13:47 -0700125BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
126{
127 shared_ptr<Interest> interest = makeInterest("/A/B/C");
128 shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first;
129
130 measurements.get("/");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000131 BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") != nullptr);
132 BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) != nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700133
134 measurements.get("/A");
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
139BOOST_AUTO_TEST_CASE(FindExactMatch)
140{
141 measurements.get("/");
142 measurements.get("/A");
143 measurements.get("/A/B");
144 measurements.get("/A/B/C");
145 measurements.get("/A/D");
146
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000147 BOOST_CHECK(accessor1->findExactMatch("/" ) != nullptr);
148 BOOST_CHECK(accessor1->findExactMatch("/A" ) == nullptr);
149 BOOST_CHECK(accessor1->findExactMatch("/A/B" ) == nullptr);
150 BOOST_CHECK(accessor1->findExactMatch("/A/B/C") == nullptr);
151 BOOST_CHECK(accessor1->findExactMatch("/A/D" ) == nullptr);
152 BOOST_CHECK(accessor1->findExactMatch("/A/E" ) == nullptr);
153 BOOST_CHECK(accessor1->findExactMatch("/F" ) == nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700154
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000155 BOOST_CHECK(accessor2->findExactMatch("/" ) == nullptr);
156 BOOST_CHECK(accessor2->findExactMatch("/A" ) != nullptr);
157 BOOST_CHECK(accessor2->findExactMatch("/A/B" ) == nullptr);
158 BOOST_CHECK(accessor2->findExactMatch("/A/B/C") == nullptr);
159 BOOST_CHECK(accessor2->findExactMatch("/A/D" ) != nullptr);
160 BOOST_CHECK(accessor2->findExactMatch("/A/E" ) == nullptr);
161 BOOST_CHECK(accessor2->findExactMatch("/F" ) == nullptr);
162
163 BOOST_CHECK(accessor3->findExactMatch("/" ) == nullptr);
164 BOOST_CHECK(accessor3->findExactMatch("/A" ) == nullptr);
165 BOOST_CHECK(accessor3->findExactMatch("/A/B" ) != nullptr);
166 BOOST_CHECK(accessor3->findExactMatch("/A/B/C") != nullptr);
167 BOOST_CHECK(accessor3->findExactMatch("/A/D" ) == nullptr);
168 BOOST_CHECK(accessor3->findExactMatch("/A/E" ) == nullptr);
169 BOOST_CHECK(accessor3->findExactMatch("/F" ) == nullptr);
Junxiao Shidbe71732014-02-21 22:23:28 -0700170}
171
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000172BOOST_AUTO_TEST_SUITE_END() // TestMeasurementsAccessor
173BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shidbe71732014-02-21 22:23:28 -0700174
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400175} // namespace nfd::tests