blob: 22f2a104f364cce7ea24921e2882132c16b4dd66 [file] [log] [blame]
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07001/* -*- 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
25namespace ndn {
26namespace gep {
27namespace tests {
28
29using namespace boost::posix_time;
30
31BOOST_AUTO_TEST_SUITE(TestSchedule)
32
33BOOST_AUTO_TEST_CASE(CalculateCoveringInterval)
34{
35 Schedule schedule;
36
37 RepetitiveInterval interval1(from_iso_string("20150825T000000"),
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070038 from_iso_string("20150827T000000"),
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070039 5, 10, 2, RepetitiveInterval::RepeatUnit::DAY);
40 RepetitiveInterval interval2(from_iso_string("20150825T000000"),
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070041 from_iso_string("20150827T000000"),
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070042 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 Zhang7cc09fc2015-09-01 13:40:32 -070056 bool isPositive;
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070057
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070058 // tp1 --> positive 8.25 4-10
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070059 TimeStamp tp1 = from_iso_string("20150825T063000");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070060 std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp1);
61 BOOST_CHECK_EQUAL(isPositive, true);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070062 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T040000");
63 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000");
64
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070065 // tp2 --> positive 8.26 6-8
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070066 TimeStamp tp2 = from_iso_string("20150826T073000");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070067 std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp2);
68 BOOST_CHECK_EQUAL(isPositive, true);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070069 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150826T060000");
70 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150826T080000");
71
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070072 // tp3 --> positive 8.27 5-7
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070073 TimeStamp tp3 = from_iso_string("20150827T053000");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070074 std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp3);
75 BOOST_CHECK_EQUAL(isPositive, true);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070076 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150827T050000");
77 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150827T070000");
78
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070079 // tp4 --> positive 8.27 5-7
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070080 TimeStamp tp4 = from_iso_string("20150827T063000");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070081 std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp4);
82 BOOST_CHECK_EQUAL(isPositive, true);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070083 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150827T050000");
84 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150827T070000");
85
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070086 // tp5 --> negative 8.27 7-8
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070087 TimeStamp tp5 = from_iso_string("20150827T073000");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070088 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 Zhangcea58d52015-08-26 10:19:56 -070093
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070094 // tp6 --> negative 8.25 10-24
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070095 TimeStamp tp6 = from_iso_string("20150825T113000");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070096 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 Zhangcea58d52015-08-26 10:19:56 -0700101}
102
103const 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
166BOOST_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 Zhang7cc09fc2015-09-01 13:40:32 -0700194 bool isPositive;
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700195
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700196 // tp1 --> positive 8.25 4-10
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700197 TimeStamp tp1 = from_iso_string("20150825T063000");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700198 std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp1);
199 BOOST_CHECK_EQUAL(isPositive, true);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700200 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T040000");
201 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000");
202
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700203 // tp2 --> positive 8.26 6-8
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700204 TimeStamp tp2 = from_iso_string("20150826T073000");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700205 std::tie(isPositive, resultInterval) = schedule.getCoveringInterval(tp2);
206 BOOST_CHECK_EQUAL(isPositive, true);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700207 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150826T060000");
208 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150826T080000");
209}
210
211BOOST_AUTO_TEST_SUITE_END()
212
213} // namespace tests
214} // namespace gep
215} // namespace ndn