Imported Upstream version 0.9.2
[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_malloc.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
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);
42 #ifdef __cplusplus
43 }
44 #endif
45
46 OicSecDoxm_t * getBinDoxm()
47 {
48     OicSecDoxm_t * doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
49     doxm->oxmTypeLen =  1;
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);
53     doxm->oxmLen     = 1;
54     doxm->oxm        = (OicSecOxm_t *)OICCalloc(doxm->oxmLen, sizeof(short));
55     doxm->oxm[0]     = OIC_JUST_WORKS;
56     doxm->oxmSel     = OIC_JUST_WORKS;
57     doxm->owned      = true;
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");
62     return doxm;
63 }
64
65  //InitDoxmResource Tests
66 TEST(InitDoxmResourceTest, InitDoxmResource)
67 {
68     EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
69 }
70
71 //DeInitDoxmResource Tests
72 TEST(DeInitDoxmResourceTest, DeInitDoxmResource)
73 {
74     EXPECT_EQ(OC_STACK_ERROR, DeInitDoxmResource());
75 }
76
77 //CreateDoxmResource Tests
78 TEST(CreateDoxmResourceTest, CreateDoxmResource)
79 {
80     EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateDoxmResource());
81 }
82
83  //DoxmEntityHandler Tests
84 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithDummyRequest)
85 {
86     OCEntityHandlerRequest req;
87     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
88 }
89
90 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithNULLRequest)
91 {
92     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
93 }
94
95 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerInvalidFlag)
96 {
97     OCEntityHandlerRequest req;
98     EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
99 }
100
101 TEST(DoxmEntityHandlerTest, DoxmEntityHandlerValidRequest)
102 {
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));
110
111     OICFree(req.query);
112 }
113
114 //BinToDoxmJSON Tests
115 TEST(BinToDoxmJSONTest, BinToDoxmJSONNullDoxm)
116 {
117     char* value = BinToDoxmJSON(NULL);
118     EXPECT_TRUE(value == NULL);
119 }
120
121 TEST(BinToDoxmJSONTest, BinToDoxmJSONValidDoxm)
122 {
123     OicSecDoxm_t * doxm =  getBinDoxm();
124
125     char * json = BinToDoxmJSON(doxm);
126     printf("BinToDoxmJSON:%s\n", json);
127     EXPECT_TRUE(json != NULL);
128
129     DeleteDoxmBinData(doxm);
130     OICFree(json);
131 }
132
133 //JSONToDoxmBin Tests
134 TEST(JSONToDoxmBinTest, JSONToDoxmBinValidJSON)
135 {
136     OicSecDoxm_t * doxm1 =  getBinDoxm();
137     char * json = BinToDoxmJSON(doxm1);
138     EXPECT_TRUE(json != NULL);
139
140     OicSecDoxm_t *doxm2 = JSONToDoxmBin(json);
141     EXPECT_TRUE(doxm2 != NULL);
142
143     DeleteDoxmBinData(doxm1);
144     OICFree(json);
145 }
146
147 TEST(JSONToDoxmBinTest, JSONToDoxmBinNullJSON)
148 {
149     OicSecDoxm_t *doxm = JSONToDoxmBin(NULL);
150     EXPECT_TRUE(doxm == NULL);
151 }
152
153 #if 0
154 //HandleDoxmPostRequest Test
155 TEST(HandleDoxmPostRequestTest, HandleDoxmPostRequestValidInput)
156 {
157     OCEntityHandlerRequest ehRequest = {};
158     OCServerRequest svRequest = {};
159
160     OicSecDoxm_t * doxm =  getBinDoxm();
161
162     strcpy(svRequest.addressInfo.IP.ipAddress, "10.10.10.10");
163     svRequest.addressInfo.IP.port = 2345;
164     svRequest.connectivityType = CA_ETHERNET;
165
166     ehRequest.reqJSONPayload = (unsigned char *) BinToDoxmJSON(doxm);
167     ehRequest.requestHandle = (OCRequestHandle) &svRequest;
168
169     EXPECT_EQ(OC_EH_ERROR, HandleDoxmPostRequest(&ehRequest));
170     DeleteDoxmBinData(doxm);
171     OICFree(ehRequest.reqJSONPayload);
172 }
173 #endif