Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Original copyright notice from NFD: |
| 24 | * |
| 25 | * Copyright (c) 2014, Regents of the University of California, |
| 26 | * Arizona Board of Regents, |
| 27 | * Colorado State University, |
| 28 | * University Pierre & Marie Curie, Sorbonne University, |
| 29 | * Washington University in St. Louis, |
| 30 | * Beijing Institute of Technology, |
| 31 | * The University of Memphis |
| 32 | * |
| 33 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 34 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 35 | * |
| 36 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 37 | * of the GNU General Public License as published by the Free Software Foundation, |
| 38 | * either version 3 of the License, or (at your option) any later version. |
| 39 | * |
| 40 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 41 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE. See the GNU General Public License for more details. |
| 43 | * |
| 44 | * You should have received a copy of the GNU General Public License along with |
| 45 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 46 | */ |
| 47 | |
| 48 | #include "util/notification-subscriber.hpp" |
| 49 | #include "util/notification-stream.hpp" |
| 50 | #include "simple-notification.hpp" |
| 51 | |
| 52 | #include "boost-test.hpp" |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 53 | #include <boost/asio.hpp> |
| 54 | #include "util/dummy-client-face.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 55 | |
| 56 | namespace ndn { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 57 | namespace util { |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 58 | namespace tests { |
| 59 | |
| 60 | BOOST_AUTO_TEST_SUITE(UtilNotificationSubscriber) |
| 61 | |
| 62 | class EndToEndFixture |
| 63 | { |
| 64 | public: |
| 65 | EndToEndFixture() |
| 66 | : streamPrefix("ndn:/NotificationSubscriberTest") |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 67 | , publisherFace(makeDummyClientFace(ioService)) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 68 | , notificationStream(*publisherFace, streamPrefix, publisherKeyChain) |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 69 | , subscriberFace(makeDummyClientFace(ioService)) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 70 | , subscriber(*subscriberFace, streamPrefix, time::seconds(1)) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | /** \brief post one notification and deliver to subscriber |
| 75 | */ |
| 76 | void |
| 77 | deliverNotification(const std::string& msg) |
| 78 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 79 | publisherFace->sentDatas.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 80 | SimpleNotification notification(msg); |
| 81 | notificationStream.postNotification(notification); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 82 | |
| 83 | publisherFace->processEvents(time::milliseconds(100)); |
| 84 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 85 | BOOST_REQUIRE_EQUAL(publisherFace->sentDatas.size(), 1); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 86 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 87 | lastDeliveredSeqNo = publisherFace->sentDatas[0].getName().at(-1).toSequenceNumber(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 88 | |
| 89 | lastNotification.setMessage(""); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 90 | subscriberFace->receive(publisherFace->sentDatas[0]); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
| 94 | afterNotification(const SimpleNotification& notification) |
| 95 | { |
| 96 | lastNotification = notification; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | clearNotificationHandlers() |
| 101 | { |
| 102 | subscriber.onNotification.clear(); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | afterTimeout() |
| 107 | { |
| 108 | hasTimeout = true; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | afterDecodeError(const Data& data) |
| 113 | { |
| 114 | lastDecodeErrorData = data; |
| 115 | } |
| 116 | |
| 117 | /** \return true if subscriberFace has an initial request (first sent Interest) |
| 118 | */ |
| 119 | bool |
| 120 | hasInitialRequest() const |
| 121 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 122 | if (subscriberFace->sentInterests.empty()) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 123 | return 0; |
| 124 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 125 | const Interest& interest = subscriberFace->sentInterests[0]; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 126 | return interest.getName() == streamPrefix && |
| 127 | interest.getChildSelector() == 1 && |
| 128 | interest.getMustBeFresh() && |
| 129 | interest.getInterestLifetime() == subscriber.getInterestLifetime(); |
| 130 | } |
| 131 | |
| 132 | /** \return sequence number of the continuation request sent from subscriberFace |
| 133 | * or 0 if there's no such request as sole sent Interest |
| 134 | */ |
| 135 | uint64_t |
| 136 | getRequestSeqNo() const |
| 137 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 138 | if (subscriberFace->sentInterests.size() != 1) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 139 | return 0; |
| 140 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 141 | const Interest& interest = subscriberFace->sentInterests[0]; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 142 | const Name& name = interest.getName(); |
| 143 | if (streamPrefix.isPrefixOf(name) && |
| 144 | name.size() == streamPrefix.size() + 1 && |
| 145 | interest.getInterestLifetime() == subscriber.getInterestLifetime()) |
| 146 | return name[-1].toSequenceNumber(); |
| 147 | else |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | protected: |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 152 | boost::asio::io_service ioService; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 153 | Name streamPrefix; |
| 154 | shared_ptr<DummyClientFace> publisherFace; |
| 155 | ndn::KeyChain publisherKeyChain; |
| 156 | util::NotificationStream<SimpleNotification> notificationStream; |
| 157 | shared_ptr<DummyClientFace> subscriberFace; |
| 158 | util::NotificationSubscriber<SimpleNotification> subscriber; |
| 159 | |
| 160 | uint64_t lastDeliveredSeqNo; |
| 161 | |
| 162 | SimpleNotification lastNotification; |
| 163 | bool hasTimeout; |
| 164 | Data lastDecodeErrorData; |
| 165 | }; |
| 166 | |
| 167 | BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) |
| 168 | { |
| 169 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false); |
| 170 | |
| 171 | // has no effect because onNotification has no handler |
| 172 | subscriber.start(); |
| 173 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false); |
| 174 | |
| 175 | subscriber.onNotification += bind(&EndToEndFixture::afterNotification, this, _1); |
| 176 | subscriber.onTimeout += bind(&EndToEndFixture::afterTimeout, this); |
| 177 | subscriber.onDecodeError += bind(&EndToEndFixture::afterDecodeError, this, _1); |
| 178 | |
| 179 | // not received when subscriber is not running |
| 180 | this->deliverNotification("n1"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 181 | subscriberFace->processEvents(time::milliseconds(100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 182 | BOOST_CHECK(lastNotification.getMessage().empty()); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 183 | BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 184 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 185 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 186 | subscriber.start(); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 187 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 188 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true); |
| 189 | BOOST_CHECK(this->hasInitialRequest()); |
| 190 | |
| 191 | // respond to initial request |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 192 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 193 | this->deliverNotification("n2"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 194 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2"); |
| 196 | BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1); |
| 197 | |
| 198 | // respond to continuation request |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 199 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 200 | this->deliverNotification("n3"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 201 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 202 | BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3"); |
| 203 | BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1); |
| 204 | |
| 205 | // timeout |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 206 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 207 | lastNotification.setMessage(""); |
| 208 | subscriberFace->processEvents(2 * subscriber.getInterestLifetime()); |
| 209 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 210 | BOOST_CHECK_EQUAL(hasTimeout, true); |
| 211 | BOOST_CHECK(this->hasInitialRequest()); |
| 212 | |
| 213 | // decode error on sequence number |
| 214 | Name wrongName = streamPrefix; |
| 215 | wrongName.append("%07%07"); |
| 216 | Data wrongData(wrongName); |
| 217 | publisherKeyChain.sign(wrongData); |
| 218 | subscriberFace->receive(wrongData); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 219 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 220 | lastNotification.setMessage(""); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 221 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 222 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 223 | BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName); |
| 224 | BOOST_CHECK(this->hasInitialRequest()); |
| 225 | |
| 226 | // decode error in payload |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 227 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 228 | lastNotification.setMessage(""); |
| 229 | this->deliverNotification("\x07n4"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 230 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 231 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 232 | BOOST_CHECK(this->hasInitialRequest()); |
| 233 | |
| 234 | // stop if handlers are cleared |
| 235 | subscriber.onNotification += bind(&EndToEndFixture::clearNotificationHandlers, this); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 236 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 237 | this->deliverNotification("n5"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 238 | subscriberFace->processEvents(time::milliseconds(-100)); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 239 | BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | BOOST_AUTO_TEST_SUITE_END() |
| 243 | |
| 244 | } // namespace tests |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame^] | 245 | } // namespace util |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 246 | } // namespace ndn |