[IOT-1763] Implemented device onboarding state CR 23
[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 "iotivity_config.h"
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #include "gtest/gtest.h"
26
27 #include "ocpayload.h"
28 #include "ocstack.h"
29 #include "oic_malloc.h"
30 #include "cainterface.h"
31 #include "resourcemanager.h"
32 #include "secureresourcemanager.h"
33 #include "pstatresource.h"
34 #include "security_internals.h"
35
36 // InitPstatResource Tests
37 TEST(PstatResourceTest, InitPstatResource)
38 {
39     EXPECT_EQ(OC_STACK_INVALID_PARAM, InitPstatResource());
40 }
41
42 // DeInitPstatResource Tests
43 TEST(PstatResourceTest, DeInitPstatResource)
44 {
45     EXPECT_EQ(OC_STACK_INVALID_PARAM, DeInitPstatResource());
46 }
47
48 //CreatePstatResource Tests
49 TEST(PstatResourceTest, CreatePstatResource)
50 {
51     EXPECT_EQ(OC_STACK_INVALID_PARAM, CreatePstatResource());
52 }
53
54 //PstatEntityHandler Tests
55 TEST(PstatResourceTest, PstatEntityHandlerWithDummyRequest)
56 {
57     OCEntityHandlerRequest req = OCEntityHandlerRequest();
58     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
59 }
60
61 TEST(PstatResourceTest, PstatEntityHandlerWithPostRequest)
62 {
63     OicSecPstat_t *defaultPstat = (OicSecPstat_t *) OICCalloc(1, sizeof(*defaultPstat));
64     ASSERT_TRUE(defaultPstat != NULL);
65     defaultPstat->dos.state = DOS_RFOTM;
66     defaultPstat->dos.pending = false;
67     defaultPstat->isOp = false;
68     defaultPstat->commitHash = 1234;
69     defaultPstat->cm = 63;
70     defaultPstat->tm = 48;
71     defaultPstat->om = 0;
72     defaultPstat->smLen = 1;
73     defaultPstat->sm = (OicSecDpom_t *)OICCalloc(defaultPstat->smLen, sizeof(*defaultPstat->sm));
74     ASSERT_TRUE(defaultPstat->sm != NULL);
75     defaultPstat->sm[0] = 3;
76     size_t size = 0;
77     uint8_t *cbor = NULL;
78     EXPECT_EQ(OC_STACK_OK, PstatToCBORPayload(defaultPstat, &cbor, &size, true));
79     DeletePstatBinData(defaultPstat);
80     ASSERT_TRUE(cbor != NULL);
81
82     OCEntityHandlerRequest req = OCEntityHandlerRequest();
83     req.method = OC_REST_POST;
84     req.payload = (OCPayload *) OCSecurityPayloadCreate(cbor, size);
85     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
86     OICFree(cbor);
87     OCPayloadDestroy(req.payload);
88 }
89
90 TEST(PstatResourceTest, PstatEntityHandlerInvalidRequest)
91 {
92     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, NULL));
93 }
94
95 TEST(PstatResourceTest, PstatToCBORPayloadNULL)
96 {
97     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(NULL, NULL, 0, false));
98     // Case when cbor payload is NULL
99     OicSecPstat_t pstat;
100     size_t size = 10;
101     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(&pstat, NULL, &size, false));
102     uint8_t *cborPayload = (uint8_t *) OICCalloc(1, size);
103     ASSERT_TRUE(NULL != cborPayload);
104     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(&pstat, &cborPayload, &size, false));
105     OICFree(cborPayload);
106     cborPayload = NULL;
107     // Case when pstat is zero.
108     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(NULL, &cborPayload, &size, false));
109     // Case when size is 0.
110     EXPECT_EQ(OC_STACK_INVALID_PARAM, PstatToCBORPayload(&pstat, &cborPayload, 0, false));
111     OICFree(cborPayload);
112 }
113
114 TEST(PstatResourceTest, CBORPayloadToPstat)
115 {
116     EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToPstat(NULL, 0, NULL));
117 }
118
119 TEST(PstatResourceTest, PstatToCBORPayloadAndCBORPayloadToPstat)
120 {
121     OicSecPstat_t pstat;
122     pstat.dos.state = DOS_RFNOP;
123     pstat.dos.pending = false;
124     pstat.cm = NORMAL;
125     pstat.commitHash = 0;
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, false));
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.dos.state, pstat1->dos.state);
148     EXPECT_EQ(pstat.dos.pending, pstat1->dos.pending);
149     EXPECT_EQ(pstat.commitHash, pstat1->commitHash);
150     EXPECT_EQ(pstat.isOp, pstat1->isOp);
151     EXPECT_EQ(pstat.tm, pstat1->tm);
152     EXPECT_EQ(pstat.om, pstat1->om);
153     EXPECT_EQ(pstat.smLen, pstat1->smLen);
154     EXPECT_EQ(pstat.sm[0], pstat1->sm[0]);
155
156     DeletePstatBinData(pstat1);
157     OICFree(cbor);
158     OICFree(pstat.sm);
159 }