8d4553fdd63b95206f28503da8fbdbfc571e0108
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / svcresourcetest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH 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 "ocstack.h"
23 #include "oic_malloc.h"
24 #include "cainterface.h"
25 #include "secureresourcemanager.h"
26 #include "securevirtualresourcetypes.h"
27 #include "srmresourcestrings.h"
28 #include "svcresource.h"
29 #include "srmtestcommon.h"
30 #include "security_internals.h"
31
32 using namespace std;
33
34 #define NUM_SVC_IN_CBOR_DB (2)
35
36 TEST(SVCResourceTest, CBORConversionTests)
37 {
38     OicSecSvc_t *svc1 = (OicSecSvc_t *) OICCalloc(1, sizeof(*svc1));
39     ASSERT_TRUE(NULL != svc1);
40     uint8_t svcdid[] = {0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35,
41                         0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35};
42     memcpy(svc1->svcdid.id, svcdid, sizeof(svcdid));
43     ASSERT_EQ(sizeof(svc1->svcdid.id), sizeof(svcdid));
44
45     svc1->svct = (OicSecSvcType_t) 1;
46     uint8_t owners[] = {0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39,
47                         0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39};
48     svc1->ownersLen = 1;
49     svc1->owners = (OicUuid_t *)OICCalloc(svc1->ownersLen, sizeof(*svc1->owners));
50     ASSERT_TRUE(NULL != svc1->owners);
51     memcpy(svc1->owners[0].id, owners, sizeof(owners));
52     ASSERT_EQ(sizeof(svc1->owners[0].id), sizeof(owners));
53
54     svc1->next = (OicSecSvc_t *) OICCalloc(1, sizeof(*svc1->next));
55     ASSERT_TRUE(svc1->next != NULL);
56     memcpy(svc1->next->svcdid.id, svcdid, sizeof(svcdid));
57     ASSERT_EQ(sizeof(svc1->next->svcdid.id), sizeof(svcdid));
58     svc1->next->svct = (OicSecSvcType_t) 1;
59     uint8_t owners1[] = {0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
60                         0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36};
61     svc1->next->ownersLen = 1;
62     svc1->next->owners = (OicUuid_t *)OICCalloc(svc1->next->ownersLen,
63                                                 sizeof(*svc1->next->owners));
64     ASSERT_TRUE(NULL != svc1->next->owners);
65     memcpy(svc1->next->owners[0].id, owners1, sizeof(owners1));
66     svc1->next->next = NULL;
67
68     size_t size = 0;
69     uint8_t *psStorage = NULL;
70     EXPECT_EQ(OC_STACK_OK, SVCToCBORPayload(svc1, &psStorage, &size));
71     ASSERT_TRUE(NULL != psStorage);
72
73     OicSecSvc_t *svc = NULL;
74     EXPECT_EQ(OC_STACK_OK, CBORPayloadToSVC(psStorage, size, &svc));
75     ASSERT_TRUE(NULL != svc);
76
77     int cnt = 0;
78     OicSecSvc_t *tempSvc = svc;
79     while (tempSvc)
80     {
81         EXPECT_EQ(ACCESS_MGMT_SERVICE, tempSvc->svct);
82         cnt++;
83         tempSvc = tempSvc->next;
84     }
85     EXPECT_EQ(NUM_SVC_IN_CBOR_DB, cnt);
86
87     OICFree(psStorage);
88     DeleteSVCList(svc);
89     DeleteSVCList(svc1);
90 }