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));
52 DeleteAmaclList(secAmacl);
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 DeleteAmaclList(secAmacl);
66 ASSERT_TRUE(NULL != secAmacl->resources);
67 for (size_t i = 0 ; i < secAmacl->resourcesLen; i++)
69 secAmacl->resources[i] = OICStrdup(rsrc[i]);
70 ASSERT_TRUE(NULL != secAmacl->resources[i]);
72 uint8_t ownrs[] = {0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32,
73 0x32, 0x32, 0x32, 0x32, 0x32, 0x32};
74 memcpy(secAmacl->rownerID.id, ownrs, sizeof(ownrs));
76 OicSecAmacl_t *secAmacl1 = (OicSecAmacl_t *) OICCalloc(1, sizeof(*secAmacl1));
79 DeleteAmaclList(secAmacl);
81 ASSERT_TRUE(NULL != secAmacl1);
82 secAmacl1->amssLen = 2;
83 secAmacl1->amss = (OicUuid_t *)OICCalloc(2, sizeof(*secAmacl1->amss));
86 DeleteAmaclList(secAmacl);
87 DeleteAmaclList(secAmacl1);
89 ASSERT_TRUE(NULL != secAmacl1->amss);
90 memcpy(secAmacl1->amss[0].id, amss, sizeof(amss));
91 memcpy(secAmacl1->amss[1].id, amss1, sizeof(amss1));
93 const char *rsrc1[] = { "/b/led", "/b/fan"};
94 secAmacl1->resourcesLen = 2;
95 secAmacl1->resources = (char **)OICCalloc(secAmacl1->resourcesLen,
96 sizeof(*secAmacl1->resources));
97 if (!secAmacl1->resources)
99 DeleteAmaclList(secAmacl);
100 DeleteAmaclList(secAmacl1);
102 ASSERT_TRUE(NULL != secAmacl1->resources);
103 for (size_t i = 0 ; i < secAmacl1->resourcesLen; i++)
105 secAmacl1->resources[i] = OICStrdup(rsrc1[i]);
106 ASSERT_TRUE(NULL != secAmacl1->resources[i]);
108 memcpy(secAmacl1->rownerID.id, ownrs, sizeof(ownrs));
109 secAmacl1->next = NULL;
110 secAmacl->next = secAmacl1;
113 uint8_t *psStorage = NULL;
114 EXPECT_EQ(OC_STACK_OK, AmaclToCBORPayload(secAmacl, &psStorage, &size));
117 DeleteAmaclList(secAmacl);
119 ASSERT_TRUE(NULL != psStorage);
121 OicSecAmacl_t *amacl = NULL;
122 EXPECT_EQ(OC_STACK_OK, CBORPayloadToAmacl(psStorage, size, &amacl));
125 DeleteAmaclList(secAmacl);
128 ASSERT_TRUE(NULL != amacl);
130 EXPECT_EQ(secAmacl->amssLen, amacl->amssLen);
131 EXPECT_EQ(sizeof(secAmacl->amss[0].id), sizeof(amacl->amss[0].id));
132 EXPECT_EQ(sizeof(secAmacl->amss[1].id), sizeof(amacl->amss[1].id));
133 EXPECT_STREQ(secAmacl->resources[0], amacl->resources[0]);
134 EXPECT_STREQ(secAmacl->resources[1], amacl->resources[1]);
135 EXPECT_EQ(secAmacl->resourcesLen, amacl->resourcesLen);
136 EXPECT_EQ(memcmp(secAmacl->rownerID.id, amacl->rownerID.id, sizeof(amacl->rownerID.id)), 0);
138 DeleteAmaclList(secAmacl);
139 DeleteAmaclList(amacl);