blob: 0d76d761d5e0103fb357cd5b777e172c09069b4d [file] [log] [blame]
Junxiao Shi727ed292014-02-19 23:26:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi99540072017-01-27 19:57:33 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * 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 Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shi727ed292014-02-19 23:26:45 -070025
Davide Pesavento97210d52016-10-14 15:45:48 +020026#ifndef NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP
27#define NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP
Junxiao Shi727ed292014-02-19 23:26:45 -070028
Junxiao Shi727ed292014-02-19 23:26:45 -070029#include "fw/strategy.hpp"
Junxiao Shi99540072017-01-27 19:57:33 +000030#include "tests/limited-io.hpp"
Junxiao Shi727ed292014-02-19 23:26:45 -070031
32namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080033namespace fw {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Junxiao Shi727ed292014-02-19 23:26:45 -070035
Junxiao Shi5e5e4452015-09-24 16:56:52 -070036/** \brief extends strategy S for unit testing
Junxiao Shi727ed292014-02-19 23:26:45 -070037 *
38 * Actions invoked by S are recorded but not passed to forwarder
Junxiao Shi890afe92016-12-15 14:34:34 +000039 *
40 * StrategyTester should be registered into the strategy registry prior to use.
41 * \code
42 * // appears in or included by every .cpp MyStrategyTester is used
43 * typedef StrategyTester<MyStrategy> MyStrategyTester;
44 *
45 * // appears in only one .cpp
46 * NFD_REGISTER_STRATEGY(MyStrategyTester);
47 * \endcode
Junxiao Shi727ed292014-02-19 23:26:45 -070048 */
49template<typename S>
50class StrategyTester : public S
51{
52public:
53 explicit
Junxiao Shi890afe92016-12-15 14:34:34 +000054 StrategyTester(Forwarder& forwarder, const Name& name = getStrategyName())
55 : S(forwarder, name)
Junxiao Shi727ed292014-02-19 23:26:45 -070056 {
57 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070058
Junxiao Shi890afe92016-12-15 14:34:34 +000059 static Name
60 getStrategyName()
61 {
62 Name name = S::getStrategyName();
63 if (!name.empty() && name[-1].isVersion()) {
64 // insert "tester" before version component
65 name::Component versionComp = name[-1];
66 name = name.getPrefix(-1);
67 name.append("tester");
68 name.append(versionComp);
69 }
70 else {
71 name.append("tester");
72 }
73 return name;
74 }
75
76 /** \brief signal emitted after each Action
77 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -070078 signal::Signal<StrategyTester<S>> afterAction;
Junxiao Shi727ed292014-02-19 23:26:45 -070079
Junxiao Shi99540072017-01-27 19:57:33 +000080 /** \brief execute f and wait for a number of strategy actions
81 * \note The actions may occur either during f() invocation or afterwards.
82 * \return whether expected number of actions have occurred
83 */
84 bool
85 waitForAction(const std::function<void()>& f,
86 nfd::tests::LimitedIo& limitedIo, int nExpectedActions = 1)
87 {
88 int nActions = 0;
89
90 signal::ScopedConnection conn = afterAction.connect([&] {
91 limitedIo.afterOp();
92 ++nActions;
93 });
94
95 f();
96
97 if (nActions < nExpectedActions) {
98 // A correctly implemented strategy is required to invoke reject pending Interest action if it
99 // decides to not forward an Interest. If a test case is stuck in the call below, check that
100 // rejectPendingInterest is invoked under proper condition.
101 return limitedIo.run(nExpectedActions - nActions, nfd::tests::LimitedIo::UNLIMITED_TIME) ==
102 nfd::tests::LimitedIo::EXCEED_OPS;
103 }
104 return nActions == nExpectedActions;
105 }
106
Junxiao Shi727ed292014-02-19 23:26:45 -0700107protected:
Junxiao Shi890afe92016-12-15 14:34:34 +0000108 void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000109 sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
110 const Interest& interest) override
Junxiao Shib9420cf2016-08-13 04:38:52 +0000111 {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000112 sendInterestHistory.push_back({pitEntry->getInterest(), outFace.getId(), interest});
113 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000114 afterAction();
115 }
Junxiao Shi727ed292014-02-19 23:26:45 -0700116
Junxiao Shi890afe92016-12-15 14:34:34 +0000117 void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000118 rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry) override
119 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000120 rejectPendingInterestHistory.push_back({pitEntry->getInterest()});
Junxiao Shib9420cf2016-08-13 04:38:52 +0000121 afterAction();
122 }
Junxiao Shi727ed292014-02-19 23:26:45 -0700123
Junxiao Shi890afe92016-12-15 14:34:34 +0000124 void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000125 sendNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
126 const lp::NackHeader& header) override
127 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000128 sendNackHistory.push_back({pitEntry->getInterest(), outFace.getId(), header});
Junxiao Shib9420cf2016-08-13 04:38:52 +0000129 pitEntry->deleteInRecord(outFace);
130 afterAction();
131 }
Junxiao Shi727ed292014-02-19 23:26:45 -0700132
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700133public:
134 struct SendInterestArgs
135 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000136 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700137 FaceId outFaceId;
Junxiao Shic5f651f2016-11-17 22:58:12 +0000138 Interest interest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700139 };
140 std::vector<SendInterestArgs> sendInterestHistory;
141
142 struct RejectPendingInterestArgs
143 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000144 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700145 };
146 std::vector<RejectPendingInterestArgs> rejectPendingInterestHistory;
147
148 struct SendNackArgs
149 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000150 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700151 FaceId outFaceId;
152 lp::NackHeader header;
153 };
154 std::vector<SendNackArgs> sendNackHistory;
Junxiao Shi727ed292014-02-19 23:26:45 -0700155};
156
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700157} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800158} // namespace fw
Junxiao Shi727ed292014-02-19 23:26:45 -0700159} // namespace nfd
160
Davide Pesavento97210d52016-10-14 15:45:48 +0200161#endif // NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP