blob: a3f0314e4cfab2ed6fdd773ce7827a47fd8155ae [file] [log] [blame]
Yingdi Yuae734272015-07-04 17:38:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento8aad3722017-09-16 20:57:28 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yuae734272015-07-04 17:38:48 -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 Pesavento8aad3722017-09-16 20:57:28 -040022#include "security/transform/digest-filter.hpp"
23
24#include "encoding/buffer-stream.hpp"
Yingdi Yuae734272015-07-04 17:38:48 -070025#include "security/transform/buffer-source.hpp"
26#include "security/transform/step-source.hpp"
Yingdi Yuae734272015-07-04 17:38:48 -070027#include "security/transform/stream-sink.hpp"
Yingdi Yuae734272015-07-04 17:38:48 -070028
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(TestDigestFilter)
39
Davide Pesavento8aad3722017-09-16 20:57:28 -040040static const uint8_t in[] = {
41 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};
58static const uint8_t out[] = {
59 0x3e, 0x14, 0xfd, 0x66, 0x9a, 0x79, 0x80, 0x65,
60 0xc4, 0x0d, 0x61, 0xf8, 0x6a, 0xc7, 0x98, 0x29,
61 0xc0, 0x6b, 0x90, 0x8f, 0xbb, 0x19, 0xa0, 0x85,
62 0xf7, 0xfa, 0x7b, 0x2d, 0xd6, 0x8c, 0xd5, 0xa3
63};
64
Yingdi Yuae734272015-07-04 17:38:48 -070065BOOST_AUTO_TEST_CASE(Basic)
66{
Yingdi Yuae734272015-07-04 17:38:48 -070067 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{
Yingdi Yuae734272015-07-04 17:38:48 -070076 StepSource source;
77 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -070078 source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
Yingdi Yuae734272015-07-04 17:38:48 -070079 source.write(in, 32);
80 source.write(in + 32, 1);
81 source.write(in + 33, 2);
82 source.write(in + 35, 3);
83 source.write(in + 38, 26);
84 source.write(in + 64, 64);
85 source.end();
86
87 ConstBufferPtr buf = os.buf();
88 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end());
89}
90
91BOOST_AUTO_TEST_CASE(EmptyInput)
92{
Davide Pesavento8aad3722017-09-16 20:57:28 -040093 const uint8_t out[] = {
Yingdi Yuae734272015-07-04 17:38:48 -070094 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
95 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
96 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
97 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
98 };
99
100 OBufferStream os;
101 StepSource source;
Yingdi Yu99b2a002015-08-12 12:47:44 -0700102 source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
Yingdi Yuae734272015-07-04 17:38:48 -0700103 source.end();
104
105 ConstBufferPtr buf = os.buf();
106 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end());
107}
108
Davide Pesavento8aad3722017-09-16 20:57:28 -0400109BOOST_AUTO_TEST_CASE(InvalidAlgorithm)
Yingdi Yuae734272015-07-04 17:38:48 -0700110{
Davide Pesavento8aad3722017-09-16 20:57:28 -0400111 BOOST_CHECK_THROW(DigestFilter{DigestAlgorithm::NONE}, Error);
Yingdi Yuae734272015-07-04 17:38:48 -0700112}
113
114BOOST_AUTO_TEST_SUITE_END() // TestDigestFilter
115BOOST_AUTO_TEST_SUITE_END() // Transform
116BOOST_AUTO_TEST_SUITE_END() // Security
117
118} // namespace tests
119} // namespace transform
120} // namespace security
121} // namespace ndn