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;
88 //TODO: Need more clarification on deviceIDFormat field type.
89 //doxm.deviceIDFormat = URN;
90 strcpy((char *) doxm->deviceID.id, "deviceId");
91 strcpy((char *)doxm->owner.id, "ownersId");
95 //InitDoxmResource Tests
96 TEST(InitDoxmResourceTest, InitDoxmResource)
98 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
101 //DeInitDoxmResource Tests
102 TEST(DeInitDoxmResourceTest, DeInitDoxmResource)
104 EXPECT_EQ(OC_STACK_ERROR, DeInitDoxmResource());
107 //CreateDoxmResource Tests
108 TEST(CreateDoxmResourceTest, CreateDoxmResource)
110 EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateDoxmResource());
113 //DoxmEntityHandler Tests
114 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithDummyRequest)
116 OCEntityHandlerRequest req;
117 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
120 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithNULLRequest)
122 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
125 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerInvalidFlag)
127 OCEntityHandlerRequest req;
128 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
131 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerValidRequest)
133 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
134 char query[] = "oxm=0;owned=false;owner=owner1";
135 OCEntityHandlerRequest req = OCEntityHandlerRequest();
136 req.method = OC_REST_GET;
137 req.query = OICStrdup(query);
138 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
143 //BinToDoxmJSON Tests
144 TEST(BinToDoxmJSONTest, BinToDoxmJSONNullDoxm)
146 char* value = BinToDoxmJSON(NULL);
147 EXPECT_TRUE(value == NULL);
150 TEST(BinToDoxmJSONTest, BinToDoxmJSONValidDoxm)
152 OicSecDoxm_t * doxm = getBinDoxm();
154 char * json = BinToDoxmJSON(doxm);
155 OC_LOG_V(INFO, TAG, PCF("BinToDoxmJSON:%s"), json);
156 EXPECT_TRUE(json != NULL);
158 DeleteDoxmBinData(doxm);
162 //JSONToDoxmBin Tests
163 TEST(JSONToDoxmBinTest, JSONToDoxmBinValidJSON)
165 OicSecDoxm_t * doxm1 = getBinDoxm();
166 char * json = BinToDoxmJSON(doxm1);
167 EXPECT_TRUE(json != NULL);
169 OicSecDoxm_t *doxm2 = JSONToDoxmBin(json);
170 EXPECT_TRUE(doxm2 != NULL);
172 DeleteDoxmBinData(doxm1);
173 DeleteDoxmBinData(doxm2);
177 TEST(JSONToDoxmBinTest, JSONToDoxmBinNullJSON)
179 OicSecDoxm_t *doxm = JSONToDoxmBin(NULL);
180 EXPECT_TRUE(doxm == NULL);
184 //HandleDoxmPostRequest Test
185 TEST(HandleDoxmPostRequestTest, HandleDoxmPostRequestValidInput)
187 OCEntityHandlerRequest ehRequest = {};
188 OCServerRequest svRequest = {};
190 OicSecDoxm_t * doxm = getBinDoxm();
192 strcpy(svRequest.addressInfo.IP.ipAddress, "10.10.10.10");
193 svRequest.addressInfo.IP.port = 2345;
194 svRequest.connectivityType = CA_ETHERNET;
196 ehRequest.reqJSONPayload = (unsigned char *) BinToDoxmJSON(doxm);
197 ehRequest.requestHandle = (OCRequestHandle) &svRequest;
199 EXPECT_EQ(OC_EH_ERROR, HandleDoxmPostRequest(&ehRequest));
200 DeleteDoxmBinData(doxm);
201 OICFree(ehRequest.reqJSONPayload);