Revert back cbor related patches.
[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 "gtest/gtest.h"
22 #include "ocstack.h"
23 #include "resourcemanager.h"
24 #include "pstatresource.h"
25 #include "oic_malloc.h"
26 #include "cJSON.h"
27 #include "base64.h"
28 #include "cainterface.h"
29 #include "secureresourcemanager.h"
30 #include "srmtestcommon.h"
31 #include "ocpayload.h"
32 #include <unistd.h>
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 //Declare Provision status resource methods for testing
37 OCStackResult CreatePstatResource();
38 OCEntityHandlerResult PstatEntityHandler (OCEntityHandlerFlag flag,
39                                         OCEntityHandlerRequest * ehRequest);
40 char * BinToPstatJSON(const OicSecPstat_t * pstat);
41 OicSecPstat_t * JSONToPstatBin(const char * jsonStr);
42 const char* UNIT_TEST_JSON_FILE_NAME = "oic_unittest.json";
43 #ifdef __cplusplus
44 }
45 #endif
46
47 //InitPstatResource Tests
48 TEST(InitPstatResourceTest, InitPstatResource)
49 {
50     EXPECT_EQ(OC_STACK_INVALID_PARAM,  InitPstatResource());
51 }
52
53
54 //DeInitPstatResource Tests
55 TEST(DeInitPstatResourceTest, DeInitPstatResource)
56 {
57     EXPECT_EQ(OC_STACK_INVALID_PARAM, DeInitPstatResource());
58 }
59
60 //CreatePstatResource Tests
61 TEST(CreatePstatResourceTest, CreatePstatResource)
62 {
63     EXPECT_EQ(OC_STACK_INVALID_PARAM,  CreatePstatResource());
64 }
65
66 //PstatEntityHandler Tests
67 TEST(PstatEntityHandlerTest, PstatEntityHandlerWithDummyRequest)
68 {
69     OCEntityHandlerRequest req;
70     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
71 }
72
73 TEST(PstatEntityHandlerTest, PstatEntityHandlerWithPostRequest)
74 {
75     OCEntityHandlerRequest req;
76     req.method = OC_REST_POST;
77     req.payload = reinterpret_cast<OCPayload*>(
78             OCSecurityPayloadCreate("{ \"pstat\": { \"tm\": 0, \"om\": 3 }}"));
79     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
80     OCPayloadDestroy(req.payload);
81 }
82
83 TEST(PstatEntityHandlerTest, PstatEntityHandlerInvalidRequest)
84 {
85     EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, NULL));
86 }
87
88 //BinToJSON Tests
89 TEST(BinToJSONTest, BinToNullJSON)
90 {
91     char* value = BinToPstatJSON(NULL);
92     EXPECT_TRUE(value == NULL);
93 }
94
95 TEST(JSONToBinTest, NullJSONToBin)
96 {
97     OicSecPstat_t *pstat1 = JSONToPstatBin(NULL);
98     EXPECT_TRUE(pstat1 == NULL);
99 }
100
101 TEST(MarshalingAndUnMarshalingTest, BinToPstatJSONAndJSONToPstatBin)
102 {
103     const char* id = "ZGV2aWNlaWQAAAAAABhanw==";
104     OicSecPstat_t pstat;
105     pstat.cm = NORMAL;
106     pstat.commitHash = 0;
107     uint32_t outLen = 0;
108     unsigned char base64Buff[sizeof(((OicUuid_t*) 0)->id)] = {};
109     EXPECT_EQ(B64_OK, b64Decode(id, strlen(id), base64Buff, sizeof(base64Buff), &outLen));
110     memcpy(pstat.deviceID.id, base64Buff, outLen);
111     pstat.isOp = true;
112     pstat.tm = NORMAL;
113     pstat.om = SINGLE_SERVICE_CLIENT_DRIVEN;
114     pstat.smLen = 2;
115     pstat.sm = (OicSecDpom_t*)OICCalloc(pstat.smLen, sizeof(OicSecDpom_t));
116     if(!pstat.sm)
117     {
118         FAIL() << "Failed to allocate the pstat.sm";
119     }
120     pstat.sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN;
121     pstat.sm[1] = SINGLE_SERVICE_SERVER_DRIVEN;
122     char* jsonPstat = BinToPstatJSON(&pstat);
123     if(!jsonPstat)
124     {
125         OICFree(pstat.sm);
126         FAIL() << "Failed to convert BinToPstatJSON";
127         return;
128     }
129     printf("BinToJSON Dump:\n%s\n\n", jsonPstat);
130     EXPECT_TRUE(jsonPstat != NULL);
131     OicSecPstat_t *pstat1 = JSONToPstatBin(jsonPstat);
132     EXPECT_TRUE(pstat1 != NULL);
133     if(pstat1)
134     {
135         OICFree(pstat1->sm);
136     }
137     OICFree(pstat1);
138     OICFree(jsonPstat);
139     OICFree(pstat.sm);
140 }
141
142 TEST(PstatTests, JSONMarshalliingTests)
143 {
144     char *jsonStr1 = ReadFile(UNIT_TEST_JSON_FILE_NAME);
145     if (NULL != jsonStr1)
146     {
147         cJSON_Minify(jsonStr1);
148         /* Workaround : cJSON_Minify does not remove all the unwanted characters
149          from the end. Here is an attempt to remove those characters */
150         int len = strlen(jsonStr1);
151         while (len > 0)
152         {
153             if (jsonStr1[--len] == '}')
154             {
155                 break;
156             }
157         }
158         jsonStr1[len + 1] = 0;
159
160         OicSecPstat_t* pstat = JSONToPstatBin(jsonStr1);
161         EXPECT_TRUE(NULL != pstat);
162
163         char* jsonStr2 = BinToPstatJSON(pstat);
164         EXPECT_STRNE(jsonStr1, jsonStr2);
165
166         OICFree(jsonStr1);
167         OICFree(jsonStr2);
168         OICFree(pstat);
169    }
170     else
171     {
172         printf("Please copy %s into unittest folder\n", UNIT_TEST_JSON_FILE_NAME);
173     }
174 }