tests: Add partial unit test coverage for Face class
Change-Id: Ica156cae150b8fbb7cb5161765960f248e3eef70
Refs: #1947
diff --git a/tests/unit-tests/dummy-client-face.hpp b/tests/unit-tests/dummy-client-face.hpp
index b744882..82d9c7c 100644
--- a/tests/unit-tests/dummy-client-face.hpp
+++ b/tests/unit-tests/dummy-client-face.hpp
@@ -24,6 +24,9 @@
#include "face.hpp"
#include "transport/transport.hpp"
+#include "management/nfd-controller.hpp"
+#include "management/nfd-control-response.hpp"
+#include "util/event-emitter.hpp"
namespace ndn {
namespace tests {
@@ -57,10 +60,12 @@
send(const Block& wire)
{
if (wire.type() == tlv::Interest) {
- m_sentInterests->push_back(Interest(wire));
+ shared_ptr<Interest> interest = make_shared<Interest>(wire);
+ (*m_onInterest)(*interest, this);
}
else if (wire.type() == tlv::Data) {
- m_sentDatas->push_back(Data(wire));
+ shared_ptr<Data> data = make_shared<Data>(wire);
+ (*m_onData)(*data, this);
}
}
@@ -70,12 +75,45 @@
this->send(payload);
}
-public:
- std::vector<Interest>* m_sentInterests;
- std::vector<Data>* m_sentDatas;
+ boost::asio::io_service&
+ getIoService()
+ {
+ return *m_ioService;
+ }
+
+private:
+ friend class DummyClientFace;
+ util::EventEmitter<Interest, DummyClientTransport*>* m_onInterest;
+ util::EventEmitter<Data, DummyClientTransport*>* m_onData;
};
+/** \brief Callback to connect
+ */
+inline void
+replyNfdRibCommands(const Interest& interest, DummyClientTransport* transport)
+{
+ static const Name localhostRegistration("/localhost/nfd/rib");
+ if (localhostRegistration.isPrefixOf(interest.getName())) {
+ shared_ptr<Data> okResponse = make_shared<Data>(interest.getName());
+ nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
+ params.setFaceId(1);
+ params.setOrigin(0);
+ if (interest.getName().get(3) == name::Component("register")) {
+ params.setCost(0);
+ }
+ nfd::ControlResponse resp;
+ resp.setCode(200);
+ resp.setBody(params.wireEncode());
+ okResponse->setContent(resp.wireEncode());
+ KeyChain keyChain;
+ keyChain.signWithSha256(*okResponse);
+
+ transport->getIoService().post(bind(&DummyClientTransport::receive, transport,
+ okResponse->wireEncode()));
+ }
+}
+
/** \brief a client-side face for unit testing
*/
class DummyClientFace : public ndn::Face
@@ -86,16 +124,20 @@
: Face(transport)
, m_transport(transport)
{
- m_transport->m_sentInterests = &m_sentInterests;
- m_transport->m_sentDatas = &m_sentDatas;
+ m_transport->m_onInterest = &onInterest;
+ m_transport->m_onData = &onData;
+
+ enablePacketLogging();
}
DummyClientFace(shared_ptr<DummyClientTransport> transport, boost::asio::io_service& ioService)
: Face(transport, ioService)
, m_transport(transport)
{
- m_transport->m_sentInterests = &m_sentInterests;
- m_transport->m_sentDatas = &m_sentDatas;
+ m_transport->m_onInterest = &onInterest;
+ m_transport->m_onData = &onData;
+
+ enablePacketLogging();
}
/** \brief cause the Face to receive a packet
@@ -107,6 +149,26 @@
m_transport->receive(packet.wireEncode());
}
+ void
+ enablePacketLogging()
+ {
+ // @todo Replace with C++11 lambdas
+
+ onInterest += bind(static_cast<void(std::vector<Interest>::*)(const Interest&)>(
+ &std::vector<Interest>::push_back),
+ &m_sentInterests, _1);
+
+ onData += bind(static_cast<void(std::vector<Data>::*)(const Data&)>(
+ &std::vector<Data>::push_back),
+ &m_sentDatas, _1);
+ }
+
+ void
+ enableRegistrationReply()
+ {
+ onInterest += &replyNfdRibCommands;
+ }
+
public:
/** \brief sent Interests
* \note After .expressInterest, .processEvents must be called before
@@ -119,6 +181,19 @@
*/
std::vector<Data> m_sentDatas;
+public:
+ /** \brief Event to be called whenever an Interest is received
+ * \note After .expressInterest, .processEvents must be called before
+ * the Interest would show up here.
+ */
+ util::EventEmitter<Interest, DummyClientTransport*> onInterest;
+
+ /** \brief Event to be called whenever a Data packet is received
+ * \note After .put, .processEvents must be called before
+ * the Interest would show up here.
+ */
+ util::EventEmitter<Data, DummyClientTransport*> onData;
+
private:
shared_ptr<DummyClientTransport> m_transport;
};
@@ -135,6 +210,7 @@
return make_shared<DummyClientFace>(make_shared<DummyClientTransport>(), ref(ioService));
}
+
} // namespace tests
} // namespace ndn