blob: 23d882887fab1abde03203cd1c61ad86d5c2b480 [file] [log] [blame]
Junxiao Shi8c8d2182014-01-30 22:33:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "fw/forwarder.hpp"
8#include "../face/dummy-face.hpp"
9
10#include <boost/test/unit_test.hpp>
11
12namespace nfd {
13
14class ForwarderTestFace : public Face {
15public:
16 ForwarderTestFace(boost::asio::io_service& ioService)
17 : m_ioService(ioService)
18 {
19 }
20
21 virtual void
22 sendInterest(const Interest& interest)
23 {
24 m_sentInterests.push_back(interest);
25 m_ioService.stop();
26 }
27
28 virtual void
29 sendData(const Data& data)
30 {
31 m_sentDatas.push_back(data);
32 m_ioService.stop();
33 }
34
35 void
36 receiveInterest(const Interest& interest)
37 {
38 onReceiveInterest(interest);
39 }
40
41 void
42 receiveData(const Data& data)
43 {
44 onReceiveData(data);
45 }
46
47public:
48 std::vector<Interest> m_sentInterests;
49 std::vector<Data> m_sentDatas;
50
51private:
52 boost::asio::io_service& m_ioService;
53};
54
55BOOST_AUTO_TEST_SUITE(FwForwarder)
56
57BOOST_AUTO_TEST_CASE(AddRemoveFace)
58{
59 boost::asio::io_service io;
60 Forwarder forwarder(io);
61
62 shared_ptr<Face> face1 = make_shared<DummyFace>();
63 shared_ptr<Face> face2 = make_shared<DummyFace>();
64
65 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
66 BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
67
68 forwarder.addFace(face1);
69 forwarder.addFace(face2);
70
71 BOOST_CHECK_NE(face1->getId(), INVALID_FACEID);
72 BOOST_CHECK_NE(face2->getId(), INVALID_FACEID);
73 BOOST_CHECK_NE(face1->getId(), face2->getId());
74
75 forwarder.removeFace(face1);
76 forwarder.removeFace(face2);
77
78 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
79 BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
80}
81
82BOOST_AUTO_TEST_CASE(SimpleExchange)
83{
84 Name nameA ("ndn:/A");
85 Name nameAB ("ndn:/A/B");
86 Name nameABC("ndn:/A/B/C");
87 Interest interestAB(nameAB);
88 interestAB.setInterestLifetime(4000);
89 Data dataABC(nameABC);
90
91 boost::asio::io_service io;
92 Forwarder forwarder(io);
93
94 shared_ptr<ForwarderTestFace> face1 = make_shared<ForwarderTestFace>(boost::ref(io));
95 shared_ptr<ForwarderTestFace> face2 = make_shared<ForwarderTestFace>(boost::ref(io));
96 forwarder.addFace(face1);
97 forwarder.addFace(face2);
98
99 Fib& fib = forwarder.getFib();
100 std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
101 fib.insert(Name("ndn:/A"));
102 shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
103 fibEntry->addNextHop(face2, 0);
104
105 face1->receiveInterest(interestAB);
106 io.run();
107 io.reset();
108 BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
109 BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
Junxiao Shi06887ac2014-02-13 20:15:42 -0700110 BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700111
112 face2->receiveData(dataABC);
113 io.run();
114 io.reset();
115 BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1);
116 BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC));
Junxiao Shi06887ac2014-02-13 20:15:42 -0700117 BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700118}
119
120BOOST_AUTO_TEST_SUITE_END()
121
122} // namespace nfd