blob: 0279569fadb9d21b9a11986bcb1efc74ab8c2e64 [file] [log] [blame]
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07003 * Copyright (c) 2013-2016 Regents of the University of California,
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08004 * 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 Afanasyev4abdbf12014-08-11 12:48:54 -070010 *
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 Afanasyev4abdbf12014-08-11 12:48:54 -070028#include "util/notification-subscriber.hpp"
29#include "util/notification-stream.hpp"
30#include "simple-notification.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080031#include "util/dummy-client-face.hpp"
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070032
33#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070034#include "../identity-management-time-fixture.hpp"
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070035
36namespace ndn {
Junxiao Shia60d9362014-11-12 09:38:21 -070037namespace util {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070038namespace tests {
39
40BOOST_AUTO_TEST_SUITE(UtilNotificationSubscriber)
41
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070042class EndToEndFixture : public ndn::tests::IdentityManagementTimeFixture
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070043{
44public:
45 EndToEndFixture()
46 : streamPrefix("ndn:/NotificationSubscriberTest")
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070047 , publisherFace(io, m_keyChain)
48 , notificationStream(publisherFace, streamPrefix, m_keyChain)
49 , subscriberFace(io, m_keyChain)
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080050 , subscriber(subscriberFace, streamPrefix, time::seconds(1))
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070051 {
52 }
53
54 /** \brief post one notification and deliver to subscriber
55 */
56 void
57 deliverNotification(const std::string& msg)
58 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080059 publisherFace.sentData.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070060 SimpleNotification notification(msg);
61 notificationStream.postNotification(notification);
Alexander Afanasyev370d2602014-08-20 10:21:34 -050062
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080063 advanceClocks(time::milliseconds(1));
Alexander Afanasyev370d2602014-08-20 10:21:34 -050064
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080065 BOOST_REQUIRE_EQUAL(publisherFace.sentData.size(), 1);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070066
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080067 lastDeliveredSeqNo = publisherFace.sentData[0].getName().at(-1).toSequenceNumber();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070068
69 lastNotification.setMessage("");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080070 subscriberFace.receive(publisherFace.sentData[0]);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070071 }
72
73 void
74 afterNotification(const SimpleNotification& notification)
75 {
76 lastNotification = notification;
77 }
78
79 void
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070080 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 Afanasyev9bdbb832015-12-30 12:54:22 -080096 if (subscriberFace.sentInterests.empty())
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070097 return 0;
98
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080099 const Interest& interest = subscriberFace.sentInterests[0];
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700100 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800112 if (subscriberFace.sentInterests.size() != 1)
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700113 return 0;
114
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800115 const Interest& interest = subscriberFace.sentInterests[0];
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700116 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
125protected:
126 Name streamPrefix;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800127 DummyClientFace publisherFace;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700128 util::NotificationStream<SimpleNotification> notificationStream;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800129 DummyClientFace subscriberFace;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700130 util::NotificationSubscriber<SimpleNotification> subscriber;
Junxiao Shi728873f2015-01-20 16:01:26 -0700131 util::signal::Connection notificationConn;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700132
133 uint64_t lastDeliveredSeqNo;
134
135 SimpleNotification lastNotification;
136 bool hasTimeout;
137 Data lastDecodeErrorData;
138};
139
140BOOST_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 Shi728873f2015-01-20 16:01:26 -0700148 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 Afanasyev4abdbf12014-08-11 12:48:54 -0700152
153 // not received when subscriber is not running
154 this->deliverNotification("n1");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800155 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700156 BOOST_CHECK(lastNotification.getMessage().empty());
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800157 BOOST_CHECK_EQUAL(subscriberFace.sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700158
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800159 subscriberFace.sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700160 subscriber.start();
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800161 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700162 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true);
163 BOOST_CHECK(this->hasInitialRequest());
164
165 // respond to initial request
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800166 subscriberFace.sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700167 this->deliverNotification("n2");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800168 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700169 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2");
170 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
171
172 // respond to continuation request
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800173 subscriberFace.sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700174 this->deliverNotification("n3");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800175 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700176 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3");
177 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
178
179 // timeout
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800180 subscriberFace.sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700181 lastNotification.setMessage("");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800182 advanceClocks(subscriber.getInterestLifetime(), 2);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700183 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 Afanasyeve4f8c3b2016-06-23 16:03:48 -0700191 m_keyChain.sign(wrongData);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800192 subscriberFace.receive(wrongData);
193 subscriberFace.sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700194 lastNotification.setMessage("");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800195 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700196 BOOST_CHECK(lastNotification.getMessage().empty());
197 BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName);
198 BOOST_CHECK(this->hasInitialRequest());
199
200 // decode error in payload
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800201 subscriberFace.sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700202 lastNotification.setMessage("");
203 this->deliverNotification("\x07n4");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800204 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700205 BOOST_CHECK(lastNotification.getMessage().empty());
206 BOOST_CHECK(this->hasInitialRequest());
207
208 // stop if handlers are cleared
Junxiao Shi728873f2015-01-20 16:01:26 -0700209 notificationConn.disconnect();
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800210 subscriberFace.sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700211 this->deliverNotification("n5");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800212 advanceClocks(time::milliseconds(1));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800213 BOOST_CHECK_EQUAL(subscriberFace.sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700214}
215
216BOOST_AUTO_TEST_SUITE_END()
217
218} // namespace tests
Junxiao Shia60d9362014-11-12 09:38:21 -0700219} // namespace util
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700220} // namespace ndn