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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
28 #include "gtest/gtest.h"
29 #include <sys/types.h>
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
46 #define DEFAULT_CONTEXT_VALUE 0x99
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 static const char TAG[] = "TestHarness";
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 extern "C" OCStackApplicationResult asyncDoResourcesCallback(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
57 OC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
59 EXPECT_EQ(OC_STACK_OK, clientResponse->result);
61 if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
62 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
64 OC_LOG_V(INFO, TAG, "result = %d", clientResponse->result);
66 return OC_STACK_KEEP_TRANSACTION;
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
72 OCEntityHandlerResult entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest)
74 OC_LOG(INFO, TAG, "Entering entityHandler");
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 void InitStack(OCMode mode)
84 OC_LOG(INFO, TAG, "Entering InitStack");
86 EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, mode));
87 OC_LOG(INFO, TAG, "Leaving InitStack");
90 uint8_t InitNumExpectedResources()
93 // When presence is enabled, it is a resource and so is (currently) included
94 // in the returned resource count returned by the OCGetNumberOfResources API.
101 uint8_t InitResourceIndex()
104 // When presence is enabled, it is a resource and so is (currently) included
105 // in the returned resource count returned by the OCGetNumberOfResources API.
106 // The index of the presence resource is 0, so the first user app resource index
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
117 TEST(StackInit, StackInitNullAddr)
119 EXPECT_EQ(OC_STACK_OK, OCInit(0, 5683, OC_SERVER));
120 EXPECT_EQ(OC_STACK_OK, OCStop());
123 TEST(StackInit, StackInitNullPort)
125 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 0, OC_SERVER));
126 EXPECT_EQ(OC_STACK_OK, OCStop());
129 TEST(StackInit, StackInitNullAddrAndPort)
131 EXPECT_EQ(OC_STACK_OK, OCInit(0, 0, OC_SERVER));
132 EXPECT_EQ(OC_STACK_OK, OCStop());
135 TEST(StackInit, StackInitInvalidMode)
137 EXPECT_EQ(OC_STACK_ERROR, OCInit(0, 0, (OCMode)10));
138 EXPECT_EQ(OC_STACK_ERROR, OCStop());
141 TEST(StackStart, StackStartSuccessClient)
143 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
144 EXPECT_EQ(OC_STACK_OK, OCStop());
147 TEST(StackStart, StackStartSuccessServer)
149 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
150 EXPECT_EQ(OC_STACK_OK, OCStop());
153 TEST(StackStart, StackStartSuccessClientServer)
155 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
156 EXPECT_EQ(OC_STACK_OK, OCStop());
159 TEST(StackStart, StackStartSuccessiveInits)
161 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
162 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.2", 5683, OC_SERVER));
163 EXPECT_EQ(OC_STACK_OK, OCStop());
166 TEST(StackDiscovery, DoResourceDeviceDiscovery)
168 OCCallbackData cbData;
171 OC_LOG(INFO, TAG, "Starting DoResourceDeviceDiscovery test ");
172 InitStack(OC_CLIENT);
174 /* Start a discovery query*/
175 char szQueryUri[64] = { 0 };
176 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
177 cbData.cb = asyncDoResourcesCallback;
178 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
180 EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle,
190 EXPECT_EQ(OC_STACK_OK, OCStop());
193 TEST(StackStop, StackStopWithoutInit)
195 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
196 EXPECT_EQ(OC_STACK_OK, OCStop());
197 EXPECT_EQ(OC_STACK_ERROR, OCStop());
200 TEST(StackResource, UpdateResourceNullURI)
202 OCCallbackData cbData;
205 OC_LOG(INFO, TAG, "Starting UpdateResourceNullURI test");
206 InitStack(OC_CLIENT);
208 /* Start a discovery query*/
209 char szQueryUri[64] = { 0 };
210 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
211 cbData.cb = asyncDoResourcesCallback;
212 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
214 EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle,
224 EXPECT_EQ(OC_STACK_OK, OCStop());
227 TEST(StackResource, CreateResourceBadParams)
229 OC_LOG(INFO, TAG, "Starting CreateResourceBadParams test");
230 InitStack(OC_SERVER);
232 OCResourceHandle handle;
234 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(NULL, //&handle,
239 OC_DISCOVERABLE|OC_OBSERVABLE));
241 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
246 OC_DISCOVERABLE|OC_OBSERVABLE));
248 // Property bitmask out of range
249 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
254 128));// invalid bitmask for OCResourceProperty
256 EXPECT_EQ(OC_STACK_OK, OCStop());
260 TEST(StackResource, CreateResourceSuccess)
262 OC_LOG(INFO, TAG, "Starting CreateResourceSuccess test");
263 InitStack(OC_SERVER);
265 OCResourceHandle handle;
266 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
271 OC_DISCOVERABLE|OC_OBSERVABLE));
272 const char *url = OCGetResourceUri(handle);
273 EXPECT_STREQ("/a/led", url);
275 EXPECT_EQ(OC_STACK_OK, OCStop());
278 TEST(StackResource, CreateResourceFailDuplicateUri)
280 OC_LOG(INFO, TAG, "Starting CreateResourceFailDuplicateUri test");
281 InitStack(OC_SERVER);
283 OCResourceHandle handle;
284 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
289 OC_DISCOVERABLE|OC_OBSERVABLE));
290 const char *url = OCGetResourceUri(handle);
291 EXPECT_STREQ("/a/led", url);
293 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
298 OC_DISCOVERABLE|OC_OBSERVABLE));
300 EXPECT_EQ(OC_STACK_OK, OCStop());
303 TEST(StackResource, CreateResourceMultipleResources)
305 OC_LOG(INFO, TAG, "Starting CreateResourceMultipleResources test");
306 InitStack(OC_SERVER);
308 OCResourceHandle handle1;
309 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
314 OC_DISCOVERABLE|OC_OBSERVABLE));
316 OCResourceHandle handle2;
317 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
322 OC_DISCOVERABLE|OC_OBSERVABLE));
323 OCResourceHandle handle3;
324 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
329 OC_DISCOVERABLE|OC_OBSERVABLE));
331 const char *url = OCGetResourceUri(handle1);
332 EXPECT_STREQ("/a/led1", url);
334 url = OCGetResourceUri(handle2);
335 EXPECT_STREQ("/a/led2", url);
337 url = OCGetResourceUri(handle3);
338 EXPECT_STREQ("/a/led3", url);
340 EXPECT_EQ(OC_STACK_OK, OCStop());
343 TEST(StackResource, CreateResourceBadResoureType)
345 OC_LOG(INFO, TAG, "Starting CreateResourceBadResoureType test");
346 InitStack(OC_SERVER);
348 OCResourceHandle handle;
349 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
354 OC_DISCOVERABLE|OC_OBSERVABLE));
356 EXPECT_EQ(OC_STACK_OK, OCStop());
359 TEST(StackResource, CreateResourceGoodResourceType)
361 OC_LOG(INFO, TAG, "Starting CreateResourceGoodResourceType test");
362 InitStack(OC_SERVER);
364 OCResourceHandle handle;
365 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
370 OC_DISCOVERABLE|OC_OBSERVABLE));
372 EXPECT_EQ(OC_STACK_OK, OCStop());
375 TEST(StackResource, ResourceTypeName)
377 OC_LOG(INFO, TAG, "Starting ResourceTypeName test");
378 InitStack(OC_SERVER);
380 OCResourceHandle handle;
381 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
386 OC_DISCOVERABLE|OC_OBSERVABLE));
388 uint8_t numResourceTypes;
389 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
390 EXPECT_EQ(1, numResourceTypes);
391 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
392 EXPECT_STREQ("core.led", resourceTypeName);
394 // try getting resource type names with an invalid index
395 resourceTypeName = OCGetResourceTypeName(handle, 1);
396 EXPECT_STREQ(NULL, resourceTypeName);
397 // try getting resource type names with an invalid index
398 resourceTypeName = OCGetResourceTypeName(handle, 10);
399 EXPECT_STREQ(NULL, resourceTypeName);
401 EXPECT_EQ(OC_STACK_OK, OCStop());
404 TEST(StackResource, ResourceTypeAttrRepresentation)
406 OC_LOG(INFO, TAG, "Starting ResourceTypeAttrRepresentation test");
407 InitStack(OC_SERVER);
409 OCResourceHandle handle;
410 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
415 OC_DISCOVERABLE|OC_OBSERVABLE));
417 uint8_t numResourceTypes;
418 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
419 EXPECT_EQ(1, numResourceTypes);
421 EXPECT_EQ(OC_STACK_OK, OCStop());
424 TEST(StackResource, ResourceTypeInterface)
426 OC_LOG(INFO, TAG, "Starting ResourceTypeInterface test");
427 InitStack(OC_SERVER);
429 OCResourceHandle handle;
430 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
435 OC_DISCOVERABLE|OC_OBSERVABLE));
437 uint8_t numResourceInterfaces;
438 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
439 EXPECT_EQ(1, numResourceInterfaces);
440 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
441 EXPECT_STREQ("core.rw", resourceInterfaceName);
443 // try getting resource interface names with an invalid index
444 resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
445 EXPECT_STREQ(NULL, resourceInterfaceName);
446 // try getting resource interface names with an invalid index
447 resourceInterfaceName = OCGetResourceInterfaceName(handle, 10);
448 EXPECT_STREQ(NULL, resourceInterfaceName);
450 EXPECT_EQ(OC_STACK_OK, OCStop());
453 TEST(StackResource, ResourceTypeInterfaceMethods)
455 OC_LOG(INFO, TAG, "Starting ResourceTypeInterfaceMethods test");
456 InitStack(OC_SERVER);
458 OCResourceHandle handle;
459 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
464 OC_DISCOVERABLE|OC_OBSERVABLE));
466 uint8_t numResourceInterfaces;
467 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
468 EXPECT_EQ(1, numResourceInterfaces);
470 EXPECT_EQ(OC_STACK_OK, OCStop());
473 TEST(StackResource, GetResourceProperties)
475 OC_LOG(INFO, TAG, "Starting GetResourceProperties test");
476 InitStack(OC_SERVER);
478 OCResourceHandle handle;
479 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
484 OC_DISCOVERABLE|OC_OBSERVABLE));
486 EXPECT_EQ(OC_ACTIVE|OC_DISCOVERABLE|OC_OBSERVABLE, OCGetResourceProperties(handle));
487 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
489 EXPECT_EQ(OC_STACK_OK, OCStop());
492 TEST(StackResource, StackTestResourceDiscoverOneResourceBad)
494 OC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverOneResourceBad test");
495 InitStack(OC_SERVER);
497 OCResourceHandle handle;
498 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
503 OC_DISCOVERABLE|OC_OBSERVABLE));
504 const char *url = OCGetResourceUri(handle);
505 EXPECT_STREQ("/a1/led", url);
507 //EXPECT_EQ(OC_STACK_INVALID_URI, OCHandleServerRequest(&res, uri, query, req, rsp));
508 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
509 uint8_t numResources = 0;
510 uint8_t numExpectedResources = InitNumExpectedResources();
512 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
513 EXPECT_EQ(numExpectedResources, numResources);
515 EXPECT_EQ(OC_STACK_OK, OCStop());
518 TEST(StackResource, StackTestResourceDiscoverOneResource)
520 OC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverOneResource test");
521 InitStack(OC_SERVER);
523 OCResourceHandle handle;
524 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
529 OC_DISCOVERABLE|OC_OBSERVABLE));
530 const char *url = OCGetResourceUri(handle);
531 EXPECT_STREQ("/a/led", url);
533 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
534 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
536 EXPECT_EQ(OC_STACK_OK, OCStop());
539 TEST(StackResource, StackTestResourceDiscoverManyResources)
541 OC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverManyResources test");
542 InitStack(OC_SERVER);
544 OCResourceHandle handle1;
545 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
551 const char *url = OCGetResourceUri(handle1);
552 EXPECT_STREQ("/a/led1", url);
554 OCResourceHandle handle2;
555 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
560 OC_DISCOVERABLE|OC_OBSERVABLE));
561 url = OCGetResourceUri(handle2);
562 EXPECT_STREQ("/a/led2", url);
564 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.brightled"));
565 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.colorled"));
567 OCResourceHandle handle3;
568 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
573 OC_DISCOVERABLE|OC_OBSERVABLE));
574 url = OCGetResourceUri(handle3);
575 EXPECT_STREQ("/a/led3", url);
577 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.ll"));
578 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.b"));
580 OCResourceHandle handle4;
581 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
587 url = OCGetResourceUri(handle4);
588 EXPECT_STREQ("/a/led4", url);
590 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle4, "core.brightled"));
591 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, "oc.mi.ll"));
592 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, "oc.mi.b"));
594 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
596 EXPECT_EQ(OC_STACK_OK, OCStop());
599 TEST(StackBind, BindResourceTypeNameBad)
601 OC_LOG(INFO, TAG, "Starting BindResourceTypeNameBad test");
602 InitStack(OC_SERVER);
604 OCResourceHandle handle;
605 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
610 OC_DISCOVERABLE|OC_OBSERVABLE));
612 uint8_t numResourceTypes;
613 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
614 EXPECT_EQ(1, numResourceTypes);
615 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
616 EXPECT_STREQ("core.led", resourceTypeName);
618 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceTypeToResource(handle, NULL));
620 EXPECT_EQ(OC_STACK_OK, OCStop());
623 TEST(StackBind, BindResourceTypeNameGood)
625 OC_LOG(INFO, TAG, "Starting BindResourceTypeNameGood test");
626 InitStack(OC_SERVER);
628 OCResourceHandle handle;
629 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
634 OC_DISCOVERABLE|OC_OBSERVABLE));
636 uint8_t numResourceTypes;
637 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
638 EXPECT_EQ(1, numResourceTypes);
639 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
640 EXPECT_STREQ("core.led", resourceTypeName);
642 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
643 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
644 EXPECT_EQ(2, numResourceTypes);
645 resourceTypeName = OCGetResourceTypeName(handle, 1);
646 EXPECT_STREQ("core.brightled", resourceTypeName);
648 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
649 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
650 EXPECT_EQ(3, numResourceTypes);
651 resourceTypeName = OCGetResourceTypeName(handle, 2);
652 EXPECT_STREQ("core.reallybrightled", resourceTypeName);
654 EXPECT_EQ(OC_STACK_OK, OCStop());
657 TEST(StackBind, BindResourceTypeAttribRepGood)
659 OC_LOG(INFO, TAG, "Starting BindResourceTypeAttribRepGood test");
660 InitStack(OC_SERVER);
662 OCResourceHandle handle;
663 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
668 OC_DISCOVERABLE|OC_OBSERVABLE));
670 uint8_t numResourceTypes;
671 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
672 EXPECT_EQ(1, numResourceTypes);
674 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
675 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
676 EXPECT_EQ(2, numResourceTypes);
678 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
679 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
680 EXPECT_EQ(3, numResourceTypes);
682 EXPECT_EQ(OC_STACK_OK, OCStop());
686 TEST(StackBind, BindResourceInterfaceNameBad)
688 OC_LOG(INFO, TAG, "Starting BindResourceInterfaceNameBad test");
689 InitStack(OC_SERVER);
691 OCResourceHandle handle;
692 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
697 OC_DISCOVERABLE|OC_OBSERVABLE));
699 uint8_t numResourceInterfaces;
700 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
701 EXPECT_EQ(1, numResourceInterfaces);
702 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
703 EXPECT_STREQ("core.rw", resourceInterfaceName);
705 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, NULL));
707 EXPECT_EQ(OC_STACK_OK, OCStop());
710 TEST(StackBind, BindResourceInterfaceNameGood)
712 OC_LOG(INFO, TAG, "Starting BindResourceInterfaceNameGood test");
713 InitStack(OC_SERVER);
715 OCResourceHandle handle;
716 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
721 OC_DISCOVERABLE|OC_OBSERVABLE));
723 uint8_t numResourceInterfaces;
724 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
725 EXPECT_EQ(1, numResourceInterfaces);
726 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
727 EXPECT_STREQ("core.rw", resourceInterfaceName);
729 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
731 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
732 EXPECT_EQ(2, numResourceInterfaces);
733 resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
734 EXPECT_STREQ("core.r", resourceInterfaceName);
736 EXPECT_EQ(OC_STACK_OK, OCStop());
739 TEST(StackBind, BindResourceInterfaceMethodsBad)
741 OC_LOG(INFO, TAG, "Starting BindResourceInterfaceMethodsBad test");
742 InitStack(OC_SERVER);
744 OCResourceHandle handle;
745 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
750 OC_DISCOVERABLE|OC_OBSERVABLE));
752 uint8_t numResourceInterfaces;
753 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
754 EXPECT_EQ(1, numResourceInterfaces);
756 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, 0));
758 EXPECT_EQ(OC_STACK_OK, OCStop());
761 TEST(StackBind, BindResourceInterfaceMethodsGood)
763 OC_LOG(INFO, TAG, "Starting BindResourceInterfaceMethodsGood test");
764 InitStack(OC_SERVER);
766 OCResourceHandle handle;
767 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
772 OC_DISCOVERABLE|OC_OBSERVABLE));
774 uint8_t numResourceInterfaces;
775 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
776 EXPECT_EQ(1, numResourceInterfaces);
778 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
780 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
781 EXPECT_EQ(2, numResourceInterfaces);
783 EXPECT_EQ(OC_STACK_OK, OCStop());
786 TEST(StackBind, BindContainedResourceBad)
788 OC_LOG(INFO, TAG, "Starting BindContainedResourceBad test");
789 InitStack(OC_SERVER);
791 OCResourceHandle containerHandle;
792 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
797 OC_DISCOVERABLE|OC_OBSERVABLE));
799 OCResourceHandle handle0;
800 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
805 OC_DISCOVERABLE|OC_OBSERVABLE));
807 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResource(containerHandle, containerHandle));
809 EXPECT_EQ(OC_STACK_ERROR, OCBindResource((OCResourceHandle) 0, handle0));
811 EXPECT_EQ(OC_STACK_OK, OCStop());
814 TEST(StackBind, BindContainedResourceGood)
816 OC_LOG(INFO, TAG, "Starting BindContainedResourceGood test");
817 InitStack(OC_SERVER);
819 uint8_t numResources = 0;
820 uint8_t numExpectedResources = InitNumExpectedResources();
822 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
823 EXPECT_EQ(numExpectedResources, numResources);
825 OCResourceHandle containerHandle;
826 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
831 OC_DISCOVERABLE|OC_OBSERVABLE));
832 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
833 EXPECT_EQ(++numExpectedResources, numResources);
835 OCResourceHandle handle0;
836 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
841 OC_DISCOVERABLE|OC_OBSERVABLE));
842 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
843 EXPECT_EQ(++numExpectedResources, numResources);
845 OCResourceHandle handle1;
846 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
851 OC_DISCOVERABLE|OC_OBSERVABLE));
852 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
853 EXPECT_EQ(++numExpectedResources, numResources);
855 OCResourceHandle handle2;
856 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
861 OC_DISCOVERABLE|OC_OBSERVABLE));
862 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
863 EXPECT_EQ(++numExpectedResources, numResources);
865 OCResourceHandle handle3;
866 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
871 OC_DISCOVERABLE|OC_OBSERVABLE));
872 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
873 EXPECT_EQ(++numExpectedResources, numResources);
875 OCResourceHandle handle4;
876 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
881 OC_DISCOVERABLE|OC_OBSERVABLE));
882 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
883 EXPECT_EQ(++numExpectedResources, numResources);
885 OCResourceHandle handle5;
886 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
891 OC_DISCOVERABLE|OC_OBSERVABLE));
892 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
893 EXPECT_EQ(++numExpectedResources, numResources);
896 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle0));
897 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle1));
898 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle2));
899 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle3));
900 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle4));
901 EXPECT_EQ(OC_STACK_ERROR, OCBindResource(containerHandle, handle5));
903 EXPECT_EQ(handle0, OCGetResourceHandleFromCollection(containerHandle, 0));
904 EXPECT_EQ(handle1, OCGetResourceHandleFromCollection(containerHandle, 1));
905 EXPECT_EQ(handle2, OCGetResourceHandleFromCollection(containerHandle, 2));
906 EXPECT_EQ(handle3, OCGetResourceHandleFromCollection(containerHandle, 3));
907 EXPECT_EQ(handle4, OCGetResourceHandleFromCollection(containerHandle, 4));
909 EXPECT_EQ(NULL, OCGetResourceHandleFromCollection(containerHandle, 5));
911 EXPECT_EQ(OC_STACK_OK, OCStop());
915 TEST(StackBind, BindEntityHandlerBad)
917 OC_LOG(INFO, TAG, "Starting BindEntityHandlerBad test");
918 InitStack(OC_SERVER);
920 OCResourceHandle handle;
921 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
926 OC_DISCOVERABLE|OC_OBSERVABLE));
928 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceHandler(NULL, NULL));
930 EXPECT_EQ(OC_STACK_OK, OCStop());
933 TEST(StackBind, BindEntityHandlerGood)
935 OC_LOG(INFO, TAG, "Starting BindEntityHandlerGood test");
936 InitStack(OC_SERVER);
938 OCResourceHandle handle;
939 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
944 OC_DISCOVERABLE|OC_OBSERVABLE));
946 OCEntityHandler myHandler = entityHandler;
948 EXPECT_EQ(OC_STACK_OK, OCBindResourceHandler(handle, myHandler));
950 EXPECT_EQ(myHandler, OCGetResourceHandler(handle));
952 EXPECT_EQ(OC_STACK_OK, OCStop());
955 TEST(StackResourceAccess, GetResourceByIndex)
957 OC_LOG(INFO, TAG, "Starting GetResourceByIndex test");
958 InitStack(OC_SERVER);
960 uint8_t numResources = 0;
961 uint8_t numExpectedResources = InitNumExpectedResources();
962 uint8_t resourceIndex = InitResourceIndex();
964 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
965 EXPECT_EQ(numExpectedResources, numResources);
967 OCResourceHandle containerHandle;
968 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
973 OC_DISCOVERABLE|OC_OBSERVABLE));
974 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
975 EXPECT_EQ(++numExpectedResources, numResources);
977 OCResourceHandle handle0;
978 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
983 OC_DISCOVERABLE|OC_OBSERVABLE));
984 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
985 EXPECT_EQ(++numExpectedResources, numResources);
987 OCResourceHandle handle1;
988 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
993 OC_DISCOVERABLE|OC_OBSERVABLE));
994 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
995 EXPECT_EQ(++numExpectedResources, numResources);
997 OCResourceHandle handle2;
998 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1003 OC_DISCOVERABLE|OC_OBSERVABLE));
1004 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1005 EXPECT_EQ(++numExpectedResources, numResources);
1007 OCResourceHandle handle3;
1008 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
1013 OC_DISCOVERABLE|OC_OBSERVABLE));
1014 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1015 EXPECT_EQ(++numExpectedResources, numResources);
1017 OCResourceHandle handle4;
1018 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
1023 OC_DISCOVERABLE|OC_OBSERVABLE));
1024 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1025 EXPECT_EQ(++numExpectedResources, numResources);
1027 OCResourceHandle handle5;
1028 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
1033 OC_DISCOVERABLE|OC_OBSERVABLE));
1034 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1035 EXPECT_EQ(++numExpectedResources, numResources);
1037 EXPECT_EQ(containerHandle, OCGetResourceHandle(resourceIndex));
1038 EXPECT_EQ(handle0, OCGetResourceHandle(++resourceIndex));
1039 EXPECT_EQ(handle1, OCGetResourceHandle(++resourceIndex));
1040 EXPECT_EQ(handle2, OCGetResourceHandle(++resourceIndex));
1041 EXPECT_EQ(handle3, OCGetResourceHandle(++resourceIndex));
1042 EXPECT_EQ(handle4, OCGetResourceHandle(++resourceIndex));
1043 EXPECT_EQ(handle5, OCGetResourceHandle(++resourceIndex));
1045 EXPECT_EQ(OC_STACK_OK, OCStop());
1048 TEST(StackResourceAccess, DeleteHeadResource)
1050 OC_LOG(INFO, TAG, "Starting DeleteHeadResource test");
1051 InitStack(OC_SERVER);
1053 uint8_t numResources = 0;
1054 uint8_t numExpectedResources = InitNumExpectedResources();
1056 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1057 EXPECT_EQ(numExpectedResources, numResources);
1059 OCResourceHandle handle0;
1060 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1065 OC_DISCOVERABLE|OC_OBSERVABLE));
1066 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1067 EXPECT_EQ(++numExpectedResources, numResources);
1069 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1070 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1071 EXPECT_EQ(--numExpectedResources, numResources);
1073 EXPECT_EQ(OC_STACK_OK, OCStop());
1076 TEST(StackResourceAccess, DeleteHeadResource2)
1078 OC_LOG(INFO, TAG, "Starting DeleteHeadResource2 test");
1079 InitStack(OC_SERVER);
1081 uint8_t numResources = 0;
1082 uint8_t numExpectedResources = InitNumExpectedResources();
1083 uint8_t resourceIndex = InitResourceIndex();
1085 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1086 EXPECT_EQ(numExpectedResources, numResources);
1088 OCResourceHandle handle0;
1089 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1094 OC_DISCOVERABLE|OC_OBSERVABLE));
1095 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1096 EXPECT_EQ(++numExpectedResources, numResources);
1098 OCResourceHandle handle1;
1099 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1104 OC_DISCOVERABLE|OC_OBSERVABLE));
1105 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1106 EXPECT_EQ(++numExpectedResources, numResources);
1108 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1109 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1110 EXPECT_EQ(--numExpectedResources, numResources);
1112 EXPECT_EQ(handle1, OCGetResourceHandle(resourceIndex));
1114 EXPECT_EQ(OC_STACK_OK, OCStop());
1118 TEST(StackResourceAccess, DeleteLastResource)
1120 OC_LOG(INFO, TAG, "Starting DeleteLastResource test");
1121 InitStack(OC_SERVER);
1123 uint8_t numResources = 0;
1124 uint8_t numExpectedResources = InitNumExpectedResources();
1125 uint8_t resourceIndex = InitResourceIndex();
1127 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1128 EXPECT_EQ(numExpectedResources, numResources);
1130 OCResourceHandle handle0;
1131 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1136 OC_DISCOVERABLE|OC_OBSERVABLE));
1137 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1138 EXPECT_EQ(++numExpectedResources, numResources);
1140 OCResourceHandle handle1;
1141 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1146 OC_DISCOVERABLE|OC_OBSERVABLE));
1147 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1148 EXPECT_EQ(++numExpectedResources, numResources);
1150 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1151 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1152 EXPECT_EQ(--numExpectedResources, numResources);
1154 EXPECT_EQ(handle0, OCGetResourceHandle(resourceIndex));
1156 OCResourceHandle handle2;
1157 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1162 OC_DISCOVERABLE|OC_OBSERVABLE));
1163 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1164 EXPECT_EQ(++numExpectedResources, numResources);
1166 EXPECT_EQ(OC_STACK_OK, OCStop());
1169 TEST(StackResourceAccess, DeleteMiddleResource)
1171 OC_LOG(INFO, TAG, "Starting DeleteMiddleResource test");
1172 InitStack(OC_SERVER);
1174 uint8_t numResources = 0;
1175 uint8_t numExpectedResources = InitNumExpectedResources();
1176 uint8_t resourceIndex = InitResourceIndex();
1178 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1179 EXPECT_EQ(numExpectedResources, numResources);
1181 OCResourceHandle handle0;
1182 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1187 OC_DISCOVERABLE|OC_OBSERVABLE));
1188 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1189 EXPECT_EQ(++numExpectedResources, numResources);
1191 OCResourceHandle handle1;
1192 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1197 OC_DISCOVERABLE|OC_OBSERVABLE));
1198 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1199 EXPECT_EQ(++numExpectedResources, numResources);
1201 OCResourceHandle handle2;
1202 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1207 OC_DISCOVERABLE|OC_OBSERVABLE));
1208 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1209 EXPECT_EQ(++numExpectedResources, numResources);
1211 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1212 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1213 EXPECT_EQ(--numExpectedResources, numResources);
1215 EXPECT_EQ(handle0, OCGetResourceHandle(resourceIndex));
1216 EXPECT_EQ(handle2, OCGetResourceHandle(++resourceIndex));
1218 // Make sure the resource elements are still correct
1219 uint8_t numResourceInterfaces;
1220 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle2, &numResourceInterfaces));
1221 EXPECT_EQ(1, numResourceInterfaces);
1222 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle2, 0);
1223 EXPECT_STREQ("core.rw", resourceInterfaceName);
1225 EXPECT_EQ(OC_STACK_OK, OCStop());