Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/validity-period.hpp" |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/unit/clock-fixture.hpp" |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 26 | |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 31 | namespace tests { |
| 32 | |
| 33 | using namespace ndn::tests; |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(Security) |
| 36 | BOOST_AUTO_TEST_SUITE(TestValidityPeriod) |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(MakeRelative) |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE(FromNow) |
| 41 | { |
| 42 | auto vp = ValidityPeriod::makeRelative(-1_s, 365_days, time::fromIsoString("20091117T203458,651387237")); |
| 43 | auto period = vp.getPeriod(); |
| 44 | BOOST_CHECK_EQUAL(period.first, time::fromIsoString("20091117T203458")); |
| 45 | BOOST_CHECK_EQUAL(period.second, time::fromIsoString("20101117T203458")); |
| 46 | } |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE(Positive) |
| 49 | { |
| 50 | auto vp = ValidityPeriod::makeRelative(10_s, 1_days, time::fromIsoString("20091117T203458,651387237")); |
| 51 | auto period = vp.getPeriod(); |
| 52 | BOOST_CHECK_EQUAL(period.first, time::fromIsoString("20091117T203509")); |
| 53 | BOOST_CHECK_EQUAL(period.second, time::fromIsoString("20091118T203458")); |
| 54 | } |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(Negative) |
| 57 | { |
| 58 | auto vp = ValidityPeriod::makeRelative(-1_days, -10_s, time::fromIsoString("20091117T203458,651387237")); |
| 59 | auto period = vp.getPeriod(); |
| 60 | BOOST_CHECK_EQUAL(period.first, time::fromIsoString("20091116T203459")); |
| 61 | BOOST_CHECK_EQUAL(period.second, time::fromIsoString("20091117T203448")); |
| 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_SUITE_END() // MakeRelative |
| 65 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 66 | BOOST_FIXTURE_TEST_CASE(ConstructorSetter, ClockFixture) |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 67 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 68 | auto now = m_systemClock->getNow(); |
| 69 | auto notBefore = now - 1_day; |
| 70 | auto notAfter = notBefore + 2_days; |
| 71 | ValidityPeriod validity1(notBefore, notAfter); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 72 | |
| 73 | auto period = validity1.getPeriod(); |
| 74 | BOOST_CHECK_GE(period.first, notBefore); // fractional seconds will be removed |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 75 | BOOST_CHECK_LT(period.first, notBefore + 1_s); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 76 | |
| 77 | BOOST_CHECK_LE(period.second, notAfter); // fractional seconds will be removed |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 78 | BOOST_CHECK_GT(period.second, notAfter - 1_s); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(validity1.isValid(), true); |
| 80 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(ValidityPeriod(now - 2_days, now - 1_day).isValid(), false); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 82 | |
| 83 | BOOST_CHECK_NO_THROW((ValidityPeriod())); |
| 84 | ValidityPeriod validity2; |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(validity2.isValid(), false); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 86 | |
| 87 | validity2.setPeriod(notBefore, notAfter); |
| 88 | BOOST_CHECK(validity2.getPeriod() != std::make_pair(time::getUnixEpoch(), time::getUnixEpoch())); |
| 89 | BOOST_CHECK_EQUAL(validity2, validity1); |
| 90 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 91 | validity1.setPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 10 * 365_days); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1), |
| 93 | "(19700101T000000, 19791230T000000)"); |
| 94 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 95 | validity1.setPeriod(time::getUnixEpoch() + 1_ns, |
| 96 | time::getUnixEpoch() + (10 * 365_days) + 1_ns); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1), |
| 98 | "(19700101T000001, 19791230T000000)"); |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 99 | |
| 100 | BOOST_CHECK_EQUAL(ValidityPeriod(now, now).isValid(), true); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(ValidityPeriod(now + 1_s, now).isValid(), false); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | const uint8_t VP1[] = { |
| 105 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 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 | |
| 114 | BOOST_AUTO_TEST_CASE(EncodingDecoding) |
| 115 | { |
| 116 | time::system_clock::TimePoint notBefore = time::getUnixEpoch(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 117 | time::system_clock::TimePoint notAfter = notBefore + 1_day; |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 118 | ValidityPeriod v1(notBefore, notAfter); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 119 | BOOST_CHECK_EQUAL_COLLECTIONS(v1.wireEncode().begin(), v1.wireEncode().end(), |
| 120 | VP1, VP1 + sizeof(VP1)); |
| 121 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 122 | ValidityPeriod v2(Block{VP1}); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 123 | BOOST_CHECK(v1.getPeriod() == v2.getPeriod()); |
| 124 | } |
| 125 | |
| 126 | const uint8_t VP_E1[] = { |
| 127 | 0xfd, 0x00, 0xff, 0x26, // ValidityPeriod (error) |
| 128 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 129 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 130 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 131 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 132 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 133 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 134 | }; |
| 135 | |
| 136 | const uint8_t VP_E2[] = { |
| 137 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 138 | 0xfd, 0x00, 0xff, 0x0f, // NotBefore (error) |
| 139 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 140 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 141 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 142 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 143 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 144 | }; |
| 145 | |
| 146 | const uint8_t VP_E3[] = { |
| 147 | 0xfd, 0x00, 0xfd, 0x26, // 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 | 0xfd, 0x00, 0xfe, 0x0f, // NotAfter (error) |
| 152 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 153 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 154 | }; |
| 155 | |
| 156 | const uint8_t VP_E4[] = { |
| 157 | 0xfd, 0x00, 0xfd, 0x39, // ValidityPeriod |
| 158 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 159 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 160 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 161 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 162 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 163 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 164 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter (error) |
| 165 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 166 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 167 | }; |
| 168 | |
| 169 | const uint8_t VP_E5[] = { |
| 170 | 0xfd, 0x00, 0xfd, 0x13, // ValidityPeriod |
| 171 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 172 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 173 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 174 | }; |
| 175 | |
| 176 | const uint8_t VP_E6[] = { |
| 177 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 178 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 179 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T00000\xFF |
| 180 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFF, |
| 181 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 182 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 183 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 184 | }; |
| 185 | |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 186 | BOOST_AUTO_TEST_CASE(DecodingError) |
| 187 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 188 | BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E1}), ValidityPeriod::Error); |
| 189 | BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E2}), ValidityPeriod::Error); |
| 190 | BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E3}), ValidityPeriod::Error); |
| 191 | BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E4}), ValidityPeriod::Error); |
| 192 | BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E5}), ValidityPeriod::Error); |
| 193 | BOOST_CHECK_THROW(ValidityPeriod(Block{VP_E6}), ValidityPeriod::Error); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 194 | |
| 195 | Block emptyBlock; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 196 | BOOST_CHECK_THROW(ValidityPeriod{emptyBlock}, ValidityPeriod::Error); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | BOOST_AUTO_TEST_CASE(Comparison) |
| 200 | { |
| 201 | time::system_clock::TimePoint notBefore = time::getUnixEpoch(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 202 | time::system_clock::TimePoint notAfter = notBefore + 1_day; |
| 203 | time::system_clock::TimePoint notAfter2 = notBefore + 2_days; |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 204 | |
| 205 | ValidityPeriod validity1(notBefore, notAfter); |
| 206 | ValidityPeriod validity2(notBefore, notAfter); |
| 207 | BOOST_CHECK(validity1 == validity2); |
| 208 | |
| 209 | ValidityPeriod validity3(notBefore, notAfter2); |
| 210 | BOOST_CHECK(validity1 != validity3); |
| 211 | } |
| 212 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 213 | BOOST_AUTO_TEST_SUITE_END() // TestValidityPeriod |
| 214 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 215 | |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 216 | } // namespace tests |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 217 | } // namespace security |
| 218 | } // namespace ndn |