blob: 96f5bf41176c246d0e6f7d5d78ec2f355e1951c5 [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"
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
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:
152 Name streamPrefix;
153 shared_ptr<DummyClientFace> publisherFace;
154 ndn::KeyChain publisherKeyChain;
155 util::NotificationStream<SimpleNotification> notificationStream;
156 shared_ptr<DummyClientFace> subscriberFace;
157 util::NotificationSubscriber<SimpleNotification> subscriber;
158
159 uint64_t lastDeliveredSeqNo;
160
161 SimpleNotification lastNotification;
162 bool hasTimeout;
163 Data lastDecodeErrorData;
164};
165
166BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
167{
168 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
169
170 // has no effect because onNotification has no handler
171 subscriber.start();
172 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
173
174 subscriber.onNotification += bind(&EndToEndFixture::afterNotification, this, _1);
175 subscriber.onTimeout += bind(&EndToEndFixture::afterTimeout, this);
176 subscriber.onDecodeError += bind(&EndToEndFixture::afterDecodeError, this, _1);
177
178 // not received when subscriber is not running
179 this->deliverNotification("n1");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800180 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700181 BOOST_CHECK(lastNotification.getMessage().empty());
Junxiao Shia60d9362014-11-12 09:38:21 -0700182 BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700183
Junxiao Shia60d9362014-11-12 09:38:21 -0700184 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700185 subscriber.start();
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800186 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700187 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true);
188 BOOST_CHECK(this->hasInitialRequest());
189
190 // respond to initial request
Junxiao Shia60d9362014-11-12 09:38:21 -0700191 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700192 this->deliverNotification("n2");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800193 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700194 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2");
195 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
196
197 // respond to continuation request
Junxiao Shia60d9362014-11-12 09:38:21 -0700198 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700199 this->deliverNotification("n3");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800200 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700201 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3");
202 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
203
204 // timeout
Junxiao Shia60d9362014-11-12 09:38:21 -0700205 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700206 lastNotification.setMessage("");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800207 advanceClocks(subscriber.getInterestLifetime(), 2);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700208 BOOST_CHECK(lastNotification.getMessage().empty());
209 BOOST_CHECK_EQUAL(hasTimeout, true);
210 BOOST_CHECK(this->hasInitialRequest());
211
212 // decode error on sequence number
213 Name wrongName = streamPrefix;
214 wrongName.append("%07%07");
215 Data wrongData(wrongName);
216 publisherKeyChain.sign(wrongData);
217 subscriberFace->receive(wrongData);
Junxiao Shia60d9362014-11-12 09:38:21 -0700218 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700219 lastNotification.setMessage("");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800220 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700221 BOOST_CHECK(lastNotification.getMessage().empty());
222 BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName);
223 BOOST_CHECK(this->hasInitialRequest());
224
225 // decode error in payload
Junxiao Shia60d9362014-11-12 09:38:21 -0700226 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700227 lastNotification.setMessage("");
228 this->deliverNotification("\x07n4");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800229 advanceClocks(time::milliseconds(1));
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700230 BOOST_CHECK(lastNotification.getMessage().empty());
231 BOOST_CHECK(this->hasInitialRequest());
232
233 // stop if handlers are cleared
234 subscriber.onNotification += bind(&EndToEndFixture::clearNotificationHandlers, this);
Junxiao Shia60d9362014-11-12 09:38:21 -0700235 subscriberFace->sentInterests.clear();
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700236 this->deliverNotification("n5");
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800237 advanceClocks(time::milliseconds(1));
Junxiao Shia60d9362014-11-12 09:38:21 -0700238 BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700239}
240
241BOOST_AUTO_TEST_SUITE_END()
242
243} // namespace tests
Junxiao Shia60d9362014-11-12 09:38:21 -0700244} // namespace util
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700245} // namespace ndn