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