blob: f3685f65eb775a5f5d3e52c3ad79e76818a14b96 [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
39namespace repo {
40namespace tests {
41
42using ndn::time::milliseconds;
43using ndn::time::seconds;
44using ndn::EventId;
Shuo Chenca329182014-03-19 18:05:18 -070045
weijia yuan3aa8d2b2018-03-06 15:35:57 -080046
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040047// All the test cases in this test suite should be run at once.
Shuo Chenca329182014-03-19 18:05:18 -070048BOOST_AUTO_TEST_SUITE(TestBasicCommandInsertDelete)
49
50const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
51
52template<class Dataset>
Junxiao Shi047a6fb2017-06-08 16:16:05 +000053class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070054{
55public:
56 Fixture()
weijia yuan82cf9142018-10-21 12:25:02 -070057 : writeHandle(repoFace, *handle, dispatcher, scheduler, validator)
58 , deleteHandle(repoFace, *handle, dispatcher, scheduler, validator)
Shuo Chenca329182014-03-19 18:05:18 -070059 , insertFace(repoFace.getIoService())
60 , deleteFace(repoFace.getIoService())
weijia yuan3aa8d2b2018-03-06 15:35:57 -080061 , signer(keyChain)
Shuo Chenca329182014-03-19 18:05:18 -070062 {
Junxiao Shi2b7b8312017-06-16 03:43:24 +000063 Name cmdPrefix("/repo/command");
64 repoFace.registerPrefix(cmdPrefix, nullptr,
65 [] (const Name& cmdPrefix, const std::string& reason) {
66 BOOST_FAIL("Command prefix registration error: " << reason);
67 });
Shuo Chenca329182014-03-19 18:05:18 -070068 }
69
Shuo Chen028dcd32014-06-21 16:36:44 +080070 void
Shuo Chenca329182014-03-19 18:05:18 -070071 scheduleInsertEvent();
72
73 void
74 scheduleDeleteEvent();
75
76 void
77 onInsertInterest(const Interest& interest);
78
79 void
80 onRegisterFailed(const std::string& reason);
81
82 void
83 delayedInterest();
84
85 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080086 onInsertData(const Interest& interest, const Data& data);
Shuo Chenca329182014-03-19 18:05:18 -070087
88 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080089 onDeleteData(const Interest& interest, const Data& data);
Shuo Chenca329182014-03-19 18:05:18 -070090
91 void
92 onInsertTimeout(const Interest& interest);
93
94 void
95 onDeleteTimeout(const Interest& interest);
96
97 void
98 sendInsertInterest(const Interest& interest);
99
100 void
101 sendDeleteInterest(const Interest& deleteInterest);
102
103 void
Shuo Chen028dcd32014-06-21 16:36:44 +0800104 checkInsertOk(const Interest& interest);
Shuo Chenca329182014-03-19 18:05:18 -0700105
106 void
Shuo Chen028dcd32014-06-21 16:36:44 +0800107 checkDeleteOk(const Interest& interest);
Shuo Chenca329182014-03-19 18:05:18 -0700108
109public:
Shuo Chenca329182014-03-19 18:05:18 -0700110 WriteHandle writeHandle;
111 DeleteHandle deleteHandle;
112 Face insertFace;
113 Face deleteFace;
114 std::map<Name, EventId> insertEvents;
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800115 std::map<Name, Name> deleteNamePairs;
116 ndn::security::CommandInterestSigner signer;
Shuo Chenca329182014-03-19 18:05:18 -0700117};
118
119template<class T> void
120Fixture<T>::onInsertInterest(const Interest& interest)
121{
122 Data data(Name(interest.getName()));
123 data.setContent(content, sizeof(content));
weijia yuan82cf9142018-10-21 12:25:02 -0700124 data.setFreshnessPeriod(0_ms);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000125 keyChain.sign(data);
Shuo Chenca329182014-03-19 18:05:18 -0700126 insertFace.put(data);
Shuo Chenca329182014-03-19 18:05:18 -0700127 std::map<Name, EventId>::iterator event = insertEvents.find(interest.getName());
128 if (event != insertEvents.end()) {
129 scheduler.cancelEvent(event->second);
130 insertEvents.erase(event);
131 }
Shuo Chen028dcd32014-06-21 16:36:44 +0800132 // schedule an event 50ms later to check whether insert is Ok
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800133 scheduler.scheduleEvent(500_ms, std::bind(&Fixture<T>::checkInsertOk, this, interest));
Shuo Chenca329182014-03-19 18:05:18 -0700134}
135
136
137template<class T> void
138Fixture<T>::onRegisterFailed(const std::string& reason)
139{
140 BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason);
141}
142
143template<class T> void
144Fixture<T>::delayedInterest()
145{
146 BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong");
147}
148
149template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800150Fixture<T>::onInsertData(const Interest& interest, const Data& data)
Shuo Chenca329182014-03-19 18:05:18 -0700151{
152 RepoCommandResponse response;
153 response.wireDecode(data.getContent().blockFromValue());
weijia yuan82cf9142018-10-21 12:25:02 -0700154 int statusCode = response.getCode();
Shuo Chenca329182014-03-19 18:05:18 -0700155 BOOST_CHECK_EQUAL(statusCode, 100);
156}
157
158template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800159Fixture<T>::onDeleteData(const Interest& interest, const Data& data)
Shuo Chenca329182014-03-19 18:05:18 -0700160{
161 RepoCommandResponse response;
162 response.wireDecode(data.getContent().blockFromValue());
weijia yuan82cf9142018-10-21 12:25:02 -0700163 int statusCode = response.getCode();
Shuo Chenca329182014-03-19 18:05:18 -0700164 BOOST_CHECK_EQUAL(statusCode, 200);
165
Shuo Chen028dcd32014-06-21 16:36:44 +0800166 //schedlute an event to check whether delete is Ok.
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800167 scheduler.scheduleEvent(100_ms, std::bind(&Fixture<T>::checkDeleteOk, this, interest));
Shuo Chenca329182014-03-19 18:05:18 -0700168}
169
170template<class T> void
171Fixture<T>::onInsertTimeout(const Interest& interest)
172{
Junxiao Shi2b7b8312017-06-16 03:43:24 +0000173 BOOST_ERROR("Insert command timeout");
Shuo Chenca329182014-03-19 18:05:18 -0700174}
175
176template<class T> void
177Fixture<T>::onDeleteTimeout(const Interest& interest)
178{
Junxiao Shi2b7b8312017-06-16 03:43:24 +0000179 BOOST_ERROR("Delete command timeout");
Shuo Chenca329182014-03-19 18:05:18 -0700180}
181
182template<class T> void
183Fixture<T>::sendInsertInterest(const Interest& insertInterest)
184{
185 insertFace.expressInterest(insertInterest,
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800186 std::bind(&Fixture<T>::onInsertData, this, _1, _2),
187 std::bind(&Fixture<T>::onInsertTimeout, this, _1), // Nack
188 std::bind(&Fixture<T>::onInsertTimeout, this, _1));
Shuo Chenca329182014-03-19 18:05:18 -0700189}
190
191template<class T> void
192Fixture<T>::sendDeleteInterest(const Interest& deleteInterest)
193{
194 deleteFace.expressInterest(deleteInterest,
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800195 std::bind(&Fixture<T>::onDeleteData, this, _1, _2),
196 std::bind(&Fixture<T>::onDeleteTimeout, this, _1), // Nack
197 std::bind(&Fixture<T>::onDeleteTimeout, this, _1));
Shuo Chenca329182014-03-19 18:05:18 -0700198}
199
200template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800201Fixture<T>::checkInsertOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700202{
Shuo Chenca329182014-03-19 18:05:18 -0700203 BOOST_TEST_MESSAGE(interest);
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800204 std::shared_ptr<Data> data = handle->readData(interest);
Weiqi Shif0330d52014-07-09 10:54:27 -0700205 if (data) {
206 int rc = memcmp(data->getContent().value(), content, sizeof(content));
207 BOOST_CHECK_EQUAL(rc, 0);
208 }
209 else {
weijia yuan82cf9142018-10-21 12:25:02 -0700210 BOOST_ERROR("Check Insert Failed");
Weiqi Shif0330d52014-07-09 10:54:27 -0700211 }
Shuo Chenca329182014-03-19 18:05:18 -0700212}
213
214template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800215Fixture<T>::checkDeleteOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700216{
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800217 std::map<Name, Name>::iterator name = deleteNamePairs.find(interest.getName());
218 BOOST_CHECK_MESSAGE(name != deleteNamePairs.end(), "Delete name not found: " << interest.getName());
219 Interest dataInterest(name->second);
220 std::shared_ptr<Data> data = handle->readData(dataInterest);
221 BOOST_CHECK(!data);
Shuo Chenca329182014-03-19 18:05:18 -0700222}
223
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800224
Shuo Chenca329182014-03-19 18:05:18 -0700225template<class T> void
226Fixture<T>::scheduleInsertEvent()
227{
228 int timeCount = 1;
229 for (typename T::DataContainer::iterator i = this->data.begin();
230 i != this->data.end(); ++i) {
231 Name insertCommandName("/repo/command/insert");
232 RepoCommandParameter insertParameter;
233 insertParameter.setName(Name((*i)->getName())
Davide Pesavento49f3a5f2017-09-23 01:36:33 -0400234 .appendNumber(ndn::random::generateWord64()));
Shuo Chenca329182014-03-19 18:05:18 -0700235 insertCommandName.append(insertParameter.wireEncode());
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800236 Interest insertInterest = signer.makeCommandInterest(insertCommandName);
weijia yuan82cf9142018-10-21 12:25:02 -0700237 // schedule a job to express insertInterest every 50ms
Shuo Chenca329182014-03-19 18:05:18 -0700238 scheduler.scheduleEvent(milliseconds(timeCount * 50 + 1000),
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800239 std::bind(&Fixture<T>::sendInsertInterest, this, insertInterest));
weijia yuan82cf9142018-10-21 12:25:02 -0700240 // schedule what to do when interest timeout
Shuo Chenca329182014-03-19 18:05:18 -0700241 EventId delayEventId = scheduler.scheduleEvent(milliseconds(5000 + timeCount * 50),
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800242 std::bind(&Fixture<T>::delayedInterest, this));
Shuo Chenca329182014-03-19 18:05:18 -0700243 insertEvents[insertParameter.getName()] = delayEventId;
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800244 // The delayEvent will be canceled in onInsertInterest
Shuo Chenca329182014-03-19 18:05:18 -0700245 insertFace.setInterestFilter(insertParameter.getName(),
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800246 std::bind(&Fixture<T>::onInsertInterest, this, _2),
Wentao Shang91fb4f22014-05-20 10:55:22 -0700247 ndn::RegisterPrefixSuccessCallback(),
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800248 std::bind(&Fixture<T>::onRegisterFailed, this, _2));
Shuo Chenca329182014-03-19 18:05:18 -0700249 timeCount++;
250 }
251}
252
Shuo Chenca329182014-03-19 18:05:18 -0700253template<class T> void
254Fixture<T>::scheduleDeleteEvent()
255{
256 int timeCount = 1;
257 for (typename T::DataContainer::iterator i = this->data.begin();
258 i != this->data.end(); ++i) {
259 Name deleteCommandName("/repo/command/delete");
260 RepoCommandParameter deleteParameter;
Davide Pesavento49f3a5f2017-09-23 01:36:33 -0400261 deleteParameter.setProcessId(ndn::random::generateWord64());
Shuo Chenca329182014-03-19 18:05:18 -0700262 deleteParameter.setName((*i)->getName());
263 deleteCommandName.append(deleteParameter.wireEncode());
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800264 Interest deleteInterest = signer.makeCommandInterest(deleteCommandName);
265 deleteNamePairs[deleteInterest.getName()] = (*i)->getName();
Shuo Chenca329182014-03-19 18:05:18 -0700266 scheduler.scheduleEvent(milliseconds(4000 + timeCount * 50),
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800267 std::bind(&Fixture<T>::sendDeleteInterest, this, deleteInterest));
Shuo Chenca329182014-03-19 18:05:18 -0700268 timeCount++;
269 }
270}
271
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800272using Datasets = boost::mpl::vector<BasicDataset,
273 FetchByPrefixDataset,
274 SamePrefixDataset<10>>;
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700275
276BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertDelete, T, Datasets, Fixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700277{
Shuo Chenca329182014-03-19 18:05:18 -0700278 // schedule events
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800279 this->scheduler.scheduleEvent(0_s, std::bind(&Fixture<T>::scheduleInsertEvent, this));
280 this->scheduler.scheduleEvent(10_s, std::bind(&Fixture<T>::scheduleDeleteEvent, this));
Shuo Chenca329182014-03-19 18:05:18 -0700281
weijia yuan82cf9142018-10-21 12:25:02 -0700282 this->repoFace.processEvents(30_s);
Shuo Chenca329182014-03-19 18:05:18 -0700283}
284
285BOOST_AUTO_TEST_SUITE_END()
286
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800287} // namespace tests
288} // namespace repo