[IOT-1873] Implement OCF Security CR50
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / amaclresourcetest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "gtest/gtest.h"
22 #include "cainterface.h"
23 #include "ocstack.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"
35
36 using namespace std;
37
38 #define TAG  "SRM-AMACL-UT"
39
40 TEST(AMACLResourceTest, CBORAMACLConversion)
41 {
42     OicSecAmacl_t *secAmacl = (OicSecAmacl_t *) OICCalloc(1, sizeof(*secAmacl));
43     ASSERT_TRUE(NULL != secAmacl);
44
45     const char *rsrc[] = { "/a/led", "/a/fan"};
46     secAmacl->resourcesLen = 2;
47     secAmacl->resources = (char **)OICCalloc(secAmacl->resourcesLen,
48                           sizeof(*secAmacl->resources));
49     if (!secAmacl->resources)
50     {
51         DeleteAmaclList(secAmacl);
52     }
53     ASSERT_TRUE(NULL != secAmacl->resources);
54     for (size_t i = 0 ; i < secAmacl->resourcesLen; i++)
55     {
56         secAmacl->resources[i] = OICStrdup(rsrc[i]);
57         ASSERT_TRUE(NULL != secAmacl->resources[i]);
58     }
59
60     OicSecAmacl_t *secAmacl1 = (OicSecAmacl_t *) OICCalloc(1, sizeof(*secAmacl1));
61     if (!secAmacl1)
62     {
63         DeleteAmaclList(secAmacl);
64     }
65     ASSERT_TRUE(NULL != secAmacl1);
66
67     const char *rsrc1[] = { "/b/led", "/b/fan"};
68     secAmacl1->resourcesLen = 2;
69     secAmacl1->resources = (char **)OICCalloc(secAmacl1->resourcesLen,
70                             sizeof(*secAmacl1->resources));
71     if (!secAmacl1->resources)
72     {
73         DeleteAmaclList(secAmacl);
74         DeleteAmaclList(secAmacl1);
75     }
76     ASSERT_TRUE(NULL != secAmacl1->resources);
77     for (size_t i = 0 ; i < secAmacl1->resourcesLen; i++)
78     {
79         secAmacl1->resources[i] = OICStrdup(rsrc1[i]);
80         ASSERT_TRUE(NULL != secAmacl1->resources[i]);
81     }
82     secAmacl1->next = NULL;
83     secAmacl->next = secAmacl1;
84
85     size_t size = 0;
86     uint8_t *psStorage = NULL;
87     EXPECT_EQ(OC_STACK_OK, AmaclToCBORPayload(secAmacl, &psStorage, &size));
88     if (!psStorage)
89     {
90         DeleteAmaclList(secAmacl);
91     }
92     ASSERT_TRUE(NULL != psStorage);
93
94     OicSecAmacl_t *amacl = NULL;
95     EXPECT_EQ(OC_STACK_OK, CBORPayloadToAmacl(psStorage, size, &amacl));
96     if (!amacl)
97     {
98         DeleteAmaclList(secAmacl);
99         OICFree(psStorage);
100     }
101     ASSERT_TRUE(NULL != amacl);
102
103     EXPECT_STREQ(secAmacl->resources[0], amacl->resources[0]);
104     EXPECT_STREQ(secAmacl->resources[1], amacl->resources[1]);
105     EXPECT_EQ(secAmacl->resourcesLen, amacl->resourcesLen);
106
107     DeleteAmaclList(secAmacl);
108     DeleteAmaclList(amacl);
109     OICFree(psStorage);
110 }