blob: 83d2ca7a7a1c24f63d561b0ce49130a257dde181 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento10b24be2017-07-12 23:23:46 -04002/*
Eric Newberrya3c8bd12020-05-15 17:27:07 -07003 * Copyright (c) 2013-2020 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/digest-sha256.hpp"
23#include "ndn-cxx/security/verification-helpers.hpp"
24#include "ndn-cxx/util/sha256.hpp"
25#include "ndn-cxx/util/string-helper.hpp"
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050028#include "tests/key-chain-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070029
Yingdi Yu21157162014-02-28 13:02:34 -080030namespace ndn {
Alexander Afanasyevadc71842017-01-26 22:17:58 -050031namespace security {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032namespace tests {
Yingdi Yu21157162014-02-28 13:02:34 -080033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_SUITE(Security)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050035BOOST_FIXTURE_TEST_SUITE(TestDigestSha256, ndn::tests::KeyChainFixture)
Yingdi Yu21157162014-02-28 13:02:34 -080036
Yingdi Yu4a557052014-07-09 16:40:37 -070037BOOST_AUTO_TEST_CASE(Sha256)
Yingdi Yu21157162014-02-28 13:02:34 -080038{
Yingdi Yu21157162014-02-28 13:02:34 -080039 char content[6] = "1234\n";
Davide Pesavento10b24be2017-07-12 23:23:46 -040040 ConstBufferPtr buf = util::Sha256::computeDigest(reinterpret_cast<uint8_t*>(content), 5);
Yingdi Yu21157162014-02-28 13:02:34 -080041
Davide Pesavento5d0b0102017-10-07 13:43:16 -040042 BOOST_CHECK_EQUAL(toHex(buf->data(), buf->size(), false),
Davide Pesaventoeee3e822016-11-26 19:19:34 +010043 "a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4");
Yingdi Yu21157162014-02-28 13:02:34 -080044}
45
Yingdi Yu6ab67812014-11-27 15:00:34 -080046BOOST_AUTO_TEST_CASE(DataSignature)
Yingdi Yu21157162014-02-28 13:02:34 -080047{
Yingdi Yu21157162014-02-28 13:02:34 -080048 Name name("/TestSignatureSha/Basic");
49 Data testData(name);
50 char content[5] = "1234";
51 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
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
Eric Newberrya3c8bd12020-05-15 17:27:07 -070054 BOOST_CHECK_THROW(testData.getSignatureInfo().getKeyLocator(), ndn::SignatureInfo::Error);
Alexander Afanasyevadc71842017-01-26 22:17:58 -050055 verifyDigest(testData, DigestAlgorithm::SHA256);
Yingdi Yu21157162014-02-28 13:02:34 -080056}
57
Yingdi Yu6ab67812014-11-27 15:00:34 -080058BOOST_AUTO_TEST_CASE(InterestSignature)
59{
60 Name name("/SecurityTestDigestSha256/InterestSignature/Interest1");
61 Interest testInterest(name);
Eric Newberryb74bbda2020-06-18 19:33:58 -070062 testInterest.setCanBePrefix(false);
Yingdi Yu6ab67812014-11-27 15:00:34 -080063
Yingdi Yu1b0311c2015-06-10 14:58:47 -070064 m_keyChain.sign(testInterest, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
Alexander Afanasyevadc71842017-01-26 22:17:58 -050065 verifyDigest(testInterest, DigestAlgorithm::SHA256);
Yingdi Yu6ab67812014-11-27 15:00:34 -080066}
67
Davide Pesaventoeee3e822016-11-26 19:19:34 +010068BOOST_AUTO_TEST_SUITE_END() // TestDigestSha256
69BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu21157162014-02-28 13:02:34 -080070
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080071} // namespace tests
Alexander Afanasyevadc71842017-01-26 22:17:58 -050072} // namespace security
Yingdi Yu21157162014-02-28 13:02:34 -080073} // namespace ndn