blob: 1633ccfcb2114ff24522448cf0f986e65ed194fb [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"
53#include "../dummy-client-face.hpp"
54
55namespace ndn {
56namespace tests {
57
58BOOST_AUTO_TEST_SUITE(UtilNotificationSubscriber)
59
60class EndToEndFixture
61{
62public:
63 EndToEndFixture()
64 : streamPrefix("ndn:/NotificationSubscriberTest")
65 , publisherFace(makeDummyClientFace())
66 , notificationStream(*publisherFace, streamPrefix, publisherKeyChain)
67 , subscriberFace(makeDummyClientFace())
68 , subscriber(*subscriberFace, streamPrefix, time::seconds(1))
69 {
70 }
71
72 /** \brief post one notification and deliver to subscriber
73 */
74 void
75 deliverNotification(const std::string& msg)
76 {
77 publisherFace->m_sentDatas.clear();
78 SimpleNotification notification(msg);
79 notificationStream.postNotification(notification);
80 publisherFace->processEvents();
81 BOOST_REQUIRE_EQUAL(publisherFace->m_sentDatas.size(), 1);
82
83 lastDeliveredSeqNo = publisherFace->m_sentDatas[0].getName().at(-1).toSequenceNumber();
84
85 lastNotification.setMessage("");
86 subscriberFace->receive(publisherFace->m_sentDatas[0]);
87 }
88
89 void
90 afterNotification(const SimpleNotification& notification)
91 {
92 lastNotification = notification;
93 }
94
95 void
96 clearNotificationHandlers()
97 {
98 subscriber.onNotification.clear();
99 }
100
101 void
102 afterTimeout()
103 {
104 hasTimeout = true;
105 }
106
107 void
108 afterDecodeError(const Data& data)
109 {
110 lastDecodeErrorData = data;
111 }
112
113 /** \return true if subscriberFace has an initial request (first sent Interest)
114 */
115 bool
116 hasInitialRequest() const
117 {
118 if (subscriberFace->m_sentInterests.empty())
119 return 0;
120
121 const Interest& interest = subscriberFace->m_sentInterests[0];
122 return interest.getName() == streamPrefix &&
123 interest.getChildSelector() == 1 &&
124 interest.getMustBeFresh() &&
125 interest.getInterestLifetime() == subscriber.getInterestLifetime();
126 }
127
128 /** \return sequence number of the continuation request sent from subscriberFace
129 * or 0 if there's no such request as sole sent Interest
130 */
131 uint64_t
132 getRequestSeqNo() const
133 {
134 if (subscriberFace->m_sentInterests.size() != 1)
135 return 0;
136
137 const Interest& interest = subscriberFace->m_sentInterests[0];
138 const Name& name = interest.getName();
139 if (streamPrefix.isPrefixOf(name) &&
140 name.size() == streamPrefix.size() + 1 &&
141 interest.getInterestLifetime() == subscriber.getInterestLifetime())
142 return name[-1].toSequenceNumber();
143 else
144 return 0;
145 }
146
147protected:
148 Name streamPrefix;
149 shared_ptr<DummyClientFace> publisherFace;
150 ndn::KeyChain publisherKeyChain;
151 util::NotificationStream<SimpleNotification> notificationStream;
152 shared_ptr<DummyClientFace> subscriberFace;
153 util::NotificationSubscriber<SimpleNotification> subscriber;
154
155 uint64_t lastDeliveredSeqNo;
156
157 SimpleNotification lastNotification;
158 bool hasTimeout;
159 Data lastDecodeErrorData;
160};
161
162BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
163{
164 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
165
166 // has no effect because onNotification has no handler
167 subscriber.start();
168 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
169
170 subscriber.onNotification += bind(&EndToEndFixture::afterNotification, this, _1);
171 subscriber.onTimeout += bind(&EndToEndFixture::afterTimeout, this);
172 subscriber.onDecodeError += bind(&EndToEndFixture::afterDecodeError, this, _1);
173
174 // not received when subscriber is not running
175 this->deliverNotification("n1");
176 subscriberFace->processEvents(time::milliseconds(10));
177 BOOST_CHECK(lastNotification.getMessage().empty());
178 BOOST_CHECK_EQUAL(subscriberFace->m_sentInterests.size(), 0);
179
180 subscriberFace->m_sentInterests.clear();
181 subscriber.start();
182 subscriberFace->processEvents(time::milliseconds(10));
183 BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true);
184 BOOST_CHECK(this->hasInitialRequest());
185
186 // respond to initial request
187 subscriberFace->m_sentInterests.clear();
188 this->deliverNotification("n2");
189 subscriberFace->processEvents(time::milliseconds(10));
190 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2");
191 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
192
193 // respond to continuation request
194 subscriberFace->m_sentInterests.clear();
195 this->deliverNotification("n3");
196 subscriberFace->processEvents(time::milliseconds(10));
197 BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3");
198 BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
199
200 // timeout
201 subscriberFace->m_sentInterests.clear();
202 lastNotification.setMessage("");
203 subscriberFace->processEvents(2 * subscriber.getInterestLifetime());
204 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);
214 subscriberFace->m_sentInterests.clear();
215 lastNotification.setMessage("");
216 subscriberFace->processEvents(time::milliseconds(10));
217 BOOST_CHECK(lastNotification.getMessage().empty());
218 BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName);
219 BOOST_CHECK(this->hasInitialRequest());
220
221 // decode error in payload
222 subscriberFace->m_sentInterests.clear();
223 lastNotification.setMessage("");
224 this->deliverNotification("\x07n4");
225 subscriberFace->processEvents(time::milliseconds(10));
226 BOOST_CHECK(lastNotification.getMessage().empty());
227 BOOST_CHECK(this->hasInitialRequest());
228
229 // stop if handlers are cleared
230 subscriber.onNotification += bind(&EndToEndFixture::clearNotificationHandlers, this);
231 subscriberFace->m_sentInterests.clear();
232 this->deliverNotification("n5");
233 subscriberFace->processEvents(time::milliseconds(10));
234 BOOST_CHECK_EQUAL(subscriberFace->m_sentInterests.size(), 0);
235}
236
237BOOST_AUTO_TEST_SUITE_END()
238
239} // namespace tests
240} // namespace ndn