Added support for proactive link checking
authorRandeep Singh <randeep.s@samsung.com>
Tue, 22 Sep 2015 01:44:31 +0000 (10:44 +0900)
committerSachin Agrawal <sachin.agrawal@intel.com>
Mon, 28 Sep 2015 15:59:07 +0000 (15:59 +0000)
In current implementation whenever user tries to add link between two devices
which already exists apis logs error and doesn't return callback.
In this change, link api first checks that link between two intended devices
exists or not, if it exists then api return error and doesn't move ahead.

Change-Id: Iac02a44542235b962de2fe965f45548d96f9c625
Signed-off-by: Randeep Singh <randeep.s@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2899
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
(cherry picked from commit 6aa9d7eb1a93cdac934be8a4ff834de5f27ede3d)
Reviewed-on: https://gerrit.iotivity.org/gerrit/3181

resource/csdk/security/provisioning/include/internal/provisioningdatabasemanager.h
resource/csdk/security/provisioning/src/ocprovisioningmanager.c
resource/csdk/security/provisioning/src/provisioningdatabasemanager.c
resource/csdk/security/provisioning/src/secureresourceprovider.c
resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp

index 45588b3..3d3f2d4 100644 (file)
@@ -152,6 +152,19 @@ void PDMDestoryOicUuidLinkList(OCUuidList_t* ptr);
  */
 void PDMDestoryStaleLinkList(OCPairList_t* ptr);
 
+/**
+ * This method is used by provisioning manager to check does the link exists between
+ * two devices or not.
+ *
+ * @param[in] uuidOfDevice1 UUID of device1.
+ * @param[in] uuidOfDevice2 UUID of device2.
+ * @param[out] result true when link exists otherwise false.
+ *
+ * @return OC_STACK_OK in case of success and other value otherwise.
+ */
+OCStackResult PDMIsLinkExists(const OicUuid_t* uuidOfDevice1, const OicUuid_t* uuidOfDevice2,
+                                bool *result );
+
 #ifdef __cplusplus
 }
 #endif
index 4b84d84..59f6895 100644 (file)
@@ -518,12 +518,27 @@ OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_
         OC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
         return OC_STACK_INVALID_PARAM;
     }
+
+    OC_LOG(DEBUG, TAG, "Checking link in DB");
+    bool linkExists = true;
+    OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExists);
+    if(res != OC_STACK_OK)
+    {
+        OC_LOG(ERROR, TAG, "Internal Error Occured");
+        return res;
+    }
+    if (linkExists)
+    {
+        OC_LOG(ERROR, TAG, "Link already exists");
+        return OC_STACK_INVALID_PARAM;
+    }
+
     int noOfResults = 2; // Initial Value
-    if (NULL!=pDev1Acl)
+    if (NULL != pDev1Acl)
     {
         ++noOfResults;
     }
-    if (NULL!=pDev2Acl)
+    if (NULL != pDev2Acl)
     {
        ++noOfResults;
     }
@@ -546,7 +561,7 @@ OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_
     link->resultCallback = resultCallback;
     link->currentCountResults = 0;
     link->resArr = (OCProvisionResult_t*) OICMalloc(sizeof(OCProvisionResult_t)*noOfResults);
-    OCStackResult res = SRPProvisionCredentials(link, type, keySize,
+    res = SRPProvisionCredentials(link, type, keySize,
                                      pDev1, pDev2, &ProvisionCredsCB);
     if (res != OC_STACK_OK)
     {
@@ -691,4 +706,5 @@ OCStackResult OCProvisionCRL(void* ctx, const OCProvisionDev_t *selectedDeviceIn
 {
     return SRPProvisionCRL(ctx, selectedDeviceInfo, crl, resultCallback);
 }
-#endif // __WITH_X509__
\ No newline at end of file
+#endif // __WITH_X509__
+
index 30ce3f3..52a9844 100755 (executable)
@@ -680,3 +680,50 @@ void PDMDestoryStaleLinkList(OCPairList_t* ptr)
         }
     }
 }
+
+OCStackResult PDMIsLinkExists(const OicUuid_t* uuidOfDevice1, const OicUuid_t* uuidOfDevice2,
+                               bool* result)
+{
+    CHECK_PDM_INIT(TAG);
+    if (NULL == uuidOfDevice1 || NULL == uuidOfDevice2 || NULL == result)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+    int id1 = 0;
+    int id2 = 0;
+    if (OC_STACK_OK != getIdForUUID(uuidOfDevice1, &id1))
+    {
+        OC_LOG(ERROR, TAG, "Requested value not found");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    if (OC_STACK_OK != getIdForUUID(uuidOfDevice2, &id2))
+    {
+        OC_LOG(ERROR, TAG, "Requested value not found");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    ASCENDING_ORDER(id1, id2);
+
+    sqlite3_stmt *stmt = 0;
+    int res = 0;
+    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_LINKED_DEVICES,
+                              strlen(PDM_SQLITE_GET_LINKED_DEVICES) + 1, &stmt, NULL);
+    PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
+
+    res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id1);
+    PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
+
+    res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_SECOND, id2);
+    PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
+
+    bool ret = false;
+    while(SQLITE_ROW == sqlite3_step(stmt))
+    {
+        OC_LOG(INFO, TAG, "Link already exists between devices");
+        ret = true;
+    }
+    sqlite3_finalize(stmt);
+    *result = ret;
+    return OC_STACK_OK;
+}
index b64cff2..78846ff 100755 (executable)
@@ -629,6 +629,19 @@ OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t k
 
     OC_LOG(INFO, TAG, "In SRPProvisionCredentials");
 
+    bool linkExisits = true;
+    OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
+    if (res != OC_STACK_OK)
+    {
+        OC_LOG(ERROR, TAG, "Internal error occured");
+        return res;
+    }
+    if (linkExisits)
+    {
+        OC_LOG(ERROR, TAG, "Link already exists");
+        return OC_STACK_INVALID_PARAM;
+    }
+
     OicUuid_t provTooldeviceID =   {{0,}};
     if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
     {
index c77656c..63a4dd8 100644 (file)
@@ -26,6 +26,8 @@ const char ID_3[] = "3111111111111111";
 const char ID_4[] = "4111111111111111";
 const char ID_5[] = "5111111111111111";
 const char ID_6[] = "6111111111111111";
+const char ID_7[] = "7777777777777777";
+const char ID_8[] = "8777777777777777";
 
 TEST(CallPDMAPIbeforeInit, BeforeInit)
 {
@@ -38,6 +40,7 @@ TEST(CallPDMAPIbeforeInit, BeforeInit)
     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetLinkedDevices(NULL, NULL, NULL));
     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMSetLinkStale(NULL, NULL));
     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetToBeUnlinkedDevices(NULL, NULL));
+    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMIsLinkExists(NULL, NULL, NULL));
 }
 
 TEST(PDMInitTest, PDMInitWithNULL)
@@ -242,3 +245,19 @@ TEST(PDMDestoryStaleLinkList, NULLParam)
 {
     PDMDestoryStaleLinkList(NULL);
 }
+
+TEST(PDMIsLinkExistsTest, DuplicateID)
+{
+    EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
+    OicUuid_t uid1 = {{0,}};
+    memcpy(&uid1.id, ID_7, sizeof(uid1.id));
+    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
+    OicUuid_t uid2 = {{0,}};
+    memcpy(&uid2.id, ID_8, sizeof(uid2.id));
+    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
+
+    bool linkExisits = true;
+    OCStackResult res = PDMIsLinkExists(&uid1, &uid2, &linkExisits);
+    EXPECT_EQ(OC_STACK_OK, res);
+    EXPECT_FALSE(linkExisits);
+}
\ No newline at end of file