blob: 643daf0ba9be755a2be8bfa436bda8f4aafac51a [file] [log] [blame]
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi728873f2015-01-20 16:01:26 -07003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -07004 *
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 Afanasyevd3a55b22014-11-18 19:23:28 -080051#include "util/dummy-client-face.hpp"
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070052
53#include "boost-test.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080054#include "../unit-test-time-fixture.hpp"
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070055
56namespace ndn {
Junxiao Shia60d9362014-11-12 09:38:21 -070057namespace util {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070058namespace tests {
59
60BOOST_AUTO_TEST_SUITE(UtilNotificationSubscriber)
61
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080062class EndToEndFixture : public ndn::tests::UnitTestTimeFixture
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070063{
64public:
65 EndToEndFixture()
66 : streamPrefix("ndn:/NotificationSubscriberTest")
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080067 , publisherFace(makeDummyClientFace(io))
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070068 , notificationStream(*publisherFace, streamPrefix, publisherKeyChain)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080069 , subscriberFace(makeDummyClientFace(io))
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070070 , 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 Shia60d9362014-11-12 09:38:21 -070079 publisherFace->sentDatas.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070080 SimpleNotification notification(msg);
81 notificationStream.postNotification(notification);
Alexander Afanasyev370d2602014-08-20 10:21:34 -050082
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080083 advanceClocks(time::milliseconds(1));
Alexander Afanasyev370d2602014-08-20 10:21:34 -050084
Junxiao Shia60d9362014-11-12 09:38:21 -070085 BOOST_REQUIRE_EQUAL(publisherFace->sentDatas.size(), 1);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070086
Junxiao Shia60d9362014-11-12 09:38:21 -070087 lastDeliveredSeqNo = publisherFace->sentDatas[0].getName().at(-1).toSequenceNumber();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070088
89 lastNotification.setMessage("");
Junxiao Shia60d9362014-11-12 09:38:21 -070090 subscriberFace->receive(publisherFace->sentDatas[0]);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070091 }
92
93 void
94 afterNotification(const SimpleNotification& notification)
95 {
96 lastNotification = notification;
97 }
98
99 void
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700100 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 Shia60d9362014-11-12 09:38:21 -0700116 if (subscriberFace->sentInterests.empty())
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700117 return 0;
118
Junxiao Shia60d9362014-11-12 09:38:21 -0700119 const Interest& interest = subscriberFace->sentInterests[0];
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700120 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 Shia60d9362014-11-12 09:38:21 -0700132 if (subscriberFace->sentInterests.size() != 1)
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700133 return 0;
134
Junxiao Shia60d9362014-11-12 09:38:21 -0700135 const Interest& interest = subscriberFace->sentInterests[0];
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700136 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
145protected:
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 Shi728873f2015-01-20 16:01:26 -0700152 util::signal::Connection notificationConn;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700153
154 uint64_t lastDeliveredSeqNo;
155
156 SimpleNotification lastNotification;
157 bool hasTimeout;
158 Data lastDecodeErrorData;
159};
160
161BOOST_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 Shi728873f2015-01-20 16:01:26 -0700169 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 Afanasyev4abdbf12014-08-11 12:48:54 -0700173
174 // not received when subscriber is not running
175 this->deliverNotification("n1");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800176 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700177 BOOST_CHECK(lastNotification.getMessage().empty());
Junxiao Shia60d9362014-11-12 09:38:21 -0700178 BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700179
Junxiao Shia60d9362014-11-12 09:38:21 -0700180 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700181 subscriber.start();
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800182 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700183 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true);
184 BOOST_CHECK(this->hasInitialRequest());
185
186 // respond to initial request
Junxiao Shia60d9362014-11-12 09:38:21 -0700187 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700188 this->deliverNotification("n2");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800189 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700190 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2");
191 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
192
193 // respond to continuation request
Junxiao Shia60d9362014-11-12 09:38:21 -0700194 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700195 this->deliverNotification("n3");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800196 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700197 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3");
198 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
199
200 // timeout
Junxiao Shia60d9362014-11-12 09:38:21 -0700201 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700202 lastNotification.setMessage("");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800203 advanceClocks(subscriber.getInterestLifetime(), 2);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700204 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 Shia60d9362014-11-12 09:38:21 -0700214 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700215 lastNotification.setMessage("");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800216 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700217 BOOST_CHECK(lastNotification.getMessage().empty());
218 BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName);
219 BOOST_CHECK(this->hasInitialRequest());
220
221 // decode error in payload
Junxiao Shia60d9362014-11-12 09:38:21 -0700222 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700223 lastNotification.setMessage("");
224 this->deliverNotification("\x07n4");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800225 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700226 BOOST_CHECK(lastNotification.getMessage().empty());
227 BOOST_CHECK(this->hasInitialRequest());
228
229 // stop if handlers are cleared
Junxiao Shi728873f2015-01-20 16:01:26 -0700230 notificationConn.disconnect();
Junxiao Shia60d9362014-11-12 09:38:21 -0700231 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700232 this->deliverNotification("n5");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800233 advanceClocks(time::milliseconds(1));
Junxiao Shia60d9362014-11-12 09:38:21 -0700234 BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700235}
236
237BOOST_AUTO_TEST_SUITE_END()
238
239} // namespace tests
Junxiao Shia60d9362014-11-12 09:38:21 -0700240} // namespace util
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700241} // namespace ndn