blob: 6d054fff8d0b9fcbb323e6d9152a67100e16860c [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/*
Eric Newberry17d7c472020-06-18 21:29:22 -07003 * Copyright (c) 2013-2020 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"
Davide Pesavento7e780642018-11-24 15:51:34 -050023#include "ndn-cxx/security/signing-helpers.hpp"
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26#include "tests/unit/identity-management-time-fixture.hpp"
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080027
28namespace ndn {
29namespace security {
30namespace tests {
31
32using namespace ndn::tests;
33
34BOOST_AUTO_TEST_SUITE(Security)
Eric Newberry17d7c472020-06-18 21:29:22 -070035BOOST_FIXTURE_TEST_SUITE(TestInterestSigner, IdentityManagementTimeFixture)
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080036
Eric Newberry17d7c472020-06-18 21:29:22 -070037BOOST_AUTO_TEST_CASE(V02)
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080038{
39 addIdentity("/test");
40
Eric Newberry17d7c472020-06-18 21:29:22 -070041 InterestSigner signer(m_keyChain);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080042 Interest i1 = signer.makeCommandInterest("/hello/world");
Eric Newberry17d7c472020-06-18 21:29:22 -070043 BOOST_REQUIRE_EQUAL(i1.getName().size(), 6);
Eric Newberry1caa6342020-08-23 19:29:08 -070044 BOOST_TEST(i1.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type() == tlv::SignatureValue);
45 BOOST_TEST(i1.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type() == tlv::SignatureInfo);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080046
47 time::milliseconds timestamp = toUnixTimestamp(time::system_clock::now());
Eric Newberry1caa6342020-08-23 19:29:08 -070048 BOOST_TEST(i1.getName().at(command_interest::POS_TIMESTAMP).toNumber() == timestamp.count());
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080049
50 Interest i2 = signer.makeCommandInterest("/hello/world/!", signingByIdentity("/test"));
Eric Newberry17d7c472020-06-18 21:29:22 -070051 BOOST_REQUIRE_EQUAL(i2.getName().size(), 7);
Eric Newberry1caa6342020-08-23 19:29:08 -070052 BOOST_TEST(i2.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type() == tlv::SignatureValue);
53 BOOST_TEST(i2.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type() == tlv::SignatureInfo);
54 // These doesn't play well with BOOST_TEST for some reason
55 BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP),
56 i1.getName().at(command_interest::POS_TIMESTAMP));
Alexander Afanasyev70244f42017-01-04 12:47:12 -080057 BOOST_CHECK_NE(i2.getName().at(command_interest::POS_RANDOM_VAL),
58 i1.getName().at(command_interest::POS_RANDOM_VAL)); // this sometimes can fail
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080059
Davide Pesavento0f830802018-01-16 23:58:58 -050060 advanceClocks(100_s);
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080061
62 i2 = signer.makeCommandInterest("/hello/world/!");
Eric Newberry1caa6342020-08-23 19:29:08 -070063 // This doesn't play well with BOOST_TEST for some reason
64 BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP),
65 i1.getName().at(command_interest::POS_TIMESTAMP));
Alexander Afanasyev1b58a032017-01-04 14:00:47 -080066}
67
Eric Newberry17d7c472020-06-18 21:29:22 -070068BOOST_AUTO_TEST_CASE(V03)
69{
70 addIdentity("/test");
71
72 InterestSigner signer(m_keyChain);
73 Interest i1("/hello/world");
74 i1.setCanBePrefix(false);
Eric Newberry1caa6342020-08-23 19:29:08 -070075 signer.makeSignedInterest(i1, SigningInfo(),
76 InterestSigner::SigningFlags::WantNonce |
77 InterestSigner::SigningFlags::WantTime);
78 BOOST_TEST(i1.isSigned() == true);
79 BOOST_TEST_REQUIRE(i1.getName().size() == 3);
80 BOOST_TEST_REQUIRE(i1.getSignatureInfo().has_value());
Eric Newberry17d7c472020-06-18 21:29:22 -070081
Eric Newberry1caa6342020-08-23 19:29:08 -070082 BOOST_TEST(i1.getSignatureInfo()->getNonce().has_value() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -070083 BOOST_TEST(*i1.getSignatureInfo()->getTime() == time::system_clock::now());
Eric Newberry1caa6342020-08-23 19:29:08 -070084 BOOST_TEST(i1.getSignatureInfo()->getSeqNum().has_value() == false);
Eric Newberry17d7c472020-06-18 21:29:22 -070085
86 Interest i2("/hello/world/!");
87 i2.setCanBePrefix(false);
Eric Newberry1caa6342020-08-23 19:29:08 -070088 signer.makeSignedInterest(i2, signingByIdentity("/test"),
89 InterestSigner::SigningFlags::WantNonce |
90 InterestSigner::SigningFlags::WantTime |
91 InterestSigner::SigningFlags::WantSeqNum);
92 BOOST_TEST(i2.isSigned() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -070093 BOOST_REQUIRE_EQUAL(i2.getName().size(), 4);
94 BOOST_REQUIRE(i2.getSignatureInfo());
95
Eric Newberry17d7c472020-06-18 21:29:22 -070096 BOOST_TEST(*i2.getSignatureInfo()->getNonce() != *i1.getSignatureInfo()->getNonce());
Eric Newberry1caa6342020-08-23 19:29:08 -070097 BOOST_TEST(*i2.getSignatureInfo()->getTime() > *i1.getSignatureInfo()->getTime());
98 BOOST_TEST_REQUIRE(i2.getSignatureInfo()->getSeqNum().has_value() == true);
Eric Newberry17d7c472020-06-18 21:29:22 -070099
100 advanceClocks(100_s);
101
Eric Newberry1caa6342020-08-23 19:29:08 -0700102 Interest i3("/hello/world/2");
103 i3.setCanBePrefix(false);
104 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
125} // namespace tests
126} // namespace security
127} // namespace ndn