blob: 86a005ad43a5d6be3c04b1a8c043f41a391812a3 [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 Shi0e4a1f12016-12-24 02:39:01 +000031#include "../fw/choose-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 Shi0e4a1f12016-12-24 02:39:01 +000042 static void
43 registerAs(const Name& strategyName)
44 {
45 registerAsImpl<MeasurementsAccessorTestStrategy>(strategyName);
46 }
47
Junxiao Shi7bb01512014-03-05 21:34:09 -070048 MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi1c93cae2014-12-09 22:52:17 -070049 : DummyStrategy(forwarder, name)
Junxiao Shidbe71732014-02-21 22:23:28 -070050 {
51 }
Junxiao Shic041ca32014-02-25 20:01:15 -070052
Junxiao Shidbe71732014-02-21 22:23:28 -070053 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 Shi0e4a1f12016-12-24 02:39:01 +000064 : measurements(forwarder.getMeasurements())
Junxiao Shi35353962015-01-08 09:13:47 -070065 {
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000066 const Name strategyP("/measurements-accessor-test-strategy-P/%FD%01");
67 const Name strategyQ("/measurements-accessor-test-strategy-Q/%FD%01");
68 MeasurementsAccessorTestStrategy::registerAs(strategyP);
69 MeasurementsAccessorTestStrategy::registerAs(strategyQ);
70
71 accessor1 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/", strategyP)
72 .getMeasurementsAccessor();
73 accessor2 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/A", strategyQ)
74 .getMeasurementsAccessor();
75 accessor3 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/A/B", strategyP)
76 .getMeasurementsAccessor();
77
78 // Despite accessor1 and accessor3 are associated with the same strategy name,
79 // they are different strategy instances and thus cannot access each other's measurements.
Junxiao Shi35353962015-01-08 09:13:47 -070080 }
81
82protected:
Junxiao Shic041ca32014-02-25 20:01:15 -070083 Forwarder forwarder;
Junxiao Shi35353962015-01-08 09:13:47 -070084 Measurements& measurements;
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000085 MeasurementsAccessor* accessor1;
86 MeasurementsAccessor* accessor2;
87 MeasurementsAccessor* accessor3;
Junxiao Shi35353962015-01-08 09:13:47 -070088};
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000090BOOST_AUTO_TEST_SUITE(Table)
91BOOST_FIXTURE_TEST_SUITE(TestMeasurementsAccessor, MeasurementsAccessorFixture)
Junxiao Shic041ca32014-02-25 20:01:15 -070092
Junxiao Shi35353962015-01-08 09:13:47 -070093BOOST_AUTO_TEST_CASE(Get)
94{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000095 BOOST_CHECK(accessor1->get("/" ) != nullptr);
96 BOOST_CHECK(accessor1->get("/A" ) == nullptr);
97 BOOST_CHECK(accessor1->get("/A/B" ) == nullptr);
98 BOOST_CHECK(accessor1->get("/A/B/C") == nullptr);
99 BOOST_CHECK(accessor1->get("/A/D" ) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700100
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000101 BOOST_CHECK(accessor2->get("/" ) == nullptr);
102 BOOST_CHECK(accessor2->get("/A" ) != nullptr);
103 BOOST_CHECK(accessor2->get("/A/B" ) == nullptr);
104 BOOST_CHECK(accessor2->get("/A/B/C") == nullptr);
105 BOOST_CHECK(accessor2->get("/A/D" ) != nullptr);
106
107 BOOST_CHECK(accessor3->get("/" ) == nullptr);
108 BOOST_CHECK(accessor3->get("/A" ) == nullptr);
109 BOOST_CHECK(accessor3->get("/A/B" ) != nullptr);
110 BOOST_CHECK(accessor3->get("/A/B/C") != nullptr);
111 BOOST_CHECK(accessor3->get("/A/D" ) == nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700112}
Junxiao Shic041ca32014-02-25 20:01:15 -0700113
Junxiao Shi35353962015-01-08 09:13:47 -0700114BOOST_AUTO_TEST_CASE(GetParent)
115{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000116 Entry& entryRoot = measurements.get("/");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000117 BOOST_CHECK(accessor1->getParent(entryRoot) == nullptr);
118 BOOST_CHECK(accessor2->getParent(entryRoot) == nullptr);
Junxiao Shic041ca32014-02-25 20:01:15 -0700119
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000120 Entry& entryA = measurements.get("/A");
121 BOOST_CHECK(accessor2->getParent(entryA) == nullptr);
122 Entry& entryAD = measurements.get("/A/D");
123 BOOST_CHECK(accessor2->getParent(entryAD) != nullptr);
124 // whether accessor1 and accessor3 can getParent(entryA) and getParent(entryAD) is undefined,
125 // because they shouldn't have obtained those entries in the first place
Junxiao Shi35353962015-01-08 09:13:47 -0700126}
Junxiao Shic041ca32014-02-25 20:01:15 -0700127
Junxiao Shi35353962015-01-08 09:13:47 -0700128BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
129{
130 shared_ptr<Interest> interest = makeInterest("/A/B/C");
131 shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first;
132
133 measurements.get("/");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000134 BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") != nullptr);
135 BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) != nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700136
137 measurements.get("/A");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000138 BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") == nullptr);
139 BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) == nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700140}
141
142BOOST_AUTO_TEST_CASE(FindExactMatch)
143{
144 measurements.get("/");
145 measurements.get("/A");
146 measurements.get("/A/B");
147 measurements.get("/A/B/C");
148 measurements.get("/A/D");
149
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000150 BOOST_CHECK(accessor1->findExactMatch("/" ) != nullptr);
151 BOOST_CHECK(accessor1->findExactMatch("/A" ) == nullptr);
152 BOOST_CHECK(accessor1->findExactMatch("/A/B" ) == nullptr);
153 BOOST_CHECK(accessor1->findExactMatch("/A/B/C") == nullptr);
154 BOOST_CHECK(accessor1->findExactMatch("/A/D" ) == nullptr);
155 BOOST_CHECK(accessor1->findExactMatch("/A/E" ) == nullptr);
156 BOOST_CHECK(accessor1->findExactMatch("/F" ) == nullptr);
Junxiao Shi35353962015-01-08 09:13:47 -0700157
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000158 BOOST_CHECK(accessor2->findExactMatch("/" ) == nullptr);
159 BOOST_CHECK(accessor2->findExactMatch("/A" ) != nullptr);
160 BOOST_CHECK(accessor2->findExactMatch("/A/B" ) == nullptr);
161 BOOST_CHECK(accessor2->findExactMatch("/A/B/C") == nullptr);
162 BOOST_CHECK(accessor2->findExactMatch("/A/D" ) != nullptr);
163 BOOST_CHECK(accessor2->findExactMatch("/A/E" ) == nullptr);
164 BOOST_CHECK(accessor2->findExactMatch("/F" ) == nullptr);
165
166 BOOST_CHECK(accessor3->findExactMatch("/" ) == nullptr);
167 BOOST_CHECK(accessor3->findExactMatch("/A" ) == nullptr);
168 BOOST_CHECK(accessor3->findExactMatch("/A/B" ) != nullptr);
169 BOOST_CHECK(accessor3->findExactMatch("/A/B/C") != nullptr);
170 BOOST_CHECK(accessor3->findExactMatch("/A/D" ) == nullptr);
171 BOOST_CHECK(accessor3->findExactMatch("/A/E" ) == nullptr);
172 BOOST_CHECK(accessor3->findExactMatch("/F" ) == nullptr);
Junxiao Shidbe71732014-02-21 22:23:28 -0700173}
174
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000175BOOST_AUTO_TEST_SUITE_END() // TestMeasurementsAccessor
176BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shidbe71732014-02-21 22:23:28 -0700177
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700178} // namespace tests
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000179} // namespace measurements
Junxiao Shidbe71732014-02-21 22:23:28 -0700180} // namespace nfd