From e4e633206940f01faaf87dbc4acfebf1ab66136a Mon Sep 17 00:00:00 2001 From: Woochul Shim Date: Wed, 9 Sep 2015 22:20:04 +0900 Subject: [PATCH] Check PDM Initialization before excuting exposed PDM APIs - There is a case that user might call PDM API without initialization. This patch could prevent this case by checking initialized status of sqlite DB. [Patch #2] Removing Tab Change-Id: Iaa9aa0f8f04fa8e69ab79c90fb5fc6ee6bbfd3b3 Signed-off-by: Woochul Shim Reviewed-on: https://gerrit.iotivity.org/gerrit/2424 Tested-by: jenkins-iotivity Reviewed-by: Ashwini Kumar Reviewed-by: Sachin Agrawal --- .../provisioning/src/provisioningdatabasemanager.c | 59 ++++++++++++++-------- .../unittest/provisioningdatabasemanager.cpp | 13 +++++ resource/csdk/stack/include/octypes.h | 1 + resource/include/StringConstants.h | 1 + resource/src/OCException.cpp | 2 + resource/unittests/OCExceptionTest.cpp | 2 + 6 files changed, 58 insertions(+), 20 deletions(-) diff --git a/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c b/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c index ccbbbba..e27fdba 100644 --- a/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c +++ b/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c @@ -18,7 +18,6 @@ * * *****************************************************************/ - #include #include #include @@ -78,7 +77,12 @@ #define ASCENDING_ORDER(id1, id2) do{if( (id1) > (id2) )\ { int temp; temp = id1; id1 = id2; id2 = temp; }}while(0) +#define CHECK_PDM_INIT(tag) do{if(true != gInit)\ + { OC_LOG(ERROR, (tag), PCF("PDB is not initialized")); \ + return OC_STACK_PDM_IS_NOT_INITIALIZED; }}while(0) + static sqlite3 *g_db = NULL; +static bool gInit = false; /* Only if we can open sqlite db successfully, gInit is true. */ /** * function to create DB in case DB doesn't exists @@ -98,6 +102,7 @@ static OCStackResult createDB(const char* path) PDM_VERIFY_SQLITE_OK(TAG, result, ERROR, OC_STACK_ERROR); OC_LOG(INFO, TAG, "Created T_DEVICE_LINK_STATE"); + gInit = true; return OC_STACK_OK; } @@ -169,12 +174,14 @@ OCStackResult PDMInit(const char *path) OC_LOG_V(INFO, TAG, "ERROR: Can't open database: %s", sqlite3_errmsg(g_db)); return createDB(dbPath); } + gInit = true; return OC_STACK_OK; } OCStackResult PDMAddDevice(const OicUuid_t *UUID) { + CHECK_PDM_INIT(TAG); if (NULL == UUID) { return OC_STACK_INVALID_PARAM; @@ -236,30 +243,35 @@ static OCStackResult getIdForUUID(const OicUuid_t *UUID , int *id) */ bool PDMIsDuplicateDevice(const OicUuid_t* UUID) { - if (NULL == UUID) + if(gInit) { - OC_LOG(ERROR, TAG, "UUID is NULL"); - return true; - } + if (NULL == UUID) + { + OC_LOG(ERROR, TAG, "UUID is NULL"); + return true; + } - sqlite3_stmt *stmt = 0; - int res = 0; - res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_ID, strlen(PDM_SQLITE_GET_ID) + 1, &stmt, NULL); - PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); + sqlite3_stmt *stmt = 0; + int res = 0; + res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_ID, strlen(PDM_SQLITE_GET_ID) + 1, &stmt, NULL); + PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); - res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_FIRST, UUID, UUID_LENGTH, SQLITE_STATIC); - PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); + res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_FIRST, UUID, UUID_LENGTH, SQLITE_STATIC); + PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); - OC_LOG(DEBUG, TAG, "Binding Done"); + OC_LOG(DEBUG, TAG, "Binding Done"); - while (SQLITE_ROW == sqlite3_step(stmt)) - { - OC_LOG(ERROR, TAG, "UUID is duplicated"); + while (SQLITE_ROW == sqlite3_step(stmt)) + { + OC_LOG(ERROR, TAG, "UUID is duplicated"); + sqlite3_finalize(stmt); + return true; + } sqlite3_finalize(stmt); - return true; + return false; } - sqlite3_finalize(stmt); - return false; + OC_LOG(ERROR, TAG, PCF("PDB is not initialized")); + return true; // return true will stop futher process. } /** @@ -294,7 +306,7 @@ static OCStackResult addlink(int id1, int id2) OCStackResult PDMLinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2) { - + CHECK_PDM_INIT(TAG); if (NULL == UUID1 || NULL == UUID2) { OC_LOG(ERROR, TAG, "Invalid PARAM"); @@ -344,6 +356,7 @@ static OCStackResult removeLink(int id1, int id2) OCStackResult PDMUnlinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2) { + CHECK_PDM_INIT(TAG); if (NULL == UUID1 || NULL == UUID2) { OC_LOG(ERROR, TAG, "Invalid PARAM"); @@ -390,6 +403,7 @@ static OCStackResult removeFromDeviceList(int id) OCStackResult PDMDeleteDevice(const OicUuid_t *UUID) { + CHECK_PDM_INIT(TAG); if (NULL == UUID) { return OC_STACK_INVALID_PARAM; @@ -412,7 +426,7 @@ OCStackResult PDMDeleteDevice(const OicUuid_t *UUID) } -OCStackResult updateLinkState(int id1, int id2, int state) +static OCStackResult updateLinkState(int id1, int id2, int state) { sqlite3_stmt *stmt = 0; int res = 0 ; @@ -441,6 +455,7 @@ OCStackResult updateLinkState(int id1, int id2, int state) OCStackResult PDMSetLinkStale(const OicUuid_t* uuidOfDevice1, const OicUuid_t* uuidOfDevice2) { + CHECK_PDM_INIT(TAG); if (NULL == uuidOfDevice1 || NULL == uuidOfDevice2) { OC_LOG(ERROR, TAG, "Invalid PARAM"); @@ -467,6 +482,7 @@ OCStackResult PDMSetLinkStale(const OicUuid_t* uuidOfDevice1, const OicUuid_t* u OCStackResult PDMGetOwnedDevices(OCUuidList_t **uuidList, size_t *numOfDevices) { + CHECK_PDM_INIT(TAG); if (NULL != *uuidList) { OC_LOG(ERROR, TAG, "Not null list will cause memory leak"); @@ -522,6 +538,7 @@ static OCStackResult getUUIDforId(int id, OicUuid_t *uid) OCStackResult PDMGetLinkedDevices(const OicUuid_t *UUID, OCUuidList_t **UUIDLIST, size_t *numOfDevices) { + CHECK_PDM_INIT(TAG); if (NULL != *UUIDLIST) { OC_LOG(ERROR, TAG, "Not null list will cause memory leak"); @@ -584,6 +601,7 @@ OCStackResult PDMGetLinkedDevices(const OicUuid_t *UUID, OCUuidList_t **UUIDLIST OCStackResult PDMGetToBeUnlinkedDevices(OCPairList_t **staleDevList, size_t *numOfDevices) { + CHECK_PDM_INIT(TAG); if (NULL != *staleDevList) { OC_LOG(ERROR, TAG, "Not null list will cause memory leak"); @@ -628,6 +646,7 @@ OCStackResult PDMGetToBeUnlinkedDevices(OCPairList_t **staleDevList, size_t *num OCStackResult PDMClose() { + CHECK_PDM_INIT(TAG); int res = 0; res = sqlite3_close(g_db); PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); diff --git a/resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp b/resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp index e1e3ec3..2265f60 100644 --- a/resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp +++ b/resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp @@ -27,6 +27,19 @@ const char ID_4[] = "4111111111111111"; const char ID_5[] = "5111111111111111"; const char ID_6[] = "6111111111111111"; +TEST(CallPDMAPIbeforeInit, BeforeInit) +{ + EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMAddDevice(NULL)); + EXPECT_EQ(true, PDMIsDuplicateDevice(NULL)); // return type is not OCStackResult + EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMLinkDevices(NULL, NULL)); + EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMUnlinkDevices(NULL, NULL)); + EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMDeleteDevice(NULL)); + EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetOwnedDevices(NULL, NULL)); + 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)); +} + TEST(PDMInitTest, PDMInitWithNULL) { EXPECT_EQ(OC_STACK_OK, PDMInit(NULL)); diff --git a/resource/csdk/stack/include/octypes.h b/resource/csdk/stack/include/octypes.h index 6603fbe..3c3ba8c 100644 --- a/resource/csdk/stack/include/octypes.h +++ b/resource/csdk/stack/include/octypes.h @@ -634,6 +634,7 @@ typedef enum OC_STACK_UNAUTHORIZED_REQ, /** Error code from PDM */ + OC_STACK_PDM_IS_NOT_INITIALIZED, OC_STACK_DUPLICATE_UUID, OC_STACK_INCONSISTENT_DB, diff --git a/resource/include/StringConstants.h b/resource/include/StringConstants.h index eeaecd5..d6fd818 100644 --- a/resource/include/StringConstants.h +++ b/resource/include/StringConstants.h @@ -105,6 +105,7 @@ namespace OC static const char INVALID_ATTRIBUTE[] = "Invalid Attribute: "; static const char INVALID_DEVICE_INFO[] = "Invalid Device Information"; static const char UNAUTHORIZED_REQUEST[] = "Unauthorized Request"; + static const char PDM_DB_NOT_INITIALIZED[] = "Provisioning DB is not initialized"; static const char DUPLICATE_UUID[] = "Duplicate UUID in DB"; static const char INCONSISTENT_DB[] = "Data in provisioning DB is inconsistent"; diff --git a/resource/src/OCException.cpp b/resource/src/OCException.cpp index 8d4b584..4d0b4a2 100644 --- a/resource/src/OCException.cpp +++ b/resource/src/OCException.cpp @@ -97,6 +97,8 @@ std::string OC::OCException::reason(const OCStackResult sr) return OC::Exception::INVALID_REPRESENTATION; case OC_STACK_UNAUTHORIZED_REQ: return OC::Exception::UNAUTHORIZED_REQUEST; + case OC_STACK_PDM_IS_NOT_INITIALIZED: + return OC::Exception::PDM_DB_NOT_INITIALIZED; case OC_STACK_DUPLICATE_UUID: return OC::Exception::DUPLICATE_UUID; case OC_STACK_INCONSISTENT_DB: diff --git a/resource/unittests/OCExceptionTest.cpp b/resource/unittests/OCExceptionTest.cpp index 80e67b9..39436cd 100644 --- a/resource/unittests/OCExceptionTest.cpp +++ b/resource/unittests/OCExceptionTest.cpp @@ -68,6 +68,7 @@ namespace OC OC_STACK_PRESENCE_TIMEOUT, OC_STACK_PRESENCE_DO_NOT_HANDLE, OC_STACK_ERROR, + OC_STACK_PDM_IS_NOT_INITIALIZED, OC_STACK_DUPLICATE_UUID, OC_STACK_INCONSISTENT_DB }; @@ -109,6 +110,7 @@ namespace OC OC::Exception::PRESENCE_TIMEOUT, OC::Exception::PRESENCE_NOT_HANDLED, OC::Exception::GENERAL_FAULT, + OC::Exception::PDM_DB_NOT_INITIALIZED, OC::Exception::DUPLICATE_UUID, OC::Exception::INCONSISTENT_DB }; -- 2.7.4