blob: 066b28095e383cff6a2e02c41d3ce50a2755e27a [file] [log] [blame]
Junxiao Shi49e11e72014-12-14 19:46:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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
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/>.
24 */
25
26#include "fw/strategy.hpp"
27#include "dummy-strategy.hpp"
28#include "tests/daemon/face/dummy-face.hpp"
29#include <boost/range/adaptor/filtered.hpp>
30
31#include "tests/test-common.hpp"
32
33namespace nfd {
34namespace tests {
35
36BOOST_FIXTURE_TEST_SUITE(FwStrategy, BaseFixture)
37
38class FaceTableAccessTestStrategy : public DummyStrategy
39{
40public:
41 explicit
42 FaceTableAccessTestStrategy(Forwarder& forwarder)
43 : DummyStrategy(forwarder, Name("ndn:/strategy"))
44 {
45 this->afterAddFace.connect([this] (shared_ptr<Face> face) {
46 this->addedFaces.push_back(face->getId());
47 });
48 this->beforeRemoveFace.connect([this] (shared_ptr<Face> face) {
49 this->removedFaces.push_back(face->getId());
50 });
51 }
52
53 std::vector<FaceId>
54 getLocalFaces()
55 {
56 auto enumerable = this->getFaceTable() |
57 boost::adaptors::filtered([] (shared_ptr<Face> face) {
58 return face->isLocal();
59 });
60
61 std::vector<FaceId> results;
62 for (shared_ptr<Face> face : enumerable) {
63 results.push_back(face->getId());
64 }
65 return results;
66 }
67
68public:
69 std::vector<FaceId> addedFaces;
70 std::vector<FaceId> removedFaces;
71};
72
73BOOST_AUTO_TEST_CASE(FaceTableAccess)
74{
75 Forwarder forwarder;
76 FaceTableAccessTestStrategy strategy(forwarder);
77
78 auto face1 = make_shared<DummyFace>();
79 auto face2 = make_shared<DummyLocalFace>();
80 forwarder.addFace(face1);
81 forwarder.addFace(face2);
82 FaceId id1 = face1->getId();
83 FaceId id2 = face2->getId();
84
85 BOOST_CHECK(strategy.getLocalFaces() == std::vector<FaceId>{id2});
86
87 face2->close();
88 face1->close();
89
90 BOOST_CHECK((strategy.addedFaces == std::vector<FaceId>{id1, id2}));
91 BOOST_CHECK((strategy.removedFaces == std::vector<FaceId>{id2, id1}));
92}
93
94BOOST_AUTO_TEST_SUITE_END()
95
96} // namespace tests
97} // namespace nfd