1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "gtest/gtest.h"
23 #include "resourcemanager.h"
24 #include "pstatresource.h"
25 #include "oic_malloc.h"
28 #include "cainterface.h"
29 #include "secureresourcemanager.h"
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 const char* UNIT_TEST_JSON_FILE_NAME = "oic_unittest.json";
46 //InitPstatResource Tests
47 TEST(InitPstatResourceTest, InitPstatResource)
49 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitPstatResource());
53 //DeInitPstatResource Tests
54 TEST(DeInitPstatResourceTest, DeInitPstatResource)
56 EXPECT_EQ(OC_STACK_INVALID_PARAM, DeInitPstatResource());
59 //CreatePstatResource Tests
60 TEST(CreatePstatResourceTest, CreatePstatResource)
62 EXPECT_EQ(OC_STACK_INVALID_PARAM, CreatePstatResource());
65 //PstatEntityHandler Tests
66 TEST(PstatEntityHandlerTest, PstatEntityHandlerWithDummyRequest)
68 OCEntityHandlerRequest req;
69 EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
72 TEST(PstatEntityHandlerTest, PstatEntityHandlerWithPostRequest)
74 OCEntityHandlerRequest req;
75 req.method = OC_REST_POST;
76 req.payload = (OCPayload*)calloc(1, sizeof(OCSecurityPayload));
77 req.payload->type = PAYLOAD_TYPE_SECURITY;
78 ((OCSecurityPayload*)req.payload)->securityData =
79 (char*)"{ \"pstat\": { \"tm\": 0, \"om\": 3 }}";
80 EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
83 TEST(PstatEntityHandlerTest, PstatEntityHandlerInvalidRequest)
85 EXPECT_EQ(OC_EH_ERROR, PstatEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, NULL));
89 TEST(BinToJSONTest, BinToNullJSON)
91 char* value = BinToPstatJSON(NULL);
92 EXPECT_TRUE(value == NULL);
95 TEST(JSONToBinTest, NullJSONToBin)
97 OicSecPstat_t *pstat1 = JSONToPstatBin(NULL);
98 EXPECT_TRUE(pstat1 == NULL);
101 TEST(MarshalingAndUnMarshalingTest, BinToPstatJSONAndJSONToPstatBin)
103 const char* id = "ZGV2aWNlaWQAAAAAABhanw==";
106 pstat.commitHash = 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);
113 pstat.om = SINGLE_SERVICE_CLIENT_DRIVEN;
115 pstat.sm = (OicSecDpom_t*)OICCalloc(pstat.smLen, sizeof(OicSecDpom_t));
116 pstat.sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN;
117 pstat.sm[1] = SINGLE_SERVICE_SERVER_DRIVEN;
118 char* jsonPstat = BinToPstatJSON(&pstat);
119 printf("BinToJSON Dump:\n%s\n\n", jsonPstat);
120 EXPECT_TRUE(jsonPstat != NULL);
121 OicSecPstat_t *pstat1 = JSONToPstatBin(jsonPstat);
122 EXPECT_TRUE(pstat1 != NULL);
129 TEST(PstatTests, JSONMarshalliingTests)
131 char *jsonStr1 = ReadFile(UNIT_TEST_JSON_FILE_NAME);
132 if (NULL != jsonStr1)
134 cJSON_Minify(jsonStr1);
135 /* Workaround : cJSON_Minify does not remove all the unwanted characters
136 from the end. Here is an attempt to remove those characters */
137 int len = strlen(jsonStr1);
140 if (jsonStr1[--len] == '}')
145 jsonStr1[len + 1] = 0;
147 OicSecPstat_t* pstat = JSONToPstatBin(jsonStr1);
148 EXPECT_TRUE(NULL != pstat);
150 char* jsonStr2 = BinToPstatJSON(pstat);
151 printf("BinToPstatJSON Dump:\n%s\n\n", jsonStr2);
152 EXPECT_STRNE(jsonStr1, jsonStr2);
160 printf("Please copy %s into unittest folder\n", UNIT_TEST_JSON_FILE_NAME);