blob: 6f9928e0d4c92c04b1f3c94f0a4f0edef3c31247 [file] [log] [blame]
Junxiao Shi20377642023-08-24 23:55:40 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, The University of Memphis,
Junxiao Shi20377642023-08-24 23:55:40 +00004 * 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 "lsa/name-lsa.hpp"
23
24#include "tests/boost-test.hpp"
25
Davide Pesavento288141a2024-02-13 17:30:35 -050026namespace nlsr::tests {
Junxiao Shi20377642023-08-24 23:55:40 +000027
28BOOST_AUTO_TEST_SUITE(TestNameLsa)
29
30const uint8_t NAME_LSA1[] = {
31 0x89, 0x37, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
32 0x82, 0x01, 0x0C, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
33 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61,
34 0x6D, 0x65, 0x31, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32
35};
36
37const uint8_t NAME_LSA_EXTRA_NAME[] = {
38 0x89, 0x40, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
39 0x82, 0x01, 0x0C, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
40 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61,
41 0x6D, 0x65, 0x31, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, 0x07, 0x07, 0x08,
42 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33
43};
44
45const uint8_t NAME_LSA_DIFF_SEQ[] = {
46 0x89, 0x40, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
47 0x82, 0x01, 0x0E, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
48 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61,
49 0x6D, 0x65, 0x31, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, 0x07, 0x07, 0x08,
50 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33
51};
52
53const uint8_t NAME_LSA_DIFF_TS[] = {
54 0x89, 0x40, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
55 0x82, 0x01, 0x0E, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
56 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x34, 0x34, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61,
57 0x6D, 0x65, 0x31, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, 0x07, 0x07, 0x08,
58 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33
59};
60
61BOOST_AUTO_TEST_CASE(Basic)
62{
63 ndn::Name s1{"name1"};
64 ndn::Name s2{"name2"};
65 NamePrefixList npl1{s1, s2};
66
67 auto testTimePoint = ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196014943));
68
69 // 3rd argument is seqNo
70 NameLsa nlsa1("router1", 12, testTimePoint, npl1);
71 NameLsa nlsa2("router2", 12, testTimePoint, npl1);
72
73 BOOST_CHECK_EQUAL(nlsa1.getType(), Lsa::Type::NAME);
74 BOOST_CHECK(nlsa1.getExpirationTimePoint() == nlsa2.getExpirationTimePoint());
75
76 auto wire = nlsa1.wireEncode();
77 BOOST_TEST(wire == NAME_LSA1, boost::test_tools::per_element());
78
79 nlsa1.addName("name3");
80 wire = nlsa1.wireEncode();
81 BOOST_TEST(wire == NAME_LSA_EXTRA_NAME, boost::test_tools::per_element());
82
83 nlsa1.setSeqNo(14);
84 wire = nlsa1.wireEncode();
85 BOOST_TEST(wire == NAME_LSA_DIFF_SEQ, boost::test_tools::per_element());
86
87 testTimePoint =
88 ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196024993));
89 nlsa1.setExpirationTimePoint(testTimePoint);
90 wire = nlsa1.wireEncode();
91 BOOST_TEST(wire == NAME_LSA_DIFF_TS, boost::test_tools::per_element());
92
93 // Not testing router name as not sure if that will ever change once set
94}
95
96BOOST_AUTO_TEST_CASE(InitializeFromContent)
97{
98 auto testTimePoint = ndn::time::system_clock::now();
99 ndn::Name s1{"name1"};
100 ndn::Name s2{"name2"};
101 NamePrefixList npl1{s1, s2};
102
103 NameLsa nlsa1("router1", 1, testTimePoint, npl1);
104 NameLsa nlsa2(nlsa1.wireEncode());
105 BOOST_CHECK_EQUAL(nlsa1.wireEncode(), nlsa2.wireEncode());
106}
107
108BOOST_AUTO_TEST_CASE(OperatorEquals)
109{
Junxiao Shi20377642023-08-24 23:55:40 +0000110 ndn::Name name1("/ndn/test/name1");
111 ndn::Name name2("/ndn/test/name2");
112 ndn::Name name3("/ndn/some/other/name1");
113
Junxiao Shi2ecb0592023-08-14 11:35:05 +0000114 NameLsa lsa1;
Junxiao Shi20377642023-08-24 23:55:40 +0000115 lsa1.addName(name1);
116 lsa1.addName(name2);
117 lsa1.addName(name3);
118
Junxiao Shi2ecb0592023-08-14 11:35:05 +0000119 NameLsa lsa2;
Junxiao Shi20377642023-08-24 23:55:40 +0000120 lsa2.addName(name1);
121 lsa2.addName(name2);
Junxiao Shi2ecb0592023-08-14 11:35:05 +0000122 BOOST_CHECK_NE(lsa1, lsa2);
Junxiao Shi20377642023-08-24 23:55:40 +0000123
Junxiao Shi2ecb0592023-08-14 11:35:05 +0000124 lsa2.addName(name3);
125 BOOST_CHECK_EQUAL(lsa1, lsa2);
Junxiao Shi20377642023-08-24 23:55:40 +0000126}
127
128BOOST_AUTO_TEST_CASE(Update)
129{
130 NameLsa knownNameLsa;
131 knownNameLsa.m_originRouter = ndn::Name("/yoursunny/_/%C1.Router/dal");
132 knownNameLsa.m_seqNo = 2683;
133 knownNameLsa.setExpirationTimePoint(ndn::time::system_clock::now() + 3561_ms);
134 knownNameLsa.addName("/yoursunny/_/dal");
135 knownNameLsa.addName("/ndn");
136
137 auto rcvdLsa = std::make_shared<NameLsa>();
138 rcvdLsa->m_originRouter = ndn::Name("/yoursunny/_/%C1.Router/dal");
139 rcvdLsa->m_seqNo = 2684;
140 rcvdLsa->setExpirationTimePoint(ndn::time::system_clock::now() + 3600_ms);
141
142 auto nlsa = std::static_pointer_cast<NameLsa>(rcvdLsa);
143 nlsa->addName("/ndn");
144 nlsa->addName("/yoursunny/_/dal");
145 ndn::Name addedName1("/yoursunny/video/ndn-dpdk_acmicn20_20200917");
146 ndn::Name addedName2("/yoursunny/pushups");
147 nlsa->addName(addedName1);
148 nlsa->addName(addedName2);
149
150 auto [updated, namesToAdd, namesToRemove] = knownNameLsa.update(rcvdLsa);
151
152 BOOST_CHECK_EQUAL(updated, true);
153 BOOST_CHECK_EQUAL(namesToAdd.size(), 2);
154 BOOST_CHECK_EQUAL(namesToRemove.size(), 0);
155 auto it = std::find(namesToAdd.begin(), namesToAdd.end(), addedName1);
156 BOOST_CHECK(it != namesToAdd.end());
157 it = std::find(namesToAdd.begin(), namesToAdd.end(), addedName2);
158 BOOST_CHECK(it != namesToAdd.end());
159}
160
161BOOST_AUTO_TEST_SUITE_END()
162
Davide Pesavento288141a2024-02-13 17:30:35 -0500163} // namespace nlsr::tests