Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * 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 | **/ |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 24 | |
| 25 | #include "fw/face-table.hpp" |
| 26 | #include "fw/forwarder.hpp" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 29 | #include "tests/daemon/face/dummy-face.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace tests { |
| 33 | |
| 34 | BOOST_FIXTURE_TEST_SUITE(FwFaceTable, BaseFixture) |
| 35 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 36 | static inline void |
| 37 | saveFaceId(std::vector<FaceId>& faceIds, shared_ptr<Face> face) |
| 38 | { |
| 39 | faceIds.push_back(face->getId()); |
| 40 | } |
| 41 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(AddRemove) |
| 43 | { |
| 44 | Forwarder forwarder; |
| 45 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 46 | FaceTable& faceTable = forwarder.getFaceTable(); |
| 47 | std::vector<FaceId> onAddHistory; |
| 48 | std::vector<FaceId> onRemoveHistory; |
| 49 | faceTable.onAdd += bind(&saveFaceId, boost::ref(onAddHistory ), _1); |
| 50 | faceTable.onRemove += bind(&saveFaceId, boost::ref(onRemoveHistory), _1); |
| 51 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 52 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 53 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 54 | |
| 55 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
| 56 | BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); |
| 57 | |
| 58 | forwarder.addFace(face1); |
| 59 | forwarder.addFace(face2); |
| 60 | |
| 61 | BOOST_CHECK_NE(face1->getId(), INVALID_FACEID); |
| 62 | BOOST_CHECK_NE(face2->getId(), INVALID_FACEID); |
| 63 | BOOST_CHECK_NE(face1->getId(), face2->getId()); |
| 64 | |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame] | 65 | FaceId oldId1 = face1->getId(); |
| 66 | faceTable.add(face1); |
| 67 | BOOST_CHECK_EQUAL(face1->getId(), oldId1); |
| 68 | BOOST_CHECK_EQUAL(faceTable.size(), 2); |
| 69 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 70 | BOOST_REQUIRE_EQUAL(onAddHistory.size(), 2); |
| 71 | BOOST_CHECK_EQUAL(onAddHistory[0], face1->getId()); |
| 72 | BOOST_CHECK_EQUAL(onAddHistory[1], face2->getId()); |
| 73 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 74 | face1->close(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 75 | |
| 76 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 77 | |
| 78 | BOOST_REQUIRE_EQUAL(onRemoveHistory.size(), 1); |
| 79 | BOOST_CHECK_EQUAL(onRemoveHistory[0], onAddHistory[0]); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(Enumerate) |
| 83 | { |
| 84 | Forwarder forwarder; |
| 85 | FaceTable& faceTable = forwarder.getFaceTable(); |
| 86 | |
| 87 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 88 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 89 | bool hasFace1 = false, hasFace2 = false; |
| 90 | |
| 91 | BOOST_CHECK_EQUAL(faceTable.size(), 0); |
| 92 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 93 | |
| 94 | faceTable.add(face1); |
| 95 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 96 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 97 | hasFace1 = hasFace2 = false; |
| 98 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 99 | if (*it == face1) { |
| 100 | hasFace1 = true; |
| 101 | } |
| 102 | } |
| 103 | BOOST_CHECK(hasFace1); |
| 104 | |
| 105 | faceTable.add(face2); |
| 106 | BOOST_CHECK_EQUAL(faceTable.size(), 2); |
| 107 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 108 | hasFace1 = hasFace2 = false; |
| 109 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 110 | if (*it == face1) { |
| 111 | hasFace1 = true; |
| 112 | } |
| 113 | if (*it == face2) { |
| 114 | hasFace2 = true; |
| 115 | } |
| 116 | } |
| 117 | BOOST_CHECK(hasFace1); |
| 118 | BOOST_CHECK(hasFace2); |
| 119 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 120 | face1->close(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 122 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 123 | hasFace1 = hasFace2 = false; |
| 124 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 125 | if (*it == face1) { |
| 126 | hasFace1 = true; |
| 127 | } |
| 128 | if (*it == face2) { |
| 129 | hasFace2 = true; |
| 130 | } |
| 131 | } |
| 132 | BOOST_CHECK(!hasFace1); |
| 133 | BOOST_CHECK(hasFace2); |
| 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_SUITE_END() |
| 137 | |
| 138 | } // namespace tests |
| 139 | } // namespace nfd |