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