blob: 5dc8d9d8dc87ce209bf5ebc4b4066f4915681043 [file] [log] [blame]
Yingdi Yu7a813892015-06-09 14:19:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
Davide Pesaventofbea4fc2022-02-08 07:26:04 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Yingdi Yu7a813892015-06-09 14:19:54 -07004 *
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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/validity-period.hpp"
Yingdi Yu7a813892015-06-09 14:19:54 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/unit/clock-fixture.hpp"
Davide Pesavento74daf742018-11-23 18:14:13 -050026
Yingdi Yu7a813892015-06-09 14:19:54 -070027#include <boost/lexical_cast.hpp>
28
29namespace ndn {
30namespace security {
Yingdi Yu10bf63a2015-11-04 14:14:37 -080031namespace tests {
32
33using namespace ndn::tests;
Yingdi Yu7a813892015-06-09 14:19:54 -070034
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(Security)
36BOOST_AUTO_TEST_SUITE(TestValidityPeriod)
Yingdi Yu7a813892015-06-09 14:19:54 -070037
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050038BOOST_FIXTURE_TEST_CASE(ConstructorSetter, ClockFixture)
Yingdi Yu7a813892015-06-09 14:19:54 -070039{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050040 auto now = m_systemClock->getNow();
41 auto notBefore = now - 1_day;
42 auto notAfter = notBefore + 2_days;
43 ValidityPeriod validity1(notBefore, notAfter);
Yingdi Yu7a813892015-06-09 14:19:54 -070044
45 auto period = validity1.getPeriod();
46 BOOST_CHECK_GE(period.first, notBefore); // fractional seconds will be removed
Davide Pesavento0f830802018-01-16 23:58:58 -050047 BOOST_CHECK_LT(period.first, notBefore + 1_s);
Yingdi Yu7a813892015-06-09 14:19:54 -070048
49 BOOST_CHECK_LE(period.second, notAfter); // fractional seconds will be removed
Davide Pesavento0f830802018-01-16 23:58:58 -050050 BOOST_CHECK_GT(period.second, notAfter - 1_s);
Yingdi Yu7a813892015-06-09 14:19:54 -070051 BOOST_CHECK_EQUAL(validity1.isValid(), true);
52
Davide Pesavento0f830802018-01-16 23:58:58 -050053 BOOST_CHECK_EQUAL(ValidityPeriod(now - 2_days, now - 1_day).isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070054
55 BOOST_CHECK_NO_THROW((ValidityPeriod()));
56 ValidityPeriod validity2;
Yingdi Yu10bf63a2015-11-04 14:14:37 -080057 BOOST_CHECK_EQUAL(validity2.isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070058
59 validity2.setPeriod(notBefore, notAfter);
60 BOOST_CHECK(validity2.getPeriod() != std::make_pair(time::getUnixEpoch(), time::getUnixEpoch()));
61 BOOST_CHECK_EQUAL(validity2, validity1);
62
Davide Pesavento0f830802018-01-16 23:58:58 -050063 validity1.setPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 10 * 365_days);
Yingdi Yu7a813892015-06-09 14:19:54 -070064 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
65 "(19700101T000000, 19791230T000000)");
66
Davide Pesavento0f830802018-01-16 23:58:58 -050067 validity1.setPeriod(time::getUnixEpoch() + 1_ns,
68 time::getUnixEpoch() + (10 * 365_days) + 1_ns);
Yingdi Yu7a813892015-06-09 14:19:54 -070069 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
70 "(19700101T000001, 19791230T000000)");
Yingdi Yu10bf63a2015-11-04 14:14:37 -080071
72 BOOST_CHECK_EQUAL(ValidityPeriod(now, now).isValid(), true);
Davide Pesavento0f830802018-01-16 23:58:58 -050073 BOOST_CHECK_EQUAL(ValidityPeriod(now + 1_s, now).isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070074}
75
76const uint8_t VP1[] = {
77 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
78 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
79 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
80 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
81 0xfd, 0x00, 0xff, 0x0f, // NotAfter
82 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
83 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
84};
85
86BOOST_AUTO_TEST_CASE(EncodingDecoding)
87{
88 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -050089 time::system_clock::TimePoint notAfter = notBefore + 1_day;
Yingdi Yu7a813892015-06-09 14:19:54 -070090 ValidityPeriod v1(notBefore, notAfter);
Yingdi Yu7a813892015-06-09 14:19:54 -070091 BOOST_CHECK_EQUAL_COLLECTIONS(v1.wireEncode().begin(), v1.wireEncode().end(),
92 VP1, VP1 + sizeof(VP1));
93
Davide Pesaventofbea4fc2022-02-08 07:26:04 -050094 ValidityPeriod v2(Block{VP1});
Yingdi Yu7a813892015-06-09 14:19:54 -070095 BOOST_CHECK(v1.getPeriod() == v2.getPeriod());
96}
97
98const uint8_t VP_E1[] = {
99 0xfd, 0x00, 0xff, 0x26, // ValidityPeriod (error)
100 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
101 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
102 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
103 0xfd, 0x00, 0xff, 0x0f, // NotAfter
104 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
105 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
106};
107
108const uint8_t VP_E2[] = {
109 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
110 0xfd, 0x00, 0xff, 0x0f, // NotBefore (error)
111 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
112 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
113 0xfd, 0x00, 0xff, 0x0f, // NotAfter
114 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
115 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
116};
117
118const uint8_t VP_E3[] = {
119 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
120 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
121 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
122 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
123 0xfd, 0x00, 0xfe, 0x0f, // NotAfter (error)
124 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
125 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
126};
127
128const uint8_t VP_E4[] = {
129 0xfd, 0x00, 0xfd, 0x39, // ValidityPeriod
130 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
131 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
132 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
133 0xfd, 0x00, 0xff, 0x0f, // NotAfter
134 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
135 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
136 0xfd, 0x00, 0xff, 0x0f, // NotAfter (error)
137 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
138 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
139};
140
141const uint8_t VP_E5[] = {
142 0xfd, 0x00, 0xfd, 0x13, // ValidityPeriod
143 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
144 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
145 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
146};
147
148const uint8_t VP_E6[] = {
149 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
150 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
151 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T00000\xFF
152 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFF,
153 0xfd, 0x00, 0xff, 0x0f, // NotAfter
154 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
155 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
156};
157
Yingdi Yu7a813892015-06-09 14:19:54 -0700158BOOST_AUTO_TEST_CASE(DecodingError)
159{
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500160 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E1}), ValidityPeriod::Error);
161 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E2}), ValidityPeriod::Error);
162 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E3}), ValidityPeriod::Error);
163 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E4}), ValidityPeriod::Error);
164 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E5}), ValidityPeriod::Error);
165 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E6}), ValidityPeriod::Error);
Yingdi Yu7a813892015-06-09 14:19:54 -0700166
167 Block emptyBlock;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500168 BOOST_CHECK_THROW(ValidityPeriod{emptyBlock}, ValidityPeriod::Error);
Yingdi Yu7a813892015-06-09 14:19:54 -0700169}
170
171BOOST_AUTO_TEST_CASE(Comparison)
172{
173 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -0500174 time::system_clock::TimePoint notAfter = notBefore + 1_day;
175 time::system_clock::TimePoint notAfter2 = notBefore + 2_days;
Yingdi Yu7a813892015-06-09 14:19:54 -0700176
177 ValidityPeriod validity1(notBefore, notAfter);
178 ValidityPeriod validity2(notBefore, notAfter);
179 BOOST_CHECK(validity1 == validity2);
180
181 ValidityPeriod validity3(notBefore, notAfter2);
182 BOOST_CHECK(validity1 != validity3);
183}
184
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100185BOOST_AUTO_TEST_SUITE_END() // TestValidityPeriod
186BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu7a813892015-06-09 14:19:54 -0700187
Yingdi Yu10bf63a2015-11-04 14:14:37 -0800188} // namespace tests
Yingdi Yu7a813892015-06-09 14:19:54 -0700189} // namespace security
190} // namespace ndn