blob: 5edcaaac5337a7a24ac64dd0c22297f60a4b6658 [file] [log] [blame]
Yingdi Yu38317e52015-07-22 13:58:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "security/transform/hex-encode.hpp"
23#include "security/transform/buffer-source.hpp"
24#include "security/transform/step-source.hpp"
25#include "security/transform/stream-sink.hpp"
26#include "encoding/buffer-stream.hpp"
27#include <iostream>
28
29#include "boost-test.hpp"
30
31namespace ndn {
32namespace security {
33namespace transform {
34namespace tests {
35
36BOOST_AUTO_TEST_SUITE(Security)
37BOOST_AUTO_TEST_SUITE(Transform)
38BOOST_AUTO_TEST_SUITE(TestHexEncode)
39
40BOOST_AUTO_TEST_CASE(Basic)
41{
42 uint8_t in[128] = {
43 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
44 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
45 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
46 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
47 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
48 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
49 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
50 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
51 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
52 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
53 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
54 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
55 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
56 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
57 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
58 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
59 };
60
61 std::string out =
62 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
63 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
64 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
65 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
66
67 OBufferStream os;
68 bufferSource(in, sizeof(in)) >> hexEncode() >> streamSink(os);
69
70 ConstBufferPtr buf1 = os.buf();
71 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), buf1->begin(), buf1->end());
72}
73
74BOOST_AUTO_TEST_CASE(UpperCase)
75{
76 uint8_t in[128] = {
77 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
78 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
79 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
80 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
81 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
82 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
83 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
84 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
85 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
86 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
87 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
88 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
89 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
90 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
91 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
92 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
93 };
94
95 std::string out =
96 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
97 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
98 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
99 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F";
100
101 OBufferStream os;
102 bufferSource(in, sizeof(in)) >> hexEncode(true) >> streamSink(os);
103
104 ConstBufferPtr buf1 = os.buf();
105 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), buf1->begin(), buf1->end());
106}
107
108BOOST_AUTO_TEST_CASE(StepByStep)
109{
110 uint8_t in[] = {
111 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
112 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
113 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
114 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
115 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
116 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
117 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
118 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
119 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
120 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
121 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
122 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
123 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
124 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
125 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
126 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
127 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
128 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
129 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
130 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
131 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
132 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
133 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
134 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
135 };
136
137 std::string out =
138 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
139 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
140 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
141 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
142 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
143 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
144 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
145 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
146 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
147 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
148 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
149 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
150
151 OBufferStream os;
152 StepSource source;
153 source >> hexEncode() >> streamSink(os);
154 source.write(in, 64); // complete chunk
155 source.write(in + 64, 32); // first half of a chunk
156 source.write(in + 96, 32); // second half of a chunk
157 source.write(in + 128, 20); // front of a chunk
158 source.write(in + 148, 20); // middle of a chunk
159 source.write(in + 168, 24); // end of a chunk
160 source.write(in + 192, 63); // odd number of bytes
161 source.write(in + 255, 85); // one and half chunk
162 source.write(in + 340, 44); // remaining part
163 source.end();
164
165 ConstBufferPtr buf1 = os.buf();
166 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), buf1->begin(), buf1->end());
167}
168
169BOOST_AUTO_TEST_CASE(EmptyInput)
170{
171 OBufferStream os;
172 StepSource source;
173 source >> hexEncode() >> streamSink(os);
174 source.end();
175 BOOST_CHECK_EQUAL(os.buf()->size(), 0);
176}
177
178BOOST_AUTO_TEST_SUITE_END() // TestHexEncode
179BOOST_AUTO_TEST_SUITE_END() // Transform
180BOOST_AUTO_TEST_SUITE_END() // Security
181
182} // namespace tests
183} // namespace transform
184} // namespace security
185} // namespace ndn