Merge branch 'security-CKM' into 'master'
[iotivity.git] / resource / unittests / OCResourceResponseTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <OCPlatform.h>
22 #include <OCApi.h>
23 #include <gtest/gtest.h>
24 #include <OCResourceResponse.h>
25 #include <ocserverrequest.h>
26 #include <InProcServerWrapper.h>
27
28 namespace PH = std::placeholders;
29 namespace OCResourceResponseTest
30 {
31     using namespace OC;
32     using namespace std;
33
34     TEST(ErrorCodeTest, SetGetErrorCodeValidCode)
35     {
36         OCResourceResponse response;
37         int setCode = 200;
38         EXPECT_NO_THROW(response.setErrorCode(setCode));
39         EXPECT_EQ(setCode, response.getErrorCode());
40     }
41
42     TEST(NewResourceUriTest, SetGetNewResourceUriValidUri)
43     {
44         OCResourceResponse response;
45         std::string uri = "/a/light";
46         EXPECT_NO_THROW(response.setNewResourceUri(uri));
47         EXPECT_EQ(uri, response.getNewResourceUri());
48     }
49
50     TEST(NewResourceUriTest, SetGetNewResourceUriEmpty)
51     {
52         OCResourceResponse response;
53         std::string uri = "/a/light";
54         EXPECT_NO_THROW(response.setNewResourceUri(uri));
55         EXPECT_NE("", response.getNewResourceUri());
56     }
57
58     TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsValidOption)
59     {
60         OCResourceResponse response;
61         const uint16_t API_VERSION = 2048;
62         const std::string FRIDGE_CLIENT_API_VERSION = "v.1.0";
63         HeaderOptions headerOptions;
64         HeaderOption::OCHeaderOption apiVersion(API_VERSION, FRIDGE_CLIENT_API_VERSION);
65         headerOptions.push_back(apiVersion);
66
67         EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
68         EXPECT_FALSE(headerOptions.empty());
69         EXPECT_EQ(apiVersion.getOptionID(),
70                 response.getHeaderOptions()[0].getOptionID());
71         EXPECT_EQ(apiVersion.getOptionData(),
72                 response.getHeaderOptions()[0].getOptionData());
73      }
74
75     TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsEmpty)
76     {
77         OCResourceResponse response;
78         HeaderOptions headerOptions;
79
80         EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
81         EXPECT_TRUE(headerOptions.empty());
82     }
83
84     TEST(RequestHandleTest, SetGetRequestHandleValidHandle)
85     {
86         char query[] = "?rt=core.light";
87         char address[] = "127.0.0.1";
88         OCResourceResponse response;
89         OCServerRequest request;
90         request.method = OC_REST_GET;
91         strncpy(request.query, query, sizeof(query));
92         request.devAddr.flags = OC_DEFAULT_FLAGS;
93         request.devAddr.adapter = OC_DEFAULT_ADAPTER;
94         strncpy(request.devAddr.addr, address, sizeof(query));
95         request.devAddr.port = 5364;
96         request.qos = OC_LOW_QOS;
97         request.coapID = 0;
98         request.delayedResNeeded = 0;
99
100         OCRequestHandle handle = static_cast<OCRequestHandle>(&request);
101         EXPECT_EQ(NULL, response.getRequestHandle());
102         EXPECT_NO_THROW(response.setRequestHandle(handle));
103         EXPECT_NE(static_cast<OCRequestHandle>(NULL), response.getRequestHandle());
104     }
105
106     TEST(RequestHandleTest, SetGetRequestHandleNullHandle)
107     {
108         OCResourceResponse response;
109         OCRequestHandle handle = nullptr;
110
111         EXPECT_EQ(NULL, response.getRequestHandle());
112         EXPECT_NO_THROW(response.setRequestHandle(handle));
113         EXPECT_EQ(NULL, response.getRequestHandle());
114     }
115
116     TEST(ResourceHandleTest, SetGetResourceHandleValidHandle)
117     {
118         OCResourceResponse response;
119         OCResourceHandle resHandle;
120
121         std::string resourceURI = "/a/light2";
122         std::string resourceTypeName = "core.light";
123         std::string resourceInterface = DEFAULT_INTERFACE;
124         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
125
126         EXPECT_EQ(OC_STACK_OK, OCCreateResource(&resHandle, resourceTypeName.c_str(),
127                 resourceInterface.c_str(), resourceURI.c_str(), nullptr, nullptr,
128                 resourceProperty));
129         EXPECT_EQ(NULL, response.getResourceHandle());
130         EXPECT_NO_THROW(response.setResourceHandle(resHandle));
131         EXPECT_NE(static_cast<OCResourceHandle>(NULL), response.getResourceHandle());
132     }
133
134     TEST(ResourceHandleTest, SetGetResourceHandleNullHandle)
135     {
136         OCResourceResponse response;
137         OCResourceHandle handle = nullptr;
138
139         EXPECT_EQ(NULL, response.getResourceHandle());
140         EXPECT_NO_THROW(response.setResourceHandle(handle));
141         EXPECT_EQ(NULL, response.getResourceHandle());
142     }
143
144     TEST(ResponseResultTest, SetGetResponseResultValidInput)
145     {
146         OCResourceResponse response;
147         OCEntityHandlerResult result = OC_EH_SLOW;
148         EXPECT_NO_THROW(response.setResponseResult(result));
149         EXPECT_EQ(result, response.getResponseResult());
150     }
151
152     TEST(ResourceRepresentation, SetGetResourceRepresentationWithValidRepresentation)
153     {
154         OCResourceResponse response;
155         OCRepresentation lightRepresentation;
156         const std::string LIGHT_RESOURCE_KEY = "light";
157         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
158         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
159         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation));
160         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
161     }
162
163     TEST(ResourceRepresentation,
164             SetGetResourceRepresentationWithRepresentationAndEmptyInterface)
165     {
166         OCResourceResponse response;
167         OCRepresentation lightRepresentation;
168
169         const std::string LIGHT_RESOURCE_KEY = "light";
170         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
171         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
172         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation, ""));
173         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
174     }
175
176     TEST(ResourceRepresentation,
177             SetGetResourceRepresentationWithRepresentationAndInterface)
178     {
179         OCResourceResponse response;
180         OCRepresentation lightRepresentation;
181
182         const std::string LIGHT_RESOURCE_KEY = "light";
183         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
184         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
185         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation,
186                 LINK_INTERFACE));
187         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
188     }
189 }