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_malloc.h"
32 //Declare Doxm resource methods for testing
33 OCStackResult CreateDoxmResource();
34 OCEntityHandlerResult DoxmEntityHandler (OCEntityHandlerFlag flag,
35 OCEntityHandlerRequest * ehRequest);
36 char * BinToDoxmJSON(const OicSecDoxm_t * doxm);
37 OicSecDoxm_t * JSONToDoxmBin(const char * jsonStr);
38 void InitSecDoxmInstance(OicSecDoxm_t * doxm);
39 OCEntityHandlerResult HandleDoxmPostRequest (const OCEntityHandlerRequest * ehRequest);
40 void DeleteDoxmBinData(OicSecDoxm_t* doxm);
41 OCEntityHandlerResult HandleDoxmGetRequest (const OCEntityHandlerRequest * ehRequest);
46 OicSecDoxm_t * getBinDoxm()
48 OicSecDoxm_t * doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
50 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
51 doxm->oxmType[0] = (char*)OICMalloc(strlen(OXM_JUST_WORKS) + 1);
52 strcpy(doxm->oxmType[0], OXM_JUST_WORKS);
54 doxm->oxm = (OicSecOxm_t *)OICCalloc(doxm->oxmLen, sizeof(short));
55 doxm->oxm[0] = OIC_JUST_WORKS;
56 doxm->oxmSel = OIC_JUST_WORKS;
58 //TODO: Need more clarification on deviceIDFormat field type.
59 //doxm.deviceIDFormat = URN;
60 strcpy((char *) doxm->deviceID.id, "deviceId");
61 strcpy((char *)doxm->owner.id, "ownersId");
65 //InitDoxmResource Tests
66 TEST(InitDoxmResourceTest, InitDoxmResource)
68 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
71 //DeInitDoxmResource Tests
72 TEST(DeInitDoxmResourceTest, DeInitDoxmResource)
74 EXPECT_EQ(OC_STACK_ERROR, DeInitDoxmResource());
77 //CreateDoxmResource Tests
78 TEST(CreateDoxmResourceTest, CreateDoxmResource)
80 EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateDoxmResource());
83 //DoxmEntityHandler Tests
84 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithDummyRequest)
86 OCEntityHandlerRequest req;
87 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
90 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithNULLRequest)
92 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
95 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerInvalidFlag)
97 OCEntityHandlerRequest req;
98 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
101 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerValidRequest)
103 EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
104 char query[] = "oxm=0&owned=false&owner=owner1";
105 OCEntityHandlerRequest req = {};
106 req.method = OC_REST_GET;
107 req.query = (char*)OICMalloc(strlen(query) + 1);
108 strcpy((char *)req.query, query);
109 EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
114 //BinToDoxmJSON Tests
115 TEST(BinToDoxmJSONTest, BinToDoxmJSONNullDoxm)
117 char* value = BinToDoxmJSON(NULL);
118 EXPECT_TRUE(value == NULL);
121 TEST(BinToDoxmJSONTest, BinToDoxmJSONValidDoxm)
123 OicSecDoxm_t * doxm = getBinDoxm();
125 char * json = BinToDoxmJSON(doxm);
126 printf("BinToDoxmJSON:%s\n", json);
127 EXPECT_TRUE(json != NULL);
129 DeleteDoxmBinData(doxm);
133 //JSONToDoxmBin Tests
134 TEST(JSONToDoxmBinTest, JSONToDoxmBinValidJSON)
136 OicSecDoxm_t * doxm1 = getBinDoxm();
137 char * json = BinToDoxmJSON(doxm1);
138 EXPECT_TRUE(json != NULL);
140 OicSecDoxm_t *doxm2 = JSONToDoxmBin(json);
141 EXPECT_TRUE(doxm2 != NULL);
143 DeleteDoxmBinData(doxm1);
147 TEST(JSONToDoxmBinTest, JSONToDoxmBinNullJSON)
149 OicSecDoxm_t *doxm = JSONToDoxmBin(NULL);
150 EXPECT_TRUE(doxm == NULL);
154 //HandleDoxmPostRequest Test
155 TEST(HandleDoxmPostRequestTest, HandleDoxmPostRequestValidInput)
157 OCEntityHandlerRequest ehRequest = {};
158 OCServerRequest svRequest = {};
160 OicSecDoxm_t * doxm = getBinDoxm();
162 strcpy(svRequest.addressInfo.IP.ipAddress, "10.10.10.10");
163 svRequest.addressInfo.IP.port = 2345;
164 svRequest.connectivityType = CA_ETHERNET;
166 ehRequest.reqJSONPayload = (unsigned char *) BinToDoxmJSON(doxm);
167 ehRequest.requestHandle = (OCRequestHandle) &svRequest;
169 EXPECT_EQ(OC_EH_ERROR, HandleDoxmPostRequest(&ehRequest));
170 DeleteDoxmBinData(doxm);
171 OICFree(ehRequest.reqJSONPayload);