blob: 8b8f3273458f81d7e445d3eafc4038f45706dbe2 [file] [log] [blame]
Weiqi Shi098f91c2014-07-23 17:41:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev42290b22017-03-09 12:58:29 -08003 * Copyright (c) 2014-2017, 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
29#include <boost/test/unit_test.hpp>
30#include <fstream>
31
32namespace repo {
33namespace tests {
34
35using ndn::time::milliseconds;
36using ndn::time::seconds;
37using ndn::EventId;
Alexander Afanasyev42290b22017-03-09 12:58:29 -080038namespace random = ndn::random;
Weiqi Shi098f91c2014-07-23 17:41:35 -070039
40//All the test cases in this test suite should be run at once.
41BOOST_AUTO_TEST_SUITE(TestBasicCommandWatchDelete)
42
43const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
44
45template<class Dataset>
Junxiao Shi047a6fb2017-06-08 16:16:05 +000046class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset
Weiqi Shi098f91c2014-07-23 17:41:35 -070047{
48public:
49 Fixture()
Junxiao Shi047a6fb2017-06-08 16:16:05 +000050 : watchHandle(repoFace, *handle, keyChain, scheduler, validator)
Weiqi Shi098f91c2014-07-23 17:41:35 -070051 , watchFace(repoFace.getIoService())
52 {
53 watchHandle.listen(Name("/repo/command"));
54 }
55
Weiqi Shi098f91c2014-07-23 17:41:35 -070056 void
57 scheduleWatchEvent();
58
59 void
60 onWatchInterest(const Interest& interest);
61
62 void
63 onRegisterFailed(const std::string& reason);
64
65 void
66 delayedInterest();
67
68 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080069 onWatchData(const Interest& interest, const Data& data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070070
71 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080072 onWatchStopData(const Interest& interest, const Data& data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070073
74 void
75 onWatchTimeout(const Interest& interest);
76
77 void
78 sendWatchStartInterest(const Interest& interest);
79
80 void
81 sendWatchStopInterest(const Interest& interest);
82
83 void
84 checkWatchOk(const Interest& interest);
85
86public:
Weiqi Shi098f91c2014-07-23 17:41:35 -070087 WatchHandle watchHandle;
88 Face watchFace;
89 std::map<Name, EventId> watchEvents;
90};
91
Weiqi Shi098f91c2014-07-23 17:41:35 -070092template<class T> void
93Fixture<T>::onWatchInterest(const Interest& interest)
94{
95 shared_ptr<Data> data = make_shared<Data>(Name(interest.getName()).appendNumber(random::generateWord64()+100));
96 data->setContent(content, sizeof(content));
97 data->setFreshnessPeriod(milliseconds(0));
Junxiao Shi047a6fb2017-06-08 16:16:05 +000098 keyChain.sign(*data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070099 watchFace.put(*data);
100
101 // schedule an event 50ms later to check whether watch is Ok
102 scheduler.scheduleEvent(milliseconds(10000),
103 bind(&Fixture<T>::checkWatchOk, this,
104 Interest(data->getName())));
105}
106
107
108template<class T> void
109Fixture<T>::onRegisterFailed(const std::string& reason)
110{
111 BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason);
112}
113
114template<class T> void
115Fixture<T>::delayedInterest()
116{
117 BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong");
118}
119
120template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800121Fixture<T>::onWatchData(const Interest& interest, const Data& data)
Weiqi Shi098f91c2014-07-23 17:41:35 -0700122{
123 RepoCommandResponse response;
124 response.wireDecode(data.getContent().blockFromValue());
125
126 int statusCode = response.getStatusCode();
127 BOOST_CHECK_EQUAL(statusCode, 100);
128}
129
130template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800131Fixture<T>::onWatchStopData(const Interest& interest, const Data& data)
Weiqi Shi098f91c2014-07-23 17:41:35 -0700132{
133 RepoCommandResponse response;
134 response.wireDecode(data.getContent().blockFromValue());
135
136 int statusCode = response.getStatusCode();
137 BOOST_CHECK_EQUAL(statusCode, 101);
138}
139
140template<class T> void
141Fixture<T>::onWatchTimeout(const Interest& interest)
142{
143 BOOST_ERROR("Watch command timeout");
144}
145
146template<class T> void
147Fixture<T>::sendWatchStartInterest(const Interest& watchInterest)
148{
149 watchFace.expressInterest(watchInterest,
150 bind(&Fixture<T>::onWatchData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800151 bind(&Fixture<T>::onWatchTimeout, this, _1), // Nack
Weiqi Shi098f91c2014-07-23 17:41:35 -0700152 bind(&Fixture<T>::onWatchTimeout, this, _1));
153}
154
155template<class T> void
156Fixture<T>::sendWatchStopInterest(const Interest& watchInterest)
157{
158 watchFace.expressInterest(watchInterest,
159 bind(&Fixture<T>::onWatchStopData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800160 bind(&Fixture<T>::onWatchTimeout, this, _1), // Nack
Weiqi Shi098f91c2014-07-23 17:41:35 -0700161 bind(&Fixture<T>::onWatchTimeout, this, _1));
162}
163
164template<class T> void
165Fixture<T>::checkWatchOk(const Interest& interest)
166{
167 BOOST_TEST_MESSAGE(interest);
168 shared_ptr<Data> data = handle->readData(interest);
169 if (data) {
170 int rc = memcmp(data->getContent().value(), content, sizeof(content));
171 BOOST_CHECK_EQUAL(rc, 0);
172 }
173 else {
174 std::cerr<<"Check Watch Failed"<<std::endl;
175 }
176}
177
178template<class T> void
179Fixture<T>::scheduleWatchEvent()
180{
181 Name watchCommandName("/repo/command/watch/start");
182 RepoCommandParameter watchParameter;
183 watchParameter.setName(Name("/a/b"));
184 watchParameter.setMaxInterestNum(10);
185 watchParameter.setInterestLifetime(milliseconds(50000));
186 watchParameter.setWatchTimeout(milliseconds(1000000000));
187 watchCommandName.append(watchParameter.wireEncode());
188 Interest watchInterest(watchCommandName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000189 keyChain.sign(watchInterest);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700190 //schedule a job to express watchInterest
191 scheduler.scheduleEvent(milliseconds(1000),
192 bind(&Fixture<T>::sendWatchStartInterest, this, watchInterest));
193
194 Name watchStopName("/repo/command/watch/stop");
195 RepoCommandParameter watchStopParameter;
196 watchStopName.append(watchStopParameter.wireEncode());
197 Interest watchStopInterest(watchStopName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000198 keyChain.sign(watchStopInterest);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700199
200 // scheduler.scheduleEvent(milliseconds(10000),
201 // bind(&Fixture<T>::sendWatchStopInterest, this, watchStopInterest));
202 //The delayEvent will be canceled in onWatchInterest
203 watchFace.setInterestFilter(watchParameter.getName(),
204 bind(&Fixture<T>::onWatchInterest, this, _2),
205 ndn::RegisterPrefixSuccessCallback(),
206 bind(&Fixture<T>::onRegisterFailed, this, _2));
207}
208
209typedef boost::mpl::vector< BasicDataset > Dataset;
210
211BOOST_FIXTURE_TEST_CASE_TEMPLATE(WatchDelete, T, Dataset, Fixture<T>)
212{
Weiqi Shi098f91c2014-07-23 17:41:35 -0700213 // schedule events
214 this->scheduler.scheduleEvent(seconds(0),
215 bind(&Fixture<T>::scheduleWatchEvent, this));
216
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000217 this->repoFace.processEvents(seconds(500));
Weiqi Shi098f91c2014-07-23 17:41:35 -0700218}
219
220BOOST_AUTO_TEST_SUITE_END()
221
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800222} // namespace tests
223} // namespace repo