akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 21 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 22 | #include "lsa.hpp" |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame^] | 23 | #include "test-common.hpp" |
| 24 | #include "adjacent.hpp" |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 25 | #include "name-prefix-list.hpp" |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 26 | |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 27 | #include <ndn-cxx/util/time.hpp> |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 28 | #include <sstream> |
| 29 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 30 | namespace nlsr { |
| 31 | namespace test { |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 32 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(TestLsa) |
| 34 | |
| 35 | BOOST_AUTO_TEST_CASE(NameLsaBasic) |
| 36 | { |
| 37 | NamePrefixList npl1; |
| 38 | |
| 39 | std::string s1 = "name1"; |
| 40 | std::string s2 = "name2"; |
| 41 | |
| 42 | npl1.insert(s1); |
| 43 | npl1.insert(s2); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 44 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 45 | |
| 46 | //3rd arg is seqNo. which will be a random number I just put in 12. |
| 47 | NameLsa nlsa1("router1", 12, testTimePoint, npl1); |
| 48 | NameLsa nlsa2("router2", 12, testTimePoint, npl1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 49 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(nlsa1.getLsType(), NameLsa::TYPE_STRING); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 51 | |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 52 | BOOST_CHECK(nlsa1.getExpirationTimePoint() == nlsa2.getExpirationTimePoint()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 53 | |
| 54 | BOOST_CHECK(nlsa1.getKey() != nlsa2.getKey()); |
| 55 | } |
| 56 | |
| 57 | BOOST_AUTO_TEST_CASE(AdjacentLsaConstructorAndGetters) |
| 58 | { |
alvy | c69c9af | 2015-04-09 12:38:54 -0500 | [diff] [blame] | 59 | ndn::Name routerName("/ndn/site/router"); |
| 60 | ndn::Name adjacencyName("/ndn/site/adjacency"); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 61 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
alvy | c69c9af | 2015-04-09 12:38:54 -0500 | [diff] [blame] | 62 | uint32_t seqNo = 12; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 63 | |
alvy | c69c9af | 2015-04-09 12:38:54 -0500 | [diff] [blame] | 64 | // An AdjLsa initialized with ACTIVE adjacencies should copy the adjacencies |
| 65 | AdjacencyList activeAdjacencies; |
| 66 | Adjacent activeAdjacency(adjacencyName); |
| 67 | activeAdjacency.setStatus(Adjacent::STATUS_ACTIVE); |
| 68 | activeAdjacencies.insert(activeAdjacency); |
| 69 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 70 | AdjLsa alsa1(routerName, seqNo, testTimePoint, |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame^] | 71 | activeAdjacencies.size(), activeAdjacencies); |
| 72 | BOOST_CHECK_EQUAL(alsa1.getAdl().size(), 1); |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(alsa1.getLsType(), AdjLsa::TYPE_STRING); |
alvy | c69c9af | 2015-04-09 12:38:54 -0500 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(alsa1.getLsSeqNo(), seqNo); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(alsa1.getExpirationTimePoint(), testTimePoint); |
alvy | c69c9af | 2015-04-09 12:38:54 -0500 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(alsa1.getNoLink(), 1); |
| 77 | BOOST_CHECK(alsa1.getAdl().isNeighbor(activeAdjacency.getName())); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 78 | |
alvy | c69c9af | 2015-04-09 12:38:54 -0500 | [diff] [blame] | 79 | // An AdjLsa initialized with INACTIVE adjacencies should not copy the adjacencies |
| 80 | AdjacencyList inactiveAdjacencies; |
| 81 | Adjacent inactiveAdjacency(adjacencyName); |
| 82 | inactiveAdjacency.setStatus(Adjacent::STATUS_INACTIVE); |
| 83 | inactiveAdjacencies.insert(inactiveAdjacency); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 84 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 85 | AdjLsa alsa2(routerName, seqNo, testTimePoint, |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame^] | 86 | inactiveAdjacencies.size(), inactiveAdjacencies); |
| 87 | BOOST_CHECK_EQUAL(alsa2.getAdl().size(), 0); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 88 | |
alvy | c69c9af | 2015-04-09 12:38:54 -0500 | [diff] [blame] | 89 | // Thus, the two LSAs should not have equal content |
| 90 | BOOST_CHECK_EQUAL(alsa1.isEqualContent(alsa2), false); |
| 91 | |
| 92 | // Create a duplicate of alsa1 which should have equal content |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 93 | AdjLsa alsa3(routerName, seqNo, testTimePoint, |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame^] | 94 | activeAdjacencies.size(), activeAdjacencies); |
alvy | c69c9af | 2015-04-09 12:38:54 -0500 | [diff] [blame] | 95 | BOOST_CHECK(alsa1.isEqualContent(alsa3)); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE(CoordinateLsaConstructorAndGetters) |
| 99 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 100 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 101 | std::vector<double> angles1, angles2; |
| 102 | angles1.push_back(30.0); |
| 103 | angles2.push_back(30.0); |
| 104 | CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles1); |
| 105 | CoordinateLsa clsa2("router1", 12, testTimePoint, 2.5, angles2); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 106 | |
| 107 | BOOST_CHECK_CLOSE(clsa1.getCorRadius(), 2.5, 0.0001); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 108 | BOOST_CHECK(clsa1.getCorTheta() == angles1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 109 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 110 | BOOST_CHECK(clsa1.isEqualContent(clsa2)); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 111 | |
| 112 | BOOST_CHECK_EQUAL(clsa1.getData(), clsa2.getData()); |
| 113 | } |
| 114 | |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 115 | BOOST_AUTO_TEST_CASE(IncrementAdjacentNumber) |
| 116 | { |
| 117 | Adjacent adj1("adjacent1"); |
| 118 | Adjacent adj2("adjacent2"); |
| 119 | |
| 120 | adj1.setStatus(Adjacent::STATUS_ACTIVE); |
| 121 | adj2.setStatus(Adjacent::STATUS_ACTIVE); |
| 122 | |
| 123 | AdjacencyList adjList; |
| 124 | adjList.insert(adj1); |
| 125 | adjList.insert(adj2); |
| 126 | |
| 127 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 128 | std::ostringstream ss; |
| 129 | ss << testTimePoint; |
| 130 | |
| 131 | const std::string TEST_TIME_POINT_STRING = ss.str(); |
| 132 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame^] | 133 | AdjLsa lsa("router1", 12, testTimePoint, adjList.size(), adjList); |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 134 | |
| 135 | std::string EXPECTED_OUTPUT = |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 136 | "Adj Lsa:\n" |
| 137 | " Origination Router: /router1\n" |
| 138 | " Ls Type: adjacency\n" |
| 139 | " Ls Seq No: 12\n" |
| 140 | " Ls Lifetime: " + TEST_TIME_POINT_STRING + "\n" |
| 141 | " Adjacents: \n" |
| 142 | " Adjacent 1:\n" |
| 143 | " Adjacent Name: /adjacent1\n" |
| 144 | " Connecting FaceUri: ://\n" |
| 145 | " Link Cost: 10\n" |
| 146 | " Adjacent 2:\n" |
| 147 | " Adjacent Name: /adjacent2\n" |
| 148 | " Connecting FaceUri: ://\n" |
| 149 | " Link Cost: 10\n" |
| 150 | "adj_lsa_end"; |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 151 | |
| 152 | std::ostringstream os; |
| 153 | os << lsa; |
| 154 | |
| 155 | BOOST_CHECK_EQUAL(os.str(), EXPECTED_OUTPUT); |
| 156 | } |
| 157 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 158 | BOOST_AUTO_TEST_CASE(TestInitializeFromContent) |
| 159 | { |
| 160 | //Adj LSA |
| 161 | Adjacent adj1("adjacent1"); |
| 162 | Adjacent adj2("adjacent2"); |
| 163 | |
| 164 | adj1.setStatus(Adjacent::STATUS_ACTIVE); |
| 165 | adj2.setStatus(Adjacent::STATUS_ACTIVE); |
| 166 | |
| 167 | //If we don't do this the test will fail |
| 168 | //Adjacent has default cost of 10 but no default |
| 169 | //connecting face URI, so initializeFromContent fails |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 170 | adj1.setFaceUri(ndn::util::FaceUri("udp://10.0.0.1")); |
| 171 | adj2.setFaceUri(ndn::util::FaceUri("udp://10.0.0.2")); |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 172 | |
| 173 | AdjacencyList adjList; |
| 174 | adjList.insert(adj1); |
| 175 | adjList.insert(adj2); |
| 176 | |
| 177 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
| 178 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame^] | 179 | AdjLsa adjlsa1("router1", 1, testTimePoint, adjList.size(), adjList); |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 180 | AdjLsa adjlsa2; |
| 181 | |
| 182 | BOOST_CHECK(adjlsa2.initializeFromContent(adjlsa1.getData())); |
| 183 | |
| 184 | BOOST_CHECK(adjlsa1.isEqualContent(adjlsa2)); |
| 185 | |
| 186 | //Name LSA |
| 187 | NamePrefixList npl1; |
| 188 | |
| 189 | std::string s1 = "name1"; |
| 190 | std::string s2 = "name2"; |
| 191 | |
| 192 | npl1.insert(s1); |
| 193 | npl1.insert(s2); |
| 194 | |
| 195 | NameLsa nlsa1("router1", 1, testTimePoint, npl1); |
| 196 | NameLsa nlsa2; |
| 197 | |
| 198 | BOOST_CHECK(nlsa2.initializeFromContent(nlsa1.getData())); |
| 199 | |
| 200 | BOOST_CHECK_EQUAL(nlsa1.getData(), nlsa2.getData()); |
| 201 | |
| 202 | //Coordinate LSA |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 203 | std::vector<double> angles = {30, 40.0}; |
| 204 | CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles); |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 205 | CoordinateLsa clsa2; |
| 206 | |
| 207 | BOOST_CHECK(clsa2.initializeFromContent(clsa1.getData())); |
| 208 | |
| 209 | BOOST_CHECK_EQUAL(clsa1.getData(), clsa2.getData()); |
| 210 | } |
| 211 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 212 | BOOST_AUTO_TEST_SUITE(TestNameLsa) |
| 213 | |
| 214 | BOOST_AUTO_TEST_CASE(OperatorEquals) |
| 215 | { |
| 216 | NameLsa lsa1; |
| 217 | NameLsa lsa2; |
| 218 | ndn::Name name1("/ndn/test/name1"); |
| 219 | ndn::Name name2("/ndn/test/name2"); |
| 220 | ndn::Name name3("/ndn/some/other/name1"); |
| 221 | |
| 222 | lsa1.addName(name1); |
| 223 | lsa1.addName(name2); |
| 224 | lsa1.addName(name3); |
| 225 | |
| 226 | lsa2.addName(name1); |
| 227 | lsa2.addName(name2); |
| 228 | lsa2.addName(name3); |
| 229 | |
| 230 | BOOST_CHECK(lsa1.isEqualContent(lsa2)); |
| 231 | } |
| 232 | |
| 233 | BOOST_AUTO_TEST_SUITE_END() // TestNameLsa |
| 234 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 235 | BOOST_AUTO_TEST_SUITE_END() |
| 236 | |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 237 | } // namespace test |
| 238 | } // namespace nlsr |