blob: 85806c2eb3d8042db309312a141e0c446eae9c87 [file] [log] [blame]
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2018, Regents of the University of California,
4 * 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)
42 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
43 , lsdb(nlsr.getLsdb())
44 , lsaStorage(lsdb.getLsaStorage())
45 {
46 }
47
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060048 std::string
49 makeLsaContent()
50 {
51 ndn::Name s1{"name1"};
52 ndn::Name s2{"name2"};
53 NamePrefixList npl1{s1, s2};
54 NameLsa nameLsa("/ndn/other-site/%C1.Router/other-router", 12,
Ashlesh Gawanded65786a2018-03-30 01:17:58 -050055 ndn::time::system_clock::now() + ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), npl1);
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060056 return nameLsa.serialize();
57 }
58
59 shared_ptr<ndn::Data>
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000060 makeLsaSegment(const ndn::Name& baseName, uint64_t segmentNo, bool isFinal)
61 {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000062 ndn::Name lsaDataName(baseName);
63 lsaDataName.appendSegment(segmentNo);
64 auto lsaSegment = make_shared<ndn::Data>(ndn::Name(lsaDataName));
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060065 std::string content = makeLsaContent();
66 lsaSegment->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.length());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000067 if (isFinal) {
Ashlesh Gawanded65786a2018-03-30 01:17:58 -050068 lsaSegment->setFinalBlock(lsaSegment->getName()[-1]);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000069 }
70
71 return signData(lsaSegment);
72 }
73
74 void
75 receiveLsaInterest(const ndn::Name& baseInterestName, uint64_t segmentNo,
76 bool isSegmentZero)
77 {
78 if (isSegmentZero) {
79 lsdb.processInterest(ndn::Name(), ndn::Interest(baseInterestName));
80 }
81 else {
82 ndn::Name nextInterestName(baseInterestName);
83 nextInterestName.appendSegment(segmentNo);
84 lsdb.processInterest(ndn::Name(), ndn::Interest(nextInterestName));
85 advanceClocks(ndn::time::milliseconds(1), 10);
86 }
87 }
88
89public:
90 ndn::util::DummyClientFace face;
91 Nlsr nlsr;
92 Lsdb& lsdb;
93 LsaSegmentStorage& lsaStorage;
94};
95
96BOOST_FIXTURE_TEST_SUITE(TestLsaSegmentStorage, LsaSegmentStorageFixture)
97
98BOOST_AUTO_TEST_CASE(Basic)
99{
100 ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
101 lsaInterestName.appendNumber(12);
102
103 ndn::Name lsaDataName(lsaInterestName);
104 lsaDataName.appendVersion();
105
106 for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) {
107 auto lsaData = makeLsaSegment(lsaDataName, segmentNo, segmentNo == 4);
108 lsaStorage.afterFetcherSignalEmitted(*lsaData);
109 }
110
111 // receive interest for other-router's LSA that is stored in this router's storage
112 for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) {
113 receiveLsaInterest(lsaInterestName, segmentNo, segmentNo == 0);
114 }
115
116 // 4 data segments should be sent in response to 4 interests
117 BOOST_CHECK_EQUAL(face.sentData.size(), 4);
118}
119
120BOOST_AUTO_TEST_CASE(DeleteOldLsa)
121{
122 ndn::Name lsaDataName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
123 uint64_t segmentNo = 0;
124
125 uint64_t oldSeqNo = 12;
126 ndn::Name oldLsaDataName(lsaDataName);
127 oldLsaDataName.appendNumber(oldSeqNo);
128 oldLsaDataName.appendVersion();
129
130 auto oldLsaData = makeLsaSegment(oldLsaDataName, segmentNo, true);
131 lsaStorage.afterFetcherSignalEmitted(*oldLsaData);
132 advanceClocks(ndn::time::milliseconds(1), 10);
133
134 uint64_t newSeqNo = 13;
135 ndn::Name newLsaDataName(lsaDataName);
136 newLsaDataName.appendNumber(newSeqNo);
137 newLsaDataName.appendVersion();
138
139 auto newLsaData = makeLsaSegment(newLsaDataName, segmentNo, true);
140 lsaStorage.afterFetcherSignalEmitted(*newLsaData);
141 advanceClocks(ndn::time::milliseconds(1), 10);
142
143 ndn::Name lsaInterestName(lsaDataName);
144
145 ndn::Name oldLsaInterestName(lsaInterestName);
146 oldLsaInterestName.appendNumber(oldSeqNo);
147 receiveLsaInterest(oldLsaInterestName, segmentNo, true);
148
149 advanceClocks(ndn::time::milliseconds(1), 10);
150
151 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
152
153 ndn::Name newLsaInterestName(lsaInterestName);
154 newLsaInterestName.appendNumber(newSeqNo);
155 receiveLsaInterest(newLsaInterestName, segmentNo, true);
156
157 advanceClocks(ndn::time::milliseconds(1), 10);
158
159 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
160}
161
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600162BOOST_AUTO_TEST_CASE(ScheduledDeletion)
163{
164 ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
165 lsaInterestName.appendNumber(12);
166
167 ndn::Name lsaDataName(lsaInterestName);
168 lsaDataName.appendVersion();
169
170 auto lsaData = makeLsaSegment(lsaDataName, 0, true);
171 lsaStorage.afterFetcherSignalEmitted(*lsaData);
172
173 BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) != nullptr);
174
Ashlesh Gawanded65786a2018-03-30 01:17:58 -0500175 // Make sure it was not deleted earlier somehow
176 advanceClocks(ndn::time::seconds(100), 10);
177 BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) != nullptr);
178
179 advanceClocks(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), 10);
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600180
181 BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) == nullptr);
182}
183
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000184BOOST_AUTO_TEST_SUITE_END() // TestLsaSegmentStorage
185
186} // namespace test
187} // namespace nlsr