blob: 14d2a0f8da4d90d3a89e9d9c38118ed9c57cc559 [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/*
3 * Copyright (c) 2013-2018 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
22#include "security/validity-period.hpp"
23
24#include "boost-test.hpp"
Davide Pesavento74daf742018-11-23 18:14:13 -050025#include "../unit-test-time-fixture.hpp"
26
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
Yingdi Yu10bf63a2015-11-04 14:14:37 -080038BOOST_FIXTURE_TEST_CASE(ConstructorSetter, UnitTestTimeFixture)
Yingdi Yu7a813892015-06-09 14:19:54 -070039{
Yingdi Yu10bf63a2015-11-04 14:14:37 -080040 time::system_clock::TimePoint now = this->systemClock->getNow();
41
Davide Pesavento0f830802018-01-16 23:58:58 -050042 time::system_clock::TimePoint notBefore = now - 1_day;
43 time::system_clock::TimePoint notAfter = notBefore + 2_days;
Yingdi Yu7a813892015-06-09 14:19:54 -070044
45 ValidityPeriod validity1 = ValidityPeriod(notBefore, notAfter);
46
47 auto period = validity1.getPeriod();
48 BOOST_CHECK_GE(period.first, notBefore); // fractional seconds will be removed
Davide Pesavento0f830802018-01-16 23:58:58 -050049 BOOST_CHECK_LT(period.first, notBefore + 1_s);
Yingdi Yu7a813892015-06-09 14:19:54 -070050
51 BOOST_CHECK_LE(period.second, notAfter); // fractional seconds will be removed
Davide Pesavento0f830802018-01-16 23:58:58 -050052 BOOST_CHECK_GT(period.second, notAfter - 1_s);
Yingdi Yu7a813892015-06-09 14:19:54 -070053 BOOST_CHECK_EQUAL(validity1.isValid(), true);
54
Davide Pesavento0f830802018-01-16 23:58:58 -050055 BOOST_CHECK_EQUAL(ValidityPeriod(now - 2_days, now - 1_day).isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070056
57 BOOST_CHECK_NO_THROW((ValidityPeriod()));
58 ValidityPeriod validity2;
Yingdi Yu10bf63a2015-11-04 14:14:37 -080059 BOOST_CHECK_EQUAL(validity2.isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070060
61 validity2.setPeriod(notBefore, notAfter);
62 BOOST_CHECK(validity2.getPeriod() != std::make_pair(time::getUnixEpoch(), time::getUnixEpoch()));
63 BOOST_CHECK_EQUAL(validity2, validity1);
64
Davide Pesavento0f830802018-01-16 23:58:58 -050065 validity1.setPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 10 * 365_days);
Yingdi Yu7a813892015-06-09 14:19:54 -070066 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
67 "(19700101T000000, 19791230T000000)");
68
Davide Pesavento0f830802018-01-16 23:58:58 -050069 validity1.setPeriod(time::getUnixEpoch() + 1_ns,
70 time::getUnixEpoch() + (10 * 365_days) + 1_ns);
Yingdi Yu7a813892015-06-09 14:19:54 -070071 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
72 "(19700101T000001, 19791230T000000)");
Yingdi Yu10bf63a2015-11-04 14:14:37 -080073
74 BOOST_CHECK_EQUAL(ValidityPeriod(now, now).isValid(), true);
Davide Pesavento0f830802018-01-16 23:58:58 -050075 BOOST_CHECK_EQUAL(ValidityPeriod(now + 1_s, now).isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070076}
77
78const uint8_t VP1[] = {
79 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
80 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
81 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
82 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
83 0xfd, 0x00, 0xff, 0x0f, // NotAfter
84 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
85 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
86};
87
88BOOST_AUTO_TEST_CASE(EncodingDecoding)
89{
90 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -050091 time::system_clock::TimePoint notAfter = notBefore + 1_day;
Yingdi Yu7a813892015-06-09 14:19:54 -070092
93 ValidityPeriod v1(notBefore, notAfter);
94
95 BOOST_CHECK_EQUAL_COLLECTIONS(v1.wireEncode().begin(), v1.wireEncode().end(),
96 VP1, VP1 + sizeof(VP1));
97
98 BOOST_REQUIRE_NO_THROW(ValidityPeriod(Block(VP1, sizeof(VP1))));
99 Block block(VP1, sizeof(VP1));
100 ValidityPeriod v2(block);
101 BOOST_CHECK(v1.getPeriod() == v2.getPeriod());
102}
103
104const uint8_t VP_E1[] = {
105 0xfd, 0x00, 0xff, 0x26, // ValidityPeriod (error)
106 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
107 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
108 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
109 0xfd, 0x00, 0xff, 0x0f, // NotAfter
110 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
111 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
112};
113
114const uint8_t VP_E2[] = {
115 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
116 0xfd, 0x00, 0xff, 0x0f, // NotBefore (error)
117 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
118 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
119 0xfd, 0x00, 0xff, 0x0f, // NotAfter
120 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
121 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
122};
123
124const uint8_t VP_E3[] = {
125 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
126 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
127 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
128 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
129 0xfd, 0x00, 0xfe, 0x0f, // NotAfter (error)
130 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
131 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
132};
133
134const uint8_t VP_E4[] = {
135 0xfd, 0x00, 0xfd, 0x39, // ValidityPeriod
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 0xfd, 0x00, 0xff, 0x0f, // NotAfter (error)
143 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
144 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
145};
146
147const uint8_t VP_E5[] = {
148 0xfd, 0x00, 0xfd, 0x13, // ValidityPeriod
149 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
150 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
151 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
152};
153
154const uint8_t VP_E6[] = {
155 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
156 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
157 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T00000\xFF
158 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFF,
159 0xfd, 0x00, 0xff, 0x0f, // NotAfter
160 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
161 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
162};
163
164
165BOOST_AUTO_TEST_CASE(DecodingError)
166{
167 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E1, sizeof(VP_E1))), ValidityPeriod::Error);
168
169 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E2, sizeof(VP_E2))), ValidityPeriod::Error);
170 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E3, sizeof(VP_E3))), ValidityPeriod::Error);
171
172 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E4, sizeof(VP_E4))), ValidityPeriod::Error);
173 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E5, sizeof(VP_E5))), ValidityPeriod::Error);
174
175 Block emptyBlock;
176 BOOST_CHECK_THROW((ValidityPeriod(emptyBlock)), ValidityPeriod::Error);
177
178 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E6, sizeof(VP_E6))), ValidityPeriod::Error);
179}
180
181BOOST_AUTO_TEST_CASE(Comparison)
182{
183 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -0500184 time::system_clock::TimePoint notAfter = notBefore + 1_day;
185 time::system_clock::TimePoint notAfter2 = notBefore + 2_days;
Yingdi Yu7a813892015-06-09 14:19:54 -0700186
187 ValidityPeriod validity1(notBefore, notAfter);
188 ValidityPeriod validity2(notBefore, notAfter);
189 BOOST_CHECK(validity1 == validity2);
190
191 ValidityPeriod validity3(notBefore, notAfter2);
192 BOOST_CHECK(validity1 != validity3);
193}
194
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100195BOOST_AUTO_TEST_SUITE_END() // TestValidityPeriod
196BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu7a813892015-06-09 14:19:54 -0700197
Yingdi Yu10bf63a2015-11-04 14:14:37 -0800198} // namespace tests
Yingdi Yu7a813892015-06-09 14:19:54 -0700199} // namespace security
200} // namespace ndn