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