Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | class ForwarderTestFace : public Face { |
| 15 | public: |
| 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 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 35 | virtual void |
| 36 | close() |
| 37 | { |
| 38 | } |
| 39 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 40 | void |
| 41 | receiveInterest(const Interest& interest) |
| 42 | { |
| 43 | onReceiveInterest(interest); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | receiveData(const Data& data) |
| 48 | { |
| 49 | onReceiveData(data); |
| 50 | } |
| 51 | |
| 52 | public: |
| 53 | std::vector<Interest> m_sentInterests; |
| 54 | std::vector<Data> m_sentDatas; |
| 55 | |
| 56 | private: |
| 57 | boost::asio::io_service& m_ioService; |
| 58 | }; |
| 59 | |
| 60 | BOOST_AUTO_TEST_SUITE(FwForwarder) |
| 61 | |
| 62 | BOOST_AUTO_TEST_CASE(AddRemoveFace) |
| 63 | { |
| 64 | boost::asio::io_service io; |
| 65 | Forwarder forwarder(io); |
| 66 | |
| 67 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 68 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 69 | |
| 70 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
| 71 | BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); |
| 72 | |
| 73 | forwarder.addFace(face1); |
| 74 | forwarder.addFace(face2); |
| 75 | |
| 76 | BOOST_CHECK_NE(face1->getId(), INVALID_FACEID); |
| 77 | BOOST_CHECK_NE(face2->getId(), INVALID_FACEID); |
| 78 | BOOST_CHECK_NE(face1->getId(), face2->getId()); |
| 79 | |
| 80 | forwarder.removeFace(face1); |
| 81 | forwarder.removeFace(face2); |
| 82 | |
| 83 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
| 84 | BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(SimpleExchange) |
| 88 | { |
| 89 | Name nameA ("ndn:/A"); |
| 90 | Name nameAB ("ndn:/A/B"); |
| 91 | Name nameABC("ndn:/A/B/C"); |
| 92 | Interest interestAB(nameAB); |
| 93 | interestAB.setInterestLifetime(4000); |
| 94 | Data dataABC(nameABC); |
| 95 | |
| 96 | boost::asio::io_service io; |
| 97 | Forwarder forwarder(io); |
| 98 | |
| 99 | shared_ptr<ForwarderTestFace> face1 = make_shared<ForwarderTestFace>(boost::ref(io)); |
| 100 | shared_ptr<ForwarderTestFace> face2 = make_shared<ForwarderTestFace>(boost::ref(io)); |
| 101 | forwarder.addFace(face1); |
| 102 | forwarder.addFace(face2); |
| 103 | |
| 104 | Fib& fib = forwarder.getFib(); |
| 105 | std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = |
| 106 | fib.insert(Name("ndn:/A")); |
| 107 | shared_ptr<fib::Entry> fibEntry = fibInsertResult.first; |
| 108 | fibEntry->addNextHop(face2, 0); |
| 109 | |
| 110 | face1->receiveInterest(interestAB); |
| 111 | io.run(); |
| 112 | io.reset(); |
| 113 | BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1); |
| 114 | BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB)); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 116 | |
| 117 | face2->receiveData(dataABC); |
| 118 | io.run(); |
| 119 | io.reset(); |
| 120 | BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1); |
| 121 | BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC)); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | BOOST_AUTO_TEST_SUITE_END() |
| 126 | |
| 127 | } // namespace nfd |