blob: 6fe677d71663f6c48ef57d2ce81241109f28dc05 [file] [log] [blame]
Weiqi Shi098f91c2014-07-23 17:41:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento49f3a5f2017-09-23 01:36:33 -04002/*
weijia yuan82cf9142018-10-21 12:25:02 -07003 * Copyright (c) 2014-2018, Regents of the University of California.
Weiqi Shi098f91c2014-07-23 17:41:35 -07004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "handles/watch-handle.hpp"
21#include "storage/sqlite-storage.hpp"
Weiqi Shi098f91c2014-07-23 17:41:35 -070022
Junxiao Shi047a6fb2017-06-08 16:16:05 +000023#include "command-fixture.hpp"
Weiqi Shi098f91c2014-07-23 17:41:35 -070024#include "../repo-storage-fixture.hpp"
25#include "../dataset-fixtures.hpp"
26
27#include <ndn-cxx/util/random.hpp>
Weiqi Shi098f91c2014-07-23 17:41:35 -070028
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040029#include <boost/mpl/vector.hpp>
Weiqi Shi098f91c2014-07-23 17:41:35 -070030#include <boost/test/unit_test.hpp>
Weiqi Shi098f91c2014-07-23 17:41:35 -070031
32namespace repo {
33namespace tests {
34
35using ndn::time::milliseconds;
36using ndn::time::seconds;
37using ndn::EventId;
Weiqi Shi098f91c2014-07-23 17:41:35 -070038
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040039// All the test cases in this test suite should be run at once.
Weiqi Shi098f91c2014-07-23 17:41:35 -070040BOOST_AUTO_TEST_SUITE(TestBasicCommandWatchDelete)
41
42const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
43
44template<class Dataset>
Junxiao Shi047a6fb2017-06-08 16:16:05 +000045class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset
Weiqi Shi098f91c2014-07-23 17:41:35 -070046{
47public:
48 Fixture()
weijia yuan82cf9142018-10-21 12:25:02 -070049 : watchHandle(repoFace, *handle, dispatcher, scheduler, validator)
Weiqi Shi098f91c2014-07-23 17:41:35 -070050 , watchFace(repoFace.getIoService())
51 {
Junxiao Shi2b7b8312017-06-16 03:43:24 +000052 Name cmdPrefix("/repo/command");
53 repoFace.registerPrefix(cmdPrefix, nullptr,
54 [] (const Name& cmdPrefix, const std::string& reason) {
55 BOOST_FAIL("Command prefix registration error: " << reason);
56 });
Weiqi Shi098f91c2014-07-23 17:41:35 -070057 }
58
Weiqi Shi098f91c2014-07-23 17:41:35 -070059 void
60 scheduleWatchEvent();
61
62 void
63 onWatchInterest(const Interest& interest);
64
65 void
66 onRegisterFailed(const std::string& reason);
67
68 void
69 delayedInterest();
70
71 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080072 onWatchData(const Interest& interest, const Data& data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070073
74 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080075 onWatchStopData(const Interest& interest, const Data& data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070076
77 void
78 onWatchTimeout(const Interest& interest);
79
80 void
81 sendWatchStartInterest(const Interest& interest);
82
83 void
84 sendWatchStopInterest(const Interest& interest);
85
86 void
87 checkWatchOk(const Interest& interest);
88
89public:
Weiqi Shi098f91c2014-07-23 17:41:35 -070090 WatchHandle watchHandle;
91 Face watchFace;
92 std::map<Name, EventId> watchEvents;
93};
94
Weiqi Shi098f91c2014-07-23 17:41:35 -070095template<class T> void
96Fixture<T>::onWatchInterest(const Interest& interest)
97{
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040098 auto data = make_shared<Data>(Name(interest.getName())
99 .appendNumber(ndn::random::generateWord64() + 100));
Weiqi Shi098f91c2014-07-23 17:41:35 -0700100 data->setContent(content, sizeof(content));
weijia yuan82cf9142018-10-21 12:25:02 -0700101 data->setFreshnessPeriod(0_ms);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000102 keyChain.sign(*data);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700103 watchFace.put(*data);
104
105 // schedule an event 50ms later to check whether watch is Ok
weijia yuan82cf9142018-10-21 12:25:02 -0700106 scheduler.scheduleEvent(10000_ms,
Weiqi Shi098f91c2014-07-23 17:41:35 -0700107 bind(&Fixture<T>::checkWatchOk, this,
108 Interest(data->getName())));
109}
110
111
112template<class T> void
113Fixture<T>::onRegisterFailed(const std::string& reason)
114{
115 BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason);
116}
117
118template<class T> void
119Fixture<T>::delayedInterest()
120{
121 BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong");
122}
123
124template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800125Fixture<T>::onWatchData(const Interest& interest, const Data& data)
Weiqi Shi098f91c2014-07-23 17:41:35 -0700126{
127 RepoCommandResponse response;
128 response.wireDecode(data.getContent().blockFromValue());
129
weijia yuan82cf9142018-10-21 12:25:02 -0700130 int statusCode = response.getCode();
Weiqi Shi098f91c2014-07-23 17:41:35 -0700131 BOOST_CHECK_EQUAL(statusCode, 100);
132}
133
134template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800135Fixture<T>::onWatchStopData(const Interest& interest, const Data& data)
Weiqi Shi098f91c2014-07-23 17:41:35 -0700136{
137 RepoCommandResponse response;
138 response.wireDecode(data.getContent().blockFromValue());
139
weijia yuan82cf9142018-10-21 12:25:02 -0700140 int statusCode = response.getCode();
Weiqi Shi098f91c2014-07-23 17:41:35 -0700141 BOOST_CHECK_EQUAL(statusCode, 101);
142}
143
144template<class T> void
145Fixture<T>::onWatchTimeout(const Interest& interest)
146{
147 BOOST_ERROR("Watch command timeout");
148}
149
150template<class T> void
151Fixture<T>::sendWatchStartInterest(const Interest& watchInterest)
152{
153 watchFace.expressInterest(watchInterest,
154 bind(&Fixture<T>::onWatchData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800155 bind(&Fixture<T>::onWatchTimeout, this, _1), // Nack
Weiqi Shi098f91c2014-07-23 17:41:35 -0700156 bind(&Fixture<T>::onWatchTimeout, this, _1));
157}
158
159template<class T> void
160Fixture<T>::sendWatchStopInterest(const Interest& watchInterest)
161{
162 watchFace.expressInterest(watchInterest,
163 bind(&Fixture<T>::onWatchStopData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800164 bind(&Fixture<T>::onWatchTimeout, this, _1), // Nack
Weiqi Shi098f91c2014-07-23 17:41:35 -0700165 bind(&Fixture<T>::onWatchTimeout, this, _1));
166}
167
168template<class T> void
169Fixture<T>::checkWatchOk(const Interest& interest)
170{
171 BOOST_TEST_MESSAGE(interest);
172 shared_ptr<Data> data = handle->readData(interest);
173 if (data) {
174 int rc = memcmp(data->getContent().value(), content, sizeof(content));
175 BOOST_CHECK_EQUAL(rc, 0);
176 }
177 else {
178 std::cerr<<"Check Watch Failed"<<std::endl;
179 }
180}
181
182template<class T> void
183Fixture<T>::scheduleWatchEvent()
184{
185 Name watchCommandName("/repo/command/watch/start");
186 RepoCommandParameter watchParameter;
187 watchParameter.setName(Name("/a/b"));
188 watchParameter.setMaxInterestNum(10);
weijia yuan82cf9142018-10-21 12:25:02 -0700189 watchParameter.setInterestLifetime(50000_ms);
190 watchParameter.setWatchTimeout(1000000000_ms);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700191 watchCommandName.append(watchParameter.wireEncode());
192 Interest watchInterest(watchCommandName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000193 keyChain.sign(watchInterest);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700194 //schedule a job to express watchInterest
weijia yuan82cf9142018-10-21 12:25:02 -0700195 scheduler.scheduleEvent(1000_ms,
Weiqi Shi098f91c2014-07-23 17:41:35 -0700196 bind(&Fixture<T>::sendWatchStartInterest, this, watchInterest));
197
198 Name watchStopName("/repo/command/watch/stop");
199 RepoCommandParameter watchStopParameter;
200 watchStopName.append(watchStopParameter.wireEncode());
201 Interest watchStopInterest(watchStopName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000202 keyChain.sign(watchStopInterest);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700203
Weiqi Shi098f91c2014-07-23 17:41:35 -0700204 //The delayEvent will be canceled in onWatchInterest
205 watchFace.setInterestFilter(watchParameter.getName(),
206 bind(&Fixture<T>::onWatchInterest, this, _2),
207 ndn::RegisterPrefixSuccessCallback(),
208 bind(&Fixture<T>::onRegisterFailed, this, _2));
209}
210
weijia yuan82cf9142018-10-21 12:25:02 -0700211typedef boost::mpl::vector<BasicDataset> Dataset;
Weiqi Shi098f91c2014-07-23 17:41:35 -0700212
213BOOST_FIXTURE_TEST_CASE_TEMPLATE(WatchDelete, T, Dataset, Fixture<T>)
214{
Weiqi Shi098f91c2014-07-23 17:41:35 -0700215 // schedule events
weijia yuan82cf9142018-10-21 12:25:02 -0700216 this->scheduler.scheduleEvent(1_s,
Weiqi Shi098f91c2014-07-23 17:41:35 -0700217 bind(&Fixture<T>::scheduleWatchEvent, this));
218
weijia yuan82cf9142018-10-21 12:25:02 -0700219 this->repoFace.processEvents(500_s);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700220}
221
222BOOST_AUTO_TEST_SUITE_END()
223
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800224} // namespace tests
225} // namespace repo