Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 2 | /* |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 Regents of the University of California. |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [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 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 22 | #include "ndn-cxx/security/interest-signer.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "ndn-cxx/security/signing-helpers.hpp" |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | #include "tests/unit/identity-management-time-fixture.hpp" |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
| 30 | namespace tests { |
| 31 | |
| 32 | using namespace ndn::tests; |
| 33 | |
| 34 | BOOST_AUTO_TEST_SUITE(Security) |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestInterestSigner, IdentityManagementTimeFixture) |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 36 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(V02) |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 38 | { |
| 39 | addIdentity("/test"); |
| 40 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 41 | InterestSigner signer(m_keyChain); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 42 | Interest i1 = signer.makeCommandInterest("/hello/world"); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 43 | BOOST_REQUIRE_EQUAL(i1.getName().size(), 6); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 44 | BOOST_CHECK_EQUAL(i1.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type(), tlv::SignatureValue); |
| 45 | BOOST_CHECK_EQUAL(i1.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type(), tlv::SignatureInfo); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 46 | |
| 47 | time::milliseconds timestamp = toUnixTimestamp(time::system_clock::now()); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(i1.getName().at(command_interest::POS_TIMESTAMP).toNumber(), timestamp.count()); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 49 | |
| 50 | Interest i2 = signer.makeCommandInterest("/hello/world/!", signingByIdentity("/test")); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 51 | BOOST_REQUIRE_EQUAL(i2.getName().size(), 7); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(i2.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type(), tlv::SignatureValue); |
| 53 | BOOST_CHECK_EQUAL(i2.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type(), tlv::SignatureInfo); |
| 54 | BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP), i1.getName().at(command_interest::POS_TIMESTAMP)); |
| 55 | BOOST_CHECK_NE(i2.getName().at(command_interest::POS_RANDOM_VAL), |
| 56 | i1.getName().at(command_interest::POS_RANDOM_VAL)); // this sometimes can fail |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 57 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 58 | advanceClocks(100_s); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 59 | |
| 60 | i2 = signer.makeCommandInterest("/hello/world/!"); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 61 | BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP), i1.getName().at(command_interest::POS_TIMESTAMP)); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 64 | BOOST_AUTO_TEST_CASE(V03) |
| 65 | { |
| 66 | addIdentity("/test"); |
| 67 | |
| 68 | InterestSigner signer(m_keyChain); |
| 69 | Interest i1("/hello/world"); |
| 70 | i1.setCanBePrefix(false); |
| 71 | signer.makeSignedInterest(i1); |
| 72 | BOOST_CHECK_EQUAL(i1.isSigned(), true); |
| 73 | BOOST_REQUIRE_EQUAL(i1.getName().size(), 3); |
| 74 | BOOST_REQUIRE(i1.getSignatureInfo()); |
| 75 | |
| 76 | BOOST_TEST(*i1.getSignatureInfo()->getTime() == time::system_clock::now()); |
| 77 | |
| 78 | Interest i2("/hello/world/!"); |
| 79 | i2.setCanBePrefix(false); |
| 80 | signer.makeSignedInterest(i2, signingByIdentity("/test")); |
| 81 | BOOST_CHECK_EQUAL(i2.isSigned(), true); |
| 82 | BOOST_REQUIRE_EQUAL(i2.getName().size(), 4); |
| 83 | BOOST_REQUIRE(i2.getSignatureInfo()); |
| 84 | |
| 85 | BOOST_TEST(*i2.getSignatureInfo()->getTime() > *i1.getSignatureInfo()->getTime()); |
| 86 | BOOST_TEST(*i2.getSignatureInfo()->getNonce() != *i1.getSignatureInfo()->getNonce()); |
| 87 | |
| 88 | advanceClocks(100_s); |
| 89 | |
| 90 | signer.makeSignedInterest(i2); |
| 91 | BOOST_CHECK_EQUAL(i2.isSigned(), true); |
| 92 | |
| 93 | BOOST_TEST(*i2.getSignatureInfo()->getTime() == time::system_clock::now()); |
| 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_SUITE_END() // TestInterestSigner |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 97 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 98 | |
| 99 | } // namespace tests |
| 100 | } // namespace security |
| 101 | } // namespace ndn |