Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of NSL (NDN Signature Logger). |
| 6 | * See AUTHORS.md for complete list of NSL authors and contributors. |
| 7 | * |
| 8 | * NSL 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, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NSL 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 | * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of nsl authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "node.hpp" |
| 23 | #include "cryptopp.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
| 26 | #include "boost-test.hpp" |
| 27 | |
| 28 | namespace nsl { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestNode) |
| 32 | |
| 33 | BOOST_AUTO_TEST_CASE(IndexTest1) |
| 34 | { |
| 35 | Node::Index idx(0, 0); |
| 36 | |
| 37 | BOOST_CHECK_EQUAL(idx.seqNo, 0); |
| 38 | BOOST_CHECK_EQUAL(idx.level, 0); |
| 39 | BOOST_CHECK_EQUAL(idx.range, 1); |
| 40 | |
| 41 | Node::Index idx2(0, 1); |
| 42 | BOOST_CHECK_EQUAL(idx2.seqNo, 0); |
| 43 | BOOST_CHECK_EQUAL(idx2.level, 1); |
| 44 | BOOST_CHECK_EQUAL(idx2.range, 2); |
| 45 | |
| 46 | Node::Index idx3(2, 1); |
| 47 | BOOST_CHECK_EQUAL(idx3.seqNo, 2); |
| 48 | BOOST_CHECK_EQUAL(idx3.level, 1); |
| 49 | BOOST_CHECK_EQUAL(idx3.range, 2); |
| 50 | |
| 51 | Node::Index idx4(4, 2); |
| 52 | BOOST_CHECK_EQUAL(idx4.seqNo, 4); |
| 53 | BOOST_CHECK_EQUAL(idx4.level, 2); |
| 54 | BOOST_CHECK_EQUAL(idx4.range, 4); |
| 55 | |
| 56 | BOOST_CHECK_THROW(Node::Index(1, 1), Node::Error); |
| 57 | BOOST_CHECK_THROW(Node::Index(2, 2), Node::Error); |
| 58 | } |
| 59 | |
| 60 | BOOST_AUTO_TEST_CASE(IndexTest2) |
| 61 | { |
| 62 | Node::Index idx1(0, 0); |
| 63 | Node::Index idx2(0, 1); |
| 64 | Node::Index idx3(2, 0); |
| 65 | Node::Index idx4(2, 1); |
| 66 | |
| 67 | BOOST_CHECK(idx1 < idx2); |
| 68 | BOOST_CHECK(idx1 < idx3); |
| 69 | BOOST_CHECK(idx1 < idx4); |
| 70 | BOOST_CHECK(idx2 < idx3); |
| 71 | BOOST_CHECK(idx2 < idx4); |
| 72 | BOOST_CHECK(idx3 < idx4); |
| 73 | |
| 74 | BOOST_CHECK(idx1 == idx1); |
| 75 | BOOST_CHECK_EQUAL(idx1 == idx2, false); |
| 76 | BOOST_CHECK_EQUAL(idx1 == idx3, false); |
| 77 | BOOST_CHECK_EQUAL(idx1 == idx4, false); |
| 78 | } |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(NodeTest1) |
| 81 | { |
| 82 | std::string hash("ABCDEFGHIJKLMNOPabcdefghijklmno"); |
| 83 | auto buffer = make_shared<const ndn::Buffer>(hash.c_str(), hash.size()); |
| 84 | |
| 85 | Node node(0, 0); |
| 86 | BOOST_CHECK(node.getIndex() == Node::Index(0, 0)); |
| 87 | BOOST_CHECK(!node.isFull()); |
| 88 | BOOST_CHECK_EQUAL(node.getLeafSeqNo(), 0); |
| 89 | BOOST_CHECK(node.getHash() == nullptr); |
| 90 | |
| 91 | node.setLeafSeqNo(1); |
| 92 | BOOST_CHECK(node.isFull()); |
| 93 | BOOST_CHECK_EQUAL(node.getLeafSeqNo(), 1); |
| 94 | |
| 95 | Node node2(2, 1); |
| 96 | BOOST_CHECK(!node2.isFull()); |
| 97 | BOOST_CHECK_EQUAL(node2.getLeafSeqNo(), 2); |
| 98 | BOOST_CHECK(node2.getHash() == nullptr); |
| 99 | |
| 100 | Node node3(2, 1, 4); |
| 101 | BOOST_CHECK(node3.isFull()); |
| 102 | BOOST_CHECK_EQUAL(node3.getLeafSeqNo(), 4); |
| 103 | BOOST_CHECK(node3.getHash() == nullptr); |
| 104 | |
| 105 | Node node4(2, 1, 3, buffer); |
| 106 | BOOST_CHECK(!node4.isFull()); |
| 107 | BOOST_CHECK_EQUAL(node4.getLeafSeqNo(), 3); |
| 108 | BOOST_CHECK_EQUAL_COLLECTIONS(node4.getHash()->begin(), node4.getHash()->end(), |
| 109 | buffer->begin(), buffer->end()); |
| 110 | |
| 111 | |
| 112 | { |
| 113 | using namespace CryptoPP; |
| 114 | |
| 115 | ndn::OBufferStream os; |
| 116 | std::string emptyHash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 117 | StringSource ss(reinterpret_cast<const uint8_t*>(emptyHash.c_str()), emptyHash.size(), |
| 118 | true, new HexDecoder(new FileSink(os))); |
| 119 | BOOST_CHECK_EQUAL_COLLECTIONS(Node::getEmptyHash()->begin(), Node::getEmptyHash()->end(), |
| 120 | os.buf()->begin(), os.buf()->end()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_SUITE_END() |
| 125 | |
| 126 | } // namespace tests |
| 127 | } // namespace nsl |