Fixed bug IOT-798
authorShilpa Sodani <shilpa.a.sodani@intel.com>
Wed, 14 Oct 2015 20:37:21 +0000 (13:37 -0700)
committerSachin Agrawal <sachin.agrawal@intel.com>
Mon, 19 Oct 2015 17:46:16 +0000 (17:46 +0000)
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 <shilpa.a.sodani@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3881
Reviewed-by: Mark Tung <mark.y.tung@intel.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
resource/csdk/security/src/iotvticalendar.c

index 8276365ca34c81dd26235bd8b087997d66191313..d65578a30cb50d35f62f4eb00ed533e545e9fe36 100644 (file)
@@ -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