blob: 1e38da9cd6abe5a7271ae809fba532c2634aabf1 [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/*
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
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()
Junxiao Shi047a6fb2017-06-08 16:16:05 +000049 : watchHandle(repoFace, *handle, keyChain, 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 });
57 watchHandle.listen(cmdPrefix);
Weiqi Shi098f91c2014-07-23 17:41:35 -070058 }
59
Weiqi Shi098f91c2014-07-23 17:41:35 -070060 void
61 scheduleWatchEvent();
62
63 void
64 onWatchInterest(const Interest& interest);
65
66 void
67 onRegisterFailed(const std::string& reason);
68
69 void
70 delayedInterest();
71
72 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080073 onWatchData(const Interest& interest, const Data& data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070074
75 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080076 onWatchStopData(const Interest& interest, const Data& data);
Weiqi Shi098f91c2014-07-23 17:41:35 -070077
78 void
79 onWatchTimeout(const Interest& interest);
80
81 void
82 sendWatchStartInterest(const Interest& interest);
83
84 void
85 sendWatchStopInterest(const Interest& interest);
86
87 void
88 checkWatchOk(const Interest& interest);
89
90public:
Weiqi Shi098f91c2014-07-23 17:41:35 -070091 WatchHandle watchHandle;
92 Face watchFace;
93 std::map<Name, EventId> watchEvents;
94};
95
Weiqi Shi098f91c2014-07-23 17:41:35 -070096template<class T> void
97Fixture<T>::onWatchInterest(const Interest& interest)
98{
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040099 auto data = make_shared<Data>(Name(interest.getName())
100 .appendNumber(ndn::random::generateWord64() + 100));
Weiqi Shi098f91c2014-07-23 17:41:35 -0700101 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