blob: a302f01eb2b0004177097d404342906beeff6cfb [file] [log] [blame]
Yingdi Yud514c172014-08-26 21:49:39 -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 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
24namespace chronosync {
25namespace test {
26
Yingdi Yud514c172014-08-26 21:49:39 -070027BOOST_AUTO_TEST_SUITE(StateTests)
28
29BOOST_AUTO_TEST_CASE(Basic)
30{
31 BOOST_CHECK_NO_THROW(State());
32 State state;
33 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
34
35 Name info("/test/name");
36 info.appendNumber(0);
37
38 BOOST_CHECK_NO_THROW(state.update(info, 12));
39
40 BOOST_CHECK_NO_THROW(state.reset());
41 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
42
Davide Pesaventofae9def2019-01-29 14:34:33 -050043 std::tuple<bool, bool, SeqNo> result;
Yingdi Yud514c172014-08-26 21:49:39 -070044 result = state.update(info, 12);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050045 BOOST_CHECK_EQUAL(std::get<0>(result), true);
46 BOOST_CHECK_EQUAL(std::get<1>(result), false);
47 BOOST_CHECK_EQUAL(std::get<2>(result), 0);
Yingdi Yud514c172014-08-26 21:49:39 -070048
49 BOOST_CHECK_NO_THROW(state.update(info, 12));
50 result = state.update(info, 12);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050051 BOOST_CHECK_EQUAL(std::get<0>(result), false);
52 BOOST_CHECK_EQUAL(std::get<1>(result), false);
53 BOOST_CHECK_EQUAL(std::get<2>(result), 0);
Yingdi Yud514c172014-08-26 21:49:39 -070054
55 BOOST_CHECK_NO_THROW(state.update(info, 11));
56 result = state.update(info, 11);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050057 BOOST_CHECK_EQUAL(std::get<0>(result), false);
58 BOOST_CHECK_EQUAL(std::get<1>(result), false);
59 BOOST_CHECK_EQUAL(std::get<2>(result), 0);
Yingdi Yud514c172014-08-26 21:49:39 -070060
61 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
62 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 12);
63
64 BOOST_CHECK_NO_THROW(state.update(info, 13));
65 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
66 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 13);
67
68 result = state.update(info, 14);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050069 BOOST_CHECK_EQUAL(std::get<0>(result), false);
70 BOOST_CHECK_EQUAL(std::get<1>(result), true);
71 BOOST_CHECK_EQUAL(std::get<2>(result), 13);
Yingdi Yud514c172014-08-26 21:49:39 -070072
73 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
74 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 14);
75
76 Name info2("/test/name");
77 info2.appendNumber(1);
78 BOOST_CHECK_NO_THROW(state.update(info2, 3));
79 BOOST_CHECK_EQUAL(state.getLeaves().size(), 2);
80}
81
82BOOST_AUTO_TEST_CASE(StateDigest)
83{
84 State state;
85 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
86
87 Name info1("/test/name");
88 info1.appendNumber(0);
89
90 Name info2("/test/name");
91 info2.appendNumber(1);
92
93 Name info3("/test/mane");
94 info3.appendNumber(0);
95
96 state.update(info1, 10);
97 ndn::ConstBufferPtr digest1 = state.getRootDigest();
98
99 state.update(info2, 12);
100 ndn::ConstBufferPtr digest2 = state.getRootDigest();
101
102 state.update(info3, 8);
103 ndn::ConstBufferPtr digest3 = state.getRootDigest();
104
105 BOOST_CHECK(*digest1 != *digest2);
106 BOOST_CHECK(*digest2 != *digest3);
107 BOOST_CHECK(*digest1 != *digest3);
108
109 state.reset();
110
111 state.update(info1, 10);
112 ndn::ConstBufferPtr digest4 = state.getRootDigest();
113
114 state.update(info3, 8);
115 ndn::ConstBufferPtr digest5 = state.getRootDigest();
116
117 state.update(info2, 12);
118 ndn::ConstBufferPtr digest6 = state.getRootDigest();
119
120 BOOST_CHECK(*digest4 == *digest1);
121 BOOST_CHECK(*digest5 != *digest2);
122 BOOST_CHECK(*digest6 == *digest3);
123}
124
125BOOST_AUTO_TEST_CASE(DecodeEncode)
126{
127 const uint8_t wire[] = {
128 0x80, 0x2c, // SyncReply
129 0x81, 0x14, // StateLeaf
130 0x07, 0x0f, // Name: /test/name/[0]
131 0x08, 0x04,
132 0x74, 0x65, 0x73, 0x74,
133 0x08, 0x04,
134 0x6e, 0x61, 0x6d, 0x65,
135 0x08, 0x01,
136 0x00,
137 0x82, 0x1, // SeqNo: 14
138 0x0e,
139 0x81, 0x14, // StateLeaf
140 0x07, 0x0f, // Name: /test/name/[1]
141 0x08, 0x04,
142 0x74, 0x65, 0x73, 0x74,
143 0x08, 0x04,
144 0x6e, 0x61, 0x6d, 0x65,
145 0x08, 0x01,
146 0x01,
147 0x82, 0x1, // SeqNo: 4
148 0x04
149 };
150
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500151 Block block(wire);
Yingdi Yud514c172014-08-26 21:49:39 -0700152 State state;
153 BOOST_REQUIRE_NO_THROW(state.wireDecode(block));
154
155 BOOST_CHECK_EQUAL(state.getLeaves().size(), 2);
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500156 auto it = state.getLeaves().get<ordered>().begin();
Yingdi Yud514c172014-08-26 21:49:39 -0700157 BOOST_CHECK_EQUAL((*it)->getSeq(), 14);
158 it++;
159 BOOST_CHECK_EQUAL((*it)->getSeq(), 4);
160
Yingdi Yud514c172014-08-26 21:49:39 -0700161 State state2;
162
163 Name info1("/test/name");
164 info1.appendNumber(0);
165 state2.update(info1, 14);
166
167 Name info2("/test/name");
168 info2.appendNumber(1);
169 state2.update(info2, 4);
170
Yingdi Yud514c172014-08-26 21:49:39 -0700171 Block block2 = state2.wireEncode();
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500172 BOOST_TEST(block == block2, boost::test_tools::per_element());
173 BOOST_TEST(*state.getRootDigest() == *state2.getRootDigest(), boost::test_tools::per_element());
Yingdi Yud514c172014-08-26 21:49:39 -0700174}
175
176BOOST_AUTO_TEST_CASE(Combine)
177{
178 State state1;
179 State state2;
180
181 Name info1("/test/name");
182 info1.appendNumber(0);
183
184 Name info2("/test/name");
185 info2.appendNumber(1);
186
187 Name info3("/test/name");
188 info3.appendNumber(2);
189
190 state1.update(info1, 4);
191 state1.update(info2, 14);
192
193 state2.update(info2, 15);
194 state2.update(info3, 25);
195
196 BOOST_CHECK_EQUAL(state1.getLeaves().size(), 2);
197 BOOST_CHECK_EQUAL(state2.getLeaves().size(), 2);
198
199 BOOST_REQUIRE_NO_THROW(state2 += state1);
200
201 BOOST_CHECK_EQUAL(state1.getLeaves().size(), 2);
202 BOOST_CHECK_EQUAL(state2.getLeaves().size(), 3);
203
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500204 auto it = state2.getLeaves().get<ordered>().begin();
Yingdi Yud514c172014-08-26 21:49:39 -0700205 BOOST_CHECK_EQUAL((*it)->getSeq(), 4);
206 it++;
207 BOOST_CHECK_EQUAL((*it)->getSeq(), 15);
208 it++;
209 BOOST_CHECK_EQUAL((*it)->getSeq(), 25);
210}
211
212BOOST_AUTO_TEST_SUITE_END()
213
214} // namespace test
215} // namespace chronosync