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