Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "signature-info.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(TestSignatureInfo) |
| 29 | |
| 30 | const uint8_t sigInfoRsa[] = { |
| 31 | 0x16, 0x1b, // SignatureInfo |
| 32 | 0x1b, 0x01, // SignatureType |
| 33 | 0x01, // Sha256WithRsa |
| 34 | 0x1c, 0x16, // KeyLocator |
| 35 | 0x07, 0x14, // Name |
| 36 | 0x08, 0x04, |
| 37 | 0x74, 0x65, 0x73, 0x74, |
| 38 | 0x08, 0x03, |
| 39 | 0x6b, 0x65, 0x79, |
| 40 | 0x08, 0x07, |
| 41 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 42 | }; |
| 43 | |
| 44 | BOOST_AUTO_TEST_CASE(Constructor) |
| 45 | { |
| 46 | SignatureInfo info; |
| 47 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 48 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 49 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 50 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 51 | SignatureInfo sha256Info(tlv::DigestSha256); |
| 52 | BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false); |
| 54 | BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error); |
| 55 | |
| 56 | KeyLocator keyLocator("/test/key/locator"); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 57 | SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator); |
| 58 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
| 60 | BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator()); |
| 61 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
| 62 | |
| 63 | const Block& encoded = sha256RsaInfo.wireEncode(); |
| 64 | Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa)); |
| 65 | |
| 66 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), |
| 67 | sigInfoBlock.wire() + sigInfoBlock.size(), |
| 68 | encoded.wire(), |
| 69 | encoded.wire() + encoded.size()); |
| 70 | |
| 71 | sha256RsaInfo = SignatureInfo(sigInfoBlock); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
| 74 | BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator()); |
| 75 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
| 76 | } |
| 77 | |
| 78 | BOOST_AUTO_TEST_CASE(ConstructorError) |
| 79 | { |
| 80 | const uint8_t error1[] = { |
| 81 | 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b) |
| 82 | 0x1b, 0x01, // SignatureType |
| 83 | 0x01, // Sha256WithRsa |
| 84 | 0x1c, 0x16, // KeyLocator |
| 85 | 0x07, 0x14, // Name |
| 86 | 0x08, 0x04, |
| 87 | 0x74, 0x65, 0x73, 0x74, |
| 88 | 0x08, 0x03, |
| 89 | 0x6b, 0x65, 0x79, |
| 90 | 0x08, 0x07, |
| 91 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 92 | }; |
| 93 | Block errorBlock1(error1, sizeof(error1)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 94 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 95 | |
| 96 | const uint8_t error2[] = { |
| 97 | 0x16, 0x01, // SignatureInfo |
| 98 | 0x01 // Wrong SignatureInfo value |
| 99 | }; |
| 100 | Block errorBlock2(error2, sizeof(error2)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 101 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 102 | |
| 103 | const uint8_t error3[] = { |
| 104 | 0x16, 0x01, // SignatureInfo |
| 105 | 0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b) |
| 106 | 0x01, // Sha256WithRsa |
| 107 | 0x1c, 0x16, // KeyLocator |
| 108 | 0x07, 0x14, // Name |
| 109 | 0x08, 0x04, |
| 110 | 0x74, 0x65, 0x73, 0x74, |
| 111 | 0x08, 0x03, |
| 112 | 0x6b, 0x65, 0x79, |
| 113 | 0x08, 0x07, |
| 114 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 115 | }; |
| 116 | Block errorBlock3(error3, sizeof(error3)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 117 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 118 | |
| 119 | const uint8_t error4[] = { |
| 120 | 0x16, 0x00 // Empty SignatureInfo |
| 121 | }; |
| 122 | Block errorBlock4(error4, sizeof(error4)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 123 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 124 | |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(SetterGetter) |
| 128 | { |
| 129 | SignatureInfo info; |
| 130 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 131 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 132 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 133 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 134 | info.setSignatureType(tlv::SignatureSha256WithRsa); |
| 135 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 137 | |
| 138 | KeyLocator keyLocator("/test/key/locator"); |
| 139 | info.setKeyLocator(keyLocator); |
| 140 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 141 | BOOST_CHECK_NO_THROW(info.getKeyLocator()); |
| 142 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator")); |
| 143 | |
| 144 | const Block& encoded = info.wireEncode(); |
| 145 | Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa)); |
| 146 | |
| 147 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), |
| 148 | sigInfoBlock.wire() + sigInfoBlock.size(), |
| 149 | encoded.wire(), |
| 150 | encoded.wire() + encoded.size()); |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 151 | |
| 152 | info.unsetKeyLocator(); |
| 153 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 154 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(OtherTlvs) |
| 158 | { |
| 159 | SignatureInfo info; |
| 160 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 161 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 162 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 163 | |
| 164 | const uint8_t tlv1[] = { |
| 165 | 0x81, // T |
| 166 | 0x01, // L |
| 167 | 0x01, // V |
| 168 | }; |
| 169 | Block block1(tlv1, sizeof(tlv1)); |
| 170 | |
| 171 | info.appendTypeSpecificTlv(block1); |
| 172 | BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error); |
| 173 | BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81)); |
| 174 | } |
| 175 | |
| 176 | BOOST_AUTO_TEST_SUITE_END() |
| 177 | |
| 178 | } // namespace ndn |