1 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
3 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
19 #include "gtest/gtest.h"
21 #include "resourcemanager.h"
22 #include "securevirtualresourcetypes.h"
23 #include "srmresourcestrings.h"
24 #include "doxmresource.h"
25 #include "ocserverrequest.h"
26 #include "oic_string.h"
27 #include "oic_malloc.h"
30 #define TAG PCF("SRM-DOXM")
37 //Declare Doxm resource methods for testing
38 OCStackResult CreateDoxmResource();
39 OCEntityHandlerResult DoxmEntityHandler (OCEntityHandlerFlag flag,
40 OCEntityHandlerRequest * ehRequest);
41 char * BinToDoxmJSON(const OicSecDoxm_t * doxm);
42 OicSecDoxm_t * JSONToDoxmBin(const char * jsonStr);
43 void InitSecDoxmInstance(OicSecDoxm_t * doxm);
44 OCEntityHandlerResult HandleDoxmPostRequest (const OCEntityHandlerRequest * ehRequest);
45 void DeleteDoxmBinData(OicSecDoxm_t* doxm);
46 OCEntityHandlerResult HandleDoxmGetRequest (const OCEntityHandlerRequest * ehRequest);
52 OicSecDoxm_t * getBinDoxm()
54 OicSecDoxm_t * doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
60 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
66 doxm->oxmType[0] = (char*)OICMalloc(strlen(OXM_JUST_WORKS) + 1);
69 OICFree(doxm->oxmType);
74 strcpy(doxm->oxmType[0], OXM_JUST_WORKS);
76 doxm->oxm = (OicSecOxm_t *)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
79 OICFree(doxm->oxmType[0]);
80 OICFree(doxm->oxmType);
85 doxm->oxm[0] = OIC_JUST_WORKS;
86 doxm->oxmSel = OIC_JUST_WORKS;
87 doxm->sct = SYMMETRIC_PAIR_WISE_KEY;
89 //TODO: Need more clarification on deviceIDFormat field type.
90 //doxm.deviceIDFormat = URN;
91 strcpy((char *) doxm->deviceID.id, "deviceId");
92 strcpy((char *)doxm->owner.id, "ownersId");
96 //InitDoxmResource Tests
97 TEST(InitDoxmResourceTest, InitDoxmResource)
99 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
102 //DeInitDoxmResource Tests
103 TEST(DeInitDoxmResourceTest, DeInitDoxmResource)
105 EXPECT_EQ(OC_STACK_ERROR, DeInitDoxmResource());
108 //CreateDoxmResource Tests
109 TEST(CreateDoxmResourceTest, CreateDoxmResource)
111 EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateDoxmResource());
114 //DoxmEntityHandler Tests
115 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithDummyRequest)
117 OCEntityHandlerRequest req;
118 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
121 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithNULLRequest)
123 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
126 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerInvalidFlag)
128 OCEntityHandlerRequest req;
129 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
132 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerValidRequest)
134 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
135 char query[] = "oxm=0;owned=false;owner=owner1";
136 OCEntityHandlerRequest req = OCEntityHandlerRequest();
137 req.method = OC_REST_GET;
138 req.query = OICStrdup(query);
139 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
144 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerDeviceIdQuery)
146 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
147 char query[] = "deviceid=MjIyMjIyMjIyMjIyMjIyMg==";
148 OCEntityHandlerRequest req = OCEntityHandlerRequest();
149 req.method = OC_REST_GET;
150 req.query = OICStrdup(query);
151 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
156 //BinToDoxmJSON Tests
157 TEST(BinToDoxmJSONTest, BinToDoxmJSONNullDoxm)
159 char* value = BinToDoxmJSON(NULL);
160 EXPECT_TRUE(value == NULL);
163 TEST(BinToDoxmJSONTest, BinToDoxmJSONValidDoxm)
165 OicSecDoxm_t * doxm = getBinDoxm();
167 char * json = BinToDoxmJSON(doxm);
168 OC_LOG_V(INFO, TAG, PCF("BinToDoxmJSON:%s"), json);
169 EXPECT_TRUE(json != NULL);
171 DeleteDoxmBinData(doxm);
175 //JSONToDoxmBin Tests
176 TEST(JSONToDoxmBinTest, JSONToDoxmBinValidJSON)
178 OicSecDoxm_t * doxm1 = getBinDoxm();
179 char * json = BinToDoxmJSON(doxm1);
180 EXPECT_TRUE(json != NULL);
182 OicSecDoxm_t *doxm2 = JSONToDoxmBin(json);
183 EXPECT_TRUE(doxm2 != NULL);
185 DeleteDoxmBinData(doxm1);
186 DeleteDoxmBinData(doxm2);
190 TEST(JSONToDoxmBinTest, JSONToDoxmBinNullJSON)
192 OicSecDoxm_t *doxm = JSONToDoxmBin(NULL);
193 EXPECT_TRUE(doxm == NULL);
197 //HandleDoxmPostRequest Test
198 TEST(HandleDoxmPostRequestTest, HandleDoxmPostRequestValidInput)
200 OCEntityHandlerRequest ehRequest = {};
201 OCServerRequest svRequest = {};
203 OicSecDoxm_t * doxm = getBinDoxm();
205 strcpy(svRequest.addressInfo.IP.ipAddress, "10.10.10.10");
206 svRequest.addressInfo.IP.port = 2345;
207 svRequest.connectivityType = CA_ETHERNET;
209 ehRequest.reqJSONPayload = (unsigned char *) BinToDoxmJSON(doxm);
210 ehRequest.requestHandle = (OCRequestHandle) &svRequest;
212 EXPECT_EQ(OC_EH_ERROR, HandleDoxmPostRequest(&ehRequest));
213 DeleteDoxmBinData(doxm);
214 OICFree(ehRequest.reqJSONPayload);