replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / credentialresource.cpp
index 13fce86..6640831 100644 (file)
@@ -19,6 +19,7 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include "gtest/gtest.h"
+extern "C" {
 #include "logger.h"
 #include "ocpayload.h"
 #include "ocstack.h"
 #include "srmutility.h"
 #include "psinterface.h"
 #include "security_internals.h"
-
+#include "secureresourcemanager.h"
+}
 #define TAG "SRM-CRED-UT"
 
+const char *SRP_TEST_FILE_NAME = "secureresourceprovider.dat";
+static OCPersistentStorage ps;
+
 OicSecCred_t * getCredList()
 {
     size_t sz = 0;
@@ -54,6 +59,7 @@ OicSecCred_t * getCredList()
     cred->credType = SYMMETRIC_PAIR_WISE_KEY;
     cred->privateData.encoding = OIC_ENCODING_RAW;
     cred->privateData.data = (uint8_t *)OICCalloc(1, strlen("My private Key11") + 1);
+    cred->privateData.len = strlen("My private Key11");
     VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
     OICStrcpy((char *)cred->privateData.data, strlen("My private Key11")+1,"My private Key11");
     // use |memcpy| for copying full-lengthed UUID without null termination
@@ -68,7 +74,8 @@ OicSecCred_t * getCredList()
 #endif
     cred->next->credType = SYMMETRIC_PAIR_WISE_KEY;
     cred->next->privateData.encoding = OIC_ENCODING_RAW;
-    sz = strlen("My private Key21") + 1;
+    cred->next->privateData.len = strlen("My private Key21");
+    sz = cred->next->privateData.len + 1;
     cred->next->privateData.data = (uint8_t *)OICCalloc(1, sz);
     VERIFY_NON_NULL(TAG, cred->next->privateData.data, ERROR);
     OICStrcpy((char *)cred->next->privateData.data, sz, "My private Key21");
@@ -106,16 +113,37 @@ static void printCred(const OicSecCred_t * cred)
         {
             OIC_LOG_V(INFO, TAG, "cred->privateData.data = %s", credTmp1->privateData.data);
         }
-#ifdef __WITH_X509__
+#ifdef __WITH_DTLS__
         if(credTmp1->publicData.data)
         {
            OIC_LOG_V(INFO, TAG, "cred->publicData.data = %s", credTmp1->publicData.data);
         }
-#endif /* __WITH_X509__ */
+#endif /* __WITH_DTLS__ */
     OIC_LOG_V(INFO, TAG, "cred->rownerID = %s", credTmp1->rownerID.id);
     }
 }
 
+FILE *fOpenCustom(const char *path, const char *mode)
+{
+    (void)path;
+    FILE *fp = fopen(SRP_TEST_FILE_NAME, mode);
+    return fp;
+}
+
+//init db
+TEST(CredResourceTest,initDb)
+{
+    ps.open = fOpenCustom;
+    ps.read = fread;
+    ps.write = fwrite;
+    ps.close = fclose;
+    ps.unlink = unlink;
+
+    SRMRegisterPersistentStorageHandler(&ps);
+
+    InitPersistentStorageInterface();
+}
+
  //InitCredResource Tests
 TEST(CredResourceTest, InitCredResource)
 {
@@ -156,17 +184,15 @@ TEST(CredResourceTest, CredEntityHandlerInvalidFlag)
 }
 
 //Cred DELETE request
+/*
 TEST(CredResourceTest, CredEntityHandlerDeleteTest)
 {
     OCEntityHandlerRequest ehReq =  OCEntityHandlerRequest();
-    static OCPersistentStorage ps =  OCPersistentStorage();
     const OicSecCred_t* subjectCred1 = NULL;
     const OicSecCred_t* subjectCred2 = NULL;
     OCEntityHandlerResult ehRet = OC_EH_ERROR;
     char query[] = "subjectuuid=31313131-3131-3131-3131-313131313131"; //canonical uuid of subject1
 
-    SetPersistentHandler(&ps, true);
-
     OicSecCred_t *cred = getCredList();
     ASSERT_TRUE(NULL != cred);
     uint8_t *payload = NULL;
@@ -219,6 +245,7 @@ TEST(CredResourceTest, CredEntityHandlerDeleteTest)
     DeleteCredList(cred);
     OCPayloadDestroy((OCPayload *)ehReq.payload);
 }
+*/
 
 TEST(CredResourceTest, CredToCBORPayloadNULL)
 {
@@ -337,13 +364,14 @@ TEST(CredResourceTest, GenerateCredentialValidInput)
 
     OicSecCred_t * cred  = NULL;
     cred = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
-                              &key, &rownerID);
+                              &key, &rownerID, NULL);
     printCred(cred);
 
     ASSERT_TRUE(NULL != cred);
     DeleteCredList(cred);
 }
 
+/*
 TEST(CredResourceTest, GenerateAndAddCredentialValidInput)
 {
     OicUuid_t rownerID = {{0}};
@@ -359,7 +387,7 @@ TEST(CredResourceTest, GenerateAndAddCredentialValidInput)
     OicSecCred_t *headCred = NULL;
 
     cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
-                               &key, &rownerID);
+                               &key, &rownerID, NULL);
 
     EXPECT_EQ(OC_STACK_OK, AddCredential(cred1));
     headCred = cred1;
@@ -367,13 +395,13 @@ TEST(CredResourceTest, GenerateAndAddCredentialValidInput)
     OICStrcpy((char *)rownerID.id, sizeof(rownerID.id), "ownersId22");
     OICStrcpy((char *)subject.id, sizeof(subject.id), "subject22");
     cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
-                               &key, &rownerID);
+                               &key, &rownerID, NULL);
     EXPECT_EQ(OC_STACK_OK, AddCredential(cred1));
 
     OICStrcpy((char *)rownerID.id, sizeof(rownerID.id), "ownersId33");
     OICStrcpy((char *)subject.id, sizeof(subject.id), "subject33");
     cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
-                               &key, &rownerID);
+                               &key, &rownerID, NULL);
     EXPECT_EQ(OC_STACK_OK, AddCredential(cred1));
 
     const OicSecCred_t* credList = GetCredResourceData(&headCred->subject);
@@ -382,6 +410,7 @@ TEST(CredResourceTest, GenerateAndAddCredentialValidInput)
 
     DeleteCredList(headCred);
 }
+*/
 
 #if 0
 TEST(CredGetResourceDataTest, GetCredResourceDataValidSubject)
@@ -391,48 +420,7 @@ TEST(CredGetResourceDataTest, GetCredResourceDataValidSubject)
 }
 #endif
 
-#ifdef __WITH_X509__
-#include <stdlib.h>
-
-static char PROV_TOOL_DB_FILE[] = "/oic_svr_db_prov.dat";
-
-#define STRINGIZE2(x) #x
-#define STRINGIZE(x) STRINGIZE2(x)
-
-static FILE *client_fopen(const char* UNUSED_PARAM , const char *mode)
-{
-    (void)UNUSED_PARAM;
-
-    int len = strlen(STRINGIZE(SECURITY_BUILD_UNITTEST_DIR)) + strlen(PROV_TOOL_DB_FILE) + 1;
-    char *filepath = (char *)OICCalloc(1, len);
-
-    if (!filepath)
-    {
-        printf("filepath memory allocation failed. \n");
-        return NULL;
-    }
-
-    snprintf(filepath, len, "%s%s", STRINGIZE(SECURITY_BUILD_UNITTEST_DIR), PROV_TOOL_DB_FILE);
-
-    FILE* file =  fopen(filepath, mode);
-    OICFree(filepath);
-    return file;
-}
-
-static OCPersistentStorage ps = { client_fopen, fread, fwrite, fclose, unlink };
-
-//GetDtlsX509Credentials Test
-TEST(CredResourceTest, GetDtlsX509Credentials)
-{
-    ASSERT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_CLIENT_SERVER));
-    OCRegisterPersistentStorageHandler(&ps);
-    InitCredResource();
-    CADtlsX509Creds_t g_X509Cred = {{0}, 0, 0, {0}, {0}, {0}};
-    EXPECT_EQ(0, GetDtlsX509Credentials(&g_X509Cred));
-}
-
-#endif
-#if defined(__WITH_DTLS__)
+#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
 TEST(CredGetDtlsPskCredentialsTest, NullResult)
 {
     EXPECT_EQ(-1, GetDtlsPskCredentials(CA_DTLS_PSK_KEY, NULL, 0, NULL, 0));
@@ -443,7 +431,7 @@ TEST(CredAddTmpPskWithPINTest, NullSubject)
     EXPECT_EQ(OC_STACK_INVALID_PARAM, AddTmpPskWithPIN(NULL, SYMMETRIC_PAIR_WISE_KEY,
               NULL, 0, NULL, NULL));
 }
-#endif
+#endif // __WITH_DTLS__ or __WITH_TLS__
 TEST(CredCBORPayloadToCredTest, NullPayload)
 {
     EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToCred(NULL, 0, NULL));