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