blob: a29deede17ea8ac083a17a93baebca84154dbdf7 [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 {
Junxiao Shi2b7b8312017-06-16 03:43:24 +000053 Name cmdPrefix("/repo/command");
54 repoFace.registerPrefix(cmdPrefix, nullptr,
55 [] (const Name& cmdPrefix, const std::string& reason) {
56 BOOST_FAIL("Command prefix registration error: " << reason);
57 });
58 watchHandle.listen(cmdPrefix);
Weiqi Shi098f91c2014-07-23 17:41:35 -070059 }
60
Weiqi Shi098f91c2014-07-23 17:41:35 -070061 void
62 scheduleWatchEvent();
63
64 void
65 onWatchInterest(const Interest& interest);
66
67 void
68 onRegisterFailed(const std::string& reason);
69
70 void
71 delayedInterest();
72
73 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080074 onWatchData(const Interest& interest, const Data& data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070075
76 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080077 onWatchStopData(const Interest& interest, const Data& data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070078
79 void
80 onWatchTimeout(const Interest& interest);
81
82 void
83 sendWatchStartInterest(const Interest& interest);
84
85 void
86 sendWatchStopInterest(const Interest& interest);
87
88 void
89 checkWatchOk(const Interest& interest);
90
91public:
Weiqi Shi098f91c2014-07-23 17:41:35 -070092 WatchHandle watchHandle;
93 Face watchFace;
94 std::map<Name, EventId> watchEvents;
95};
96
Weiqi Shi098f91c2014-07-23 17:41:35 -070097template<class T> void
98Fixture<T>::onWatchInterest(const Interest& interest)
99{
100 shared_ptr<Data> data = make_shared<Data>(Name(interest.getName()).appendNumber(random::generateWord64()+100));
101 data->setContent(content, sizeof(content));
102 data->setFreshnessPeriod(milliseconds(0));
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000103 keyChain.sign(*data);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700104 watchFace.put(*data);
105
106 // schedule an event 50ms later to check whether watch is Ok
107 scheduler.scheduleEvent(milliseconds(10000),
108 bind(&Fixture<T>::checkWatchOk, this,
109 Interest(data->getName())));
110}
111
112
113template<class T> void
114Fixture<T>::onRegisterFailed(const std::string& reason)
115{
116 BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason);
117}
118
119template<class T> void
120Fixture<T>::delayedInterest()
121{
122 BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong");
123}
124
125template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800126Fixture<T>::onWatchData(const Interest& interest, const Data& data)
Weiqi Shi098f91c2014-07-23 17:41:35 -0700127{
128 RepoCommandResponse response;
129 response.wireDecode(data.getContent().blockFromValue());
130
131 int statusCode = response.getStatusCode();
132 BOOST_CHECK_EQUAL(statusCode, 100);
133}
134
135template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800136Fixture<T>::onWatchStopData(const Interest& interest, const Data& data)
Weiqi Shi098f91c2014-07-23 17:41:35 -0700137{
138 RepoCommandResponse response;
139 response.wireDecode(data.getContent().blockFromValue());
140
141 int statusCode = response.getStatusCode();
142 BOOST_CHECK_EQUAL(statusCode, 101);
143}
144
145template<class T> void
146Fixture<T>::onWatchTimeout(const Interest& interest)
147{
148 BOOST_ERROR("Watch command timeout");
149}
150
151template<class T> void
152Fixture<T>::sendWatchStartInterest(const Interest& watchInterest)
153{
154 watchFace.expressInterest(watchInterest,
155 bind(&Fixture<T>::onWatchData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800156 bind(&Fixture<T>::onWatchTimeout, this, _1), // Nack
Weiqi Shi098f91c2014-07-23 17:41:35 -0700157 bind(&Fixture<T>::onWatchTimeout, this, _1));
158}
159
160template<class T> void
161Fixture<T>::sendWatchStopInterest(const Interest& watchInterest)
162{
163 watchFace.expressInterest(watchInterest,
164 bind(&Fixture<T>::onWatchStopData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800165 bind(&Fixture<T>::onWatchTimeout, this, _1), // Nack
Weiqi Shi098f91c2014-07-23 17:41:35 -0700166 bind(&Fixture<T>::onWatchTimeout, this, _1));
167}
168
169template<class T> void
170Fixture<T>::checkWatchOk(const Interest& interest)
171{
172 BOOST_TEST_MESSAGE(interest);
173 shared_ptr<Data> data = handle->readData(interest);
174 if (data) {
175 int rc = memcmp(data->getContent().value(), content, sizeof(content));
176 BOOST_CHECK_EQUAL(rc, 0);
177 }
178 else {
179 std::cerr<<"Check Watch Failed"<<std::endl;
180 }
181}
182
183template<class T> void
184Fixture<T>::scheduleWatchEvent()
185{
186 Name watchCommandName("/repo/command/watch/start");
187 RepoCommandParameter watchParameter;
188 watchParameter.setName(Name("/a/b"));
189 watchParameter.setMaxInterestNum(10);
190 watchParameter.setInterestLifetime(milliseconds(50000));
191 watchParameter.setWatchTimeout(milliseconds(1000000000));
192 watchCommandName.append(watchParameter.wireEncode());
193 Interest watchInterest(watchCommandName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000194 keyChain.sign(watchInterest);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700195 //schedule a job to express watchInterest
196 scheduler.scheduleEvent(milliseconds(1000),
197 bind(&Fixture<T>::sendWatchStartInterest, this, watchInterest));
198
199 Name watchStopName("/repo/command/watch/stop");
200 RepoCommandParameter watchStopParameter;
201 watchStopName.append(watchStopParameter.wireEncode());
202 Interest watchStopInterest(watchStopName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000203 keyChain.sign(watchStopInterest);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700204
205 // scheduler.scheduleEvent(milliseconds(10000),
206 // bind(&Fixture<T>::sendWatchStopInterest, this, watchStopInterest));
207 //The delayEvent will be canceled in onWatchInterest
208 watchFace.setInterestFilter(watchParameter.getName(),
209 bind(&Fixture<T>::onWatchInterest, this, _2),
210 ndn::RegisterPrefixSuccessCallback(),
211 bind(&Fixture<T>::onRegisterFailed, this, _2));
212}
213
214typedef boost::mpl::vector< BasicDataset > Dataset;
215
216BOOST_FIXTURE_TEST_CASE_TEMPLATE(WatchDelete, T, Dataset, Fixture<T>)
217{
Weiqi Shi098f91c2014-07-23 17:41:35 -0700218 // schedule events
219 this->scheduler.scheduleEvent(seconds(0),
220 bind(&Fixture<T>::scheduleWatchEvent, this));
221
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000222 this->repoFace.processEvents(seconds(500));
Weiqi Shi098f91c2014-07-23 17:41:35 -0700223}
224
225BOOST_AUTO_TEST_SUITE_END()
226
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800227} // namespace tests
228} // namespace repo