blob: cbac8e2df4217451786d0750ba7566ff489d00dc [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu21157162014-02-28 13:02:34 -08002/**
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu21157162014-02-28 13:02:34 -080020 */
21
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080022#include "security/digest-sha256.hpp"
Yingdi Yu21157162014-02-28 13:02:34 -080023#include "security/key-chain.hpp"
24#include "security/validator.hpp"
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070025#include "util/string-helper.hpp"
26
Yingdi Yu6ab67812014-11-27 15:00:34 -080027#include "identity-management-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028#include "boost-test.hpp"
29
Yingdi Yu21157162014-02-28 13:02:34 -080030namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031namespace tests {
Yingdi Yu21157162014-02-28 13:02:34 -080032
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Security)
34BOOST_FIXTURE_TEST_SUITE(TestDigestSha256, IdentityManagementFixture)
Yingdi Yu21157162014-02-28 13:02:34 -080035
Yingdi Yu4a557052014-07-09 16:40:37 -070036BOOST_AUTO_TEST_CASE(Sha256)
Yingdi Yu21157162014-02-28 13:02:34 -080037{
Yingdi Yu21157162014-02-28 13:02:34 -080038 char content[6] = "1234\n";
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070039 ConstBufferPtr buf = crypto::computeSha256Digest(reinterpret_cast<uint8_t*>(content), 5);
Yingdi Yu21157162014-02-28 13:02:34 -080040
Davide Pesaventoeee3e822016-11-26 19:19:34 +010041 BOOST_CHECK_EQUAL(toHex(buf->buf(), buf->size(), false),
42 "a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4");
Yingdi Yu21157162014-02-28 13:02:34 -080043}
44
Yingdi Yu6ab67812014-11-27 15:00:34 -080045BOOST_AUTO_TEST_CASE(DataSignature)
Yingdi Yu21157162014-02-28 13:02:34 -080046{
Yingdi Yu21157162014-02-28 13:02:34 -080047 Name name("/TestSignatureSha/Basic");
48 Data testData(name);
49 char content[5] = "1234";
50 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070051
Yingdi Yu1b0311c2015-06-10 14:58:47 -070052 m_keyChain.sign(testData, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
Yingdi Yu21157162014-02-28 13:02:34 -080053
54 testData.wireEncode();
55
Yingdi Yubf6a2812014-06-17 15:32:11 -070056 DigestSha256 sig(testData.getSignature());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070057
Yingdi Yu4a557052014-07-09 16:40:37 -070058 BOOST_CHECK(Validator::verifySignature(testData, sig));
59
60 BOOST_CHECK_THROW(sig.getKeyLocator(), ndn::SignatureInfo::Error);
Yingdi Yu21157162014-02-28 13:02:34 -080061}
62
Yingdi Yu6ab67812014-11-27 15:00:34 -080063BOOST_AUTO_TEST_CASE(InterestSignature)
64{
65 Name name("/SecurityTestDigestSha256/InterestSignature/Interest1");
66 Interest testInterest(name);
67
Yingdi Yu1b0311c2015-06-10 14:58:47 -070068 m_keyChain.sign(testInterest, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
Yingdi Yu6ab67812014-11-27 15:00:34 -080069 testInterest.wireEncode();
70 const Name& signedName = testInterest.getName();
71
72 Signature signature(signedName[signed_interest::POS_SIG_INFO].blockFromValue(),
73 signedName[signed_interest::POS_SIG_VALUE].blockFromValue());
74 DigestSha256 sig(signature);
75 BOOST_CHECK(Validator::verifySignature(testInterest, sig));
76}
77
Davide Pesaventoeee3e822016-11-26 19:19:34 +010078BOOST_AUTO_TEST_SUITE_END() // TestDigestSha256
79BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu21157162014-02-28 13:02:34 -080080
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080081} // namespace tests
Yingdi Yu21157162014-02-28 13:02:34 -080082} // namespace ndn