Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 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. |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 8 | * NAC is free software: you can redistribute it and/or modify it under the terms |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 12 | * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 17 | * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "encrypted-content.hpp" |
| 21 | |
| 22 | #include "boost-test.hpp" |
| 23 | #include <algorithm> |
| 24 | |
| 25 | namespace ndn { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 26 | namespace nac { |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 27 | namespace tests { |
| 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(TestEncryptedContent) |
| 30 | |
| 31 | const uint8_t encrypted[] = { |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 32 | 0x82, 0x30, // EncryptedContent |
| 33 | 0x1c, 0x16, // KeyLocator |
| 34 | 0x07, 0x14, // Name |
| 35 | 0x08, 0x04, |
| 36 | 0x74, 0x65, 0x73, 0x74, // 'test' |
| 37 | 0x08, 0x03, |
| 38 | 0x6b, 0x65, 0x79, // 'key' |
| 39 | 0x08, 0x07, |
| 40 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator' |
| 41 | 0x83, 0x01, // EncryptedAlgorithm |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 42 | 0x03, |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 43 | 0x85, 0x0a, // InitialVector |
| 44 | 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73, |
| 45 | 0x84, 0x07, // EncryptedPayload |
| 46 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 47 | }; |
| 48 | |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 49 | const uint8_t encryptedNoIv[] = { |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 50 | 0x82, 0x24, // EncryptedContent |
| 51 | 0x1c, 0x16, // KeyLocator |
| 52 | 0x07, 0x14, // Name |
| 53 | 0x08, 0x04, |
| 54 | 0x74, 0x65, 0x73, 0x74, // 'test' |
| 55 | 0x08, 0x03, |
| 56 | 0x6b, 0x65, 0x79, // 'key' |
| 57 | 0x08, 0x07, |
| 58 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator' |
| 59 | 0x83, 0x01, // EncryptedAlgorithm |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 60 | 0x03, |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 61 | 0x84, 0x07, // EncryptedPayload |
| 62 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 63 | }; |
| 64 | |
| 65 | const uint8_t message[] = { |
| 66 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 67 | }; |
| 68 | |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 69 | const uint8_t iv[] = { |
| 70 | 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73 |
| 71 | }; |
| 72 | |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 73 | BOOST_AUTO_TEST_CASE(Constructor) |
| 74 | { |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 75 | // checking default setting. |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 76 | EncryptedContent content; |
| 77 | BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(content.getPayload().size(), 0); |
| 79 | BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(content.hasKeyLocator(), false); |
| 81 | BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error); |
| 82 | |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 83 | // check an encrypted content with IV |
| 84 | EncryptedContent rsaOaepContent(tlv::AlgorithmRsaOaep, |
| 85 | KeyLocator("test/key/locator"), |
| 86 | message, sizeof(message), |
| 87 | iv, sizeof(iv)); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 88 | |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(rsaOaepContent.getAlgorithmType(), tlv::AlgorithmRsaOaep); |
| 90 | BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContent.getPayload().begin(), |
| 91 | rsaOaepContent.getPayload().end(), |
| 92 | message, |
| 93 | message + sizeof(message)); |
| 94 | BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContent.getInitialVector().begin(), |
| 95 | rsaOaepContent.getInitialVector().end(), |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 96 | iv, |
| 97 | iv + sizeof(iv)); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(rsaOaepContent.hasKeyLocator(), true); |
| 99 | BOOST_CHECK_NO_THROW(rsaOaepContent.getKeyLocator()); |
| 100 | BOOST_CHECK_EQUAL(rsaOaepContent.getKeyLocator().getName(), Name("test/key/locator")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 101 | |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 102 | // encoding |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 103 | Block encryptedBlock(encrypted, sizeof(encrypted)); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 104 | const Block& encoded = rsaOaepContent.wireEncode(); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 105 | |
| 106 | BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock.wire(), |
| 107 | encryptedBlock.wire() + encryptedBlock.size(), |
| 108 | encoded.wire(), |
| 109 | encoded.wire() + encoded.size()); |
| 110 | |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 111 | // decoding |
| 112 | EncryptedContent rsaOaepContent2(encryptedBlock); |
| 113 | BOOST_CHECK_EQUAL(rsaOaepContent2.getAlgorithmType(), tlv::AlgorithmRsaOaep); |
| 114 | BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContent2.getPayload().begin(), |
| 115 | rsaOaepContent2.getPayload().end(), |
| 116 | message, |
| 117 | message + sizeof(message)); |
| 118 | BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContent2.getInitialVector().begin(), |
| 119 | rsaOaepContent2.getInitialVector().end(), |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 120 | iv, |
| 121 | iv + sizeof(iv)); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(rsaOaepContent2.hasKeyLocator(), true); |
| 123 | BOOST_CHECK_NO_THROW(rsaOaepContent2.getKeyLocator()); |
| 124 | BOOST_CHECK_EQUAL(rsaOaepContent2.getKeyLocator().getName(), Name("test/key/locator")); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 125 | |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 126 | |
| 127 | // Check no IV case |
| 128 | EncryptedContent rsaOaepContentNoIv(tlv::AlgorithmRsaOaep, |
| 129 | KeyLocator("test/key/locator"), |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 130 | message, sizeof(message)); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(rsaOaepContentNoIv.getAlgorithmType(), tlv::AlgorithmRsaOaep); |
| 132 | BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContentNoIv.getPayload().begin(), |
| 133 | rsaOaepContentNoIv.getPayload().end(), |
| 134 | message, |
| 135 | message + sizeof(message)); |
| 136 | BOOST_CHECK_EQUAL(rsaOaepContentNoIv.getInitialVector().size(), 0); |
| 137 | BOOST_CHECK_EQUAL(rsaOaepContentNoIv.hasKeyLocator(), true); |
| 138 | BOOST_CHECK_NO_THROW(rsaOaepContentNoIv.getKeyLocator()); |
| 139 | BOOST_CHECK_EQUAL(rsaOaepContentNoIv.getKeyLocator().getName(), Name("test/key/locator")); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 140 | |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 141 | // encoding |
| 142 | Block encryptedBlock2(encryptedNoIv, sizeof(encryptedNoIv)); |
| 143 | const Block& encodedNoIV = rsaOaepContentNoIv.wireEncode(); |
| 144 | BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock2.wire(), |
| 145 | encryptedBlock2.wire() + encryptedBlock2.size(), |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 146 | encodedNoIV.wire(), |
| 147 | encodedNoIV.wire() + encodedNoIV.size()); |
| 148 | |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 149 | // decoding |
| 150 | EncryptedContent rsaOaepContentNoIv2(encryptedBlock2); |
| 151 | BOOST_CHECK_EQUAL(rsaOaepContentNoIv2.getAlgorithmType(), tlv::AlgorithmRsaOaep); |
| 152 | BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContentNoIv2.getPayload().begin(), |
| 153 | rsaOaepContentNoIv2.getPayload().end(), |
| 154 | message, |
| 155 | message + sizeof(message)); |
| 156 | BOOST_CHECK_EQUAL(rsaOaepContentNoIv2.getInitialVector().size(), 0); |
| 157 | BOOST_CHECK_EQUAL(rsaOaepContentNoIv2.hasKeyLocator(), true); |
| 158 | BOOST_CHECK_NO_THROW(rsaOaepContentNoIv2.getKeyLocator()); |
| 159 | BOOST_CHECK_EQUAL(rsaOaepContentNoIv2.getKeyLocator().getName(), Name("test/key/locator")); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 160 | |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_CASE(ConstructorError) |
| 164 | { |
| 165 | const uint8_t error1[] = { |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 166 | 0x1f, 0x30, // Wrong EncryptedContent (0x82, 0x24) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 167 | 0x1c, 0x16, // KeyLocator |
| 168 | 0x07, 0x14, // Name |
| 169 | 0x08, 0x04, |
| 170 | 0x74, 0x65, 0x73, 0x74, |
| 171 | 0x08, 0x03, |
| 172 | 0x6b, 0x65, 0x79, |
| 173 | 0x08, 0x07, |
| 174 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 175 | 0x83, 0x01, // EncryptedAlgorithm |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 176 | 0x03, |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 177 | 0x85, 0x0a, // InitialVector |
| 178 | 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73, |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 179 | 0x84, 0x07, // EncryptedPayload |
| 180 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 181 | }; |
| 182 | Block errorBlock1(error1, sizeof(error1)); |
| 183 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock1), EncryptedContent::Error); |
| 184 | |
| 185 | const uint8_t error2[] = { |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 186 | 0x82, 0x30, // EncryptedContent |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 187 | 0x1d, 0x16, // Wrong KeyLocator (0x1c, 0x16) |
| 188 | 0x07, 0x14, // Name |
| 189 | 0x08, 0x04, |
| 190 | 0x74, 0x65, 0x73, 0x74, |
| 191 | 0x08, 0x03, |
| 192 | 0x6b, 0x65, 0x79, |
| 193 | 0x08, 0x07, |
| 194 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 195 | 0x83, 0x01, // EncryptedAlgorithm |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 196 | 0x03, |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 197 | 0x85, 0x0a, // InitialVector |
| 198 | 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73, |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 199 | 0x84, 0x07, // EncryptedPayload |
| 200 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 201 | }; |
| 202 | Block errorBlock2(error2, sizeof(error2)); |
| 203 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock2), EncryptedContent::Error); |
| 204 | |
| 205 | const uint8_t error3[] = { |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 206 | 0x82, 0x30, // EncryptedContent |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 207 | 0x1c, 0x16, // KeyLocator |
| 208 | 0x07, 0x14, // Name |
| 209 | 0x08, 0x04, |
| 210 | 0x74, 0x65, 0x73, 0x74, |
| 211 | 0x08, 0x03, |
| 212 | 0x6b, 0x65, 0x79, |
| 213 | 0x08, 0x07, |
| 214 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 215 | 0x1d, 0x01, // Wrong EncryptedAlgorithm (0x83, 0x01) |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 216 | 0x03, |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 217 | 0x85, 0x0a, // InitialVector |
| 218 | 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73, |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 219 | 0x84, 0x07, // EncryptedPayload |
| 220 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 221 | }; |
| 222 | Block errorBlock3(error3, sizeof(error3)); |
| 223 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock3), EncryptedContent::Error); |
| 224 | |
| 225 | const uint8_t error4[] = { |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 226 | 0x82, 0x30, // EncryptedContent |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 227 | 0x1c, 0x16, // KeyLocator |
| 228 | 0x07, 0x14, // Name |
| 229 | 0x08, 0x04, |
| 230 | 0x74, 0x65, 0x73, 0x74, // 'test' |
| 231 | 0x08, 0x03, |
| 232 | 0x6b, 0x65, 0x79, // 'key' |
| 233 | 0x08, 0x07, |
| 234 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator' |
| 235 | 0x83, 0x01, // EncryptedAlgorithm |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 236 | 0x03, |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 237 | 0x1f, 0x0a, // InitialVector (0x84, 0x0a) |
| 238 | 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73, |
| 239 | 0x84, 0x07, // EncryptedPayload |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 240 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 241 | }; |
| 242 | Block errorBlock4(error4, sizeof(error4)); |
| 243 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock4), EncryptedContent::Error); |
| 244 | |
| 245 | const uint8_t error5[] = { |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 246 | 0x82, 0x30, // EncryptedContent |
| 247 | 0x1c, 0x16, // KeyLocator |
| 248 | 0x07, 0x14, // Name |
| 249 | 0x08, 0x04, |
| 250 | 0x74, 0x65, 0x73, 0x74, // 'test' |
| 251 | 0x08, 0x03, |
| 252 | 0x6b, 0x65, 0x79, // 'key' |
| 253 | 0x08, 0x07, |
| 254 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator' |
| 255 | 0x83, 0x01, // EncryptedAlgorithm |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 256 | 0x03, |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 257 | 0x85, 0x0a, // InitialVector |
| 258 | 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73, |
| 259 | 0x21, 0x07, // EncryptedPayload (0x85, 0x07) |
| 260 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 261 | }; |
| 262 | Block errorBlock5(error5, sizeof(error5)); |
| 263 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock5), EncryptedContent::Error); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 264 | |
| 265 | const uint8_t error6[] = { |
| 266 | 0x82, 0x00 // Empty EncryptedContent |
| 267 | }; |
| 268 | Block errorBlock6(error6, sizeof(error6)); |
| 269 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock6), EncryptedContent::Error); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | BOOST_AUTO_TEST_CASE(SetterGetter) |
| 273 | { |
| 274 | EncryptedContent content; |
| 275 | BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 276 | BOOST_CHECK_EQUAL(content.getPayload().size(), 0); |
| 277 | BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 278 | BOOST_CHECK_EQUAL(content.hasKeyLocator(), false); |
| 279 | BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error); |
| 280 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 281 | content.setAlgorithmType(tlv::AlgorithmRsaOaep); |
| 282 | BOOST_CHECK_EQUAL(content.getAlgorithmType(), tlv::AlgorithmRsaOaep); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 283 | BOOST_CHECK_EQUAL(content.getPayload().size(), 0); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 284 | BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 285 | BOOST_CHECK_EQUAL(content.hasKeyLocator(), false); |
| 286 | |
| 287 | KeyLocator keyLocator("/test/key/locator"); |
| 288 | content.setKeyLocator(keyLocator); |
| 289 | BOOST_CHECK_EQUAL(content.hasKeyLocator(), true); |
| 290 | BOOST_CHECK_NO_THROW(content.getKeyLocator()); |
| 291 | BOOST_CHECK_EQUAL(content.getKeyLocator().getName(), Name("/test/key/locator")); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 292 | BOOST_CHECK_EQUAL(content.getPayload().size(), 0); |
| 293 | BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 294 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 295 | content.setPayload(message, sizeof(message)); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 296 | BOOST_CHECK_EQUAL_COLLECTIONS(content.getPayload().begin(), |
| 297 | content.getPayload().end(), |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 298 | message, |
| 299 | message + sizeof(message)); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 300 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 301 | content.setInitialVector(iv, sizeof(iv)); |
Yingdi Yu | 416dae0 | 2016-03-09 15:50:53 -0800 | [diff] [blame] | 302 | BOOST_CHECK_EQUAL_COLLECTIONS(content.getInitialVector().begin(), |
| 303 | content.getInitialVector().end(), |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 304 | iv, |
| 305 | iv + sizeof(iv)); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 306 | |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 307 | const Block& encoded = content.wireEncode(); |
| 308 | Block contentBlock(encrypted, sizeof(encrypted)); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 309 | BOOST_CHECK_EQUAL_COLLECTIONS(contentBlock.wire(), |
| 310 | contentBlock.wire() + contentBlock.size(), |
| 311 | encoded.wire(), |
| 312 | encoded.wire() + encoded.size()); |
| 313 | } |
| 314 | BOOST_AUTO_TEST_SUITE_END() |
| 315 | |
| 316 | } // namespace tests |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 317 | } // namespace nac |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 318 | } // namespace ndn |