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