blob: 7ebd900a0507d8b41a4e64892663087eac0a9d98 [file] [log] [blame]
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, Regents of the University of California,
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "lsa-segment-storage.hpp"
27#include "test-common.hpp"
28#include "nlsr.hpp"
29#include "name-prefix-list.hpp"
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060030#include "lsa.hpp"
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000031
32#include <boost/test/unit_test.hpp>
33
34namespace nlsr {
35namespace test {
36
37class LsaSegmentStorageFixture : public UnitTestTimeFixture
38{
39public:
40 LsaSegmentStorageFixture()
41 : face(m_ioService, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 , conf(face)
43 , nlsr(face, m_keyChain, conf)
44 , lsdb(nlsr.m_lsdb)
45 , lsaStorage(lsdb.m_lsaStorage)
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000046 {
47 }
48
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060049 std::string
50 makeLsaContent()
51 {
52 ndn::Name s1{"name1"};
53 ndn::Name s2{"name2"};
54 NamePrefixList npl1{s1, s2};
55 NameLsa nameLsa("/ndn/other-site/%C1.Router/other-router", 12,
Ashlesh Gawanded65786a2018-03-30 01:17:58 -050056 ndn::time::system_clock::now() + ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), npl1);
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060057 return nameLsa.serialize();
58 }
59
60 shared_ptr<ndn::Data>
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000061 makeLsaSegment(const ndn::Name& baseName, uint64_t segmentNo, bool isFinal)
62 {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000063 ndn::Name lsaDataName(baseName);
64 lsaDataName.appendSegment(segmentNo);
65 auto lsaSegment = make_shared<ndn::Data>(ndn::Name(lsaDataName));
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060066 std::string content = makeLsaContent();
67 lsaSegment->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.length());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000068 if (isFinal) {
Ashlesh Gawanded65786a2018-03-30 01:17:58 -050069 lsaSegment->setFinalBlock(lsaSegment->getName()[-1]);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000070 }
71
72 return signData(lsaSegment);
73 }
74
75 void
76 receiveLsaInterest(const ndn::Name& baseInterestName, uint64_t segmentNo,
77 bool isSegmentZero)
78 {
79 if (isSegmentZero) {
80 lsdb.processInterest(ndn::Name(), ndn::Interest(baseInterestName));
81 }
82 else {
83 ndn::Name nextInterestName(baseInterestName);
84 nextInterestName.appendSegment(segmentNo);
85 lsdb.processInterest(ndn::Name(), ndn::Interest(nextInterestName));
86 advanceClocks(ndn::time::milliseconds(1), 10);
87 }
88 }
89
90public:
91 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 ConfParameter conf;
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000093 Nlsr nlsr;
94 Lsdb& lsdb;
95 LsaSegmentStorage& lsaStorage;
96};
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
105 ndn::Name lsaDataName(lsaInterestName);
106 lsaDataName.appendVersion();
107
108 for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) {
109 auto lsaData = makeLsaSegment(lsaDataName, segmentNo, segmentNo == 4);
110 lsaStorage.afterFetcherSignalEmitted(*lsaData);
111 }
112
113 // receive interest for other-router's LSA that is stored in this router's storage
114 for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) {
115 receiveLsaInterest(lsaInterestName, segmentNo, segmentNo == 0);
116 }
117
118 // 4 data segments should be sent in response to 4 interests
119 BOOST_CHECK_EQUAL(face.sentData.size(), 4);
120}
121
122BOOST_AUTO_TEST_CASE(DeleteOldLsa)
123{
124 ndn::Name lsaDataName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
125 uint64_t segmentNo = 0;
126
127 uint64_t oldSeqNo = 12;
128 ndn::Name oldLsaDataName(lsaDataName);
129 oldLsaDataName.appendNumber(oldSeqNo);
130 oldLsaDataName.appendVersion();
131
132 auto oldLsaData = makeLsaSegment(oldLsaDataName, segmentNo, true);
133 lsaStorage.afterFetcherSignalEmitted(*oldLsaData);
134 advanceClocks(ndn::time::milliseconds(1), 10);
135
136 uint64_t newSeqNo = 13;
137 ndn::Name newLsaDataName(lsaDataName);
138 newLsaDataName.appendNumber(newSeqNo);
139 newLsaDataName.appendVersion();
140
141 auto newLsaData = makeLsaSegment(newLsaDataName, segmentNo, true);
142 lsaStorage.afterFetcherSignalEmitted(*newLsaData);
143 advanceClocks(ndn::time::milliseconds(1), 10);
144
145 ndn::Name lsaInterestName(lsaDataName);
146
147 ndn::Name oldLsaInterestName(lsaInterestName);
148 oldLsaInterestName.appendNumber(oldSeqNo);
149 receiveLsaInterest(oldLsaInterestName, segmentNo, true);
150
151 advanceClocks(ndn::time::milliseconds(1), 10);
152
153 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
154
155 ndn::Name newLsaInterestName(lsaInterestName);
156 newLsaInterestName.appendNumber(newSeqNo);
157 receiveLsaInterest(newLsaInterestName, segmentNo, true);
158
159 advanceClocks(ndn::time::milliseconds(1), 10);
160
161 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
162}
163
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600164BOOST_AUTO_TEST_CASE(ScheduledDeletion)
165{
166 ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
167 lsaInterestName.appendNumber(12);
168
169 ndn::Name lsaDataName(lsaInterestName);
170 lsaDataName.appendVersion();
171
172 auto lsaData = makeLsaSegment(lsaDataName, 0, true);
173 lsaStorage.afterFetcherSignalEmitted(*lsaData);
174
175 BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) != nullptr);
176
Ashlesh Gawanded65786a2018-03-30 01:17:58 -0500177 // Make sure it was not deleted earlier somehow
178 advanceClocks(ndn::time::seconds(100), 10);
179 BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) != nullptr);
180
181 advanceClocks(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), 10);
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600182
183 BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) == nullptr);
184}
185
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000186BOOST_AUTO_TEST_SUITE_END() // TestLsaSegmentStorage
187
188} // namespace test
189} // namespace nlsr