Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / pstatresource.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 <unistd.h>
22 #include "gtest/gtest.h"
23
24 #include "ocpayload.h"
25 #include "ocstack.h"
26 #include "oic_malloc.h"
27 #include "cainterface.h"
28 #include "resourcemanager.h"
29 #include "secureresourcemanager.h"
30 #include "pstatresource.h"
31 #include "security_internals.h"
32
33 // InitPstatResource Tests
34 TEST(PstatResourceTest, InitPstatResource)
35 {
36     EXPECT_EQ(OC_STACK_INVALID_PARAM, InitPstatResource());
37 }
38
39 // DeInitPstatResource Tests
40 TEST(PstatResourceTest, DeInitPstatResource)
41 {
42     EXPECT_EQ(OC_STACK_INVALID_PARAM, DeInitPstatResource());
43 }
44
45 //CreatePstatResource Tests
46 TEST(PstatResourceTest, CreatePstatResource)
47 {
48     EXPECT_EQ(OC_STACK_INVALID_PARAM, CreatePstatResource());
49 }
50
51 //PstatEntityHandler Tests
52 TEST(PstatResourceTest, PstatEntityHandlerWithDummyRequest)
53 {
54     OCEntityHandlerRequest req = OCEntityHandlerRequest();
55     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
56 }
57
58 TEST(PstatResourceTest, PstatEntityHandlerWithPostRequest)
59 {
60     OicSecPstat_t *defaultPstat = (OicSecPstat_t *) OICCalloc(1, sizeof(*defaultPstat));
61     ASSERT_TRUE(defaultPstat != NULL);
62     defaultPstat->isOp = false;
63     uint8_t deviceId[] = {0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x69, 0x64, 0x00,
64                           0x00, 0x00, 0x00, 0x00, 0x18, 0x5a, 0x9f};
65     memcpy(defaultPstat->deviceID.id, deviceId, sizeof(deviceId));
66     EXPECT_EQ(sizeof(defaultPstat->deviceID.id), sizeof(deviceId));
67     defaultPstat->commitHash = 1234;
68     defaultPstat->cm = (OicSecDpm_t) 63;
69     defaultPstat->tm = (OicSecDpm_t) 48;
70     defaultPstat->om = (OicSecDpom_t) 0;
71     defaultPstat->smLen = 1;
72     defaultPstat->sm = (OicSecDpom_t *)OICCalloc(defaultPstat->smLen, sizeof(*defaultPstat->sm));
73     ASSERT_TRUE(defaultPstat->sm != NULL);
74     defaultPstat->sm[0] = (OicSecDpom_t) 3;
75     size_t size = 0;
76     uint8_t *cbor = NULL;
77     EXPECT_EQ(OC_STACK_OK, PstatToCBORPayload(defaultPstat, &cbor, &size));
78     DeletePstatBinData(defaultPstat);
79     ASSERT_TRUE(cbor != NULL);
80
81     OCEntityHandlerRequest req = OCEntityHandlerRequest();
82     req.method = OC_REST_POST;
83     req.payload = (OCPayload *) OCSecurityPayloadCreate(cbor, size);
84     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
85     OICFree(cbor);
86     OCPayloadDestroy(req.payload);
87 }
88
89 TEST(PstatResourceTest, PstatEntityHandlerInvalidRequest)
90 {
91     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, NULL));
92 }
93
94 TEST(PstatResourceTest, PstatToCBORPayloadNULL)
95 {
96     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(NULL, NULL, 0));
97     // Case when cbor payload is NULL
98     OicSecPstat_t pstat;
99     size_t size = 10;
100     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(&pstat, NULL, &size));
101     uint8_t *cborPayload = (uint8_t *) OICCalloc(1, size);
102     ASSERT_TRUE(NULL != cborPayload);
103     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(&pstat, &cborPayload, &size));
104     OICFree(cborPayload);
105     cborPayload = NULL;
106     // Case when pstat is zero.
107     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(NULL, &cborPayload, &size));
108     // Case when size is 0.
109     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(&pstat, &cborPayload, 0));
110     OICFree(cborPayload);
111 }
112
113 TEST(PstatResourceTest, CBORPayloadToPstat)
114 {
115     EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToPstat(NULL, 0, NULL));
116 }
117
118 TEST(PstatResourceTest, PstatToCBORPayloadAndCBORPayloadToPstat)
119 {
120     OicSecPstat_t pstat;
121     pstat.cm = NORMAL;
122     pstat.commitHash = 0;
123     uint8_t deviceId[] = {0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x69, 0x64, 0x00,
124                           0x00, 0x00, 0x00, 0x00, 0x18, 0x5a, 0x9f};
125     memcpy(pstat.deviceID.id, deviceId, sizeof(deviceId));
126     pstat.isOp = true;
127     pstat.tm = NORMAL;
128     pstat.om = SINGLE_SERVICE_CLIENT_DRIVEN;
129     pstat.smLen = 1;
130     pstat.sm = (OicSecDpom_t*)OICCalloc(pstat.smLen, sizeof(*pstat.sm));
131     ASSERT_TRUE(NULL != pstat.sm);
132     pstat.sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN;
133
134     size_t size = 0;
135     uint8_t *cbor = NULL;
136     EXPECT_EQ(OC_STACK_OK, PstatToCBORPayload(&pstat, &cbor, &size));
137     if (!cbor)
138     {
139         OICFree(pstat.sm);
140         FAIL() << "Failed to convert PstatToCBORPayload";
141         return;
142     }
143     ASSERT_TRUE(NULL != cbor);
144     OicSecPstat_t *pstat1 = NULL;
145     EXPECT_EQ(OC_STACK_OK, CBORPayloadToPstat(cbor, size, &pstat1));
146     ASSERT_TRUE(NULL != pstat1);
147     EXPECT_EQ(pstat.commitHash, pstat1->commitHash);
148     EXPECT_EQ(pstat.isOp, pstat1->isOp);
149     EXPECT_EQ(pstat.tm, pstat1->tm);
150     EXPECT_EQ(pstat.om, pstat1->om);
151     EXPECT_EQ(pstat.smLen, pstat1->smLen);
152     EXPECT_EQ(pstat.sm[0], pstat1->sm[0]);
153
154     DeletePstatBinData(pstat1);
155     OICFree(cbor);
156     OICFree(pstat.sm);
157 }