blob: dbcd47f8c6a2c28b4f9ca45fbae647b3d65fd1bd [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 Pesavento187e9f92023-03-20 22:46:22 -04003 * Copyright (c) 2013-2023 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"
Davide Pesavento152ef442023-04-22 02:02:29 -040023#include "ndn-cxx/util/concepts.hpp"
Yingdi Yu7a813892015-06-09 14:19:54 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050026#include "tests/unit/clock-fixture.hpp"
Davide Pesavento74daf742018-11-23 18:14:13 -050027
Yingdi Yu7a813892015-06-09 14:19:54 -070028#include <boost/lexical_cast.hpp>
29
30namespace ndn {
31namespace security {
Yingdi Yu10bf63a2015-11-04 14:14:37 -080032namespace tests {
33
34using namespace ndn::tests;
Yingdi Yu7a813892015-06-09 14:19:54 -070035
Davide Pesavento152ef442023-04-22 02:02:29 -040036BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ValidityPeriod>));
37BOOST_CONCEPT_ASSERT((WireEncodable<ValidityPeriod>));
38BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<ValidityPeriod>));
39BOOST_CONCEPT_ASSERT((WireDecodable<ValidityPeriod>));
40static_assert(std::is_convertible_v<ValidityPeriod::Error*, tlv::Error*>,
41 "ValidityPeriod::Error must inherit from tlv::Error");
42
Davide Pesaventoeee3e822016-11-26 19:19:34 +010043BOOST_AUTO_TEST_SUITE(Security)
44BOOST_AUTO_TEST_SUITE(TestValidityPeriod)
Yingdi Yu7a813892015-06-09 14:19:54 -070045
Junxiao Shi9ee770b2022-04-25 23:33:33 +000046BOOST_AUTO_TEST_SUITE(MakeRelative)
47
48BOOST_AUTO_TEST_CASE(FromNow)
49{
50 auto vp = ValidityPeriod::makeRelative(-1_s, 365_days, time::fromIsoString("20091117T203458,651387237"));
51 auto period = vp.getPeriod();
52 BOOST_CHECK_EQUAL(period.first, time::fromIsoString("20091117T203458"));
53 BOOST_CHECK_EQUAL(period.second, time::fromIsoString("20101117T203458"));
54}
55
56BOOST_AUTO_TEST_CASE(Positive)
57{
58 auto vp = ValidityPeriod::makeRelative(10_s, 1_days, time::fromIsoString("20091117T203458,651387237"));
59 auto period = vp.getPeriod();
60 BOOST_CHECK_EQUAL(period.first, time::fromIsoString("20091117T203509"));
61 BOOST_CHECK_EQUAL(period.second, time::fromIsoString("20091118T203458"));
62}
63
64BOOST_AUTO_TEST_CASE(Negative)
65{
66 auto vp = ValidityPeriod::makeRelative(-1_days, -10_s, time::fromIsoString("20091117T203458,651387237"));
67 auto period = vp.getPeriod();
68 BOOST_CHECK_EQUAL(period.first, time::fromIsoString("20091116T203459"));
69 BOOST_CHECK_EQUAL(period.second, time::fromIsoString("20091117T203448"));
70}
71
72BOOST_AUTO_TEST_SUITE_END() // MakeRelative
73
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050074BOOST_FIXTURE_TEST_CASE(ConstructorSetter, ClockFixture)
Yingdi Yu7a813892015-06-09 14:19:54 -070075{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050076 auto now = m_systemClock->getNow();
77 auto notBefore = now - 1_day;
78 auto notAfter = notBefore + 2_days;
79 ValidityPeriod validity1(notBefore, notAfter);
Yingdi Yu7a813892015-06-09 14:19:54 -070080
81 auto period = validity1.getPeriod();
82 BOOST_CHECK_GE(period.first, notBefore); // fractional seconds will be removed
Davide Pesavento0f830802018-01-16 23:58:58 -050083 BOOST_CHECK_LT(period.first, notBefore + 1_s);
Yingdi Yu7a813892015-06-09 14:19:54 -070084
85 BOOST_CHECK_LE(period.second, notAfter); // fractional seconds will be removed
Davide Pesavento0f830802018-01-16 23:58:58 -050086 BOOST_CHECK_GT(period.second, notAfter - 1_s);
Yingdi Yu7a813892015-06-09 14:19:54 -070087 BOOST_CHECK_EQUAL(validity1.isValid(), true);
88
Davide Pesavento0f830802018-01-16 23:58:58 -050089 BOOST_CHECK_EQUAL(ValidityPeriod(now - 2_days, now - 1_day).isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070090
91 BOOST_CHECK_NO_THROW((ValidityPeriod()));
92 ValidityPeriod validity2;
Yingdi Yu10bf63a2015-11-04 14:14:37 -080093 BOOST_CHECK_EQUAL(validity2.isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070094
95 validity2.setPeriod(notBefore, notAfter);
Davide Pesavento187e9f92023-03-20 22:46:22 -040096 BOOST_CHECK(validity2.getPeriod() != std::pair(time::getUnixEpoch(), time::getUnixEpoch()));
Yingdi Yu7a813892015-06-09 14:19:54 -070097 BOOST_CHECK_EQUAL(validity2, validity1);
98
Davide Pesavento0f830802018-01-16 23:58:58 -050099 validity1.setPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 10 * 365_days);
Yingdi Yu7a813892015-06-09 14:19:54 -0700100 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
101 "(19700101T000000, 19791230T000000)");
102
Davide Pesavento0f830802018-01-16 23:58:58 -0500103 validity1.setPeriod(time::getUnixEpoch() + 1_ns,
104 time::getUnixEpoch() + (10 * 365_days) + 1_ns);
Yingdi Yu7a813892015-06-09 14:19:54 -0700105 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
106 "(19700101T000001, 19791230T000000)");
Yingdi Yu10bf63a2015-11-04 14:14:37 -0800107
108 BOOST_CHECK_EQUAL(ValidityPeriod(now, now).isValid(), true);
Davide Pesavento0f830802018-01-16 23:58:58 -0500109 BOOST_CHECK_EQUAL(ValidityPeriod(now + 1_s, now).isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -0700110}
111
112const uint8_t VP1[] = {
113 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
114 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
115 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
116 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
117 0xfd, 0x00, 0xff, 0x0f, // NotAfter
118 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
119 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
120};
121
122BOOST_AUTO_TEST_CASE(EncodingDecoding)
123{
124 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -0500125 time::system_clock::TimePoint notAfter = notBefore + 1_day;
Yingdi Yu7a813892015-06-09 14:19:54 -0700126 ValidityPeriod v1(notBefore, notAfter);
Yingdi Yu7a813892015-06-09 14:19:54 -0700127 BOOST_CHECK_EQUAL_COLLECTIONS(v1.wireEncode().begin(), v1.wireEncode().end(),
128 VP1, VP1 + sizeof(VP1));
129
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500130 ValidityPeriod v2(Block{VP1});
Yingdi Yu7a813892015-06-09 14:19:54 -0700131 BOOST_CHECK(v1.getPeriod() == v2.getPeriod());
132}
133
134const uint8_t VP_E1[] = {
135 0xfd, 0x00, 0xff, 0x26, // ValidityPeriod (error)
136 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
137 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
138 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
139 0xfd, 0x00, 0xff, 0x0f, // NotAfter
140 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
141 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
142};
143
144const uint8_t VP_E2[] = {
145 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
146 0xfd, 0x00, 0xff, 0x0f, // NotBefore (error)
147 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
148 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
149 0xfd, 0x00, 0xff, 0x0f, // NotAfter
150 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
151 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
152};
153
154const uint8_t VP_E3[] = {
155 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
156 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
157 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
158 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
159 0xfd, 0x00, 0xfe, 0x0f, // NotAfter (error)
160 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
161 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
162};
163
164const uint8_t VP_E4[] = {
165 0xfd, 0x00, 0xfd, 0x39, // ValidityPeriod
166 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
167 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
168 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
169 0xfd, 0x00, 0xff, 0x0f, // NotAfter
170 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
171 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
172 0xfd, 0x00, 0xff, 0x0f, // NotAfter (error)
173 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
174 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
175};
176
177const uint8_t VP_E5[] = {
178 0xfd, 0x00, 0xfd, 0x13, // ValidityPeriod
179 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
180 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
181 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
182};
183
184const uint8_t VP_E6[] = {
185 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
186 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
187 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T00000\xFF
188 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFF,
189 0xfd, 0x00, 0xff, 0x0f, // NotAfter
190 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
191 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
192};
193
Yingdi Yu7a813892015-06-09 14:19:54 -0700194BOOST_AUTO_TEST_CASE(DecodingError)
195{
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500196 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E1}), ValidityPeriod::Error);
197 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E2}), ValidityPeriod::Error);
198 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E3}), ValidityPeriod::Error);
199 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E4}), ValidityPeriod::Error);
200 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E5}), ValidityPeriod::Error);
201 BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E6}), ValidityPeriod::Error);
Yingdi Yu7a813892015-06-09 14:19:54 -0700202
203 Block emptyBlock;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500204 BOOST_CHECK_THROW(ValidityPeriod{emptyBlock}, ValidityPeriod::Error);
Yingdi Yu7a813892015-06-09 14:19:54 -0700205}
206
207BOOST_AUTO_TEST_CASE(Comparison)
208{
209 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -0500210 time::system_clock::TimePoint notAfter = notBefore + 1_day;
211 time::system_clock::TimePoint notAfter2 = notBefore + 2_days;
Yingdi Yu7a813892015-06-09 14:19:54 -0700212
213 ValidityPeriod validity1(notBefore, notAfter);
214 ValidityPeriod validity2(notBefore, notAfter);
215 BOOST_CHECK(validity1 == validity2);
216
217 ValidityPeriod validity3(notBefore, notAfter2);
218 BOOST_CHECK(validity1 != validity3);
219}
220
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100221BOOST_AUTO_TEST_SUITE_END() // TestValidityPeriod
222BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu7a813892015-06-09 14:19:54 -0700223
Yingdi Yu10bf63a2015-11-04 14:14:37 -0800224} // namespace tests
Yingdi Yu7a813892015-06-09 14:19:54 -0700225} // namespace security
226} // namespace ndn