blob: 5553a05421af67be412789fd1cbff4f9c023e838 [file] [log] [blame]
Yingdi Yudea99be2014-08-15 10:45:43 -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
Yingdi Yu274a2272014-08-26 19:25:16 -070020
Yingdi Yudea99be2014-08-15 10:45:43 -070021#include "leaf.hpp"
Yingdi Yu274a2272014-08-26 19:25:16 -070022#include "leaf-container.hpp"
Yingdi Yudea99be2014-08-15 10:45:43 -070023#include <ndn-cxx/encoding/buffer-stream.hpp>
24
25#include "boost-test.hpp"
26
27
28namespace chronosync {
29namespace test {
30
31BOOST_AUTO_TEST_SUITE(LeafTests)
32
33BOOST_AUTO_TEST_CASE(LeafBasic)
34{
35 Name userPrefix("/test/name");
36 BOOST_CHECK_NO_THROW(Leaf leaf(userPrefix, 1, 10));
37
38 Leaf leaf(userPrefix, 1, 10);
39 Name sessionName = userPrefix;
40 sessionName.appendNumber(1);
41 BOOST_CHECK_EQUAL(leaf.getSessionName(), sessionName);
42 BOOST_CHECK_EQUAL(leaf.getSeq(), 10);
43
44 leaf.setSeq(9);
45 BOOST_CHECK_EQUAL(leaf.getSeq(), 10);
46 leaf.setSeq(11);
47 BOOST_CHECK_EQUAL(leaf.getSeq(), 11);
48}
49
50BOOST_AUTO_TEST_CASE(LeafDigest)
51{
52 using namespace CryptoPP;
53
54 std::string hexResult = "05fe7f728d3341e9eff82526277b02171044124d0a52e8c4610982261c20de2b";
55 ndn::OBufferStream os;
56 StringSource(hexResult, true, new HexDecoder(new FileSink(os)));
57 ndn::ConstBufferPtr result = os.buf();
58
59 Name userPrefix("/test/name");
60 Leaf leaf(userPrefix, 1, 10);
61
62 BOOST_CHECK_NO_THROW(leaf.getDigest());
63
64 ndn::ConstBufferPtr digest = leaf.getDigest();
65 BOOST_CHECK(*result == *digest);
66}
67
Yingdi Yu274a2272014-08-26 19:25:16 -070068BOOST_AUTO_TEST_CASE(Container)
69{
70 LeafPtr leaf1 = make_shared<Leaf>(Name("/test/name"), 1, 10);
71 LeafPtr leaf2 = make_shared<Leaf>(Name("/test/name"), 2, 10);
72
73 LeafContainer container;
74
75 container.insert(leaf1);
76 container.insert(leaf2);
77
78 Name idx1("/test/name");
79 idx1.appendNumber(1);
80
81 Name idx2("/test/name");
82 idx2.appendNumber(2);
83
84 Name idx3("/test/name");
85 idx3.appendNumber(3);
86
87 Name idx4("/test/mane");
88 idx4.appendNumber(4);
89
90 LeafContainer::index<hashed>::type& hashedIndex = container.get<hashed>();
91
92 BOOST_CHECK_EQUAL(container.get<ordered>().size(), 2);
93 BOOST_CHECK_EQUAL(container.get<hashed>().size(), 2);
94 BOOST_CHECK(container.find(idx1) != container.end());
95 BOOST_CHECK(container.find(idx2) != container.end());
96 BOOST_CHECK(container.find(idx3) == container.end());
97
98 BOOST_CHECK(hashedIndex.find(idx1) != hashedIndex.end());
99 BOOST_CHECK(hashedIndex.find(idx2) != hashedIndex.end());
100 BOOST_CHECK(hashedIndex.find(idx3) == hashedIndex.end());
101
102 LeafPtr leaf3 = make_shared<Leaf>(Name("/test/mane"), 3, 10);
103 container.insert(leaf3);
104
105 Name idx0("/test/mane");
106 idx0.appendNumber(3);
107
108 LeafContainer::index<ordered>::type::iterator it = container.get<ordered>().begin();
109 BOOST_CHECK_EQUAL((*it)->getSessionName(), idx0);
110 it++;
111 BOOST_REQUIRE(it != container.get<ordered>().end());
112 BOOST_CHECK_EQUAL((*it)->getSessionName(), idx1);
113 it++;
114 BOOST_REQUIRE(it != container.get<ordered>().end());
115 BOOST_CHECK_EQUAL((*it)->getSessionName(), idx2);
116
117
118 BOOST_CHECK(hashedIndex.find(idx0) != hashedIndex.end());
119 BOOST_CHECK(hashedIndex.find(idx1) != hashedIndex.end());
120 BOOST_CHECK(hashedIndex.find(idx2) != hashedIndex.end());
121 BOOST_CHECK(hashedIndex.find(idx4) == hashedIndex.end());
122}
123
Yingdi Yudea99be2014-08-15 10:45:43 -0700124BOOST_AUTO_TEST_SUITE_END()
125
126} // namespace test
127} // namespace chronosync