1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include <OCPlatform.h>
24 #include <gtest/gtest.h>
28 static char gLongHostAddress[] = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111:5000";
30 namespace OCResourceTest
34 void onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&)
38 void onGetPut(const HeaderOptions&, const OCRepresentation& , const int eCode)
40 EXPECT_EQ(eCode, OC_STACK_OK);
43 void foundResource(std::shared_ptr<OCResource> )
47 void deleteResponse(const HeaderOptions&, const int)
52 OCResource::Ptr ConstructResourceObject(std::string host, std::string uri)
54 OCConnectivityType connectivityType = CT_DEFAULT;
55 std::vector<std::string> types = {"intel.rpost"};
56 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
58 auto ret = OCPlatform::constructResourceObject(host, uri,
59 connectivityType, false, types, ifaces);
63 ADD_FAILURE() << "ConstructResourceObject result was null";
64 throw std::runtime_error("ConstructResourceObject result was null");
71 TEST(ConstructResourceTest, ConstructResourceObject)
73 EXPECT_ANY_THROW(ConstructResourceObject(std::string(""), std::string("")));
76 TEST(ConstructResourceTest, ConstructResourceObjectWithoutCoapScheme)
78 EXPECT_ANY_THROW(ConstructResourceObject("//192.168.1.2:5000", "/resource"));
81 TEST(ConstructResourceTest, ConstructResourceObjectWithoutPortNumber)
83 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2", "/resource"));
86 TEST(ConstructResourceTest, ConstructResourceObjectWithLongHostAddress)
88 EXPECT_ANY_THROW(ConstructResourceObject(gLongHostAddress, "/resource"));
91 TEST(ConstructResourceTest, ConstructResourceObjectInvalidHost)
93 EXPECT_ANY_THROW(ConstructResourceObject("192.168.1.2:5000", "/resource"));
96 TEST(ConstructResourceTest, ConstructResourceObjectInvalidHost2)
98 EXPECT_ANY_THROW(ConstructResourceObject("coap://:5000", "/resource"));
101 TEST(ConstructResourceTest, ConstructResourceObjectInvalidUri)
103 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2:5000", "/"));
106 TEST(ConstructResourceTest, ConstructResourceObjectInvalidUri2)
108 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2:5000", "resource"));
111 TEST(ResourceGetTest, DISABLED_ResourceGetForValidUri)
113 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
114 EXPECT_TRUE(resource != NULL);
115 EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
119 TEST(ResourceGetTest, DISABLED_ResourceGetForBadUri)
121 OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
122 EXPECT_TRUE(resource != NULL);
123 EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
126 TEST(ResourceGetTest, DISABLED_ResourceGetWithHighQos)
128 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
129 EXPECT_TRUE(resource != NULL);
130 EXPECT_EQ(OC_STACK_OK,
131 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::HighQos));
134 TEST(ResourceGetTest, DISABLED_ResourceGetWithLowQos)
136 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
137 EXPECT_TRUE(resource != NULL);
138 EXPECT_EQ(OC_STACK_OK,
139 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::LowQos));
142 TEST(ResourceGetTest, DISABLED_ResourceGetWithMidQos)
144 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
145 EXPECT_TRUE(resource != NULL);
146 EXPECT_EQ(OC_STACK_OK,
147 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::MidQos));
150 TEST(ResourceGetTest, DISABLED_ResourceGetWithNaQos)
152 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
153 EXPECT_TRUE(resource != NULL);
154 EXPECT_EQ(OC_STACK_OK,
155 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
158 TEST(ResourceGetTest, DISABLED_ResourceGetWithRTIFNaQos)
160 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
161 EXPECT_TRUE(resource != NULL);
162 EXPECT_EQ(OC_STACK_OK,
163 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
164 QualityOfService::NaQos));
167 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceType)
169 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
170 EXPECT_TRUE(resource != NULL);
172 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
173 QualityOfService::NaQos));
177 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceInterface)
179 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
180 EXPECT_TRUE(resource != NULL);
182 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
185 TEST(ResourceGetTest, DISABLED_ResourceGetWithTypeAndInterface)
187 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
188 EXPECT_TRUE(resource != NULL);
189 EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut));
193 TEST(ResourcePostTest, DISABLED_ResourcePostValidConfiguration)
195 OCRepresentation rep;
196 QueryParamsMap query = {};
197 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
198 EXPECT_TRUE(resource != NULL);
199 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut));
202 TEST(ResourcePostTest, DISABLED_ResourcePostWithNaQos)
204 OCRepresentation rep;
205 QueryParamsMap query = {};
206 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
207 EXPECT_TRUE(resource != NULL);
208 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos));
211 TEST(ResourcePostTest, DISABLED_ResourcePostWithMidQos)
213 OCRepresentation rep;
214 QueryParamsMap query = {};
215 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
216 EXPECT_TRUE(resource != NULL);
217 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos));
220 TEST(ResourcePostTest, DISABLED_ResourcePostWithLowQos)
222 OCRepresentation rep;
223 QueryParamsMap query = {};
224 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
225 EXPECT_TRUE(resource != NULL);
226 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos));
229 TEST(ResourcePostTest, DISABLED_ResourcePostWithHighQos)
231 OCRepresentation rep;
232 QueryParamsMap query = {};
233 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
234 EXPECT_TRUE(resource != NULL);
235 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::HighQos));
239 TEST(ResourcePutTest, DISABLED_ResourcePutForValid)
241 OCRepresentation rep;
242 QueryParamsMap query = {};
243 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
244 EXPECT_TRUE(resource != NULL);
245 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut));
248 TEST(ResourcePutTest, DISABLED_ResourcePutWithNaQos)
250 OCRepresentation rep;
251 QueryParamsMap query = {};
252 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
253 EXPECT_TRUE(resource != NULL);
254 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos));
257 TEST(ResourcePutTest, DISABLED_ResourcePutWithLowQos)
259 OCRepresentation rep;
260 QueryParamsMap query = {};
261 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
262 EXPECT_TRUE(resource != NULL);
263 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos));
266 TEST(ResourcePutTest, DISABLED_ResourcePutWithMidQos)
268 OCRepresentation rep;
269 QueryParamsMap query = {};
270 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
271 EXPECT_TRUE(resource != NULL);
272 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos));
275 TEST(ResourcePutTest, DISABLED_ResourcePutWithHighQos)
277 OCRepresentation rep;
278 QueryParamsMap query = {};
279 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
280 EXPECT_TRUE(resource != NULL);
281 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos));
284 TEST(ResourcePutTest, DISABLED_ResourcePutWithTypeAndInterface)
286 OCRepresentation rep;
287 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
288 EXPECT_TRUE(resource != NULL);
289 EXPECT_EQ(OC_STACK_OK,
290 resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
293 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullType)
295 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
296 EXPECT_TRUE(resource != NULL);
297 OCRepresentation rep;
299 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
300 HeaderOptions headerOptions;
301 onGetPut(headerOptions, rep, OC_STACK_OK);
304 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullInterface)
306 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
307 EXPECT_TRUE(resource != NULL);
308 OCRepresentation rep;
309 EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut));
313 TEST(ResourceObserveTest, DISABLED_ResourceObserveValidUri)
315 OCResource::Ptr resource =
316 ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
317 EXPECT_TRUE(resource != NULL);
318 QueryParamsMap query = {};
319 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
322 TEST(ResourceObserveTest, DISABLED_ResourceObserveLoQos)
324 QueryParamsMap query = {};
325 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
326 EXPECT_TRUE(resource != NULL);
327 EXPECT_EQ(OC_STACK_OK,
328 resource->observe(ObserveType::ObserveAll, query, &onObserve,
329 QualityOfService::LowQos));
332 TEST(ResourceObserveTest, DISABLED_ResourceObserveNaQos)
334 QueryParamsMap query = {};
335 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
336 EXPECT_TRUE(resource != NULL);
337 EXPECT_EQ(OC_STACK_OK,
338 resource->observe(ObserveType::ObserveAll, query, &onObserve,
339 QualityOfService::NaQos));
342 TEST(ResourceObserveTest, DISABLED_ResourceObserveMidQos)
344 QueryParamsMap query = {};
345 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
346 EXPECT_TRUE(resource != NULL);
347 EXPECT_EQ(OC_STACK_OK,
348 resource->observe(ObserveType::ObserveAll, query, &onObserve,
349 QualityOfService::MidQos));
352 TEST(ResourceObserveTest, DISABLED_ResourceObserveHighQos)
354 QueryParamsMap query = {};
355 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
356 EXPECT_TRUE(resource != NULL);
357 EXPECT_EQ(OC_STACK_OK,
358 resource->observe(ObserveType::ObserveAll, query, &onObserve,
359 QualityOfService::HighQos));
362 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveValidUri)
364 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
365 EXPECT_TRUE(resource != NULL);
366 QueryParamsMap query = {};
367 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
368 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve());
371 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithNaQos)
373 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
374 EXPECT_TRUE(resource != NULL);
375 QueryParamsMap query = {};
376 EXPECT_EQ(OC_STACK_OK,
377 resource->observe(ObserveType::ObserveAll, query, &onObserve,
378 QualityOfService::NaQos));
379 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos));
382 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithLowQos)
384 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
385 EXPECT_TRUE(resource != NULL);
386 QueryParamsMap query = {};
387 EXPECT_EQ(OC_STACK_OK,
388 resource->observe(ObserveType::ObserveAll, query, &onObserve,
389 QualityOfService::LowQos));
390 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos));
393 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithMidQos)
395 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
396 EXPECT_TRUE(resource != NULL);
397 QueryParamsMap query = {};
398 EXPECT_EQ(OC_STACK_OK,
399 resource->observe(ObserveType::ObserveAll, query, &onObserve,
400 QualityOfService::MidQos));
401 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos));
404 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithHighQos)
406 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
407 EXPECT_TRUE(resource != NULL);
408 QueryParamsMap query = {};
409 EXPECT_EQ(OC_STACK_OK,
410 resource->observe(ObserveType::ObserveAll, query, &onObserve,
411 QualityOfService::HighQos));
412 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos));
416 TEST(DeleteResourceTest, DISABLED_DeleteResourceValidUri)
418 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
419 EXPECT_TRUE(resource != NULL);
420 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse));
423 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithLowQos)
425 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
426 EXPECT_TRUE(resource != NULL);
427 EXPECT_EQ(OC_STACK_OK,
428 resource->deleteResource(&deleteResponse, QualityOfService::LowQos));
431 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithMidQos)
433 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
434 EXPECT_TRUE(resource != NULL);
435 EXPECT_EQ(OC_STACK_OK,
436 resource->deleteResource(&deleteResponse, QualityOfService::MidQos));
439 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithHighQos)
441 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
442 EXPECT_TRUE(resource != NULL);
443 EXPECT_EQ(OC_STACK_OK,
444 resource->deleteResource(&deleteResponse, QualityOfService::HighQos));
446 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithNaQos)
448 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
449 EXPECT_TRUE(resource != NULL);
450 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos));
453 //GetResourceInterfaces Test
454 TEST(GetResourceInterfacesTest, GetResourceInterfaces)
456 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
457 EXPECT_TRUE(resource != NULL);
458 EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE);
461 //GetResourceTypes Test
462 TEST(GetResourceTypesTest, GetResourceTypes)
464 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
465 EXPECT_TRUE(resource != NULL);
466 EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost");
472 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
473 EXPECT_TRUE(resource != NULL);
474 EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000");
480 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
481 EXPECT_TRUE(resource != NULL);
482 EXPECT_TRUE(resource->uri() == "/resource");
485 //ConnectivityType Test
486 TEST(ConnectivityTypeTest, ConnectivityType)
488 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
489 EXPECT_TRUE(resource != NULL);
490 EXPECT_TRUE(resource->connectivityType() == CT_DEFAULT);
494 TEST(IsObservableTest, isObservable)
496 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
497 EXPECT_TRUE(resource != NULL);
498 EXPECT_FALSE(resource->isObservable());
504 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
505 EXPECT_TRUE(resource != NULL);
506 EXPECT_TRUE(resource->sid() == "");
509 //UniqueIdentifier Test
510 TEST(UniqueIdentifierTest, uniqueIdentifier)
512 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
513 EXPECT_TRUE(resource != NULL);
514 std::ostringstream ss;
515 ss << resource->uniqueIdentifier();
516 EXPECT_TRUE(ss.str() == "/resource");
519 // HeaderOptions Test
520 TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
522 const uint16_t API_VERSION = 2048;
523 const uint16_t TOKEN = 3000;
524 HeaderOptions headerOptions;
525 HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
526 HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
527 headerOptions.push_back(apiVersion);
528 headerOptions.push_back(clientToken);
530 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
531 EXPECT_TRUE(resource != NULL);
532 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
533 EXPECT_NO_THROW(resource->unsetHeaderOptions());
536 TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
538 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
539 EXPECT_TRUE(resource != NULL);
540 HeaderOptions headerOptions;
541 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
542 EXPECT_NO_THROW(resource->unsetHeaderOptions());