laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "tlv/routing-table-entry.hpp" |
| 23 | |
| 24 | #include "../boost-test.hpp" |
| 25 | |
| 26 | namespace nlsr { |
| 27 | namespace tlv { |
| 28 | namespace test { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TlvTestRoutingTableEntry) |
| 31 | |
| 32 | const uint8_t RoutingTableEntryWithNexthopsData[] = |
| 33 | { |
| 34 | // Header |
Ashlesh Gawande | 8df1598 | 2018-05-09 17:52:33 -0500 | [diff] [blame^] | 35 | 0x91, 0x3b, |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 36 | // Destination |
| 37 | 0x8e, 0x09, 0x07, 0x07, 0x08, 0x05, 0x64, 0x65, 0x73, 0x74, 0x31, |
| 38 | // Nexthop |
Ashlesh Gawande | 8df1598 | 2018-05-09 17:52:33 -0500 | [diff] [blame^] | 39 | 0x8f, 0x16, 0x8d, 0x08, 0x6e, 0x65, 0x78, 0x74, 0x68, 0x6f, 0x70, |
| 40 | 0x31, 0x86, 0x0a, 0x86, 0x08, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xfa, 0x3f, |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 41 | // Nexthop |
Ashlesh Gawande | 8df1598 | 2018-05-09 17:52:33 -0500 | [diff] [blame^] | 42 | 0x8f, 0x16, 0x8d, 0x08, 0x6e, 0x65, 0x78, 0x74, 0x68, 0x6f, 0x70, |
| 43 | 0x32, 0x86, 0x0a, 0x86, 0x08, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xfa, 0x3f |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | const uint8_t RoutingTableEntryWithoutNexthopsData[] = |
| 47 | { |
| 48 | // Header |
| 49 | 0x91, 0x0b, |
| 50 | // Destination |
| 51 | 0x8e, 0x09, 0x07, 0x07, 0x08, 0x05, 0x64, 0x65, 0x73, 0x74, 0x31 |
| 52 | }; |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(RoutingTableEntryEncodeWithNexthops) |
| 55 | { |
| 56 | RoutingTable rt; |
| 57 | |
| 58 | Destination des1; |
| 59 | des1.setName("dest1"); |
| 60 | rt.setDestination(des1); |
| 61 | |
| 62 | NextHop nexthops1; |
| 63 | nexthops1.setUri("nexthop1"); |
| 64 | nexthops1.setCost(1.65); |
| 65 | rt.addNexthops(nexthops1); |
| 66 | |
| 67 | NextHop nexthops2; |
| 68 | nexthops2.setUri("nexthop2"); |
| 69 | nexthops2.setCost(1.65); |
| 70 | rt.addNexthops(nexthops2); |
| 71 | |
| 72 | const ndn::Block& wire = rt.wireEncode(); |
| 73 | |
| 74 | BOOST_REQUIRE_EQUAL_COLLECTIONS(RoutingTableEntryWithNexthopsData, |
| 75 | RoutingTableEntryWithNexthopsData + |
| 76 | sizeof(RoutingTableEntryWithNexthopsData), |
| 77 | wire.begin(), wire.end()); |
| 78 | } |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(RoutingTableEntryDecodeWithNexthops) |
| 81 | { |
| 82 | RoutingTable rt; |
| 83 | |
| 84 | rt.wireDecode(ndn::Block(RoutingTableEntryWithNexthopsData, |
| 85 | sizeof(RoutingTableEntryWithNexthopsData))); |
| 86 | |
| 87 | Destination des = rt.getDestination(); |
| 88 | BOOST_CHECK_EQUAL(des.getName(), "dest1"); |
| 89 | |
| 90 | BOOST_CHECK_EQUAL(rt.hasNexthops(), true); |
| 91 | std::list<NextHop> nexthops = rt.getNextHops(); |
| 92 | std::list<NextHop>::const_iterator it = nexthops.begin(); |
| 93 | BOOST_CHECK_EQUAL(it->getUri(), "nexthop1"); |
| 94 | BOOST_CHECK_EQUAL(it->getCost(), 1.65); |
| 95 | |
| 96 | it++; |
| 97 | BOOST_CHECK_EQUAL(it->getUri(), "nexthop2"); |
| 98 | BOOST_CHECK_EQUAL(it->getCost(), 1.65); |
| 99 | } |
| 100 | |
| 101 | BOOST_AUTO_TEST_CASE(RoutingTableEntryEncodeWithoutNexthops) |
| 102 | { |
| 103 | RoutingTable rt; |
| 104 | |
| 105 | Destination des1; |
| 106 | des1.setName("dest1"); |
| 107 | rt.setDestination(des1); |
| 108 | |
| 109 | const ndn::Block& wire = rt.wireEncode(); |
| 110 | |
| 111 | BOOST_REQUIRE_EQUAL_COLLECTIONS(RoutingTableEntryWithoutNexthopsData, |
| 112 | RoutingTableEntryWithoutNexthopsData + |
| 113 | sizeof(RoutingTableEntryWithoutNexthopsData), |
| 114 | wire.begin(), wire.end()); |
| 115 | } |
| 116 | |
| 117 | BOOST_AUTO_TEST_CASE(RoutingTableEntryDecodeWithoutNexthops) |
| 118 | { |
| 119 | RoutingTable rt; |
| 120 | |
| 121 | rt.wireDecode(ndn::Block(RoutingTableEntryWithoutNexthopsData, |
| 122 | sizeof(RoutingTableEntryWithoutNexthopsData))); |
| 123 | |
| 124 | Destination des = rt.getDestination(); |
| 125 | BOOST_CHECK_EQUAL(des.getName(), "dest1"); |
| 126 | |
| 127 | BOOST_CHECK_EQUAL(rt.hasNexthops(), false); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(RoutingTableEntryClear) |
| 132 | { |
| 133 | RoutingTable rt; |
| 134 | Destination des1; |
| 135 | des1.setName("dest1"); |
| 136 | rt.setDestination(des1); |
| 137 | |
| 138 | NextHop nexthops1; |
| 139 | nexthops1.setUri("nexthop1"); |
| 140 | nexthops1.setCost(99); |
| 141 | rt.addNexthops(nexthops1); |
| 142 | |
| 143 | BOOST_CHECK_EQUAL(rt.getNextHops().size(), 1); |
| 144 | |
| 145 | std::list<NextHop> nexthops = rt.getNextHops(); |
| 146 | std::list<NextHop>::const_iterator it = nexthops.begin(); |
| 147 | BOOST_CHECK_EQUAL(it->getUri(), "nexthop1"); |
| 148 | BOOST_CHECK_EQUAL(it->getCost(), 99); |
| 149 | |
| 150 | rt.clearNexthops(); |
| 151 | BOOST_CHECK_EQUAL(rt.getNextHops().size(), 0); |
| 152 | |
| 153 | NextHop nexthops2; |
| 154 | nexthops2.setUri("nexthop2"); |
| 155 | nexthops2.setCost(99); |
| 156 | rt.addNexthops(nexthops2); |
| 157 | |
| 158 | BOOST_CHECK_EQUAL(rt.getNextHops().size(), 1); |
| 159 | |
| 160 | nexthops = rt.getNextHops(); |
| 161 | it = nexthops.begin(); |
| 162 | BOOST_CHECK_EQUAL(it->getUri(), "nexthop2"); |
| 163 | BOOST_CHECK_EQUAL(it->getCost(), 99); |
| 164 | } |
| 165 | |
| 166 | BOOST_AUTO_TEST_CASE(RoutingTableEntryOutputStream) |
| 167 | { |
| 168 | RoutingTable rt; |
| 169 | Destination des1; |
| 170 | des1.setName("dest1"); |
| 171 | rt.setDestination(des1); |
| 172 | |
| 173 | NextHop nexthops1; |
| 174 | nexthops1.setUri("nexthop1"); |
| 175 | nexthops1.setCost(99); |
| 176 | rt.addNexthops(nexthops1); |
| 177 | |
| 178 | std::ostringstream os; |
| 179 | os << rt; |
| 180 | |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 181 | BOOST_CHECK_EQUAL(os.str(), |
| 182 | "Destination: /dest1\n" |
| 183 | "NexthopList(\n" |
| 184 | "NextHop(Uri: nexthop1, Cost: 99)\n" |
| 185 | ")"); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_SUITE_END() |
| 189 | |
| 190 | } // namespace test |
| 191 | } // namespace tlv |
| 192 | } // namespace nlsr |