1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "gtest/gtest.h"
24 #include <linux/limits.h>
27 #include "ocpayload.h"
28 #include "oic_malloc.h"
30 #include "cainterface.h"
31 #include "secureresourcemanager.h"
32 #include "securevirtualresourcetypes.h"
33 #include "srmresourcestrings.h"
34 #include "aclresource.h"
41 extern char * BinToAclJSON(const OicSecAcl_t * acl);
42 extern OicSecAcl_t * JSONToAclBin(const char * jsonStr);
43 char* ReadFile(const char* filename);
44 extern void DeleteACLList(OicSecAcl_t* acl);
45 OCStackResult GetDefaultACL(OicSecAcl_t** defaultAcl);
46 OCEntityHandlerResult ACLEntityHandler (OCEntityHandlerFlag flag,
47 OCEntityHandlerRequest * ehRequest);
52 const char* JSON_FILE_NAME = "oic_unittest.json";
53 const char* DEFAULT_ACL_JSON_FILE_NAME = "oic_unittest_default_acl.json";
54 const char* ACL1_JSON_FILE_NAME = "oic_unittest_acl1.json";
56 #define NUM_ACE_FOR_WILDCARD_IN_ACL1_JSON (2)
58 char* ReadFile(const char* filename)
64 // TODO: Find the location of the executable and concatenate the SVR file name
66 fp = fopen(filename, "r");
69 if (stat(filename, &st) == 0)
71 data = (char*)OICMalloc(st.st_size);
74 if (fread(data, 1, st.st_size, fp) != (size_t)st.st_size)
76 printf("Error in reading file %s", filename);
84 printf("Unable to open %s file", filename);
90 void SetPersistentHandler(OCPersistentStorage *ps, bool set)
102 memset(ps, 0, sizeof(OCPersistentStorage));
104 EXPECT_EQ(OC_STACK_OK,
105 OCRegisterPersistentStorageHandler(ps));
108 // JSON Marshalling Tests
109 TEST(ACLResourceTest, JSONMarshallingTests)
111 char *jsonStr1 = ReadFile(ACL1_JSON_FILE_NAME);
114 cJSON_Minify(jsonStr1);
115 /* Workaround : cJSON_Minify does not remove all the unwanted characters
116 from the end. Here is an attempt to remove those characters */
117 int len = strlen(jsonStr1);
120 if (jsonStr1[--len] == '}')
125 jsonStr1[len + 1] = 0;
127 OicSecAcl_t * acl = JSONToAclBin(jsonStr1);
128 EXPECT_TRUE(NULL != acl);
130 char * jsonStr2 = BinToAclJSON(acl);
131 EXPECT_TRUE(NULL != jsonStr2);
133 EXPECT_STREQ(jsonStr1, jsonStr2);
142 TEST(ACLResourceTest, GetDefaultACLTests)
144 // Read default ACL from the file
145 char *jsonStr = ReadFile(DEFAULT_ACL_JSON_FILE_NAME);
148 OicSecAcl_t * acl = JSONToAclBin(jsonStr);
149 EXPECT_TRUE(NULL != acl);
151 // Invoke API to generate default ACL
152 OicSecAcl_t * defaultAcl = NULL;
153 OCStackResult ret = GetDefaultACL(&defaultAcl);
154 EXPECT_TRUE(NULL == defaultAcl);
156 EXPECT_TRUE(OC_STACK_ERROR == ret);
158 // Verify if the SRM generated default ACL matches with unit test default
159 if (acl && defaultAcl)
161 EXPECT_TRUE(memcmp(&(acl->subject), &(defaultAcl->subject), sizeof(OicUuid_t)) == 0);
162 EXPECT_EQ(acl->resourcesLen, defaultAcl->resourcesLen);
163 for (size_t i = 0; i < acl->resourcesLen; i++)
165 EXPECT_EQ(strlen(acl->resources[i]), strlen(defaultAcl->resources[i]));
167 memcmp(acl->resources[i], defaultAcl->resources[i],
168 strlen(acl->resources[i])) == 0);
170 EXPECT_EQ(acl->permission, defaultAcl->permission);
175 DeleteACLList(defaultAcl);
182 TEST(ACLResourceTest, ACLPostTest)
184 OCEntityHandlerRequest ehReq = {};
186 // Read an ACL from the file
187 char *jsonStr = ReadFile(ACL1_JSON_FILE_NAME);
190 static OCPersistentStorage ps =
192 SetPersistentHandler(&ps, true);
194 // Create Entity Handler POST request payload
195 ehReq.method = OC_REST_POST;
196 ehReq.payload = (OCPayload*)OCSecurityPayloadCreate(jsonStr);
198 OCEntityHandlerResult ehRet = ACLEntityHandler(OC_REQUEST_FLAG, &ehReq);
199 EXPECT_TRUE(OC_EH_ERROR == ehRet);
201 // Convert JSON into OicSecAcl_t for verification
202 OicSecAcl_t * acl = JSONToAclBin(jsonStr);
203 EXPECT_TRUE(NULL != acl);
205 // Verify if SRM contains ACL for the subject
206 OicSecAcl_t* savePtr = NULL;
207 const OicSecAcl_t* subjectAcl = GetACLResourceData(&(acl->subject), &savePtr);
208 EXPECT_TRUE(NULL != subjectAcl);
213 OCPayloadDestroy(ehReq.payload);
219 // GetACLResource tests
220 TEST(ACLResourceTest, GetACLResourceTests)
222 // gAcl is a pointer to the the global ACL used by SRM
223 extern OicSecAcl_t *gAcl;
225 // Read an ACL from the file
226 char *jsonStr = ReadFile(ACL1_JSON_FILE_NAME);
229 gAcl = JSONToAclBin(jsonStr);
230 EXPECT_TRUE(NULL != gAcl);
232 // Verify that ACL file contains 2 ACE entries for 'WILDCARD' subject
233 const OicSecAcl_t* acl = NULL;
234 OicSecAcl_t* savePtr = NULL;
235 OicUuid_t subject = WILDCARD_SUBJECT_ID;
240 acl = GetACLResourceData(&subject, &savePtr);
241 count = (NULL != acl) ? count + 1 : count;
242 } while (acl != NULL);
244 EXPECT_EQ(count, NUM_ACE_FOR_WILDCARD_IN_ACL1_JSON);
246 /* Perform cleanup */