Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 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 { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 27 | namespace tests { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(TestSignatureInfo) |
| 30 | |
| 31 | const uint8_t sigInfoRsa[] = { |
| 32 | 0x16, 0x1b, // SignatureInfo |
| 33 | 0x1b, 0x01, // SignatureType |
| 34 | 0x01, // Sha256WithRsa |
| 35 | 0x1c, 0x16, // KeyLocator |
| 36 | 0x07, 0x14, // Name |
| 37 | 0x08, 0x04, |
| 38 | 0x74, 0x65, 0x73, 0x74, |
| 39 | 0x08, 0x03, |
| 40 | 0x6b, 0x65, 0x79, |
| 41 | 0x08, 0x07, |
| 42 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 43 | }; |
| 44 | |
| 45 | BOOST_AUTO_TEST_CASE(Constructor) |
| 46 | { |
| 47 | SignatureInfo info; |
| 48 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 49 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 50 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 51 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 52 | SignatureInfo sha256Info(tlv::DigestSha256); |
| 53 | BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false); |
| 55 | BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error); |
| 56 | |
| 57 | KeyLocator keyLocator("/test/key/locator"); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 58 | SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator); |
| 59 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
| 61 | BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator()); |
| 62 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
| 63 | |
| 64 | const Block& encoded = sha256RsaInfo.wireEncode(); |
| 65 | Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa)); |
| 66 | |
| 67 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), |
| 68 | sigInfoBlock.wire() + sigInfoBlock.size(), |
| 69 | encoded.wire(), |
| 70 | encoded.wire() + encoded.size()); |
| 71 | |
| 72 | sha256RsaInfo = SignatureInfo(sigInfoBlock); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
| 75 | BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator()); |
| 76 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
| 77 | } |
| 78 | |
| 79 | BOOST_AUTO_TEST_CASE(ConstructorError) |
| 80 | { |
| 81 | const uint8_t error1[] = { |
| 82 | 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b) |
| 83 | 0x1b, 0x01, // SignatureType |
| 84 | 0x01, // Sha256WithRsa |
| 85 | 0x1c, 0x16, // KeyLocator |
| 86 | 0x07, 0x14, // Name |
| 87 | 0x08, 0x04, |
| 88 | 0x74, 0x65, 0x73, 0x74, |
| 89 | 0x08, 0x03, |
| 90 | 0x6b, 0x65, 0x79, |
| 91 | 0x08, 0x07, |
| 92 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 93 | }; |
| 94 | Block errorBlock1(error1, sizeof(error1)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 95 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 96 | |
| 97 | const uint8_t error2[] = { |
| 98 | 0x16, 0x01, // SignatureInfo |
| 99 | 0x01 // Wrong SignatureInfo value |
| 100 | }; |
| 101 | Block errorBlock2(error2, sizeof(error2)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 102 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 103 | |
| 104 | const uint8_t error3[] = { |
| 105 | 0x16, 0x01, // SignatureInfo |
| 106 | 0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b) |
| 107 | 0x01, // Sha256WithRsa |
| 108 | 0x1c, 0x16, // KeyLocator |
| 109 | 0x07, 0x14, // Name |
| 110 | 0x08, 0x04, |
| 111 | 0x74, 0x65, 0x73, 0x74, |
| 112 | 0x08, 0x03, |
| 113 | 0x6b, 0x65, 0x79, |
| 114 | 0x08, 0x07, |
| 115 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 116 | }; |
| 117 | Block errorBlock3(error3, sizeof(error3)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 118 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 119 | |
| 120 | const uint8_t error4[] = { |
| 121 | 0x16, 0x00 // Empty SignatureInfo |
| 122 | }; |
| 123 | Block errorBlock4(error4, sizeof(error4)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 124 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 125 | |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(SetterGetter) |
| 129 | { |
| 130 | SignatureInfo info; |
| 131 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 132 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 133 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 134 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 135 | info.setSignatureType(tlv::SignatureSha256WithRsa); |
| 136 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 138 | |
| 139 | KeyLocator keyLocator("/test/key/locator"); |
| 140 | info.setKeyLocator(keyLocator); |
| 141 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 142 | BOOST_CHECK_NO_THROW(info.getKeyLocator()); |
| 143 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator")); |
| 144 | |
| 145 | const Block& encoded = info.wireEncode(); |
| 146 | Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa)); |
| 147 | |
| 148 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), |
| 149 | sigInfoBlock.wire() + sigInfoBlock.size(), |
| 150 | encoded.wire(), |
| 151 | encoded.wire() + encoded.size()); |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 152 | |
| 153 | info.unsetKeyLocator(); |
| 154 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 155 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | BOOST_AUTO_TEST_CASE(OtherTlvs) |
| 159 | { |
| 160 | SignatureInfo info; |
| 161 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 162 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 163 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 164 | |
| 165 | const uint8_t tlv1[] = { |
| 166 | 0x81, // T |
| 167 | 0x01, // L |
| 168 | 0x01, // V |
| 169 | }; |
| 170 | Block block1(tlv1, sizeof(tlv1)); |
| 171 | |
| 172 | info.appendTypeSpecificTlv(block1); |
| 173 | BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error); |
| 174 | BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81)); |
| 175 | } |
| 176 | |
| 177 | BOOST_AUTO_TEST_SUITE_END() |
| 178 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 179 | } // namespace tests |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 180 | } // namespace ndn |