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