blob: 922e78cd8363569cd61c671bed4cc93ab3ce726d [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/*
Junxiao Shi43f37a02023-08-09 00:09:00 +00003 * Copyright (c) 2014-2023, 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
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000030namespace nlsr {
31namespace test {
32
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040033class LsaSegmentStorageFixture : public IoKeyChainFixture
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000034{
35public:
36 LsaSegmentStorageFixture()
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000037 {
Davide Pesavento65ee9922022-11-16 00:21:43 -050038 afterSegmentValidatedConn = lsdb.afterSegmentValidatedSignal.connect([this] (auto&&...) {
39 ++numValidationSignal;
40 });
41
Ashlesh Gawande15052402018-12-12 20:20:00 -060042 std::string config = R"CONF(
43 trust-anchor
44 {
45 type any
46 }
47 )CONF";
48 conf.m_validator.load(config, "config-file-from-string");
49
Ashlesh Gawande15052402018-12-12 20:20:00 -060050 this->advanceClocks(ndn::time::milliseconds(10));
Ashlesh Gawande15052402018-12-12 20:20:00 -060051 face.sentInterests.clear();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000052 }
53
Ashlesh Gawande15052402018-12-12 20:20:00 -060054 void
Davide Pesavento65ee9922022-11-16 00:21:43 -050055 makeLsaContent(ndn::Name interestName, int numNames = 1000)
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060056 {
Davide Pesavento65ee9922022-11-16 00:21:43 -050057 const ndn::time::seconds refreshTime{LSA_REFRESH_TIME_DEFAULT};
58
Ashlesh Gawande15052402018-12-12 20:20:00 -060059 NamePrefixList npl1;
60 for (int i = 0; i < numNames; ++i) {
61 npl1.insert("name1-" + std::to_string(i));
62 }
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060063 NameLsa nameLsa("/ndn/other-site/%C1.Router/other-router", 12,
Davide Pesavento65ee9922022-11-16 00:21:43 -050064 ndn::time::system_clock::now() + refreshTime, npl1);
65
66 interestName.appendVersion();
Junxiao Shi43f37a02023-08-09 00:09:00 +000067 ndn::Segmenter segmenter(m_keyChain, ndn::security::SigningInfo());
Davide Pesavento65ee9922022-11-16 00:21:43 -050068 auto segments = segmenter.segment(nameLsa.wireEncode(), interestName,
69 ndn::MAX_NDN_PACKET_SIZE / 2, refreshTime);
70 for (const auto& seg : segments) {
71 ims.insert(*seg, refreshTime);
72 }
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000073 }
74
75 void
Davide Pesavento65ee9922022-11-16 00:21:43 -050076 sendReplies()
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000077 {
Davide Pesavento65ee9922022-11-16 00:21:43 -050078 for (const auto& interest : face.sentInterests) {
79 if (auto data = ims.find(interest.getName()); data) {
80 face.put(*data);
81 }
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000082 }
Davide Pesavento65ee9922022-11-16 00:21:43 -050083 face.sentInterests.clear();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000084 }
85
86public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000087 ndn::DummyClientFace face{m_io, m_keyChain, {true, true}};
Davide Pesavento65ee9922022-11-16 00:21:43 -050088 ConfParameter conf{face, m_keyChain};
89 DummyConfFileProcessor confProcessor{conf};
90 Nlsr nlsr{face, m_keyChain, conf};
91 Lsdb& lsdb{nlsr.m_lsdb};
92 ndn::InMemoryStorageFifo ims{100};
Ashlesh Gawande15052402018-12-12 20:20:00 -060093
Davide Pesavento65ee9922022-11-16 00:21:43 -050094 int numValidationSignal = 0;
Junxiao Shi43f37a02023-08-09 00:09:00 +000095 ndn::signal::ScopedConnection afterSegmentValidatedConn;
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000096};
97
98BOOST_FIXTURE_TEST_SUITE(TestLsaSegmentStorage, LsaSegmentStorageFixture)
99
100BOOST_AUTO_TEST_CASE(Basic)
101{
102 ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
103 lsaInterestName.appendNumber(12);
104
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400105 lsdb.expressInterest(lsaInterestName, 0, 0);
Ashlesh Gawande15052402018-12-12 20:20:00 -0600106 advanceClocks(ndn::time::milliseconds(10));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000107
Ashlesh Gawande15052402018-12-12 20:20:00 -0600108 makeLsaContent(lsaInterestName);
Davide Pesavento65ee9922022-11-16 00:21:43 -0500109 BOOST_CHECK_EQUAL(ims.size(), 3);
Ashlesh Gawande15052402018-12-12 20:20:00 -0600110
Davide Pesavento65ee9922022-11-16 00:21:43 -0500111 // 1st segment
112 sendReplies();
113 advanceClocks(ndn::time::milliseconds(10));
114 // 2nd and 3rd segments
115 sendReplies();
116 advanceClocks(ndn::time::milliseconds(10));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000117
Ashlesh Gawande15052402018-12-12 20:20:00 -0600118 // 3 data segments should be in the storage
119 BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 3);
120 BOOST_CHECK_EQUAL(numValidationSignal, 3);
121 numValidationSignal = 0;
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000122
Ashlesh Gawande15052402018-12-12 20:20:00 -0600123 // Remove older LSA from storage upon receiving that of higher sequence
124 ndn::Name lsaInterestName2("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
125 lsaInterestName2.appendNumber(13);
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400126 lsdb.expressInterest(lsaInterestName2, 0, 0);
Ashlesh Gawande15052402018-12-12 20:20:00 -0600127 advanceClocks(ndn::time::milliseconds(10));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000128
Ashlesh Gawande15052402018-12-12 20:20:00 -0600129 makeLsaContent(lsaInterestName2, 1);
Davide Pesavento65ee9922022-11-16 00:21:43 -0500130 sendReplies();
Ashlesh Gawande15052402018-12-12 20:20:00 -0600131 advanceClocks(ndn::time::milliseconds(10));
Davide Pesavento65ee9922022-11-16 00:21:43 -0500132
Ashlesh Gawande15052402018-12-12 20:20:00 -0600133 // Should have cleared all the three segments for the previous interest w/ seq 12
134 // And add one segment for this sequence 13
135 BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 1);
136 BOOST_CHECK_EQUAL(numValidationSignal, 1);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000137
Ashlesh Gawande15052402018-12-12 20:20:00 -0600138 // Scheduled removal of LSA
139 advanceClocks(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT));
140 BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 0);
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600141}
142
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000143BOOST_AUTO_TEST_SUITE_END() // TestLsaSegmentStorage
144
145} // namespace test
146} // namespace nlsr