Merge branch 'master' into easysetup & CBOR changes
[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  PCF("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->owned      = true;
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");
92     return doxm;
93 }
94
95  //InitDoxmResource Tests
96 TEST(InitDoxmResourceTest, InitDoxmResource)
97 {
98     EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
99 }
100
101 //DeInitDoxmResource Tests
102 TEST(DeInitDoxmResourceTest, DeInitDoxmResource)
103 {
104     EXPECT_EQ(OC_STACK_ERROR, DeInitDoxmResource());
105 }
106
107 //CreateDoxmResource Tests
108 TEST(CreateDoxmResourceTest, CreateDoxmResource)
109 {
110     EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateDoxmResource());
111 }
112
113  //DoxmEntityHandler Tests
114 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithDummyRequest)
115 {
116     OCEntityHandlerRequest req;
117     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
118 }
119
120 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithNULLRequest)
121 {
122     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
123 }
124
125 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerInvalidFlag)
126 {
127     OCEntityHandlerRequest req;
128     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
129 }
130
131 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerValidRequest)
132 {
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));
139
140     OICFree(req.query);
141 }
142
143 //BinToDoxmJSON Tests
144 TEST(BinToDoxmJSONTest, BinToDoxmJSONNullDoxm)
145 {
146     char* value = BinToDoxmJSON(NULL);
147     EXPECT_TRUE(value == NULL);
148 }
149
150 TEST(BinToDoxmJSONTest, BinToDoxmJSONValidDoxm)
151 {
152     OicSecDoxm_t * doxm =  getBinDoxm();
153
154     char * json = BinToDoxmJSON(doxm);
155     OC_LOG_V(INFO, TAG, PCF("BinToDoxmJSON:%s"), json);
156     EXPECT_TRUE(json != NULL);
157
158     DeleteDoxmBinData(doxm);
159     OICFree(json);
160 }
161
162 //JSONToDoxmBin Tests
163 TEST(JSONToDoxmBinTest, JSONToDoxmBinValidJSON)
164 {
165     OicSecDoxm_t * doxm1 =  getBinDoxm();
166     char * json = BinToDoxmJSON(doxm1);
167     EXPECT_TRUE(json != NULL);
168
169     OicSecDoxm_t *doxm2 = JSONToDoxmBin(json);
170     EXPECT_TRUE(doxm2 != NULL);
171
172     DeleteDoxmBinData(doxm1);
173     DeleteDoxmBinData(doxm2);
174     OICFree(json);
175 }
176
177 TEST(JSONToDoxmBinTest, JSONToDoxmBinNullJSON)
178 {
179     OicSecDoxm_t *doxm = JSONToDoxmBin(NULL);
180     EXPECT_TRUE(doxm == NULL);
181 }
182
183 #if 0
184 //HandleDoxmPostRequest Test
185 TEST(HandleDoxmPostRequestTest, HandleDoxmPostRequestValidInput)
186 {
187     OCEntityHandlerRequest ehRequest = {};
188     OCServerRequest svRequest = {};
189
190     OicSecDoxm_t * doxm =  getBinDoxm();
191
192     strcpy(svRequest.addressInfo.IP.ipAddress, "10.10.10.10");
193     svRequest.addressInfo.IP.port = 2345;
194     svRequest.connectivityType = CA_ETHERNET;
195
196     ehRequest.reqJSONPayload = (unsigned char *) BinToDoxmJSON(doxm);
197     ehRequest.requestHandle = (OCRequestHandle) &svRequest;
198
199     EXPECT_EQ(OC_EH_ERROR, HandleDoxmPostRequest(&ehRequest));
200     DeleteDoxmBinData(doxm);
201     OICFree(ehRequest.reqJSONPayload);
202 }
203 #endif