Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / iotvticalendartest.cpp
1 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
2 //
3 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
18
19 //Not supported on Arduino due lack of absolute time need to implement iCalendar
20 #ifndef WITH_ARDUINO
21
22 #include "gtest/gtest.h"
23 #include "iotvticalendar.h"
24 #include "logger.h"
25
26 #define TAG  "CALENDAR-UT"
27
28 static void printPeriod(IotvtICalPeriod_t *period)
29 {
30     if(NULL == period)
31     {
32         return;
33     }
34
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);
41
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);
48 }
49
50
51 static void printRecur(IotvtICalRecur_t *recur)
52 {
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);
57
58     if(0 != (recur->byDay & SUNDAY))
59     {
60         OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Sunday");
61     }
62     if(0 != (recur->byDay & MONDAY))
63     {
64         OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Monday");
65     }
66     if(0 != (recur->byDay & TUESDAY))
67     {
68         OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Tuesday");
69     }
70     if(0 != (recur->byDay & WEDNESDAY))
71     {
72         OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Wednesday");
73     }
74     if(0 != (recur->byDay & THURSDAY))
75     {
76         OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Thursday");
77     }
78     if(0 != (recur->byDay & FRIDAY))
79     {
80         OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Friday");
81     }
82     if(0 != (recur->byDay & SATURDAY))
83     {
84         OIC_LOG_V(INFO, TAG, "recur->byDay = %s", "Saturday");
85     }
86 }
87
88
89 static void checkValidityOfRequest(char *recurStr, char *periodStr,int startTime, int endTime,
90                                     int byDay)
91 {
92     time_t rt;
93     IotvtICalDateTime_t t1;
94     IotvtICalDateTime_t t2;
95     int isValidWD;
96     IotvtICalResult_t result;
97
98     memset(&t1, 0, sizeof(t1));
99     memset(&t2, 0, sizeof(t2));
100
101     do
102     {
103         rt = time(0);
104         tzset();
105         localtime_r(&rt, &t1);
106
107         result = IsRequestWithinValidTime(periodStr, recurStr);
108
109         rt = time(0);
110         tzset();
111         localtime_r(&rt, &t2);
112
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);
116
117     if(byDay > 0)
118     {
119         isValidWD = (0x1 << t2.tm_wday) & byDay;
120         if(isValidWD && startTime <= t2.tm_hour && endTime > t2.tm_hour)
121         {
122             EXPECT_EQ(IOTVTICAL_VALID_ACCESS, result);
123         }
124         else
125         {
126             EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, result);
127         }
128     }
129     else
130     {
131         if(startTime <= t2.tm_hour && endTime > t2.tm_hour)
132         {
133             EXPECT_EQ(IOTVTICAL_VALID_ACCESS, result);
134         }
135         else
136         {
137             EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, result);
138         }
139     }
140 }
141
142 //ParsePeriod Tests
143 TEST(ParsePeriodTest, ParsePeriodValidDateTime)
144 {
145     char periodStr[] = "20150629T153050/20150630T233055";
146     IotvtICalPeriod_t period = IotvtICalPeriod_t();
147     EXPECT_EQ(IOTVTICAL_SUCCESS, ParsePeriod(periodStr,&period));
148     printPeriod(&period);
149 }
150
151 TEST(ParsePeriodTest, ParsePeriodValidDate)
152 {
153     char periodStr[] = "20150629/20150630";
154     IotvtICalPeriod_t period = IotvtICalPeriod_t();
155     EXPECT_EQ(IOTVTICAL_SUCCESS, ParsePeriod(periodStr,&period));
156     printPeriod(&period);
157 }
158
159 TEST(ParsePeriodTest, ParsePeriodMismatchStartDTEndDT1)
160 {
161     char periodStr[] = "20150629T153050/20150630";
162     IotvtICalPeriod_t period = IotvtICalPeriod_t();
163     EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
164 }
165
166 TEST(ParsePeriodTest, ParsePeriodMismatchStartDTEndDT2)
167 {
168     char periodStr[] = "20150629/20150630T203055";
169     IotvtICalPeriod_t period = IotvtICalPeriod_t();
170     EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
171 }
172
173 TEST(ParsePeriodTest, ParsePeriodInvalidStartDT1)
174 {
175     char periodStr[] = "20150629T1530/20150630T203055";
176     IotvtICalPeriod_t period = IotvtICalPeriod_t();
177     EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
178 }
179
180 TEST(ParsePeriodTest, ParsePeriodInvalidEndtDT2)
181 {
182     char periodStr[] = "20150629T153050/20150630203055";
183     IotvtICalPeriod_t period = IotvtICalPeriod_t();
184     EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
185 }
186
187 TEST(ParsePeriodTest, ParsePeriodInvalidStartD3)
188 {
189     char periodStr[] = "201506/20150630";
190     IotvtICalPeriod_t period = IotvtICalPeriod_t();
191     EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
192 }
193
194 TEST(ParsePeriodTest, ParsePeriodInvalidEndD4)
195 {
196     char periodStr[] = "20150629/201530";
197     IotvtICalPeriod_t period = IotvtICalPeriod_t();
198     EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
199 }
200
201 TEST(ParsePeriodTest, ParsePeriodEndDTBeforeStartDT)
202 {
203     char periodStr[] = "20150630T203055/20150629T153050";
204     IotvtICalPeriod_t period = IotvtICalPeriod_t();
205     EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
206 }
207 TEST(ParsePeriodTest, ParsePeriodEndDBeforeStartD)
208 {
209     char periodStr[] = "20150630/20150629";
210     IotvtICalPeriod_t period = IotvtICalPeriod_t();
211     EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
212 }
213
214 TEST(ParseRecurTest, ParseRecurValid1)
215 {
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));
219     printRecur(&recur);
220 }
221
222 TEST(ParseRecurTest, ParseRecurValid2)
223 {
224     char recurStr[] = "FREQ=DAILY";
225     IotvtICalRecur_t recur =  IotvtICalRecur_t();
226     EXPECT_EQ(IOTVTICAL_SUCCESS, ParseRecur(recurStr, &recur));
227     printRecur(&recur);
228 }
229
230 TEST(ParseRecurTest, ParseRecurInValidFreq1)
231 {
232     char recurStr[] = "FREQ=WEEKLY; UNTIL=20150703; BYDAY=TU";
233     IotvtICalRecur_t recur = IotvtICalRecur_t();
234     EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
235 }
236
237 TEST(ParseRecurTest, ParseRecurInValidFreq2)
238 {
239     char recurStr[] = "UNTIL=20150703; BYDAY=TU";
240     IotvtICalRecur_t recur =  IotvtICalRecur_t();
241     EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
242 }
243
244 TEST(ParseRecurTest, ParseRecurInValidUntil)
245 {
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));
249 }
250
251 TEST(ParseRecurTest, ParseRecurInValidByday)
252 {
253     char recurStr[] = "FREQ=DAILY; UNTIL=20150703; BYDAY=";
254     IotvtICalRecur_t recur = IotvtICalRecur_t();
255     EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
256 }
257
258 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriod1)
259 {
260     char periodStr[] = "20150630/20551230";
261     EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, NULL));
262 }
263
264 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur1)
265 {
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";
269     int startTime = 6;
270     int endTime = 20;
271     int byDay = MONDAY | WEDNESDAY | FRIDAY;
272
273     checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
274 }
275
276 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur2)
277 {
278     //Daily forever from 6:00:00am to 8:00:00pm
279     char recurStr[] = "FREQ=DAILY";
280     char periodStr[] = "20150630T060000/20150630T200000";
281     int startTime = 6;
282     int endTime = 20;
283     int byDay = -1;
284
285     checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
286 }
287
288 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur3)
289 {
290     //Daily until 20551230 from 6:00:00am to 8:00:00pm
291     char recurStr[] = "FREQ=DAILY; UNTIL=20551230";
292     char periodStr[] = "20150630T060000/20150630T200000";
293     int startTime = 6;
294     int endTime = 20;
295     int byDay = -1;
296
297     checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
298
299 }
300
301 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur4)
302 {
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";
306     int startTime = 6;
307     int endTime = 20;
308     int byDay = MONDAY | WEDNESDAY | FRIDAY;
309
310     checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
311
312  }
313
314 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeInValidPeriodAndValidRecur)
315 {
316     //Daily forever on days MO, WE & Fr
317     char recurStr[] = "FREQ=DAILY; BYDAY=MO, WE, FR";
318     char periodStr[] = "20150630/20150730";
319
320     EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
321 }
322
323 #endif