blob: b44546ba00315841472e4867fb67cb8a9ce69a2c [file] [log] [blame]
Alexander Afanasyev1b58a032017-01-04 14:00:47 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevadc71842017-01-26 22:17:58 -05002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyev1b58a032017-01-04 14:00:47 -08004 *
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 Newberry17d7c472020-06-18 21:29:22 -070022#include "ndn-cxx/security/interest-signer.hpp"
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/key-chain-fixture.hpp"
26#include "tests/unit/clock-fixture.hpp"
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030using ndn::security::InterestSigner;
31using ndn::security::SigningInfo;
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080032
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050033class InterestSignerFixture : public ClockFixture, public KeyChainFixture
34{
35};
36
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080037BOOST_AUTO_TEST_SUITE(Security)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050038BOOST_FIXTURE_TEST_SUITE(TestInterestSigner, InterestSignerFixture)
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080039
Eric Newberry17d7c472020-06-18 21:29:22 -070040BOOST_AUTO_TEST_CASE(V02)
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080041{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050042 m_keyChain.createIdentity("/test");
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080043
Eric Newberry17d7c472020-06-18 21:29:22 -070044 InterestSigner signer(m_keyChain);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080045 Interest i1 = signer.makeCommandInterest("/hello/world");
Eric Newberry17d7c472020-06-18 21:29:22 -070046 BOOST_REQUIRE_EQUAL(i1.getName().size(), 6);
Eric Newberry1caa6342020-08-23 19:29:08 -070047 BOOST_TEST(i1.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type() == tlv::SignatureValue);
48 BOOST_TEST(i1.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type() == tlv::SignatureInfo);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080049
50 time::milliseconds timestamp = toUnixTimestamp(time::system_clock::now());
Eric Newberry1caa6342020-08-23 19:29:08 -070051 BOOST_TEST(i1.getName().at(command_interest::POS_TIMESTAMP).toNumber() == timestamp.count());
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080052
53 Interest i2 = signer.makeCommandInterest("/hello/world/!", signingByIdentity("/test"));
Eric Newberry17d7c472020-06-18 21:29:22 -070054 BOOST_REQUIRE_EQUAL(i2.getName().size(), 7);
Eric Newberry1caa6342020-08-23 19:29:08 -070055 BOOST_TEST(i2.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type() == tlv::SignatureValue);
56 BOOST_TEST(i2.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type() == tlv::SignatureInfo);
57 // These doesn't play well with BOOST_TEST for some reason
58 BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP),
59 i1.getName().at(command_interest::POS_TIMESTAMP));
Alexander Afanasyev70244f42017-01-04 12:47:12 -080060 BOOST_CHECK_NE(i2.getName().at(command_interest::POS_RANDOM_VAL),
61 i1.getName().at(command_interest::POS_RANDOM_VAL)); // this sometimes can fail
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080062
Davide Pesavento0f830802018-01-16 23:58:58 -050063 advanceClocks(100_s);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080064
65 i2 = signer.makeCommandInterest("/hello/world/!");
Eric Newberry1caa6342020-08-23 19:29:08 -070066 // This doesn't play well with BOOST_TEST for some reason
67 BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP),
68 i1.getName().at(command_interest::POS_TIMESTAMP));
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080069}
70
Eric Newberry17d7c472020-06-18 21:29:22 -070071BOOST_AUTO_TEST_CASE(V03)
72{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050073 m_keyChain.createIdentity("/test");
Eric Newberry17d7c472020-06-18 21:29:22 -070074
75 InterestSigner signer(m_keyChain);
76 Interest i1("/hello/world");
Eric Newberry1caa6342020-08-23 19:29:08 -070077 signer.makeSignedInterest(i1, SigningInfo(),
78 InterestSigner::SigningFlags::WantNonce |
Davide Pesaventoaee2ada2022-02-18 14:43:02 -050079 InterestSigner::SigningFlags::WantTime);
Eric Newberry1caa6342020-08-23 19:29:08 -070080 BOOST_TEST(i1.isSigned() == true);
81 BOOST_TEST_REQUIRE(i1.getName().size() == 3);
82 BOOST_TEST_REQUIRE(i1.getSignatureInfo().has_value());
Eric Newberry17d7c472020-06-18 21:29:22 -070083
Eric Newberry1caa6342020-08-23 19:29:08 -070084 BOOST_TEST(i1.getSignatureInfo()->getNonce().has_value() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -070085 BOOST_TEST(*i1.getSignatureInfo()->getTime() == time::system_clock::now());
Eric Newberry1caa6342020-08-23 19:29:08 -070086 BOOST_TEST(i1.getSignatureInfo()->getSeqNum().has_value() == false);
Eric Newberry17d7c472020-06-18 21:29:22 -070087
88 Interest i2("/hello/world/!");
Eric Newberry1caa6342020-08-23 19:29:08 -070089 signer.makeSignedInterest(i2, signingByIdentity("/test"),
90 InterestSigner::SigningFlags::WantNonce |
Davide Pesaventoaee2ada2022-02-18 14:43:02 -050091 InterestSigner::SigningFlags::WantTime |
92 InterestSigner::SigningFlags::WantSeqNum);
Eric Newberry1caa6342020-08-23 19:29:08 -070093 BOOST_TEST(i2.isSigned() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -070094 BOOST_REQUIRE_EQUAL(i2.getName().size(), 4);
95 BOOST_REQUIRE(i2.getSignatureInfo());
96
Eric Newberry17d7c472020-06-18 21:29:22 -070097 BOOST_TEST(*i2.getSignatureInfo()->getNonce() != *i1.getSignatureInfo()->getNonce());
Eric Newberry1caa6342020-08-23 19:29:08 -070098 BOOST_TEST(*i2.getSignatureInfo()->getTime() > *i1.getSignatureInfo()->getTime());
99 BOOST_TEST_REQUIRE(i2.getSignatureInfo()->getSeqNum().has_value() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -0700100
101 advanceClocks(100_s);
102
Eric Newberry1caa6342020-08-23 19:29:08 -0700103 Interest i3("/hello/world/2");
Eric Newberry1caa6342020-08-23 19:29:08 -0700104 signer.makeSignedInterest(i3, SigningInfo(), InterestSigner::SigningFlags::WantSeqNum);
105 BOOST_TEST(i3.isSigned() == true);
106 BOOST_REQUIRE_EQUAL(i3.getName().size(), 4);
107 BOOST_REQUIRE(i3.getSignatureInfo());
Eric Newberry17d7c472020-06-18 21:29:22 -0700108
Eric Newberry1caa6342020-08-23 19:29:08 -0700109 BOOST_TEST(i3.getSignatureInfo()->getNonce().has_value() == false);
110 BOOST_TEST(i3.getSignatureInfo()->getTime().has_value() == false);
111 BOOST_TEST_REQUIRE(i3.getSignatureInfo()->getSeqNum().has_value() == true);
112 BOOST_TEST(*i3.getSignatureInfo()->getSeqNum() > *i2.getSignatureInfo()->getSeqNum());
113
114 signer.makeSignedInterest(i3);
115 BOOST_TEST(i3.isSigned() == true);
116
117 BOOST_TEST(*i3.getSignatureInfo()->getTime() == time::system_clock::now());
118
119 BOOST_CHECK_THROW(signer.makeSignedInterest(i3, SigningInfo(), 0), std::invalid_argument);
Eric Newberry17d7c472020-06-18 21:29:22 -0700120}
121
122BOOST_AUTO_TEST_SUITE_END() // TestInterestSigner
Alexander Afanasyev1b58a032017-01-04 14:00:47 -0800123BOOST_AUTO_TEST_SUITE_END() // Security
124
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400125} // namespace ndn::tests