blob: b4a39915d513c6f9473ea21655c81fe564be028d [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 Pesaventoaee2ada2022-02-18 14:43:02 -05003 * Copyright (c) 2013-2022 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
28namespace ndn {
29namespace security {
30namespace tests {
31
32using namespace ndn::tests;
33
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050034class InterestSignerFixture : public ClockFixture, public KeyChainFixture
35{
36};
37
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080038BOOST_AUTO_TEST_SUITE(Security)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050039BOOST_FIXTURE_TEST_SUITE(TestInterestSigner, InterestSignerFixture)
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080040
Eric Newberry17d7c472020-06-18 21:29:22 -070041BOOST_AUTO_TEST_CASE(V02)
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080042{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050043 m_keyChain.createIdentity("/test");
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080044
Eric Newberry17d7c472020-06-18 21:29:22 -070045 InterestSigner signer(m_keyChain);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080046 Interest i1 = signer.makeCommandInterest("/hello/world");
Eric Newberry17d7c472020-06-18 21:29:22 -070047 BOOST_REQUIRE_EQUAL(i1.getName().size(), 6);
Eric Newberry1caa6342020-08-23 19:29:08 -070048 BOOST_TEST(i1.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type() == tlv::SignatureValue);
49 BOOST_TEST(i1.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type() == tlv::SignatureInfo);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080050
51 time::milliseconds timestamp = toUnixTimestamp(time::system_clock::now());
Eric Newberry1caa6342020-08-23 19:29:08 -070052 BOOST_TEST(i1.getName().at(command_interest::POS_TIMESTAMP).toNumber() == timestamp.count());
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080053
54 Interest i2 = signer.makeCommandInterest("/hello/world/!", signingByIdentity("/test"));
Eric Newberry17d7c472020-06-18 21:29:22 -070055 BOOST_REQUIRE_EQUAL(i2.getName().size(), 7);
Eric Newberry1caa6342020-08-23 19:29:08 -070056 BOOST_TEST(i2.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type() == tlv::SignatureValue);
57 BOOST_TEST(i2.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type() == tlv::SignatureInfo);
58 // These doesn't play well with BOOST_TEST for some reason
59 BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP),
60 i1.getName().at(command_interest::POS_TIMESTAMP));
Alexander Afanasyev70244f42017-01-04 12:47:12 -080061 BOOST_CHECK_NE(i2.getName().at(command_interest::POS_RANDOM_VAL),
62 i1.getName().at(command_interest::POS_RANDOM_VAL)); // this sometimes can fail
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080063
Davide Pesavento0f830802018-01-16 23:58:58 -050064 advanceClocks(100_s);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080065
66 i2 = signer.makeCommandInterest("/hello/world/!");
Eric Newberry1caa6342020-08-23 19:29:08 -070067 // This doesn't play well with BOOST_TEST for some reason
68 BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP),
69 i1.getName().at(command_interest::POS_TIMESTAMP));
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080070}
71
Eric Newberry17d7c472020-06-18 21:29:22 -070072BOOST_AUTO_TEST_CASE(V03)
73{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050074 m_keyChain.createIdentity("/test");
Eric Newberry17d7c472020-06-18 21:29:22 -070075
76 InterestSigner signer(m_keyChain);
77 Interest i1("/hello/world");
Eric Newberry1caa6342020-08-23 19:29:08 -070078 signer.makeSignedInterest(i1, SigningInfo(),
79 InterestSigner::SigningFlags::WantNonce |
Davide Pesaventoaee2ada2022-02-18 14:43:02 -050080 InterestSigner::SigningFlags::WantTime);
Eric Newberry1caa6342020-08-23 19:29:08 -070081 BOOST_TEST(i1.isSigned() == true);
82 BOOST_TEST_REQUIRE(i1.getName().size() == 3);
83 BOOST_TEST_REQUIRE(i1.getSignatureInfo().has_value());
Eric Newberry17d7c472020-06-18 21:29:22 -070084
Eric Newberry1caa6342020-08-23 19:29:08 -070085 BOOST_TEST(i1.getSignatureInfo()->getNonce().has_value() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -070086 BOOST_TEST(*i1.getSignatureInfo()->getTime() == time::system_clock::now());
Eric Newberry1caa6342020-08-23 19:29:08 -070087 BOOST_TEST(i1.getSignatureInfo()->getSeqNum().has_value() == false);
Eric Newberry17d7c472020-06-18 21:29:22 -070088
89 Interest i2("/hello/world/!");
Eric Newberry1caa6342020-08-23 19:29:08 -070090 signer.makeSignedInterest(i2, signingByIdentity("/test"),
91 InterestSigner::SigningFlags::WantNonce |
Davide Pesaventoaee2ada2022-02-18 14:43:02 -050092 InterestSigner::SigningFlags::WantTime |
93 InterestSigner::SigningFlags::WantSeqNum);
Eric Newberry1caa6342020-08-23 19:29:08 -070094 BOOST_TEST(i2.isSigned() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -070095 BOOST_REQUIRE_EQUAL(i2.getName().size(), 4);
96 BOOST_REQUIRE(i2.getSignatureInfo());
97
Eric Newberry17d7c472020-06-18 21:29:22 -070098 BOOST_TEST(*i2.getSignatureInfo()->getNonce() != *i1.getSignatureInfo()->getNonce());
Eric Newberry1caa6342020-08-23 19:29:08 -070099 BOOST_TEST(*i2.getSignatureInfo()->getTime() > *i1.getSignatureInfo()->getTime());
100 BOOST_TEST_REQUIRE(i2.getSignatureInfo()->getSeqNum().has_value() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -0700101
102 advanceClocks(100_s);
103
Eric Newberry1caa6342020-08-23 19:29:08 -0700104 Interest i3("/hello/world/2");
Eric Newberry1caa6342020-08-23 19:29:08 -0700105 signer.makeSignedInterest(i3, SigningInfo(), InterestSigner::SigningFlags::WantSeqNum);
106 BOOST_TEST(i3.isSigned() == true);
107 BOOST_REQUIRE_EQUAL(i3.getName().size(), 4);
108 BOOST_REQUIRE(i3.getSignatureInfo());
Eric Newberry17d7c472020-06-18 21:29:22 -0700109
Eric Newberry1caa6342020-08-23 19:29:08 -0700110 BOOST_TEST(i3.getSignatureInfo()->getNonce().has_value() == false);
111 BOOST_TEST(i3.getSignatureInfo()->getTime().has_value() == false);
112 BOOST_TEST_REQUIRE(i3.getSignatureInfo()->getSeqNum().has_value() == true);
113 BOOST_TEST(*i3.getSignatureInfo()->getSeqNum() > *i2.getSignatureInfo()->getSeqNum());
114
115 signer.makeSignedInterest(i3);
116 BOOST_TEST(i3.isSigned() == true);
117
118 BOOST_TEST(*i3.getSignatureInfo()->getTime() == time::system_clock::now());
119
120 BOOST_CHECK_THROW(signer.makeSignedInterest(i3, SigningInfo(), 0), std::invalid_argument);
Eric Newberry17d7c472020-06-18 21:29:22 -0700121}
122
123BOOST_AUTO_TEST_SUITE_END() // TestInterestSigner
Alexander Afanasyev1b58a032017-01-04 14:00:47 -0800124BOOST_AUTO_TEST_SUITE_END() // Security
125
126} // namespace tests
127} // namespace security
128} // namespace ndn