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