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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
29 #include "gtest/gtest.h"
30 #include <sys/types.h>
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
47 #define DEFAULT_CONTEXT_VALUE 0x99
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
51 static const char TAG[] = "TestHarness";
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
56 extern "C" OCStackApplicationResult asyncDoResourcesCallback(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
58 OC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
60 EXPECT_EQ(OC_STACK_OK, clientResponse->result);
62 if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
63 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
65 OC_LOG_V(INFO, TAG, "result = %d", clientResponse->result);
67 return OC_STACK_KEEP_TRANSACTION;
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 OCEntityHandlerResult entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest)
75 OC_LOG(INFO, TAG, "Entering entityHandler");
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 void InitStack(OCMode mode)
85 OC_LOG(INFO, TAG, "Entering InitStack");
87 EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, mode));
88 OC_LOG(INFO, TAG, "Leaving InitStack");
91 uint8_t InitNumExpectedResources()
94 // When presence is enabled, it is a resource and so is (currently) included
95 // in the returned resource count returned by the OCGetNumberOfResources API.
102 uint8_t InitResourceIndex()
105 // When presence is enabled, it is a resource and so is (currently) included
106 // in the returned resource count returned by the OCGetNumberOfResources API.
107 // The index of the presence resource is 0, so the first user app resource index
114 //-----------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------
118 TEST(StackInit, StackInitNullAddr)
120 EXPECT_EQ(OC_STACK_OK, OCInit(0, 5683, OC_SERVER));
121 EXPECT_EQ(OC_STACK_OK, OCStop());
124 TEST(StackInit, StackInitNullPort)
126 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 0, OC_SERVER));
127 EXPECT_EQ(OC_STACK_OK, OCStop());
130 TEST(StackInit, StackInitNullAddrAndPort)
132 EXPECT_EQ(OC_STACK_OK, OCInit(0, 0, OC_SERVER));
133 EXPECT_EQ(OC_STACK_OK, OCStop());
136 TEST(StackInit, StackInitInvalidMode)
138 EXPECT_EQ(OC_STACK_ERROR, OCInit(0, 0, (OCMode)10));
139 EXPECT_EQ(OC_STACK_ERROR, OCStop());
142 TEST(StackStart, StackStartSuccessClient)
144 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
145 EXPECT_EQ(OC_STACK_OK, OCStop());
148 TEST(StackStart, StackStartSuccessServer)
150 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
151 EXPECT_EQ(OC_STACK_OK, OCStop());
154 TEST(StackStart, StackStartSuccessClientServer)
156 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
157 EXPECT_EQ(OC_STACK_OK, OCStop());
160 TEST(StackStart, StackStartSuccessiveInits)
162 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
163 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.2", 5683, OC_SERVER));
164 EXPECT_EQ(OC_STACK_OK, OCStop());
167 TEST(StackDiscovery, DoResourceDeviceDiscovery)
169 OCCallbackData cbData;
172 OC_LOG(INFO, TAG, "Starting DoResourceDeviceDiscovery test ");
173 InitStack(OC_CLIENT);
175 /* Start a discovery query*/
176 char szQueryUri[64] = { 0 };
177 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
178 cbData.cb = asyncDoResourcesCallback;
179 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
181 EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle,
191 EXPECT_EQ(OC_STACK_OK, OCStop());
194 TEST(StackStop, StackStopWithoutInit)
196 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
197 EXPECT_EQ(OC_STACK_OK, OCStop());
198 EXPECT_EQ(OC_STACK_ERROR, OCStop());
201 TEST(StackResource, UpdateResourceNullURI)
203 OCCallbackData cbData;
206 OC_LOG(INFO, TAG, "Starting UpdateResourceNullURI test");
207 InitStack(OC_CLIENT);
209 /* Start a discovery query*/
210 char szQueryUri[64] = { 0 };
211 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
212 cbData.cb = asyncDoResourcesCallback;
213 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
215 EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle,
225 EXPECT_EQ(OC_STACK_OK, OCStop());
228 TEST(StackResource, CreateResourceBadParams)
230 OC_LOG(INFO, TAG, "Starting CreateResourceBadParams test");
231 InitStack(OC_SERVER);
233 OCResourceHandle handle;
235 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(NULL, //&handle,
240 OC_DISCOVERABLE|OC_OBSERVABLE));
242 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
247 OC_DISCOVERABLE|OC_OBSERVABLE));
249 // Property bitmask out of range
250 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
255 128));// invalid bitmask for OCResourceProperty
257 EXPECT_EQ(OC_STACK_OK, OCStop());
261 TEST(StackResource, CreateResourceSuccess)
263 OC_LOG(INFO, TAG, "Starting CreateResourceSuccess test");
264 InitStack(OC_SERVER);
266 OCResourceHandle handle;
267 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
272 OC_DISCOVERABLE|OC_OBSERVABLE));
273 const char *url = OCGetResourceUri(handle);
274 EXPECT_STREQ("/a/led", url);
276 EXPECT_EQ(OC_STACK_OK, OCStop());
279 TEST(StackResource, CreateResourceFailDuplicateUri)
281 OC_LOG(INFO, TAG, "Starting CreateResourceFailDuplicateUri test");
282 InitStack(OC_SERVER);
284 OCResourceHandle handle;
285 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
290 OC_DISCOVERABLE|OC_OBSERVABLE));
291 const char *url = OCGetResourceUri(handle);
292 EXPECT_STREQ("/a/led", url);
294 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
299 OC_DISCOVERABLE|OC_OBSERVABLE));
301 EXPECT_EQ(OC_STACK_OK, OCStop());
304 TEST(StackResource, CreateResourceMultipleResources)
306 OC_LOG(INFO, TAG, "Starting CreateResourceMultipleResources test");
307 InitStack(OC_SERVER);
309 OCResourceHandle handle1;
310 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
315 OC_DISCOVERABLE|OC_OBSERVABLE));
317 OCResourceHandle handle2;
318 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
323 OC_DISCOVERABLE|OC_OBSERVABLE));
324 OCResourceHandle handle3;
325 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
330 OC_DISCOVERABLE|OC_OBSERVABLE));
332 const char *url = OCGetResourceUri(handle1);
333 EXPECT_STREQ("/a/led1", url);
335 url = OCGetResourceUri(handle2);
336 EXPECT_STREQ("/a/led2", url);
338 url = OCGetResourceUri(handle3);
339 EXPECT_STREQ("/a/led3", url);
341 EXPECT_EQ(OC_STACK_OK, OCStop());
344 TEST(StackResource, CreateResourceBadResoureType)
346 OC_LOG(INFO, TAG, "Starting CreateResourceBadResoureType test");
347 InitStack(OC_SERVER);
349 OCResourceHandle handle;
350 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
355 OC_DISCOVERABLE|OC_OBSERVABLE));
357 EXPECT_EQ(OC_STACK_OK, OCStop());
360 TEST(StackResource, CreateResourceGoodResourceType)
362 OC_LOG(INFO, TAG, "Starting CreateResourceGoodResourceType test");
363 InitStack(OC_SERVER);
365 OCResourceHandle handle;
366 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
371 OC_DISCOVERABLE|OC_OBSERVABLE));
373 EXPECT_EQ(OC_STACK_OK, OCStop());
376 TEST(StackResource, ResourceTypeName)
378 OC_LOG(INFO, TAG, "Starting ResourceTypeName test");
379 InitStack(OC_SERVER);
381 OCResourceHandle handle;
382 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
387 OC_DISCOVERABLE|OC_OBSERVABLE));
389 uint8_t numResourceTypes;
390 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
391 EXPECT_EQ(1, numResourceTypes);
392 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
393 EXPECT_STREQ("core.led", resourceTypeName);
395 // try getting resource type names with an invalid index
396 resourceTypeName = OCGetResourceTypeName(handle, 1);
397 EXPECT_STREQ(NULL, resourceTypeName);
398 // try getting resource type names with an invalid index
399 resourceTypeName = OCGetResourceTypeName(handle, 10);
400 EXPECT_STREQ(NULL, resourceTypeName);
402 EXPECT_EQ(OC_STACK_OK, OCStop());
405 TEST(StackResource, ResourceTypeAttrRepresentation)
407 OC_LOG(INFO, TAG, "Starting ResourceTypeAttrRepresentation test");
408 InitStack(OC_SERVER);
410 OCResourceHandle handle;
411 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
416 OC_DISCOVERABLE|OC_OBSERVABLE));
418 uint8_t numResourceTypes;
419 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
420 EXPECT_EQ(1, numResourceTypes);
422 EXPECT_EQ(OC_STACK_OK, OCStop());
425 TEST(StackResource, ResourceTypeInterface)
427 OC_LOG(INFO, TAG, "Starting ResourceTypeInterface test");
428 InitStack(OC_SERVER);
430 OCResourceHandle handle;
431 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
436 OC_DISCOVERABLE|OC_OBSERVABLE));
438 uint8_t numResourceInterfaces;
439 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
440 EXPECT_EQ(1, numResourceInterfaces);
441 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
442 EXPECT_STREQ("core.rw", resourceInterfaceName);
444 // try getting resource interface names with an invalid index
445 resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
446 EXPECT_STREQ(NULL, resourceInterfaceName);
447 // try getting resource interface names with an invalid index
448 resourceInterfaceName = OCGetResourceInterfaceName(handle, 10);
449 EXPECT_STREQ(NULL, resourceInterfaceName);
451 EXPECT_EQ(OC_STACK_OK, OCStop());
454 TEST(StackResource, ResourceTypeInterfaceMethods)
456 OC_LOG(INFO, TAG, "Starting ResourceTypeInterfaceMethods test");
457 InitStack(OC_SERVER);
459 OCResourceHandle handle;
460 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
465 OC_DISCOVERABLE|OC_OBSERVABLE));
467 uint8_t numResourceInterfaces;
468 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
469 EXPECT_EQ(1, numResourceInterfaces);
471 EXPECT_EQ(OC_STACK_OK, OCStop());
474 TEST(StackResource, GetResourceProperties)
476 OC_LOG(INFO, TAG, "Starting GetResourceProperties test");
477 InitStack(OC_SERVER);
479 OCResourceHandle handle;
480 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
485 OC_DISCOVERABLE|OC_OBSERVABLE));
487 EXPECT_EQ(OC_ACTIVE|OC_DISCOVERABLE|OC_OBSERVABLE, OCGetResourceProperties(handle));
488 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
490 EXPECT_EQ(OC_STACK_OK, OCStop());
493 TEST(StackResource, StackTestResourceDiscoverOneResourceBad)
495 OC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverOneResourceBad test");
496 InitStack(OC_SERVER);
498 OCResourceHandle handle;
499 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
504 OC_DISCOVERABLE|OC_OBSERVABLE));
505 const char *url = OCGetResourceUri(handle);
506 EXPECT_STREQ("/a1/led", url);
508 //EXPECT_EQ(OC_STACK_INVALID_URI, OCHandleServerRequest(&res, uri, query, req, rsp));
509 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
510 uint8_t numResources = 0;
511 uint8_t numExpectedResources = InitNumExpectedResources();
513 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
514 EXPECT_EQ(numExpectedResources, numResources);
516 EXPECT_EQ(OC_STACK_OK, OCStop());
519 TEST(StackResource, StackTestResourceDiscoverOneResource)
521 OC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverOneResource test");
522 InitStack(OC_SERVER);
524 OCResourceHandle handle;
525 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
530 OC_DISCOVERABLE|OC_OBSERVABLE));
531 const char *url = OCGetResourceUri(handle);
532 EXPECT_STREQ("/a/led", url);
534 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
535 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
537 EXPECT_EQ(OC_STACK_OK, OCStop());
540 TEST(StackResource, StackTestResourceDiscoverManyResources)
542 OC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverManyResources test");
543 InitStack(OC_SERVER);
545 OCResourceHandle handle1;
546 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
552 const char *url = OCGetResourceUri(handle1);
553 EXPECT_STREQ("/a/led1", url);
555 OCResourceHandle handle2;
556 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
561 OC_DISCOVERABLE|OC_OBSERVABLE));
562 url = OCGetResourceUri(handle2);
563 EXPECT_STREQ("/a/led2", url);
565 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.brightled"));
566 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.colorled"));
568 OCResourceHandle handle3;
569 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
574 OC_DISCOVERABLE|OC_OBSERVABLE));
575 url = OCGetResourceUri(handle3);
576 EXPECT_STREQ("/a/led3", url);
578 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.ll"));
579 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.b"));
581 OCResourceHandle handle4;
582 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
588 url = OCGetResourceUri(handle4);
589 EXPECT_STREQ("/a/led4", url);
591 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle4, "core.brightled"));
592 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, "oc.mi.ll"));
593 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, "oc.mi.b"));
595 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
597 EXPECT_EQ(OC_STACK_OK, OCStop());
600 TEST(StackBind, BindResourceTypeNameBad)
602 OC_LOG(INFO, TAG, "Starting BindResourceTypeNameBad test");
603 InitStack(OC_SERVER);
605 OCResourceHandle handle;
606 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
611 OC_DISCOVERABLE|OC_OBSERVABLE));
613 uint8_t numResourceTypes;
614 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
615 EXPECT_EQ(1, numResourceTypes);
616 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
617 EXPECT_STREQ("core.led", resourceTypeName);
619 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceTypeToResource(handle, NULL));
621 EXPECT_EQ(OC_STACK_OK, OCStop());
624 TEST(StackBind, BindResourceTypeNameGood)
626 OC_LOG(INFO, TAG, "Starting BindResourceTypeNameGood test");
627 InitStack(OC_SERVER);
629 OCResourceHandle handle;
630 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
635 OC_DISCOVERABLE|OC_OBSERVABLE));
637 uint8_t numResourceTypes;
638 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
639 EXPECT_EQ(1, numResourceTypes);
640 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
641 EXPECT_STREQ("core.led", resourceTypeName);
643 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
644 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
645 EXPECT_EQ(2, numResourceTypes);
646 resourceTypeName = OCGetResourceTypeName(handle, 1);
647 EXPECT_STREQ("core.brightled", resourceTypeName);
649 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
650 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
651 EXPECT_EQ(3, numResourceTypes);
652 resourceTypeName = OCGetResourceTypeName(handle, 2);
653 EXPECT_STREQ("core.reallybrightled", resourceTypeName);
655 EXPECT_EQ(OC_STACK_OK, OCStop());
658 TEST(StackBind, BindResourceTypeAttribRepGood)
660 OC_LOG(INFO, TAG, "Starting BindResourceTypeAttribRepGood test");
661 InitStack(OC_SERVER);
663 OCResourceHandle handle;
664 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
669 OC_DISCOVERABLE|OC_OBSERVABLE));
671 uint8_t numResourceTypes;
672 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
673 EXPECT_EQ(1, numResourceTypes);
675 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
676 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
677 EXPECT_EQ(2, numResourceTypes);
679 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
680 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
681 EXPECT_EQ(3, numResourceTypes);
683 EXPECT_EQ(OC_STACK_OK, OCStop());
687 TEST(StackBind, BindResourceInterfaceNameBad)
689 OC_LOG(INFO, TAG, "Starting BindResourceInterfaceNameBad test");
690 InitStack(OC_SERVER);
692 OCResourceHandle handle;
693 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
698 OC_DISCOVERABLE|OC_OBSERVABLE));
700 uint8_t numResourceInterfaces;
701 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
702 EXPECT_EQ(1, numResourceInterfaces);
703 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
704 EXPECT_STREQ("core.rw", resourceInterfaceName);
706 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, NULL));
708 EXPECT_EQ(OC_STACK_OK, OCStop());
711 TEST(StackBind, BindResourceInterfaceNameGood)
713 OC_LOG(INFO, TAG, "Starting BindResourceInterfaceNameGood test");
714 InitStack(OC_SERVER);
716 OCResourceHandle handle;
717 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
722 OC_DISCOVERABLE|OC_OBSERVABLE));
724 uint8_t numResourceInterfaces;
725 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
726 EXPECT_EQ(1, numResourceInterfaces);
727 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
728 EXPECT_STREQ("core.rw", resourceInterfaceName);
730 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
732 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
733 EXPECT_EQ(2, numResourceInterfaces);
734 resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
735 EXPECT_STREQ("core.r", resourceInterfaceName);
737 EXPECT_EQ(OC_STACK_OK, OCStop());
740 TEST(StackBind, BindResourceInterfaceMethodsBad)
742 OC_LOG(INFO, TAG, "Starting BindResourceInterfaceMethodsBad test");
743 InitStack(OC_SERVER);
745 OCResourceHandle handle;
746 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
751 OC_DISCOVERABLE|OC_OBSERVABLE));
753 uint8_t numResourceInterfaces;
754 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
755 EXPECT_EQ(1, numResourceInterfaces);
757 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, 0));
759 EXPECT_EQ(OC_STACK_OK, OCStop());
762 TEST(StackBind, BindResourceInterfaceMethodsGood)
764 OC_LOG(INFO, TAG, "Starting BindResourceInterfaceMethodsGood test");
765 InitStack(OC_SERVER);
767 OCResourceHandle handle;
768 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
773 OC_DISCOVERABLE|OC_OBSERVABLE));
775 uint8_t numResourceInterfaces;
776 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
777 EXPECT_EQ(1, numResourceInterfaces);
779 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
781 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
782 EXPECT_EQ(2, numResourceInterfaces);
784 EXPECT_EQ(OC_STACK_OK, OCStop());
787 TEST(StackBind, BindContainedResourceBad)
789 OC_LOG(INFO, TAG, "Starting BindContainedResourceBad test");
790 InitStack(OC_SERVER);
792 OCResourceHandle containerHandle;
793 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
798 OC_DISCOVERABLE|OC_OBSERVABLE));
800 OCResourceHandle handle0;
801 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
806 OC_DISCOVERABLE|OC_OBSERVABLE));
808 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResource(containerHandle, containerHandle));
810 EXPECT_EQ(OC_STACK_ERROR, OCBindResource((OCResourceHandle) 0, handle0));
812 EXPECT_EQ(OC_STACK_OK, OCStop());
815 TEST(StackBind, BindContainedResourceGood)
817 OC_LOG(INFO, TAG, "Starting BindContainedResourceGood test");
818 InitStack(OC_SERVER);
820 uint8_t numResources = 0;
821 uint8_t numExpectedResources = InitNumExpectedResources();
823 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
824 EXPECT_EQ(numExpectedResources, numResources);
826 OCResourceHandle containerHandle;
827 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
832 OC_DISCOVERABLE|OC_OBSERVABLE));
833 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
834 EXPECT_EQ(++numExpectedResources, numResources);
836 OCResourceHandle handle0;
837 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
842 OC_DISCOVERABLE|OC_OBSERVABLE));
843 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
844 EXPECT_EQ(++numExpectedResources, numResources);
846 OCResourceHandle handle1;
847 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
852 OC_DISCOVERABLE|OC_OBSERVABLE));
853 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
854 EXPECT_EQ(++numExpectedResources, numResources);
856 OCResourceHandle handle2;
857 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
862 OC_DISCOVERABLE|OC_OBSERVABLE));
863 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
864 EXPECT_EQ(++numExpectedResources, numResources);
866 OCResourceHandle handle3;
867 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
872 OC_DISCOVERABLE|OC_OBSERVABLE));
873 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
874 EXPECT_EQ(++numExpectedResources, numResources);
876 OCResourceHandle handle4;
877 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
882 OC_DISCOVERABLE|OC_OBSERVABLE));
883 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
884 EXPECT_EQ(++numExpectedResources, numResources);
886 OCResourceHandle handle5;
887 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
892 OC_DISCOVERABLE|OC_OBSERVABLE));
893 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
894 EXPECT_EQ(++numExpectedResources, numResources);
897 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle0));
898 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle1));
899 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle2));
900 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle3));
901 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle4));
902 EXPECT_EQ(OC_STACK_ERROR, OCBindResource(containerHandle, handle5));
904 EXPECT_EQ(handle0, OCGetResourceHandleFromCollection(containerHandle, 0));
905 EXPECT_EQ(handle1, OCGetResourceHandleFromCollection(containerHandle, 1));
906 EXPECT_EQ(handle2, OCGetResourceHandleFromCollection(containerHandle, 2));
907 EXPECT_EQ(handle3, OCGetResourceHandleFromCollection(containerHandle, 3));
908 EXPECT_EQ(handle4, OCGetResourceHandleFromCollection(containerHandle, 4));
910 EXPECT_EQ(NULL, OCGetResourceHandleFromCollection(containerHandle, 5));
912 EXPECT_EQ(OC_STACK_OK, OCStop());
916 TEST(StackBind, BindEntityHandlerBad)
918 OC_LOG(INFO, TAG, "Starting BindEntityHandlerBad test");
919 InitStack(OC_SERVER);
921 OCResourceHandle handle;
922 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
927 OC_DISCOVERABLE|OC_OBSERVABLE));
929 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceHandler(NULL, NULL));
931 EXPECT_EQ(OC_STACK_OK, OCStop());
934 TEST(StackBind, BindEntityHandlerGood)
936 OC_LOG(INFO, TAG, "Starting BindEntityHandlerGood test");
937 InitStack(OC_SERVER);
939 OCResourceHandle handle;
940 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
945 OC_DISCOVERABLE|OC_OBSERVABLE));
947 OCEntityHandler myHandler = entityHandler;
949 EXPECT_EQ(OC_STACK_OK, OCBindResourceHandler(handle, myHandler));
951 EXPECT_EQ(myHandler, OCGetResourceHandler(handle));
953 EXPECT_EQ(OC_STACK_OK, OCStop());
956 TEST(StackResourceAccess, GetResourceByIndex)
958 OC_LOG(INFO, TAG, "Starting GetResourceByIndex test");
959 InitStack(OC_SERVER);
961 uint8_t numResources = 0;
962 uint8_t numExpectedResources = InitNumExpectedResources();
963 uint8_t resourceIndex = InitResourceIndex();
965 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
966 EXPECT_EQ(numExpectedResources, numResources);
968 OCResourceHandle containerHandle;
969 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
974 OC_DISCOVERABLE|OC_OBSERVABLE));
975 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
976 EXPECT_EQ(++numExpectedResources, numResources);
978 OCResourceHandle handle0;
979 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
984 OC_DISCOVERABLE|OC_OBSERVABLE));
985 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
986 EXPECT_EQ(++numExpectedResources, numResources);
988 OCResourceHandle handle1;
989 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
994 OC_DISCOVERABLE|OC_OBSERVABLE));
995 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
996 EXPECT_EQ(++numExpectedResources, numResources);
998 OCResourceHandle handle2;
999 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1004 OC_DISCOVERABLE|OC_OBSERVABLE));
1005 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1006 EXPECT_EQ(++numExpectedResources, numResources);
1008 OCResourceHandle handle3;
1009 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
1014 OC_DISCOVERABLE|OC_OBSERVABLE));
1015 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1016 EXPECT_EQ(++numExpectedResources, numResources);
1018 OCResourceHandle handle4;
1019 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
1024 OC_DISCOVERABLE|OC_OBSERVABLE));
1025 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1026 EXPECT_EQ(++numExpectedResources, numResources);
1028 OCResourceHandle handle5;
1029 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
1034 OC_DISCOVERABLE|OC_OBSERVABLE));
1035 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1036 EXPECT_EQ(++numExpectedResources, numResources);
1038 EXPECT_EQ(containerHandle, OCGetResourceHandle(resourceIndex));
1039 EXPECT_EQ(handle0, OCGetResourceHandle(++resourceIndex));
1040 EXPECT_EQ(handle1, OCGetResourceHandle(++resourceIndex));
1041 EXPECT_EQ(handle2, OCGetResourceHandle(++resourceIndex));
1042 EXPECT_EQ(handle3, OCGetResourceHandle(++resourceIndex));
1043 EXPECT_EQ(handle4, OCGetResourceHandle(++resourceIndex));
1044 EXPECT_EQ(handle5, OCGetResourceHandle(++resourceIndex));
1046 EXPECT_EQ(OC_STACK_OK, OCStop());
1049 TEST(StackResourceAccess, DeleteHeadResource)
1051 OC_LOG(INFO, TAG, "Starting DeleteHeadResource test");
1052 InitStack(OC_SERVER);
1054 uint8_t numResources = 0;
1055 uint8_t numExpectedResources = InitNumExpectedResources();
1057 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1058 EXPECT_EQ(numExpectedResources, numResources);
1060 OCResourceHandle handle0;
1061 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1066 OC_DISCOVERABLE|OC_OBSERVABLE));
1067 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1068 EXPECT_EQ(++numExpectedResources, numResources);
1070 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1071 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1072 EXPECT_EQ(--numExpectedResources, numResources);
1074 EXPECT_EQ(OC_STACK_OK, OCStop());
1077 TEST(StackResourceAccess, DeleteHeadResource2)
1079 OC_LOG(INFO, TAG, "Starting DeleteHeadResource2 test");
1080 InitStack(OC_SERVER);
1082 uint8_t numResources = 0;
1083 uint8_t numExpectedResources = InitNumExpectedResources();
1084 uint8_t resourceIndex = InitResourceIndex();
1086 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1087 EXPECT_EQ(numExpectedResources, numResources);
1089 OCResourceHandle handle0;
1090 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1095 OC_DISCOVERABLE|OC_OBSERVABLE));
1096 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1097 EXPECT_EQ(++numExpectedResources, numResources);
1099 OCResourceHandle handle1;
1100 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1105 OC_DISCOVERABLE|OC_OBSERVABLE));
1106 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1107 EXPECT_EQ(++numExpectedResources, numResources);
1109 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1110 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1111 EXPECT_EQ(--numExpectedResources, numResources);
1113 EXPECT_EQ(handle1, OCGetResourceHandle(resourceIndex));
1115 EXPECT_EQ(OC_STACK_OK, OCStop());
1119 TEST(StackResourceAccess, DeleteLastResource)
1121 OC_LOG(INFO, TAG, "Starting DeleteLastResource test");
1122 InitStack(OC_SERVER);
1124 uint8_t numResources = 0;
1125 uint8_t numExpectedResources = InitNumExpectedResources();
1126 uint8_t resourceIndex = InitResourceIndex();
1128 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1129 EXPECT_EQ(numExpectedResources, numResources);
1131 OCResourceHandle handle0;
1132 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1137 OC_DISCOVERABLE|OC_OBSERVABLE));
1138 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1139 EXPECT_EQ(++numExpectedResources, numResources);
1141 OCResourceHandle handle1;
1142 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1147 OC_DISCOVERABLE|OC_OBSERVABLE));
1148 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1149 EXPECT_EQ(++numExpectedResources, numResources);
1151 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1152 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1153 EXPECT_EQ(--numExpectedResources, numResources);
1155 EXPECT_EQ(handle0, OCGetResourceHandle(resourceIndex));
1157 OCResourceHandle handle2;
1158 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1163 OC_DISCOVERABLE|OC_OBSERVABLE));
1164 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1165 EXPECT_EQ(++numExpectedResources, numResources);
1167 EXPECT_EQ(OC_STACK_OK, OCStop());
1170 TEST(StackResourceAccess, DeleteMiddleResource)
1172 OC_LOG(INFO, TAG, "Starting DeleteMiddleResource test");
1173 InitStack(OC_SERVER);
1175 uint8_t numResources = 0;
1176 uint8_t numExpectedResources = InitNumExpectedResources();
1177 uint8_t resourceIndex = InitResourceIndex();
1179 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1180 EXPECT_EQ(numExpectedResources, numResources);
1182 OCResourceHandle handle0;
1183 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1188 OC_DISCOVERABLE|OC_OBSERVABLE));
1189 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1190 EXPECT_EQ(++numExpectedResources, numResources);
1192 OCResourceHandle handle1;
1193 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1198 OC_DISCOVERABLE|OC_OBSERVABLE));
1199 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1200 EXPECT_EQ(++numExpectedResources, numResources);
1202 OCResourceHandle handle2;
1203 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1208 OC_DISCOVERABLE|OC_OBSERVABLE));
1209 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1210 EXPECT_EQ(++numExpectedResources, numResources);
1212 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1213 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1214 EXPECT_EQ(--numExpectedResources, numResources);
1216 EXPECT_EQ(handle0, OCGetResourceHandle(resourceIndex));
1217 EXPECT_EQ(handle2, OCGetResourceHandle(++resourceIndex));
1219 // Make sure the resource elements are still correct
1220 uint8_t numResourceInterfaces;
1221 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle2, &numResourceInterfaces));
1222 EXPECT_EQ(1, numResourceInterfaces);
1223 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle2, 0);
1224 EXPECT_STREQ("core.rw", resourceInterfaceName);
1226 EXPECT_EQ(OC_STACK_OK, OCStop());
1232 #endif // __cplusplus
1233 void parsePresencePayload(char* payload, uint32_t* seqNum, uint32_t* maxAge, char** resType);
1236 #endif // __cplusplus
1238 TEST(StackPresence, ParsePresencePayload)
1241 OC_LOG(INFO, TAG, "Starting ParsePresencePayload test");
1244 uint32_t seqNum = 0, maxAge = 0;
1245 char * resType = NULL;
1248 strncpy(payload, "{\"oc\":[100:99:presence]}", sizeof(payload));
1249 parsePresencePayload(payload, &seqNum, &maxAge, &resType);
1250 EXPECT_TRUE(100 == seqNum);
1251 EXPECT_TRUE(99 == maxAge);
1252 EXPECT_STREQ("presence", resType);
1255 //Bad Scenario -- should not result in Seg Fault
1256 parsePresencePayload(payload, NULL, &maxAge, &resType);
1259 seqNum = 0; maxAge = 0; resType = NULL;
1260 strncpy(payload, "{abracadabra}", sizeof(payload));
1261 parsePresencePayload(payload, &seqNum, &maxAge, &resType);
1262 EXPECT_TRUE(0 == seqNum);
1263 EXPECT_TRUE(0 == maxAge);
1264 EXPECT_EQ(NULL, resType);
1268 seqNum = 0; maxAge = 0; resType = NULL;
1269 strncpy(payload, "{\"oc\":[100]}", sizeof(payload));
1270 parsePresencePayload(payload, &seqNum, &maxAge, &resType);
1271 EXPECT_TRUE(100 == seqNum);
1272 EXPECT_TRUE(0 == maxAge);
1273 EXPECT_EQ(NULL, resType);
1277 seqNum = 0; maxAge = 0; resType = NULL;
1278 strncpy(payload, "{\"oc\":[]}", sizeof(payload));
1279 parsePresencePayload(payload, &seqNum, &maxAge, &resType);
1280 EXPECT_TRUE(0 == seqNum);
1281 EXPECT_TRUE(0 == maxAge);
1282 EXPECT_EQ(NULL, resType);
1286 strncpy(payload, "{:]}", sizeof(payload));
1287 parsePresencePayload(payload, &seqNum, &maxAge, &resType);
1288 EXPECT_TRUE(0 == seqNum);
1289 EXPECT_TRUE(0 == maxAge);
1290 EXPECT_EQ(NULL, resType);
1294 strncpy(payload, "{:[presence}", sizeof(payload));
1295 parsePresencePayload(payload, &seqNum, &maxAge, &resType);
1296 EXPECT_TRUE(0 == seqNum);
1297 EXPECT_TRUE(0 == maxAge);
1298 EXPECT_EQ(NULL, resType);