blob: 7df4ec8b4a48af6e1cd6e3e10103cb7e6152a37f [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento3dade002019-03-19 11:29:56 -06002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shib2831282015-01-09 14:55:58 -07004 * 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.
Junxiao Shi0e42c572014-10-05 20:33:48 -070010 *
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 "table/dead-nonce-list.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
Junxiao Shi0e42c572014-10-05 20:33:48 -070028
29#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi0e42c572014-10-05 20:33:48 -070031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tests {
Junxiao Shi0e42c572014-10-05 20:33:48 -070033
Junxiao Shi3978c922016-07-15 18:24:11 +000034BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040035BOOST_FIXTURE_TEST_SUITE(TestDeadNonceList, GlobalIoFixture)
Junxiao Shi0e42c572014-10-05 20:33:48 -070036
37BOOST_AUTO_TEST_CASE(Basic)
38{
39 Name nameA("ndn:/A");
40 Name nameB("ndn:/B");
Davide Pesavento51cf75c2020-03-11 22:21:13 -040041 const Interest::Nonce nonce1(0x53b4eaa8);
42 const Interest::Nonce nonce2(0x1f46372b);
Junxiao Shi0e42c572014-10-05 20:33:48 -070043
44 DeadNonceList dnl;
Junxiao Shia110f262014-10-12 12:35:20 -070045 BOOST_CHECK_EQUAL(dnl.size(), 0);
Junxiao Shi0e42c572014-10-05 20:33:48 -070046 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), false);
Junxiao Shia110f262014-10-12 12:35:20 -070047
Junxiao Shi0e42c572014-10-05 20:33:48 -070048 dnl.add(nameA, nonce1);
Junxiao Shia110f262014-10-12 12:35:20 -070049 BOOST_CHECK_EQUAL(dnl.size(), 1);
Junxiao Shi0e42c572014-10-05 20:33:48 -070050 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
51 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
52 BOOST_CHECK_EQUAL(dnl.has(nameB, nonce1), false);
53}
54
Varun Patil67ad70f2021-06-04 10:04:00 -070055BOOST_AUTO_TEST_CASE(NoDuplicates)
56{
57 Name nameA("ndn:/A");
58 const Interest::Nonce nonce1(0x53b4eaa8);
59 const Interest::Nonce nonce2(0x63b4eaa8);
60 const Interest::Nonce nonce3(0x73b4eaa8);
61 const Interest::Nonce nonce4(0x83b4eaa8);
62 const Interest::Nonce nonce5(0x93b4eaa8);
63
64 DeadNonceList dnl;
65 BOOST_CHECK_EQUAL(dnl.size(), 0);
66
67 dnl.m_capacity = 4;
68 dnl.add(nameA, nonce1);
69 dnl.add(nameA, nonce2);
70 dnl.add(nameA, nonce3);
71 dnl.add(nameA, nonce4);
72 BOOST_CHECK_EQUAL(dnl.size(), 4);
73
74 dnl.add(nameA, nonce1);
75 BOOST_CHECK_EQUAL(dnl.size(), 4);
76 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
77 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), true);
78 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true);
79 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true);
80
81 dnl.add(nameA, nonce5);
82 BOOST_CHECK_EQUAL(dnl.size(), 4);
83 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
84 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
85 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true);
86 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true);
87 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce5), true);
88
89 dnl.add(nameA, nonce5);
90 BOOST_CHECK_EQUAL(dnl.size(), 4);
91 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
92 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
93 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true);
94 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true);
95 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce5), true);
96}
97
Junxiao Shia110f262014-10-12 12:35:20 -070098BOOST_AUTO_TEST_CASE(MinLifetime)
99{
Davide Pesaventod4123932021-05-28 18:48:37 -0400100 BOOST_CHECK_THROW(DeadNonceList(0_ms), std::invalid_argument);
Junxiao Shia110f262014-10-12 12:35:20 -0700101}
102
Davide Pesaventod4123932021-05-28 18:48:37 -0400103/// A fixture that periodically inserts Nonces
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400104class PeriodicalInsertionFixture : public GlobalIoTimeFixture
Junxiao Shi0e42c572014-10-05 20:33:48 -0700105{
106protected:
107 PeriodicalInsertionFixture()
Junxiao Shi0e42c572014-10-05 20:33:48 -0700108 {
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400109 addNonce();
Junxiao Shi0e42c572014-10-05 20:33:48 -0700110 }
111
Junxiao Shi0e42c572014-10-05 20:33:48 -0700112 void
113 setRate(size_t nNoncesPerLifetime)
114 {
115 addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT;
116 }
117
118 void
119 addNonce()
120 {
121 for (size_t i = 0; i < addNonceBatch; ++i) {
122 dnl.add(name, ++lastNonce);
123 }
124
Davide Pesaventod4123932021-05-28 18:48:37 -0400125 addNonceEvent = getScheduler().schedule(ADD_INTERVAL, [this] { addNonce(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -0700126 }
127
Junxiao Shib2831282015-01-09 14:55:58 -0700128 /** \brief advance clocks by LIFETIME*t
129 */
130 void
Davide Pesaventod4123932021-05-28 18:48:37 -0400131 advanceClocksByLifetime(double t)
Junxiao Shib2831282015-01-09 14:55:58 -0700132 {
Davide Pesaventod4123932021-05-28 18:48:37 -0400133 advanceClocks(ADD_INTERVAL / 2, time::duration_cast<time::nanoseconds>(LIFETIME * t));
Junxiao Shib2831282015-01-09 14:55:58 -0700134 }
135
Junxiao Shi0e42c572014-10-05 20:33:48 -0700136protected:
Davide Pesaventod4123932021-05-28 18:48:37 -0400137 static constexpr time::nanoseconds LIFETIME = 200_ms;
138 static constexpr time::nanoseconds ADD_INTERVAL = LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT;
139
140 DeadNonceList dnl{LIFETIME};
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400141 Name name = "/N";
142 uint32_t lastNonce = 0;
143 size_t addNonceBatch = 0;
Junxiao Shib2831282015-01-09 14:55:58 -0700144 scheduler::ScopedEventId addNonceEvent;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700145};
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400146
Junxiao Shi0e42c572014-10-05 20:33:48 -0700147BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture)
148{
Junxiao Shia110f262014-10-12 12:35:20 -0700149 BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME);
150
Junxiao Shi0e42c572014-10-05 20:33:48 -0700151 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
152 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700153 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700154
155 Name nameC("ndn:/C");
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400156 const Interest::Nonce nonceC(0x25390656);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700157 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
158 dnl.add(nameC, nonceC);
159 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
160
Junxiao Shib2831282015-01-09 14:55:58 -0700161 this->advanceClocksByLifetime(0.5); // -50%, entry should exist
Junxiao Shi0e42c572014-10-05 20:33:48 -0700162 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
163
Junxiao Shib2831282015-01-09 14:55:58 -0700164 this->advanceClocksByLifetime(1.0); // +50%, entry should be gone
Junxiao Shi0e42c572014-10-05 20:33:48 -0700165 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
166}
167
168BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
169{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700170 ssize_t cap0 = dnl.m_capacity;
171
172 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
173 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700174 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700175
176 ssize_t cap1 = dnl.m_capacity;
177 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
178}
179
180BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
181{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700182 ssize_t cap0 = dnl.m_capacity;
183
184 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
185 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700186 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700187
188 ssize_t cap1 = dnl.m_capacity;
189 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
190}
191
Junxiao Shi3978c922016-07-15 18:24:11 +0000192BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList
193BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi0e42c572014-10-05 20:33:48 -0700194
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400195} // namespace nfd::tests