blob: 9d7ab65736c07eb84e92f5651fd077fa1210d011 [file] [log] [blame]
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
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"
51
52#include "boost-test.hpp"
Junxiao Shia60d9362014-11-12 09:38:21 -070053#include <boost/asio.hpp>
54#include "util/dummy-client-face.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
62class EndToEndFixture
63{
64public:
65 EndToEndFixture()
66 : streamPrefix("ndn:/NotificationSubscriberTest")
Alexander Afanasyev370d2602014-08-20 10:21:34 -050067 , publisherFace(makeDummyClientFace(ioService))
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070068 , notificationStream(*publisherFace, streamPrefix, publisherKeyChain)
Alexander Afanasyev370d2602014-08-20 10:21:34 -050069 , subscriberFace(makeDummyClientFace(ioService))
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
83 publisherFace->processEvents(time::milliseconds(100));
84
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
100 clearNotificationHandlers()
101 {
102 subscriber.onNotification.clear();
103 }
104
105 void
106 afterTimeout()
107 {
108 hasTimeout = true;
109 }
110
111 void
112 afterDecodeError(const Data& data)
113 {
114 lastDecodeErrorData = data;
115 }
116
117 /** \return true if subscriberFace has an initial request (first sent Interest)
118 */
119 bool
120 hasInitialRequest() const
121 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700122 if (subscriberFace->sentInterests.empty())
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700123 return 0;
124
Junxiao Shia60d9362014-11-12 09:38:21 -0700125 const Interest& interest = subscriberFace->sentInterests[0];
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700126 return interest.getName() == streamPrefix &&
127 interest.getChildSelector() == 1 &&
128 interest.getMustBeFresh() &&
129 interest.getInterestLifetime() == subscriber.getInterestLifetime();
130 }
131
132 /** \return sequence number of the continuation request sent from subscriberFace
133 * or 0 if there's no such request as sole sent Interest
134 */
135 uint64_t
136 getRequestSeqNo() const
137 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700138 if (subscriberFace->sentInterests.size() != 1)
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700139 return 0;
140
Junxiao Shia60d9362014-11-12 09:38:21 -0700141 const Interest& interest = subscriberFace->sentInterests[0];
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700142 const Name& name = interest.getName();
143 if (streamPrefix.isPrefixOf(name) &&
144 name.size() == streamPrefix.size() + 1 &&
145 interest.getInterestLifetime() == subscriber.getInterestLifetime())
146 return name[-1].toSequenceNumber();
147 else
148 return 0;
149 }
150
151protected:
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500152 boost::asio::io_service ioService;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700153 Name streamPrefix;
154 shared_ptr<DummyClientFace> publisherFace;
155 ndn::KeyChain publisherKeyChain;
156 util::NotificationStream<SimpleNotification> notificationStream;
157 shared_ptr<DummyClientFace> subscriberFace;
158 util::NotificationSubscriber<SimpleNotification> subscriber;
159
160 uint64_t lastDeliveredSeqNo;
161
162 SimpleNotification lastNotification;
163 bool hasTimeout;
164 Data lastDecodeErrorData;
165};
166
167BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
168{
169 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
170
171 // has no effect because onNotification has no handler
172 subscriber.start();
173 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
174
175 subscriber.onNotification += bind(&EndToEndFixture::afterNotification, this, _1);
176 subscriber.onTimeout += bind(&EndToEndFixture::afterTimeout, this);
177 subscriber.onDecodeError += bind(&EndToEndFixture::afterDecodeError, this, _1);
178
179 // not received when subscriber is not running
180 this->deliverNotification("n1");
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500181 subscriberFace->processEvents(time::milliseconds(100));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700182 BOOST_CHECK(lastNotification.getMessage().empty());
Junxiao Shia60d9362014-11-12 09:38:21 -0700183 BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700184
Junxiao Shia60d9362014-11-12 09:38:21 -0700185 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700186 subscriber.start();
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500187 subscriberFace->processEvents(time::milliseconds(-100));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700188 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true);
189 BOOST_CHECK(this->hasInitialRequest());
190
191 // respond to initial request
Junxiao Shia60d9362014-11-12 09:38:21 -0700192 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700193 this->deliverNotification("n2");
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500194 subscriberFace->processEvents(time::milliseconds(-100));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700195 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2");
196 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
197
198 // respond to continuation request
Junxiao Shia60d9362014-11-12 09:38:21 -0700199 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700200 this->deliverNotification("n3");
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500201 subscriberFace->processEvents(time::milliseconds(-100));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700202 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3");
203 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
204
205 // timeout
Junxiao Shia60d9362014-11-12 09:38:21 -0700206 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700207 lastNotification.setMessage("");
208 subscriberFace->processEvents(2 * subscriber.getInterestLifetime());
209 BOOST_CHECK(lastNotification.getMessage().empty());
210 BOOST_CHECK_EQUAL(hasTimeout, true);
211 BOOST_CHECK(this->hasInitialRequest());
212
213 // decode error on sequence number
214 Name wrongName = streamPrefix;
215 wrongName.append("%07%07");
216 Data wrongData(wrongName);
217 publisherKeyChain.sign(wrongData);
218 subscriberFace->receive(wrongData);
Junxiao Shia60d9362014-11-12 09:38:21 -0700219 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700220 lastNotification.setMessage("");
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500221 subscriberFace->processEvents(time::milliseconds(-100));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700222 BOOST_CHECK(lastNotification.getMessage().empty());
223 BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName);
224 BOOST_CHECK(this->hasInitialRequest());
225
226 // decode error in payload
Junxiao Shia60d9362014-11-12 09:38:21 -0700227 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700228 lastNotification.setMessage("");
229 this->deliverNotification("\x07n4");
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500230 subscriberFace->processEvents(time::milliseconds(-100));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700231 BOOST_CHECK(lastNotification.getMessage().empty());
232 BOOST_CHECK(this->hasInitialRequest());
233
234 // stop if handlers are cleared
235 subscriber.onNotification += bind(&EndToEndFixture::clearNotificationHandlers, this);
Junxiao Shia60d9362014-11-12 09:38:21 -0700236 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700237 this->deliverNotification("n5");
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500238 subscriberFace->processEvents(time::milliseconds(-100));
Junxiao Shia60d9362014-11-12 09:38:21 -0700239 BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700240}
241
242BOOST_AUTO_TEST_SUITE_END()
243
244} // namespace tests
Junxiao Shia60d9362014-11-12 09:38:21 -0700245} // namespace util
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700246} // namespace ndn