From 18a2ace5c4d2453607056496c31230931bbf4b63 Mon Sep 17 00:00:00 2001 From: Shilpa Sodani Date: Wed, 14 Oct 2015 13:37:21 -0700 Subject: [PATCH] Fixed bug IOT-798 IsRequestWithinValidTime in iotvticalendar.c was only checking for the valid date when recurrence was NULL. Update the IsRequestWithinValidTime function to check for both valid date and time. Change-Id: Ifec7520f07958fd7ded36c017c0b08acd3053ff0 Signed-off-by: Shilpa Sodani Reviewed-on: https://gerrit.iotivity.org/gerrit/3881 Reviewed-by: Mark Tung Tested-by: jenkins-iotivity Reviewed-by: Sachin Agrawal --- resource/csdk/security/src/iotvticalendar.c | 61 +++++++++++++++++++-- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/resource/csdk/security/src/iotvticalendar.c b/resource/csdk/security/src/iotvticalendar.c index 8276365ca..d65578a30 100644 --- a/resource/csdk/security/src/iotvticalendar.c +++ b/resource/csdk/security/src/iotvticalendar.c @@ -320,6 +320,59 @@ static int DiffSecs(IotvtICalDateTime_t *time1, IotvtICalDateTime_t *time2) (3600 * time1->tm_hour + 60 * time1->tm_min + time1->tm_sec); } +/** + * Validates if the @param currentTime is with in allowable period + * + * @param period -- allowable period + * @param currentTime -- the time that need to be validated against allowable time + * + * @return IOTVTICAL_VALID_ACCESS -- if the request is within valid time period + * IOTVTICAL_INVALID_ACCESS -- if the request is not within valid time period + * IOTVTICAL_INVALID_PARAMETER -- if parameter are invalid + */ +static IotvtICalResult_t ValidatePeriod(IotvtICalPeriod_t *period, IotvtICalDateTime_t *currentTime) +{ + if(NULL == period || NULL == currentTime) + { + return IOTVTICAL_INVALID_PARAMETER; + } + + bool validStartTime = true; + bool validEndTime = true; + bool validDay = false; + bool todayIsStartDay = (0 == DiffDays(&period->startDateTime, currentTime)) ? true : false; + bool todayIsEndDay = (0 == DiffDays(currentTime, &period->endDateTime)) ? true : false; + + //If today is the start day of the allowable period then check + //currentTime > allowable period startTime + if(todayIsStartDay) + { + validStartTime = (0 <= DiffSecs(&period->startDateTime, currentTime)) ? true : false; + } + + //If today is the end day of allowable period then check + //currentTime < allowable period endTime + if(todayIsEndDay) + { + validEndTime = (0 <= DiffSecs(currentTime, &period->endDateTime)) ? true :false; + } + + //Check if today is valid day between startDate and EndDate inclusive + if((0 <= DiffDays(&period->startDateTime, currentTime)) && + (0 <= DiffDays(currentTime, &period->endDateTime))) + { + validDay = true; + } + + if(validDay && validStartTime && validEndTime) + { + return IOTVTICAL_VALID_ACCESS; + } + else + { + return IOTVTICAL_INVALID_ACCESS; + } +} /** * This API is used by policy engine to checks if the @@ -358,14 +411,10 @@ IotvtICalResult_t IsRequestWithinValidTime(char *periodStr, char *recurStr) return ret; } - //If recur is NULL then the access time is between period's startDate and endDate + //If recur is NULL then the access time is between period's startDateTime and endDateTime if(NULL == recurStr) { - if((0 <= DiffDays(&period.startDateTime, currentTime)) && - (0 <= DiffDays(currentTime, &period.endDateTime))) - { - ret = IOTVTICAL_VALID_ACCESS; - } + ret = ValidatePeriod(&period, currentTime); } //If recur is not NULL then the access time is between period's startTime and -- 2.34.1