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 | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_CASE(ConstructorSetter, ClockFixture) |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 39 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 40 | auto now = m_systemClock->getNow(); |
| 41 | auto notBefore = now - 1_day; |
| 42 | auto notAfter = notBefore + 2_days; |
| 43 | ValidityPeriod validity1(notBefore, notAfter); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 44 | |
| 45 | auto period = validity1.getPeriod(); |
| 46 | BOOST_CHECK_GE(period.first, notBefore); // fractional seconds will be removed |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 47 | BOOST_CHECK_LT(period.first, notBefore + 1_s); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 48 | |
| 49 | BOOST_CHECK_LE(period.second, notAfter); // fractional seconds will be removed |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 50 | BOOST_CHECK_GT(period.second, notAfter - 1_s); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(validity1.isValid(), true); |
| 52 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(ValidityPeriod(now - 2_days, now - 1_day).isValid(), false); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 54 | |
| 55 | BOOST_CHECK_NO_THROW((ValidityPeriod())); |
| 56 | ValidityPeriod validity2; |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(validity2.isValid(), false); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 58 | |
| 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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 63 | validity1.setPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 10 * 365_days); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1), |
| 65 | "(19700101T000000, 19791230T000000)"); |
| 66 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 67 | validity1.setPeriod(time::getUnixEpoch() + 1_ns, |
| 68 | time::getUnixEpoch() + (10 * 365_days) + 1_ns); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1), |
| 70 | "(19700101T000001, 19791230T000000)"); |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 71 | |
| 72 | BOOST_CHECK_EQUAL(ValidityPeriod(now, now).isValid(), true); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(ValidityPeriod(now + 1_s, now).isValid(), false); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | const 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 | |
| 86 | BOOST_AUTO_TEST_CASE(EncodingDecoding) |
| 87 | { |
| 88 | time::system_clock::TimePoint notBefore = time::getUnixEpoch(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 89 | time::system_clock::TimePoint notAfter = notBefore + 1_day; |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 90 | |
| 91 | ValidityPeriod v1(notBefore, notAfter); |
| 92 | |
| 93 | BOOST_CHECK_EQUAL_COLLECTIONS(v1.wireEncode().begin(), v1.wireEncode().end(), |
| 94 | VP1, VP1 + sizeof(VP1)); |
| 95 | |
| 96 | BOOST_REQUIRE_NO_THROW(ValidityPeriod(Block(VP1, sizeof(VP1)))); |
| 97 | Block block(VP1, sizeof(VP1)); |
| 98 | ValidityPeriod v2(block); |
| 99 | BOOST_CHECK(v1.getPeriod() == v2.getPeriod()); |
| 100 | } |
| 101 | |
| 102 | const uint8_t VP_E1[] = { |
| 103 | 0xfd, 0x00, 0xff, 0x26, // ValidityPeriod (error) |
| 104 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 105 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 106 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 107 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 108 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 109 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 110 | }; |
| 111 | |
| 112 | const uint8_t VP_E2[] = { |
| 113 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 114 | 0xfd, 0x00, 0xff, 0x0f, // NotBefore (error) |
| 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 | |
| 122 | const uint8_t VP_E3[] = { |
| 123 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 124 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 125 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 126 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 127 | 0xfd, 0x00, 0xfe, 0x0f, // NotAfter (error) |
| 128 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 129 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 130 | }; |
| 131 | |
| 132 | const uint8_t VP_E4[] = { |
| 133 | 0xfd, 0x00, 0xfd, 0x39, // ValidityPeriod |
| 134 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 135 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 136 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 137 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 138 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 139 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 140 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter (error) |
| 141 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 142 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 143 | }; |
| 144 | |
| 145 | const uint8_t VP_E5[] = { |
| 146 | 0xfd, 0x00, 0xfd, 0x13, // ValidityPeriod |
| 147 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 148 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 149 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 150 | }; |
| 151 | |
| 152 | const uint8_t VP_E6[] = { |
| 153 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 154 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 155 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T00000\xFF |
| 156 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFF, |
| 157 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 158 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 159 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | BOOST_AUTO_TEST_CASE(DecodingError) |
| 164 | { |
| 165 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E1, sizeof(VP_E1))), ValidityPeriod::Error); |
| 166 | |
| 167 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E2, sizeof(VP_E2))), ValidityPeriod::Error); |
| 168 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E3, sizeof(VP_E3))), ValidityPeriod::Error); |
| 169 | |
| 170 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E4, sizeof(VP_E4))), ValidityPeriod::Error); |
| 171 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E5, sizeof(VP_E5))), ValidityPeriod::Error); |
| 172 | |
| 173 | Block emptyBlock; |
| 174 | BOOST_CHECK_THROW((ValidityPeriod(emptyBlock)), ValidityPeriod::Error); |
| 175 | |
| 176 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E6, sizeof(VP_E6))), ValidityPeriod::Error); |
| 177 | } |
| 178 | |
| 179 | BOOST_AUTO_TEST_CASE(Comparison) |
| 180 | { |
| 181 | time::system_clock::TimePoint notBefore = time::getUnixEpoch(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 182 | time::system_clock::TimePoint notAfter = notBefore + 1_day; |
| 183 | time::system_clock::TimePoint notAfter2 = notBefore + 2_days; |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 184 | |
| 185 | ValidityPeriod validity1(notBefore, notAfter); |
| 186 | ValidityPeriod validity2(notBefore, notAfter); |
| 187 | BOOST_CHECK(validity1 == validity2); |
| 188 | |
| 189 | ValidityPeriod validity3(notBefore, notAfter2); |
| 190 | BOOST_CHECK(validity1 != validity3); |
| 191 | } |
| 192 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 193 | BOOST_AUTO_TEST_SUITE_END() // TestValidityPeriod |
| 194 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 195 | |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 196 | } // namespace tests |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 197 | } // namespace security |
| 198 | } // namespace ndn |