blob: ea58abe85c897d8ac30daaca96e01f078ef501b0 [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -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.
Shuo Chenca329182014-03-19 18:05:18 -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
Shuo Chenca329182014-03-19 18:05:18 -070020#include "handles/delete-handle.hpp"
weijia yuan82cf9142018-10-21 12:25:02 -070021#include "handles/write-handle.hpp"
22
Weiqi Shif0330d52014-07-09 10:54:27 -070023#include "storage/repo-storage.hpp"
weijia yuan82cf9142018-10-21 12:25:02 -070024#include "storage/sqlite-storage.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070025
Junxiao Shi047a6fb2017-06-08 16:16:05 +000026#include "command-fixture.hpp"
Weiqi Shif0330d52014-07-09 10:54:27 -070027#include "../repo-storage-fixture.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070028#include "../dataset-fixtures.hpp"
29
weijia yuan82cf9142018-10-21 12:25:02 -070030#include <ndn-cxx/security/command-interest-signer.hpp>
31#include <ndn-cxx/security/signing-helpers.hpp>
Shuo Chen028dcd32014-06-21 16:36:44 +080032#include <ndn-cxx/util/random.hpp>
weijia yuan82cf9142018-10-21 12:25:02 -070033#include <ndn-cxx/util/time.hpp>
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040034
weijia yuan82cf9142018-10-21 12:25:02 -070035#include <boost/asio/io_service.hpp>
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040036#include <boost/mpl/vector.hpp>
Shuo Chenca329182014-03-19 18:05:18 -070037#include <boost/test/unit_test.hpp>
38
weijia yuan82cf9142018-10-21 12:25:02 -070039
40#include <iostream>
41
Shuo Chenca329182014-03-19 18:05:18 -070042namespace repo {
43namespace tests {
44
45using ndn::time::milliseconds;
46using ndn::time::seconds;
47using ndn::EventId;
Shuo Chenca329182014-03-19 18:05:18 -070048
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040049// All the test cases in this test suite should be run at once.
Shuo Chenca329182014-03-19 18:05:18 -070050BOOST_AUTO_TEST_SUITE(TestBasicCommandInsertDelete)
51
52const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
53
54template<class Dataset>
Junxiao Shi047a6fb2017-06-08 16:16:05 +000055class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070056{
57public:
58 Fixture()
weijia yuan82cf9142018-10-21 12:25:02 -070059 : writeHandle(repoFace, *handle, dispatcher, scheduler, validator)
60 , deleteHandle(repoFace, *handle, dispatcher, scheduler, validator)
Shuo Chenca329182014-03-19 18:05:18 -070061 , insertFace(repoFace.getIoService())
62 , deleteFace(repoFace.getIoService())
63 {
Junxiao Shi2b7b8312017-06-16 03:43:24 +000064 Name cmdPrefix("/repo/command");
65 repoFace.registerPrefix(cmdPrefix, nullptr,
66 [] (const Name& cmdPrefix, const std::string& reason) {
67 BOOST_FAIL("Command prefix registration error: " << reason);
68 });
Shuo Chenca329182014-03-19 18:05:18 -070069 }
70
Shuo Chen028dcd32014-06-21 16:36:44 +080071 void
Shuo Chenca329182014-03-19 18:05:18 -070072 scheduleInsertEvent();
73
74 void
75 scheduleDeleteEvent();
76
77 void
78 onInsertInterest(const Interest& interest);
79
80 void
81 onRegisterFailed(const std::string& reason);
82
83 void
84 delayedInterest();
85
86 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080087 onInsertData(const Interest& interest, const Data& data);
Shuo Chenca329182014-03-19 18:05:18 -070088
89 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080090 onDeleteData(const Interest& interest, const Data& data);
Shuo Chenca329182014-03-19 18:05:18 -070091
92 void
93 onInsertTimeout(const Interest& interest);
94
95 void
96 onDeleteTimeout(const Interest& interest);
97
98 void
99 sendInsertInterest(const Interest& interest);
100
101 void
102 sendDeleteInterest(const Interest& deleteInterest);
103
104 void
Shuo Chen028dcd32014-06-21 16:36:44 +0800105 checkInsertOk(const Interest& interest);
Shuo Chenca329182014-03-19 18:05:18 -0700106
107 void
Shuo Chen028dcd32014-06-21 16:36:44 +0800108 checkDeleteOk(const Interest& interest);
Shuo Chenca329182014-03-19 18:05:18 -0700109
110public:
Shuo Chenca329182014-03-19 18:05:18 -0700111 WriteHandle writeHandle;
112 DeleteHandle deleteHandle;
113 Face insertFace;
114 Face deleteFace;
115 std::map<Name, EventId> insertEvents;
116};
117
118template<class T> void
119Fixture<T>::onInsertInterest(const Interest& interest)
120{
121 Data data(Name(interest.getName()));
122 data.setContent(content, sizeof(content));
weijia yuan82cf9142018-10-21 12:25:02 -0700123 data.setFreshnessPeriod(0_ms);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000124 keyChain.sign(data);
Shuo Chenca329182014-03-19 18:05:18 -0700125 insertFace.put(data);
Shuo Chenca329182014-03-19 18:05:18 -0700126 std::map<Name, EventId>::iterator event = insertEvents.find(interest.getName());
127 if (event != insertEvents.end()) {
128 scheduler.cancelEvent(event->second);
129 insertEvents.erase(event);
130 }
Shuo Chen028dcd32014-06-21 16:36:44 +0800131 // schedule an event 50ms later to check whether insert is Ok
weijia yuan82cf9142018-10-21 12:25:02 -0700132 scheduler.scheduleEvent(500_ms,
Shuo Chen028dcd32014-06-21 16:36:44 +0800133 bind(&Fixture<T>::checkInsertOk, this, interest));
Shuo Chenca329182014-03-19 18:05:18 -0700134
135}
136
137
138template<class T> void
139Fixture<T>::onRegisterFailed(const std::string& reason)
140{
141 BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason);
142}
143
144template<class T> void
145Fixture<T>::delayedInterest()
146{
147 BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong");
148}
149
150template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800151Fixture<T>::onInsertData(const Interest& interest, const Data& data)
Shuo Chenca329182014-03-19 18:05:18 -0700152{
153 RepoCommandResponse response;
154 response.wireDecode(data.getContent().blockFromValue());
weijia yuan82cf9142018-10-21 12:25:02 -0700155 int statusCode = response.getCode();
Shuo Chenca329182014-03-19 18:05:18 -0700156 BOOST_CHECK_EQUAL(statusCode, 100);
157}
158
159template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800160Fixture<T>::onDeleteData(const Interest& interest, const Data& data)
Shuo Chenca329182014-03-19 18:05:18 -0700161{
162 RepoCommandResponse response;
163 response.wireDecode(data.getContent().blockFromValue());
weijia yuan82cf9142018-10-21 12:25:02 -0700164 int statusCode = response.getCode();
Shuo Chenca329182014-03-19 18:05:18 -0700165 BOOST_CHECK_EQUAL(statusCode, 200);
166
Shuo Chen028dcd32014-06-21 16:36:44 +0800167 //schedlute an event to check whether delete is Ok.
weijia yuan82cf9142018-10-21 12:25:02 -0700168 scheduler.scheduleEvent(100_ms,
Shuo Chen028dcd32014-06-21 16:36:44 +0800169 bind(&Fixture<T>::checkDeleteOk, this, interest));
Shuo Chenca329182014-03-19 18:05:18 -0700170}
171
172template<class T> void
173Fixture<T>::onInsertTimeout(const Interest& interest)
174{
Junxiao Shi2b7b8312017-06-16 03:43:24 +0000175 BOOST_ERROR("Insert command timeout");
Shuo Chenca329182014-03-19 18:05:18 -0700176}
177
178template<class T> void
179Fixture<T>::onDeleteTimeout(const Interest& interest)
180{
Junxiao Shi2b7b8312017-06-16 03:43:24 +0000181 BOOST_ERROR("Delete command timeout");
Shuo Chenca329182014-03-19 18:05:18 -0700182}
183
184template<class T> void
185Fixture<T>::sendInsertInterest(const Interest& insertInterest)
186{
187 insertFace.expressInterest(insertInterest,
188 bind(&Fixture<T>::onInsertData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800189 bind(&Fixture<T>::onInsertTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700190 bind(&Fixture<T>::onInsertTimeout, this, _1));
191}
192
193template<class T> void
194Fixture<T>::sendDeleteInterest(const Interest& deleteInterest)
195{
196 deleteFace.expressInterest(deleteInterest,
197 bind(&Fixture<T>::onDeleteData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800198 bind(&Fixture<T>::onDeleteTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700199 bind(&Fixture<T>::onDeleteTimeout, this, _1));
200}
201
202template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800203Fixture<T>::checkInsertOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700204{
Shuo Chenca329182014-03-19 18:05:18 -0700205 BOOST_TEST_MESSAGE(interest);
Weiqi Shif0330d52014-07-09 10:54:27 -0700206 shared_ptr<Data> data = handle->readData(interest);
207 if (data) {
208 int rc = memcmp(data->getContent().value(), content, sizeof(content));
209 BOOST_CHECK_EQUAL(rc, 0);
210 }
211 else {
weijia yuan82cf9142018-10-21 12:25:02 -0700212 BOOST_ERROR("Check Insert Failed");
Weiqi Shif0330d52014-07-09 10:54:27 -0700213 }
Shuo Chenca329182014-03-19 18:05:18 -0700214}
215
216template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800217Fixture<T>::checkDeleteOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700218{
Weiqi Shif0330d52014-07-09 10:54:27 -0700219 shared_ptr<Data> data = handle->readData(interest);
220 BOOST_CHECK_EQUAL(data, shared_ptr<Data>());
Shuo Chenca329182014-03-19 18:05:18 -0700221}
222
Shuo Chenca329182014-03-19 18:05:18 -0700223template<class T> void
224Fixture<T>::scheduleInsertEvent()
225{
226 int timeCount = 1;
227 for (typename T::DataContainer::iterator i = this->data.begin();
228 i != this->data.end(); ++i) {
229 Name insertCommandName("/repo/command/insert");
230 RepoCommandParameter insertParameter;
231 insertParameter.setName(Name((*i)->getName())
Davide Pesavento49f3a5f2017-09-23 01:36:33 -0400232 .appendNumber(ndn::random::generateWord64()));
Shuo Chenca329182014-03-19 18:05:18 -0700233
234 insertCommandName.append(insertParameter.wireEncode());
235 Interest insertInterest(insertCommandName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000236 keyChain.sign(insertInterest);
weijia yuan82cf9142018-10-21 12:25:02 -0700237
238 // schedule a job to express insertInterest every 50ms
Shuo Chenca329182014-03-19 18:05:18 -0700239 scheduler.scheduleEvent(milliseconds(timeCount * 50 + 1000),
240 bind(&Fixture<T>::sendInsertInterest, this, insertInterest));
weijia yuan82cf9142018-10-21 12:25:02 -0700241 // schedule what to do when interest timeout
Shuo Chenca329182014-03-19 18:05:18 -0700242 EventId delayEventId = scheduler.scheduleEvent(milliseconds(5000 + timeCount * 50),
243 bind(&Fixture<T>::delayedInterest, this));
244 insertEvents[insertParameter.getName()] = delayEventId;
245
246 //The delayEvent will be canceled in onInsertInterest
247 insertFace.setInterestFilter(insertParameter.getName(),
248 bind(&Fixture<T>::onInsertInterest, this, _2),
Wentao Shang91fb4f22014-05-20 10:55:22 -0700249 ndn::RegisterPrefixSuccessCallback(),
Shuo Chenca329182014-03-19 18:05:18 -0700250 bind(&Fixture<T>::onRegisterFailed, this, _2));
251 timeCount++;
252 }
253}
254
Shuo Chenca329182014-03-19 18:05:18 -0700255template<class T> void
256Fixture<T>::scheduleDeleteEvent()
257{
258 int timeCount = 1;
259 for (typename T::DataContainer::iterator i = this->data.begin();
260 i != this->data.end(); ++i) {
261 Name deleteCommandName("/repo/command/delete");
262 RepoCommandParameter deleteParameter;
Davide Pesavento49f3a5f2017-09-23 01:36:33 -0400263 deleteParameter.setProcessId(ndn::random::generateWord64());
Shuo Chenca329182014-03-19 18:05:18 -0700264 deleteParameter.setName((*i)->getName());
265 deleteCommandName.append(deleteParameter.wireEncode());
266 Interest deleteInterest(deleteCommandName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000267 keyChain.sign(deleteInterest);
Shuo Chenca329182014-03-19 18:05:18 -0700268 scheduler.scheduleEvent(milliseconds(4000 + timeCount * 50),
269 bind(&Fixture<T>::sendDeleteInterest, this, deleteInterest));
270 timeCount++;
271 }
272}
273
weijia yuan82cf9142018-10-21 12:25:02 -0700274typedef boost::mpl::vector<BasicDataset,
275 FetchByPrefixDataset,
276 BasicChildSelectorDataset,
277 ExtendedChildSelectorDataset,
278 SamePrefixDataset<10>> Datasets;
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700279
280BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertDelete, T, Datasets, Fixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700281{
Shuo Chenca329182014-03-19 18:05:18 -0700282 // schedule events
weijia yuan82cf9142018-10-21 12:25:02 -0700283 this->scheduler.scheduleEvent(0_s,
Shuo Chenca329182014-03-19 18:05:18 -0700284 bind(&Fixture<T>::scheduleInsertEvent, this));
weijia yuan82cf9142018-10-21 12:25:02 -0700285 this->scheduler.scheduleEvent(10_s,
Shuo Chenca329182014-03-19 18:05:18 -0700286 bind(&Fixture<T>::scheduleDeleteEvent, this));
287
weijia yuan82cf9142018-10-21 12:25:02 -0700288 this->repoFace.processEvents(30_s);
Shuo Chenca329182014-03-19 18:05:18 -0700289}
290
291BOOST_AUTO_TEST_SUITE_END()
292
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800293} // namespace tests
294} // namespace repo