blob: c6e296fbad8da1d17044cf4726d0b17458055542 [file] [log] [blame]
Yingdi Yuae734272015-07-04 17:38:48 -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/buffer-source.hpp"
23#include "security/transform/step-source.hpp"
24#include "security/transform/digest-filter.hpp"
25#include "security/transform/stream-sink.hpp"
26#include "encoding/buffer-stream.hpp"
27
28#include "boost-test.hpp"
29
30namespace ndn {
31namespace security {
32namespace transform {
33namespace tests {
34
35BOOST_AUTO_TEST_SUITE(Security)
36BOOST_AUTO_TEST_SUITE(Transform)
37BOOST_AUTO_TEST_SUITE(TestDigestFilter)
38
39BOOST_AUTO_TEST_CASE(Basic)
40{
41 uint8_t in[128] = {
42 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
43 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
44 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
45 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
46 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
47 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
48 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
49 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
50 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
51 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
52 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
53 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
54 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
56 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
57 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
58 };
59
60 uint8_t out[] = {
61 0x3e, 0x14, 0xfd, 0x66, 0x9a, 0x79, 0x80, 0x65,
62 0xc4, 0x0d, 0x61, 0xf8, 0x6a, 0xc7, 0x98, 0x29,
63 0xc0, 0x6b, 0x90, 0x8f, 0xbb, 0x19, 0xa0, 0x85,
64 0xf7, 0xfa, 0x7b, 0x2d, 0xd6, 0x8c, 0xd5, 0xa3
65 };
66
67 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -070068 bufferSource(in, sizeof(in)) >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
Yingdi Yuae734272015-07-04 17:38:48 -070069
70 ConstBufferPtr buf = os.buf();
71 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end());
72}
73
74BOOST_AUTO_TEST_CASE(StepByStep)
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 uint8_t out[] = {
96 0x3e, 0x14, 0xfd, 0x66, 0x9a, 0x79, 0x80, 0x65,
97 0xc4, 0x0d, 0x61, 0xf8, 0x6a, 0xc7, 0x98, 0x29,
98 0xc0, 0x6b, 0x90, 0x8f, 0xbb, 0x19, 0xa0, 0x85,
99 0xf7, 0xfa, 0x7b, 0x2d, 0xd6, 0x8c, 0xd5, 0xa3
100 };
101
102 StepSource source;
103 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -0700104 source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
Yingdi Yuae734272015-07-04 17:38:48 -0700105 source.write(in, 32);
106 source.write(in + 32, 1);
107 source.write(in + 33, 2);
108 source.write(in + 35, 3);
109 source.write(in + 38, 26);
110 source.write(in + 64, 64);
111 source.end();
112
113 ConstBufferPtr buf = os.buf();
114 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end());
115}
116
117BOOST_AUTO_TEST_CASE(EmptyInput)
118{
119 uint8_t out[] = {
120 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
121 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
122 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
123 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
124 };
125
126 OBufferStream os;
127 StepSource source;
Yingdi Yu99b2a002015-08-12 12:47:44 -0700128 source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
Yingdi Yuae734272015-07-04 17:38:48 -0700129 source.end();
130
131 ConstBufferPtr buf = os.buf();
132 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end());
133}
134
135BOOST_AUTO_TEST_CASE(Error)
136{
137 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -0700138 BOOST_REQUIRE_THROW(stepSource() >> digestFilter(DigestAlgorithm::NONE) >> streamSink(os),
Yingdi Yuae734272015-07-04 17:38:48 -0700139 transform::Error);
140}
141
142BOOST_AUTO_TEST_SUITE_END() // TestDigestFilter
143BOOST_AUTO_TEST_SUITE_END() // Transform
144BOOST_AUTO_TEST_SUITE_END() // Security
145
146} // namespace tests
147} // namespace transform
148} // namespace security
149} // namespace ndn