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 EXPECT_STREQ(jsonStr1, jsonStr2);
141 TEST(ACLResourceTest, GetDefaultACLTests)
143 // Read default ACL from the file
144 char *jsonStr = ReadFile(DEFAULT_ACL_JSON_FILE_NAME);
147 OicSecAcl_t * acl = JSONToAclBin(jsonStr);
148 EXPECT_TRUE(NULL != acl);
150 // Invoke API to generate default ACL
151 OicSecAcl_t * defaultAcl = NULL;
152 OCStackResult ret = GetDefaultACL(&defaultAcl);
153 EXPECT_TRUE(NULL == defaultAcl);
155 EXPECT_TRUE(OC_STACK_ERROR == ret);
157 // Verify if the SRM generated default ACL matches with unit test default
158 if (acl && defaultAcl)
160 EXPECT_TRUE(memcmp(&(acl->subject), &(defaultAcl->subject), sizeof(OicUuid_t)) == 0);
161 EXPECT_EQ(acl->resourcesLen, defaultAcl->resourcesLen);
162 for (size_t i = 0; i < acl->resourcesLen; i++)
164 EXPECT_EQ(strlen(acl->resources[i]), strlen(defaultAcl->resources[i]));
166 memcmp(acl->resources[i], defaultAcl->resources[i],
167 strlen(acl->resources[i])) == 0);
169 EXPECT_EQ(acl->permission, defaultAcl->permission);
174 DeleteACLList(defaultAcl);
181 TEST(ACLResourceTest, ACLPostTest)
183 OCEntityHandlerRequest ehReq = {};
185 // Read an ACL from the file
186 char *jsonStr = ReadFile(ACL1_JSON_FILE_NAME);
189 static OCPersistentStorage ps =
191 SetPersistentHandler(&ps, true);
193 // Create Entity Handler POST request payload
194 ehReq.method = OC_REST_POST;
195 ehReq.payload = (OCPayload*)calloc(1, sizeof(OCSecurityPayload));
196 ehReq.payload->type = PAYLOAD_TYPE_SECURITY;
197 ((OCSecurityPayload*)ehReq.payload)->securityData = jsonStr;
199 OCEntityHandlerResult ehRet = ACLEntityHandler(OC_REQUEST_FLAG, &ehReq);
200 EXPECT_TRUE(OC_EH_ERROR == ehRet);
202 // Convert JSON into OicSecAcl_t for verification
203 OicSecAcl_t * acl = JSONToAclBin(jsonStr);
204 EXPECT_TRUE(NULL != acl);
206 // Verify if SRM contains ACL for the subject
207 OicSecAcl_t* savePtr = NULL;
208 const OicSecAcl_t* subjectAcl = GetACLResourceData(&(acl->subject), &savePtr);
209 EXPECT_TRUE(NULL != subjectAcl);
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 */