Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "management/nfd-fib-entry.hpp" |
| 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace nfd { |
| 28 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 29 | BOOST_AUTO_TEST_SUITE(ManagementTestNfdFibEntry) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 30 | |
| 31 | const uint8_t TestNextHopRecord[] = |
| 32 | { |
| 33 | 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8 |
| 34 | }; |
| 35 | |
| 36 | const uint8_t TestFibEntryNoNextHops[] = |
| 37 | { |
| 38 | 0x80, 0x15, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, |
| 39 | 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, 0x61, 0x08, 0x04, 0x74, |
| 40 | 0x65, 0x73, 0x74 |
| 41 | }; |
| 42 | |
| 43 | const uint8_t TestFibEntry[] = |
| 44 | { |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 45 | 0x80, 0x38, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, |
| 46 | 0x61, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8, 0x81, |
| 47 | 0x07, 0x69, 0x01, 0x14, 0x6a, 0x02, 0x01, 0x2c, 0x81, 0x07, 0x69, 0x01, 0x1e, 0x6a, 0x02, 0x01, |
| 48 | 0x90, 0x81, 0x07, 0x69, 0x01, 0x28, 0x6a, 0x02, 0x01, 0xf4 |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE(TestNextHopRecordEncode) |
| 52 | { |
| 53 | NextHopRecord record; |
| 54 | record.setFaceId(10); |
| 55 | record.setCost(200); |
| 56 | |
| 57 | const Block& wire = record.wireEncode(); |
| 58 | BOOST_REQUIRE_EQUAL_COLLECTIONS(TestNextHopRecord, |
| 59 | TestNextHopRecord + sizeof(TestNextHopRecord), |
| 60 | wire.begin(), wire.end()); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(TestNextHopRecordDecode) |
| 65 | { |
| 66 | NextHopRecord record; |
| 67 | |
| 68 | BOOST_REQUIRE_NO_THROW(record.wireDecode(Block(TestNextHopRecord, |
| 69 | sizeof(TestNextHopRecord)))); |
| 70 | BOOST_REQUIRE_EQUAL(record.getFaceId(), 10); |
| 71 | BOOST_REQUIRE_EQUAL(record.getCost(), 200); |
| 72 | } |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopEncode) |
| 75 | { |
| 76 | FibEntry entry; |
| 77 | entry.setPrefix("/this/is/a/test"); |
| 78 | |
| 79 | const Block& wire = entry.wireEncode(); |
| 80 | BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibEntryNoNextHops, |
| 81 | TestFibEntryNoNextHops + sizeof(TestFibEntryNoNextHops), |
| 82 | wire.begin(), wire.end()); |
| 83 | } |
| 84 | |
| 85 | BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopsDecode) |
| 86 | { |
| 87 | FibEntry entry; |
| 88 | BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntryNoNextHops, |
| 89 | sizeof(TestFibEntryNoNextHops)))); |
| 90 | |
| 91 | BOOST_REQUIRE_EQUAL(entry.getPrefix(), "/this/is/a/test"); |
| 92 | BOOST_REQUIRE(entry.getNextHopRecords().empty()); |
| 93 | } |
| 94 | |
| 95 | BOOST_AUTO_TEST_CASE(TestFibEntryEncode) |
| 96 | { |
| 97 | FibEntry entry; |
| 98 | entry.setPrefix("/this/is/a/test"); |
| 99 | |
| 100 | std::list<NextHopRecord> records; |
| 101 | |
| 102 | for (int i = 1; i < 4; i++) |
| 103 | { |
| 104 | NextHopRecord record; |
| 105 | record.setFaceId(i * 10); |
| 106 | record.setCost((i * 100) + 100); |
| 107 | records.push_back(record); |
| 108 | } |
| 109 | |
| 110 | entry.setNextHopRecords(records.begin(), records.end()); |
| 111 | |
| 112 | NextHopRecord oneMore; |
| 113 | oneMore.setFaceId(40); |
| 114 | oneMore.setCost(500); |
| 115 | |
| 116 | entry.addNextHopRecord(oneMore); |
| 117 | |
| 118 | const Block& wire = entry.wireEncode(); |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 119 | BOOST_CHECK_EQUAL_COLLECTIONS(TestFibEntry, |
| 120 | TestFibEntry + sizeof(TestFibEntry), |
| 121 | wire.begin(), wire.end()); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 122 | |
| 123 | // std::ofstream of("out.tmp"); |
| 124 | // of.write((const char*)entry.wireEncode().wire(), |
| 125 | // entry.wireEncode().size()); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(TestFibEntryDecode) |
| 129 | { |
| 130 | FibEntry entry; |
| 131 | BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntry, |
| 132 | sizeof(TestFibEntry)))); |
| 133 | |
| 134 | std::list<NextHopRecord> records = entry.getNextHopRecords(); |
| 135 | |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(entry.getPrefix(), "/this/is/a/test"); |
| 137 | BOOST_CHECK_EQUAL(entry.getNextHopRecords().size(), 4); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 138 | |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 139 | size_t value = 1; |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 140 | |
| 141 | for (std::list<NextHopRecord>::const_iterator i = records.begin(); |
| 142 | i != records.end(); |
| 143 | ++i) |
| 144 | { |
| 145 | BOOST_CHECK_EQUAL(i->getFaceId(), value * 10); |
| 146 | BOOST_CHECK_EQUAL(i->getCost(), (value * 100) + 100); |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 147 | ++value; |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_SUITE_END() |
| 152 | |
| 153 | } // namespace nfd |
| 154 | } // namespace ndn |