blob: 84ce74e80d2b5ce8940b0b526a41070ba87a2572 [file] [log] [blame]
Yingdi Yu0c3e5912015-03-17 14:22:38 -07001/* -*- 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 "db.hpp"
23#include "db-fixture.hpp"
24
25#include <ndn-cxx/security/digest-sha256.hpp>
26#include <ndn-cxx/encoding/buffer-stream.hpp>
27#include "boost-test.hpp"
28
29namespace nsl {
30namespace tests {
31
32BOOST_FIXTURE_TEST_SUITE(TestDb, DbFixture)
33
34BOOST_AUTO_TEST_CASE(Basic)
35{
36 ndn::DigestSha256 digest;
37 ndn::ConstBufferPtr hash = make_shared<ndn::Buffer>(32);
38 Data data1(Name("/logger/name/5/0/abcdabcdabcdabcdabcd/complete"));
39 data1.setSignature(digest);
40 data1.setSignatureValue(Block(tlv::SignatureValue, hash));
41 Data data2(Name("/logger/name/5/32/abcdabcdabcdabcdabcd/complete"));
42 data2.setSignature(digest);
43 data2.setSignatureValue(Block(tlv::SignatureValue, hash));
44 Data data3(Name("/logger/name/5/32/abcdabcdabcdabcdabcd/33"));
45 data3.setSignature(digest);
46 data3.setSignatureValue(Block(tlv::SignatureValue, hash));
47
48 BOOST_CHECK_EQUAL(db.getPendingSubTrees().size(), 0);
49 BOOST_CHECK(db.getSubTreeData(5, 0) == nullptr);
50 db.insertSubTreeData(5, 0, data1);
51 BOOST_REQUIRE(db.getSubTreeData(5, 0) != nullptr);
52 BOOST_CHECK(db.getSubTreeData(5, 0)->wireEncode() == data1.wireEncode());
53 BOOST_CHECK_EQUAL(db.getPendingSubTrees().size(), 0);
54
55 BOOST_CHECK(db.getSubTreeData(5, 32) == nullptr);
56 db.insertSubTreeData(5, 32, data3, false, 33);
57 BOOST_REQUIRE(db.getSubTreeData(5, 32) != nullptr);
58 BOOST_CHECK(db.getSubTreeData(5, 32)->wireEncode() == data3.wireEncode());
59 BOOST_CHECK_EQUAL(db.getPendingSubTrees().size(), 1);
60
61 db.insertSubTreeData(5, 32, data2);
62 BOOST_REQUIRE(db.getSubTreeData(5, 32) != nullptr);
63 BOOST_CHECK(db.getSubTreeData(5, 32)->wireEncode() == data2.wireEncode());
64 BOOST_CHECK_EQUAL(db.getPendingSubTrees().size(), 0);
65}
66
67BOOST_AUTO_TEST_CASE(Basic2)
68{
69 ndn::DigestSha256 digest;
70 ndn::ConstBufferPtr hash = make_shared<ndn::Buffer>(32);
71 Data data1(Name("/logger/name/10/0/abcdabcdabcdabcdabcd/33"));
72 data1.setSignature(digest);
73 data1.setSignatureValue(Block(tlv::SignatureValue, hash));
74 Data data2(Name("/logger/name/5/32/abcdabcdabcdabcdabcd/33"));
75 data2.setSignature(digest);
76 data2.setSignatureValue(Block(tlv::SignatureValue, hash));
77
78 db.insertSubTreeData(5, 32, data2, false, 33);
79 db.insertSubTreeData(10, 0, data1, false, 33);
80 std::vector<shared_ptr<Data>> subtrees = db.getPendingSubTrees();
81
82 BOOST_CHECK_EQUAL(subtrees.size(), 2);
83 BOOST_CHECK(subtrees[0]->wireEncode() == data1.wireEncode());
84 BOOST_CHECK(subtrees[1]->wireEncode() == data2.wireEncode());
85}
86
87const uint8_t Data1[] = {
880x06, 0xc5, // NDN Data
89 0x07, 0x14, // Name
90 0x08, 0x05,
91 0x6c, 0x6f, 0x63, 0x61, 0x6c,
92 0x08, 0x03,
93 0x6e, 0x64, 0x6e,
94 0x08, 0x06,
95 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
96 0x14, 0x04, // MetaInfo
97 0x19, 0x02, // FreshnessPeriod
98 0x27, 0x10,
99 0x15, 0x08, // Content
100 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21,
101 0x16, 0x1b, // SignatureInfo
102 0x1b, 0x01, // SignatureType
103 0x01,
104 0x1c, 0x16, // KeyLocator
105 0x07, 0x14, // Name
106 0x08, 0x04,
107 0x74, 0x65, 0x73, 0x74,
108 0x08, 0x03,
109 0x6b, 0x65, 0x79,
110 0x08, 0x07,
111 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
112 0x17, 0x80, // SignatureValue
113 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
114 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
115 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
116 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
117 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
118 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
119 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
120 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
121 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
122 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
123};
124
125BOOST_AUTO_TEST_CASE(Basic3)
126{
127 Name loggerName("/test/logger");
128 Name dataName("/test/data");
129 Block block(Data1, sizeof(Data1));
130 Data data(block);
131
132 BOOST_CHECK_EQUAL(db.getMaxLeafSeq(), 0);
133
134 Leaf leaf(dataName, 1, 0, 0, loggerName);
135 BOOST_CHECK(db.insertLeafData(leaf, data));
136 BOOST_CHECK_EQUAL(db.getMaxLeafSeq(), 1);
137
138 auto result = db.getLeaf(0);
139 BOOST_CHECK_EQUAL(result.first->getDataName(), dataName);
140 BOOST_CHECK_EQUAL(result.first->getTimestamp(), 1);
141 BOOST_CHECK_EQUAL(result.first->getDataSeqNo(), 0);
142 BOOST_CHECK_EQUAL(result.first->getSignerSeqNo(), 0);
143 BOOST_REQUIRE(result.second != nullptr);
144 BOOST_CHECK_EQUAL(result.second->getName(), data.getName());
145
146 Leaf leaf2(dataName, 2, 1, 0, loggerName);
147 BOOST_CHECK(db.insertLeafData(leaf2));
148 BOOST_CHECK_EQUAL(db.getMaxLeafSeq(), 2);
149
150 result = db.getLeaf(1);
151 BOOST_REQUIRE(result.first != nullptr);
152 BOOST_CHECK_EQUAL(result.first->getDataName(), dataName);
153 BOOST_CHECK_EQUAL(result.first->getTimestamp(), 2);
154 BOOST_CHECK_EQUAL(result.first->getDataSeqNo(), 1);
155 BOOST_CHECK_EQUAL(result.first->getSignerSeqNo(), 0);
156 BOOST_REQUIRE(result.second == nullptr);
157
158 Leaf leaf3(dataName, 2, 5, 0, loggerName);
159 BOOST_CHECK_EQUAL(db.insertLeafData(leaf), false);
160 BOOST_CHECK_EQUAL(db.insertLeafData(leaf2), false);
161 BOOST_CHECK_EQUAL(db.insertLeafData(leaf3), false);
162}
163
164BOOST_AUTO_TEST_SUITE_END()
165
166} // namespace tests
167} // namespace nsl