blob: ccb5feb0e2bcfceb0e52b6eed04a54ab6acd8290 [file] [log] [blame]
Yingdi Yud12fb972015-08-01 17:38:49 -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/hmac-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(TestHmacFilter)
38
39BOOST_AUTO_TEST_CASE(Basic)
40{
41 uint8_t key[16] = {
42 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
43 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
44 };
45
46 uint8_t data[16] = {
47 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
48 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
49 };
50
51 uint8_t digest[32] = {
52 0x9f, 0x3a, 0xa2, 0x88, 0x26, 0xb3, 0x74, 0x85,
53 0xca, 0x05, 0x01, 0x4d, 0x71, 0x42, 0xb3, 0xea,
54 0x3f, 0xfb, 0xda, 0x5a, 0x35, 0xbf, 0xd2, 0x0f,
55 0x2f, 0x9c, 0x8f, 0xcc, 0x6d, 0x30, 0x48, 0x54
56 };
57
58 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -070059 bufferSource(data, sizeof(data)) >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os);
Yingdi Yud12fb972015-08-01 17:38:49 -070060
61 ConstBufferPtr buf = os.buf();
62 BOOST_CHECK_EQUAL_COLLECTIONS(digest, digest + sizeof(digest), buf->begin(), buf->end());
63}
64
65BOOST_AUTO_TEST_CASE(StepByStep)
66{
67 uint8_t key[16] = {
68 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
69 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
70 };
71
72 uint8_t data[16] = {
73 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
74 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
75 };
76
77 uint8_t digest[32] = {
78 0x9f, 0x3a, 0xa2, 0x88, 0x26, 0xb3, 0x74, 0x85,
79 0xca, 0x05, 0x01, 0x4d, 0x71, 0x42, 0xb3, 0xea,
80 0x3f, 0xfb, 0xda, 0x5a, 0x35, 0xbf, 0xd2, 0x0f,
81 0x2f, 0x9c, 0x8f, 0xcc, 0x6d, 0x30, 0x48, 0x54
82 };
83
84 OBufferStream os;
85 StepSource source;
Yingdi Yu99b2a002015-08-12 12:47:44 -070086 source >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os);
Yingdi Yud12fb972015-08-01 17:38:49 -070087 source.write(data, 1);
88 source.write(data + 1, 2);
89 source.write(data + 3, 3);
90 source.write(data + 6, 4);
91 source.write(data + 10, 5);
92 source.write(data + 15, 1);
93 source.end();
94
95 ConstBufferPtr buf = os.buf();
96 BOOST_CHECK_EQUAL_COLLECTIONS(digest, digest + sizeof(digest), buf->begin(), buf->end());
97}
98
99BOOST_AUTO_TEST_CASE(EmptyInput)
100{
101 uint8_t key[16] = {
102 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
103 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
104 };
105
106 uint8_t digest[32] = {
107 0x07, 0xEF, 0xF8, 0xB3, 0x26, 0xB7, 0x79, 0x8C,
108 0x9C, 0xCF, 0xCB, 0xDB, 0xE5, 0x79, 0x48, 0x9A,
109 0xC7, 0x85, 0xA7, 0x99, 0x5A, 0x04, 0x61, 0x8B,
110 0x1A, 0x28, 0x13, 0xC2, 0x67, 0x44, 0x77, 0x7D
111 };
112
113 OBufferStream os;
114 StepSource source;
Yingdi Yu99b2a002015-08-12 12:47:44 -0700115 source >> hmacFilter(DigestAlgorithm::SHA256, key, sizeof(key)) >> streamSink(os);
Yingdi Yud12fb972015-08-01 17:38:49 -0700116 source.end();
117
118 ConstBufferPtr buf = os.buf();
119 BOOST_CHECK_EQUAL_COLLECTIONS(digest, digest + sizeof(digest), buf->begin(), buf->end());
120}
121
122BOOST_AUTO_TEST_CASE(Error)
123{
124 uint8_t key[16] = {
125 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
126 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
127 };
128
129 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -0700130 BOOST_REQUIRE_THROW(stepSource() >> hmacFilter(DigestAlgorithm::NONE, key, sizeof(key)) >> streamSink(os),
Yingdi Yud12fb972015-08-01 17:38:49 -0700131 transform::Error);
132}
133
134BOOST_AUTO_TEST_SUITE_END() // TestHmacFilter
135BOOST_AUTO_TEST_SUITE_END() // Transform
136BOOST_AUTO_TEST_SUITE_END() // Security
137
138} // namespace tests
139} // namespace transform
140} // namespace security
141} // namespace ndn