iotvticalendartest.cpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

//Not supported on Arduino due lack of absolute time need to implement iCalendar
#ifndef WITH_ARDUINO

22
#include <gtest/gtest.h>
23 24 25
#include "iotvticalendar.h"
#include "logger.h"

26
#define TAG  "CALENDAR-UT"
27 28 29

static void printPeriod(IotvtICalPeriod_t *period)
{
Shilpa Sodani's avatar
Shilpa Sodani committed
30 31 32 33 34
    if(NULL == period)
    {
        return;
    }

35 36 37 38 39 40 41 42 43 44 45 46 47
    OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_year = %d",period->startDateTime.tm_year);
    OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_mon = %d",period->startDateTime.tm_mon);
    OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_mday = %d",period->startDateTime.tm_mday);
    OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_hour = %d",period->startDateTime.tm_hour);
    OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_min = %d",period->startDateTime.tm_min);
    OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_sec = %d",period->startDateTime.tm_sec);

    OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_year = %d",period->endDateTime.tm_year);
    OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_mon = %d",period->endDateTime.tm_mon);
    OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_mday = %d",period->endDateTime.tm_mday);
    OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_hour = %d",period->endDateTime.tm_hour);
    OIC_LOG_V(INFO, TAG, "period->endDateTime.tm_min = %d",period->endDateTime.tm_min);
    OIC_LOG_V(INFO, TAG, "period->startDateTime.tm_sec = %d",period->endDateTime.tm_sec);
48 49
}

Shilpa Sodani's avatar
Shilpa Sodani committed
50

51 52
static void printRecur(IotvtICalRecur_t *recur)
{
53 54 55 56
    OIC_LOG_V(INFO, TAG, "recur->freq = %d", recur->freq);
    OIC_LOG_V(INFO, TAG, "recur->until.tm_year = %d", recur->until.tm_year);
    OIC_LOG_V(INFO, TAG, "recur->until.tm_mon = %d", recur->until.tm_mon);
    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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#ifndef HAVE_LOCALTIME_R

#define localtime_r localtime_r_compat

/**
 * @description An implementation of `localtime_r` for systems which only
 * have a `localtime` implementation.
 */
static tm* localtime_r_compat(const time_t* timer, tm* result)
{
    if (NULL == result)
    {
        OIC_LOG(ERROR, TAG, "localtime_r received null results parameter");
    }

    if (NULL == timer)
    {
        OIC_LOG(WARNING, TAG, "localtime_r received null timer parameter");
    }

    tm* tempPtr = localtime(timer);
    memcpy(result, tempPtr, sizeof(tm));
    return result;
}
#endif
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

static void checkValidityOfRequest(char *recurStr, char *periodStr,int startTime, int endTime,
                                    int byDay)
{
    time_t rt;
    IotvtICalDateTime_t t1;
    IotvtICalDateTime_t t2;
    int isValidWD;
    IotvtICalResult_t result;

    memset(&t1, 0, sizeof(t1));
    memset(&t2, 0, sizeof(t2));

    do
    {
        rt = time(0);
        tzset();
        localtime_r(&rt, &t1);

        result = IsRequestWithinValidTime(periodStr, recurStr);

        rt = time(0);
        tzset();
        localtime_r(&rt, &t2);

138 139
        OIC_LOG_V(INFO, TAG, "t1 = %02d:%02d:%02d", t1.tm_hour, t1.tm_min, t1.tm_sec );
        OIC_LOG_V(INFO, TAG, "t2 = %02d:%02d:%02d", t2.tm_hour, t2.tm_min, t2.tm_sec );
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    }while(t1.tm_hour != t2.tm_hour);

    if(byDay > 0)
    {
        isValidWD = (0x1 << t2.tm_wday) & byDay;
        if(isValidWD && startTime <= t2.tm_hour && endTime > t2.tm_hour)
        {
            EXPECT_EQ(IOTVTICAL_VALID_ACCESS, result);
        }
        else
        {
            EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, result);
        }
    }
    else
    {
        if(startTime <= t2.tm_hour && endTime > t2.tm_hour)
        {
            EXPECT_EQ(IOTVTICAL_VALID_ACCESS, result);
        }
        else
        {
            EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, result);
        }
    }
}

167 168 169 170
//ParsePeriod Tests
TEST(ParsePeriodTest, ParsePeriodValidDateTime)
{
    char periodStr[] = "20150629T153050/20150630T233055";
Shilpa Sodani's avatar
Shilpa Sodani committed
171
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
172 173 174 175 176 177 178
    EXPECT_EQ(IOTVTICAL_SUCCESS, ParsePeriod(periodStr,&period));
    printPeriod(&period);
}

TEST(ParsePeriodTest, ParsePeriodValidDate)
{
    char periodStr[] = "20150629/20150630";
Shilpa Sodani's avatar
Shilpa Sodani committed
179
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
180 181 182 183 184 185 186
    EXPECT_EQ(IOTVTICAL_SUCCESS, ParsePeriod(periodStr,&period));
    printPeriod(&period);
}

TEST(ParsePeriodTest, ParsePeriodMismatchStartDTEndDT1)
{
    char periodStr[] = "20150629T153050/20150630";
Shilpa Sodani's avatar
Shilpa Sodani committed
187
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
188 189 190 191 192 193
    EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
}

TEST(ParsePeriodTest, ParsePeriodMismatchStartDTEndDT2)
{
    char periodStr[] = "20150629/20150630T203055";
Shilpa Sodani's avatar
Shilpa Sodani committed
194
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
195 196 197 198 199 200
    EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
}

TEST(ParsePeriodTest, ParsePeriodInvalidStartDT1)
{
    char periodStr[] = "20150629T1530/20150630T203055";
Shilpa Sodani's avatar
Shilpa Sodani committed
201
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
202 203 204 205 206 207
    EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
}

TEST(ParsePeriodTest, ParsePeriodInvalidEndtDT2)
{
    char periodStr[] = "20150629T153050/20150630203055";
Shilpa Sodani's avatar
Shilpa Sodani committed
208
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
209 210 211 212 213 214
    EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
}

TEST(ParsePeriodTest, ParsePeriodInvalidStartD3)
{
    char periodStr[] = "201506/20150630";
Shilpa Sodani's avatar
Shilpa Sodani committed
215
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
216 217 218 219 220 221
    EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
}

TEST(ParsePeriodTest, ParsePeriodInvalidEndD4)
{
    char periodStr[] = "20150629/201530";
Shilpa Sodani's avatar
Shilpa Sodani committed
222
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
223 224 225 226 227 228
    EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
}

TEST(ParsePeriodTest, ParsePeriodEndDTBeforeStartDT)
{
    char periodStr[] = "20150630T203055/20150629T153050";
Shilpa Sodani's avatar
Shilpa Sodani committed
229
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
230 231 232 233 234
    EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
}
TEST(ParsePeriodTest, ParsePeriodEndDBeforeStartD)
{
    char periodStr[] = "20150630/20150629";
Shilpa Sodani's avatar
Shilpa Sodani committed
235
    IotvtICalPeriod_t period = IotvtICalPeriod_t();
236 237 238 239 240 241
    EXPECT_EQ(IOTVTICAL_INVALID_PERIOD, ParsePeriod(periodStr,&period));
}

TEST(ParseRecurTest, ParseRecurValid1)
{
    char recurStr[] = "FREQ=DAILY; UNTIL=20150703; BYDAY=MO, WE, FR";
Shilpa Sodani's avatar
Shilpa Sodani committed
242
    IotvtICalRecur_t recur = IotvtICalRecur_t();
243 244 245 246 247 248 249
    EXPECT_EQ(IOTVTICAL_SUCCESS, ParseRecur(recurStr, &recur));
    printRecur(&recur);
}

TEST(ParseRecurTest, ParseRecurValid2)
{
    char recurStr[] = "FREQ=DAILY";
Shilpa Sodani's avatar
Shilpa Sodani committed
250
    IotvtICalRecur_t recur =  IotvtICalRecur_t();
251 252 253 254 255 256 257
    EXPECT_EQ(IOTVTICAL_SUCCESS, ParseRecur(recurStr, &recur));
    printRecur(&recur);
}

TEST(ParseRecurTest, ParseRecurInValidFreq1)
{
    char recurStr[] = "FREQ=WEEKLY; UNTIL=20150703; BYDAY=TU";
Shilpa Sodani's avatar
Shilpa Sodani committed
258
    IotvtICalRecur_t recur = IotvtICalRecur_t();
259 260 261 262 263 264
    EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
}

TEST(ParseRecurTest, ParseRecurInValidFreq2)
{
    char recurStr[] = "UNTIL=20150703; BYDAY=TU";
Shilpa Sodani's avatar
Shilpa Sodani committed
265
    IotvtICalRecur_t recur =  IotvtICalRecur_t();
266 267 268 269 270 271
    EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
}

TEST(ParseRecurTest, ParseRecurInValidUntil)
{
    char recurStr[] = "FREQ=DAILY; UNTIL=20150703T095055; BYDAY=MO, WE, FR";
Shilpa Sodani's avatar
Shilpa Sodani committed
272
    IotvtICalRecur_t recur = IotvtICalRecur_t();
273 274 275 276 277 278
    EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
}

TEST(ParseRecurTest, ParseRecurInValidByday)
{
    char recurStr[] = "FREQ=DAILY; UNTIL=20150703; BYDAY=";
Shilpa Sodani's avatar
Shilpa Sodani committed
279
    IotvtICalRecur_t recur = IotvtICalRecur_t();
280 281 282 283 284
    EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
}

TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriod1)
{
285
    char periodStr[] = "20150630/20551230";
286 287 288 289 290
    EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, NULL));
}

TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur1)
{
291 292
    //Daily on days MO, WE & FR from 6:00:00am to 8:00:00pm until 20551230
    char recurStr[] = "FREQ=DAILY; UNTIL=20551230; BYDAY=MO, WE, FR";
293
    char periodStr[] = "20150630T060000/20150630T200000";
294 295
    int startTime = 6;
    int endTime = 20;
296 297
    int byDay = MONDAY | WEDNESDAY | FRIDAY;

298
    checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
299 300 301 302 303 304 305
}

TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur2)
{
    //Daily forever from 6:00:00am to 8:00:00pm
    char recurStr[] = "FREQ=DAILY";
    char periodStr[] = "20150630T060000/20150630T200000";
306 307 308
    int startTime = 6;
    int endTime = 20;
    int byDay = -1;
309

310
    checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
311 312 313 314
}

TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur3)
{
315 316
    //Daily until 20551230 from 6:00:00am to 8:00:00pm
    char recurStr[] = "FREQ=DAILY; UNTIL=20551230";
317
    char periodStr[] = "20150630T060000/20150630T200000";
318 319 320
    int startTime = 6;
    int endTime = 20;
    int byDay = -1;
321

322
    checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
323 324 325 326 327 328 329 330

}

TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur4)
{
    //Daily forever on days MO, WE & Fr from 6:00:00am to 8:00:00pm
    char recurStr[] = "FREQ=DAILY; BYDAY=MO, WE, FR";
    char periodStr[] = "20150630T060000/20150630T200000";
331 332
    int startTime = 6;
    int endTime = 20;
333 334
    int byDay = MONDAY | WEDNESDAY | FRIDAY;

335 336 337
    checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);

 }
338 339 340 341 342 343 344 345 346 347 348

TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeInValidPeriodAndValidRecur)
{
    //Daily forever on days MO, WE & Fr
    char recurStr[] = "FREQ=DAILY; BYDAY=MO, WE, FR";
    char periodStr[] = "20150630/20150730";

    EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
}

#endif