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 PCF("CALENDAR-UT")
28 static void printPeriod(IotvtICalPeriod_t *period)
30 OC_LOG_V(INFO, TAG, PCF("period->startDateTime.tm_year = %d"),period->startDateTime.tm_year);
31 OC_LOG_V(INFO, TAG, PCF("period->startDateTime.tm_mon = %d"),period->startDateTime.tm_mon);
32 OC_LOG_V(INFO, TAG, PCF("period->startDateTime.tm_mday = %d"),period->startDateTime.tm_mday);
33 OC_LOG_V(INFO, TAG, PCF("period->startDateTime.tm_hour = %d"),period->startDateTime.tm_hour);
34 OC_LOG_V(INFO, TAG, PCF("period->startDateTime.tm_min = %d"),period->startDateTime.tm_min);
35 OC_LOG_V(INFO, TAG, PCF("period->startDateTime.tm_sec = %d"),period->startDateTime.tm_sec);
37 OC_LOG_V(INFO, TAG, PCF("period->endDateTime.tm_year = %d"),period->endDateTime.tm_year);
38 OC_LOG_V(INFO, TAG, PCF("period->endDateTime.tm_mon = %d"),period->endDateTime.tm_mon);
39 OC_LOG_V(INFO, TAG, PCF("period->endDateTime.tm_mday = %d"),period->endDateTime.tm_mday);
40 OC_LOG_V(INFO, TAG, PCF("period->endDateTime.tm_hour = %d"),period->endDateTime.tm_hour);
41 OC_LOG_V(INFO, TAG, PCF("period->endDateTime.tm_min = %d"),period->endDateTime.tm_min);
42 OC_LOG_V(INFO, TAG, PCF("period->startDateTime.tm_sec = %d"),period->endDateTime.tm_sec);
45 static void printRecur(IotvtICalRecur_t *recur)
47 OC_LOG_V(INFO, TAG, PCF("recur->freq = %d"), recur->freq);
48 OC_LOG_V(INFO, TAG, PCF("recur->until.tm_year = %d"), recur->until.tm_year);
49 OC_LOG_V(INFO, TAG, PCF("recur->until.tm_mon = %d"), recur->until.tm_mon);
50 OC_LOG_V(INFO, TAG, PCF("recur->until.tm_mday = %d"), recur->until.tm_mday);
52 if(recur->byDay & SUNDAY)
54 OC_LOG_V(INFO, TAG, PCF("recur->byDay = %s"), "Sunday");
56 if(recur->byDay & MONDAY)
58 OC_LOG_V(INFO, TAG, PCF("recur->byDay = %s"), "Monday");
60 if(recur->byDay & TUESDAY)
62 OC_LOG_V(INFO, TAG, PCF("recur->byDay = %s"), "Tuesday");
64 if(recur->byDay & WEDNESDAY)
66 OC_LOG_V(INFO, TAG, PCF("recur->byDay = %s"), "Wednesday");
68 if(recur->byDay & THURSDAY)
70 OC_LOG_V(INFO, TAG, PCF("recur->byDay = %s"), "Thursday");
72 if(recur->byDay & FRIDAY)
74 OC_LOG_V(INFO, TAG, PCF("recur->byDay = %s"), "Friday");
76 if(recur->byDay & SATURDAY)
78 OC_LOG_V(INFO, TAG, PCF("recur->byDay = %s"), "Saturday");
83 TEST(ParsePeriodTest, ParsePeriodValidDateTime)
85 char periodStr[] = "20150629T153050/20150630T233055";
86 IotvtICalPeriod_t period = {};
87 EXPECT_EQ(IOTVTICAL_SUCCESS, ParsePeriod(periodStr,&period));
91 TEST(ParsePeriodTest, ParsePeriodValidDate)
93 char periodStr[] = "20150629/20150630";
94 IotvtICalPeriod_t period = {};
95 EXPECT_EQ(IOTVTICAL_SUCCESS, ParsePeriod(periodStr,&period));
99 TEST(ParsePeriodTest, ParsePeriodMismatchStartDTEndDT1)
101 char periodStr[] = "20150629T153050/20150630";
102 IotvtICalPeriod_t period = {};
103 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
106 TEST(ParsePeriodTest, ParsePeriodMismatchStartDTEndDT2)
108 char periodStr[] = "20150629/20150630T203055";
109 IotvtICalPeriod_t period = {};
110 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
113 TEST(ParsePeriodTest, ParsePeriodInvalidStartDT1)
115 char periodStr[] = "20150629T1530/20150630T203055";
116 IotvtICalPeriod_t period = {};
117 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
120 TEST(ParsePeriodTest, ParsePeriodInvalidEndtDT2)
122 char periodStr[] = "20150629T153050/20150630203055";
123 IotvtICalPeriod_t period = {};
124 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
127 TEST(ParsePeriodTest, ParsePeriodInvalidStartD3)
129 char periodStr[] = "201506/20150630";
130 IotvtICalPeriod_t period = {};
131 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
134 TEST(ParsePeriodTest, ParsePeriodInvalidEndD4)
136 char periodStr[] = "20150629/201530";
137 IotvtICalPeriod_t period = {};
138 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
141 TEST(ParsePeriodTest, ParsePeriodEndDTBeforeStartDT)
143 char periodStr[] = "20150630T203055/20150629T153050";
144 IotvtICalPeriod_t period = {};
145 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
147 TEST(ParsePeriodTest, ParsePeriodEndDBeforeStartD)
149 char periodStr[] = "20150630/20150629";
150 IotvtICalPeriod_t period = {};
151 EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
154 TEST(ParseRecurTest, ParseRecurValid1)
156 char recurStr[] = "FREQ=DAILY; UNTIL=20150703; BYDAY=MO, WE, FR";
157 IotvtICalRecur_t recur = {};
158 EXPECT_EQ(IOTVTICAL_SUCCESS, ParseRecur(recurStr, &recur));
162 TEST(ParseRecurTest, ParseRecurValid2)
164 char recurStr[] = "FREQ=DAILY";
165 IotvtICalRecur_t recur = {};
166 EXPECT_EQ(IOTVTICAL_SUCCESS, ParseRecur(recurStr, &recur));
170 TEST(ParseRecurTest, ParseRecurInValidFreq1)
172 char recurStr[] = "FREQ=WEEKLY; UNTIL=20150703; BYDAY=TU";
173 IotvtICalRecur_t recur = {};
174 EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
177 TEST(ParseRecurTest, ParseRecurInValidFreq2)
179 char recurStr[] = "UNTIL=20150703; BYDAY=TU";
180 IotvtICalRecur_t recur = {};
181 EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
184 TEST(ParseRecurTest, ParseRecurInValidUntil)
186 char recurStr[] = "FREQ=DAILY; UNTIL=20150703T095055; BYDAY=MO, WE, FR";
187 IotvtICalRecur_t recur = {};
188 EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
191 TEST(ParseRecurTest, ParseRecurInValidByday)
193 char recurStr[] = "FREQ=DAILY; UNTIL=20150703; BYDAY=";
194 IotvtICalRecur_t recur = {};
195 EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
198 //FIXME: ALL IsRequestWithinValidTime tests will fail after 20151230
199 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriod1)
201 char periodStr[] = "20150630/20151230";
202 EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, NULL));
205 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur1)
207 //Daily on days MO, WE & FR from 6:00:00am to 8:00:00pm until 20151230
208 char recurStr[] = "FREQ=DAILY; UNTIL=20151230; BYDAY=MO, WE, FR";
209 char periodStr[] = "20150630T060000/20150630T200000";
212 IotvtICalDateTime_t *ct = localtime(&rt);
214 int byDay = MONDAY | WEDNESDAY | FRIDAY;
215 int isValidWD = (0x1 << ct->tm_wday) & byDay;
217 if(isValidWD && 6 <= ct->tm_hour && 20>= ct->tm_hour)
219 EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
223 EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
227 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur2)
229 //Daily forever from 6:00:00am to 8:00:00pm
230 char recurStr[] = "FREQ=DAILY";
231 char periodStr[] = "20150630T060000/20150630T200000";
234 IotvtICalDateTime_t *ct = localtime(&rt);
236 if(6 <= ct->tm_hour && 20>= ct->tm_hour)
238 EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
242 EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
246 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur3)
248 //Daily until 20151230 from 6:00:00am to 8:00:00pm
249 char recurStr[] = "FREQ=DAILY; UNTIL=20151230";
250 char periodStr[] = "20150630T060000/20150630T200000";
253 IotvtICalDateTime_t *ct = localtime(&rt);
255 if(6 <= ct->tm_hour && 20>= ct->tm_hour)
257 EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
261 EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
265 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur4)
267 //Daily forever on days MO, WE & Fr from 6:00:00am to 8:00:00pm
268 char recurStr[] = "FREQ=DAILY; BYDAY=MO, WE, FR";
269 char periodStr[] = "20150630T060000/20150630T200000";
272 IotvtICalDateTime_t *ct = localtime(&rt);
274 int byDay = MONDAY | WEDNESDAY | FRIDAY;
275 int isValidWD = (0x1 << ct->tm_wday) & byDay;
277 if(isValidWD && 6 <= ct->tm_hour && 20>= ct->tm_hour)
279 EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
283 EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
287 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeInValidPeriodAndValidRecur)
289 //Daily forever on days MO, WE & Fr
290 char recurStr[] = "FREQ=DAILY; BYDAY=MO, WE, FR";
291 char periodStr[] = "20150630/20150730";
293 EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));