1 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
3 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
19 //Not supported on Arduino due lack of absolute time need to implement iCalendar
22 #include "gtest/gtest.h"
23 #include "iotvticalendar.h"
26 #define TAG "CALENDAR-UT"
28 static void printPeriod(IotvtICalPeriod_t *period)
35 OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_year = %d",period->startDateTime.tm_year);
36 OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_mon = %d",period->startDateTime.tm_mon);
37 OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_mday = %d",period->startDateTime.tm_mday);
38 OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_hour = %d",period->startDateTime.tm_hour);
39 OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_min = %d",period->startDateTime.tm_min);
40 OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_sec = %d",period->startDateTime.tm_sec);
42 OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_year = %d",period->endDateTime.tm_year);
43 OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_mon = %d",period->endDateTime.tm_mon);
44 OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_mday = %d",period->endDateTime.tm_mday);
45 OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_hour = %d",period->endDateTime.tm_hour);
46 OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_min = %d",period->endDateTime.tm_min);
47 OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_sec = %d",period->endDateTime.tm_sec);
51 static void printRecur(IotvtICalRecur_t *recur)
53 OIC_LOG_V(INFO, TAG, "recur->freq = %d", recur->freq);
54 OIC_LOG_V(INFO, TAG, "recur->until.tm_year = %d", recur->until.tm_year);
55 OIC_LOG_V(INFO, TAG, "recur->until.tm_mon = %d", recur->until.tm_mon);
56 OIC_LOG_V(INFO, TAG, "recur->until.tm_mday = %d", recur->until.tm_mday);
58 if(recur->byDay & SUNDAY)
60 OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Sunday");
62 if(recur->byDay & MONDAY)
64 OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Monday");
66 if(recur->byDay & TUESDAY)
68 OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Tuesday");
70 if(recur->byDay & WEDNESDAY)
72 OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Wednesday");
74 if(recur->byDay & THURSDAY)
76 OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Thursday");
78 if(recur->byDay & FRIDAY)
80 OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Friday");
82 if(recur->byDay & SATURDAY)
84 OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Saturday");
89 static void checkValidityOfRequest(char *recurStr, char *periodStr,int startTime, int endTime,
93 IotvtICalDateTime_t t1;
94 IotvtICalDateTime_t t2;
96 IotvtICalResult_t result;
98 memset(&t1, 0, sizeof(t1));
99 memset(&t2, 0, sizeof(t2));
105 localtime_r(&rt, &t1);
107 result = IsRequestWithinValidTime(periodStr, recurStr);
111 localtime_r(&rt, &t2);
113 OIC_LOG_V(INFO, TAG, "t1 = %02d:%02d:%02d", t1.tm_hour, t1.tm_min, t1.tm_sec );
114 OIC_LOG_V(INFO, TAG, "t2 = %02d:%02d:%02d", t2.tm_hour, t2.tm_min, t2.tm_sec );
115 }while(t1.tm_hour != t2.tm_hour);
119 isValidWD = (0x1 << t2.tm_wday) & byDay;
120 if(isValidWD && startTime <= t2.tm_hour && endTime > t2.tm_hour)
122 EXPECT_EQ(IOTVTICAL_VALID_ACCESS, result);
126 EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, result);
131 if(startTime <= t2.tm_hour && endTime > t2.tm_hour)
133 EXPECT_EQ(IOTVTICAL_VALID_ACCESS, result);
137 EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, result);
143 TEST(ParsePeriodTest, ParsePeriodValidDateTime)
145 char periodStr[] = "20150629T153050/20150630T233055";
146 IotvtICalPeriod_t period = IotvtICalPeriod_t();
147 EXPECT_EQ(IOTVTICAL_SUCCESS, ParsePeriod(periodStr,&period));
148 printPeriod(&period);
151 TEST(ParsePeriodTest, ParsePeriodValidDate)
153 char periodStr[] = "20150629/20150630";
154 IotvtICalPeriod_t period = IotvtICalPeriod_t();
155 EXPECT_EQ(IOTVTICAL_SUCCESS, ParsePeriod(periodStr,&period));
156 printPeriod(&period);
159 TEST(ParsePeriodTest, ParsePeriodMismatchStartDTEndDT1)
161 char periodStr[] = "20150629T153050/20150630";
162 IotvtICalPeriod_t period = IotvtICalPeriod_t();
163 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
166 TEST(ParsePeriodTest, ParsePeriodMismatchStartDTEndDT2)
168 char periodStr[] = "20150629/20150630T203055";
169 IotvtICalPeriod_t period = IotvtICalPeriod_t();
170 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
173 TEST(ParsePeriodTest, ParsePeriodInvalidStartDT1)
175 char periodStr[] = "20150629T1530/20150630T203055";
176 IotvtICalPeriod_t period = IotvtICalPeriod_t();
177 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
180 TEST(ParsePeriodTest, ParsePeriodInvalidEndtDT2)
182 char periodStr[] = "20150629T153050/20150630203055";
183 IotvtICalPeriod_t period = IotvtICalPeriod_t();
184 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
187 TEST(ParsePeriodTest, ParsePeriodInvalidStartD3)
189 char periodStr[] = "201506/20150630";
190 IotvtICalPeriod_t period = IotvtICalPeriod_t();
191 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
194 TEST(ParsePeriodTest, ParsePeriodInvalidEndD4)
196 char periodStr[] = "20150629/201530";
197 IotvtICalPeriod_t period = IotvtICalPeriod_t();
198 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
201 TEST(ParsePeriodTest, ParsePeriodEndDTBeforeStartDT)
203 char periodStr[] = "20150630T203055/20150629T153050";
204 IotvtICalPeriod_t period = IotvtICalPeriod_t();
205 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
207 TEST(ParsePeriodTest, ParsePeriodEndDBeforeStartD)
209 char periodStr[] = "20150630/20150629";
210 IotvtICalPeriod_t period = IotvtICalPeriod_t();
211 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
214 TEST(ParseRecurTest, ParseRecurValid1)
216 char recurStr[] = "FREQ=DAILY; UNTIL=20150703; BYDAY=MO, WE, FR";
217 IotvtICalRecur_t recur = IotvtICalRecur_t();
218 EXPECT_EQ(IOTVTICAL_SUCCESS, ParseRecur(recurStr, &recur));
222 TEST(ParseRecurTest, ParseRecurValid2)
224 char recurStr[] = "FREQ=DAILY";
225 IotvtICalRecur_t recur = IotvtICalRecur_t();
226 EXPECT_EQ(IOTVTICAL_SUCCESS, ParseRecur(recurStr, &recur));
230 TEST(ParseRecurTest, ParseRecurInValidFreq1)
232 char recurStr[] = "FREQ=WEEKLY; UNTIL=20150703; BYDAY=TU";
233 IotvtICalRecur_t recur = IotvtICalRecur_t();
234 EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
237 TEST(ParseRecurTest, ParseRecurInValidFreq2)
239 char recurStr[] = "UNTIL=20150703; BYDAY=TU";
240 IotvtICalRecur_t recur = IotvtICalRecur_t();
241 EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
244 TEST(ParseRecurTest, ParseRecurInValidUntil)
246 char recurStr[] = "FREQ=DAILY; UNTIL=20150703T095055; BYDAY=MO, WE, FR";
247 IotvtICalRecur_t recur = IotvtICalRecur_t();
248 EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
251 TEST(ParseRecurTest, ParseRecurInValidByday)
253 char recurStr[] = "FREQ=DAILY; UNTIL=20150703; BYDAY=";
254 IotvtICalRecur_t recur = IotvtICalRecur_t();
255 EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
258 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriod1)
260 char periodStr[] = "20150630/20551230";
261 EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, NULL));
264 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur1)
266 //Daily on days MO, WE & FR from 6:00:00am to 8:00:00pm until 20551230
267 char recurStr[] = "FREQ=DAILY; UNTIL=20551230; BYDAY=MO, WE, FR";
268 char periodStr[] = "20150630T060000/20150630T200000";
271 int byDay = MONDAY | WEDNESDAY | FRIDAY;
273 checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
276 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur2)
278 //Daily forever from 6:00:00am to 8:00:00pm
279 char recurStr[] = "FREQ=DAILY";
280 char periodStr[] = "20150630T060000/20150630T200000";
285 checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
288 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur3)
290 //Daily until 20551230 from 6:00:00am to 8:00:00pm
291 char recurStr[] = "FREQ=DAILY; UNTIL=20551230";
292 char periodStr[] = "20150630T060000/20150630T200000";
297 checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
301 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur4)
303 //Daily forever on days MO, WE & Fr from 6:00:00am to 8:00:00pm
304 char recurStr[] = "FREQ=DAILY; BYDAY=MO, WE, FR";
305 char periodStr[] = "20150630T060000/20150630T200000";
308 int byDay = MONDAY | WEDNESDAY | FRIDAY;
310 checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
314 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeInValidPeriodAndValidRecur)
316 //Daily forever on days MO, WE & Fr
317 char recurStr[] = "FREQ=DAILY; BYDAY=MO, WE, FR";
318 char periodStr[] = "20150630/20150730";
320 EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));