blob: 0dd8432524310346e377cef52e243735d3a10496 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7b984c62014-07-17 22:18:34 -07003 * 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 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
35BOOST_FIXTURE_TEST_SUITE(FwFaceTable, BaseFixture)
36
Junxiao Shibd392bf2014-03-17 15:54:11 -070037static inline void
38saveFaceId(std::vector<FaceId>& faceIds, shared_ptr<Face> face)
39{
40 faceIds.push_back(face->getId());
41}
42
Junxiao Shia4f2be82014-03-02 22:56:41 -070043BOOST_AUTO_TEST_CASE(AddRemove)
44{
45 Forwarder forwarder;
46
Junxiao Shibd392bf2014-03-17 15:54:11 -070047 FaceTable& faceTable = forwarder.getFaceTable();
48 std::vector<FaceId> onAddHistory;
49 std::vector<FaceId> onRemoveHistory;
Alexander Afanasyevf6980282014-05-13 18:28:40 -070050 faceTable.onAdd += bind(&saveFaceId, ndn::ref(onAddHistory ), _1);
51 faceTable.onRemove += bind(&saveFaceId, ndn::ref(onRemoveHistory), _1);
Junxiao Shibd392bf2014-03-17 15:54:11 -070052
Junxiao Shia4f2be82014-03-02 22:56:41 -070053 shared_ptr<Face> face1 = make_shared<DummyFace>();
54 shared_ptr<Face> face2 = make_shared<DummyFace>();
55
56 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
57 BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
58
59 forwarder.addFace(face1);
60 forwarder.addFace(face2);
61
62 BOOST_CHECK_NE(face1->getId(), INVALID_FACEID);
63 BOOST_CHECK_NE(face2->getId(), INVALID_FACEID);
64 BOOST_CHECK_NE(face1->getId(), face2->getId());
Junxiao Shi7b984c62014-07-17 22:18:34 -070065 BOOST_CHECK_GT(face1->getId(), FACEID_RESERVED_MAX);
66 BOOST_CHECK_GT(face2->getId(), FACEID_RESERVED_MAX);
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 Shibd392bf2014-03-17 15:54:11 -070073 BOOST_REQUIRE_EQUAL(onAddHistory.size(), 2);
74 BOOST_CHECK_EQUAL(onAddHistory[0], face1->getId());
75 BOOST_CHECK_EQUAL(onAddHistory[1], face2->getId());
76
Junxiao Shic542b2b2014-03-16 21:45:52 -070077 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -070078
79 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
Junxiao Shibd392bf2014-03-17 15:54:11 -070080
81 BOOST_REQUIRE_EQUAL(onRemoveHistory.size(), 1);
82 BOOST_CHECK_EQUAL(onRemoveHistory[0], onAddHistory[0]);
Junxiao Shia4f2be82014-03-02 22:56:41 -070083}
84
Junxiao Shi7b984c62014-07-17 22:18:34 -070085BOOST_AUTO_TEST_CASE(AddReserved)
86{
87 Forwarder forwarder;
88 FaceTable& faceTable = forwarder.getFaceTable();
89
90 shared_ptr<Face> face1 = make_shared<DummyFace>();
91 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
92
93 faceTable.addReserved(face1, 5);
94 BOOST_CHECK_EQUAL(face1->getId(), 5);
95}
96
Junxiao Shia4f2be82014-03-02 22:56:41 -070097BOOST_AUTO_TEST_CASE(Enumerate)
98{
99 Forwarder forwarder;
100 FaceTable& faceTable = forwarder.getFaceTable();
101
102 shared_ptr<Face> face1 = make_shared<DummyFace>();
103 shared_ptr<Face> face2 = make_shared<DummyFace>();
104 bool hasFace1 = false, hasFace2 = false;
105
106 BOOST_CHECK_EQUAL(faceTable.size(), 0);
107 BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
108
109 faceTable.add(face1);
110 BOOST_CHECK_EQUAL(faceTable.size(), 1);
111 BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
112 hasFace1 = hasFace2 = false;
113 for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
114 if (*it == face1) {
115 hasFace1 = true;
116 }
117 }
118 BOOST_CHECK(hasFace1);
119
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) {
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
Junxiao Shic542b2b2014-03-16 21:45:52 -0700135 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700136 BOOST_CHECK_EQUAL(faceTable.size(), 1);
137 BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
138 hasFace1 = hasFace2 = false;
139 for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
140 if (*it == face1) {
141 hasFace1 = true;
142 }
143 if (*it == face2) {
144 hasFace2 = true;
145 }
146 }
147 BOOST_CHECK(!hasFace1);
148 BOOST_CHECK(hasFace2);
149}
150
151BOOST_AUTO_TEST_SUITE_END()
152
153} // namespace tests
154} // namespace nfd