Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 2 | /* |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 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 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 20 | #include "handles/delete-handle.hpp" |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 21 | #include "handles/write-handle.hpp" |
| 22 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 23 | #include "storage/repo-storage.hpp" |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 24 | #include "storage/sqlite-storage.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 26 | #include "command-fixture.hpp" |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 27 | #include "../repo-storage-fixture.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 28 | #include "../dataset-fixtures.hpp" |
| 29 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 30 | #include <ndn-cxx/security/command-interest-signer.hpp> |
| 31 | #include <ndn-cxx/security/signing-helpers.hpp> |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 32 | #include <ndn-cxx/util/random.hpp> |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 33 | #include <ndn-cxx/util/time.hpp> |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 34 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 35 | #include <boost/asio/io_service.hpp> |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 36 | #include <boost/mpl/vector.hpp> |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 37 | #include <boost/test/unit_test.hpp> |
| 38 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 39 | |
| 40 | #include <iostream> |
| 41 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 42 | namespace repo { |
| 43 | namespace tests { |
| 44 | |
| 45 | using ndn::time::milliseconds; |
| 46 | using ndn::time::seconds; |
| 47 | using ndn::EventId; |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 48 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 49 | // All the test cases in this test suite should be run at once. |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 50 | BOOST_AUTO_TEST_SUITE(TestBasicCommandInsertDelete) |
| 51 | |
| 52 | const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6}; |
| 53 | |
| 54 | template<class Dataset> |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 55 | class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 56 | { |
| 57 | public: |
| 58 | Fixture() |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 59 | : writeHandle(repoFace, *handle, dispatcher, scheduler, validator) |
| 60 | , deleteHandle(repoFace, *handle, dispatcher, scheduler, validator) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 61 | , insertFace(repoFace.getIoService()) |
| 62 | , deleteFace(repoFace.getIoService()) |
| 63 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 64 | 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 Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 71 | void |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 72 | 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 Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 87 | onInsertData(const Interest& interest, const Data& data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 88 | |
| 89 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 90 | onDeleteData(const Interest& interest, const Data& data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 91 | |
| 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 Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 105 | checkInsertOk(const Interest& interest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 106 | |
| 107 | void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 108 | checkDeleteOk(const Interest& interest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 109 | |
| 110 | public: |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 111 | WriteHandle writeHandle; |
| 112 | DeleteHandle deleteHandle; |
| 113 | Face insertFace; |
| 114 | Face deleteFace; |
| 115 | std::map<Name, EventId> insertEvents; |
| 116 | }; |
| 117 | |
| 118 | template<class T> void |
| 119 | Fixture<T>::onInsertInterest(const Interest& interest) |
| 120 | { |
| 121 | Data data(Name(interest.getName())); |
| 122 | data.setContent(content, sizeof(content)); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 123 | data.setFreshnessPeriod(0_ms); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 124 | keyChain.sign(data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 125 | insertFace.put(data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 126 | 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 Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 131 | // schedule an event 50ms later to check whether insert is Ok |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 132 | scheduler.scheduleEvent(500_ms, |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 133 | bind(&Fixture<T>::checkInsertOk, this, interest)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 134 | |
| 135 | } |
| 136 | |
| 137 | |
| 138 | template<class T> void |
| 139 | Fixture<T>::onRegisterFailed(const std::string& reason) |
| 140 | { |
| 141 | BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason); |
| 142 | } |
| 143 | |
| 144 | template<class T> void |
| 145 | Fixture<T>::delayedInterest() |
| 146 | { |
| 147 | BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong"); |
| 148 | } |
| 149 | |
| 150 | template<class T> void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 151 | Fixture<T>::onInsertData(const Interest& interest, const Data& data) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 152 | { |
| 153 | RepoCommandResponse response; |
| 154 | response.wireDecode(data.getContent().blockFromValue()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 155 | int statusCode = response.getCode(); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(statusCode, 100); |
| 157 | } |
| 158 | |
| 159 | template<class T> void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 160 | Fixture<T>::onDeleteData(const Interest& interest, const Data& data) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 161 | { |
| 162 | RepoCommandResponse response; |
| 163 | response.wireDecode(data.getContent().blockFromValue()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 164 | int statusCode = response.getCode(); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(statusCode, 200); |
| 166 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 167 | //schedlute an event to check whether delete is Ok. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 168 | scheduler.scheduleEvent(100_ms, |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 169 | bind(&Fixture<T>::checkDeleteOk, this, interest)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | template<class T> void |
| 173 | Fixture<T>::onInsertTimeout(const Interest& interest) |
| 174 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 175 | BOOST_ERROR("Insert command timeout"); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | template<class T> void |
| 179 | Fixture<T>::onDeleteTimeout(const Interest& interest) |
| 180 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 181 | BOOST_ERROR("Delete command timeout"); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | template<class T> void |
| 185 | Fixture<T>::sendInsertInterest(const Interest& insertInterest) |
| 186 | { |
| 187 | insertFace.expressInterest(insertInterest, |
| 188 | bind(&Fixture<T>::onInsertData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 189 | bind(&Fixture<T>::onInsertTimeout, this, _1), // Nack |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 190 | bind(&Fixture<T>::onInsertTimeout, this, _1)); |
| 191 | } |
| 192 | |
| 193 | template<class T> void |
| 194 | Fixture<T>::sendDeleteInterest(const Interest& deleteInterest) |
| 195 | { |
| 196 | deleteFace.expressInterest(deleteInterest, |
| 197 | bind(&Fixture<T>::onDeleteData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 198 | bind(&Fixture<T>::onDeleteTimeout, this, _1), // Nack |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 199 | bind(&Fixture<T>::onDeleteTimeout, this, _1)); |
| 200 | } |
| 201 | |
| 202 | template<class T> void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 203 | Fixture<T>::checkInsertOk(const Interest& interest) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 204 | { |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 205 | BOOST_TEST_MESSAGE(interest); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 206 | 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 yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 212 | BOOST_ERROR("Check Insert Failed"); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 213 | } |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | template<class T> void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 217 | Fixture<T>::checkDeleteOk(const Interest& interest) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 218 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 219 | shared_ptr<Data> data = handle->readData(interest); |
| 220 | BOOST_CHECK_EQUAL(data, shared_ptr<Data>()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 223 | template<class T> void |
| 224 | Fixture<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 Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 232 | .appendNumber(ndn::random::generateWord64())); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 233 | |
| 234 | insertCommandName.append(insertParameter.wireEncode()); |
| 235 | Interest insertInterest(insertCommandName); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 236 | keyChain.sign(insertInterest); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 237 | |
| 238 | // schedule a job to express insertInterest every 50ms |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 239 | scheduler.scheduleEvent(milliseconds(timeCount * 50 + 1000), |
| 240 | bind(&Fixture<T>::sendInsertInterest, this, insertInterest)); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 241 | // schedule what to do when interest timeout |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 242 | 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 Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 249 | ndn::RegisterPrefixSuccessCallback(), |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 250 | bind(&Fixture<T>::onRegisterFailed, this, _2)); |
| 251 | timeCount++; |
| 252 | } |
| 253 | } |
| 254 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 255 | template<class T> void |
| 256 | Fixture<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 Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 263 | deleteParameter.setProcessId(ndn::random::generateWord64()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 264 | deleteParameter.setName((*i)->getName()); |
| 265 | deleteCommandName.append(deleteParameter.wireEncode()); |
| 266 | Interest deleteInterest(deleteCommandName); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 267 | keyChain.sign(deleteInterest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 268 | scheduler.scheduleEvent(milliseconds(4000 + timeCount * 50), |
| 269 | bind(&Fixture<T>::sendDeleteInterest, this, deleteInterest)); |
| 270 | timeCount++; |
| 271 | } |
| 272 | } |
| 273 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 274 | typedef boost::mpl::vector<BasicDataset, |
| 275 | FetchByPrefixDataset, |
| 276 | BasicChildSelectorDataset, |
| 277 | ExtendedChildSelectorDataset, |
| 278 | SamePrefixDataset<10>> Datasets; |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 279 | |
| 280 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertDelete, T, Datasets, Fixture<T>) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 281 | { |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 282 | // schedule events |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 283 | this->scheduler.scheduleEvent(0_s, |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 284 | bind(&Fixture<T>::scheduleInsertEvent, this)); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 285 | this->scheduler.scheduleEvent(10_s, |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 286 | bind(&Fixture<T>::scheduleDeleteEvent, this)); |
| 287 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 288 | this->repoFace.processEvents(30_s); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | BOOST_AUTO_TEST_SUITE_END() |
| 292 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 293 | } // namespace tests |
| 294 | } // namespace repo |