blob: 23542b237a17f1d2ff1ad082dc2fca6048d59705 [file] [log] [blame]
Yingdi Yu0c3e5912015-03-17 14:22:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
Yingdi Yu0c3e5912015-03-17 14:22:38 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * This file is part of NDN DeLorean, An Authentication System for Data Archives in
6 * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors
7 * and contributors.
Yingdi Yu0c3e5912015-03-17 14:22:38 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * NDN DeLorean is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
Yingdi Yu0c3e5912015-03-17 14:22:38 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Yingdi Yu0c3e5912015-03-17 14:22:38 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * You should have received a copy of the GNU General Public License along with NDN
19 * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu0c3e5912015-03-17 14:22:38 -070020 */
21
22#include "node.hpp"
23#include "cryptopp.hpp"
24
25#include <ndn-cxx/encoding/buffer-stream.hpp>
26#include "boost-test.hpp"
27
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070028namespace ndn {
29namespace delorean {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070030namespace tests {
31
32BOOST_AUTO_TEST_SUITE(TestNode)
33
34BOOST_AUTO_TEST_CASE(IndexTest1)
35{
36 Node::Index idx(0, 0);
37
38 BOOST_CHECK_EQUAL(idx.seqNo, 0);
39 BOOST_CHECK_EQUAL(idx.level, 0);
40 BOOST_CHECK_EQUAL(idx.range, 1);
41
42 Node::Index idx2(0, 1);
43 BOOST_CHECK_EQUAL(idx2.seqNo, 0);
44 BOOST_CHECK_EQUAL(idx2.level, 1);
45 BOOST_CHECK_EQUAL(idx2.range, 2);
46
47 Node::Index idx3(2, 1);
48 BOOST_CHECK_EQUAL(idx3.seqNo, 2);
49 BOOST_CHECK_EQUAL(idx3.level, 1);
50 BOOST_CHECK_EQUAL(idx3.range, 2);
51
52 Node::Index idx4(4, 2);
53 BOOST_CHECK_EQUAL(idx4.seqNo, 4);
54 BOOST_CHECK_EQUAL(idx4.level, 2);
55 BOOST_CHECK_EQUAL(idx4.range, 4);
56
57 BOOST_CHECK_THROW(Node::Index(1, 1), Node::Error);
58 BOOST_CHECK_THROW(Node::Index(2, 2), Node::Error);
59}
60
61BOOST_AUTO_TEST_CASE(IndexTest2)
62{
63 Node::Index idx1(0, 0);
64 Node::Index idx2(0, 1);
65 Node::Index idx3(2, 0);
66 Node::Index idx4(2, 1);
67
68 BOOST_CHECK(idx1 < idx2);
69 BOOST_CHECK(idx1 < idx3);
70 BOOST_CHECK(idx1 < idx4);
71 BOOST_CHECK(idx2 < idx3);
72 BOOST_CHECK(idx2 < idx4);
73 BOOST_CHECK(idx3 < idx4);
74
75 BOOST_CHECK(idx1 == idx1);
76 BOOST_CHECK_EQUAL(idx1 == idx2, false);
77 BOOST_CHECK_EQUAL(idx1 == idx3, false);
78 BOOST_CHECK_EQUAL(idx1 == idx4, false);
79}
80
81BOOST_AUTO_TEST_CASE(NodeTest1)
82{
83 std::string hash("ABCDEFGHIJKLMNOPabcdefghijklmno");
84 auto buffer = make_shared<const ndn::Buffer>(hash.c_str(), hash.size());
85
86 Node node(0, 0);
87 BOOST_CHECK(node.getIndex() == Node::Index(0, 0));
88 BOOST_CHECK(!node.isFull());
89 BOOST_CHECK_EQUAL(node.getLeafSeqNo(), 0);
90 BOOST_CHECK(node.getHash() == nullptr);
91
92 node.setLeafSeqNo(1);
93 BOOST_CHECK(node.isFull());
94 BOOST_CHECK_EQUAL(node.getLeafSeqNo(), 1);
95
96 Node node2(2, 1);
97 BOOST_CHECK(!node2.isFull());
98 BOOST_CHECK_EQUAL(node2.getLeafSeqNo(), 2);
99 BOOST_CHECK(node2.getHash() == nullptr);
100
101 Node node3(2, 1, 4);
102 BOOST_CHECK(node3.isFull());
103 BOOST_CHECK_EQUAL(node3.getLeafSeqNo(), 4);
104 BOOST_CHECK(node3.getHash() == nullptr);
105
106 Node node4(2, 1, 3, buffer);
107 BOOST_CHECK(!node4.isFull());
108 BOOST_CHECK_EQUAL(node4.getLeafSeqNo(), 3);
109 BOOST_CHECK_EQUAL_COLLECTIONS(node4.getHash()->begin(), node4.getHash()->end(),
110 buffer->begin(), buffer->end());
111
112
113 {
114 using namespace CryptoPP;
115
116 ndn::OBufferStream os;
117 std::string emptyHash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
118 StringSource ss(reinterpret_cast<const uint8_t*>(emptyHash.c_str()), emptyHash.size(),
119 true, new HexDecoder(new FileSink(os)));
120 BOOST_CHECK_EQUAL_COLLECTIONS(Node::getEmptyHash()->begin(), Node::getEmptyHash()->end(),
121 os.buf()->begin(), os.buf()->end());
122 }
123}
124
125BOOST_AUTO_TEST_SUITE_END()
126
127} // namespace tests
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700128} // namespace delorean
129} // namespace ndn