blob: 71ebbb1addf38e367a860580112f705eee9a2631 [file] [log] [blame]
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento9062a502020-01-04 17:14:04 -05002/*
3 * Copyright (c) 2014-2020, Regents of the University of California
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -04004 *
5 * This file is part of NAC (Name-Based Access Control for NDN).
6 * See AUTHORS.md for complete list of NAC authors and contributors.
7 *
8 * NAC is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "encrypted-content.hpp"
21
Davide Pesaventoba3f6892020-12-08 22:18:35 -050022#include "tests/boost-test.hpp"
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040023
24namespace ndn {
25namespace nac {
26namespace tests {
27
28class EncryptedContentFixture
29{
30public:
31 EncryptedContentFixture()
32 {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050033 BOOST_ASSERT(randomBlock.value_size() == 3);
34 BOOST_ASSERT(randomBuffer->size() == 10);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040035 }
36
37public:
38 EncryptedContent content;
39 Block randomBlock = "01 03 000000"_block;
Davide Pesaventoba3f6892020-12-08 22:18:35 -050040 ConstBufferPtr randomBuffer = make_shared<const Buffer>(10);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040041};
42
43BOOST_FIXTURE_TEST_SUITE(TestEncryptedContent, EncryptedContentFixture)
44
45BOOST_AUTO_TEST_CASE(Constructor)
46{
47 BOOST_CHECK_THROW(content.wireEncode(), tlv::Error);
48 content.setPayload(randomBlock);
49
50 BOOST_CHECK(!content.hasIv());
51 BOOST_CHECK(!content.hasPayloadKey());
52 BOOST_CHECK(!content.hasKeyLocator());
53
54 BOOST_CHECK_EQUAL(content.wireEncode(), "82 07 84050103000000"_block);
55
56 content.setIv(randomBlock);
57 BOOST_CHECK_EQUAL(content.wireEncode(), "82[0E]=8405010300000085050103000000"_block);
58
59 content.setKeyLocator("/random/name");
60 BOOST_CHECK_EQUAL(content.wireEncode(), "82[1E]=8405010300000085050103000000070E080672616E646F6D08046E616D65"_block);
61
62 content = EncryptedContent("82 07 84050103000000"_block);
63 BOOST_CHECK(!content.hasIv());
64 BOOST_CHECK(!content.hasPayloadKey());
65 BOOST_CHECK(!content.hasKeyLocator());
66
67 content = EncryptedContent("82 1E 8505010300000084050103000000070E080672616E646F6D08046E616D65"_block);
68 BOOST_CHECK(content.hasIv());
69 BOOST_CHECK(!content.hasPayloadKey());
70 BOOST_CHECK(content.hasKeyLocator());
71}
72
73BOOST_AUTO_TEST_SUITE(SetterGetter)
74
75BOOST_AUTO_TEST_CASE(Iv)
76{
77 content.setPayload(randomBlock);
78
79 content.setIv(randomBlock);
80 BOOST_REQUIRE(content.hasIv());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040081 BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040082 BOOST_CHECK_EQUAL(content.getIv().blockFromValue(), randomBlock);
83
84 content.unsetIv();
85 BOOST_CHECK(!content.hasIv());
86
87 content.setIv(randomBuffer);
88 BOOST_REQUIRE(content.hasIv());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040089 BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040090 BOOST_CHECK_THROW(content.getIv().blockFromValue(), tlv::Error);
91 BOOST_CHECK_EQUAL(content.getIv().value_size(), randomBuffer->size());
92
93 content = EncryptedContent("82[13]=84050103000000850A00000000000000000000"_block);
94 BOOST_REQUIRE(content.hasIv());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040095 BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040096 BOOST_CHECK_THROW(content.getIv().blockFromValue(), tlv::Error);
97 BOOST_CHECK_EQUAL(content.getIv().value_size(), randomBuffer->size());
98}
99
100BOOST_AUTO_TEST_CASE(Payload)
101{
102 content.setPayload(randomBlock);
103 BOOST_CHECK_EQUAL(content.getPayload().type(), tlv::EncryptedPayload);
104 BOOST_CHECK_EQUAL(content.getPayload().blockFromValue(), randomBlock);
105
106 content.setPayload(randomBuffer);
107 BOOST_CHECK_EQUAL(content.getPayload().type(), tlv::EncryptedPayload);
108 BOOST_CHECK_THROW(content.getPayload().blockFromValue(), tlv::Error);
109 BOOST_CHECK_EQUAL(content.getPayload().value_size(), randomBuffer->size());
110
111 content = EncryptedContent("82[0C]=840A00000000000000000000"_block);
112 BOOST_CHECK_EQUAL(content.getPayload().type(), tlv::EncryptedPayload);
113 BOOST_CHECK_THROW(content.getPayload().blockFromValue(), tlv::Error);
114 BOOST_CHECK_EQUAL(content.getPayload().value_size(), randomBuffer->size());
115}
116
117BOOST_AUTO_TEST_CASE(PayloadKey)
118{
119 content.setPayload(randomBlock);
120
121 content.setPayloadKey(randomBlock);
122 BOOST_REQUIRE(content.hasPayloadKey());
123 BOOST_CHECK_EQUAL(content.getPayloadKey().type(), tlv::EncryptedPayloadKey);
124 BOOST_CHECK_EQUAL(content.getPayloadKey().blockFromValue(), randomBlock);
125
126 content.unsetPayloadKey();
127 BOOST_CHECK(!content.hasPayloadKey());
128
129 content.setPayloadKey(randomBuffer);
130 BOOST_REQUIRE(content.hasPayloadKey());
131 BOOST_CHECK_EQUAL(content.getPayloadKey().type(), tlv::EncryptedPayloadKey);
132 BOOST_CHECK_THROW(content.getPayloadKey().blockFromValue(), tlv::Error);
133 BOOST_CHECK_EQUAL(content.getPayloadKey().value_size(), randomBuffer->size());
134
135 content = EncryptedContent("82[13]=84050103000000860A00000000000000000000"_block);
136 BOOST_CHECK_EQUAL(content.getPayloadKey().type(), tlv::EncryptedPayloadKey);
137 BOOST_CHECK_THROW(content.getPayloadKey().blockFromValue(), tlv::Error);
138 BOOST_CHECK_EQUAL(content.getPayloadKey().value_size(), randomBuffer->size());
139}
140
141BOOST_AUTO_TEST_CASE(KeyLocator)
142{
143 content.setPayload(randomBlock);
144
145 content.setKeyLocator("/random/name");
146 BOOST_REQUIRE(content.hasKeyLocator());
147 BOOST_CHECK_EQUAL(content.getKeyLocator(), "/random/name");
148
149 content.unsetPayloadKey();
150 BOOST_CHECK(!content.hasPayloadKey());
151
152 content = EncryptedContent("82[17]=84050103000000070E080672616E646F6D08046E616D65"_block);
153 BOOST_REQUIRE(content.hasKeyLocator());
154 BOOST_CHECK_EQUAL(content.getKeyLocator(), "/random/name");
155}
156
157BOOST_AUTO_TEST_SUITE_END() // SetterGetter
158
159BOOST_AUTO_TEST_SUITE_END()
160
161} // namespace tests
162} // namespace nac
163} // namespace ndn