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 "oic_malloc.h"
29 #include "cainterface.h"
30 #include "secureresourcemanager.h"
31 #include "securevirtualresourcetypes.h"
32 #include "srmresourcestrings.h"
33 #include "aclresource.h"
40 extern char * BinToAclJSON(const OicSecAcl_t * acl);
41 extern OicSecAcl_t * JSONToAclBin(const char * jsonStr);
42 char* ReadFile(const char* filename);
43 extern void DeleteACLList(OicSecAcl_t* acl);
44 OCStackResult GetDefaultACL(OicSecAcl_t** defaultAcl);
45 OCEntityHandlerResult ACLEntityHandler (OCEntityHandlerFlag flag,
46 OCEntityHandlerRequest * ehRequest);
51 const char* JSON_FILE_NAME = "oic_unittest.json";
52 const char* DEFAULT_ACL_JSON_FILE_NAME = "oic_unittest_default_acl.json";
53 const char* ACL1_JSON_FILE_NAME = "oic_unittest_acl1.json";
55 #define NUM_ACE_FOR_WILDCARD_IN_ACL1_JSON (2)
57 char* ReadFile(const char* filename)
63 // TODO: Find the location of the executable and concatenate the SVR file name
65 fp = fopen(filename, "r");
68 if (stat(filename, &st) == 0)
70 data = (char*)OICMalloc(st.st_size);
73 if (fread(data, 1, st.st_size, fp) != (size_t)st.st_size)
75 printf("Error in reading file %s", filename);
83 printf("Unable to open %s file", filename);
89 void SetPersistentHandler(OCPersistentStorage *ps, bool set)
101 memset(ps, 0, sizeof(OCPersistentStorage));
103 EXPECT_EQ(OC_STACK_OK,
104 OCRegisterPersistentStorageHandler(ps));
107 // JSON Marshalling Tests
108 TEST(ACLResourceTest, JSONMarshallingTests)
110 char *jsonStr1 = ReadFile(ACL1_JSON_FILE_NAME);
113 cJSON_Minify(jsonStr1);
114 /* Workaround : cJSON_Minify does not remove all the unwanted characters
115 from the end. Here is an attempt to remove those characters */
116 int len = strlen(jsonStr1);
119 if (jsonStr1[--len] == '}')
124 jsonStr1[len + 1] = 0;
126 OicSecAcl_t * acl = JSONToAclBin(jsonStr1);
127 EXPECT_TRUE(NULL != acl);
129 char * jsonStr2 = BinToAclJSON(acl);
130 EXPECT_TRUE(NULL != jsonStr2);
132 if (jsonStr1 && jsonStr2)
134 EXPECT_STREQ(jsonStr1, jsonStr2);
144 TEST(ACLResourceTest, GetDefaultACLTests)
146 // Read default ACL from the file
147 char *jsonStr = ReadFile(DEFAULT_ACL_JSON_FILE_NAME);
150 OicSecAcl_t * acl = JSONToAclBin(jsonStr);
151 EXPECT_TRUE(NULL != acl);
153 // Invoke API to generate default ACL
154 OicSecAcl_t * defaultAcl = NULL;
155 OCStackResult ret = GetDefaultACL(&defaultAcl);
156 EXPECT_TRUE(NULL == defaultAcl);
158 EXPECT_TRUE(OC_STACK_ERROR == ret);
160 // Verify if the SRM generated default ACL matches with unit test default
161 if (acl && defaultAcl)
163 EXPECT_TRUE(memcmp(&(acl->subject), &(defaultAcl->subject), sizeof(OicUuid_t)) == 0);
164 EXPECT_EQ(acl->resourcesLen, defaultAcl->resourcesLen);
165 for (size_t i = 0; i < acl->resourcesLen; i++)
167 EXPECT_EQ(strlen(acl->resources[i]), strlen(defaultAcl->resources[i]));
169 memcmp(acl->resources[i], defaultAcl->resources[i],
170 strlen(acl->resources[i])) == 0);
172 EXPECT_EQ(acl->permission, defaultAcl->permission);
177 DeleteACLList(defaultAcl);
184 TEST(ACLResourceTest, ACLPostTest)
186 OCEntityHandlerRequest ehReq = {};
188 // Read an ACL from the file
189 char *jsonStr = ReadFile(ACL1_JSON_FILE_NAME);
192 static OCPersistentStorage ps =
194 SetPersistentHandler(&ps, true);
196 // Create Entity Handler POST request payload
197 ehReq.method = OC_REST_POST;
198 ehReq.reqJSONPayload = jsonStr;
200 OCEntityHandlerResult ehRet = ACLEntityHandler(OC_REQUEST_FLAG, &ehReq);
201 EXPECT_TRUE(OC_EH_ERROR == ehRet);
203 // Convert JSON into OicSecAcl_t for verification
204 OicSecAcl_t * acl = JSONToAclBin(jsonStr);
205 EXPECT_TRUE(NULL != acl);
207 // Verify if SRM contains ACL for the subject
208 OicSecAcl_t* savePtr = NULL;
209 const OicSecAcl_t* subjectAcl = GetACLResourceData(&(acl->subject), &savePtr);
210 EXPECT_TRUE(NULL != subjectAcl);
220 // GetACLResource tests
221 TEST(ACLResourceTest, GetACLResourceTests)
223 // gAcl is a pointer to the the global ACL used by SRM
224 extern OicSecAcl_t *gAcl;
226 // Read an ACL from the file
227 char *jsonStr = ReadFile(ACL1_JSON_FILE_NAME);
230 gAcl = JSONToAclBin(jsonStr);
231 EXPECT_TRUE(NULL != gAcl);
233 // Verify that ACL file contains 2 ACE entries for 'WILDCARD' subject
234 const OicSecAcl_t* acl = NULL;
235 OicSecAcl_t* savePtr = NULL;
236 OicUuid_t subject = WILDCARD_SUBJECT_ID;
241 acl = GetACLResourceData(&subject, &savePtr);
242 count = (NULL != acl) ? count + 1 : count;
243 } while (acl != NULL);
245 EXPECT_EQ(count, NUM_ACE_FOR_WILDCARD_IN_ACL1_JSON);
247 /* Perform cleanup */