Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 728873f | 2015-01-20 16:01:26 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 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" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 51 | #include "util/dummy-client-face.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 52 | |
| 53 | #include "boost-test.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 54 | #include "../unit-test-time-fixture.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 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 62 | class EndToEndFixture : public ndn::tests::UnitTestTimeFixture |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 63 | { |
| 64 | public: |
| 65 | EndToEndFixture() |
| 66 | : streamPrefix("ndn:/NotificationSubscriberTest") |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 67 | , publisherFace(makeDummyClientFace(io)) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 68 | , notificationStream(*publisherFace, streamPrefix, publisherKeyChain) |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 69 | , subscriberFace(makeDummyClientFace(io)) |
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 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 83 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 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 |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 100 | afterTimeout() |
| 101 | { |
| 102 | hasTimeout = true; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | afterDecodeError(const Data& data) |
| 107 | { |
| 108 | lastDecodeErrorData = data; |
| 109 | } |
| 110 | |
| 111 | /** \return true if subscriberFace has an initial request (first sent Interest) |
| 112 | */ |
| 113 | bool |
| 114 | hasInitialRequest() const |
| 115 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 116 | if (subscriberFace->sentInterests.empty()) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 117 | return 0; |
| 118 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 119 | const Interest& interest = subscriberFace->sentInterests[0]; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 120 | return interest.getName() == streamPrefix && |
| 121 | interest.getChildSelector() == 1 && |
| 122 | interest.getMustBeFresh() && |
| 123 | interest.getInterestLifetime() == subscriber.getInterestLifetime(); |
| 124 | } |
| 125 | |
| 126 | /** \return sequence number of the continuation request sent from subscriberFace |
| 127 | * or 0 if there's no such request as sole sent Interest |
| 128 | */ |
| 129 | uint64_t |
| 130 | getRequestSeqNo() const |
| 131 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 132 | if (subscriberFace->sentInterests.size() != 1) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 133 | return 0; |
| 134 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 135 | const Interest& interest = subscriberFace->sentInterests[0]; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 136 | const Name& name = interest.getName(); |
| 137 | if (streamPrefix.isPrefixOf(name) && |
| 138 | name.size() == streamPrefix.size() + 1 && |
| 139 | interest.getInterestLifetime() == subscriber.getInterestLifetime()) |
| 140 | return name[-1].toSequenceNumber(); |
| 141 | else |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | protected: |
| 146 | Name streamPrefix; |
| 147 | shared_ptr<DummyClientFace> publisherFace; |
| 148 | ndn::KeyChain publisherKeyChain; |
| 149 | util::NotificationStream<SimpleNotification> notificationStream; |
| 150 | shared_ptr<DummyClientFace> subscriberFace; |
| 151 | util::NotificationSubscriber<SimpleNotification> subscriber; |
Junxiao Shi | 728873f | 2015-01-20 16:01:26 -0700 | [diff] [blame^] | 152 | util::signal::Connection notificationConn; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 153 | |
| 154 | uint64_t lastDeliveredSeqNo; |
| 155 | |
| 156 | SimpleNotification lastNotification; |
| 157 | bool hasTimeout; |
| 158 | Data lastDecodeErrorData; |
| 159 | }; |
| 160 | |
| 161 | BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) |
| 162 | { |
| 163 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false); |
| 164 | |
| 165 | // has no effect because onNotification has no handler |
| 166 | subscriber.start(); |
| 167 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false); |
| 168 | |
Junxiao Shi | 728873f | 2015-01-20 16:01:26 -0700 | [diff] [blame^] | 169 | notificationConn = subscriber.onNotification.connect( |
| 170 | bind(&EndToEndFixture::afterNotification, this, _1)); |
| 171 | subscriber.onTimeout.connect(bind(&EndToEndFixture::afterTimeout, this)); |
| 172 | subscriber.onDecodeError.connect(bind(&EndToEndFixture::afterDecodeError, this, _1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 173 | |
| 174 | // not received when subscriber is not running |
| 175 | this->deliverNotification("n1"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 176 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 177 | BOOST_CHECK(lastNotification.getMessage().empty()); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 178 | BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 179 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 180 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 181 | subscriber.start(); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 182 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 183 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true); |
| 184 | BOOST_CHECK(this->hasInitialRequest()); |
| 185 | |
| 186 | // respond to initial request |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 187 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 188 | this->deliverNotification("n2"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 189 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2"); |
| 191 | BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1); |
| 192 | |
| 193 | // respond to continuation request |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 194 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 195 | this->deliverNotification("n3"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 196 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 197 | BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3"); |
| 198 | BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1); |
| 199 | |
| 200 | // timeout |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 201 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 202 | lastNotification.setMessage(""); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 203 | advanceClocks(subscriber.getInterestLifetime(), 2); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 204 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 205 | BOOST_CHECK_EQUAL(hasTimeout, true); |
| 206 | BOOST_CHECK(this->hasInitialRequest()); |
| 207 | |
| 208 | // decode error on sequence number |
| 209 | Name wrongName = streamPrefix; |
| 210 | wrongName.append("%07%07"); |
| 211 | Data wrongData(wrongName); |
| 212 | publisherKeyChain.sign(wrongData); |
| 213 | subscriberFace->receive(wrongData); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 214 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 215 | lastNotification.setMessage(""); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 216 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 217 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 218 | BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName); |
| 219 | BOOST_CHECK(this->hasInitialRequest()); |
| 220 | |
| 221 | // decode error in payload |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 222 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 223 | lastNotification.setMessage(""); |
| 224 | this->deliverNotification("\x07n4"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 225 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 226 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 227 | BOOST_CHECK(this->hasInitialRequest()); |
| 228 | |
| 229 | // stop if handlers are cleared |
Junxiao Shi | 728873f | 2015-01-20 16:01:26 -0700 | [diff] [blame^] | 230 | notificationConn.disconnect(); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 231 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 232 | this->deliverNotification("n5"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 233 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 234 | BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | BOOST_AUTO_TEST_SUITE_END() |
| 238 | |
| 239 | } // namespace tests |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 240 | } // namespace util |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 241 | } // namespace ndn |