blob: 5fcce80e8f129fa4c3ba06a665fe2a6c710657d4 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi5b43f9a2016-07-19 13:15:56 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi7b984c62014-07-17 22:18:34 -070024 */
Junxiao Shia4f2be82014-03-02 22:56:41 -070025
26#include "fw/face-table.hpp"
27#include "fw/forwarder.hpp"
28
29#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070030#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070031
32namespace nfd {
33namespace tests {
34
Junxiao Shicde37ad2015-12-24 01:02:05 -070035BOOST_AUTO_TEST_SUITE(Fw)
36BOOST_FIXTURE_TEST_SUITE(TestFaceTable, BaseFixture)
Junxiao Shia4f2be82014-03-02 22:56:41 -070037
38BOOST_AUTO_TEST_CASE(AddRemove)
39{
40 Forwarder forwarder;
Junxiao Shibd392bf2014-03-17 15:54:11 -070041 FaceTable& faceTable = forwarder.getFaceTable();
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000042
Junxiao Shicde37ad2015-12-24 01:02:05 -070043 std::vector<FaceId> addHistory;
44 std::vector<FaceId> removeHistory;
Junxiao Shiae04d342016-07-19 13:20:22 +000045 faceTable.afterAdd.connect([&] (const Face& face) { addHistory.push_back(face.getId()); });
46 faceTable.beforeRemove.connect([&] (const Face& face) { removeHistory.push_back(face.getId()); });
Junxiao Shibd392bf2014-03-17 15:54:11 -070047
Junxiao Shia4f2be82014-03-02 22:56:41 -070048 shared_ptr<Face> face1 = make_shared<DummyFace>();
49 shared_ptr<Face> face2 = make_shared<DummyFace>();
50
Junxiao Shicde37ad2015-12-24 01:02:05 -070051 BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID);
52 BOOST_CHECK_EQUAL(face2->getId(), face::INVALID_FACEID);
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000053 BOOST_CHECK(faceTable.get(face::INVALID_FACEID) == nullptr);
54 BOOST_CHECK_EQUAL(faceTable.size(), 0);
Junxiao Shia4f2be82014-03-02 22:56:41 -070055
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000056 faceTable.add(face1);
57 faceTable.add(face2);
58 BOOST_CHECK_EQUAL(faceTable.size(), 2);
Junxiao Shia4f2be82014-03-02 22:56:41 -070059
Junxiao Shicde37ad2015-12-24 01:02:05 -070060 BOOST_CHECK_NE(face1->getId(), face::INVALID_FACEID);
61 BOOST_CHECK_NE(face2->getId(), face::INVALID_FACEID);
Junxiao Shia4f2be82014-03-02 22:56:41 -070062 BOOST_CHECK_NE(face1->getId(), face2->getId());
Junxiao Shicde37ad2015-12-24 01:02:05 -070063 BOOST_CHECK_GT(face1->getId(), face::FACEID_RESERVED_MAX);
64 BOOST_CHECK_GT(face2->getId(), face::FACEID_RESERVED_MAX);
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000065 BOOST_CHECK(faceTable.get(face1->getId()) == face1.get());
66 BOOST_CHECK(faceTable.get(face2->getId()) == face2.get());
Junxiao Shia4f2be82014-03-02 22:56:41 -070067
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070068 FaceId oldId1 = face1->getId();
69 faceTable.add(face1);
70 BOOST_CHECK_EQUAL(face1->getId(), oldId1);
71 BOOST_CHECK_EQUAL(faceTable.size(), 2);
72
Junxiao Shicde37ad2015-12-24 01:02:05 -070073 BOOST_REQUIRE_EQUAL(addHistory.size(), 2);
74 BOOST_CHECK_EQUAL(addHistory[0], face1->getId());
75 BOOST_CHECK_EQUAL(addHistory[1], face2->getId());
Junxiao Shibd392bf2014-03-17 15:54:11 -070076
Junxiao Shic542b2b2014-03-16 21:45:52 -070077 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -070078
Junxiao Shicde37ad2015-12-24 01:02:05 -070079 BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID);
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000080 BOOST_CHECK_EQUAL(faceTable.size(), 1);
81 BOOST_CHECK(faceTable.get(oldId1) == nullptr);
Junxiao Shibd392bf2014-03-17 15:54:11 -070082
Junxiao Shicde37ad2015-12-24 01:02:05 -070083 BOOST_REQUIRE_EQUAL(removeHistory.size(), 1);
84 BOOST_CHECK_EQUAL(removeHistory[0], addHistory[0]);
Junxiao Shia4f2be82014-03-02 22:56:41 -070085}
86
Junxiao Shi7b984c62014-07-17 22:18:34 -070087BOOST_AUTO_TEST_CASE(AddReserved)
88{
89 Forwarder forwarder;
90 FaceTable& faceTable = forwarder.getFaceTable();
91
92 shared_ptr<Face> face1 = make_shared<DummyFace>();
Junxiao Shicde37ad2015-12-24 01:02:05 -070093 BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID);
Junxiao Shi7b984c62014-07-17 22:18:34 -070094
95 faceTable.addReserved(face1, 5);
96 BOOST_CHECK_EQUAL(face1->getId(), 5);
97}
98
Junxiao Shia4f2be82014-03-02 22:56:41 -070099BOOST_AUTO_TEST_CASE(Enumerate)
100{
101 Forwarder forwarder;
102 FaceTable& faceTable = forwarder.getFaceTable();
103
104 shared_ptr<Face> face1 = make_shared<DummyFace>();
105 shared_ptr<Face> face2 = make_shared<DummyFace>();
106 bool hasFace1 = false, hasFace2 = false;
107
108 BOOST_CHECK_EQUAL(faceTable.size(), 0);
109 BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
110
111 faceTable.add(face1);
112 BOOST_CHECK_EQUAL(faceTable.size(), 1);
113 BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
114 hasFace1 = hasFace2 = false;
115 for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
Junxiao Shib84e6742016-07-19 13:16:22 +0000116 hasFace1 = hasFace1 || &*it == face1.get();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700117 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000118 BOOST_CHECK_EQUAL(hasFace1, true);
Junxiao Shia4f2be82014-03-02 22:56:41 -0700119
120 faceTable.add(face2);
121 BOOST_CHECK_EQUAL(faceTable.size(), 2);
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) {
Junxiao Shib84e6742016-07-19 13:16:22 +0000125 hasFace1 = hasFace1 || &*it == face1.get();
126 hasFace2 = hasFace2 || &*it == face2.get();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700127 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000128 BOOST_CHECK_EQUAL(hasFace1, true);
129 BOOST_CHECK_EQUAL(hasFace2, true);
Junxiao Shia4f2be82014-03-02 22:56:41 -0700130
Junxiao Shic542b2b2014-03-16 21:45:52 -0700131 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700132 BOOST_CHECK_EQUAL(faceTable.size(), 1);
133 BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
134 hasFace1 = hasFace2 = false;
135 for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
Junxiao Shib84e6742016-07-19 13:16:22 +0000136 hasFace1 = hasFace1 || &*it == face1.get();
137 hasFace2 = hasFace2 || &*it == face2.get();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700138 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000139 BOOST_CHECK_EQUAL(hasFace1, false);
140 BOOST_CHECK_EQUAL(hasFace2, true);
Junxiao Shia4f2be82014-03-02 22:56:41 -0700141}
142
Junxiao Shicde37ad2015-12-24 01:02:05 -0700143BOOST_AUTO_TEST_SUITE_END() // TestFaceTable
144BOOST_AUTO_TEST_SUITE_END() // Fw
Junxiao Shia4f2be82014-03-02 22:56:41 -0700145
146} // namespace tests
147} // namespace nfd