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