blob: 4fa222be721f92c120e7c32ce17fd46fd29565c6 [file] [log] [blame]
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 Regents of the University of California,
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 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 Afanasyevd3a55b22014-11-18 19:23:28 -080034#include "../unit-test-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 Afanasyevd3a55b22014-11-18 19:23:28 -080042class EndToEndFixture : public ndn::tests::UnitTestTimeFixture
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070043{
44public:
45 EndToEndFixture()
46 : streamPrefix("ndn:/NotificationSubscriberTest")
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080047 , publisherFace(makeDummyClientFace(io))
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070048 , notificationStream(*publisherFace, streamPrefix, publisherKeyChain)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080049 , subscriberFace(makeDummyClientFace(io))
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070050 , subscriber(*subscriberFace, streamPrefix, time::seconds(1))
51 {
52 }
53
54 /** \brief post one notification and deliver to subscriber
55 */
56 void
57 deliverNotification(const std::string& msg)
58 {
Junxiao Shia60d9362014-11-12 09:38:21 -070059 publisherFace->sentDatas.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
Junxiao Shia60d9362014-11-12 09:38:21 -070065 BOOST_REQUIRE_EQUAL(publisherFace->sentDatas.size(), 1);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070066
Junxiao Shia60d9362014-11-12 09:38:21 -070067 lastDeliveredSeqNo = publisherFace->sentDatas[0].getName().at(-1).toSequenceNumber();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070068
69 lastNotification.setMessage("");
Junxiao Shia60d9362014-11-12 09:38:21 -070070 subscriberFace->receive(publisherFace->sentDatas[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 {
Junxiao Shia60d9362014-11-12 09:38:21 -070096 if (subscriberFace->sentInterests.empty())
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070097 return 0;
98
Junxiao Shia60d9362014-11-12 09:38:21 -070099 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 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700112 if (subscriberFace->sentInterests.size() != 1)
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700113 return 0;
114
Junxiao Shia60d9362014-11-12 09:38:21 -0700115 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;
127 shared_ptr<DummyClientFace> publisherFace;
128 ndn::KeyChain publisherKeyChain;
129 util::NotificationStream<SimpleNotification> notificationStream;
130 shared_ptr<DummyClientFace> subscriberFace;
131 util::NotificationSubscriber<SimpleNotification> subscriber;
Junxiao Shi728873f2015-01-20 16:01:26 -0700132 util::signal::Connection notificationConn;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700133
134 uint64_t lastDeliveredSeqNo;
135
136 SimpleNotification lastNotification;
137 bool hasTimeout;
138 Data lastDecodeErrorData;
139};
140
141BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
142{
143 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
144
145 // has no effect because onNotification has no handler
146 subscriber.start();
147 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
148
Junxiao Shi728873f2015-01-20 16:01:26 -0700149 notificationConn = subscriber.onNotification.connect(
150 bind(&EndToEndFixture::afterNotification, this, _1));
151 subscriber.onTimeout.connect(bind(&EndToEndFixture::afterTimeout, this));
152 subscriber.onDecodeError.connect(bind(&EndToEndFixture::afterDecodeError, this, _1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700153
154 // not received when subscriber is not running
155 this->deliverNotification("n1");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800156 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700157 BOOST_CHECK(lastNotification.getMessage().empty());
Junxiao Shia60d9362014-11-12 09:38:21 -0700158 BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700159
Junxiao Shia60d9362014-11-12 09:38:21 -0700160 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700161 subscriber.start();
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800162 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700163 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true);
164 BOOST_CHECK(this->hasInitialRequest());
165
166 // respond to initial request
Junxiao Shia60d9362014-11-12 09:38:21 -0700167 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700168 this->deliverNotification("n2");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800169 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700170 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2");
171 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
172
173 // respond to continuation request
Junxiao Shia60d9362014-11-12 09:38:21 -0700174 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700175 this->deliverNotification("n3");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800176 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700177 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3");
178 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
179
180 // timeout
Junxiao Shia60d9362014-11-12 09:38:21 -0700181 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700182 lastNotification.setMessage("");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800183 advanceClocks(subscriber.getInterestLifetime(), 2);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700184 BOOST_CHECK(lastNotification.getMessage().empty());
185 BOOST_CHECK_EQUAL(hasTimeout, true);
186 BOOST_CHECK(this->hasInitialRequest());
187
188 // decode error on sequence number
189 Name wrongName = streamPrefix;
190 wrongName.append("%07%07");
191 Data wrongData(wrongName);
192 publisherKeyChain.sign(wrongData);
193 subscriberFace->receive(wrongData);
Junxiao Shia60d9362014-11-12 09:38:21 -0700194 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700195 lastNotification.setMessage("");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800196 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700197 BOOST_CHECK(lastNotification.getMessage().empty());
198 BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName);
199 BOOST_CHECK(this->hasInitialRequest());
200
201 // decode error in payload
Junxiao Shia60d9362014-11-12 09:38:21 -0700202 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700203 lastNotification.setMessage("");
204 this->deliverNotification("\x07n4");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800205 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700206 BOOST_CHECK(lastNotification.getMessage().empty());
207 BOOST_CHECK(this->hasInitialRequest());
208
209 // stop if handlers are cleared
Junxiao Shi728873f2015-01-20 16:01:26 -0700210 notificationConn.disconnect();
Junxiao Shia60d9362014-11-12 09:38:21 -0700211 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700212 this->deliverNotification("n5");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800213 advanceClocks(time::milliseconds(1));
Junxiao Shia60d9362014-11-12 09:38:21 -0700214 BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700215}
216
217BOOST_AUTO_TEST_SUITE_END()
218
219} // namespace tests
Junxiao Shia60d9362014-11-12 09:38:21 -0700220} // namespace util
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700221} // namespace ndn