blob: 839018abc8dbd970a422f44208bcdc516784a60c [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"
Junxiao Shia4f2be82014-03-02 22:56:41 -070027
28#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070029#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070030
31namespace nfd {
32namespace tests {
33
Junxiao Shicde37ad2015-12-24 01:02:05 -070034BOOST_AUTO_TEST_SUITE(Fw)
35BOOST_FIXTURE_TEST_SUITE(TestFaceTable, BaseFixture)
Junxiao Shia4f2be82014-03-02 22:56:41 -070036
37BOOST_AUTO_TEST_CASE(AddRemove)
38{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000039 FaceTable faceTable;
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000040
Junxiao Shicde37ad2015-12-24 01:02:05 -070041 std::vector<FaceId> addHistory;
42 std::vector<FaceId> removeHistory;
Junxiao Shiae04d342016-07-19 13:20:22 +000043 faceTable.afterAdd.connect([&] (const Face& face) { addHistory.push_back(face.getId()); });
44 faceTable.beforeRemove.connect([&] (const Face& face) { removeHistory.push_back(face.getId()); });
Junxiao Shibd392bf2014-03-17 15:54:11 -070045
Junxiao Shia4f2be82014-03-02 22:56:41 -070046 shared_ptr<Face> face1 = make_shared<DummyFace>();
47 shared_ptr<Face> face2 = make_shared<DummyFace>();
48
Junxiao Shicde37ad2015-12-24 01:02:05 -070049 BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID);
50 BOOST_CHECK_EQUAL(face2->getId(), face::INVALID_FACEID);
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000051 BOOST_CHECK(faceTable.get(face::INVALID_FACEID) == nullptr);
52 BOOST_CHECK_EQUAL(faceTable.size(), 0);
Junxiao Shia4f2be82014-03-02 22:56:41 -070053
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000054 faceTable.add(face1);
55 faceTable.add(face2);
56 BOOST_CHECK_EQUAL(faceTable.size(), 2);
Junxiao Shia4f2be82014-03-02 22:56:41 -070057
Junxiao Shicde37ad2015-12-24 01:02:05 -070058 BOOST_CHECK_NE(face1->getId(), face::INVALID_FACEID);
59 BOOST_CHECK_NE(face2->getId(), face::INVALID_FACEID);
Junxiao Shia4f2be82014-03-02 22:56:41 -070060 BOOST_CHECK_NE(face1->getId(), face2->getId());
Junxiao Shicde37ad2015-12-24 01:02:05 -070061 BOOST_CHECK_GT(face1->getId(), face::FACEID_RESERVED_MAX);
62 BOOST_CHECK_GT(face2->getId(), face::FACEID_RESERVED_MAX);
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000063 BOOST_CHECK(faceTable.get(face1->getId()) == face1.get());
64 BOOST_CHECK(faceTable.get(face2->getId()) == face2.get());
Junxiao Shia4f2be82014-03-02 22:56:41 -070065
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070066 FaceId oldId1 = face1->getId();
67 faceTable.add(face1);
68 BOOST_CHECK_EQUAL(face1->getId(), oldId1);
69 BOOST_CHECK_EQUAL(faceTable.size(), 2);
70
Junxiao Shicde37ad2015-12-24 01:02:05 -070071 BOOST_REQUIRE_EQUAL(addHistory.size(), 2);
72 BOOST_CHECK_EQUAL(addHistory[0], face1->getId());
73 BOOST_CHECK_EQUAL(addHistory[1], face2->getId());
Junxiao Shibd392bf2014-03-17 15:54:11 -070074
Junxiao Shic542b2b2014-03-16 21:45:52 -070075 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -070076
Junxiao Shicde37ad2015-12-24 01:02:05 -070077 BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID);
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000078 BOOST_CHECK_EQUAL(faceTable.size(), 1);
79 BOOST_CHECK(faceTable.get(oldId1) == nullptr);
Junxiao Shibd392bf2014-03-17 15:54:11 -070080
Junxiao Shicde37ad2015-12-24 01:02:05 -070081 BOOST_REQUIRE_EQUAL(removeHistory.size(), 1);
82 BOOST_CHECK_EQUAL(removeHistory[0], addHistory[0]);
Junxiao Shia4f2be82014-03-02 22:56:41 -070083}
84
Junxiao Shi7b984c62014-07-17 22:18:34 -070085BOOST_AUTO_TEST_CASE(AddReserved)
86{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000087 FaceTable faceTable;
Junxiao Shi7b984c62014-07-17 22:18:34 -070088
89 shared_ptr<Face> face1 = make_shared<DummyFace>();
Junxiao Shicde37ad2015-12-24 01:02:05 -070090 BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID);
Junxiao Shi7b984c62014-07-17 22:18:34 -070091
92 faceTable.addReserved(face1, 5);
93 BOOST_CHECK_EQUAL(face1->getId(), 5);
94}
95
Junxiao Shia4f2be82014-03-02 22:56:41 -070096BOOST_AUTO_TEST_CASE(Enumerate)
97{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000098 FaceTable faceTable;
Junxiao Shia4f2be82014-03-02 22:56:41 -070099
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) {
Junxiao Shib84e6742016-07-19 13:16:22 +0000112 hasFace1 = hasFace1 || &*it == face1.get();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700113 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000114 BOOST_CHECK_EQUAL(hasFace1, true);
Junxiao Shia4f2be82014-03-02 22:56:41 -0700115
116 faceTable.add(face2);
117 BOOST_CHECK_EQUAL(faceTable.size(), 2);
118 BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
119 hasFace1 = hasFace2 = false;
120 for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
Junxiao Shib84e6742016-07-19 13:16:22 +0000121 hasFace1 = hasFace1 || &*it == face1.get();
122 hasFace2 = hasFace2 || &*it == face2.get();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700123 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000124 BOOST_CHECK_EQUAL(hasFace1, true);
125 BOOST_CHECK_EQUAL(hasFace2, true);
Junxiao Shia4f2be82014-03-02 22:56:41 -0700126
Junxiao Shic542b2b2014-03-16 21:45:52 -0700127 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700128 BOOST_CHECK_EQUAL(faceTable.size(), 1);
129 BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
130 hasFace1 = hasFace2 = false;
131 for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
Junxiao Shib84e6742016-07-19 13:16:22 +0000132 hasFace1 = hasFace1 || &*it == face1.get();
133 hasFace2 = hasFace2 || &*it == face2.get();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700134 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000135 BOOST_CHECK_EQUAL(hasFace1, false);
136 BOOST_CHECK_EQUAL(hasFace2, true);
Junxiao Shia4f2be82014-03-02 22:56:41 -0700137}
138
Junxiao Shicde37ad2015-12-24 01:02:05 -0700139BOOST_AUTO_TEST_SUITE_END() // TestFaceTable
140BOOST_AUTO_TEST_SUITE_END() // Fw
Junxiao Shia4f2be82014-03-02 22:56:41 -0700141
142} // namespace tests
143} // namespace nfd