Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 1 | /* -*- 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/internal-face.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 8 | #include "mgmt/fib-manager.hpp" |
| 9 | #include "table/fib.hpp" |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 10 | #include "../face/dummy-face.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 11 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 12 | #include <ndn-cpp-dev/management/fib-management-options.hpp> |
| 13 | #include <ndn-cpp-dev/management/control-response.hpp> |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 14 | |
| 15 | #include <boost/test/unit_test.hpp> |
| 16 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 17 | static nfd::FaceId g_faceCount = 1; |
| 18 | static std::vector<nfd::shared_ptr<nfd::Face> > g_faces; |
| 19 | |
| 20 | static nfd::shared_ptr<nfd::Face> |
| 21 | getFace(nfd::FaceId id) |
| 22 | { |
| 23 | if (g_faces.size() < id) |
| 24 | { |
| 25 | BOOST_FAIL("Attempted to access invalid FaceId: " << id); |
| 26 | } |
| 27 | return g_faces[id-1]; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 32 | namespace nfd { |
| 33 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 34 | NFD_LOG_INIT("InternalFaceTest"); |
| 35 | |
| 36 | void |
| 37 | receiveValidNextHopControlResponse(const ndn::Data& response) |
| 38 | { |
| 39 | // Path 1 - runtime exception on wireDecode. |
| 40 | // Extract Block from response's payload and attempt |
| 41 | // to decode. |
| 42 | // { |
| 43 | // ndn::ControlResponse control; |
| 44 | // Block controlRaw(response.getContent()); |
| 45 | |
| 46 | // NFD_LOG_DEBUG("received raw control block size = " << controlRaw.size()); |
| 47 | |
| 48 | // control.wireDecode(controlRaw); |
| 49 | |
| 50 | // NFD_LOG_DEBUG("received control response (Path 1)" |
| 51 | // << " Name: " << response.getName() |
| 52 | // << " code: " << control.getCode() |
| 53 | // << " text: " << control.getText()); |
| 54 | |
| 55 | // BOOST_REQUIRE(control.getCode() == 200); |
| 56 | // BOOST_REQUIRE(control.getText() == "OK"); |
| 57 | // } |
| 58 | |
| 59 | // Path 1.5 - same as Path 1, but offset the payload's |
| 60 | // encoded block by 2 bytes before decoding. 2 bytes |
| 61 | // is the measured Block size difference between |
| 62 | // ManagerBase's sendResponse and above Path 1's |
| 63 | // received size. |
| 64 | { |
| 65 | ndn::ControlResponse control; |
| 66 | Block controlRaw(response.getContent()); |
| 67 | |
| 68 | NFD_LOG_DEBUG("received raw control block size = " << controlRaw.size()); |
| 69 | // controlRaw is currently 2 bytes larger than what was sent |
| 70 | // try to offset it manually and create a new block |
| 71 | |
| 72 | BOOST_REQUIRE(controlRaw.hasWire()); |
| 73 | const uint8_t* buf = controlRaw.wire() + 2; |
| 74 | size_t bufSize = controlRaw.size() - 2; |
| 75 | |
| 76 | Block alt(buf, bufSize); |
| 77 | control.wireDecode(alt); |
| 78 | |
| 79 | NFD_LOG_DEBUG("received control response (Path 1)" |
| 80 | << " Name: " << response.getName() |
| 81 | << " code: " << control.getCode() |
| 82 | << " text: " << control.getText()); |
| 83 | |
| 84 | BOOST_REQUIRE(control.getCode() == 200); |
| 85 | BOOST_REQUIRE(control.getText() == "OK"); |
| 86 | } |
| 87 | |
| 88 | // Path 2 - works, but not conformant to protocol. |
| 89 | // Extract decode and ControlResponse from last |
| 90 | // component of response's name. |
| 91 | // { |
| 92 | // const Name& responseName = response.getName(); |
| 93 | // const ndn::Buffer& controlBuffer = |
| 94 | // responseName[responseName.size()-1].getValue(); |
| 95 | |
| 96 | // shared_ptr<const ndn::Buffer> tmpBuffer(new ndn::Buffer(controlBuffer)); |
| 97 | // Block controlRaw(tmpBuffer); |
| 98 | |
| 99 | // NFD_LOG_DEBUG("received raw control block size = " << controlRaw.size()); |
| 100 | |
| 101 | // ndn::ControlResponse control; |
| 102 | // control.wireDecode(controlRaw); |
| 103 | |
| 104 | // NFD_LOG_DEBUG("received control response (Path 2)" |
| 105 | // << " Name: " << response.getName() |
| 106 | // << " code: " << control.getCode() |
| 107 | // << " text: " << control.getText()); |
| 108 | |
| 109 | // BOOST_REQUIRE(control.getCode() == 200); |
| 110 | // BOOST_REQUIRE(control.getText() == "OK"); |
| 111 | // } |
| 112 | |
| 113 | |
| 114 | } |
| 115 | |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 116 | BOOST_AUTO_TEST_SUITE(MgmtInternalFace) |
| 117 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 118 | BOOST_AUTO_TEST_CASE(ValidAddNextHop) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 119 | { |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 120 | g_faceCount = 1; |
| 121 | g_faces.clear(); |
| 122 | g_faces.push_back(make_shared<DummyFace>()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 123 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 124 | shared_ptr<InternalFace> face(new InternalFace); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 125 | Fib fib; |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 126 | FibManager manager(fib, &getFace, face); |
| 127 | |
| 128 | face->setInterestFilter(manager.getRequestPrefix(), |
| 129 | bind(&FibManager::onFibRequest, |
| 130 | &manager, _2)); |
| 131 | |
| 132 | face->onReceiveData += |
| 133 | bind(&receiveValidNextHopControlResponse, _1); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 134 | |
| 135 | Name regName(manager.getRequestPrefix()); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 136 | ndn::FibManagementOptions options; |
| 137 | options.setName("/hello"); |
| 138 | options.setFaceId(1); |
| 139 | options.setCost(1); |
| 140 | |
| 141 | Block encodedOptions(options.wireEncode()); |
| 142 | |
| 143 | Name commandName(manager.getRequestPrefix()); |
| 144 | commandName.append("add-nexthop"); |
| 145 | commandName.append(encodedOptions); |
| 146 | |
| 147 | Interest command(commandName); |
| 148 | face->sendInterest(command); |
| 149 | |
| 150 | shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello"); |
| 151 | |
| 152 | if (entry) |
| 153 | { |
| 154 | const fib::NextHopList& hops = entry->getNextHops(); |
| 155 | BOOST_REQUIRE(hops.size() == 1); |
| 156 | BOOST_CHECK(hops[0].getCost() == 1); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | BOOST_FAIL("Failed to find expected fib entry"); |
| 161 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | BOOST_AUTO_TEST_CASE(InvalidPrefixRegistration) |
| 165 | { |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 166 | g_faceCount = 1; |
| 167 | g_faces.clear(); |
| 168 | g_faces.push_back(make_shared<DummyFace>()); |
| 169 | |
| 170 | shared_ptr<InternalFace> face(new InternalFace); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 171 | Fib fib; |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame^] | 172 | FibManager manager(fib, &getFace, face); |
| 173 | |
| 174 | face->setInterestFilter(manager.getRequestPrefix(), |
| 175 | bind(&FibManager::onFibRequest, |
| 176 | &manager, _2)); |
| 177 | |
| 178 | Interest nonRegInterest("/hello"); |
| 179 | face->sendInterest(nonRegInterest); |
| 180 | |
| 181 | shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello"); |
| 182 | |
| 183 | if (entry) |
| 184 | { |
| 185 | const fib::NextHopList& hops = entry->getNextHops(); |
| 186 | BOOST_REQUIRE(hops.size() == 0); |
| 187 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_SUITE_END() |
| 191 | |
| 192 | } // namespace nfd |