Security CBOR conversion
[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     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));
50     if (!secAmacl->amss)
51     {
52         DeleteAmaclList(secAmacl);
53     }
54     ASSERT_TRUE(NULL != secAmacl->amss);
55     memcpy(secAmacl->amss[0].id, amss, sizeof(amss));
56     memcpy(secAmacl->amss[1].id, amss1, sizeof(amss1));
57
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)
63     {
64         DeleteAmaclList(secAmacl);
65     }
66     ASSERT_TRUE(NULL != secAmacl->resources);
67     for (size_t i = 0 ; i < secAmacl->resourcesLen; i++)
68     {
69         secAmacl->resources[i] = OICStrdup(rsrc[i]);
70         ASSERT_TRUE(NULL != secAmacl->resources[i]);
71     }
72     secAmacl->ownersLen = 1;
73     uint8_t ownrs[] = {0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32,
74         0x32, 0x32, 0x32, 0x32, 0x32, 0x32};
75     secAmacl->owners = (OicUuid_t *)OICCalloc(1, sizeof(*secAmacl->owners));
76     if (!secAmacl->owners)
77     {
78         DeleteAmaclList(secAmacl);
79     }
80     ASSERT_TRUE(NULL != secAmacl->owners);
81     memcpy(secAmacl->owners[0].id, ownrs, sizeof(ownrs));
82
83     OicSecAmacl_t *secAmacl1 = (OicSecAmacl_t *) OICCalloc(1, sizeof(*secAmacl1));
84     if (!secAmacl1)
85     {
86         DeleteAmaclList(secAmacl);
87     }
88     ASSERT_TRUE(NULL != secAmacl1);
89     secAmacl1->amssLen = 2;
90     secAmacl1->amss = (OicUuid_t *)OICCalloc(2, sizeof(*secAmacl1->amss));
91     if (!secAmacl1->amss)
92     {
93         DeleteAmaclList(secAmacl);
94         DeleteAmaclList(secAmacl1);
95     }
96     ASSERT_TRUE(NULL != secAmacl1->amss);
97     memcpy(secAmacl1->amss[0].id, amss, sizeof(amss));
98     memcpy(secAmacl1->amss[1].id, amss1, sizeof(amss1));
99
100     const char *rsrc1[] = { "/b/led", "/b/fan"};
101     secAmacl1->resourcesLen = 2;
102     secAmacl1->resources = (char **)OICCalloc(secAmacl1->resourcesLen,
103                             sizeof(*secAmacl1->resources));
104     if (!secAmacl1->resources)
105     {
106         DeleteAmaclList(secAmacl);
107         DeleteAmaclList(secAmacl1);
108     }
109     ASSERT_TRUE(NULL != secAmacl1->resources);
110     for (size_t i = 0 ; i < secAmacl1->resourcesLen; i++)
111     {
112         secAmacl1->resources[i] = OICStrdup(rsrc1[i]);
113         ASSERT_TRUE(NULL != secAmacl1->resources[i]);
114     }
115     secAmacl1->ownersLen = 1;
116     secAmacl1->owners = (OicUuid_t *)OICCalloc(1, sizeof(*secAmacl1->owners));
117     if (!secAmacl1->owners)
118     {
119         DeleteAmaclList(secAmacl);
120         DeleteAmaclList(secAmacl1);
121     }
122     ASSERT_TRUE(NULL != secAmacl1->owners);
123     memcpy(secAmacl1->owners[0].id, ownrs, sizeof(ownrs));
124     secAmacl1->next = NULL;
125     secAmacl->next = secAmacl1;
126
127     size_t size = 0;
128     uint8_t *psStorage = NULL;
129     EXPECT_EQ(OC_STACK_OK, AmaclToCBORPayload(secAmacl, &psStorage, &size));
130     if (!psStorage)
131     {
132         DeleteAmaclList(secAmacl);
133     }
134     ASSERT_TRUE(NULL != psStorage);
135
136     OicSecAmacl_t *amacl = NULL;
137     EXPECT_EQ(OC_STACK_OK, CBORPayloadToAmacl(psStorage, size, &amacl));
138     if (!amacl)
139     {
140         DeleteAmaclList(secAmacl);
141         OICFree(psStorage);
142     }
143     ASSERT_TRUE(NULL != amacl);
144
145     EXPECT_EQ(secAmacl->amssLen, amacl->amssLen);
146     EXPECT_EQ(sizeof(secAmacl->amss[0].id), sizeof(amacl->amss[0].id));
147     EXPECT_EQ(sizeof(secAmacl->amss[1].id), sizeof(amacl->amss[1].id));
148     EXPECT_STREQ(secAmacl->resources[0], amacl->resources[0]);
149     EXPECT_STREQ(secAmacl->resources[1], amacl->resources[1]);
150     EXPECT_EQ(secAmacl->resourcesLen, amacl->resourcesLen);
151     EXPECT_EQ(secAmacl->ownersLen, amacl->ownersLen);
152     EXPECT_EQ(*secAmacl->owners[0].id, *amacl->owners[0].id);
153
154     EXPECT_EQ(secAmacl->next->amssLen, amacl->next->amssLen);
155     EXPECT_EQ(sizeof(secAmacl->next->amss[0].id), sizeof(amacl->next->amss[0].id));
156     EXPECT_STREQ(secAmacl->next->resources[0], amacl->next->resources[0]);
157     EXPECT_STREQ(secAmacl->next->resources[1], amacl->next->resources[1]);
158     EXPECT_EQ(secAmacl->next->resourcesLen, amacl->next->resourcesLen);
159     EXPECT_EQ(secAmacl->next->ownersLen, amacl->next->ownersLen);
160     EXPECT_EQ(*secAmacl->next->owners[0].id, *amacl->next->owners[0].id);
161
162     DeleteAmaclList(secAmacl);
163     DeleteAmaclList(amacl);
164     OICFree(psStorage);
165 }