blob: 10775f7a01c4e149d1c00ec73807a0f8dfc5f6f2 [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
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
92 onInsertData(const Interest& interest, Data& data);
93
94 void
95 onDeleteData(const Interest& interest, Data& data);
96
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
182Fixture<T>::onInsertData(const Interest& interest, Data& data)
183{
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
192Fixture<T>::onDeleteData(const Interest& interest, Data& data)
193{
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),
221 bind(&Fixture<T>::onInsertTimeout, this, _1));
222}
223
224template<class T> void
225Fixture<T>::sendDeleteInterest(const Interest& deleteInterest)
226{
227 deleteFace.expressInterest(deleteInterest,
228 bind(&Fixture<T>::onDeleteData, this, _1, _2),
229 bind(&Fixture<T>::onDeleteTimeout, this, _1));
230}
231
232template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800233Fixture<T>::checkInsertOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700234{
Shuo Chenca329182014-03-19 18:05:18 -0700235 BOOST_TEST_MESSAGE(interest);
Weiqi Shif0330d52014-07-09 10:54:27 -0700236 shared_ptr<Data> data = handle->readData(interest);
237 if (data) {
238 int rc = memcmp(data->getContent().value(), content, sizeof(content));
239 BOOST_CHECK_EQUAL(rc, 0);
240 }
241 else {
242 std::cerr<<"Check Insert Failed"<<std::endl;
243 }
Shuo Chenca329182014-03-19 18:05:18 -0700244}
245
246template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800247Fixture<T>::checkDeleteOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700248{
Weiqi Shif0330d52014-07-09 10:54:27 -0700249 shared_ptr<Data> data = handle->readData(interest);
250 BOOST_CHECK_EQUAL(data, shared_ptr<Data>());
Shuo Chenca329182014-03-19 18:05:18 -0700251}
252
Shuo Chenca329182014-03-19 18:05:18 -0700253template<class T> void
254Fixture<T>::scheduleInsertEvent()
255{
256 int timeCount = 1;
257 for (typename T::DataContainer::iterator i = this->data.begin();
258 i != this->data.end(); ++i) {
259 Name insertCommandName("/repo/command/insert");
260 RepoCommandParameter insertParameter;
261 insertParameter.setName(Name((*i)->getName())
262 .appendNumber(random::generateWord64()));
263
264 insertCommandName.append(insertParameter.wireEncode());
265 Interest insertInterest(insertCommandName);
Shuo Chen028dcd32014-06-21 16:36:44 +0800266 keyChain.signByIdentity(insertInterest, keyChain.getDefaultIdentity());
Shuo Chenca329182014-03-19 18:05:18 -0700267 //schedule a job to express insertInterest every 50ms
268 scheduler.scheduleEvent(milliseconds(timeCount * 50 + 1000),
269 bind(&Fixture<T>::sendInsertInterest, this, insertInterest));
270 //schedule what to do when interest timeout
271
272 EventId delayEventId = scheduler.scheduleEvent(milliseconds(5000 + timeCount * 50),
273 bind(&Fixture<T>::delayedInterest, this));
274 insertEvents[insertParameter.getName()] = delayEventId;
275
276 //The delayEvent will be canceled in onInsertInterest
277 insertFace.setInterestFilter(insertParameter.getName(),
278 bind(&Fixture<T>::onInsertInterest, this, _2),
Wentao Shang91fb4f22014-05-20 10:55:22 -0700279 ndn::RegisterPrefixSuccessCallback(),
Shuo Chenca329182014-03-19 18:05:18 -0700280 bind(&Fixture<T>::onRegisterFailed, this, _2));
281 timeCount++;
282 }
283}
284
Shuo Chenca329182014-03-19 18:05:18 -0700285template<class T> void
286Fixture<T>::scheduleDeleteEvent()
287{
288 int timeCount = 1;
289 for (typename T::DataContainer::iterator i = this->data.begin();
290 i != this->data.end(); ++i) {
291 Name deleteCommandName("/repo/command/delete");
292 RepoCommandParameter deleteParameter;
293 static boost::random::mt19937_64 gen;
294 static boost::random::uniform_int_distribution<uint64_t> dist(0, 0xFFFFFFFFFFFFFFFFLL);
295 deleteParameter.setProcessId(dist(gen));
296 deleteParameter.setName((*i)->getName());
297 deleteCommandName.append(deleteParameter.wireEncode());
298 Interest deleteInterest(deleteCommandName);
Shuo Chen028dcd32014-06-21 16:36:44 +0800299 keyChain.signByIdentity(deleteInterest, keyChain.getDefaultIdentity());
Shuo Chenca329182014-03-19 18:05:18 -0700300 scheduler.scheduleEvent(milliseconds(4000 + timeCount * 50),
301 bind(&Fixture<T>::sendDeleteInterest, this, deleteInterest));
302 timeCount++;
303 }
304}
305
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700306typedef boost::mpl::vector< BasicDataset,
307 FetchByPrefixDataset,
308 BasicChildSelectorDataset,
309 ExtendedChildSelectorDataset,
310 SamePrefixDataset<10> > Datasets;
311
312BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertDelete, T, Datasets, Fixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700313{
Shuo Chen028dcd32014-06-21 16:36:44 +0800314 this->generateDefaultCertificateFile();
315 this->validator.load("tests/integrated/insert-delete-validator-config.conf");
316
Shuo Chenca329182014-03-19 18:05:18 -0700317 // schedule events
318 this->scheduler.scheduleEvent(seconds(0),
319 bind(&Fixture<T>::scheduleInsertEvent, this));
320 this->scheduler.scheduleEvent(seconds(10),
321 bind(&Fixture<T>::scheduleDeleteEvent, this));
322
323 // schedule an event to terminate IO
Weiqi Shif0330d52014-07-09 10:54:27 -0700324 this->scheduler.scheduleEvent(seconds(30),
Shuo Chenca329182014-03-19 18:05:18 -0700325 bind(&Fixture<T>::stopFaceProcess, this));
326 this->repoFace.getIoService().run();
327}
328
329BOOST_AUTO_TEST_SUITE_END()
330
331} //namespace tests
332} //namespace repo