blob: 650c0243d1a51abe5834d8b59f990d54d0b11fec [file] [log] [blame]
Yingdi Yud12fb972015-08-01 17:38:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento8aad3722017-09-16 20:57:28 -04002/*
Davide Pesavento74daf742018-11-23 18:14:13 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yud12fb972015-08-01 17:38:49 -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/hmac-filter.hpp"
Davide Pesavento8aad3722017-09-16 20:57:28 -040023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#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 Yud12fb972015-08-01 17:38:49 -070028
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include "tests/boost-test.hpp"
Yingdi Yud12fb972015-08-01 17:38:49 -070030
31namespace ndn {
32namespace security {
33namespace transform {
34namespace tests {
35
36BOOST_AUTO_TEST_SUITE(Security)
37BOOST_AUTO_TEST_SUITE(Transform)
38BOOST_AUTO_TEST_SUITE(TestHmacFilter)
39
Davide Pesavento8aad3722017-09-16 20:57:28 -040040static const uint8_t key[] = {
41 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
42 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
43};
44static const uint8_t data[] = {
45 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
46 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
47};
48static 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 Yud12fb972015-08-01 17:38:49 -070055BOOST_AUTO_TEST_CASE(Basic)
56{
Yingdi Yud12fb972015-08-01 17:38:49 -070057 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -070058 bufferSource(data, sizeof(data)) >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os);
Yingdi Yud12fb972015-08-01 17:38:49 -070059
60 ConstBufferPtr buf = os.buf();
61 BOOST_CHECK_EQUAL_COLLECTIONS(digest, digest + sizeof(digest), buf->begin(), buf->end());
62}
63
64BOOST_AUTO_TEST_CASE(StepByStep)
65{
Yingdi Yud12fb972015-08-01 17:38:49 -070066 OBufferStream os;
67 StepSource source;
Yingdi Yu99b2a002015-08-12 12:47:44 -070068 source >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os);
Yingdi Yud12fb972015-08-01 17:38:49 -070069 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
81BOOST_AUTO_TEST_CASE(EmptyInput)
82{
Davide Pesavento8aad3722017-09-16 20:57:28 -040083 const uint8_t digest[] = {
Yingdi Yud12fb972015-08-01 17:38:49 -070084 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 Yu99b2a002015-08-12 12:47:44 -070092 source >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os);
Yingdi Yud12fb972015-08-01 17:38:49 -070093 source.end();
94
95 ConstBufferPtr buf = os.buf();
96 BOOST_CHECK_EQUAL_COLLECTIONS(digest, digest + sizeof(digest), buf->begin(), buf->end());
97}
98
Davide Pesavento8aad3722017-09-16 20:57:28 -040099BOOST_AUTO_TEST_CASE(InvalidAlgorithm)
Yingdi Yud12fb972015-08-01 17:38:49 -0700100{
Davide Pesavento8aad3722017-09-16 20:57:28 -0400101 BOOST_CHECK_THROW(HmacFilter(DigestAlgorithm::NONE, key, sizeof(key)), Error);
Yingdi Yud12fb972015-08-01 17:38:49 -0700102}
103
104BOOST_AUTO_TEST_SUITE_END() // TestHmacFilter
105BOOST_AUTO_TEST_SUITE_END() // Transform
106BOOST_AUTO_TEST_SUITE_END() // Security
107
108} // namespace tests
109} // namespace transform
110} // namespace security
111} // namespace ndn