blob: 32f5a20333d6d05cc5e141295c98cf5b21487576 [file] [log] [blame]
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060024
25#include "mgmt/notification-stream.hpp"
26#include "mgmt/internal-face.hpp"
27
28#include "tests/test-common.hpp"
29
30
31namespace nfd {
32namespace tests {
33
34NFD_LOG_INIT("NotificationStreamTest");
35
36
37
38class NotificationStreamFixture : public BaseFixture
39{
40public:
41 NotificationStreamFixture()
42 : m_callbackFired(false)
43 , m_prefix("/localhost/nfd/NotificationStreamTest")
44 , m_message("TestNotificationMessage")
45 , m_sequenceNo(0)
46 {
47 }
48
49 virtual
50 ~NotificationStreamFixture()
51 {
52 }
53
54 void
55 validateCallback(const Data& data)
56 {
57 Name expectedName(m_prefix);
58 expectedName.appendSegment(m_sequenceNo);
59 BOOST_REQUIRE_EQUAL(data.getName(), expectedName);
60
61 ndn::Block payload = data.getContent();
62 std::string message;
63
64 message.append(reinterpret_cast<const char*>(payload.value()), payload.value_size());
65
66 BOOST_REQUIRE_EQUAL(message, m_message);
67
68 m_callbackFired = true;
69 ++m_sequenceNo;
70 }
71
72 void
73 resetCallbackFired()
74 {
75 m_callbackFired = false;
76 }
77
78protected:
79 bool m_callbackFired;
80 const std::string m_prefix;
81 const std::string m_message;
82 uint64_t m_sequenceNo;
83};
84
85BOOST_FIXTURE_TEST_SUITE(MgmtNotificationStream, NotificationStreamFixture)
86
87class TestNotification
88{
89public:
90 TestNotification(const std::string& message)
91 : m_message(message)
92 {
93 }
94
95 ~TestNotification()
96 {
97 }
98
99 Block
100 wireEncode() const
101 {
102 ndn::EncodingBuffer buffer;
103
104 prependByteArrayBlock(buffer,
105 ndn::Tlv::Content,
106 reinterpret_cast<const uint8_t*>(m_message.c_str()),
107 m_message.size());
108 return buffer.block();
109 }
110
111private:
112 const std::string m_message;
113};
114
115BOOST_AUTO_TEST_CASE(TestPostEvent)
116{
117 shared_ptr<InternalFace> face(make_shared<InternalFace>());
118 NotificationStream notificationStream(face, "/localhost/nfd/NotificationStreamTest");
119
120 face->onReceiveData += bind(&NotificationStreamFixture::validateCallback, this, _1);
121
122 TestNotification event1(m_message);
123 notificationStream.postNotification(event1);
124
125 BOOST_REQUIRE(m_callbackFired);
126
127 resetCallbackFired();
128
129 TestNotification event2(m_message);
130 notificationStream.postNotification(event2);
131
132 BOOST_REQUIRE(m_callbackFired);
133}
134
135
136BOOST_AUTO_TEST_SUITE_END()
137
138
139} // namespace tests
140} // namespace nfd