Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -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 | /* |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -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 | |
| 20 | #include "handles/watch-handle.hpp" |
| 21 | #include "storage/sqlite-storage.hpp" |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 22 | |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 23 | #include "command-fixture.hpp" |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 24 | #include "../repo-storage-fixture.hpp" |
| 25 | #include "../dataset-fixtures.hpp" |
| 26 | |
| 27 | #include <ndn-cxx/util/random.hpp> |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 29 | #include <boost/mpl/vector.hpp> |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 30 | #include <boost/test/unit_test.hpp> |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 31 | |
| 32 | namespace repo { |
| 33 | namespace tests { |
| 34 | |
| 35 | using ndn::time::milliseconds; |
| 36 | using ndn::time::seconds; |
| 37 | using ndn::EventId; |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 39 | // All the test cases in this test suite should be run at once. |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(TestBasicCommandWatchDelete) |
| 41 | |
| 42 | const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6}; |
| 43 | |
| 44 | template<class Dataset> |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 45 | class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 46 | { |
| 47 | public: |
| 48 | Fixture() |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 49 | : watchHandle(repoFace, *handle, keyChain, scheduler, validator) |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 50 | , watchFace(repoFace.getIoService()) |
| 51 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 52 | Name cmdPrefix("/repo/command"); |
| 53 | repoFace.registerPrefix(cmdPrefix, nullptr, |
| 54 | [] (const Name& cmdPrefix, const std::string& reason) { |
| 55 | BOOST_FAIL("Command prefix registration error: " << reason); |
| 56 | }); |
| 57 | watchHandle.listen(cmdPrefix); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 60 | void |
| 61 | scheduleWatchEvent(); |
| 62 | |
| 63 | void |
| 64 | onWatchInterest(const Interest& interest); |
| 65 | |
| 66 | void |
| 67 | onRegisterFailed(const std::string& reason); |
| 68 | |
| 69 | void |
| 70 | delayedInterest(); |
| 71 | |
| 72 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 73 | onWatchData(const Interest& interest, const Data& data); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 74 | |
| 75 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 76 | onWatchStopData(const Interest& interest, const Data& data); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 77 | |
| 78 | void |
| 79 | onWatchTimeout(const Interest& interest); |
| 80 | |
| 81 | void |
| 82 | sendWatchStartInterest(const Interest& interest); |
| 83 | |
| 84 | void |
| 85 | sendWatchStopInterest(const Interest& interest); |
| 86 | |
| 87 | void |
| 88 | checkWatchOk(const Interest& interest); |
| 89 | |
| 90 | public: |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 91 | WatchHandle watchHandle; |
| 92 | Face watchFace; |
| 93 | std::map<Name, EventId> watchEvents; |
| 94 | }; |
| 95 | |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 96 | template<class T> void |
| 97 | Fixture<T>::onWatchInterest(const Interest& interest) |
| 98 | { |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 99 | auto data = make_shared<Data>(Name(interest.getName()) |
| 100 | .appendNumber(ndn::random::generateWord64() + 100)); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 101 | data->setContent(content, sizeof(content)); |
| 102 | data->setFreshnessPeriod(milliseconds(0)); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 103 | keyChain.sign(*data); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 104 | watchFace.put(*data); |
| 105 | |
| 106 | // schedule an event 50ms later to check whether watch is Ok |
| 107 | scheduler.scheduleEvent(milliseconds(10000), |
| 108 | bind(&Fixture<T>::checkWatchOk, this, |
| 109 | Interest(data->getName()))); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | template<class T> void |
| 114 | Fixture<T>::onRegisterFailed(const std::string& reason) |
| 115 | { |
| 116 | BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason); |
| 117 | } |
| 118 | |
| 119 | template<class T> void |
| 120 | Fixture<T>::delayedInterest() |
| 121 | { |
| 122 | BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong"); |
| 123 | } |
| 124 | |
| 125 | template<class T> void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 126 | Fixture<T>::onWatchData(const Interest& interest, const Data& data) |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 127 | { |
| 128 | RepoCommandResponse response; |
| 129 | response.wireDecode(data.getContent().blockFromValue()); |
| 130 | |
| 131 | int statusCode = response.getStatusCode(); |
| 132 | BOOST_CHECK_EQUAL(statusCode, 100); |
| 133 | } |
| 134 | |
| 135 | template<class T> void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 136 | Fixture<T>::onWatchStopData(const Interest& interest, const Data& data) |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 137 | { |
| 138 | RepoCommandResponse response; |
| 139 | response.wireDecode(data.getContent().blockFromValue()); |
| 140 | |
| 141 | int statusCode = response.getStatusCode(); |
| 142 | BOOST_CHECK_EQUAL(statusCode, 101); |
| 143 | } |
| 144 | |
| 145 | template<class T> void |
| 146 | Fixture<T>::onWatchTimeout(const Interest& interest) |
| 147 | { |
| 148 | BOOST_ERROR("Watch command timeout"); |
| 149 | } |
| 150 | |
| 151 | template<class T> void |
| 152 | Fixture<T>::sendWatchStartInterest(const Interest& watchInterest) |
| 153 | { |
| 154 | watchFace.expressInterest(watchInterest, |
| 155 | bind(&Fixture<T>::onWatchData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 156 | bind(&Fixture<T>::onWatchTimeout, this, _1), // Nack |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 157 | bind(&Fixture<T>::onWatchTimeout, this, _1)); |
| 158 | } |
| 159 | |
| 160 | template<class T> void |
| 161 | Fixture<T>::sendWatchStopInterest(const Interest& watchInterest) |
| 162 | { |
| 163 | watchFace.expressInterest(watchInterest, |
| 164 | bind(&Fixture<T>::onWatchStopData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 165 | bind(&Fixture<T>::onWatchTimeout, this, _1), // Nack |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 166 | bind(&Fixture<T>::onWatchTimeout, this, _1)); |
| 167 | } |
| 168 | |
| 169 | template<class T> void |
| 170 | Fixture<T>::checkWatchOk(const Interest& interest) |
| 171 | { |
| 172 | BOOST_TEST_MESSAGE(interest); |
| 173 | shared_ptr<Data> data = handle->readData(interest); |
| 174 | if (data) { |
| 175 | int rc = memcmp(data->getContent().value(), content, sizeof(content)); |
| 176 | BOOST_CHECK_EQUAL(rc, 0); |
| 177 | } |
| 178 | else { |
| 179 | std::cerr<<"Check Watch Failed"<<std::endl; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | template<class T> void |
| 184 | Fixture<T>::scheduleWatchEvent() |
| 185 | { |
| 186 | Name watchCommandName("/repo/command/watch/start"); |
| 187 | RepoCommandParameter watchParameter; |
| 188 | watchParameter.setName(Name("/a/b")); |
| 189 | watchParameter.setMaxInterestNum(10); |
| 190 | watchParameter.setInterestLifetime(milliseconds(50000)); |
| 191 | watchParameter.setWatchTimeout(milliseconds(1000000000)); |
| 192 | watchCommandName.append(watchParameter.wireEncode()); |
| 193 | Interest watchInterest(watchCommandName); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 194 | keyChain.sign(watchInterest); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 195 | //schedule a job to express watchInterest |
| 196 | scheduler.scheduleEvent(milliseconds(1000), |
| 197 | bind(&Fixture<T>::sendWatchStartInterest, this, watchInterest)); |
| 198 | |
| 199 | Name watchStopName("/repo/command/watch/stop"); |
| 200 | RepoCommandParameter watchStopParameter; |
| 201 | watchStopName.append(watchStopParameter.wireEncode()); |
| 202 | Interest watchStopInterest(watchStopName); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 203 | keyChain.sign(watchStopInterest); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 204 | |
| 205 | // scheduler.scheduleEvent(milliseconds(10000), |
| 206 | // bind(&Fixture<T>::sendWatchStopInterest, this, watchStopInterest)); |
| 207 | //The delayEvent will be canceled in onWatchInterest |
| 208 | watchFace.setInterestFilter(watchParameter.getName(), |
| 209 | bind(&Fixture<T>::onWatchInterest, this, _2), |
| 210 | ndn::RegisterPrefixSuccessCallback(), |
| 211 | bind(&Fixture<T>::onRegisterFailed, this, _2)); |
| 212 | } |
| 213 | |
| 214 | typedef boost::mpl::vector< BasicDataset > Dataset; |
| 215 | |
| 216 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(WatchDelete, T, Dataset, Fixture<T>) |
| 217 | { |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 218 | // schedule events |
| 219 | this->scheduler.scheduleEvent(seconds(0), |
| 220 | bind(&Fixture<T>::scheduleWatchEvent, this)); |
| 221 | |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 222 | this->repoFace.processEvents(seconds(500)); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | BOOST_AUTO_TEST_SUITE_END() |
| 226 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 227 | } // namespace tests |
| 228 | } // namespace repo |