Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 | |
| 22 | #include "security/validity-period.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 25 | #include "unit-tests/unit-test-time-fixture.hpp" |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 30 | namespace tests { |
| 31 | |
| 32 | using namespace ndn::tests; |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Security) |
| 35 | BOOST_AUTO_TEST_SUITE(TestValidityPeriod) |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 36 | |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_CASE(ConstructorSetter, UnitTestTimeFixture) |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 38 | { |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 39 | time::system_clock::TimePoint now = this->systemClock->getNow(); |
| 40 | |
| 41 | time::system_clock::TimePoint notBefore = now - time::days(1); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 42 | time::system_clock::TimePoint notAfter = notBefore + time::days(2); |
| 43 | |
| 44 | ValidityPeriod validity1 = ValidityPeriod(notBefore, notAfter); |
| 45 | |
| 46 | auto period = validity1.getPeriod(); |
| 47 | BOOST_CHECK_GE(period.first, notBefore); // fractional seconds will be removed |
| 48 | BOOST_CHECK_LT(period.first, notBefore + time::seconds(1)); |
| 49 | |
| 50 | BOOST_CHECK_LE(period.second, notAfter); // fractional seconds will be removed |
| 51 | BOOST_CHECK_GT(period.second, notAfter - time::seconds(1)); |
| 52 | BOOST_CHECK_EQUAL(validity1.isValid(), true); |
| 53 | |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(ValidityPeriod(now - time::days(2), |
| 55 | now - time::days(1)).isValid(), |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 56 | false); |
| 57 | |
| 58 | BOOST_CHECK_NO_THROW((ValidityPeriod())); |
| 59 | ValidityPeriod validity2; |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(validity2.isValid(), false); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 61 | |
| 62 | validity2.setPeriod(notBefore, notAfter); |
| 63 | BOOST_CHECK(validity2.getPeriod() != std::make_pair(time::getUnixEpoch(), time::getUnixEpoch())); |
| 64 | BOOST_CHECK_EQUAL(validity2, validity1); |
| 65 | |
| 66 | validity1.setPeriod(time::getUnixEpoch(), time::getUnixEpoch() + time::days(10 * 365)); |
| 67 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1), |
| 68 | "(19700101T000000, 19791230T000000)"); |
| 69 | |
| 70 | validity1.setPeriod(time::getUnixEpoch() + time::nanoseconds(1), |
| 71 | time::getUnixEpoch() + time::days(10 * 365) + time::nanoseconds(1)); |
| 72 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1), |
| 73 | "(19700101T000001, 19791230T000000)"); |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 74 | |
| 75 | BOOST_CHECK_EQUAL(ValidityPeriod(now, now).isValid(), true); |
| 76 | BOOST_CHECK_EQUAL(ValidityPeriod(now + time::seconds(1), now).isValid(), false); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | const uint8_t VP1[] = { |
| 80 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 81 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 82 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 83 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 84 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 85 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 86 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 87 | }; |
| 88 | |
| 89 | BOOST_AUTO_TEST_CASE(EncodingDecoding) |
| 90 | { |
| 91 | time::system_clock::TimePoint notBefore = time::getUnixEpoch(); |
| 92 | time::system_clock::TimePoint notAfter = notBefore + time::days(1); |
| 93 | |
| 94 | ValidityPeriod v1(notBefore, notAfter); |
| 95 | |
| 96 | BOOST_CHECK_EQUAL_COLLECTIONS(v1.wireEncode().begin(), v1.wireEncode().end(), |
| 97 | VP1, VP1 + sizeof(VP1)); |
| 98 | |
| 99 | BOOST_REQUIRE_NO_THROW(ValidityPeriod(Block(VP1, sizeof(VP1)))); |
| 100 | Block block(VP1, sizeof(VP1)); |
| 101 | ValidityPeriod v2(block); |
| 102 | BOOST_CHECK(v1.getPeriod() == v2.getPeriod()); |
| 103 | } |
| 104 | |
| 105 | const uint8_t VP_E1[] = { |
| 106 | 0xfd, 0x00, 0xff, 0x26, // ValidityPeriod (error) |
| 107 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 108 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 109 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 110 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 111 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 112 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 113 | }; |
| 114 | |
| 115 | const uint8_t VP_E2[] = { |
| 116 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 117 | 0xfd, 0x00, 0xff, 0x0f, // NotBefore (error) |
| 118 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 119 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 120 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 121 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 122 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 123 | }; |
| 124 | |
| 125 | const uint8_t VP_E3[] = { |
| 126 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 127 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 128 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 129 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 130 | 0xfd, 0x00, 0xfe, 0x0f, // NotAfter (error) |
| 131 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 132 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 133 | }; |
| 134 | |
| 135 | const uint8_t VP_E4[] = { |
| 136 | 0xfd, 0x00, 0xfd, 0x39, // ValidityPeriod |
| 137 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 138 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 139 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 140 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 141 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 142 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 143 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter (error) |
| 144 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 145 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 146 | }; |
| 147 | |
| 148 | const uint8_t VP_E5[] = { |
| 149 | 0xfd, 0x00, 0xfd, 0x13, // ValidityPeriod |
| 150 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 151 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 152 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 153 | }; |
| 154 | |
| 155 | const uint8_t VP_E6[] = { |
| 156 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 157 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 158 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T00000\xFF |
| 159 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFF, |
| 160 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 161 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 162 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | BOOST_AUTO_TEST_CASE(DecodingError) |
| 167 | { |
| 168 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E1, sizeof(VP_E1))), ValidityPeriod::Error); |
| 169 | |
| 170 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E2, sizeof(VP_E2))), ValidityPeriod::Error); |
| 171 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E3, sizeof(VP_E3))), ValidityPeriod::Error); |
| 172 | |
| 173 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E4, sizeof(VP_E4))), ValidityPeriod::Error); |
| 174 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E5, sizeof(VP_E5))), ValidityPeriod::Error); |
| 175 | |
| 176 | Block emptyBlock; |
| 177 | BOOST_CHECK_THROW((ValidityPeriod(emptyBlock)), ValidityPeriod::Error); |
| 178 | |
| 179 | BOOST_CHECK_THROW(ValidityPeriod(Block(VP_E6, sizeof(VP_E6))), ValidityPeriod::Error); |
| 180 | } |
| 181 | |
| 182 | BOOST_AUTO_TEST_CASE(Comparison) |
| 183 | { |
| 184 | time::system_clock::TimePoint notBefore = time::getUnixEpoch(); |
| 185 | time::system_clock::TimePoint notAfter = notBefore + time::days(1); |
| 186 | time::system_clock::TimePoint notAfter2 = notBefore + time::days(2); |
| 187 | |
| 188 | ValidityPeriod validity1(notBefore, notAfter); |
| 189 | ValidityPeriod validity2(notBefore, notAfter); |
| 190 | BOOST_CHECK(validity1 == validity2); |
| 191 | |
| 192 | ValidityPeriod validity3(notBefore, notAfter2); |
| 193 | BOOST_CHECK(validity1 != validity3); |
| 194 | } |
| 195 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 196 | BOOST_AUTO_TEST_SUITE_END() // TestValidityPeriod |
| 197 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 198 | |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 199 | } // namespace tests |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 200 | } // namespace security |
| 201 | } // namespace ndn |