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 "schedule.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(TestSchedule) |
| 32 | |
| 33 | BOOST_AUTO_TEST_CASE(CalculateCoveringInterval) |
| 34 | { |
| 35 | Schedule schedule; |
| 36 | |
| 37 | RepetitiveInterval interval1(from_iso_string("20150825T000000"), |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 38 | from_iso_string("20150827T000000"), |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 39 | 5, 10, 2, RepetitiveInterval::RepeatUnit::DAY); |
| 40 | RepetitiveInterval interval2(from_iso_string("20150825T000000"), |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 41 | from_iso_string("20150827T000000"), |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 42 | 6, 8, 1, RepetitiveInterval::RepeatUnit::DAY); |
| 43 | RepetitiveInterval interval3(from_iso_string("20150827T000000"), |
| 44 | from_iso_string("20150827T000000"), |
| 45 | 7, 8); |
| 46 | RepetitiveInterval interval4(from_iso_string("20150825T000000"), |
| 47 | from_iso_string("20150825T000000"), |
| 48 | 4, 7); |
| 49 | |
| 50 | schedule.addWhiteInterval(interval1); |
| 51 | schedule.addWhiteInterval(interval2); |
| 52 | schedule.addWhiteInterval(interval4); |
| 53 | schedule.addBlackInterval(interval3); |
| 54 | |
| 55 | Interval resultInterval; |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 56 | bool isPositive; |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 57 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 58 | // tp1 --> positive 8.25 4-10 |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 59 | TimeStamp tp1 = from_iso_string("20150825T063000"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 60 | std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp1); |
| 61 | BOOST_CHECK_EQUAL(isPositive, true); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T040000"); |
| 63 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000"); |
| 64 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 65 | // tp2 --> positive 8.26 6-8 |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 66 | TimeStamp tp2 = from_iso_string("20150826T073000"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 67 | std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp2); |
| 68 | BOOST_CHECK_EQUAL(isPositive, true); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150826T060000"); |
| 70 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150826T080000"); |
| 71 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 72 | // tp3 --> positive 8.27 5-7 |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 73 | TimeStamp tp3 = from_iso_string("20150827T053000"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 74 | std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp3); |
| 75 | BOOST_CHECK_EQUAL(isPositive, true); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150827T050000"); |
| 77 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150827T070000"); |
| 78 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 79 | // tp4 --> positive 8.27 5-7 |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 80 | TimeStamp tp4 = from_iso_string("20150827T063000"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 81 | std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp4); |
| 82 | BOOST_CHECK_EQUAL(isPositive, true); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150827T050000"); |
| 84 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150827T070000"); |
| 85 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 86 | // tp5 --> negative 8.27 7-8 |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 87 | TimeStamp tp5 = from_iso_string("20150827T073000"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 88 | std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp5); |
| 89 | BOOST_CHECK_EQUAL(isPositive, false); |
| 90 | BOOST_CHECK_EQUAL(resultInterval.isEmpty(), false); |
| 91 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150827T070000"); |
| 92 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150827T080000"); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 93 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 94 | // tp6 --> negative 8.25 10-24 |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 95 | TimeStamp tp6 = from_iso_string("20150825T113000"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 96 | std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp6); |
| 97 | BOOST_CHECK_EQUAL(isPositive, false); |
| 98 | BOOST_CHECK_EQUAL(resultInterval.isEmpty(), false); |
| 99 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T100000"); |
| 100 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150826T000000"); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | const uint8_t SCHEDULE[] = { |
| 104 | 0x8f, 0xc4,// Schedule |
| 105 | 0x8d, 0x90,// WhiteIntervalList |
| 106 | ///// |
| 107 | 0x8c, 0x2e, // RepetitiveInterval |
| 108 | 0x86, 0x0f, |
| 109 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 110 | 0x87, 0x0f, |
| 111 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x38, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 112 | 0x88, 0x01, |
| 113 | 0x05, |
| 114 | 0x89, 0x01, |
| 115 | 0x0a, |
| 116 | 0x8a, 0x01, |
| 117 | 0x02, |
| 118 | 0x8b, 0x01, |
| 119 | 0x01, |
| 120 | ///// |
| 121 | 0x8c, 0x2e, // RepetitiveInterval |
| 122 | 0x86, 0x0f, |
| 123 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 124 | 0x87, 0x0f, |
| 125 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x38, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 126 | 0x88, 0x01, |
| 127 | 0x06, |
| 128 | 0x89, 0x01, |
| 129 | 0x08, |
| 130 | 0x8a, 0x01, |
| 131 | 0x01, |
| 132 | 0x8b, 0x01, |
| 133 | 0x01, |
| 134 | ///// |
| 135 | 0x8c, 0x2e, // RepetitiveInterval |
| 136 | 0x86, 0x0f, |
| 137 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 138 | 0x87, 0x0f, |
| 139 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 140 | 0x88, 0x01, |
| 141 | 0x04, |
| 142 | 0x89, 0x01, |
| 143 | 0x07, |
| 144 | 0x8a, 0x01, |
| 145 | 0x00, |
| 146 | 0x8b, 0x01, |
| 147 | 0x00, |
| 148 | ///// |
| 149 | 0x8e, 0x30, // BlackIntervalList |
| 150 | ///// |
| 151 | 0x8c, 0x2e, // RepetitiveInterval |
| 152 | 0x86, 0x0f, |
| 153 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x37, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 154 | 0x87, 0x0f, |
| 155 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x37, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 156 | 0x88, 0x01, |
| 157 | 0x07, |
| 158 | 0x89, 0x01, |
| 159 | 0x08, |
| 160 | 0x8a, 0x01, |
| 161 | 0x00, |
| 162 | 0x8b, 0x01, |
| 163 | 0x00 |
| 164 | }; |
| 165 | |
| 166 | BOOST_AUTO_TEST_CASE(EncodeAndDecode) |
| 167 | { |
| 168 | Schedule schedule; |
| 169 | |
| 170 | RepetitiveInterval interval1(from_iso_string("20150825T000000"), |
| 171 | from_iso_string("20150828T000000"), |
| 172 | 5, 10, 2, RepetitiveInterval::RepeatUnit::DAY); |
| 173 | RepetitiveInterval interval2(from_iso_string("20150825T000000"), |
| 174 | from_iso_string("20150828T000000"), |
| 175 | 6, 8, 1, RepetitiveInterval::RepeatUnit::DAY); |
| 176 | RepetitiveInterval interval3(from_iso_string("20150827T000000"), |
| 177 | from_iso_string("20150827T000000"), |
| 178 | 7, 8); |
| 179 | RepetitiveInterval interval4(from_iso_string("20150825T000000"), |
| 180 | from_iso_string("20150825T000000"), |
| 181 | 4, 7); |
| 182 | |
| 183 | schedule.addWhiteInterval(interval1); |
| 184 | schedule.addWhiteInterval(interval2); |
| 185 | schedule.addWhiteInterval(interval4); |
| 186 | schedule.addBlackInterval(interval3); |
| 187 | |
| 188 | Block block = schedule.wireEncode(); |
| 189 | Block block2(SCHEDULE, sizeof(SCHEDULE)); |
| 190 | BOOST_CHECK(block == block2); |
| 191 | |
| 192 | Schedule schedule2(block); |
| 193 | Interval resultInterval; |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 194 | bool isPositive; |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 195 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 196 | // tp1 --> positive 8.25 4-10 |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 197 | TimeStamp tp1 = from_iso_string("20150825T063000"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 198 | std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp1); |
| 199 | BOOST_CHECK_EQUAL(isPositive, true); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T040000"); |
| 201 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000"); |
| 202 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 203 | // tp2 --> positive 8.26 6-8 |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 204 | TimeStamp tp2 = from_iso_string("20150826T073000"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 205 | std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp2); |
| 206 | BOOST_CHECK_EQUAL(isPositive, true); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150826T060000"); |
| 208 | BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150826T080000"); |
| 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_SUITE_END() |
| 212 | |
| 213 | } // namespace tests |
| 214 | } // namespace gep |
| 215 | } // namespace ndn |