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