blob: e14da4182c5bbffd18285e79e54b88941ee98995 [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/*
Davide Pesaventoaf4e4e72024-01-26 14:30:11 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento5d2f1512023-08-11 14:50:51 -040024namespace ndn::nac::tests {
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040025
26class EncryptedContentFixture
27{
28public:
29 EncryptedContentFixture()
30 {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050031 BOOST_ASSERT(randomBlock.value_size() == 3);
32 BOOST_ASSERT(randomBuffer->size() == 10);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040033 }
34
35public:
36 EncryptedContent content;
37 Block randomBlock = "01 03 000000"_block;
Davide Pesaventoaf4e4e72024-01-26 14:30:11 -050038 ConstBufferPtr randomBuffer = std::make_shared<const Buffer>(10);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040039};
40
41BOOST_FIXTURE_TEST_SUITE(TestEncryptedContent, EncryptedContentFixture)
42
43BOOST_AUTO_TEST_CASE(Constructor)
44{
45 BOOST_CHECK_THROW(content.wireEncode(), tlv::Error);
46 content.setPayload(randomBlock);
47
48 BOOST_CHECK(!content.hasIv());
49 BOOST_CHECK(!content.hasPayloadKey());
50 BOOST_CHECK(!content.hasKeyLocator());
51
52 BOOST_CHECK_EQUAL(content.wireEncode(), "82 07 84050103000000"_block);
53
54 content.setIv(randomBlock);
55 BOOST_CHECK_EQUAL(content.wireEncode(), "82[0E]=8405010300000085050103000000"_block);
56
57 content.setKeyLocator("/random/name");
58 BOOST_CHECK_EQUAL(content.wireEncode(), "82[1E]=8405010300000085050103000000070E080672616E646F6D08046E616D65"_block);
59
60 content = EncryptedContent("82 07 84050103000000"_block);
61 BOOST_CHECK(!content.hasIv());
62 BOOST_CHECK(!content.hasPayloadKey());
63 BOOST_CHECK(!content.hasKeyLocator());
64
65 content = EncryptedContent("82 1E 8505010300000084050103000000070E080672616E646F6D08046E616D65"_block);
66 BOOST_CHECK(content.hasIv());
67 BOOST_CHECK(!content.hasPayloadKey());
68 BOOST_CHECK(content.hasKeyLocator());
69}
70
71BOOST_AUTO_TEST_SUITE(SetterGetter)
72
73BOOST_AUTO_TEST_CASE(Iv)
74{
75 content.setPayload(randomBlock);
76
77 content.setIv(randomBlock);
78 BOOST_REQUIRE(content.hasIv());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040079 BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040080 BOOST_CHECK_EQUAL(content.getIv().blockFromValue(), randomBlock);
81
82 content.unsetIv();
83 BOOST_CHECK(!content.hasIv());
84
85 content.setIv(randomBuffer);
86 BOOST_REQUIRE(content.hasIv());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040087 BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040088 BOOST_CHECK_THROW(content.getIv().blockFromValue(), tlv::Error);
89 BOOST_CHECK_EQUAL(content.getIv().value_size(), randomBuffer->size());
90
91 content = EncryptedContent("82[13]=84050103000000850A00000000000000000000"_block);
92 BOOST_REQUIRE(content.hasIv());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040093 BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector);
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040094 BOOST_CHECK_THROW(content.getIv().blockFromValue(), tlv::Error);
95 BOOST_CHECK_EQUAL(content.getIv().value_size(), randomBuffer->size());
96}
97
98BOOST_AUTO_TEST_CASE(Payload)
99{
100 content.setPayload(randomBlock);
101 BOOST_CHECK_EQUAL(content.getPayload().type(), tlv::EncryptedPayload);
102 BOOST_CHECK_EQUAL(content.getPayload().blockFromValue(), randomBlock);
103
104 content.setPayload(randomBuffer);
105 BOOST_CHECK_EQUAL(content.getPayload().type(), tlv::EncryptedPayload);
106 BOOST_CHECK_THROW(content.getPayload().blockFromValue(), tlv::Error);
107 BOOST_CHECK_EQUAL(content.getPayload().value_size(), randomBuffer->size());
108
109 content = EncryptedContent("82[0C]=840A00000000000000000000"_block);
110 BOOST_CHECK_EQUAL(content.getPayload().type(), tlv::EncryptedPayload);
111 BOOST_CHECK_THROW(content.getPayload().blockFromValue(), tlv::Error);
112 BOOST_CHECK_EQUAL(content.getPayload().value_size(), randomBuffer->size());
113}
114
115BOOST_AUTO_TEST_CASE(PayloadKey)
116{
117 content.setPayload(randomBlock);
118
119 content.setPayloadKey(randomBlock);
120 BOOST_REQUIRE(content.hasPayloadKey());
121 BOOST_CHECK_EQUAL(content.getPayloadKey().type(), tlv::EncryptedPayloadKey);
122 BOOST_CHECK_EQUAL(content.getPayloadKey().blockFromValue(), randomBlock);
123
124 content.unsetPayloadKey();
125 BOOST_CHECK(!content.hasPayloadKey());
126
127 content.setPayloadKey(randomBuffer);
128 BOOST_REQUIRE(content.hasPayloadKey());
129 BOOST_CHECK_EQUAL(content.getPayloadKey().type(), tlv::EncryptedPayloadKey);
130 BOOST_CHECK_THROW(content.getPayloadKey().blockFromValue(), tlv::Error);
131 BOOST_CHECK_EQUAL(content.getPayloadKey().value_size(), randomBuffer->size());
132
133 content = EncryptedContent("82[13]=84050103000000860A00000000000000000000"_block);
134 BOOST_CHECK_EQUAL(content.getPayloadKey().type(), tlv::EncryptedPayloadKey);
135 BOOST_CHECK_THROW(content.getPayloadKey().blockFromValue(), tlv::Error);
136 BOOST_CHECK_EQUAL(content.getPayloadKey().value_size(), randomBuffer->size());
137}
138
139BOOST_AUTO_TEST_CASE(KeyLocator)
140{
141 content.setPayload(randomBlock);
142
143 content.setKeyLocator("/random/name");
144 BOOST_REQUIRE(content.hasKeyLocator());
145 BOOST_CHECK_EQUAL(content.getKeyLocator(), "/random/name");
146
147 content.unsetPayloadKey();
148 BOOST_CHECK(!content.hasPayloadKey());
149
150 content = EncryptedContent("82[17]=84050103000000070E080672616E646F6D08046E616D65"_block);
151 BOOST_REQUIRE(content.hasKeyLocator());
152 BOOST_CHECK_EQUAL(content.getKeyLocator(), "/random/name");
153}
154
155BOOST_AUTO_TEST_SUITE_END() // SetterGetter
156
157BOOST_AUTO_TEST_SUITE_END()
158
Davide Pesavento5d2f1512023-08-11 14:50:51 -0400159} // namespace ndn::nac::tests