Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, The University of Memphis, |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 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/adjacency-lsa.hpp" |
| 23 | |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame^] | 24 | #include "tests/boost-test.hpp" |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 25 | |
| 26 | namespace nlsr { |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 27 | namespace tlv { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 28 | namespace test { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TlvTestAdjacencyLsa) |
| 31 | |
| 32 | const uint8_t AdjacencyLsaWithAdjacenciesData[] = |
| 33 | { |
| 34 | // Header |
| 35 | 0x83, 0x3d, |
| 36 | // LsaInfo |
| 37 | 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01, |
| 38 | 0x80, 0x8b, 0x02, 0x27, 0x10, |
| 39 | // Adjacency |
| 40 | 0x84, 0x13, 0x07, 0x07, 0x08, 0x05, 0x74, 0x65, 0x73, 0x74, 0x31, 0x8d, 0x05, 0x74, |
| 41 | 0x65, 0x73, 0x74, 0x31, 0x8c, 0x01, 0x80, |
| 42 | // Adjacency |
| 43 | 0x84, 0x13, 0x07, 0x07, 0x08, 0x05, 0x74, 0x65, 0x73, 0x74, 0x32, 0x8d, 0x05, 0x74, |
| 44 | 0x65, 0x73, 0x74, 0x32, 0x8c, 0x01, 0x80 |
| 45 | }; |
| 46 | |
| 47 | const uint8_t AdjacencyLsaWithoutAdjacenciesData[] = |
| 48 | { |
| 49 | // Header |
| 50 | 0x83, 0x13, |
| 51 | // LsaInfo |
| 52 | 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01, |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 53 | 0x80, 0x8b, 0x02, 0x27, 0x10 |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(AdjacencyLsaEncodeWithAdjacencies) |
| 57 | { |
| 58 | AdjacencyLsa adjacencyLsa; |
| 59 | |
| 60 | LsaInfo lsaInfo; |
| 61 | lsaInfo.setOriginRouter("test"); |
| 62 | lsaInfo.setSequenceNumber(128); |
| 63 | lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000)); |
| 64 | adjacencyLsa.setLsaInfo(lsaInfo); |
| 65 | |
| 66 | Adjacency adjacency1; |
| 67 | adjacency1.setName("test1"); |
| 68 | adjacency1.setUri("test1"); |
| 69 | adjacency1.setCost(128); |
| 70 | adjacencyLsa.addAdjacency(adjacency1); |
| 71 | |
| 72 | Adjacency adjacency2; |
| 73 | adjacency2.setName("test2"); |
| 74 | adjacency2.setUri("test2"); |
| 75 | adjacency2.setCost(128); |
| 76 | adjacencyLsa.addAdjacency(adjacency2); |
| 77 | |
| 78 | const ndn::Block& wire = adjacencyLsa.wireEncode(); |
| 79 | |
| 80 | BOOST_REQUIRE_EQUAL_COLLECTIONS(AdjacencyLsaWithAdjacenciesData, |
| 81 | AdjacencyLsaWithAdjacenciesData + |
| 82 | sizeof(AdjacencyLsaWithAdjacenciesData), |
| 83 | wire.begin(), wire.end()); |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(AdjacencyLsaDecodeWithAdjacencies) |
| 87 | { |
| 88 | AdjacencyLsa adjacencyLsa; |
| 89 | |
| 90 | adjacencyLsa.wireDecode(ndn::Block(AdjacencyLsaWithAdjacenciesData, |
| 91 | sizeof(AdjacencyLsaWithAdjacenciesData))); |
| 92 | |
| 93 | LsaInfo lsaInfo = adjacencyLsa.getLsaInfo(); |
| 94 | BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test"); |
| 95 | BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128); |
| 96 | BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000)); |
| 97 | |
| 98 | BOOST_CHECK_EQUAL(adjacencyLsa.hasAdjacencies(), true); |
| 99 | std::list<Adjacency> adjacencies = adjacencyLsa.getAdjacencies(); |
| 100 | std::list<Adjacency>::const_iterator it = adjacencies.begin(); |
| 101 | BOOST_CHECK_EQUAL(it->getName(), "test1"); |
| 102 | BOOST_CHECK_EQUAL(it->getUri(), "test1"); |
| 103 | BOOST_CHECK_EQUAL(it->getCost(), 128); |
| 104 | |
| 105 | it++; |
| 106 | BOOST_CHECK_EQUAL(it->getName(), "test2"); |
| 107 | BOOST_CHECK_EQUAL(it->getUri(), "test2"); |
| 108 | BOOST_CHECK_EQUAL(it->getCost(), 128); |
| 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_CASE(AdjacencyLsaEncodeWithoutAdjacencies) |
| 112 | { |
| 113 | AdjacencyLsa adjacencyLsa; |
| 114 | |
| 115 | LsaInfo lsaInfo; |
| 116 | lsaInfo.setOriginRouter("test"); |
| 117 | lsaInfo.setSequenceNumber(128); |
| 118 | lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000)); |
| 119 | adjacencyLsa.setLsaInfo(lsaInfo); |
| 120 | |
| 121 | const ndn::Block& wire = adjacencyLsa.wireEncode(); |
| 122 | |
| 123 | BOOST_REQUIRE_EQUAL_COLLECTIONS(AdjacencyLsaWithoutAdjacenciesData, |
| 124 | AdjacencyLsaWithoutAdjacenciesData + |
| 125 | sizeof(AdjacencyLsaWithoutAdjacenciesData), |
| 126 | wire.begin(), wire.end()); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(AdjacencyLsaDecodeWithoutAdjacencies) |
| 130 | { |
| 131 | AdjacencyLsa adjacencyLsa; |
| 132 | |
| 133 | adjacencyLsa.wireDecode(ndn::Block(AdjacencyLsaWithoutAdjacenciesData, |
| 134 | sizeof(AdjacencyLsaWithoutAdjacenciesData))); |
| 135 | |
| 136 | LsaInfo lsaInfo = adjacencyLsa.getLsaInfo(); |
| 137 | BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test"); |
| 138 | BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128); |
| 139 | BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000)); |
| 140 | |
| 141 | BOOST_CHECK_EQUAL(adjacencyLsa.hasAdjacencies(), false); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | BOOST_AUTO_TEST_CASE(AdjacencyLsaClear) |
| 146 | { |
| 147 | AdjacencyLsa adjacencyLsa; |
| 148 | |
| 149 | LsaInfo lsaInfo; |
| 150 | lsaInfo.setOriginRouter("test"); |
| 151 | lsaInfo.setSequenceNumber(128); |
| 152 | lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000)); |
| 153 | adjacencyLsa.setLsaInfo(lsaInfo); |
| 154 | |
| 155 | Adjacency adjacency1; |
| 156 | adjacency1.setName("test1"); |
| 157 | adjacency1.setUri("test1"); |
| 158 | adjacency1.setCost(128); |
| 159 | adjacencyLsa.addAdjacency(adjacency1); |
| 160 | BOOST_CHECK_EQUAL(adjacencyLsa.getAdjacencies().size(), 1); |
| 161 | |
| 162 | std::list<Adjacency> adjacencies = adjacencyLsa.getAdjacencies(); |
| 163 | std::list<Adjacency>::const_iterator it = adjacencies.begin(); |
| 164 | BOOST_CHECK_EQUAL(it->getName(), "test1"); |
| 165 | BOOST_CHECK_EQUAL(it->getUri(), "test1"); |
| 166 | BOOST_CHECK_EQUAL(it->getCost(), 128); |
| 167 | |
| 168 | adjacencyLsa.clearAdjacencies(); |
| 169 | BOOST_CHECK_EQUAL(adjacencyLsa.getAdjacencies().size(), 0); |
| 170 | |
| 171 | Adjacency adjacency2; |
| 172 | adjacency2.setName("test2"); |
| 173 | adjacency2.setUri("test2"); |
| 174 | adjacency2.setCost(128); |
| 175 | adjacencyLsa.addAdjacency(adjacency2); |
| 176 | BOOST_CHECK_EQUAL(adjacencyLsa.getAdjacencies().size(), 1); |
| 177 | |
| 178 | adjacencies = adjacencyLsa.getAdjacencies(); |
| 179 | it = adjacencies.begin(); |
| 180 | BOOST_CHECK_EQUAL(it->getName(), "test2"); |
| 181 | BOOST_CHECK_EQUAL(it->getUri(), "test2"); |
| 182 | BOOST_CHECK_EQUAL(it->getCost(), 128); |
| 183 | } |
| 184 | |
| 185 | BOOST_AUTO_TEST_CASE(AdjacencyLsaOutputStream) |
| 186 | { |
| 187 | AdjacencyLsa adjacencyLsa; |
| 188 | |
| 189 | LsaInfo lsaInfo; |
| 190 | lsaInfo.setOriginRouter("test"); |
| 191 | lsaInfo.setSequenceNumber(128); |
| 192 | lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000)); |
| 193 | adjacencyLsa.setLsaInfo(lsaInfo); |
| 194 | |
| 195 | Adjacency adjacency1; |
| 196 | adjacency1.setName("test1"); |
| 197 | adjacency1.setUri("test1"); |
| 198 | adjacency1.setCost(128); |
| 199 | adjacencyLsa.addAdjacency(adjacency1); |
| 200 | |
| 201 | Adjacency adjacency2; |
| 202 | adjacency2.setName("test2"); |
| 203 | adjacency2.setUri("test2"); |
| 204 | adjacency2.setCost(128); |
| 205 | adjacencyLsa.addAdjacency(adjacency2); |
| 206 | |
| 207 | std::ostringstream os; |
| 208 | os << adjacencyLsa; |
| 209 | |
| 210 | BOOST_CHECK_EQUAL(os.str(), "AdjacencyLsa(" |
| 211 | "LsaInfo(" |
| 212 | "OriginRouter: /test, " |
| 213 | "SequenceNumber: 128, " |
| 214 | "ExpirationPeriod: 10000 milliseconds), " |
| 215 | "Adjacency(Name: /test1, Uri: test1, Cost: 128), " |
| 216 | "Adjacency(Name: /test2, Uri: test2, Cost: 128))"); |
| 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_SUITE_END() |
| 220 | |
| 221 | } // namespace test |
| 222 | } // namespace tlv |
| 223 | } // namespace nlsr |