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 "leaf.hpp" |
| 23 | #include "cryptopp.hpp" |
| 24 | |
| 25 | #include "boost-test.hpp" |
| 26 | |
| 27 | namespace nsl { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestLeaf) |
| 31 | |
| 32 | void |
| 33 | printByte(const uint8_t* buf, size_t size) |
| 34 | { |
| 35 | std::stringstream ss; |
| 36 | using namespace CryptoPP; |
| 37 | StringSource is(buf, size, true, new HexEncoder(new FileSink(ss), false)); |
| 38 | |
| 39 | std::string output = ss.str(); |
| 40 | for (size_t i = 0; i < output.size(); i++) { |
| 41 | std::cerr << "0x" << output.at(i); |
| 42 | std::cerr << output.at(++i) << ", "; |
| 43 | if ((i + 1) % 32 == 0) |
| 44 | std::cerr << std::endl; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE(Basic) |
| 49 | { |
| 50 | Name loggerName("/test/logger"); |
| 51 | Name dataName("/test/data"); |
| 52 | |
| 53 | BOOST_CHECK_NO_THROW(Leaf(dataName, 0, 1, 1, loggerName)); |
| 54 | BOOST_CHECK_NO_THROW(Leaf(dataName, 0, 2, 1, loggerName)); |
| 55 | BOOST_CHECK_THROW(Leaf(dataName, 0, 2, 3, loggerName), Leaf::Error); |
| 56 | |
| 57 | Leaf leaf(dataName, 0, 2, 1, loggerName); |
| 58 | |
| 59 | BOOST_CHECK_EQUAL(leaf.getDataName(), dataName); |
| 60 | BOOST_CHECK_EQUAL(leaf.getTimestamp(), 0); |
| 61 | BOOST_CHECK_EQUAL(leaf.getDataSeqNo(), 2); |
| 62 | BOOST_CHECK_EQUAL(leaf.getSignerSeqNo(), 1); |
| 63 | |
| 64 | BOOST_CHECK_THROW(leaf.setDataSeqNo(0), Leaf::Error); |
| 65 | BOOST_CHECK_THROW(leaf.setSignerSeqNo(5), Leaf::Error); |
| 66 | } |
| 67 | |
| 68 | uint8_t LEAF_BLOCK[] = { |
| 69 | 0x80, 0x17, |
| 70 | 0x07, 0x0c, |
| 71 | 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, |
| 72 | 0x08, 0x04, 0x64, 0x61, 0x74, 0x61, |
| 73 | 0x81, 0x01, 0x00, |
| 74 | 0x82, 0x01, 0x02, |
| 75 | 0x83, 0x01, 0x01 |
| 76 | }; |
| 77 | |
| 78 | uint8_t LEAF_HASH[] = { |
| 79 | 0x79, 0xcb, 0x54, 0xa7, 0x47, 0xa8, 0xea, 0x98, 0x92, 0x39, 0xdb, 0xcf, 0xd0, 0x9a, 0xbb, 0xbd, |
| 80 | 0xe3, 0x10, 0x82, 0x3b, 0x4d, 0x46, 0xc4, 0xc1, 0x39, 0x76, 0xbd, 0x3d, 0x17, 0xcc, 0xa9, 0x2b |
| 81 | }; |
| 82 | |
| 83 | uint8_t LEAF_DATA[] = { |
| 84 | 0x06, 0x79, |
| 85 | 0x07, 0x33, |
| 86 | 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, |
| 87 | 0x08, 0x06, 0x6c, 0x6f, 0x67, 0x67, 0x65, 0x72, |
| 88 | 0x08, 0x01, 0x02, |
| 89 | 0x08, 0x20, |
| 90 | 0x79, 0xcb, 0x54, 0xa7, 0x47, 0xa8, 0xea, 0x98, |
| 91 | 0x92, 0x39, 0xdb, 0xcf, 0xd0, 0x9a, 0xbb, 0xbd, |
| 92 | 0xe3, 0x10, 0x82, 0x3b, 0x4d, 0x46, 0xc4, 0xc1, |
| 93 | 0x39, 0x76, 0xbd, 0x3d, 0x17, 0xcc, 0xa9, 0x2b, |
| 94 | 0x14, 0x00, |
| 95 | 0x15, 0x19, |
| 96 | 0x80, 0x17, |
| 97 | 0x07, 0x0c, |
| 98 | 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, |
| 99 | 0x08, 0x04, 0x64, 0x61, 0x74, 0x61, |
| 100 | 0x81, 0x01, 0x00, |
| 101 | 0x82, 0x01, 0x02, |
| 102 | 0x83, 0x01, 0x01, |
| 103 | 0x16, 0x03, |
| 104 | 0x1b, 0x01, 0x00, |
| 105 | 0x17, 0x20, |
| 106 | 0x96, 0x49, 0xe0, 0x62, 0x23, 0x72, 0xd0, 0x90, 0x85, 0x9c, 0x28, 0xda, 0xc8, 0x50, 0x6f, 0x48, |
| 107 | 0x56, 0x62, 0x14, 0x8d, 0x75, 0x20, 0x91, 0xa9, 0x0a, 0x46, 0xd6, 0xf8, 0xfc, 0x5d, 0x8e, 0x8e |
| 108 | }; |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(Encoding) |
| 111 | { |
| 112 | Name loggerName("/test/logger"); |
| 113 | Name dataName("/test/data"); |
| 114 | |
| 115 | Leaf leaf(dataName, 0, 2, 1, loggerName); |
| 116 | const Block& block = leaf.wireEncode(); |
| 117 | |
| 118 | BOOST_CHECK_EQUAL_COLLECTIONS(block.wire(), block.wire() + block.size(), |
| 119 | LEAF_BLOCK, LEAF_BLOCK + sizeof(LEAF_BLOCK)); |
| 120 | |
| 121 | ndn::ConstBufferPtr hash = leaf.getHash(); |
| 122 | BOOST_CHECK_EQUAL_COLLECTIONS(hash->begin(), hash->end(), |
| 123 | LEAF_HASH, LEAF_HASH + sizeof(LEAF_HASH)); |
| 124 | |
| 125 | auto data = leaf.encode(); |
| 126 | BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().wire(), |
| 127 | data->wireEncode().wire() + data->wireEncode().size(), |
| 128 | LEAF_DATA, |
| 129 | LEAF_DATA + sizeof(LEAF_DATA)); |
| 130 | } |
| 131 | |
| 132 | BOOST_AUTO_TEST_CASE(Decoding) |
| 133 | { |
| 134 | Name loggerName("/test/logger"); |
| 135 | Name dataName("/test/data"); |
| 136 | |
| 137 | |
| 138 | Block block(LEAF_DATA, sizeof(LEAF_DATA)); |
| 139 | Data data(block); |
| 140 | |
| 141 | Leaf leaf; |
| 142 | BOOST_REQUIRE_NO_THROW(leaf.decode(data)); |
| 143 | |
| 144 | BOOST_CHECK_EQUAL(leaf.getDataName(), dataName); |
| 145 | BOOST_CHECK_EQUAL(leaf.getTimestamp(), 0); |
| 146 | BOOST_CHECK_EQUAL(leaf.getDataSeqNo(), 2); |
| 147 | BOOST_CHECK_EQUAL(leaf.getSignerSeqNo(), 1); |
| 148 | |
| 149 | ndn::ConstBufferPtr hash = leaf.getHash(); |
| 150 | BOOST_CHECK_EQUAL_COLLECTIONS(hash->begin(), hash->end(), |
| 151 | LEAF_HASH, LEAF_HASH + sizeof(LEAF_HASH)); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | BOOST_AUTO_TEST_SUITE_END() |
| 156 | |
| 157 | } // namespace tests |
| 158 | } // namespace nsl |