blob: 2260ccfb354e9e8440f368bdc4ad0ffab45ee30e [file] [log] [blame]
peizhen guo410e0e12014-08-12 13:24:14 -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 * @author Peizhen Guo <patrick.guopz@gmail.com>
20 */
21#include <boost-test.hpp>
22#include <iostream>
23
24#include "merkle-tree.hpp"
25
26namespace nsl {
27
28boost::test_tools::predicate_result check_buffer(ndn::ConstBufferPtr ptr1, ndn::ConstBufferPtr ptr2)
29{
30 bool result = true;
31 for (int i = 0; i < ptr1->size(); i++)
32 {
33 if ((*ptr1)[i] != (*ptr2)[i])
34 {
35 result = false;
36 break;
37 }
38 }
39 return result;
40}
41
42BOOST_AUTO_TEST_SUITE(TestTree)
43
44
45BOOST_AUTO_TEST_CASE(TestBuild)
46{
47 std::string str1 = "peizhen";
48 std::string str2 = "guo";
49 std::string str3 = "is";
50 std::string str4 = "building";
51 std::string str5 = "this";
52 std::string str6 = "logging";
53 std::string str7 = "system";
54 ndn::Buffer buf1;
55 ndn::Buffer buf2;
56 ndn::Buffer buf3;
57 ndn::Buffer buf4;
58 ndn::Buffer buf5;
59 ndn::Buffer buf6;
60 ndn::Buffer buf7;
61 for (int i=0; i < str1.size(); i++)
62 buf1.push_back(uint8_t(str1[i]));
63 for (int i=0; i < str2.size(); i++)
64 buf2.push_back(uint8_t(str2[i]));
65 for (int i=0; i < str3.size(); i++)
66 buf3.push_back(uint8_t(str3[i]));
67 for (int i=0; i < str4.size(); i++)
68 buf4.push_back(uint8_t(str4[i]));
69 for (int i=0; i < str5.size(); i++)
70 buf5.push_back(uint8_t(str5[i]));
71 for (int i=0; i < str6.size(); i++)
72 buf6.push_back(uint8_t(str6[i]));
73 for (int i=0; i < str7.size(); i++)
74 buf7.push_back(uint8_t(str7[i]));
75 ndn::ConstBufferPtr buf_p1 = boost::make_shared<ndn::Buffer>(buf1);
76 ndn::ConstBufferPtr buf_p2 = boost::make_shared<ndn::Buffer>(buf2);
77 ndn::ConstBufferPtr buf_p3 = boost::make_shared<ndn::Buffer>(buf3);
78 ndn::ConstBufferPtr buf_p4 = boost::make_shared<ndn::Buffer>(buf4);
79 ndn::ConstBufferPtr buf_p5 = boost::make_shared<ndn::Buffer>(buf5);
80 ndn::ConstBufferPtr buf_p6 = boost::make_shared<ndn::Buffer>(buf6);
81 ndn::ConstBufferPtr buf_p7 = boost::make_shared<ndn::Buffer>(buf7);
82
83 //Test add/get function
84 MerkleTree merkle_tree;
85 merkle_tree.addLeaf(buf_p1);
86 Index idx;
87 idx.number = 0;
88 idx.level = 0;
89 ndn::ConstBufferPtr tmp_ptr = ((Leaf*)(merkle_tree.getNode(idx).get()))->getData();
90 BOOST_CHECK(merkle_tree.getLeafNum() == 1 && merkle_tree.getLevel() == 1
91 && merkle_tree.getLevel() == idx.level + 1);
92 BOOST_CHECK(check_buffer(tmp_ptr, buf_p1));
93
94 merkle_tree.addLeaf(buf_p2);
95 idx.number += 1;
96 BOOST_CHECK(check_buffer(((Leaf*)(merkle_tree.getNode(idx).get()))->getData(), buf_p2));
97 idx.number = 0;
98 idx.level = 1;
99 BOOST_CHECK(((IntermediateNode*)(merkle_tree.getNode(idx).get()))->isFull() == true
100 && merkle_tree.getLeafNum() == 2 && merkle_tree.getLevel() == 2
101 && merkle_tree.getLevel() == idx.level + 1);
102
103
104 merkle_tree.addLeaf(buf_p3);
105 idx.number = 2; idx.level = 0;
106 BOOST_CHECK(check_buffer(((Leaf*)(merkle_tree.getNode(idx).get()))->getData(), buf_p3));
107 idx.level = 1;
108 BOOST_CHECK(((IntermediateNode*)(merkle_tree.getNode(idx).get()))->isFull() == false);
109 idx.number = 0;
110 BOOST_CHECK(((IntermediateNode*)(merkle_tree.getNode(idx).get()))->isFull() == true);
111 BOOST_CHECK(merkle_tree.getLeafNum() == 3 && merkle_tree.getLevel() == 3);
112
113
114 merkle_tree.addLeaf(buf_p4);
115 merkle_tree.addLeaf(buf_p5);
116 merkle_tree.addLeaf(buf_p6);
117 merkle_tree.addLeaf(buf_p7);
118 BOOST_CHECK(merkle_tree.getLeafNum() == 7 && merkle_tree.getLevel() == 4);
119 idx.level = 2;
120 idx.number = 4;
121 BOOST_CHECK(((IntermediateNode*)(merkle_tree.getNode(idx).get()))->isFull() == false);
122 idx.level = 1;
123 idx.number = 2;
124 BOOST_CHECK(((IntermediateNode*)(merkle_tree.getNode(idx).get()))->isFull() == true);
125}
126
127
128
129BOOST_AUTO_TEST_CASE(TestGenerateProof)
130{
131
132 std::string str1 = "peizhen";
133 std::string str2 = "guo";
134 std::string str3 = "is";
135 std::string str4 = "building";
136 std::string str5 = "this";
137 std::string str6 = "logging";
138 std::string str7 = "system";
139 ndn::Buffer buf1;
140 ndn::Buffer buf2;
141 ndn::Buffer buf3;
142 ndn::Buffer buf4;
143 ndn::Buffer buf5;
144 ndn::Buffer buf6;
145 ndn::Buffer buf7;
146 for (int i=0; i < str1.size(); i++)
147 buf1.push_back(uint8_t(str1[i]));
148 for (int i=0; i < str2.size(); i++)
149 buf2.push_back(uint8_t(str2[i]));
150 for (int i=0; i < str3.size(); i++)
151 buf3.push_back(uint8_t(str3[i]));
152 for (int i=0; i < str4.size(); i++)
153 buf4.push_back(uint8_t(str4[i]));
154 for (int i=0; i < str5.size(); i++)
155 buf5.push_back(uint8_t(str5[i]));
156 for (int i=0; i < str6.size(); i++)
157 buf6.push_back(uint8_t(str6[i]));
158 for (int i=0; i < str7.size(); i++)
159 buf7.push_back(uint8_t(str7[i]));
160 ndn::ConstBufferPtr buf_p1 = boost::make_shared<ndn::Buffer>(buf1);
161 ndn::ConstBufferPtr buf_p2 = boost::make_shared<ndn::Buffer>(buf2);
162 ndn::ConstBufferPtr buf_p3 = boost::make_shared<ndn::Buffer>(buf3);
163 ndn::ConstBufferPtr buf_p4 = boost::make_shared<ndn::Buffer>(buf4);
164 ndn::ConstBufferPtr buf_p5 = boost::make_shared<ndn::Buffer>(buf5);
165 ndn::ConstBufferPtr buf_p6 = boost::make_shared<ndn::Buffer>(buf6);
166 ndn::ConstBufferPtr buf_p7 = boost::make_shared<ndn::Buffer>(buf7);
167
168
169 // Test genProof function
170 MerkleTree merkle_tree;
171 merkle_tree.addLeaf(buf_p1);
172 merkle_tree.addLeaf(buf_p2);
173 merkle_tree.addLeaf(buf_p3);
174 merkle_tree.addLeaf(buf_p4);
175 merkle_tree.addLeaf(buf_p5);
176 merkle_tree.addLeaf(buf_p6);
177 merkle_tree.addLeaf(buf_p7);
178 std::vector<ConstNodePtr> verifyPathPresent = merkle_tree.generateProof(2, 5);
179 std::vector<ConstNodePtr> verifyPathPrevious = merkle_tree.generateProof(4, 6);
180 Index idx;
181 for (int i = 0; i < verifyPathPresent.size(); i++)
182 {
183 idx = (verifyPathPresent[i])->getIndex();
184 std::cout << idx.number << "," << idx.level << std::endl;
185 }
186 std::cout << std::endl;
187 for (int i = 0; i < verifyPathPrevious.size(); i++)
188 {
189 idx = (verifyPathPrevious[i])->getIndex();
190 std::cout << idx.number << "," << idx.level << std::endl;
191 }
192}
193
194
195
196BOOST_AUTO_TEST_SUITE_END()
197
198} // namespace nsl