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");
70 //ConstructResourceObject Test
71 TEST(ConstructResourceTest, ConstructResourceObjectWithValidHost)
73 EXPECT_NO_THROW(ConstructResourceObject("coap://192.168.1.2:5000", "/resource"));
76 TEST(ConstructResourceTest, ConstructResourceObjectWithValidHost2)
78 EXPECT_NO_THROW(ConstructResourceObject("coap://[ffff::ffff]:5000", "/resource"));
81 TEST(ConstructResourceTest, ConstructResourceObjectWithValidHost3)
83 EXPECT_NO_THROW(ConstructResourceObject("coap://[ffff::ffff%eth0]:5000", "/resource"));
86 TEST(ConstructResourceTest, ConstructResourceObject)
88 EXPECT_ANY_THROW(ConstructResourceObject(std::string(""), std::string("")));
91 TEST(ConstructResourceTest, ConstructResourceObjectWithoutCoapScheme)
93 EXPECT_ANY_THROW(ConstructResourceObject("//192.168.1.2:5000", "/resource"));
96 TEST(ConstructResourceTest, ConstructResourceObjectWithoutPortNumber)
98 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2", "/resource"));
101 TEST(ConstructResourceTest, ConstructResourceObjectWithoutPortNumber2)
103 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2:", "/resource"));
106 TEST(ConstructResourceTest, ConstructResourceObjectWithLongHostAddress)
108 EXPECT_ANY_THROW(ConstructResourceObject(gLongHostAddress, "/resource"));
111 TEST(ConstructResourceTest, ConstructResourceObjectInvalidHost)
113 EXPECT_ANY_THROW(ConstructResourceObject("192.168.1.2:5000", "/resource"));
116 TEST(ConstructResourceTest, ConstructResourceObjectInvalidHost2)
118 EXPECT_ANY_THROW(ConstructResourceObject("coap://:5000", "/resource"));
121 TEST(ConstructResourceTest, ConstructResourceObjectInvalidHost3)
123 EXPECT_ANY_THROW(ConstructResourceObject("coap://[ffff:::ffff]:5000", "/resource"));
126 TEST(ConstructResourceTest, ConstructResourceObjectInvalidUri)
128 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2:5000", "/"));
131 TEST(ConstructResourceTest, ConstructResourceObjectInvalidUri2)
133 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2:5000", "resource"));
137 TEST(ResourceGetTest, DISABLED_ResourceGetForValidUri)
139 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
140 EXPECT_TRUE(resource != NULL);
141 EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
145 TEST(ResourceGetTest, DISABLED_ResourceGetForBadUri)
147 OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
148 EXPECT_TRUE(resource != NULL);
149 EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
152 TEST(ResourceGetTest, DISABLED_ResourceGetWithHighQos)
154 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
155 EXPECT_TRUE(resource != NULL);
156 EXPECT_EQ(OC_STACK_OK,
157 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::HighQos));
160 TEST(ResourceGetTest, DISABLED_ResourceGetWithLowQos)
162 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
163 EXPECT_TRUE(resource != NULL);
164 EXPECT_EQ(OC_STACK_OK,
165 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::LowQos));
168 TEST(ResourceGetTest, DISABLED_ResourceGetWithMidQos)
170 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
171 EXPECT_TRUE(resource != NULL);
172 EXPECT_EQ(OC_STACK_OK,
173 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::MidQos));
176 TEST(ResourceGetTest, DISABLED_ResourceGetWithNaQos)
178 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
179 EXPECT_TRUE(resource != NULL);
180 EXPECT_EQ(OC_STACK_OK,
181 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
184 TEST(ResourceGetTest, DISABLED_ResourceGetWithRTIFNaQos)
186 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
187 EXPECT_TRUE(resource != NULL);
188 EXPECT_EQ(OC_STACK_OK,
189 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
190 QualityOfService::NaQos));
193 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceType)
195 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
196 EXPECT_TRUE(resource != NULL);
198 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
199 QualityOfService::NaQos));
203 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceInterface)
205 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
206 EXPECT_TRUE(resource != NULL);
208 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
211 TEST(ResourceGetTest, DISABLED_ResourceGetWithTypeAndInterface)
213 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
214 EXPECT_TRUE(resource != NULL);
215 EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut));
219 TEST(ResourcePostTest, DISABLED_ResourcePostValidConfiguration)
221 OCRepresentation rep;
222 QueryParamsMap query = {};
223 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
224 EXPECT_TRUE(resource != NULL);
225 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut));
228 TEST(ResourcePostTest, DISABLED_ResourcePostWithNaQos)
230 OCRepresentation rep;
231 QueryParamsMap query = {};
232 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
233 EXPECT_TRUE(resource != NULL);
234 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos));
237 TEST(ResourcePostTest, DISABLED_ResourcePostWithMidQos)
239 OCRepresentation rep;
240 QueryParamsMap query = {};
241 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
242 EXPECT_TRUE(resource != NULL);
243 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos));
246 TEST(ResourcePostTest, DISABLED_ResourcePostWithLowQos)
248 OCRepresentation rep;
249 QueryParamsMap query = {};
250 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
251 EXPECT_TRUE(resource != NULL);
252 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos));
255 TEST(ResourcePostTest, DISABLED_ResourcePostWithHighQos)
257 OCRepresentation rep;
258 QueryParamsMap query = {};
259 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
260 EXPECT_TRUE(resource != NULL);
261 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::HighQos));
265 TEST(ResourcePutTest, DISABLED_ResourcePutForValid)
267 OCRepresentation rep;
268 QueryParamsMap query = {};
269 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
270 EXPECT_TRUE(resource != NULL);
271 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut));
274 TEST(ResourcePutTest, DISABLED_ResourcePutWithNaQos)
276 OCRepresentation rep;
277 QueryParamsMap query = {};
278 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
279 EXPECT_TRUE(resource != NULL);
280 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos));
283 TEST(ResourcePutTest, DISABLED_ResourcePutWithLowQos)
285 OCRepresentation rep;
286 QueryParamsMap query = {};
287 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
288 EXPECT_TRUE(resource != NULL);
289 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos));
292 TEST(ResourcePutTest, DISABLED_ResourcePutWithMidQos)
294 OCRepresentation rep;
295 QueryParamsMap query = {};
296 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
297 EXPECT_TRUE(resource != NULL);
298 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos));
301 TEST(ResourcePutTest, DISABLED_ResourcePutWithHighQos)
303 OCRepresentation rep;
304 QueryParamsMap query = {};
305 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
306 EXPECT_TRUE(resource != NULL);
307 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos));
310 TEST(ResourcePutTest, DISABLED_ResourcePutWithTypeAndInterface)
312 OCRepresentation rep;
313 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
314 EXPECT_TRUE(resource != NULL);
315 EXPECT_EQ(OC_STACK_OK,
316 resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
319 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullType)
321 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
322 EXPECT_TRUE(resource != NULL);
323 OCRepresentation rep;
325 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
326 HeaderOptions headerOptions;
327 onGetPut(headerOptions, rep, OC_STACK_OK);
330 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullInterface)
332 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
333 EXPECT_TRUE(resource != NULL);
334 OCRepresentation rep;
335 EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut));
339 TEST(ResourceObserveTest, DISABLED_ResourceObserveValidUri)
341 OCResource::Ptr resource =
342 ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
343 EXPECT_TRUE(resource != NULL);
344 QueryParamsMap query = {};
345 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
348 TEST(ResourceObserveTest, DISABLED_ResourceObserveLoQos)
350 QueryParamsMap query = {};
351 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
352 EXPECT_TRUE(resource != NULL);
353 EXPECT_EQ(OC_STACK_OK,
354 resource->observe(ObserveType::ObserveAll, query, &onObserve,
355 QualityOfService::LowQos));
358 TEST(ResourceObserveTest, DISABLED_ResourceObserveNaQos)
360 QueryParamsMap query = {};
361 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
362 EXPECT_TRUE(resource != NULL);
363 EXPECT_EQ(OC_STACK_OK,
364 resource->observe(ObserveType::ObserveAll, query, &onObserve,
365 QualityOfService::NaQos));
368 TEST(ResourceObserveTest, DISABLED_ResourceObserveMidQos)
370 QueryParamsMap query = {};
371 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
372 EXPECT_TRUE(resource != NULL);
373 EXPECT_EQ(OC_STACK_OK,
374 resource->observe(ObserveType::ObserveAll, query, &onObserve,
375 QualityOfService::MidQos));
378 TEST(ResourceObserveTest, DISABLED_ResourceObserveHighQos)
380 QueryParamsMap query = {};
381 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
382 EXPECT_TRUE(resource != NULL);
383 EXPECT_EQ(OC_STACK_OK,
384 resource->observe(ObserveType::ObserveAll, query, &onObserve,
385 QualityOfService::HighQos));
388 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveValidUri)
390 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
391 EXPECT_TRUE(resource != NULL);
392 QueryParamsMap query = {};
393 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
394 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve());
397 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithNaQos)
399 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
400 EXPECT_TRUE(resource != NULL);
401 QueryParamsMap query = {};
402 EXPECT_EQ(OC_STACK_OK,
403 resource->observe(ObserveType::ObserveAll, query, &onObserve,
404 QualityOfService::NaQos));
405 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos));
408 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithLowQos)
410 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
411 EXPECT_TRUE(resource != NULL);
412 QueryParamsMap query = {};
413 EXPECT_EQ(OC_STACK_OK,
414 resource->observe(ObserveType::ObserveAll, query, &onObserve,
415 QualityOfService::LowQos));
416 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos));
419 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithMidQos)
421 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
422 EXPECT_TRUE(resource != NULL);
423 QueryParamsMap query = {};
424 EXPECT_EQ(OC_STACK_OK,
425 resource->observe(ObserveType::ObserveAll, query, &onObserve,
426 QualityOfService::MidQos));
427 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos));
430 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithHighQos)
432 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
433 EXPECT_TRUE(resource != NULL);
434 QueryParamsMap query = {};
435 EXPECT_EQ(OC_STACK_OK,
436 resource->observe(ObserveType::ObserveAll, query, &onObserve,
437 QualityOfService::HighQos));
438 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos));
442 TEST(DeleteResourceTest, DISABLED_DeleteResourceValidUri)
444 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
445 EXPECT_TRUE(resource != NULL);
446 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse));
449 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithLowQos)
451 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
452 EXPECT_TRUE(resource != NULL);
453 EXPECT_EQ(OC_STACK_OK,
454 resource->deleteResource(&deleteResponse, QualityOfService::LowQos));
457 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithMidQos)
459 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
460 EXPECT_TRUE(resource != NULL);
461 EXPECT_EQ(OC_STACK_OK,
462 resource->deleteResource(&deleteResponse, QualityOfService::MidQos));
465 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithHighQos)
467 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
468 EXPECT_TRUE(resource != NULL);
469 EXPECT_EQ(OC_STACK_OK,
470 resource->deleteResource(&deleteResponse, QualityOfService::HighQos));
472 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithNaQos)
474 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
475 EXPECT_TRUE(resource != NULL);
476 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos));
479 //GetResourceInterfaces Test
480 TEST(GetResourceInterfacesTest, GetResourceInterfaces)
482 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
483 EXPECT_TRUE(resource != NULL);
484 EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE);
487 //GetResourceTypes Test
488 TEST(GetResourceTypesTest, GetResourceTypes)
490 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
491 EXPECT_TRUE(resource != NULL);
492 EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost");
498 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
499 EXPECT_TRUE(resource != NULL);
500 EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000");
506 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
507 EXPECT_TRUE(resource != NULL);
508 EXPECT_TRUE(resource->uri() == "/resource");
511 //ConnectivityType Test
512 TEST(ConnectivityTypeTest, ConnectivityType)
514 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
515 EXPECT_TRUE(resource != NULL);
516 EXPECT_TRUE(resource->connectivityType() == CT_DEFAULT);
520 TEST(IsObservableTest, isObservable)
522 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
523 EXPECT_TRUE(resource != NULL);
524 EXPECT_FALSE(resource->isObservable());
530 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
531 EXPECT_TRUE(resource != NULL);
532 EXPECT_TRUE(resource->sid() == "");
535 //UniqueIdentifier Test
536 TEST(UniqueIdentifierTest, uniqueIdentifier)
538 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
539 EXPECT_TRUE(resource != NULL);
540 std::ostringstream ss;
541 ss << resource->uniqueIdentifier();
542 EXPECT_TRUE(ss.str() == "/resource");
545 // HeaderOptions Test
546 TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
548 const uint16_t API_VERSION = 2048;
549 const uint16_t TOKEN = 3000;
550 HeaderOptions headerOptions;
551 HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
552 HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
553 headerOptions.push_back(apiVersion);
554 headerOptions.push_back(clientToken);
556 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
557 EXPECT_TRUE(resource != NULL);
558 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
559 EXPECT_NO_THROW(resource->unsetHeaderOptions());
562 TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
564 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
565 EXPECT_TRUE(resource != NULL);
566 HeaderOptions headerOptions;
567 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
568 EXPECT_NO_THROW(resource->unsetHeaderOptions());