Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9062a50 | 2020-01-04 17:14:04 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2020, Regents of the University of California |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 4 | * |
| 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 Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 22 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | namespace nac { |
| 26 | namespace tests { |
| 27 | |
| 28 | class EncryptedContentFixture |
| 29 | { |
| 30 | public: |
| 31 | EncryptedContentFixture() |
| 32 | { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 33 | BOOST_ASSERT(randomBlock.value_size() == 3); |
| 34 | BOOST_ASSERT(randomBuffer->size() == 10); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | public: |
| 38 | EncryptedContent content; |
| 39 | Block randomBlock = "01 03 000000"_block; |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 40 | ConstBufferPtr randomBuffer = make_shared<const Buffer>(10); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | BOOST_FIXTURE_TEST_SUITE(TestEncryptedContent, EncryptedContentFixture) |
| 44 | |
| 45 | BOOST_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 | |
| 73 | BOOST_AUTO_TEST_SUITE(SetterGetter) |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(Iv) |
| 76 | { |
| 77 | content.setPayload(randomBlock); |
| 78 | |
| 79 | content.setIv(randomBlock); |
| 80 | BOOST_REQUIRE(content.hasIv()); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 82 | 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 Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 90 | 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 Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(content.getIv().type(), tlv::InitializationVector); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 96 | BOOST_CHECK_THROW(content.getIv().blockFromValue(), tlv::Error); |
| 97 | BOOST_CHECK_EQUAL(content.getIv().value_size(), randomBuffer->size()); |
| 98 | } |
| 99 | |
| 100 | BOOST_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 | |
| 117 | BOOST_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 | |
| 141 | BOOST_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 | |
| 157 | BOOST_AUTO_TEST_SUITE_END() // SetterGetter |
| 158 | |
| 159 | BOOST_AUTO_TEST_SUITE_END() |
| 160 | |
| 161 | } // namespace tests |
| 162 | } // namespace nac |
| 163 | } // namespace ndn |