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