blob: 2d64b135846325417c0d8063e790f40034f2640a [file] [log] [blame]
Yingdi Yud514c172014-08-26 21:49:39 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento30c41ec2024-02-12 17:36:35 -05003 * Copyright (c) 2012-2024 University of California, Los Angeles
Yingdi Yud514c172014-08-26 21:49:39 -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 "state.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050021
Davide Pesaventofae9def2019-01-29 14:34:33 -050022#include "tests/boost-test.hpp"
Yingdi Yud514c172014-08-26 21:49:39 -070023
Davide Pesavento30c41ec2024-02-12 17:36:35 -050024namespace chronosync::tests {
Yingdi Yud514c172014-08-26 21:49:39 -070025
Yingdi Yud514c172014-08-26 21:49:39 -070026BOOST_AUTO_TEST_SUITE(StateTests)
27
28BOOST_AUTO_TEST_CASE(Basic)
29{
30 BOOST_CHECK_NO_THROW(State());
31 State state;
32 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
33
34 Name info("/test/name");
35 info.appendNumber(0);
36
37 BOOST_CHECK_NO_THROW(state.update(info, 12));
38
39 BOOST_CHECK_NO_THROW(state.reset());
40 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
41
Davide Pesaventofae9def2019-01-29 14:34:33 -050042 std::tuple<bool, bool, SeqNo> result;
Yingdi Yud514c172014-08-26 21:49:39 -070043 result = state.update(info, 12);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050044 BOOST_CHECK_EQUAL(std::get<0>(result), true);
45 BOOST_CHECK_EQUAL(std::get<1>(result), false);
46 BOOST_CHECK_EQUAL(std::get<2>(result), 0);
Yingdi Yud514c172014-08-26 21:49:39 -070047
48 BOOST_CHECK_NO_THROW(state.update(info, 12));
49 result = state.update(info, 12);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050050 BOOST_CHECK_EQUAL(std::get<0>(result), false);
51 BOOST_CHECK_EQUAL(std::get<1>(result), false);
52 BOOST_CHECK_EQUAL(std::get<2>(result), 0);
Yingdi Yud514c172014-08-26 21:49:39 -070053
54 BOOST_CHECK_NO_THROW(state.update(info, 11));
55 result = state.update(info, 11);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050056 BOOST_CHECK_EQUAL(std::get<0>(result), false);
57 BOOST_CHECK_EQUAL(std::get<1>(result), false);
58 BOOST_CHECK_EQUAL(std::get<2>(result), 0);
Yingdi Yud514c172014-08-26 21:49:39 -070059
60 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
61 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 12);
62
63 BOOST_CHECK_NO_THROW(state.update(info, 13));
64 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
65 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 13);
66
67 result = state.update(info, 14);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050068 BOOST_CHECK_EQUAL(std::get<0>(result), false);
69 BOOST_CHECK_EQUAL(std::get<1>(result), true);
70 BOOST_CHECK_EQUAL(std::get<2>(result), 13);
Yingdi Yud514c172014-08-26 21:49:39 -070071
72 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
73 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 14);
74
75 Name info2("/test/name");
76 info2.appendNumber(1);
77 BOOST_CHECK_NO_THROW(state.update(info2, 3));
78 BOOST_CHECK_EQUAL(state.getLeaves().size(), 2);
79}
80
81BOOST_AUTO_TEST_CASE(StateDigest)
82{
83 State state;
84 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
85
86 Name info1("/test/name");
87 info1.appendNumber(0);
88
89 Name info2("/test/name");
90 info2.appendNumber(1);
91
92 Name info3("/test/mane");
93 info3.appendNumber(0);
94
95 state.update(info1, 10);
96 ndn::ConstBufferPtr digest1 = state.getRootDigest();
97
98 state.update(info2, 12);
99 ndn::ConstBufferPtr digest2 = state.getRootDigest();
100
101 state.update(info3, 8);
102 ndn::ConstBufferPtr digest3 = state.getRootDigest();
103
104 BOOST_CHECK(*digest1 != *digest2);
105 BOOST_CHECK(*digest2 != *digest3);
106 BOOST_CHECK(*digest1 != *digest3);
107
108 state.reset();
109
110 state.update(info1, 10);
111 ndn::ConstBufferPtr digest4 = state.getRootDigest();
112
113 state.update(info3, 8);
114 ndn::ConstBufferPtr digest5 = state.getRootDigest();
115
116 state.update(info2, 12);
117 ndn::ConstBufferPtr digest6 = state.getRootDigest();
118
119 BOOST_CHECK(*digest4 == *digest1);
120 BOOST_CHECK(*digest5 != *digest2);
121 BOOST_CHECK(*digest6 == *digest3);
122}
123
124BOOST_AUTO_TEST_CASE(DecodeEncode)
125{
126 const uint8_t wire[] = {
127 0x80, 0x2c, // SyncReply
128 0x81, 0x14, // StateLeaf
129 0x07, 0x0f, // Name: /test/name/[0]
130 0x08, 0x04,
131 0x74, 0x65, 0x73, 0x74,
132 0x08, 0x04,
133 0x6e, 0x61, 0x6d, 0x65,
134 0x08, 0x01,
135 0x00,
136 0x82, 0x1, // SeqNo: 14
137 0x0e,
138 0x81, 0x14, // StateLeaf
139 0x07, 0x0f, // Name: /test/name/[1]
140 0x08, 0x04,
141 0x74, 0x65, 0x73, 0x74,
142 0x08, 0x04,
143 0x6e, 0x61, 0x6d, 0x65,
144 0x08, 0x01,
145 0x01,
146 0x82, 0x1, // SeqNo: 4
147 0x04
148 };
149
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500150 Block block(wire);
Yingdi Yud514c172014-08-26 21:49:39 -0700151 State state;
152 BOOST_REQUIRE_NO_THROW(state.wireDecode(block));
153
154 BOOST_CHECK_EQUAL(state.getLeaves().size(), 2);
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500155 auto it = state.getLeaves().get<ordered>().begin();
Yingdi Yud514c172014-08-26 21:49:39 -0700156 BOOST_CHECK_EQUAL((*it)->getSeq(), 14);
157 it++;
158 BOOST_CHECK_EQUAL((*it)->getSeq(), 4);
159
Yingdi Yud514c172014-08-26 21:49:39 -0700160 State state2;
161
162 Name info1("/test/name");
163 info1.appendNumber(0);
164 state2.update(info1, 14);
165
166 Name info2("/test/name");
167 info2.appendNumber(1);
168 state2.update(info2, 4);
169
Yingdi Yud514c172014-08-26 21:49:39 -0700170 Block block2 = state2.wireEncode();
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500171 BOOST_TEST(block == block2, boost::test_tools::per_element());
172 BOOST_TEST(*state.getRootDigest() == *state2.getRootDigest(), boost::test_tools::per_element());
Yingdi Yud514c172014-08-26 21:49:39 -0700173}
174
175BOOST_AUTO_TEST_CASE(Combine)
176{
177 State state1;
178 State state2;
179
180 Name info1("/test/name");
181 info1.appendNumber(0);
182
183 Name info2("/test/name");
184 info2.appendNumber(1);
185
186 Name info3("/test/name");
187 info3.appendNumber(2);
188
189 state1.update(info1, 4);
190 state1.update(info2, 14);
191
192 state2.update(info2, 15);
193 state2.update(info3, 25);
194
195 BOOST_CHECK_EQUAL(state1.getLeaves().size(), 2);
196 BOOST_CHECK_EQUAL(state2.getLeaves().size(), 2);
197
198 BOOST_REQUIRE_NO_THROW(state2 += state1);
199
200 BOOST_CHECK_EQUAL(state1.getLeaves().size(), 2);
201 BOOST_CHECK_EQUAL(state2.getLeaves().size(), 3);
202
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500203 auto it = state2.getLeaves().get<ordered>().begin();
Yingdi Yud514c172014-08-26 21:49:39 -0700204 BOOST_CHECK_EQUAL((*it)->getSeq(), 4);
205 it++;
206 BOOST_CHECK_EQUAL((*it)->getSeq(), 15);
207 it++;
208 BOOST_CHECK_EQUAL((*it)->getSeq(), 25);
209}
210
211BOOST_AUTO_TEST_SUITE_END()
212
Davide Pesavento30c41ec2024-02-12 17:36:35 -0500213} // namespace chronosync::tests