blob: 2a81b6aac959656a44972f0b67601738ea692049 [file] [log] [blame]
Yingdi Yu38317e52015-07-22 13:58:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa84f4642017-08-23 16:14:51 -04002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Yingdi Yu38317e52015-07-22 13:58:02 -07004 *
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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/transform/hex-encode.hpp"
23#include "ndn-cxx/security/transform/buffer-source.hpp"
24#include "ndn-cxx/security/transform/step-source.hpp"
25#include "ndn-cxx/security/transform/stream-sink.hpp"
26#include "ndn-cxx/encoding/buffer-stream.hpp"
Yingdi Yu38317e52015-07-22 13:58:02 -070027
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "tests/boost-test.hpp"
Yingdi Yu38317e52015-07-22 13:58:02 -070029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::tests {
31
32using namespace ndn::security::transform;
Yingdi Yu38317e52015-07-22 13:58:02 -070033
34BOOST_AUTO_TEST_SUITE(Security)
35BOOST_AUTO_TEST_SUITE(Transform)
36BOOST_AUTO_TEST_SUITE(TestHexEncode)
37
38BOOST_AUTO_TEST_CASE(Basic)
39{
Davide Pesavento765abc92021-12-27 00:44:04 -050040 const uint8_t in[] = {
Yingdi Yu38317e52015-07-22 13:58:02 -070041 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
42 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
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 };
Davide Pesavento765abc92021-12-27 00:44:04 -050058 const std::string out =
Yingdi Yu38317e52015-07-22 13:58:02 -070059 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
60 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
61 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
62 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
63
64 OBufferStream os;
Davide Pesavento765abc92021-12-27 00:44:04 -050065 bufferSource(in) >> hexEncode() >> streamSink(os);
Yingdi Yu38317e52015-07-22 13:58:02 -070066
67 ConstBufferPtr buf1 = os.buf();
68 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), buf1->begin(), buf1->end());
69}
70
71BOOST_AUTO_TEST_CASE(UpperCase)
72{
Davide Pesavento765abc92021-12-27 00:44:04 -050073 const uint8_t in[] = {
Yingdi Yu38317e52015-07-22 13:58:02 -070074 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
75 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
76 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
77 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
78 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
79 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
80 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
81 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
82 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
83 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
84 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
85 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
86 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
87 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
88 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
89 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
90 };
Davide Pesavento765abc92021-12-27 00:44:04 -050091 const std::string out =
Yingdi Yu38317e52015-07-22 13:58:02 -070092 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
93 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
94 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
95 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F";
96
97 OBufferStream os;
Davide Pesavento765abc92021-12-27 00:44:04 -050098 bufferSource(in) >> hexEncode(true) >> streamSink(os);
Yingdi Yu38317e52015-07-22 13:58:02 -070099
100 ConstBufferPtr buf1 = os.buf();
101 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), buf1->begin(), buf1->end());
102}
103
104BOOST_AUTO_TEST_CASE(StepByStep)
105{
Davide Pesavento765abc92021-12-27 00:44:04 -0500106 const uint8_t in[] = {
Yingdi Yu38317e52015-07-22 13:58:02 -0700107 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
108 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
109 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
110 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
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 };
Davide Pesavento765abc92021-12-27 00:44:04 -0500132 const std::string out =
Yingdi Yu38317e52015-07-22 13:58:02 -0700133 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
134 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
135 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
136 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
137 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
138 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
139 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
140 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
141 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
142 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
143 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
144 "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
145
146 OBufferStream os;
147 StepSource source;
148 source >> hexEncode() >> streamSink(os);
Davide Pesavento765abc92021-12-27 00:44:04 -0500149 source.write({in, 64}); // complete chunk
150 source.write({in + 64, 32}); // first half of a chunk
151 source.write({in + 96, 32}); // second half of a chunk
152 source.write({in + 128, 20}); // front of a chunk
153 source.write({in + 148, 20}); // middle of a chunk
154 source.write({in + 168, 24}); // end of a chunk
155 source.write({in + 192, 63}); // odd number of bytes
156 source.write({in + 255, 85}); // one and half chunk
157 source.write({in + 340, 44}); // remaining part
Yingdi Yu38317e52015-07-22 13:58:02 -0700158 source.end();
159
160 ConstBufferPtr buf1 = os.buf();
161 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), buf1->begin(), buf1->end());
162}
163
164BOOST_AUTO_TEST_CASE(EmptyInput)
165{
166 OBufferStream os;
167 StepSource source;
168 source >> hexEncode() >> streamSink(os);
169 source.end();
Davide Pesavento765abc92021-12-27 00:44:04 -0500170
Yingdi Yu38317e52015-07-22 13:58:02 -0700171 BOOST_CHECK_EQUAL(os.buf()->size(), 0);
172}
173
174BOOST_AUTO_TEST_SUITE_END() // TestHexEncode
175BOOST_AUTO_TEST_SUITE_END() // Transform
176BOOST_AUTO_TEST_SUITE_END() // Security
177
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400178} // namespace ndn::tests