Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN). |
| 6 | * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors. |
| 7 | * |
| 8 | * ndn-group-encrypt 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 | * ndn-group-encrypt 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 | * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "encrypted-content.hpp" |
| 21 | |
| 22 | #include "boost-test.hpp" |
| 23 | #include <algorithm> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace gep { |
| 27 | namespace tests { |
| 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(TestEncryptedContent) |
| 30 | |
| 31 | const uint8_t encrypted[] = { |
| 32 | 0x82, 0x24, // 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 |
| 42 | 0x00, |
| 43 | 0x84, 0x07, // EncryptedPayload |
| 44 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 45 | }; |
| 46 | |
| 47 | const uint8_t message[] = { |
| 48 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 49 | }; |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE(Constructor) |
| 52 | { |
| 53 | EncryptedContent content; |
| 54 | BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1); |
| 55 | BOOST_CHECK_EQUAL(content.getPayload() == nullptr, true); |
| 56 | BOOST_CHECK_EQUAL(content.hasKeyLocator(), false); |
| 57 | BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error); |
| 58 | |
| 59 | ConstBufferPtr payload = make_shared<Buffer>(message, sizeof(message)); |
| 60 | KeyLocator keyLocator("test/key/locator"); |
| 61 | EncryptedContent sha256RsaContent(tlv::AlgorithmSha256WithRsa, keyLocator, payload); |
| 62 | ConstBufferPtr contentPayload = sha256RsaContent.getPayload(); |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmSha256WithRsa); |
| 65 | BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload->begin(), |
| 66 | contentPayload->end(), |
| 67 | payload->begin(), |
| 68 | payload->end()); |
| 69 | BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true); |
| 70 | BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator()); |
| 71 | BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator")); |
| 72 | |
| 73 | Block encryptedBlock(encrypted, sizeof(encrypted)); |
| 74 | const Block& encoded = sha256RsaContent.wireEncode(); |
| 75 | |
| 76 | BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock.wire(), |
| 77 | encryptedBlock.wire() + encryptedBlock.size(), |
| 78 | encoded.wire(), |
| 79 | encoded.wire() + encoded.size()); |
| 80 | |
| 81 | sha256RsaContent = EncryptedContent(encryptedBlock); |
| 82 | contentPayload = sha256RsaContent.getPayload(); |
| 83 | |
| 84 | BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmSha256WithRsa); |
| 85 | BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true); |
| 86 | BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload->begin(), |
| 87 | contentPayload->end(), |
| 88 | payload->begin(), |
| 89 | payload->end()); |
| 90 | BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator()); |
| 91 | BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator")); |
| 92 | } |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(ConstructorError) |
| 95 | { |
| 96 | const uint8_t error1[] = { |
| 97 | 0x1f, 0x24, // Wrong EncryptedContent (0x82, 0x24) |
| 98 | 0x1c, 0x16, // KeyLocator |
| 99 | 0x07, 0x14, // Name |
| 100 | 0x08, 0x04, |
| 101 | 0x74, 0x65, 0x73, 0x74, |
| 102 | 0x08, 0x03, |
| 103 | 0x6b, 0x65, 0x79, |
| 104 | 0x08, 0x07, |
| 105 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 106 | 0x83, 0x01, // EncryptedAlgorithm |
| 107 | 0x00, |
| 108 | 0x84, 0x07, // EncryptedPayload |
| 109 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 110 | }; |
| 111 | Block errorBlock1(error1, sizeof(error1)); |
| 112 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock1), EncryptedContent::Error); |
| 113 | |
| 114 | const uint8_t error2[] = { |
| 115 | 0x82, 0x24, // EncryptedContent |
| 116 | 0x1d, 0x16, // Wrong KeyLocator (0x1c, 0x16) |
| 117 | 0x07, 0x14, // Name |
| 118 | 0x08, 0x04, |
| 119 | 0x74, 0x65, 0x73, 0x74, |
| 120 | 0x08, 0x03, |
| 121 | 0x6b, 0x65, 0x79, |
| 122 | 0x08, 0x07, |
| 123 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 124 | 0x83, 0x01, // EncryptedAlgorithm |
| 125 | 0x00, |
| 126 | 0x84, 0x07, // EncryptedPayload |
| 127 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 128 | }; |
| 129 | Block errorBlock2(error2, sizeof(error2)); |
| 130 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock2), EncryptedContent::Error); |
| 131 | |
| 132 | const uint8_t error3[] = { |
| 133 | 0x82, 0x24, // EncryptedContent |
| 134 | 0x1c, 0x16, // KeyLocator |
| 135 | 0x07, 0x14, // Name |
| 136 | 0x08, 0x04, |
| 137 | 0x74, 0x65, 0x73, 0x74, |
| 138 | 0x08, 0x03, |
| 139 | 0x6b, 0x65, 0x79, |
| 140 | 0x08, 0x07, |
| 141 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 142 | 0x1d, 0x01, // Wrong EncryptedAlgorithm (0x83, 0x01) |
| 143 | 0x00, |
| 144 | 0x84, 0x07, // EncryptedPayload |
| 145 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 146 | }; |
| 147 | Block errorBlock3(error3, sizeof(error3)); |
| 148 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock3), EncryptedContent::Error); |
| 149 | |
| 150 | const uint8_t error4[] = { |
| 151 | 0x82, 0x24, // EncryptedContent |
| 152 | 0x1c, 0x16, // KeyLocator |
| 153 | 0x07, 0x14, // Name |
| 154 | 0x08, 0x04, |
| 155 | 0x74, 0x65, 0x73, 0x74, // 'test' |
| 156 | 0x08, 0x03, |
| 157 | 0x6b, 0x65, 0x79, // 'key' |
| 158 | 0x08, 0x07, |
| 159 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator' |
| 160 | 0x83, 0x01, // EncryptedAlgorithm |
| 161 | 0x00, |
| 162 | 0x21, 0x07, // EncryptedPayload (0x84, 0x07) |
| 163 | 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74 |
| 164 | }; |
| 165 | Block errorBlock4(error4, sizeof(error4)); |
| 166 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock4), EncryptedContent::Error); |
| 167 | |
| 168 | const uint8_t error5[] = { |
| 169 | 0x82, 0x00 // Empty EncryptedContent |
| 170 | }; |
| 171 | Block errorBlock5(error5, sizeof(error5)); |
| 172 | BOOST_CHECK_THROW(EncryptedContent info(errorBlock5), EncryptedContent::Error); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(SetterGetter) |
| 176 | { |
| 177 | EncryptedContent content; |
| 178 | BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1); |
| 179 | BOOST_CHECK_EQUAL(content.getPayload() == nullptr, true); |
| 180 | BOOST_CHECK_EQUAL(content.hasKeyLocator(), false); |
| 181 | BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error); |
| 182 | |
| 183 | content.setAlgorithmType(tlv::AlgorithmSha256WithRsa); |
| 184 | BOOST_CHECK_EQUAL(content.getAlgorithmType(), tlv::AlgorithmSha256WithRsa); |
| 185 | BOOST_CHECK_EQUAL(content.getPayload() == nullptr, true); |
| 186 | BOOST_CHECK_EQUAL(content.hasKeyLocator(), false); |
| 187 | |
| 188 | KeyLocator keyLocator("/test/key/locator"); |
| 189 | content.setKeyLocator(keyLocator); |
| 190 | BOOST_CHECK_EQUAL(content.hasKeyLocator(), true); |
| 191 | BOOST_CHECK_NO_THROW(content.getKeyLocator()); |
| 192 | BOOST_CHECK_EQUAL(content.getKeyLocator().getName(), Name("/test/key/locator")); |
| 193 | BOOST_CHECK_EQUAL(content.getPayload() == nullptr, true); |
| 194 | |
| 195 | ConstBufferPtr payload = make_shared<Buffer>(message, sizeof(message)); |
| 196 | content.setPayload(payload); |
| 197 | |
| 198 | ConstBufferPtr contentPayload = content.getPayload(); |
| 199 | BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload->begin(), |
| 200 | contentPayload->end(), |
| 201 | payload->begin(), |
| 202 | payload->end()); |
| 203 | |
| 204 | const Block& encoded = content.wireEncode(); |
| 205 | Block contentBlock(encrypted, sizeof(encrypted)); |
| 206 | |
| 207 | BOOST_CHECK_EQUAL_COLLECTIONS(contentBlock.wire(), |
| 208 | contentBlock.wire() + contentBlock.size(), |
| 209 | encoded.wire(), |
| 210 | encoded.wire() + encoded.size()); |
| 211 | } |
| 212 | BOOST_AUTO_TEST_SUITE_END() |
| 213 | |
| 214 | } // namespace tests |
| 215 | } // namespace gep |
| 216 | } // namespace ndn |