1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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"
22 #include "cainterface.h"
24 #include "ocpayload.h"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
27 #include "payload_logging.h"
28 #include "psinterface.h"
29 #include "secureresourcemanager.h"
30 #include "securevirtualresourcetypes.h"
31 #include "srmresourcestrings.h"
32 #include "srmutility.h"
33 #include "amaclresource.h"
34 #include "security_internals.h"
38 #define TAG "SRM-AMACL-UT"
40 TEST(AMACLResourceTest, CBORAMACLConversion)
42 OicSecAmacl_t *secAmacl = (OicSecAmacl_t *) OICCalloc(1, sizeof(*secAmacl));
43 ASSERT_TRUE(NULL != secAmacl);
44 uint8_t amss[] = {0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35,
45 0x35, 0x35, 0x35, 0x35, 0x35, 0x35};
46 uint8_t amss1[] = {0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
47 0x36, 0x36, 0x36, 0x36, 0x36, 0x36};
48 secAmacl->amssLen = 2;
49 secAmacl->amss = (OicUuid_t *)OICCalloc(secAmacl->amssLen, sizeof(*secAmacl->amss));
54 ASSERT_TRUE(NULL != secAmacl->amss);
55 memcpy(secAmacl->amss[0].id, amss, sizeof(amss));
56 memcpy(secAmacl->amss[1].id, amss1, sizeof(amss1));
58 const char *rsrc[] = { "/a/led", "/a/fan"};
59 secAmacl->resourcesLen = 2;
60 secAmacl->resources = (char **)OICCalloc(secAmacl->resourcesLen,
61 sizeof(*secAmacl->resources));
62 if (!secAmacl->resources)
64 OICFree(secAmacl->amss);
67 ASSERT_TRUE(NULL != secAmacl->resources);
68 for (size_t i = 0 ; i < secAmacl->resourcesLen; i++)
70 secAmacl->resources[i] = OICStrdup(rsrc[i]);
71 ASSERT_TRUE(NULL != secAmacl->resources[i]);
73 secAmacl->ownersLen = 1;
74 uint8_t ownrs[] = {0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32,
75 0x32, 0x32, 0x32, 0x32, 0x32, 0x32};
76 secAmacl->owners = (OicUuid_t *)OICCalloc(1, sizeof(*secAmacl->owners));
77 if (!secAmacl->owners)
79 OICFree(secAmacl->resources);
80 OICFree(secAmacl->amss);
83 ASSERT_TRUE(NULL != secAmacl->owners);
84 memcpy(secAmacl->owners[0].id, ownrs, sizeof(ownrs));
86 OicSecAmacl_t *secAmacl1 = (OicSecAmacl_t *) OICCalloc(1, sizeof(*secAmacl1));
89 OICFree(secAmacl->owners);
90 OICFree(secAmacl->resources);
91 OICFree(secAmacl->amss);
94 ASSERT_TRUE(NULL != secAmacl1);
95 secAmacl1->amssLen = 2;
96 secAmacl1->amss = (OicUuid_t *)OICCalloc(2, sizeof(*secAmacl1->amss));
99 OICFree(secAmacl->owners);
100 OICFree(secAmacl->resources);
101 OICFree(secAmacl->amss);
105 ASSERT_TRUE(NULL != secAmacl1->amss);
106 memcpy(secAmacl1->amss[0].id, amss, sizeof(amss));
107 memcpy(secAmacl1->amss[1].id, amss1, sizeof(amss1));
109 const char *rsrc1[] = { "/b/led", "/b/fan"};
110 secAmacl1->resourcesLen = 2;
111 secAmacl1->resources = (char **)OICCalloc(secAmacl1->resourcesLen,
112 sizeof(*secAmacl1->resources));
113 if (!secAmacl1->resources)
115 OICFree(secAmacl->owners);
116 OICFree(secAmacl->resources);
117 OICFree(secAmacl->amss);
119 OICFree(secAmacl1->amss);
122 ASSERT_TRUE(NULL != secAmacl1->resources);
123 for (size_t i = 0 ; i < secAmacl1->resourcesLen; i++)
125 secAmacl1->resources[i] = OICStrdup(rsrc1[i]);
126 ASSERT_TRUE(NULL != secAmacl1->resources[i]);
128 secAmacl1->ownersLen = 1;
129 secAmacl1->owners = (OicUuid_t *)OICCalloc(1, sizeof(*secAmacl1->owners));
130 if (!secAmacl1->owners)
132 OICFree(secAmacl->owners);
133 OICFree(secAmacl->resources);
134 OICFree(secAmacl->amss);
136 OICFree(secAmacl1->resources);
137 OICFree(secAmacl1->amss);
140 ASSERT_TRUE(NULL != secAmacl1->owners);
141 memcpy(secAmacl1->owners[0].id, ownrs, sizeof(ownrs));
142 secAmacl1->next = NULL;
143 secAmacl->next = secAmacl1;
146 uint8_t *psStorage = NULL;
147 EXPECT_EQ(OC_STACK_OK, AmaclToCBORPayload(secAmacl, &psStorage, &size));
150 OICFree(secAmacl->owners);
151 OICFree(secAmacl->resources);
152 OICFree(secAmacl->amss);
154 OICFree(secAmacl1->owners);
155 OICFree(secAmacl1->resources);
156 OICFree(secAmacl1->amss);
159 ASSERT_TRUE(NULL != psStorage);
161 OicSecAmacl_t *amacl = NULL;
162 EXPECT_EQ(OC_STACK_OK, CBORPayloadToAmacl(psStorage, size, &amacl));
165 OICFree(secAmacl->owners);
166 OICFree(secAmacl->resources);
167 OICFree(secAmacl->amss);
169 OICFree(secAmacl1->owners);
170 OICFree(secAmacl1->resources);
171 OICFree(secAmacl1->amss);
175 ASSERT_TRUE(NULL != amacl);
177 EXPECT_EQ(secAmacl->amssLen, amacl->amssLen);
178 EXPECT_EQ(sizeof(secAmacl->amss[0].id), sizeof(amacl->amss[0].id));
179 EXPECT_EQ(sizeof(secAmacl->amss[1].id), sizeof(amacl->amss[1].id));
180 EXPECT_STREQ(secAmacl->resources[0], amacl->resources[0]);
181 EXPECT_STREQ(secAmacl->resources[1], amacl->resources[1]);
182 EXPECT_EQ(secAmacl->resourcesLen, amacl->resourcesLen);
183 EXPECT_EQ(secAmacl->ownersLen, amacl->ownersLen);
184 EXPECT_EQ(*secAmacl->owners[0].id, *amacl->owners[0].id);
186 EXPECT_EQ(secAmacl->next->amssLen, amacl->next->amssLen);
187 EXPECT_EQ(sizeof(secAmacl->next->amss[0].id), sizeof(amacl->next->amss[0].id));
188 EXPECT_STREQ(secAmacl->next->resources[0], amacl->next->resources[0]);
189 EXPECT_STREQ(secAmacl->next->resources[1], amacl->next->resources[1]);
190 EXPECT_EQ(secAmacl->next->resourcesLen, amacl->next->resourcesLen);
191 EXPECT_EQ(secAmacl->next->ownersLen, amacl->next->ownersLen);
192 EXPECT_EQ(*secAmacl->next->owners[0].id, *amacl->next->owners[0].id);
194 OICFree(secAmacl->owners);
195 OICFree(secAmacl->resources);
196 OICFree(secAmacl->amss);
198 OICFree(secAmacl1->owners);
199 OICFree(secAmacl1->resources);
200 OICFree(secAmacl1->amss);