Fixed the IsRequestWithinValidTimeTest unit tests.
authorShilpa Sodani <shilpa.a.sodani@intel.com>
Wed, 26 Aug 2015 07:17:23 +0000 (00:17 -0700)
committerJon A. Cruz <jonc@osg.samsung.com>
Fri, 28 Aug 2015 07:25:11 +0000 (07:25 +0000)
Fixed  IsRequestWithinValidTimeTest unit tests so that they don't failing on boundary
conditions.

Change-Id: Ie52e3e75d8fc2ee052f43a532681cb2091545fbb
Signed-off-by: Shilpa Sodani <shilpa.a.sodani@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2279
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
resource/csdk/security/unittest/iotvticalendartest.cpp

index 2d207cd..781eddb 100644 (file)
@@ -85,6 +85,60 @@ static void printRecur(IotvtICalRecur_t *recur)
     }
 }
 
+
+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);
+
+        OC_LOG_V(INFO, TAG, PCF("t1 = %02d:%02d:%02d"), t1.tm_hour, t1.tm_min, t1.tm_sec );
+        OC_LOG_V(INFO, TAG, PCF("t2 = %02d:%02d:%02d"), t2.tm_hour, t2.tm_min, t2.tm_sec );
+    }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);
+        }
+    }
+}
+
 //ParsePeriod Tests
 TEST(ParsePeriodTest, ParsePeriodValidDateTime)
 {
@@ -201,33 +255,22 @@ TEST(ParseRecurTest, ParseRecurInValidByday)
     EXPECT_EQ(IOTVTICAL_INVALID_RRULE, ParseRecur(recurStr, &recur));
 }
 
-//FIXME: ALL IsRequestWithinValidTime tests will fail after 20151230
 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriod1)
 {
-    char periodStr[] = "20150630/20151230";
+    char periodStr[] = "20150630/20551230";
     EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, NULL));
 }
 
 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur1)
 {
-    //Daily on days MO, WE & FR from 6:00:00am to 8:00:00pm until 20151230
-    char recurStr[] = "FREQ=DAILY; UNTIL=20151230; BYDAY=MO, WE, FR";
+    //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";
     char periodStr[] = "20150630T060000/20150630T200000";
-
-    time_t rt = time(0);
-    IotvtICalDateTime_t *ct = localtime(&rt);
-
+    int startTime = 6;
+    int endTime = 20;
     int byDay = MONDAY | WEDNESDAY | FRIDAY;
-    int isValidWD = (0x1 << ct->tm_wday) & byDay;
 
-    if(isValidWD && 6 <= ct->tm_hour && 20>= ct->tm_hour)
-    {
-        EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
-    }
-    else
-    {
-        EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
-    }
+    checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
 }
 
 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur2)
@@ -235,37 +278,24 @@ TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur2)
     //Daily forever from 6:00:00am to 8:00:00pm
     char recurStr[] = "FREQ=DAILY";
     char periodStr[] = "20150630T060000/20150630T200000";
+    int startTime = 6;
+    int endTime = 20;
+    int byDay = -1;
 
-    time_t rt = time(0);
-    IotvtICalDateTime_t *ct = localtime(&rt);
-
-    if(6 <= ct->tm_hour && 20>= ct->tm_hour)
-    {
-        EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
-    }
-    else
-    {
-        EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
-    }
+    checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
 }
 
 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur3)
 {
-    //Daily until 20151230 from 6:00:00am to 8:00:00pm
-    char recurStr[] = "FREQ=DAILY; UNTIL=20151230";
+    //Daily until 20551230 from 6:00:00am to 8:00:00pm
+    char recurStr[] = "FREQ=DAILY; UNTIL=20551230";
     char periodStr[] = "20150630T060000/20150630T200000";
+    int startTime = 6;
+    int endTime = 20;
+    int byDay = -1;
 
-    time_t rt = time(0);
-    IotvtICalDateTime_t *ct = localtime(&rt);
+    checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
 
-    if(6 <= ct->tm_hour && 20>= ct->tm_hour)
-    {
-        EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
-    }
-    else
-    {
-        EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
-    }
 }
 
 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeValidPeriodAndRecur4)
@@ -273,22 +303,13 @@ 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";
-
-    time_t rt = time(0);
-    IotvtICalDateTime_t *ct = localtime(&rt);
-
+    int startTime = 6;
+    int endTime = 20;
     int byDay = MONDAY | WEDNESDAY | FRIDAY;
-    int isValidWD = (0x1 << ct->tm_wday) & byDay;
 
-    if(isValidWD && 6 <= ct->tm_hour && 20>= ct->tm_hour)
-    {
-        EXPECT_EQ(IOTVTICAL_VALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
-    }
-    else
-    {
-        EXPECT_EQ(IOTVTICAL_INVALID_ACCESS, IsRequestWithinValidTime(periodStr, recurStr));
-    }
-}
+    checkValidityOfRequest(recurStr, periodStr, startTime, endTime, byDay);
+
+ }
 
 TEST(IsRequestWithinValidTimeTest, IsRequestWithinValidTimeInValidPeriodAndValidRecur)
 {