blob: bbcce4b02283bc241f6383ff07a1ffb3040b2757 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "mgmt/fib-manager.hpp"
8#include "fw/forwarder.hpp"
9#include "table/fib.hpp"
10#include "face/face.hpp"
11#include "../face/dummy-face.hpp"
12
13#include <ndn-cpp-dev/management/fib-management-options.hpp>
14
15#include <boost/test/unit_test.hpp>
16
17namespace nfd {
18
19NFD_LOG_INIT("FibManagerTest");
20
21FaceId g_faceCount = 1;
22std::vector<shared_ptr<Face> > g_faces;
23
24shared_ptr<Face>
25getFace(FaceId id)
26{
27 if (g_faces.size() < id)
28 {
29 BOOST_FAIL("Attempted to access invalid FaceId: " << id);
30 }
31 return g_faces[id-1];
32}
33
34BOOST_AUTO_TEST_SUITE(MgmtFibManager)
35
36BOOST_AUTO_TEST_CASE(MalformedCommmand)
37{
38 Fib fib;
39 FibManager manager(fib, &getFace);
40
41 Interest command(manager.getRequestPrefix());
42 manager.onFibRequest(command);
43}
44
45BOOST_AUTO_TEST_CASE(UnsupportedVerb)
46{
47 Fib fib;
48 FibManager manager(fib, &getFace);
49
50 Name commandName(manager.getRequestPrefix());
51 commandName.append("unsupported");
52
53 Interest command(commandName);
54 manager.onFibRequest(command);
55}
56
57BOOST_AUTO_TEST_CASE(AddNextHopVerbInitialAdd)
58{
59 g_faceCount = 1;
60 g_faces.clear();
61 g_faces.push_back(make_shared<DummyFace>());
62
63 Fib fib;
64
65 FibManager manager(fib, &getFace);
66
67 ndn::FibManagementOptions options;
68 options.setName("/hello");
69 options.setFaceId(1);
70 options.setCost(1);
71
72 Block encodedOptions(options.wireEncode());
73
74 Name commandName(manager.getRequestPrefix());
75 commandName.append("add-nexthop");
76 commandName.append(encodedOptions);
77
78 Interest command(commandName);
79 manager.onFibRequest(command);
80
81 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
82
83 if (entry)
84 {
85 const fib::NextHopList& hops = entry->getNextHops();
86 BOOST_REQUIRE(hops.size() == 1);
87 // BOOST_CHECK(hops[0].getFace()->getFaceId() == 1);
88 BOOST_CHECK(hops[0].getCost() == 1);
89 }
90 else
91 {
92 BOOST_FAIL("Failed to find expected fib entry");
93 }
94}
95
96BOOST_AUTO_TEST_CASE(AddNextHopVerbAddToExisting)
97{
98 g_faceCount = 1;
99 g_faces.clear();
100 g_faces.push_back(make_shared<DummyFace>());
101 g_faces.push_back(make_shared<DummyFace>());
102
103 Fib fib;
104
105 FibManager manager(fib, &getFace);
106
107 // Add faces with cost == FaceID for the name /hello
108 // This test assumes:
109 // FaceIDs are assigned from 1 to N
110 // Faces are store sequentially in the NextHopList
111 // NextHopList supports random access
112
113 for (int i = 1; i <= 2; i++)
114 {
115 ndn::FibManagementOptions options;
116 options.setName("/hello");
117 options.setFaceId(i);
118 options.setCost(i);
119
120 Block encodedOptions(options.wireEncode());
121
122 Name commandName(manager.getRequestPrefix());
123 commandName.append("add-nexthop");
124 commandName.append(encodedOptions);
125
126 Interest command(commandName);
127 manager.onFibRequest(command);
128
129 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
130
131 if (entry)
132 {
133 const fib::NextHopList& hops = entry->getNextHops();
134 for (int j = 1; j <= i; j++)
135 {
136 BOOST_REQUIRE(hops.size() == i);
137 // BOOST_CHECK(hops[j-1].getFace()->getFaceId() == j);
138 BOOST_CHECK(hops[j-1].getCost() == j);
139 }
140 }
141 else
142 {
143 BOOST_FAIL("Failed to find expected fib entry");
144 }
145 }
146}
147
148BOOST_AUTO_TEST_CASE(AddNextHopVerbUpdateFaceCost)
149{
150 g_faceCount = 1;
151 g_faces.clear();
152 g_faces.push_back(make_shared<DummyFace>());
153
154 Fib fib;
155
156 FibManager manager(fib, &getFace);
157
158 ndn::FibManagementOptions options;
159 options.setName("/hello");
160 options.setFaceId(1);
161
162 {
163 options.setCost(1);
164
165 Block encodedOptions(options.wireEncode());
166
167 Name commandName(manager.getRequestPrefix());
168 commandName.append("add-nexthop");
169 commandName.append(encodedOptions);
170
171 Interest command(commandName);
172 manager.onFibRequest(command);
173 }
174
175 {
176 options.setCost(2);
177
178 Block encodedOptions(options.wireEncode());
179
180 Name commandName(manager.getRequestPrefix());
181 commandName.append("add-nexthop");
182 commandName.append(encodedOptions);
183
184 Interest command(commandName);
185 manager.onFibRequest(command);
186 }
187
188 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
189
190 // Add faces with cost == FaceID for the name /hello
191 // This test assumes:
192 // FaceIDs are assigned from 1 to N
193 // Faces are store sequentially in the NextHopList
194 // NextHopList supports random access
195 if (entry)
196 {
197 const fib::NextHopList& hops = entry->getNextHops();
198 BOOST_REQUIRE(hops.size() == 1);
199 // BOOST_CHECK(hops[0].getFace()->getFaceId() == 1);
200 BOOST_CHECK(hops[0].getCost() == 2);
201 }
202 else
203 {
204 BOOST_FAIL("Failed to find expected fib entry");
205 }
206}
207
208BOOST_AUTO_TEST_SUITE_END()
209
210} // namespace nfd