blob: 9f3e18a68e1e8590adb9d6280f21d81881006883 [file] [log] [blame]
Junxiao Shi727ed292014-02-19 23:26:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventob84bd3a2016-04-22 02:21:45 +02003 * Copyright (c) 2014-2016, 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
29#include <boost/tuple/tuple_comparison.hpp>
30#include "fw/strategy.hpp"
31
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
80protected:
Junxiao Shi890afe92016-12-15 14:34:34 +000081 void
Junxiao Shic5f651f2016-11-17 22:58:12 +000082 sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
83 const Interest& interest) override
Junxiao Shib9420cf2016-08-13 04:38:52 +000084 {
Junxiao Shic5f651f2016-11-17 22:58:12 +000085 sendInterestHistory.push_back({pitEntry->getInterest(), outFace.getId(), interest});
86 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shib9420cf2016-08-13 04:38:52 +000087 afterAction();
88 }
Junxiao Shi727ed292014-02-19 23:26:45 -070089
Junxiao Shi890afe92016-12-15 14:34:34 +000090 void
Junxiao Shib9420cf2016-08-13 04:38:52 +000091 rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry) override
92 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +000093 rejectPendingInterestHistory.push_back({pitEntry->getInterest()});
Junxiao Shib9420cf2016-08-13 04:38:52 +000094 afterAction();
95 }
Junxiao Shi727ed292014-02-19 23:26:45 -070096
Junxiao Shi890afe92016-12-15 14:34:34 +000097 void
Junxiao Shib9420cf2016-08-13 04:38:52 +000098 sendNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
99 const lp::NackHeader& header) override
100 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000101 sendNackHistory.push_back({pitEntry->getInterest(), outFace.getId(), header});
Junxiao Shib9420cf2016-08-13 04:38:52 +0000102 pitEntry->deleteInRecord(outFace);
103 afterAction();
104 }
Junxiao Shi727ed292014-02-19 23:26:45 -0700105
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700106public:
107 struct SendInterestArgs
108 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000109 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700110 FaceId outFaceId;
Junxiao Shic5f651f2016-11-17 22:58:12 +0000111 Interest interest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700112 };
113 std::vector<SendInterestArgs> sendInterestHistory;
114
115 struct RejectPendingInterestArgs
116 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000117 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700118 };
119 std::vector<RejectPendingInterestArgs> rejectPendingInterestHistory;
120
121 struct SendNackArgs
122 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000123 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700124 FaceId outFaceId;
125 lp::NackHeader header;
126 };
127 std::vector<SendNackArgs> sendNackHistory;
Junxiao Shi727ed292014-02-19 23:26:45 -0700128};
129
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700130} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800131} // namespace fw
Junxiao Shi727ed292014-02-19 23:26:45 -0700132} // namespace nfd
133
Davide Pesavento97210d52016-10-14 15:45:48 +0200134#endif // NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP