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