1 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
3 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
19 #include "gtest/gtest.h"
21 #include "resourcemanager.h"
22 #include "securevirtualresourcetypes.h"
23 #include "credresource.h"
24 #include "oic_malloc.h"
25 #include "oic_string.h"
28 #define TAG PCF("SRM-CRED-UT")
34 //Declare Cred resource methods for testing
35 OCStackResult CreateCredResource();
36 OCEntityHandlerResult CredEntityHandler (OCEntityHandlerFlag flag,
37 OCEntityHandlerRequest * ehRequest);
38 char * BinToCredJSON(const OicSecCred_t * pstat);
39 OicSecCred_t * JSONToCredBin(const char * jsonStr);
40 void InitSecCredInstance(OicSecCred_t * cred);
41 void DeleteCredList(OicSecCred_t* cred);
42 const OicSecCred_t* GetCredResourceData(const OicUuid_t* subject);
47 OicSecCred_t * getCredList()
49 OicSecCred_t * cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
55 OICStrcpy((char *)cred->subject.id, sizeof(cred->subject.id), "subject1");
59 cred->roleIds = (OicSecRole_t *)OICCalloc(cred->roleIdsLen, sizeof(OicSecRole_t));
60 OICStrcpy((char *)cred->roleIds[0].id, sizeof(cred->roleIds[0].id), "role11");
61 OICStrcpy((char *)cred->roleIds[1].id, sizeof(cred->roleIds[1].id), "role12");
66 cred->owners = (OicUuid_t*)OICCalloc(cred->ownersLen, sizeof(OicUuid_t));
72 OICStrcpy((char *)cred->owners[0].id, sizeof(cred->owners[0].id), "ownersId11");
74 cred->next = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
77 OICFree(cred->owners);
81 cred->next->credId = 5678;
82 OICStrcpy((char *)cred->next->subject.id, sizeof(cred->next->subject.id), "subject2");
84 cred->next->roleIdsLen = 0;
86 cred->next->credType = 1;
87 size_t data_size = strlen("My private Key21") + 1;
88 cred->next->privateData.data = (char *)OICCalloc(1, data_size);
89 if(!cred->next->privateData.data)
92 OICFree(cred->owners);
96 OICStrcpy(cred->next->privateData.data, data_size,"My private Key21");
98 cred->next->publicData.data = (char *)OICCalloc(1, strlen("My Public Key123") + 1);
99 OICStrcpy(cred->next->publicData.data, sizeof(cred->next->publicData.data),"My Public Key123");
101 cred->next->ownersLen = 2;
102 cred->next->owners = (OicUuid_t*)OICCalloc(cred->next->ownersLen, sizeof(OicUuid_t));
103 if(!cred->next->owners)
105 OICFree(cred->next->privateData.data);
107 OICFree(cred->owners);
111 OICStrcpy((char *)cred->next->owners[0].id, sizeof(cred->next->owners[0].id), "ownersId21");
112 OICStrcpy((char *)cred->next->owners[1].id, sizeof(cred->next->owners[1].id), "ownersId22");
116 static void printCred(const OicSecCred_t * cred)
118 EXPECT_TRUE(NULL != cred);
120 const OicSecCred_t *credTmp1 = NULL;
121 for(credTmp1 = cred; credTmp1; credTmp1 = credTmp1->next)
123 OC_LOG_V(INFO, TAG, PCF("\ncred->credId = %d"), credTmp1->credId);
124 OC_LOG_V(INFO, TAG, PCF("cred->subject.id = %s"), credTmp1->subject.id);
125 OC_LOG_V(INFO, TAG, PCF("cred->credType = %d"), credTmp1->credType);
126 if(credTmp1->privateData.data)
128 OC_LOG_V(INFO, TAG, PCF("cred->privateData.data = %s"), credTmp1->privateData.data);
130 if(credTmp1->publicData.data)
132 OC_LOG_V(INFO, TAG, PCF("cred->publicData.data = %s"), credTmp1->publicData.data);
134 OC_LOG_V(INFO, TAG, PCF("cred->ownersLen = %zd"), credTmp1->ownersLen);
135 for(size_t i = 0; i < cred->ownersLen; i++)
137 OC_LOG_V(INFO, TAG, PCF("cred->owners[%zd].id = %s"), i, credTmp1->owners[i].id);
142 //InitCredResource Tests
143 TEST(InitCredResourceTest, InitCredResource)
145 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitCredResource());
148 //DeInitCredResource Tests
149 TEST(DeInitCredResourceTest, DeInitCredResource)
151 EXPECT_EQ(OC_STACK_INVALID_PARAM, DeInitCredResource());
154 //CreateCredResource Tests
155 TEST(CreateCredResourceTest, CreateCredResource)
157 EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateCredResource());
160 //CredEntityHandler Tests
161 TEST(CredEntityHandlerTest, CredEntityHandlerWithDummyRequest)
163 OCEntityHandlerRequest req;
164 EXPECT_EQ(OC_EH_ERROR, CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
167 TEST(CredEntityHandlerTest, CredEntityHandlerWithNULLRequest)
169 EXPECT_EQ(OC_EH_ERROR, CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
172 TEST(CredEntityHandlerTest, CredEntityHandlerInvalidFlag)
174 OCEntityHandlerRequest req;
175 EXPECT_EQ(OC_EH_ERROR, CredEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
178 //BinToCredJSON Tests
179 TEST(BinToCredJSONTest, BinToCredJSONNullCred)
181 char* value = BinToCredJSON(NULL);
182 EXPECT_TRUE(value == NULL);
185 TEST(BinToCredJSONTest, BinToCredJSONValidCred)
188 OicSecCred_t * cred = getCredList();
190 json = BinToCredJSON(cred);
192 EXPECT_TRUE(json != NULL);
193 DeleteCredList(cred);
197 //JSONToCredBin Tests
198 TEST(JSONToCredBinTest, JSONToCredBinValidJSON)
200 OicSecCred_t* cred1 = getCredList();
201 char* json = BinToCredJSON(cred1);
203 EXPECT_TRUE(json != NULL);
204 OicSecCred_t *cred2 = JSONToCredBin(json);
205 EXPECT_TRUE(cred2 == NULL);
206 DeleteCredList(cred1);
207 DeleteCredList(cred2);
211 TEST(JSONToCredBinTest, JSONToCredBinNullJSON)
213 OicSecCred_t *cred = JSONToCredBin(NULL);
214 EXPECT_TRUE(cred == NULL);
217 //GetCredResourceData Test
218 TEST(CredGetResourceDataTest, GetCredResourceDataNULLSubject)
220 EXPECT_TRUE(NULL == GetCredResourceData(NULL));
223 TEST(CredGenerateCredentialTest, GenerateCredentialValidInput)
226 OICStrcpy((char *)owners[0].id, sizeof(owners[0].id), "ownersId21");
228 OicUuid_t subject = {0};
229 OICStrcpy((char *)subject.id, sizeof(subject.id), "subject11");
231 char privateKey[] = "My private Key11";
233 OicSecCred_t * cred = NULL;
235 cred = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
236 privateKey, 1, owners);
239 DeleteCredList(cred);
242 TEST(GenerateAndAddCredentialTest, GenerateAndAddCredentialValidInput)
245 OICStrcpy((char *)owners[0].id, sizeof(owners[0].id), "ownersId11");
247 OicUuid_t subject = {0};
248 OICStrcpy((char *)subject.id, sizeof(subject.id), "subject11");
250 char privateKey[] = "My private Key11";
252 OicSecCred_t * cred1 = NULL;
253 OicSecCred_t * headCred = NULL;
255 cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
256 privateKey, 1, owners);
258 EXPECT_EQ(OC_STACK_ERROR, AddCredential(cred1));
261 OICStrcpy((char *)owners[0].id, sizeof(owners[0].id), "ownersId22");
262 OICStrcpy((char *)subject.id, sizeof(subject.id), "subject22");
263 cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
264 privateKey, 1, owners);
265 EXPECT_EQ(OC_STACK_ERROR, AddCredential(cred1));
267 OICStrcpy((char *)owners[0].id, sizeof(owners[0].id), "ownersId33");
268 OICStrcpy((char *)subject.id, sizeof(subject.id), "subject33");
269 cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
270 privateKey, 1, owners);
271 EXPECT_EQ(OC_STACK_ERROR, AddCredential(cred1));
273 const OicSecCred_t* credList = GetCredResourceData(&headCred->subject);
277 DeleteCredList(headCred);
282 TEST(CredGetResourceDataTest, GetCredResourceDataValidSubject)
284 OicSecCred_t* cred = getCredList();
285 EXPECT_TRUE(NULL != GetCredResourceData(cred->subject));