Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 22 | #include "security/transform/digest-filter.hpp" |
| 23 | |
| 24 | #include "encoding/buffer-stream.hpp" |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 25 | #include "security/transform/buffer-source.hpp" |
| 26 | #include "security/transform/step-source.hpp" |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 27 | #include "security/transform/stream-sink.hpp" |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 28 | |
| 29 | #include "boost-test.hpp" |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
| 33 | namespace transform { |
| 34 | namespace tests { |
| 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(Security) |
| 37 | BOOST_AUTO_TEST_SUITE(Transform) |
| 38 | BOOST_AUTO_TEST_SUITE(TestDigestFilter) |
| 39 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 40 | static 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 | }; |
| 58 | static 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 Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 65 | BOOST_AUTO_TEST_CASE(Basic) |
| 66 | { |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 67 | OBufferStream os; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 68 | bufferSource(in, sizeof(in)) >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 69 | |
| 70 | ConstBufferPtr buf = os.buf(); |
| 71 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end()); |
| 72 | } |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE(StepByStep) |
| 75 | { |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 76 | StepSource source; |
| 77 | OBufferStream os; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 78 | source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 79 | 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 | |
| 91 | BOOST_AUTO_TEST_CASE(EmptyInput) |
| 92 | { |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 93 | const uint8_t out[] = { |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 94 | 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 Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 102 | source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 103 | source.end(); |
| 104 | |
| 105 | ConstBufferPtr buf = os.buf(); |
| 106 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end()); |
| 107 | } |
| 108 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(InvalidAlgorithm) |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 110 | { |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 111 | BOOST_CHECK_THROW(DigestFilter{DigestAlgorithm::NONE}, Error); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_SUITE_END() // TestDigestFilter |
| 115 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 116 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 117 | |
| 118 | } // namespace tests |
| 119 | } // namespace transform |
| 120 | } // namespace security |
| 121 | } // namespace ndn |