blob: f9e58e3e4ea9f38a60c2a6e5220561d06a6c4d67 [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -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.
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
20#include "handles/write-handle.hpp"
21#include "handles/delete-handle.hpp"
Weiqi Shif0330d52014-07-09 10:54:27 -070022#include "storage/sqlite-storage.hpp"
23#include "storage/repo-storage.hpp"
Shuo Chen028dcd32014-06-21 16:36:44 +080024#include "common.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070025
Weiqi Shif0330d52014-07-09 10:54:27 -070026#include "../repo-storage-fixture.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070027#include "../dataset-fixtures.hpp"
28
Shuo Chen028dcd32014-06-21 16:36:44 +080029#include <ndn-cxx/util/random.hpp>
30#include <ndn-cxx/util/io.hpp>
Weiqi Shif0330d52014-07-09 10:54:27 -070031#include <boost/preprocessor/comparison/not_equal.hpp>
Shuo Chenca329182014-03-19 18:05:18 -070032#include <boost/test/unit_test.hpp>
Shuo Chen028dcd32014-06-21 16:36:44 +080033#include <fstream>
Shuo Chenca329182014-03-19 18:05:18 -070034
35namespace repo {
36namespace tests {
37
38using ndn::time::milliseconds;
39using ndn::time::seconds;
40using ndn::EventId;
41namespace random=ndn::random;
42
43//All the test cases in this test suite should be run at once.
44BOOST_AUTO_TEST_SUITE(TestBasicCommandInsertDelete)
45
46const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
47
48template<class Dataset>
Weiqi Shif0330d52014-07-09 10:54:27 -070049class Fixture : public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070050{
51public:
52 Fixture()
53 : scheduler(repoFace.getIoService())
Shuo Chen028dcd32014-06-21 16:36:44 +080054 , validator(repoFace)
Shuo Chenca329182014-03-19 18:05:18 -070055 , writeHandle(repoFace, *handle, keyChain, scheduler, validator)
56 , deleteHandle(repoFace, *handle, keyChain, scheduler, validator)
57 , insertFace(repoFace.getIoService())
58 , deleteFace(repoFace.getIoService())
59 {
Shuo Chenca329182014-03-19 18:05:18 -070060 writeHandle.listen(Name("/repo/command"));
61 deleteHandle.listen(Name("/repo/command"));
62 }
63
64 ~Fixture()
65 {
66 repoFace.getIoService().stop();
67 }
68
69 void
Shuo Chen028dcd32014-06-21 16:36:44 +080070 generateDefaultCertificateFile();
71
72 void
Shuo Chenca329182014-03-19 18:05:18 -070073 scheduleInsertEvent();
74
75 void
76 scheduleDeleteEvent();
77
78 void
79 onInsertInterest(const Interest& interest);
80
81 void
82 onRegisterFailed(const std::string& reason);
83
84 void
85 delayedInterest();
86
87 void
88 stopFaceProcess();
89
90
91 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080092 onInsertData(const Interest& interest, const Data& data);
Shuo Chenca329182014-03-19 18:05:18 -070093
94 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080095 onDeleteData(const Interest& interest, const Data& data);
Shuo Chenca329182014-03-19 18:05:18 -070096
97 void
98 onInsertTimeout(const Interest& interest);
99
100 void
101 onDeleteTimeout(const Interest& interest);
102
103 void
104 sendInsertInterest(const Interest& interest);
105
106 void
107 sendDeleteInterest(const Interest& deleteInterest);
108
109 void
Shuo Chen028dcd32014-06-21 16:36:44 +0800110 checkInsertOk(const Interest& interest);
Shuo Chenca329182014-03-19 18:05:18 -0700111
112 void
Shuo Chen028dcd32014-06-21 16:36:44 +0800113 checkDeleteOk(const Interest& interest);
Shuo Chenca329182014-03-19 18:05:18 -0700114
115public:
116 Face repoFace;
117 Scheduler scheduler;
Shuo Chen028dcd32014-06-21 16:36:44 +0800118 ValidatorConfig validator;
Shuo Chenca329182014-03-19 18:05:18 -0700119 KeyChain keyChain;
Shuo Chenca329182014-03-19 18:05:18 -0700120 WriteHandle writeHandle;
121 DeleteHandle deleteHandle;
122 Face insertFace;
123 Face deleteFace;
124 std::map<Name, EventId> insertEvents;
125};
126
Shuo Chen028dcd32014-06-21 16:36:44 +0800127template<class T> void
128Fixture<T>::generateDefaultCertificateFile()
129{
130 Name defaultIdentity = keyChain.getDefaultIdentity();
131 Name defaultKeyname = keyChain.getDefaultKeyNameForIdentity(defaultIdentity);
132 Name defaultCertficateName = keyChain.getDefaultCertificateNameForKey(defaultKeyname);
133 shared_ptr<ndn::IdentityCertificate> defaultCertficate =
134 keyChain.getCertificate(defaultCertficateName);
135 //test-integrated should run in root directory of repo-ng.
136 //certificate file should be removed after tests for security issue.
137 std::fstream certificateFile("tests/integrated/insert-delete-test.cert",
138 std::ios::out | std::ios::binary | std::ios::trunc);
139 ndn::io::save(*defaultCertficate, certificateFile);
140 certificateFile.close();
141}
142
Shuo Chenca329182014-03-19 18:05:18 -0700143template<class T> void
144Fixture<T>::onInsertInterest(const Interest& interest)
145{
146 Data data(Name(interest.getName()));
147 data.setContent(content, sizeof(content));
148 data.setFreshnessPeriod(milliseconds(0));
Shuo Chenc88c87d2014-06-25 20:21:02 +0800149 keyChain.signByIdentity(data, keyChain.getDefaultIdentity());
Shuo Chenca329182014-03-19 18:05:18 -0700150 insertFace.put(data);
Shuo Chenca329182014-03-19 18:05:18 -0700151 std::map<Name, EventId>::iterator event = insertEvents.find(interest.getName());
152 if (event != insertEvents.end()) {
153 scheduler.cancelEvent(event->second);
154 insertEvents.erase(event);
155 }
Shuo Chen028dcd32014-06-21 16:36:44 +0800156 // schedule an event 50ms later to check whether insert is Ok
Weiqi Shif0330d52014-07-09 10:54:27 -0700157 scheduler.scheduleEvent(milliseconds(500),
Shuo Chen028dcd32014-06-21 16:36:44 +0800158 bind(&Fixture<T>::checkInsertOk, this, interest));
Shuo Chenca329182014-03-19 18:05:18 -0700159
160}
161
162
163template<class T> void
164Fixture<T>::onRegisterFailed(const std::string& reason)
165{
166 BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason);
167}
168
169template<class T> void
170Fixture<T>::delayedInterest()
171{
172 BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong");
173}
174
175template<class T> void
176Fixture<T>::stopFaceProcess()
177{
178 repoFace.getIoService().stop();
179}
180
181template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800182Fixture<T>::onInsertData(const Interest& interest, const Data& data)
Shuo Chenca329182014-03-19 18:05:18 -0700183{
184 RepoCommandResponse response;
185 response.wireDecode(data.getContent().blockFromValue());
186 int statusCode = response.getStatusCode();
187 BOOST_CHECK_EQUAL(statusCode, 100);
Weiqi Shif0330d52014-07-09 10:54:27 -0700188 // std::cout<<"statuse code of insert name = "<<response.getName()<<std::endl;
Shuo Chenca329182014-03-19 18:05:18 -0700189}
190
191template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800192Fixture<T>::onDeleteData(const Interest& interest, const Data& data)
Shuo Chenca329182014-03-19 18:05:18 -0700193{
194 RepoCommandResponse response;
195 response.wireDecode(data.getContent().blockFromValue());
196 int statusCode = response.getStatusCode();
197 BOOST_CHECK_EQUAL(statusCode, 200);
198
Shuo Chen028dcd32014-06-21 16:36:44 +0800199 //schedlute an event to check whether delete is Ok.
Shuo Chenca329182014-03-19 18:05:18 -0700200 scheduler.scheduleEvent(milliseconds(100),
Shuo Chen028dcd32014-06-21 16:36:44 +0800201 bind(&Fixture<T>::checkDeleteOk, this, interest));
Shuo Chenca329182014-03-19 18:05:18 -0700202}
203
204template<class T> void
205Fixture<T>::onInsertTimeout(const Interest& interest)
206{
207 BOOST_ERROR("Inserert command timeout");
208}
209
210template<class T> void
211Fixture<T>::onDeleteTimeout(const Interest& interest)
212{
213 BOOST_ERROR("delete command timeout");
214}
215
216template<class T> void
217Fixture<T>::sendInsertInterest(const Interest& insertInterest)
218{
219 insertFace.expressInterest(insertInterest,
220 bind(&Fixture<T>::onInsertData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800221 bind(&Fixture<T>::onInsertTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700222 bind(&Fixture<T>::onInsertTimeout, this, _1));
223}
224
225template<class T> void
226Fixture<T>::sendDeleteInterest(const Interest& deleteInterest)
227{
228 deleteFace.expressInterest(deleteInterest,
229 bind(&Fixture<T>::onDeleteData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800230 bind(&Fixture<T>::onDeleteTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700231 bind(&Fixture<T>::onDeleteTimeout, this, _1));
232}
233
234template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800235Fixture<T>::checkInsertOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700236{
Shuo Chenca329182014-03-19 18:05:18 -0700237 BOOST_TEST_MESSAGE(interest);
Weiqi Shif0330d52014-07-09 10:54:27 -0700238 shared_ptr<Data> data = handle->readData(interest);
239 if (data) {
240 int rc = memcmp(data->getContent().value(), content, sizeof(content));
241 BOOST_CHECK_EQUAL(rc, 0);
242 }
243 else {
244 std::cerr<<"Check Insert Failed"<<std::endl;
245 }
Shuo Chenca329182014-03-19 18:05:18 -0700246}
247
248template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800249Fixture<T>::checkDeleteOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700250{
Weiqi Shif0330d52014-07-09 10:54:27 -0700251 shared_ptr<Data> data = handle->readData(interest);
252 BOOST_CHECK_EQUAL(data, shared_ptr<Data>());
Shuo Chenca329182014-03-19 18:05:18 -0700253}
254
Shuo Chenca329182014-03-19 18:05:18 -0700255template<class T> void
256Fixture<T>::scheduleInsertEvent()
257{
258 int timeCount = 1;
259 for (typename T::DataContainer::iterator i = this->data.begin();
260 i != this->data.end(); ++i) {
261 Name insertCommandName("/repo/command/insert");
262 RepoCommandParameter insertParameter;
263 insertParameter.setName(Name((*i)->getName())
264 .appendNumber(random::generateWord64()));
265
266 insertCommandName.append(insertParameter.wireEncode());
267 Interest insertInterest(insertCommandName);
Shuo Chen028dcd32014-06-21 16:36:44 +0800268 keyChain.signByIdentity(insertInterest, keyChain.getDefaultIdentity());
Shuo Chenca329182014-03-19 18:05:18 -0700269 //schedule a job to express insertInterest every 50ms
270 scheduler.scheduleEvent(milliseconds(timeCount * 50 + 1000),
271 bind(&Fixture<T>::sendInsertInterest, this, insertInterest));
272 //schedule what to do when interest timeout
273
274 EventId delayEventId = scheduler.scheduleEvent(milliseconds(5000 + timeCount * 50),
275 bind(&Fixture<T>::delayedInterest, this));
276 insertEvents[insertParameter.getName()] = delayEventId;
277
278 //The delayEvent will be canceled in onInsertInterest
279 insertFace.setInterestFilter(insertParameter.getName(),
280 bind(&Fixture<T>::onInsertInterest, this, _2),
Wentao Shang91fb4f22014-05-20 10:55:22 -0700281 ndn::RegisterPrefixSuccessCallback(),
Shuo Chenca329182014-03-19 18:05:18 -0700282 bind(&Fixture<T>::onRegisterFailed, this, _2));
283 timeCount++;
284 }
285}
286
Shuo Chenca329182014-03-19 18:05:18 -0700287template<class T> void
288Fixture<T>::scheduleDeleteEvent()
289{
290 int timeCount = 1;
291 for (typename T::DataContainer::iterator i = this->data.begin();
292 i != this->data.end(); ++i) {
293 Name deleteCommandName("/repo/command/delete");
294 RepoCommandParameter deleteParameter;
295 static boost::random::mt19937_64 gen;
296 static boost::random::uniform_int_distribution<uint64_t> dist(0, 0xFFFFFFFFFFFFFFFFLL);
297 deleteParameter.setProcessId(dist(gen));
298 deleteParameter.setName((*i)->getName());
299 deleteCommandName.append(deleteParameter.wireEncode());
300 Interest deleteInterest(deleteCommandName);
Shuo Chen028dcd32014-06-21 16:36:44 +0800301 keyChain.signByIdentity(deleteInterest, keyChain.getDefaultIdentity());
Shuo Chenca329182014-03-19 18:05:18 -0700302 scheduler.scheduleEvent(milliseconds(4000 + timeCount * 50),
303 bind(&Fixture<T>::sendDeleteInterest, this, deleteInterest));
304 timeCount++;
305 }
306}
307
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700308typedef boost::mpl::vector< BasicDataset,
309 FetchByPrefixDataset,
310 BasicChildSelectorDataset,
311 ExtendedChildSelectorDataset,
312 SamePrefixDataset<10> > Datasets;
313
314BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertDelete, T, Datasets, Fixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700315{
Shuo Chen028dcd32014-06-21 16:36:44 +0800316 this->generateDefaultCertificateFile();
317 this->validator.load("tests/integrated/insert-delete-validator-config.conf");
318
Shuo Chenca329182014-03-19 18:05:18 -0700319 // schedule events
320 this->scheduler.scheduleEvent(seconds(0),
321 bind(&Fixture<T>::scheduleInsertEvent, this));
322 this->scheduler.scheduleEvent(seconds(10),
323 bind(&Fixture<T>::scheduleDeleteEvent, this));
324
325 // schedule an event to terminate IO
Weiqi Shif0330d52014-07-09 10:54:27 -0700326 this->scheduler.scheduleEvent(seconds(30),
Shuo Chenca329182014-03-19 18:05:18 -0700327 bind(&Fixture<T>::stopFaceProcess, this));
328 this->repoFace.getIoService().run();
329}
330
331BOOST_AUTO_TEST_SUITE_END()
332
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800333} // namespace tests
334} // namespace repo