f2de1ddd0e90fde7c03a73e4724a415b2452b31d
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / doxmresource.cpp
1 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
2 //
3 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
18
19 #include "gtest/gtest.h"
20 #include "ocstack.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"
28 #include "logger.h"
29
30 #define TAG  "SRM-DOXM"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36
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);
47 #ifdef __cplusplus
48 }
49 #endif
50
51
52 OicSecDoxm_t * getBinDoxm()
53 {
54     OicSecDoxm_t * doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
55     if(!doxm)
56     {
57         return NULL;
58     }
59     doxm->oxmTypeLen =  1;
60     doxm->oxmType    = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
61     if(!doxm->oxmType)
62     {
63         OICFree(doxm);
64         return NULL;
65     }
66     doxm->oxmType[0] = (char*)OICMalloc(strlen(OXM_JUST_WORKS) + 1);
67     if(!doxm->oxmType[0])
68     {
69         OICFree(doxm->oxmType);
70         OICFree(doxm);
71         return NULL;
72     }
73
74     strcpy(doxm->oxmType[0], OXM_JUST_WORKS);
75     doxm->oxmLen     = 1;
76     doxm->oxm        = (OicSecOxm_t *)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
77     if(!doxm->oxm)
78     {
79         OICFree(doxm->oxmType[0]);
80         OICFree(doxm->oxmType);
81         OICFree(doxm);
82         return NULL;
83     }
84
85     doxm->oxm[0]     = OIC_JUST_WORKS;
86     doxm->oxmSel     = OIC_JUST_WORKS;
87     doxm->sct        = SYMMETRIC_PAIR_WISE_KEY;
88     doxm->owned      = true;
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");
93     return doxm;
94 }
95
96  //InitDoxmResource Tests
97 TEST(InitDoxmResourceTest, InitDoxmResource)
98 {
99     EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
100 }
101
102 //DeInitDoxmResource Tests
103 TEST(DeInitDoxmResourceTest, DeInitDoxmResource)
104 {
105     EXPECT_EQ(OC_STACK_ERROR, DeInitDoxmResource());
106 }
107
108 //CreateDoxmResource Tests
109 TEST(CreateDoxmResourceTest, CreateDoxmResource)
110 {
111     EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateDoxmResource());
112 }
113
114  //DoxmEntityHandler Tests
115 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithDummyRequest)
116 {
117     OCEntityHandlerRequest req;
118     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
119 }
120
121 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithNULLRequest)
122 {
123     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
124 }
125
126 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerInvalidFlag)
127 {
128     OCEntityHandlerRequest req;
129     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
130 }
131
132 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerValidRequest)
133 {
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));
140
141     OICFree(req.query);
142 }
143
144 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerDeviceIdQuery)
145 {
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));
152
153     OICFree(req.query);
154 }
155
156 //BinToDoxmJSON Tests
157 TEST(BinToDoxmJSONTest, BinToDoxmJSONNullDoxm)
158 {
159     char* value = BinToDoxmJSON(NULL);
160     EXPECT_TRUE(value == NULL);
161 }
162
163 TEST(BinToDoxmJSONTest, BinToDoxmJSONValidDoxm)
164 {
165     OicSecDoxm_t * doxm =  getBinDoxm();
166
167     char * json = BinToDoxmJSON(doxm);
168     OC_LOG_V(INFO, TAG, "BinToDoxmJSON:%s", json);
169     EXPECT_TRUE(json != NULL);
170
171     DeleteDoxmBinData(doxm);
172     OICFree(json);
173 }
174
175 //JSONToDoxmBin Tests
176 TEST(JSONToDoxmBinTest, JSONToDoxmBinValidJSON)
177 {
178     OicSecDoxm_t * doxm1 =  getBinDoxm();
179     char * json = BinToDoxmJSON(doxm1);
180     EXPECT_TRUE(json != NULL);
181
182     OicSecDoxm_t *doxm2 = JSONToDoxmBin(json);
183     EXPECT_TRUE(doxm2 != NULL);
184
185     DeleteDoxmBinData(doxm1);
186     DeleteDoxmBinData(doxm2);
187     OICFree(json);
188 }
189
190 TEST(JSONToDoxmBinTest, JSONToDoxmBinNullJSON)
191 {
192     OicSecDoxm_t *doxm = JSONToDoxmBin(NULL);
193     EXPECT_TRUE(doxm == NULL);
194 }
195
196 #if 0
197 //HandleDoxmPostRequest Test
198 TEST(HandleDoxmPostRequestTest, HandleDoxmPostRequestValidInput)
199 {
200     OCEntityHandlerRequest ehRequest = {};
201     OCServerRequest svRequest = {};
202
203     OicSecDoxm_t * doxm =  getBinDoxm();
204
205     strcpy(svRequest.addressInfo.IP.ipAddress, "10.10.10.10");
206     svRequest.addressInfo.IP.port = 2345;
207     svRequest.connectivityType = CA_ETHERNET;
208
209     ehRequest.reqJSONPayload = (unsigned char *) BinToDoxmJSON(doxm);
210     ehRequest.requestHandle = (OCRequestHandle) &svRequest;
211
212     EXPECT_EQ(OC_EH_ERROR, HandleDoxmPostRequest(&ehRequest));
213     DeleteDoxmBinData(doxm);
214     OICFree(ehRequest.reqJSONPayload);
215 }
216 #endif