blob: 655762e514e2ae6ec59cb37c2108204abc476396 [file] [log] [blame]
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento5f5101a2022-03-11 20:05:03 -05003 * Copyright (c) 2012-2022 University of California, Los Angeles
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07004 *
5 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
7 *
8 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "interest-table.hpp"
Davide Pesaventofae9def2019-01-29 14:34:33 -050021
22#include "tests/boost-test.hpp"
23#include "tests/unit-test-time-fixture.hpp"
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070024
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070025#include <unistd.h>
26
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070027namespace chronosync {
28namespace test {
29
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080030class InterestTableFixture : public ndn::tests::UnitTestTimeFixture
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070031{
32public:
33 InterestTableFixture()
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070034 {
Davide Pesavento5f5101a2022-03-11 20:05:03 -050035 const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070036 Name prefix("/test/prefix");
37
38 Name interestName1;
Davide Pesavento5f5101a2022-03-11 20:05:03 -050039 digest1 = ndn::util::Sha256::computeDigest({origin, 1});
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070040 interestName1.append(prefix).append(name::Component(digest1));
Ashlesh Gawande08784d42017-09-06 23:40:21 -050041
42 interest1 = Interest(interestName1);
43 interest1.setInterestLifetime(time::milliseconds(100));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070044
45 Name interestName2;
Davide Pesavento5f5101a2022-03-11 20:05:03 -050046 digest2 = ndn::util::Sha256::computeDigest({origin, 2});
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070047 interestName2.append(prefix).append(name::Component(digest2));
Ashlesh Gawande08784d42017-09-06 23:40:21 -050048 interest2 = Interest(interestName2);
49 interest2.setInterestLifetime(time::milliseconds(100));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070050
51 Name interestName3;
Davide Pesavento5f5101a2022-03-11 20:05:03 -050052 digest3 = ndn::util::Sha256::computeDigest({origin, 3});
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070053 interestName3.append(prefix).append(name::Component(digest3));
Ashlesh Gawande08784d42017-09-06 23:40:21 -050054 interest3 = Interest(interestName3);
55 interest3.setInterestLifetime(time::milliseconds(100));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070056 }
57
58 void
Ashlesh Gawande08784d42017-09-06 23:40:21 -050059 insert(InterestTable& table, const Interest& interest, ndn::ConstBufferPtr digest)
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070060 {
61 table.insert(interest, digest);
62 }
63
Ashlesh Gawande08784d42017-09-06 23:40:21 -050064 Interest interest1;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070065 ndn::ConstBufferPtr digest1;
66
Ashlesh Gawande08784d42017-09-06 23:40:21 -050067 Interest interest2;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070068 ndn::ConstBufferPtr digest2;
69
Ashlesh Gawande08784d42017-09-06 23:40:21 -050070 Interest interest3;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070071 ndn::ConstBufferPtr digest3;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070072};
73
74BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture)
75
76BOOST_AUTO_TEST_CASE(Container)
77{
78 InterestContainer container;
79
Yingdi Yu906c2ea2014-10-31 11:24:50 -070080 container.insert(make_shared<UnsatisfiedInterest>(interest1, digest1));
81 container.insert(make_shared<UnsatisfiedInterest>(interest2, digest2));
82 container.insert(make_shared<UnsatisfiedInterest>(interest3, digest3));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070083
84 BOOST_CHECK_EQUAL(container.size(), 3);
85 BOOST_CHECK(container.find(digest3) != container.end());
86 BOOST_CHECK(container.find(digest2) != container.end());
87 BOOST_CHECK(container.find(digest1) != container.end());
88}
89
90BOOST_AUTO_TEST_CASE(Basic)
91{
92 InterestTable table(io);
93
94 table.insert(interest1, digest1);
95 table.insert(interest2, digest2);
96 table.insert(interest3, digest3);
97
98 BOOST_CHECK_EQUAL(table.size(), 3);
99 InterestTable::const_iterator it = table.begin();
100 BOOST_CHECK(it != table.end());
101 it++;
102 BOOST_CHECK(it != table.end());
103 it++;
104 BOOST_CHECK(it != table.end());
105 it++;
106 BOOST_CHECK(it == table.end());
107
108 BOOST_CHECK_EQUAL(table.size(), 3);
109 table.erase(digest1);
110 BOOST_CHECK_EQUAL(table.size(), 2);
111 table.erase(digest2);
112 BOOST_CHECK_EQUAL(table.size(), 1);
113 ConstUnsatisfiedInterestPtr pendingInterest = *table.begin();
114 table.clear();
115 BOOST_CHECK_EQUAL(table.size(), 0);
116 BOOST_CHECK(*pendingInterest->digest == *digest3);
117}
118
119BOOST_AUTO_TEST_CASE(Expire)
120{
121 InterestTable table(io);
122
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800123 insert(table, interest1, digest1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700124
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800125 advanceClocks(ndn::time::milliseconds(10), 10);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700126
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800127 insert(table, interest2, digest2);
128 insert(table, interest3, digest3);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700129
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800130 advanceClocks(ndn::time::milliseconds(10), 5);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700131
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800132 insert(table, interest2, digest2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700133
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800134 advanceClocks(ndn::time::milliseconds(10), 2);
135 BOOST_CHECK_EQUAL(table.size(), 2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700136
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800137 advanceClocks(ndn::time::milliseconds(10), 5);
138 BOOST_CHECK_EQUAL(table.size(), 1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700139
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800140 advanceClocks(ndn::time::milliseconds(10), 15);
141 BOOST_CHECK_EQUAL(table.size(), 0);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700142}
143
144BOOST_AUTO_TEST_SUITE_END()
145
146} // namespace test
147} // namespace chronosync