Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN). |
| 6 | * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors. |
| 7 | * |
| 8 | * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhiyi Zhang <dreamerbarrychang@gmail.com> |
| 20 | */ |
| 21 | |
| 22 | #include "repetitive-interval.hpp" |
| 23 | #include "boost-test.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace gep { |
| 27 | namespace tests { |
| 28 | |
| 29 | using namespace boost::posix_time; |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestRepetitiveInterval) |
| 32 | |
| 33 | BOOST_AUTO_TEST_CASE(Construction) |
| 34 | { |
| 35 | RepetitiveInterval repetitiveInterval1(from_iso_string("20150825T000000"), |
| 36 | from_iso_string("20150825T000000"), |
| 37 | 5, 10); |
| 38 | |
| 39 | BOOST_CHECK_EQUAL(to_iso_string(repetitiveInterval1.getStartDate()), "20150825T000000"); |
| 40 | BOOST_CHECK_EQUAL(to_iso_string(repetitiveInterval1.getEndDate()), "20150825T000000"); |
| 41 | BOOST_CHECK_EQUAL(repetitiveInterval1.getIntervalStartHour(), 5); |
| 42 | BOOST_CHECK_EQUAL(repetitiveInterval1.getIntervalEndHour(), 10); |
| 43 | |
| 44 | RepetitiveInterval repetitiveInterval2(from_iso_string("20150825T000000"), |
| 45 | from_iso_string("20150827T000000"), |
| 46 | 5, 10, 1, RepetitiveInterval::RepeatUnit::DAY); |
| 47 | |
| 48 | BOOST_CHECK_EQUAL(repetitiveInterval2.getNRepeats(), 1); |
| 49 | BOOST_CHECK(repetitiveInterval2.getRepeatUnit() == RepetitiveInterval::RepeatUnit::DAY); |
| 50 | |
| 51 | RepetitiveInterval repetitiveInterval3(from_iso_string("20150825T000000"), |
| 52 | from_iso_string("20151227T000000"), |
| 53 | 5, 10, 2, RepetitiveInterval::RepeatUnit::MONTH); |
| 54 | |
| 55 | BOOST_CHECK_EQUAL(repetitiveInterval3.getNRepeats(), 2); |
| 56 | BOOST_CHECK(repetitiveInterval3.getRepeatUnit() == RepetitiveInterval::RepeatUnit::MONTH); |
| 57 | |
| 58 | RepetitiveInterval repetitiveInterval4(from_iso_string("20150825T000000"), |
| 59 | from_iso_string("20301227T000000"), |
| 60 | 5, 10, 5, RepetitiveInterval::RepeatUnit::YEAR); |
| 61 | |
| 62 | BOOST_CHECK_EQUAL(repetitiveInterval4.getNRepeats(), 5); |
| 63 | BOOST_CHECK(repetitiveInterval4.getRepeatUnit() == RepetitiveInterval::RepeatUnit::YEAR); |
| 64 | |
| 65 | RepetitiveInterval repetitiveInterval5; |
| 66 | |
| 67 | BOOST_CHECK_EQUAL(repetitiveInterval5.getNRepeats(), 0); |
| 68 | BOOST_CHECK(repetitiveInterval5.getRepeatUnit() == RepetitiveInterval::RepeatUnit::NONE); |
| 69 | } |
| 70 | |
| 71 | BOOST_AUTO_TEST_CASE(CheckCoverTimePoint) |
| 72 | { |
| 73 | ///////////////////////////////////////////// with the repeat unit DAY |
| 74 | |
| 75 | RepetitiveInterval repetitiveInterval1(from_iso_string("20150825T000000"), |
| 76 | from_iso_string("20150925T000000"), |
| 77 | 5, 10, 2, RepetitiveInterval::RepeatUnit::DAY); |
| 78 | Interval resultInterval; |
| 79 | bool isPositive = false; |
| 80 | |
| 81 | TimeStamp tp1 = from_iso_string("20150825T050000"); |
| 82 | |
| 83 | std::tie(isPositive,resultInterval) = repetitiveInterval1.getInterval(tp1); |
| 84 | BOOST_CHECK_EQUAL(isPositive, true); |
| 85 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T050000"); |
| 86 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000"); |
| 87 | |
| 88 | TimeStamp tp2 = from_iso_string("20150902T060000"); |
| 89 | |
| 90 | std::tie(isPositive,resultInterval) = repetitiveInterval1.getInterval(tp2); |
| 91 | BOOST_CHECK_EQUAL(isPositive, true); |
| 92 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150902T050000"); |
| 93 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150902T100000"); |
| 94 | |
| 95 | TimeStamp tp3 = from_iso_string("20150929T040000"); |
| 96 | |
| 97 | BOOST_CHECK(std::get<0>(repetitiveInterval1.getInterval(tp3)) == false); |
| 98 | |
| 99 | ///////////////////////////////////////////// with the repeat unit MONTH |
| 100 | |
| 101 | RepetitiveInterval repetitiveInterval2(from_iso_string("20150825T000000"), |
| 102 | from_iso_string("20160825T000000"), |
| 103 | 5, 10, 2, RepetitiveInterval::RepeatUnit::MONTH); |
| 104 | |
| 105 | TimeStamp tp4 = from_iso_string("20150825T050000"); |
| 106 | |
| 107 | std::tie(isPositive,resultInterval) = repetitiveInterval2.getInterval(tp4); |
| 108 | BOOST_CHECK_EQUAL(isPositive, true); |
| 109 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T050000"); |
| 110 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000"); |
| 111 | |
| 112 | TimeStamp tp5 = from_iso_string("20151025T060000"); |
| 113 | |
| 114 | std::tie(isPositive,resultInterval) = repetitiveInterval2.getInterval(tp5); |
| 115 | BOOST_CHECK_EQUAL(isPositive, true); |
| 116 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20151025T050000"); |
| 117 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20151025T100000"); |
| 118 | |
| 119 | TimeStamp tp6 = from_iso_string("20151226T050000"); |
| 120 | |
| 121 | BOOST_CHECK(std::get<0>(repetitiveInterval2.getInterval(tp6)) == false); |
| 122 | |
| 123 | TimeStamp tp7 = from_iso_string("20151225T040000"); |
| 124 | |
| 125 | BOOST_CHECK(std::get<0>(repetitiveInterval2.getInterval(tp7)) == false); |
| 126 | |
| 127 | ///////////////////////////////////////////// with the repeat unit YEAR |
| 128 | |
| 129 | RepetitiveInterval repetitiveInterval3(from_iso_string("20150825T000000"), |
| 130 | from_iso_string("20300825T000000"), |
| 131 | 5, 10, 3, RepetitiveInterval::RepeatUnit::YEAR); |
| 132 | |
| 133 | TimeStamp tp8 = from_iso_string("20150825T050000"); |
| 134 | |
| 135 | std::tie(isPositive,resultInterval) = repetitiveInterval3.getInterval(tp8); |
| 136 | BOOST_CHECK_EQUAL(isPositive, true); |
| 137 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T050000"); |
| 138 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000"); |
| 139 | |
| 140 | TimeStamp tp9 = from_iso_string("20180825T060000"); |
| 141 | |
| 142 | std::tie(isPositive,resultInterval) = repetitiveInterval3.getInterval(tp9); |
| 143 | BOOST_CHECK_EQUAL(isPositive, true); |
| 144 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20180825T050000"); |
| 145 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20180825T100000"); |
| 146 | |
| 147 | TimeStamp tp10 = from_iso_string("20180826T050000"); |
| 148 | BOOST_CHECK(std::get<0>(repetitiveInterval3.getInterval(tp10)) == false); |
| 149 | |
| 150 | TimeStamp tp11 = from_iso_string("20210825T040000"); |
| 151 | BOOST_CHECK(std::get<0>(repetitiveInterval3.getInterval(tp11)) == false); |
| 152 | |
| 153 | TimeStamp tp12 = from_iso_string("20300825T040000"); |
| 154 | BOOST_CHECK(std::get<0>(repetitiveInterval3.getInterval(tp12)) == false); |
| 155 | } |
| 156 | |
| 157 | const uint8_t REPETITIVE_INTERVAL[] = { |
| 158 | 0x8c, 0x2e, // RepetitiveInterval |
| 159 | 0x86, 0x0f, |
| 160 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 161 | 0x87, 0x0f, |
| 162 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x39, 0x32, 0x31, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 163 | 0x88, 0x01, |
| 164 | 0x05, |
| 165 | 0x89, 0x01, |
| 166 | 0x0a, |
| 167 | 0x8a, 0x01, |
| 168 | 0x04, |
| 169 | 0x8b, 0x01, |
| 170 | 0x01 |
| 171 | }; |
| 172 | |
| 173 | BOOST_AUTO_TEST_CASE(EncodeAndDecode) |
| 174 | { |
| 175 | RepetitiveInterval repetitiveInterval1(from_iso_string("20150825T000000"), |
| 176 | from_iso_string("20150921T000000"), |
| 177 | 5, 10, 4, RepetitiveInterval::RepeatUnit::DAY); |
| 178 | |
| 179 | Block block1 = repetitiveInterval1.wireEncode(); |
| 180 | Block block2(REPETITIVE_INTERVAL, sizeof(REPETITIVE_INTERVAL)); |
| 181 | |
| 182 | BOOST_CHECK(block1 == block2); |
| 183 | |
| 184 | RepetitiveInterval RepetitiveInterval2(block1); |
| 185 | |
| 186 | BOOST_CHECK_EQUAL(to_iso_string(RepetitiveInterval2.getStartDate()), "20150825T000000"); |
| 187 | BOOST_CHECK_EQUAL(to_iso_string(RepetitiveInterval2.getEndDate()), "20150921T000000"); |
| 188 | BOOST_CHECK_EQUAL(RepetitiveInterval2.getIntervalStartHour(), 5); |
| 189 | BOOST_CHECK_EQUAL(RepetitiveInterval2.getIntervalEndHour(), 10); |
| 190 | BOOST_CHECK_EQUAL(RepetitiveInterval2.getNRepeats(), 4); |
| 191 | BOOST_CHECK(RepetitiveInterval2.getRepeatUnit() == RepetitiveInterval::RepeatUnit::DAY); |
| 192 | |
| 193 | RepetitiveInterval repetitiveInterval3(block2); |
| 194 | |
| 195 | BOOST_CHECK_EQUAL(to_iso_string(repetitiveInterval3.getStartDate()), "20150825T000000"); |
| 196 | BOOST_CHECK_EQUAL(to_iso_string(repetitiveInterval3.getEndDate()), "20150921T000000"); |
| 197 | BOOST_CHECK_EQUAL(repetitiveInterval3.getIntervalStartHour(), 5); |
| 198 | BOOST_CHECK_EQUAL(repetitiveInterval3.getIntervalEndHour(), 10); |
| 199 | BOOST_CHECK_EQUAL(repetitiveInterval3.getNRepeats(), 4); |
| 200 | BOOST_CHECK(repetitiveInterval3.getRepeatUnit() == RepetitiveInterval::RepeatUnit::DAY); |
| 201 | } |
| 202 | |
| 203 | BOOST_AUTO_TEST_SUITE_END() |
| 204 | |
| 205 | } // namespace tests |
| 206 | } // namespace gep |
| 207 | } // namespace ndn |