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