blob: 4c6fb6b9606057bc50b70dd65bbf246b7811fafe [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"
Yingdi Yu10bf63a2015-11-04 14:14:37 -080025#include "unit-tests/unit-test-time-fixture.hpp"
Yingdi Yu7a813892015-06-09 14:19:54 -070026#include <boost/lexical_cast.hpp>
27
28namespace ndn {
29namespace security {
Yingdi Yu10bf63a2015-11-04 14:14:37 -080030namespace tests {
31
32using namespace ndn::tests;
Yingdi Yu7a813892015-06-09 14:19:54 -070033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_SUITE(Security)
35BOOST_AUTO_TEST_SUITE(TestValidityPeriod)
Yingdi Yu7a813892015-06-09 14:19:54 -070036
Yingdi Yu10bf63a2015-11-04 14:14:37 -080037BOOST_FIXTURE_TEST_CASE(ConstructorSetter, UnitTestTimeFixture)
Yingdi Yu7a813892015-06-09 14:19:54 -070038{
Yingdi Yu10bf63a2015-11-04 14:14:37 -080039 time::system_clock::TimePoint now = this->systemClock->getNow();
40
Davide Pesavento0f830802018-01-16 23:58:58 -050041 time::system_clock::TimePoint notBefore = now - 1_day;
42 time::system_clock::TimePoint notAfter = notBefore + 2_days;
Yingdi Yu7a813892015-06-09 14:19:54 -070043
44 ValidityPeriod validity1 = ValidityPeriod(notBefore, notAfter);
45
46 auto period = validity1.getPeriod();
47 BOOST_CHECK_GE(period.first, notBefore); // fractional seconds will be removed
Davide Pesavento0f830802018-01-16 23:58:58 -050048 BOOST_CHECK_LT(period.first, notBefore + 1_s);
Yingdi Yu7a813892015-06-09 14:19:54 -070049
50 BOOST_CHECK_LE(period.second, notAfter); // fractional seconds will be removed
Davide Pesavento0f830802018-01-16 23:58:58 -050051 BOOST_CHECK_GT(period.second, notAfter - 1_s);
Yingdi Yu7a813892015-06-09 14:19:54 -070052 BOOST_CHECK_EQUAL(validity1.isValid(), true);
53
Davide Pesavento0f830802018-01-16 23:58:58 -050054 BOOST_CHECK_EQUAL(ValidityPeriod(now - 2_days, now - 1_day).isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070055
56 BOOST_CHECK_NO_THROW((ValidityPeriod()));
57 ValidityPeriod validity2;
Yingdi Yu10bf63a2015-11-04 14:14:37 -080058 BOOST_CHECK_EQUAL(validity2.isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070059
60 validity2.setPeriod(notBefore, notAfter);
61 BOOST_CHECK(validity2.getPeriod() != std::make_pair(time::getUnixEpoch(), time::getUnixEpoch()));
62 BOOST_CHECK_EQUAL(validity2, validity1);
63
Davide Pesavento0f830802018-01-16 23:58:58 -050064 validity1.setPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 10 * 365_days);
Yingdi Yu7a813892015-06-09 14:19:54 -070065 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
66 "(19700101T000000, 19791230T000000)");
67
Davide Pesavento0f830802018-01-16 23:58:58 -050068 validity1.setPeriod(time::getUnixEpoch() + 1_ns,
69 time::getUnixEpoch() + (10 * 365_days) + 1_ns);
Yingdi Yu7a813892015-06-09 14:19:54 -070070 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
71 "(19700101T000001, 19791230T000000)");
Yingdi Yu10bf63a2015-11-04 14:14:37 -080072
73 BOOST_CHECK_EQUAL(ValidityPeriod(now, now).isValid(), true);
Davide Pesavento0f830802018-01-16 23:58:58 -050074 BOOST_CHECK_EQUAL(ValidityPeriod(now + 1_s, now).isValid(), false);
Yingdi Yu7a813892015-06-09 14:19:54 -070075}
76
77const uint8_t VP1[] = {
78 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
79 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
80 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
81 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
82 0xfd, 0x00, 0xff, 0x0f, // NotAfter
83 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
84 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
85};
86
87BOOST_AUTO_TEST_CASE(EncodingDecoding)
88{
89 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -050090 time::system_clock::TimePoint notAfter = notBefore + 1_day;
Yingdi Yu7a813892015-06-09 14:19:54 -070091
92 ValidityPeriod v1(notBefore, notAfter);
93
94 BOOST_CHECK_EQUAL_COLLECTIONS(v1.wireEncode().begin(), v1.wireEncode().end(),
95 VP1, VP1 + sizeof(VP1));
96
97 BOOST_REQUIRE_NO_THROW(ValidityPeriod(Block(VP1, sizeof(VP1))));
98 Block block(VP1, sizeof(VP1));
99 ValidityPeriod v2(block);
100 BOOST_CHECK(v1.getPeriod() == v2.getPeriod());
101}
102
103const uint8_t VP_E1[] = {
104 0xfd, 0x00, 0xff, 0x26, // ValidityPeriod (error)
105 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
106 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
107 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
108 0xfd, 0x00, 0xff, 0x0f, // NotAfter
109 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
110 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
111};
112
113const uint8_t VP_E2[] = {
114 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
115 0xfd, 0x00, 0xff, 0x0f, // NotBefore (error)
116 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
117 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
118 0xfd, 0x00, 0xff, 0x0f, // NotAfter
119 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
120 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
121};
122
123const uint8_t VP_E3[] = {
124 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
125 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
126 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
127 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
128 0xfd, 0x00, 0xfe, 0x0f, // NotAfter (error)
129 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
130 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
131};
132
133const uint8_t VP_E4[] = {
134 0xfd, 0x00, 0xfd, 0x39, // ValidityPeriod
135 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
136 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
137 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
138 0xfd, 0x00, 0xff, 0x0f, // NotAfter
139 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
140 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
141 0xfd, 0x00, 0xff, 0x0f, // NotAfter (error)
142 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
143 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
144};
145
146const uint8_t VP_E5[] = {
147 0xfd, 0x00, 0xfd, 0x13, // ValidityPeriod
148 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
149 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
150 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
151};
152
153const uint8_t VP_E6[] = {
154 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
155 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
156 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T00000\xFF
157 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFF,
158 0xfd, 0x00, 0xff, 0x0f, // NotAfter
159 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
160 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
161};
162
163
164BOOST_AUTO_TEST_CASE(DecodingError)
165{
166 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E1, sizeof(VP_E1))), ValidityPeriod::Error);
167
168 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E2, sizeof(VP_E2))), ValidityPeriod::Error);
169 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E3, sizeof(VP_E3))), ValidityPeriod::Error);
170
171 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E4, sizeof(VP_E4))), ValidityPeriod::Error);
172 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E5, sizeof(VP_E5))), ValidityPeriod::Error);
173
174 Block emptyBlock;
175 BOOST_CHECK_THROW((ValidityPeriod(emptyBlock)), ValidityPeriod::Error);
176
177 BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E6, sizeof(VP_E6))), ValidityPeriod::Error);
178}
179
180BOOST_AUTO_TEST_CASE(Comparison)
181{
182 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -0500183 time::system_clock::TimePoint notAfter = notBefore + 1_day;
184 time::system_clock::TimePoint notAfter2 = notBefore + 2_days;
Yingdi Yu7a813892015-06-09 14:19:54 -0700185
186 ValidityPeriod validity1(notBefore, notAfter);
187 ValidityPeriod validity2(notBefore, notAfter);
188 BOOST_CHECK(validity1 == validity2);
189
190 ValidityPeriod validity3(notBefore, notAfter2);
191 BOOST_CHECK(validity1 != validity3);
192}
193
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100194BOOST_AUTO_TEST_SUITE_END() // TestValidityPeriod
195BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu7a813892015-06-09 14:19:54 -0700196
Yingdi Yu10bf63a2015-11-04 14:14:37 -0800197} // namespace tests
Yingdi Yu7a813892015-06-09 14:19:54 -0700198} // namespace security
199} // namespace ndn