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" |
| 53 | #include "../dummy-client-face.hpp" |
| 54 | |
| 55 | namespace ndn { |
| 56 | namespace tests { |
| 57 | |
| 58 | BOOST_AUTO_TEST_SUITE(UtilNotificationSubscriber) |
| 59 | |
| 60 | class EndToEndFixture |
| 61 | { |
| 62 | public: |
| 63 | EndToEndFixture() |
| 64 | : streamPrefix("ndn:/NotificationSubscriberTest") |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 65 | , publisherFace(makeDummyClientFace(ioService)) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 66 | , notificationStream(*publisherFace, streamPrefix, publisherKeyChain) |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 67 | , subscriberFace(makeDummyClientFace(ioService)) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 68 | , subscriber(*subscriberFace, streamPrefix, time::seconds(1)) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | /** \brief post one notification and deliver to subscriber |
| 73 | */ |
| 74 | void |
| 75 | deliverNotification(const std::string& msg) |
| 76 | { |
| 77 | publisherFace->m_sentDatas.clear(); |
| 78 | SimpleNotification notification(msg); |
| 79 | notificationStream.postNotification(notification); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 80 | |
| 81 | publisherFace->processEvents(time::milliseconds(100)); |
| 82 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 83 | BOOST_REQUIRE_EQUAL(publisherFace->m_sentDatas.size(), 1); |
| 84 | |
| 85 | lastDeliveredSeqNo = publisherFace->m_sentDatas[0].getName().at(-1).toSequenceNumber(); |
| 86 | |
| 87 | lastNotification.setMessage(""); |
| 88 | subscriberFace->receive(publisherFace->m_sentDatas[0]); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | afterNotification(const SimpleNotification& notification) |
| 93 | { |
| 94 | lastNotification = notification; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | clearNotificationHandlers() |
| 99 | { |
| 100 | subscriber.onNotification.clear(); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | afterTimeout() |
| 105 | { |
| 106 | hasTimeout = true; |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | afterDecodeError(const Data& data) |
| 111 | { |
| 112 | lastDecodeErrorData = data; |
| 113 | } |
| 114 | |
| 115 | /** \return true if subscriberFace has an initial request (first sent Interest) |
| 116 | */ |
| 117 | bool |
| 118 | hasInitialRequest() const |
| 119 | { |
| 120 | if (subscriberFace->m_sentInterests.empty()) |
| 121 | return 0; |
| 122 | |
| 123 | const Interest& interest = subscriberFace->m_sentInterests[0]; |
| 124 | return interest.getName() == streamPrefix && |
| 125 | interest.getChildSelector() == 1 && |
| 126 | interest.getMustBeFresh() && |
| 127 | interest.getInterestLifetime() == subscriber.getInterestLifetime(); |
| 128 | } |
| 129 | |
| 130 | /** \return sequence number of the continuation request sent from subscriberFace |
| 131 | * or 0 if there's no such request as sole sent Interest |
| 132 | */ |
| 133 | uint64_t |
| 134 | getRequestSeqNo() const |
| 135 | { |
| 136 | if (subscriberFace->m_sentInterests.size() != 1) |
| 137 | return 0; |
| 138 | |
| 139 | const Interest& interest = subscriberFace->m_sentInterests[0]; |
| 140 | const Name& name = interest.getName(); |
| 141 | if (streamPrefix.isPrefixOf(name) && |
| 142 | name.size() == streamPrefix.size() + 1 && |
| 143 | interest.getInterestLifetime() == subscriber.getInterestLifetime()) |
| 144 | return name[-1].toSequenceNumber(); |
| 145 | else |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | protected: |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 150 | boost::asio::io_service ioService; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 151 | Name streamPrefix; |
| 152 | shared_ptr<DummyClientFace> publisherFace; |
| 153 | ndn::KeyChain publisherKeyChain; |
| 154 | util::NotificationStream<SimpleNotification> notificationStream; |
| 155 | shared_ptr<DummyClientFace> subscriberFace; |
| 156 | util::NotificationSubscriber<SimpleNotification> subscriber; |
| 157 | |
| 158 | uint64_t lastDeliveredSeqNo; |
| 159 | |
| 160 | SimpleNotification lastNotification; |
| 161 | bool hasTimeout; |
| 162 | Data lastDecodeErrorData; |
| 163 | }; |
| 164 | |
| 165 | BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) |
| 166 | { |
| 167 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false); |
| 168 | |
| 169 | // has no effect because onNotification has no handler |
| 170 | subscriber.start(); |
| 171 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false); |
| 172 | |
| 173 | subscriber.onNotification += bind(&EndToEndFixture::afterNotification, this, _1); |
| 174 | subscriber.onTimeout += bind(&EndToEndFixture::afterTimeout, this); |
| 175 | subscriber.onDecodeError += bind(&EndToEndFixture::afterDecodeError, this, _1); |
| 176 | |
| 177 | // not received when subscriber is not running |
| 178 | this->deliverNotification("n1"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 179 | subscriberFace->processEvents(time::milliseconds(100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 180 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 181 | BOOST_CHECK_EQUAL(subscriberFace->m_sentInterests.size(), 0); |
| 182 | |
| 183 | subscriberFace->m_sentInterests.clear(); |
| 184 | subscriber.start(); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 185 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 186 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true); |
| 187 | BOOST_CHECK(this->hasInitialRequest()); |
| 188 | |
| 189 | // respond to initial request |
| 190 | subscriberFace->m_sentInterests.clear(); |
| 191 | this->deliverNotification("n2"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 192 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 193 | BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2"); |
| 194 | BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1); |
| 195 | |
| 196 | // respond to continuation request |
| 197 | subscriberFace->m_sentInterests.clear(); |
| 198 | this->deliverNotification("n3"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 199 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3"); |
| 201 | BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1); |
| 202 | |
| 203 | // timeout |
| 204 | subscriberFace->m_sentInterests.clear(); |
| 205 | lastNotification.setMessage(""); |
| 206 | subscriberFace->processEvents(2 * subscriber.getInterestLifetime()); |
| 207 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 208 | BOOST_CHECK_EQUAL(hasTimeout, true); |
| 209 | BOOST_CHECK(this->hasInitialRequest()); |
| 210 | |
| 211 | // decode error on sequence number |
| 212 | Name wrongName = streamPrefix; |
| 213 | wrongName.append("%07%07"); |
| 214 | Data wrongData(wrongName); |
| 215 | publisherKeyChain.sign(wrongData); |
| 216 | subscriberFace->receive(wrongData); |
| 217 | subscriberFace->m_sentInterests.clear(); |
| 218 | lastNotification.setMessage(""); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 219 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 220 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 221 | BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName); |
| 222 | BOOST_CHECK(this->hasInitialRequest()); |
| 223 | |
| 224 | // decode error in payload |
| 225 | subscriberFace->m_sentInterests.clear(); |
| 226 | lastNotification.setMessage(""); |
| 227 | this->deliverNotification("\x07n4"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 228 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 229 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 230 | BOOST_CHECK(this->hasInitialRequest()); |
| 231 | |
| 232 | // stop if handlers are cleared |
| 233 | subscriber.onNotification += bind(&EndToEndFixture::clearNotificationHandlers, this); |
| 234 | subscriberFace->m_sentInterests.clear(); |
| 235 | this->deliverNotification("n5"); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 236 | subscriberFace->processEvents(time::milliseconds(-100)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 237 | BOOST_CHECK_EQUAL(subscriberFace->m_sentInterests.size(), 0); |
| 238 | } |
| 239 | |
| 240 | BOOST_AUTO_TEST_SUITE_END() |
| 241 | |
| 242 | } // namespace tests |
| 243 | } // namespace ndn |