blob: ed883733bdd7cc1e646927eb78a7bf514ac1fefa [file] [log] [blame]
Yingdi Yud514c172014-08-26 21:49:39 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2014 University of California, Los Angeles
4 *
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"
21#include "boost-test.hpp"
22
23namespace chronosync {
24namespace test {
25
26using boost::tuple;
27
28BOOST_AUTO_TEST_SUITE(StateTests)
29
30BOOST_AUTO_TEST_CASE(Basic)
31{
32 BOOST_CHECK_NO_THROW(State());
33 State state;
34 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
35
36 Name info("/test/name");
37 info.appendNumber(0);
38
39 BOOST_CHECK_NO_THROW(state.update(info, 12));
40
41 BOOST_CHECK_NO_THROW(state.reset());
42 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
43
44 tuple<bool, bool, SeqNo> result;
45 result = state.update(info, 12);
46 BOOST_CHECK_EQUAL(result.get<0>(), true);
47 BOOST_CHECK_EQUAL(result.get<1>(), false);
48 BOOST_CHECK_EQUAL(result.get<2>(), 0);
49
50 BOOST_CHECK_NO_THROW(state.update(info, 12));
51 result = state.update(info, 12);
52 BOOST_CHECK_EQUAL(result.get<0>(), false);
53 BOOST_CHECK_EQUAL(result.get<1>(), false);
54 BOOST_CHECK_EQUAL(result.get<2>(), 0);
55
56 BOOST_CHECK_NO_THROW(state.update(info, 11));
57 result = state.update(info, 11);
58 BOOST_CHECK_EQUAL(result.get<0>(), false);
59 BOOST_CHECK_EQUAL(result.get<1>(), false);
60 BOOST_CHECK_EQUAL(result.get<2>(), 0);
61
62 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
63 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 12);
64
65 BOOST_CHECK_NO_THROW(state.update(info, 13));
66 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
67 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 13);
68
69 result = state.update(info, 14);
70 BOOST_CHECK_EQUAL(result.get<0>(), false);
71 BOOST_CHECK_EQUAL(result.get<1>(), true);
72 BOOST_CHECK_EQUAL(result.get<2>(), 13);
73
74 BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
75 BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 14);
76
77 Name info2("/test/name");
78 info2.appendNumber(1);
79 BOOST_CHECK_NO_THROW(state.update(info2, 3));
80 BOOST_CHECK_EQUAL(state.getLeaves().size(), 2);
81}
82
83BOOST_AUTO_TEST_CASE(StateDigest)
84{
85 State state;
86 BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
87
88 Name info1("/test/name");
89 info1.appendNumber(0);
90
91 Name info2("/test/name");
92 info2.appendNumber(1);
93
94 Name info3("/test/mane");
95 info3.appendNumber(0);
96
97 state.update(info1, 10);
98 ndn::ConstBufferPtr digest1 = state.getRootDigest();
99
100 state.update(info2, 12);
101 ndn::ConstBufferPtr digest2 = state.getRootDigest();
102
103 state.update(info3, 8);
104 ndn::ConstBufferPtr digest3 = state.getRootDigest();
105
106 BOOST_CHECK(*digest1 != *digest2);
107 BOOST_CHECK(*digest2 != *digest3);
108 BOOST_CHECK(*digest1 != *digest3);
109
110 state.reset();
111
112 state.update(info1, 10);
113 ndn::ConstBufferPtr digest4 = state.getRootDigest();
114
115 state.update(info3, 8);
116 ndn::ConstBufferPtr digest5 = state.getRootDigest();
117
118 state.update(info2, 12);
119 ndn::ConstBufferPtr digest6 = state.getRootDigest();
120
121 BOOST_CHECK(*digest4 == *digest1);
122 BOOST_CHECK(*digest5 != *digest2);
123 BOOST_CHECK(*digest6 == *digest3);
124}
125
126BOOST_AUTO_TEST_CASE(DecodeEncode)
127{
128 const uint8_t wire[] = {
129 0x80, 0x2c, // SyncReply
130 0x81, 0x14, // StateLeaf
131 0x07, 0x0f, // Name: /test/name/[0]
132 0x08, 0x04,
133 0x74, 0x65, 0x73, 0x74,
134 0x08, 0x04,
135 0x6e, 0x61, 0x6d, 0x65,
136 0x08, 0x01,
137 0x00,
138 0x82, 0x1, // SeqNo: 14
139 0x0e,
140 0x81, 0x14, // StateLeaf
141 0x07, 0x0f, // Name: /test/name/[1]
142 0x08, 0x04,
143 0x74, 0x65, 0x73, 0x74,
144 0x08, 0x04,
145 0x6e, 0x61, 0x6d, 0x65,
146 0x08, 0x01,
147 0x01,
148 0x82, 0x1, // SeqNo: 4
149 0x04
150 };
151
152 Block block(wire, sizeof(wire));
153 State state;
154 BOOST_REQUIRE_NO_THROW(state.wireDecode(block));
155
156 BOOST_CHECK_EQUAL(state.getLeaves().size(), 2);
157 LeafContainer::index<ordered>::type::iterator it = state.getLeaves().get<ordered>().begin();
158 BOOST_CHECK_EQUAL((*it)->getSeq(), 14);
159 it++;
160 BOOST_CHECK_EQUAL((*it)->getSeq(), 4);
161
162
163 State state2;
164
165 Name info1("/test/name");
166 info1.appendNumber(0);
167 state2.update(info1, 14);
168
169 Name info2("/test/name");
170 info2.appendNumber(1);
171 state2.update(info2, 4);
172
173 BOOST_REQUIRE_NO_THROW(state2.wireEncode());
174 Block block2 = state2.wireEncode();
175
176 BOOST_CHECK_EQUAL_COLLECTIONS(block.wire(),
177 block.wire() + block.size(),
178 block2.wire(),
179 block2.wire() + block2.size());
180
181 BOOST_CHECK(*state.getRootDigest() == *state2.getRootDigest());
182}
183
184BOOST_AUTO_TEST_CASE(Combine)
185{
186 State state1;
187 State state2;
188
189 Name info1("/test/name");
190 info1.appendNumber(0);
191
192 Name info2("/test/name");
193 info2.appendNumber(1);
194
195 Name info3("/test/name");
196 info3.appendNumber(2);
197
198 state1.update(info1, 4);
199 state1.update(info2, 14);
200
201 state2.update(info2, 15);
202 state2.update(info3, 25);
203
204 BOOST_CHECK_EQUAL(state1.getLeaves().size(), 2);
205 BOOST_CHECK_EQUAL(state2.getLeaves().size(), 2);
206
207 BOOST_REQUIRE_NO_THROW(state2 += state1);
208
209 BOOST_CHECK_EQUAL(state1.getLeaves().size(), 2);
210 BOOST_CHECK_EQUAL(state2.getLeaves().size(), 3);
211
212 LeafContainer::index<ordered>::type::iterator it = state2.getLeaves().get<ordered>().begin();
213 BOOST_CHECK_EQUAL((*it)->getSeq(), 4);
214 it++;
215 BOOST_CHECK_EQUAL((*it)->getSeq(), 15);
216 it++;
217 BOOST_CHECK_EQUAL((*it)->getSeq(), 25);
218}
219
220BOOST_AUTO_TEST_SUITE_END()
221
222} // namespace test
223} // namespace chronosync