11/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*-*/
22/*
3- * Copyright (c) 2014-2021 , Regents of the University of California.
3+ * Copyright (c) 2014-2022 , Regents of the University of California.
44 *
55 * This file is part of NDN repo-ng (Next generation of NDN repository).
66 * See AUTHORS.md for complete list of repo-ng authors and contributors.
3232#include < ndn-cxx/util/random.hpp>
3333#include < ndn-cxx/util/time.hpp>
3434
35- #include < boost/asio/io_service.hpp>
3635#include < boost/mpl/vector.hpp>
3736#include < boost/test/unit_test.hpp>
3837
@@ -44,7 +43,7 @@ using ndn::time::milliseconds;
4443// All the test cases in this test suite should be run at once.
4544BOOST_AUTO_TEST_SUITE (TestBasicCommandInsertDelete)
4645
47- const static uint8_t content[ 8 ] = {3 ,1 ,4 ,1 ,5 ,9 ,2 ,6 };
46+ const uint8_t CONTENT[ ] = {3 ,1 ,4 ,1 ,5 ,9 ,2 ,6 };
4847
4948template <class Dataset >
5049class Fixture :public CommandFixture ,public RepoStorageFixture ,public Dataset
@@ -59,7 +58,7 @@ class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset
5958 {
6059 NamecmdPrefix (" /repo/command" );
6160 repoFace.registerPrefix (cmdPrefix,nullptr ,
62- [] (const Name& cmdPrefix ,const std::string& reason) {
61+ [] (const Name&,const std::string& reason) {
6362BOOST_FAIL (" Command prefix registration error:" << reason);
6463 });
6564 }
@@ -113,11 +112,12 @@ class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset
113112 ndn::security::InterestSigner signer;
114113};
115114
116- template <class T >void
115+ template <class T >
116+ void
117117Fixture<T>::onInsertInterest(const Interest& interest)
118118{
119119 Datadata (Name (interest.getName ()));
120- data.setContent (content, sizeof (content) );
120+ data.setContent (CONTENT );
121121 data.setFreshnessPeriod (0_ms);
122122 keyChain.sign (data);
123123 insertFace.put (data);
@@ -126,56 +126,62 @@ Fixture<T>::onInsertInterest(const Interest& interest)
126126 eventIt->second .cancel ();
127127 insertEvents.erase (eventIt);
128128 }
129- // schedule an event 50ms later to check whether insert is Ok
130- scheduler.schedule (500_ms,std::bind (&Fixture<T>::checkInsertOk,this , interest));
129+
130+ // schedule an event to check whether insert is ok
131+ scheduler.schedule (500_ms, [=] {this ->checkInsertOk (interest); });
131132}
132133
133- template <class T >void
134+ template <class T >
135+ void
134136Fixture<T>::onRegisterFailed(const std::string& reason)
135137{
136138BOOST_ERROR (" ERROR: Failed to register prefix in local hub's daemon" + reason);
137139}
138140
139- template <class T >void
141+ template <class T >
142+ void
140143Fixture<T>::delayedInterest()
141144{
142145BOOST_ERROR (" Fetching interest does not come. It may be satisfied in CS or something is wrong" );
143146}
144147
145- template <class T >void
146- Fixture<T>::onInsertData(const Interest& interest,const Data& data)
148+ template <class T >
149+ void
150+ Fixture<T>::onInsertData(const Interest&,const Data& data)
147151{
148152 RepoCommandResponse response;
149153 response.wireDecode (data.getContent ().blockFromValue ());
150- int statusCode = response.getCode ();
151- BOOST_CHECK_EQUAL (statusCode,100 );
154+ BOOST_CHECK_EQUAL (response.getCode (),100 );
152155}
153156
154- template <class T >void
157+ template <class T >
158+ void
155159Fixture<T>::onDeleteData(const Interest& interest,const Data& data)
156160{
157161 RepoCommandResponse response;
158162 response.wireDecode (data.getContent ().blockFromValue ());
159- int statusCode = response.getCode ();
160- BOOST_CHECK_EQUAL (statusCode,200 );
163+ BOOST_CHECK_EQUAL (response.getCode (),200 );
161164
162- // schedlute an event to check whether delete isOk.
163- scheduler.schedule (100_ms,std::bind (&Fixture<T>::checkDeleteOk, this , interest));
165+ // schedule an event to check whether delete isok
166+ scheduler.schedule (100_ms,[=] { this -> checkDeleteOk ( interest); } );
164167}
165168
166- template <class T >void
167- Fixture<T>::onInsertTimeout(const Interest& interest)
169+ template <class T >
170+ void
171+ Fixture<T>::onInsertTimeout(const Interest&)
168172{
169173BOOST_ERROR (" Insert command timeout" );
170174}
171175
172- template <class T >void
173- Fixture<T>::onDeleteTimeout(const Interest& interest)
176+ template <class T >
177+ void
178+ Fixture<T>::onDeleteTimeout(const Interest&)
174179{
175180BOOST_ERROR (" Delete command timeout" );
176181}
177182
178- template <class T >void
183+ template <class T >
184+ void
179185Fixture<T>::sendInsertInterest(const Interest& insertInterest)
180186{
181187 insertFace.expressInterest (insertInterest,
@@ -184,7 +190,8 @@ Fixture<T>::sendInsertInterest(const Interest& insertInterest)
184190std::bind (&Fixture<T>::onInsertTimeout,this , _1));
185191}
186192
187- template <class T >void
193+ template <class T >
194+ void
188195Fixture<T>::sendDeleteInterest(const Interest& deleteInterest)
189196{
190197 deleteFace.expressInterest (deleteInterest,
@@ -193,41 +200,43 @@ Fixture<T>::sendDeleteInterest(const Interest& deleteInterest)
193200std::bind (&Fixture<T>::onDeleteTimeout,this , _1));
194201}
195202
196- template <class T >void
203+ template <class T >
204+ void
197205Fixture<T>::checkInsertOk(const Interest& interest)
198206{
199- BOOST_TEST_MESSAGE ( interest);
200- std::shared_ptr<Data> data = handle->readData (interest);
201- if (data) {
202- int rc = memcmp (data->getContent ().value (), content, sizeof (content ));
203- BOOST_CHECK_EQUAL (rc, 0 );
204- }
205- else {
206- BOOST_ERROR ( " Check Insert Failed " );
207+ BOOST_TEST_CONTEXT ( " Interest " << interest) {
208+ auto data = handle->readData (interest);
209+ if (data) {
210+ BOOST_TEST (data->getContent ().value_bytes () == CONTENT, boost::test_tools::per_element ( ));
211+ }
212+ else {
213+ BOOST_ERROR ( " Insert check failed " );
214+ }
207215 }
208216}
209217
210- template <class T >void
218+ template <class T >
219+ void
211220Fixture<T>::checkDeleteOk(const Interest& interest)
212221{
213- std::map<Name, Name>::iterator name = deleteNamePairs.find (interest.getName ());
214- BOOST_CHECK_MESSAGE (name != deleteNamePairs.end ()," Delete name not found:" << interest.getName ());
215- InterestdataInterest (name ->second );
216- std::shared_ptr<Data> data = handle->readData (dataInterest);
222+ auto nameIt = deleteNamePairs.find (interest.getName ());
223+ BOOST_CHECK_MESSAGE (nameIt != deleteNamePairs.end ()," Delete name not found:" << interest.getName ());
224+ InterestdataInterest (nameIt ->second );
225+ auto data = handle->readData (dataInterest);
217226BOOST_CHECK (!data);
218227}
219228
220- template <class T >void
229+ template <class T >
230+ void
221231Fixture<T>::scheduleInsertEvent()
222232{
223233int timeCount =1 ;
224- for (typename T::DataContainer::iterator i =this ->data .begin ();
225- i !=this ->data .end (); ++i) {
234+ for (auto i =this ->data .begin (); i !=this ->data .end (); ++i) {
226235 NameinsertCommandName (" /repo/command/insert" );
227236 RepoCommandParameter insertParameter;
228237 insertParameter.setName (Name ((*i)->getName ())
229238 .appendNumber (ndn::random::generateWord64 ()));
230- insertCommandName.append (insertParameter.wireEncode ());
239+ insertCommandName.append (tlv::GenericNameComponent, insertParameter.wireEncode ());
231240 Interest insertInterest = signer.makeCommandInterest (insertCommandName);
232241// schedule a job to express insertInterest every 50ms
233242 scheduler.schedule (milliseconds (timeCount *50 +1000 ),
@@ -245,17 +254,17 @@ Fixture<T>::scheduleInsertEvent()
245254 }
246255}
247256
248- template <class T >void
257+ template <class T >
258+ void
249259Fixture<T>::scheduleDeleteEvent()
250260{
251261int timeCount =1 ;
252- for (typename T::DataContainer::iterator i =this ->data .begin ();
253- i !=this ->data .end (); ++i) {
262+ for (auto i =this ->data .begin (); i !=this ->data .end (); ++i) {
254263 NamedeleteCommandName (" /repo/command/delete" );
255264 RepoCommandParameter deleteParameter;
256265 deleteParameter.setProcessId (ndn::random::generateWord64 ());
257266 deleteParameter.setName ((*i)->getName ());
258- deleteCommandName.append (deleteParameter.wireEncode ());
267+ deleteCommandName.append (tlv::GenericNameComponent, deleteParameter.wireEncode ());
259268 Interest deleteInterest = signer.makeCommandInterest (deleteCommandName);
260269 deleteNamePairs[deleteInterest.getName ()] = (*i)->getName ();
261270 scheduler.schedule (milliseconds (4000 + timeCount *50 ),
@@ -271,8 +280,8 @@ using Datasets = boost::mpl::vector<BasicDataset,
271280BOOST_FIXTURE_TEST_CASE_TEMPLATE (InsertDelete, T, Datasets, Fixture<T>)
272281{
273282// schedule events
274- this ->scheduler .schedule (0_s,std::bind (&Fixture<T>::scheduleInsertEvent, this ) );
275- this ->scheduler .schedule (10_s,std::bind (&Fixture<T>::scheduleDeleteEvent, this ) );
283+ this ->scheduler .schedule (0_s, [ this ] { this -> scheduleInsertEvent (); } );
284+ this ->scheduler .schedule (10_s,[ this ] { this -> scheduleDeleteEvent (); } );
276285
277286this ->repoFace .processEvents (30_s);
278287}