blob: 44b0bfd250096fcf932cb181b628d9430a6fcff7 [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
32namespace nfd {
33namespace tests {
34
Junxiao Shi3978c922016-07-15 18:24:11 +000035BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040036BOOST_FIXTURE_TEST_SUITE(TestDeadNonceList, GlobalIoFixture)
Junxiao Shi0e42c572014-10-05 20:33:48 -070037
38BOOST_AUTO_TEST_CASE(Basic)
39{
40 Name nameA("ndn:/A");
41 Name nameB("ndn:/B");
Davide Pesavento51cf75c2020-03-11 22:21:13 -040042 const Interest::Nonce nonce1(0x53b4eaa8);
43 const Interest::Nonce nonce2(0x1f46372b);
Junxiao Shi0e42c572014-10-05 20:33:48 -070044
45 DeadNonceList dnl;
Junxiao Shia110f262014-10-12 12:35:20 -070046 BOOST_CHECK_EQUAL(dnl.size(), 0);
Junxiao Shi0e42c572014-10-05 20:33:48 -070047 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), false);
Junxiao Shia110f262014-10-12 12:35:20 -070048
Junxiao Shi0e42c572014-10-05 20:33:48 -070049 dnl.add(nameA, nonce1);
Junxiao Shia110f262014-10-12 12:35:20 -070050 BOOST_CHECK_EQUAL(dnl.size(), 1);
Junxiao Shi0e42c572014-10-05 20:33:48 -070051 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
52 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
53 BOOST_CHECK_EQUAL(dnl.has(nameB, nonce1), false);
54}
55
Varun Patil67ad70f2021-06-04 10:04:00 -070056BOOST_AUTO_TEST_CASE(NoDuplicates)
57{
58 Name nameA("ndn:/A");
59 const Interest::Nonce nonce1(0x53b4eaa8);
60 const Interest::Nonce nonce2(0x63b4eaa8);
61 const Interest::Nonce nonce3(0x73b4eaa8);
62 const Interest::Nonce nonce4(0x83b4eaa8);
63 const Interest::Nonce nonce5(0x93b4eaa8);
64
65 DeadNonceList dnl;
66 BOOST_CHECK_EQUAL(dnl.size(), 0);
67
68 dnl.m_capacity = 4;
69 dnl.add(nameA, nonce1);
70 dnl.add(nameA, nonce2);
71 dnl.add(nameA, nonce3);
72 dnl.add(nameA, nonce4);
73 BOOST_CHECK_EQUAL(dnl.size(), 4);
74
75 dnl.add(nameA, nonce1);
76 BOOST_CHECK_EQUAL(dnl.size(), 4);
77 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
78 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), true);
79 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true);
80 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true);
81
82 dnl.add(nameA, nonce5);
83 BOOST_CHECK_EQUAL(dnl.size(), 4);
84 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
85 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
86 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true);
87 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true);
88 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce5), true);
89
90 dnl.add(nameA, nonce5);
91 BOOST_CHECK_EQUAL(dnl.size(), 4);
92 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
93 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
94 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true);
95 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true);
96 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce5), true);
97}
98
Junxiao Shia110f262014-10-12 12:35:20 -070099BOOST_AUTO_TEST_CASE(MinLifetime)
100{
Davide Pesaventod4123932021-05-28 18:48:37 -0400101 BOOST_CHECK_THROW(DeadNonceList(0_ms), std::invalid_argument);
Junxiao Shia110f262014-10-12 12:35:20 -0700102}
103
Davide Pesaventod4123932021-05-28 18:48:37 -0400104/// A fixture that periodically inserts Nonces
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400105class PeriodicalInsertionFixture : public GlobalIoTimeFixture
Junxiao Shi0e42c572014-10-05 20:33:48 -0700106{
107protected:
108 PeriodicalInsertionFixture()
Junxiao Shi0e42c572014-10-05 20:33:48 -0700109 {
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400110 addNonce();
Junxiao Shi0e42c572014-10-05 20:33:48 -0700111 }
112
Junxiao Shi0e42c572014-10-05 20:33:48 -0700113 void
114 setRate(size_t nNoncesPerLifetime)
115 {
116 addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT;
117 }
118
119 void
120 addNonce()
121 {
122 for (size_t i = 0; i < addNonceBatch; ++i) {
123 dnl.add(name, ++lastNonce);
124 }
125
Davide Pesaventod4123932021-05-28 18:48:37 -0400126 addNonceEvent = getScheduler().schedule(ADD_INTERVAL, [this] { addNonce(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -0700127 }
128
Junxiao Shib2831282015-01-09 14:55:58 -0700129 /** \brief advance clocks by LIFETIME*t
130 */
131 void
Davide Pesaventod4123932021-05-28 18:48:37 -0400132 advanceClocksByLifetime(double t)
Junxiao Shib2831282015-01-09 14:55:58 -0700133 {
Davide Pesaventod4123932021-05-28 18:48:37 -0400134 advanceClocks(ADD_INTERVAL / 2, time::duration_cast<time::nanoseconds>(LIFETIME * t));
Junxiao Shib2831282015-01-09 14:55:58 -0700135 }
136
Junxiao Shi0e42c572014-10-05 20:33:48 -0700137protected:
Davide Pesaventod4123932021-05-28 18:48:37 -0400138 static constexpr time::nanoseconds LIFETIME = 200_ms;
139 static constexpr time::nanoseconds ADD_INTERVAL = LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT;
140
141 DeadNonceList dnl{LIFETIME};
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400142 Name name = "/N";
143 uint32_t lastNonce = 0;
144 size_t addNonceBatch = 0;
Junxiao Shib2831282015-01-09 14:55:58 -0700145 scheduler::ScopedEventId addNonceEvent;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700146};
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400147
Junxiao Shi0e42c572014-10-05 20:33:48 -0700148BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture)
149{
Junxiao Shia110f262014-10-12 12:35:20 -0700150 BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME);
151
Junxiao Shi0e42c572014-10-05 20:33:48 -0700152 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
153 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700154 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700155
156 Name nameC("ndn:/C");
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400157 const Interest::Nonce nonceC(0x25390656);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700158 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
159 dnl.add(nameC, nonceC);
160 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
161
Junxiao Shib2831282015-01-09 14:55:58 -0700162 this->advanceClocksByLifetime(0.5); // -50%, entry should exist
Junxiao Shi0e42c572014-10-05 20:33:48 -0700163 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
164
Junxiao Shib2831282015-01-09 14:55:58 -0700165 this->advanceClocksByLifetime(1.0); // +50%, entry should be gone
Junxiao Shi0e42c572014-10-05 20:33:48 -0700166 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
167}
168
169BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
170{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700171 ssize_t cap0 = dnl.m_capacity;
172
173 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
174 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700175 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700176
177 ssize_t cap1 = dnl.m_capacity;
178 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
179}
180
181BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
182{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700183 ssize_t cap0 = dnl.m_capacity;
184
185 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
186 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700187 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700188
189 ssize_t cap1 = dnl.m_capacity;
190 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
191}
192
Junxiao Shi3978c922016-07-15 18:24:11 +0000193BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList
194BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi0e42c572014-10-05 20:33:48 -0700195
196} // namespace tests
197} // namespace nfd