Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -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 | /* |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -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 | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/transform/hmac-filter.hpp" |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
| 25 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 26 | #include "ndn-cxx/security/transform/step-source.hpp" |
| 27 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "tests/boost-test.hpp" |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 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(TestHmacFilter) |
| 39 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 40 | static const uint8_t key[] = { |
| 41 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 42 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 43 | }; |
| 44 | static const uint8_t data[] = { |
| 45 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 46 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 47 | }; |
| 48 | static const uint8_t digest[] = { |
| 49 | 0x9f, 0x3a, 0xa2, 0x88, 0x26, 0xb3, 0x74, 0x85, |
| 50 | 0xca, 0x05, 0x01, 0x4d, 0x71, 0x42, 0xb3, 0xea, |
| 51 | 0x3f, 0xfb, 0xda, 0x5a, 0x35, 0xbf, 0xd2, 0x0f, |
| 52 | 0x2f, 0x9c, 0x8f, 0xcc, 0x6d, 0x30, 0x48, 0x54 |
| 53 | }; |
| 54 | |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(Basic) |
| 56 | { |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 57 | OBufferStream os; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 58 | bufferSource(data, sizeof(data)) >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os); |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 59 | |
| 60 | ConstBufferPtr buf = os.buf(); |
| 61 | BOOST_CHECK_EQUAL_COLLECTIONS(digest, digest + sizeof(digest), buf->begin(), buf->end()); |
| 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(StepByStep) |
| 65 | { |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 66 | OBufferStream os; |
| 67 | StepSource source; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 68 | source >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os); |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 69 | source.write(data, 1); |
| 70 | source.write(data + 1, 2); |
| 71 | source.write(data + 3, 3); |
| 72 | source.write(data + 6, 4); |
| 73 | source.write(data + 10, 5); |
| 74 | source.write(data + 15, 1); |
| 75 | source.end(); |
| 76 | |
| 77 | ConstBufferPtr buf = os.buf(); |
| 78 | BOOST_CHECK_EQUAL_COLLECTIONS(digest, digest + sizeof(digest), buf->begin(), buf->end()); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(EmptyInput) |
| 82 | { |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 83 | const uint8_t digest[] = { |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 84 | 0x07, 0xEF, 0xF8, 0xB3, 0x26, 0xB7, 0x79, 0x8C, |
| 85 | 0x9C, 0xCF, 0xCB, 0xDB, 0xE5, 0x79, 0x48, 0x9A, |
| 86 | 0xC7, 0x85, 0xA7, 0x99, 0x5A, 0x04, 0x61, 0x8B, |
| 87 | 0x1A, 0x28, 0x13, 0xC2, 0x67, 0x44, 0x77, 0x7D |
| 88 | }; |
| 89 | |
| 90 | OBufferStream os; |
| 91 | StepSource source; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 92 | source >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os); |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 93 | source.end(); |
| 94 | |
| 95 | ConstBufferPtr buf = os.buf(); |
| 96 | BOOST_CHECK_EQUAL_COLLECTIONS(digest, digest + sizeof(digest), buf->begin(), buf->end()); |
| 97 | } |
| 98 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 99 | BOOST_AUTO_TEST_CASE(InvalidAlgorithm) |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 100 | { |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 101 | BOOST_CHECK_THROW(HmacFilter(DigestAlgorithm::NONE, key, sizeof(key)), Error); |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_SUITE_END() // TestHmacFilter |
| 105 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 106 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 107 | |
| 108 | } // namespace tests |
| 109 | } // namespace transform |
| 110 | } // namespace security |
| 111 | } // namespace ndn |