blob: 337cdc7cb2cd30e7ff55dcd85818719de7f0ca63 [file] [log] [blame]
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -07002/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, The University of Memphis,
Alexander Afanasyev135288c2022-04-23 23:06:56 -04004 * Regents of the University of California,
5 * Arizona Board of Regents.
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00006 *
Alexander Afanasyev135288c2022-04-23 23:06:56 -04007 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00009 *
Alexander Afanasyev135288c2022-04-23 23:06:56 -040010 * NLSR is free software: you can redistribute it and/or modify it under the terms
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000011 * 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 *
Alexander Afanasyev135288c2022-04-23 23:06:56 -040014 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000015 * 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
Alexander Afanasyev135288c2022-04-23 23:06:56 -040019 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000020 */
21
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000022#include "nlsr.hpp"
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000023
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040024#include "tests/io-key-chain-fixture.hpp"
25#include "tests/test-common.hpp"
26
Davide Pesavento65ee9922022-11-16 00:21:43 -050027#include <ndn-cxx/ims/in-memory-storage-fifo.hpp>
28#include <ndn-cxx/util/segmenter.hpp>
29
Davide Pesavento288141a2024-02-13 17:30:35 -050030namespace nlsr::tests {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000031
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040032class LsaSegmentStorageFixture : public IoKeyChainFixture
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000033{
34public:
35 LsaSegmentStorageFixture()
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000036 {
Davide Pesavento65ee9922022-11-16 00:21:43 -050037 afterSegmentValidatedConn = lsdb.afterSegmentValidatedSignal.connect([this] (auto&&...) {
38 ++numValidationSignal;
39 });
40
Ashlesh Gawande15052402018-12-12 20:20:00 -060041 std::string config = R"CONF(
42 trust-anchor
43 {
44 type any
45 }
46 )CONF";
47 conf.m_validator.load(config, "config-file-from-string");
48
Ashlesh Gawande15052402018-12-12 20:20:00 -060049 this->advanceClocks(ndn::time::milliseconds(10));
Ashlesh Gawande15052402018-12-12 20:20:00 -060050 face.sentInterests.clear();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000051 }
52
Ashlesh Gawande15052402018-12-12 20:20:00 -060053 void
Davide Pesavento65ee9922022-11-16 00:21:43 -050054 makeLsaContent(ndn::Name interestName, int numNames = 1000)
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060055 {
Davide Pesavento65ee9922022-11-16 00:21:43 -050056 const ndn::time::seconds refreshTime{LSA_REFRESH_TIME_DEFAULT};
57
Ashlesh Gawande15052402018-12-12 20:20:00 -060058 NamePrefixList npl1;
59 for (int i = 0; i < numNames; ++i) {
60 npl1.insert("name1-" + std::to_string(i));
61 }
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060062 NameLsa nameLsa("/ndn/other-site/%C1.Router/other-router", 12,
Davide Pesavento65ee9922022-11-16 00:21:43 -050063 ndn::time::system_clock::now() + refreshTime, npl1);
64
65 interestName.appendVersion();
Junxiao Shi43f37a02023-08-09 00:09:00 +000066 ndn::Segmenter segmenter(m_keyChain, ndn::security::SigningInfo());
Davide Pesavento65ee9922022-11-16 00:21:43 -050067 auto segments = segmenter.segment(nameLsa.wireEncode(), interestName,
68 ndn::MAX_NDN_PACKET_SIZE / 2, refreshTime);
69 for (const auto& seg : segments) {
70 ims.insert(*seg, refreshTime);
71 }
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000072 }
73
74 void
Davide Pesavento65ee9922022-11-16 00:21:43 -050075 sendReplies()
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000076 {
Davide Pesavento65ee9922022-11-16 00:21:43 -050077 for (const auto& interest : face.sentInterests) {
78 if (auto data = ims.find(interest.getName()); data) {
79 face.put(*data);
80 }
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000081 }
Davide Pesavento65ee9922022-11-16 00:21:43 -050082 face.sentInterests.clear();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000083 }
84
85public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000086 ndn::DummyClientFace face{m_io, m_keyChain, {true, true}};
Davide Pesavento65ee9922022-11-16 00:21:43 -050087 ConfParameter conf{face, m_keyChain};
88 DummyConfFileProcessor confProcessor{conf};
89 Nlsr nlsr{face, m_keyChain, conf};
90 Lsdb& lsdb{nlsr.m_lsdb};
91 ndn::InMemoryStorageFifo ims{100};
Ashlesh Gawande15052402018-12-12 20:20:00 -060092
Davide Pesavento65ee9922022-11-16 00:21:43 -050093 int numValidationSignal = 0;
Junxiao Shi43f37a02023-08-09 00:09:00 +000094 ndn::signal::ScopedConnection afterSegmentValidatedConn;
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000095};
96
97BOOST_FIXTURE_TEST_SUITE(TestLsaSegmentStorage, LsaSegmentStorageFixture)
98
99BOOST_AUTO_TEST_CASE(Basic)
100{
101 ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
102 lsaInterestName.appendNumber(12);
103
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400104 lsdb.expressInterest(lsaInterestName, 0, 0);
Ashlesh Gawande15052402018-12-12 20:20:00 -0600105 advanceClocks(ndn::time::milliseconds(10));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000106
Ashlesh Gawande15052402018-12-12 20:20:00 -0600107 makeLsaContent(lsaInterestName);
Davide Pesavento65ee9922022-11-16 00:21:43 -0500108 BOOST_CHECK_EQUAL(ims.size(), 3);
Ashlesh Gawande15052402018-12-12 20:20:00 -0600109
Davide Pesavento65ee9922022-11-16 00:21:43 -0500110 // 1st segment
111 sendReplies();
112 advanceClocks(ndn::time::milliseconds(10));
113 // 2nd and 3rd segments
114 sendReplies();
115 advanceClocks(ndn::time::milliseconds(10));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000116
Ashlesh Gawande15052402018-12-12 20:20:00 -0600117 // 3 data segments should be in the storage
118 BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 3);
119 BOOST_CHECK_EQUAL(numValidationSignal, 3);
120 numValidationSignal = 0;
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000121
Ashlesh Gawande15052402018-12-12 20:20:00 -0600122 // Remove older LSA from storage upon receiving that of higher sequence
123 ndn::Name lsaInterestName2("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
124 lsaInterestName2.appendNumber(13);
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400125 lsdb.expressInterest(lsaInterestName2, 0, 0);
Ashlesh Gawande15052402018-12-12 20:20:00 -0600126 advanceClocks(ndn::time::milliseconds(10));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000127
Ashlesh Gawande15052402018-12-12 20:20:00 -0600128 makeLsaContent(lsaInterestName2, 1);
Davide Pesavento65ee9922022-11-16 00:21:43 -0500129 sendReplies();
Ashlesh Gawande15052402018-12-12 20:20:00 -0600130 advanceClocks(ndn::time::milliseconds(10));
Davide Pesavento65ee9922022-11-16 00:21:43 -0500131
Ashlesh Gawande15052402018-12-12 20:20:00 -0600132 // Should have cleared all the three segments for the previous interest w/ seq 12
133 // And add one segment for this sequence 13
134 BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 1);
135 BOOST_CHECK_EQUAL(numValidationSignal, 1);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000136
Ashlesh Gawande15052402018-12-12 20:20:00 -0600137 // Scheduled removal of LSA
138 advanceClocks(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT));
139 BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 0);
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600140}
141
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000142BOOST_AUTO_TEST_SUITE_END() // TestLsaSegmentStorage
143
Davide Pesavento288141a2024-02-13 17:30:35 -0500144} // namespace nlsr::tests