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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "ocstackinternal.h"
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) {
56 OC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
58 EXPECT_EQ(OC_STACK_OK, clientResponse->result);
60 if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
61 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
63 OC_LOG_V(INFO, TAG, "result = %d", clientResponse->result);
65 return OC_STACK_KEEP_TRANSACTION;
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 TEST(StackTest, StackInitNullAddr) {
74 EXPECT_EQ(OC_STACK_OK, OCInit(0, 5683, OC_SERVER));
75 EXPECT_EQ(OC_STACK_OK, OCStop());
78 TEST(StackTest, StackInitNullPort) {
79 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 0, OC_SERVER));
80 EXPECT_EQ(OC_STACK_OK, OCStop());
83 TEST(StackTest, StackInitNullAddrAndPort) {
84 EXPECT_EQ(OC_STACK_OK, OCInit(0, 0, OC_SERVER));
85 EXPECT_EQ(OC_STACK_OK, OCStop());
88 TEST(StackTest, StackInitInvalidMode) {
89 EXPECT_EQ(OC_STACK_ERROR, OCInit(0, 0, (OCMode)10));
90 EXPECT_EQ(OC_STACK_ERROR, OCStop());
93 TEST(StackTest, StackStartSuccessClient) {
94 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
95 EXPECT_EQ(OC_STACK_OK, OCStop());
98 TEST(StackTest, StackStartSuccessServer) {
99 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
100 EXPECT_EQ(OC_STACK_OK, OCStop());
103 TEST(StackTest, StackStartSuccessClientServer) {
104 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
105 EXPECT_EQ(OC_STACK_OK, OCStop());
108 TEST(StackTest, StackStartSuccessiveInits) {
109 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
110 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.2", 5683, OC_SERVER));
111 EXPECT_EQ(OC_STACK_OK, OCStop());
114 TEST(StackTest, DoResourceDeviceDiscovery) {
116 uint16_t port = USE_RANDOM_PORT;
117 uint8_t ifname[] = "eth0";
118 OCCallbackData cbData;
121 /*Get Ip address on defined interface and initialize coap on it with random port number
122 * this port number will be used as a source port in all coap communications*/
123 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
124 OC_LOG_V(INFO, TAG, "Starting DoResourceDeviceDiscovery test on address %s",addr);
126 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_CLIENT));
127 /* Start a discovery query*/
128 char szQueryUri[64] = { 0 };
129 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
130 cbData.cb = asyncDoResourcesCallback;
131 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
134 EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0));
135 //EXPECT_EQ(OC_STACK_OK, OCUpdateResources(SERVICE_URI));
136 EXPECT_EQ(OC_STACK_OK, OCStop());
139 TEST(StackTest, StackStopWithoutInit) {
140 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
141 EXPECT_EQ(OC_STACK_OK, OCStop());
142 EXPECT_EQ(OC_STACK_ERROR, OCStop());
145 TEST(StackTest, UpdateResourceNullURI) {
147 uint16_t port = USE_RANDOM_PORT;
148 uint8_t ifname[] = "eth0";
149 OCCallbackData cbData;
152 /*Get Ip address on defined interface and initialize coap on it with random port number
153 * this port number will be used as a source port in all coap communications*/
154 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
155 OC_LOG_V(INFO, TAG, "Starting UpdateResourceNullURI test on address %s",addr);
157 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_CLIENT));
158 /* Start a discovery query*/
159 char szQueryUri[64] = { 0 };
160 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
161 cbData.cb = asyncDoResourcesCallback;
162 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
164 EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0));
165 //EXPECT_EQ(OC_STACK_INVALID_URI, OCUpdateResources(0));
166 EXPECT_EQ(OC_STACK_OK, OCStop());
169 TEST(StackTest, CreateResourceBadParams) {
171 uint16_t port = USE_RANDOM_PORT;
172 uint8_t ifname[] = "eth0";
174 /*Get Ip address on defined interface and initialize coap on it with random port number
175 * this port number will be used as a source port in all coap communications*/
176 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
178 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
179 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
181 OCResourceHandle handle;
183 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(NULL, //&handle,
188 OC_DISCOVERABLE|OC_OBSERVABLE));
190 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
195 OC_DISCOVERABLE|OC_OBSERVABLE));
197 // Property bitmask out of range
198 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
203 16));//OC_DISCOVERABLE|OC_OBSERVABLE));
205 EXPECT_EQ(OC_STACK_OK, OCStop());
209 TEST(StackTest, CreateResourceSuccess) {
211 uint16_t port = USE_RANDOM_PORT;
212 uint8_t ifname[] = "eth0";
214 /*Get Ip address on defined interface and initialize coap on it with random port number
215 * this port number will be used as a source port in all coap communications*/
216 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
218 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
219 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
221 OCResourceHandle handle;
222 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
227 OC_DISCOVERABLE|OC_OBSERVABLE));
228 const char *url = OCGetResourceUri(handle);
229 EXPECT_STREQ("/a/led", url);
231 EXPECT_EQ(OC_STACK_OK, OCStop());
234 TEST(StackTest, CreateResourceFailDuplicateUri) {
236 uint16_t port = USE_RANDOM_PORT;
237 uint8_t ifname[] = "eth0";
239 /*Get Ip address on defined interface and initialize coap on it with random port number
240 * this port number will be used as a source port in all coap communications*/
241 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
243 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
244 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
246 OCResourceHandle handle;
247 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
252 OC_DISCOVERABLE|OC_OBSERVABLE));
253 const char *url = OCGetResourceUri(handle);
254 EXPECT_STREQ("/a/led", url);
256 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
261 OC_DISCOVERABLE|OC_OBSERVABLE));
263 EXPECT_EQ(OC_STACK_OK, OCStop());
266 TEST(StackTest, CreateResourceMultipleResources) {
268 uint16_t port = USE_RANDOM_PORT;
269 uint8_t ifname[] = "eth0";
271 /*Get Ip address on defined interface and initialize coap on it with random port number
272 * this port number will be used as a source port in all coap communications*/
273 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
275 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
276 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
278 OCResourceHandle handle1;
279 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
284 OC_DISCOVERABLE|OC_OBSERVABLE));
286 OCResourceHandle handle2;
287 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
292 OC_DISCOVERABLE|OC_OBSERVABLE));
293 OCResourceHandle handle3;
294 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
299 OC_DISCOVERABLE|OC_OBSERVABLE));
301 const char *url = OCGetResourceUri(handle1);
302 EXPECT_STREQ("/a/led1", url);
304 url = OCGetResourceUri(handle2);
305 EXPECT_STREQ("/a/led2", url);
307 url = OCGetResourceUri(handle3);
308 EXPECT_STREQ("/a/led3", url);
310 EXPECT_EQ(OC_STACK_OK, OCStop());
313 TEST(StackTest, CreateResourceBadResoureType) {
315 uint16_t port = USE_RANDOM_PORT;
316 uint8_t ifname[] = "eth0";
318 /*Get Ip address on defined interface and initialize coap on it with random port number
319 * this port number will be used as a source port in all coap communications*/
320 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
322 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
323 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
325 OCResourceHandle handle;
326 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
331 OC_DISCOVERABLE|OC_OBSERVABLE));
333 EXPECT_EQ(OC_STACK_OK, OCStop());
336 TEST(StackTest, CreateResourceGoodResourceType) {
338 uint16_t port = USE_RANDOM_PORT;
339 uint8_t ifname[] = "eth0";
341 /*Get Ip address on defined interface and initialize coap on it with random port number
342 * this port number will be used as a source port in all coap communications*/
343 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
345 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
346 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
348 OCResourceHandle handle;
349 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
354 OC_DISCOVERABLE|OC_OBSERVABLE));
356 EXPECT_EQ(OC_STACK_OK, OCStop());
359 TEST(StackTest, ResourceTypeName) {
361 uint16_t port = USE_RANDOM_PORT;
362 uint8_t ifname[] = "eth0";
364 /*Get Ip address on defined interface and initialize coap on it with random port number
365 * this port number will be used as a source port in all coap communications*/
366 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
368 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
369 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
371 OCResourceHandle handle;
372 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
377 OC_DISCOVERABLE|OC_OBSERVABLE));
379 uint8_t numResourceTypes;
380 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
381 EXPECT_EQ(1, numResourceTypes);
382 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
383 EXPECT_STREQ("core.led", resourceTypeName);
385 // try getting resource type names with an invalid index
386 resourceTypeName = OCGetResourceTypeName(handle, 1);
387 EXPECT_STREQ(NULL, resourceTypeName);
388 // try getting resource type names with an invalid index
389 resourceTypeName = OCGetResourceTypeName(handle, 10);
390 EXPECT_STREQ(NULL, resourceTypeName);
392 EXPECT_EQ(OC_STACK_OK, OCStop());
395 TEST(StackTest, BindResourceTypeNameBad) {
397 uint16_t port = USE_RANDOM_PORT;
398 uint8_t ifname[] = "eth0";
400 /*Get Ip address on defined interface and initialize coap on it with random port number
401 * this port number will be used as a source port in all coap communications*/
402 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
404 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
405 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
407 OCResourceHandle handle;
408 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
413 OC_DISCOVERABLE|OC_OBSERVABLE));
415 uint8_t numResourceTypes;
416 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
417 EXPECT_EQ(1, numResourceTypes);
418 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
419 EXPECT_STREQ("core.led", resourceTypeName);
421 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceTypeToResource(handle, NULL));
423 EXPECT_EQ(OC_STACK_OK, OCStop());
426 TEST(StackTest, BindResourceTypeNameGood) {
428 uint16_t port = USE_RANDOM_PORT;
429 uint8_t ifname[] = "eth0";
431 /*Get Ip address on defined interface and initialize coap on it with random port number
432 * this port number will be used as a source port in all coap communications*/
433 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
435 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
436 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
438 OCResourceHandle handle;
439 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
444 OC_DISCOVERABLE|OC_OBSERVABLE));
446 uint8_t numResourceTypes;
447 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
448 EXPECT_EQ(1, numResourceTypes);
449 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
450 EXPECT_STREQ("core.led", resourceTypeName);
452 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
453 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
454 EXPECT_EQ(2, numResourceTypes);
455 resourceTypeName = OCGetResourceTypeName(handle, 1);
456 EXPECT_STREQ("core.brightled", resourceTypeName);
458 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
459 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
460 EXPECT_EQ(3, numResourceTypes);
461 resourceTypeName = OCGetResourceTypeName(handle, 2);
462 EXPECT_STREQ("core.reallybrightled", resourceTypeName);
464 EXPECT_EQ(OC_STACK_OK, OCStop());
467 TEST(StackTest, ResourceTypeAttrRepresentation) {
469 uint16_t port = USE_RANDOM_PORT;
470 uint8_t ifname[] = "eth0";
472 /*Get Ip address on defined interface and initialize coap on it with random port number
473 * this port number will be used as a source port in all coap communications*/
474 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
476 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
477 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
479 OCResourceHandle handle;
480 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
485 OC_DISCOVERABLE|OC_OBSERVABLE));
487 uint8_t numResourceTypes;
488 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
489 EXPECT_EQ(1, numResourceTypes);
491 EXPECT_EQ(OC_STACK_OK, OCStop());
494 TEST(StackTest, BindResourceTypeAttribRepGood) {
496 uint16_t port = USE_RANDOM_PORT;
497 uint8_t ifname[] = "eth0";
499 /*Get Ip address on defined interface and initialize coap on it with random port number
500 * this port number will be used as a source port in all coap communications*/
501 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
503 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
504 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
506 OCResourceHandle handle;
507 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
512 OC_DISCOVERABLE|OC_OBSERVABLE));
514 uint8_t numResourceTypes;
515 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
516 EXPECT_EQ(1, numResourceTypes);
518 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
519 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
520 EXPECT_EQ(2, numResourceTypes);
522 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
523 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
524 EXPECT_EQ(3, numResourceTypes);
526 EXPECT_EQ(OC_STACK_OK, OCStop());
529 TEST(StackTest, ResourceTypeInterface) {
531 uint16_t port = USE_RANDOM_PORT;
532 uint8_t ifname[] = "eth0";
534 /*Get Ip address on defined interface and initialize coap on it with random port number
535 * this port number will be used as a source port in all coap communications*/
536 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
538 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
539 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
541 OCResourceHandle handle;
542 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
547 OC_DISCOVERABLE|OC_OBSERVABLE));
549 uint8_t numResourceInterfaces;
550 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
551 EXPECT_EQ(1, numResourceInterfaces);
552 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
553 EXPECT_STREQ("core.rw", resourceInterfaceName);
555 // try getting resource interface names with an invalid index
556 resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
557 EXPECT_STREQ(NULL, resourceInterfaceName);
558 // try getting resource interface names with an invalid index
559 resourceInterfaceName = OCGetResourceInterfaceName(handle, 10);
560 EXPECT_STREQ(NULL, resourceInterfaceName);
562 EXPECT_EQ(OC_STACK_OK, OCStop());
565 TEST(StackTest, BindResourceInterfaceNameBad) {
567 uint16_t port = USE_RANDOM_PORT;
568 uint8_t ifname[] = "eth0";
570 /*Get Ip address on defined interface and initialize coap on it with random port number
571 * this port number will be used as a source port in all coap communications*/
572 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
574 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
575 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
577 OCResourceHandle handle;
578 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
583 OC_DISCOVERABLE|OC_OBSERVABLE));
585 uint8_t numResourceInterfaces;
586 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
587 EXPECT_EQ(1, numResourceInterfaces);
588 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
589 EXPECT_STREQ("core.rw", resourceInterfaceName);
591 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, NULL));
593 EXPECT_EQ(OC_STACK_OK, OCStop());
596 TEST(StackTest, BindResourceInterfaceNameGood) {
598 uint16_t port = USE_RANDOM_PORT;
599 uint8_t ifname[] = "eth0";
601 /*Get Ip address on defined interface and initialize coap on it with random port number
602 * this port number will be used as a source port in all coap communications*/
603 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
605 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
606 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
608 OCResourceHandle handle;
609 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
614 OC_DISCOVERABLE|OC_OBSERVABLE));
616 uint8_t numResourceInterfaces;
617 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
618 EXPECT_EQ(1, numResourceInterfaces);
619 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
620 EXPECT_STREQ("core.rw", resourceInterfaceName);
622 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
624 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
625 EXPECT_EQ(2, numResourceInterfaces);
626 resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
627 EXPECT_STREQ("core.r", resourceInterfaceName);
629 EXPECT_EQ(OC_STACK_OK, OCStop());
632 TEST(StackTest, ResourceTypeInterfaceMethods) {
634 uint16_t port = USE_RANDOM_PORT;
635 uint8_t ifname[] = "eth0";
637 /*Get Ip address on defined interface and initialize coap on it with random port number
638 * this port number will be used as a source port in all coap communications*/
639 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
641 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
642 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
644 OCResourceHandle handle;
645 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
650 OC_DISCOVERABLE|OC_OBSERVABLE));
652 uint8_t numResourceInterfaces;
653 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
654 EXPECT_EQ(1, numResourceInterfaces);
656 EXPECT_EQ(OC_STACK_OK, OCStop());
659 TEST(StackTest, BindResourceInterfaceMethodsBad) {
661 uint16_t port = USE_RANDOM_PORT;
662 uint8_t ifname[] = "eth0";
664 /*Get Ip address on defined interface and initialize coap on it with random port number
665 * this port number will be used as a source port in all coap communications*/
666 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
668 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
669 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
671 OCResourceHandle handle;
672 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
677 OC_DISCOVERABLE|OC_OBSERVABLE));
679 uint8_t numResourceInterfaces;
680 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
681 EXPECT_EQ(1, numResourceInterfaces);
683 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, 0));
685 EXPECT_EQ(OC_STACK_OK, OCStop());
688 TEST(StackTest, BindResourceInterfaceMethodsGood) {
690 uint16_t port = USE_RANDOM_PORT;
691 uint8_t ifname[] = "eth0";
693 /*Get Ip address on defined interface and initialize coap on it with random port number
694 * this port number will be used as a source port in all coap communications*/
695 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
697 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
698 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
700 OCResourceHandle handle;
701 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
706 OC_DISCOVERABLE|OC_OBSERVABLE));
708 uint8_t numResourceInterfaces;
709 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
710 EXPECT_EQ(1, numResourceInterfaces);
712 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
714 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
715 EXPECT_EQ(2, numResourceInterfaces);
717 EXPECT_EQ(OC_STACK_OK, OCStop());
720 TEST(StackTest, BindContainedResourceBad) {
722 uint16_t port = USE_RANDOM_PORT;
723 uint8_t ifname[] = "eth0";
725 /*Get Ip address on defined interface and initialize coap on it with random port number
726 * this port number will be used as a source port in all coap communications*/
727 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
729 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
730 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
732 OCResourceHandle containerHandle;
733 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
738 OC_DISCOVERABLE|OC_OBSERVABLE));
740 OCResourceHandle handle0;
741 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
746 OC_DISCOVERABLE|OC_OBSERVABLE));
748 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResource(containerHandle, containerHandle));
750 EXPECT_EQ(OC_STACK_ERROR, OCBindResource((OCResourceHandle) 0, handle0));
752 EXPECT_EQ(OC_STACK_OK, OCStop());
755 TEST(StackTest, BindContainedResourceGood) {
757 uint16_t port = USE_RANDOM_PORT;
758 uint8_t ifname[] = "eth0";
760 /*Get Ip address on defined interface and initialize coap on it with random port number
761 * this port number will be used as a source port in all coap communications*/
762 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
764 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
765 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
767 uint8_t numResources = 0;
768 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
769 EXPECT_EQ(0, numResources);
771 OCResourceHandle containerHandle;
772 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
777 OC_DISCOVERABLE|OC_OBSERVABLE));
778 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
779 EXPECT_EQ(1, numResources);
781 OCResourceHandle handle0;
782 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
787 OC_DISCOVERABLE|OC_OBSERVABLE));
788 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
789 EXPECT_EQ(2, numResources);
791 OCResourceHandle handle1;
792 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
797 OC_DISCOVERABLE|OC_OBSERVABLE));
798 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
799 EXPECT_EQ(3, numResources);
801 OCResourceHandle handle2;
802 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
807 OC_DISCOVERABLE|OC_OBSERVABLE));
808 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
809 EXPECT_EQ(4, numResources);
811 OCResourceHandle handle3;
812 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
817 OC_DISCOVERABLE|OC_OBSERVABLE));
818 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
819 EXPECT_EQ(5, numResources);
821 OCResourceHandle handle4;
822 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
827 OC_DISCOVERABLE|OC_OBSERVABLE));
828 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
829 EXPECT_EQ(6, numResources);
831 OCResourceHandle handle5;
832 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
837 OC_DISCOVERABLE|OC_OBSERVABLE));
838 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
839 EXPECT_EQ(7, numResources);
842 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle0));
843 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle1));
844 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle2));
845 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle3));
846 EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle4));
847 EXPECT_EQ(OC_STACK_ERROR, OCBindResource(containerHandle, handle5));
849 EXPECT_EQ(handle0, OCGetResourceHandleFromCollection(containerHandle, 0));
850 EXPECT_EQ(handle1, OCGetResourceHandleFromCollection(containerHandle, 1));
851 EXPECT_EQ(handle2, OCGetResourceHandleFromCollection(containerHandle, 2));
852 EXPECT_EQ(handle3, OCGetResourceHandleFromCollection(containerHandle, 3));
853 EXPECT_EQ(handle4, OCGetResourceHandleFromCollection(containerHandle, 4));
855 EXPECT_EQ(NULL, OCGetResourceHandleFromCollection(containerHandle, 5));
857 EXPECT_EQ(OC_STACK_OK, OCStop());
860 TEST(StackTest, GetResourceByIndex) {
862 uint16_t port = USE_RANDOM_PORT;
863 uint8_t ifname[] = "eth0";
865 /*Get Ip address on defined interface and initialize coap on it with random port number
866 * this port number will be used as a source port in all coap communications*/
867 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
869 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
870 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
872 uint8_t numResources = 0;
873 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
874 EXPECT_EQ(0, numResources);
876 OCResourceHandle containerHandle;
877 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
882 OC_DISCOVERABLE|OC_OBSERVABLE));
883 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
884 EXPECT_EQ(1, numResources);
886 OCResourceHandle handle0;
887 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
892 OC_DISCOVERABLE|OC_OBSERVABLE));
893 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
894 EXPECT_EQ(2, numResources);
896 OCResourceHandle handle1;
897 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
902 OC_DISCOVERABLE|OC_OBSERVABLE));
903 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
904 EXPECT_EQ(3, numResources);
906 OCResourceHandle handle2;
907 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
912 OC_DISCOVERABLE|OC_OBSERVABLE));
913 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
914 EXPECT_EQ(4, numResources);
916 OCResourceHandle handle3;
917 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
922 OC_DISCOVERABLE|OC_OBSERVABLE));
923 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
924 EXPECT_EQ(5, numResources);
926 OCResourceHandle handle4;
927 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
932 OC_DISCOVERABLE|OC_OBSERVABLE));
933 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
934 EXPECT_EQ(6, numResources);
936 OCResourceHandle handle5;
937 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
942 OC_DISCOVERABLE|OC_OBSERVABLE));
943 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
944 EXPECT_EQ(7, numResources);
946 EXPECT_EQ(containerHandle, OCGetResourceHandle(0));
947 EXPECT_EQ(handle0, OCGetResourceHandle(1));
948 EXPECT_EQ(handle1, OCGetResourceHandle(2));
949 EXPECT_EQ(handle2, OCGetResourceHandle(3));
950 EXPECT_EQ(handle3, OCGetResourceHandle(4));
951 EXPECT_EQ(handle4, OCGetResourceHandle(5));
952 EXPECT_EQ(handle5, OCGetResourceHandle(6));
954 EXPECT_EQ(OC_STACK_OK, OCStop());
957 TEST(StackTest, BindEntityHandlerBad) {
959 uint16_t port = USE_RANDOM_PORT;
960 uint8_t ifname[] = "eth0";
962 /*Get Ip address on defined interface and initialize coap on it with random port number
963 * this port number will be used as a source port in all coap communications*/
964 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
966 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
967 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
969 OCResourceHandle handle;
970 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
975 OC_DISCOVERABLE|OC_OBSERVABLE));
977 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceHandler(handle, NULL));
978 EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceHandler(NULL, NULL));
980 EXPECT_EQ(OC_STACK_OK, OCStop());
984 OCEntityHandlerResult entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest) {
985 OC_LOG(INFO, TAG, "Entering entityHandler");
990 TEST(StackTest, BindEntityHandlerGood) {
992 uint16_t port = USE_RANDOM_PORT;
993 uint8_t ifname[] = "eth0";
995 /*Get Ip address on defined interface and initialize coap on it with random port number
996 * this port number will be used as a source port in all coap communications*/
997 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
999 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1000 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1002 OCResourceHandle handle;
1003 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1008 OC_DISCOVERABLE|OC_OBSERVABLE));
1010 OCEntityHandler myHandler = entityHandler;
1012 EXPECT_EQ(OC_STACK_OK, OCBindResourceHandler(handle, myHandler));
1014 EXPECT_EQ(myHandler, OCGetResourceHandler(handle));
1016 EXPECT_EQ(OC_STACK_OK, OCStop());
1019 TEST(StackTest, DeleteHeadResource) {
1021 uint16_t port = USE_RANDOM_PORT;
1022 uint8_t ifname[] = "eth0";
1024 /*Get Ip address on defined interface and initialize coap on it with random port number
1025 * this port number will be used as a source port in all coap communications*/
1026 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1028 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1029 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1031 uint8_t numResources = 0;
1032 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1033 EXPECT_EQ(0, numResources);
1035 OCResourceHandle handle0;
1036 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1041 OC_DISCOVERABLE|OC_OBSERVABLE));
1042 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1043 EXPECT_EQ(1, numResources);
1045 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1046 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1047 EXPECT_EQ(0, numResources);
1049 EXPECT_EQ(OC_STACK_OK, OCStop());
1052 TEST(StackTest, DeleteHeadResource2) {
1054 uint16_t port = USE_RANDOM_PORT;
1055 uint8_t ifname[] = "eth0";
1057 /*Get Ip address on defined interface and initialize coap on it with random port number
1058 * this port number will be used as a source port in all coap communications*/
1059 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1061 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1062 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1064 uint8_t numResources = 0;
1065 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1066 EXPECT_EQ(0, numResources);
1068 OCResourceHandle handle0;
1069 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1074 OC_DISCOVERABLE|OC_OBSERVABLE));
1075 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1076 EXPECT_EQ(1, numResources);
1078 OCResourceHandle handle1;
1079 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1084 OC_DISCOVERABLE|OC_OBSERVABLE));
1085 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1086 EXPECT_EQ(2, numResources);
1088 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1089 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1090 EXPECT_EQ(1, numResources);
1092 EXPECT_EQ(handle1, OCGetResourceHandle(0));
1094 EXPECT_EQ(OC_STACK_OK, OCStop());
1098 TEST(StackTest, DeleteLastResource) {
1100 uint16_t port = USE_RANDOM_PORT;
1101 uint8_t ifname[] = "eth0";
1103 /*Get Ip address on defined interface and initialize coap on it with random port number
1104 * this port number will be used as a source port in all coap communications*/
1105 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1107 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1108 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1110 uint8_t numResources = 0;
1111 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1112 EXPECT_EQ(0, numResources);
1114 OCResourceHandle handle0;
1115 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1120 OC_DISCOVERABLE|OC_OBSERVABLE));
1121 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1122 EXPECT_EQ(1, numResources);
1124 OCResourceHandle handle1;
1125 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1130 OC_DISCOVERABLE|OC_OBSERVABLE));
1131 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1132 EXPECT_EQ(2, numResources);
1134 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1135 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1136 EXPECT_EQ(1, numResources);
1138 EXPECT_EQ(handle0, OCGetResourceHandle(0));
1140 OCResourceHandle handle2;
1141 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1146 OC_DISCOVERABLE|OC_OBSERVABLE));
1147 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1148 EXPECT_EQ(2, numResources);
1150 EXPECT_EQ(OC_STACK_OK, OCStop());
1153 TEST(StackTest, DeleteMiddleResource) {
1155 uint16_t port = USE_RANDOM_PORT;
1156 uint8_t ifname[] = "eth0";
1158 /*Get Ip address on defined interface and initialize coap on it with random port number
1159 * this port number will be used as a source port in all coap communications*/
1160 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1162 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1163 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1165 uint8_t numResources = 0;
1166 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1167 EXPECT_EQ(0, numResources);
1169 OCResourceHandle handle0;
1170 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1175 OC_DISCOVERABLE|OC_OBSERVABLE));
1176 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1177 EXPECT_EQ(1, numResources);
1179 OCResourceHandle handle1;
1180 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1185 OC_DISCOVERABLE|OC_OBSERVABLE));
1186 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1187 EXPECT_EQ(2, numResources);
1189 OCResourceHandle handle2;
1190 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1195 OC_DISCOVERABLE|OC_OBSERVABLE));
1196 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1197 EXPECT_EQ(3, numResources);
1199 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1200 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1201 EXPECT_EQ(2, numResources);
1203 EXPECT_EQ(handle0, OCGetResourceHandle(0));
1204 EXPECT_EQ(handle2, OCGetResourceHandle(1));
1206 // Make sure the resource elements are still correct
1207 uint8_t numResourceInterfaces;
1208 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle2, &numResourceInterfaces));
1209 EXPECT_EQ(1, numResourceInterfaces);
1210 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle2, 0);
1211 EXPECT_STREQ("core.rw", resourceInterfaceName);
1213 EXPECT_EQ(OC_STACK_OK, OCStop());
1216 TEST(StackTest, GetResourceProperties) {
1218 uint16_t port = USE_RANDOM_PORT;
1219 uint8_t ifname[] = "eth0";
1221 /*Get Ip address on defined interface and initialize coap on it with random port number
1222 * this port number will be used as a source port in all coap communications*/
1223 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1225 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1226 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1228 OCResourceHandle handle;
1229 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1234 OC_DISCOVERABLE|OC_OBSERVABLE));
1236 EXPECT_EQ(OC_ACTIVE|OC_DISCOVERABLE|OC_OBSERVABLE, OCGetResourceProperties(handle));
1237 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1239 EXPECT_EQ(OC_STACK_OK, OCStop());
1242 TEST(StackTest, StackTestResourceDiscoverOneResourceBad) {
1244 uint16_t port = USE_RANDOM_PORT;
1245 uint8_t ifname[] = "eth0";
1247 /*Get Ip address on defined interface and initialize coap on it with random port number
1248 * this port number will be used as a source port in all coap communications*/
1249 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1251 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1252 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1254 OCResourceHandle handle;
1255 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1260 OC_DISCOVERABLE|OC_OBSERVABLE));
1261 const char *url = OCGetResourceUri(handle);
1262 EXPECT_STREQ("/a1/led", url);
1264 //EXPECT_EQ(OC_STACK_INVALID_URI, OCHandleServerRequest(&res, uri, query, req, rsp));
1265 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1266 uint8_t numResources = 0;
1267 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1268 EXPECT_EQ(0, numResources);
1270 EXPECT_EQ(OC_STACK_OK, OCStop());
1273 TEST(StackTest, StackTestResourceDiscoverOneResource) {
1275 uint16_t port = USE_RANDOM_PORT;
1276 uint8_t ifname[] = "eth0";
1278 /*Get Ip address on defined interface and initialize coap on it with random port number
1279 * this port number will be used as a source port in all coap communications*/
1280 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1282 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1283 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1285 OCResourceHandle handle;
1286 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1291 OC_DISCOVERABLE|OC_OBSERVABLE));
1292 const char *url = OCGetResourceUri(handle);
1293 EXPECT_STREQ("/a/led", url);
1295 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1296 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1298 EXPECT_EQ(OC_STACK_OK, OCStop());
1301 TEST(StackTest, StackTestResourceDiscoverManyResources) {
1303 uint16_t port = USE_RANDOM_PORT;
1304 uint8_t ifname[] = "eth0";
1306 /*Get Ip address on defined interface and initialize coap on it with random port number
1307 * this port number will be used as a source port in all coap communications*/
1308 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1310 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1311 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1313 OCResourceHandle handle1;
1314 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1320 const char *url = OCGetResourceUri(handle1);
1321 EXPECT_STREQ("/a/led1", url);
1323 OCResourceHandle handle2;
1324 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1329 OC_DISCOVERABLE|OC_OBSERVABLE));
1330 url = OCGetResourceUri(handle2);
1331 EXPECT_STREQ("/a/led2", url);
1333 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.brightled"));
1334 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.colorled"));
1336 OCResourceHandle handle3;
1337 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
1342 OC_DISCOVERABLE|OC_OBSERVABLE));
1343 url = OCGetResourceUri(handle3);
1344 EXPECT_STREQ("/a/led3", url);
1346 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.ll"));
1347 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.b"));
1349 OCResourceHandle handle4;
1350 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
1356 url = OCGetResourceUri(handle4);
1357 EXPECT_STREQ("/a/led4", url);
1359 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle4, "core.brightled"));
1360 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, "oc.mi.ll"));
1361 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, "oc.mi.b"));
1363 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1365 EXPECT_EQ(OC_STACK_OK, OCStop());
1370 TEST(StackTest, StackTestResourceDiscoverIfFilteringBad) {
1372 uint16_t port = USE_RANDOM_PORT;
1373 uint8_t ifname[] = "eth0";
1374 char uri[] = "/oc/core";
1375 char query[] = "if";
1376 char req[1024] = {};
1377 char rsp[1024] = {};
1378 //OCServerRequestResult res;
1380 //EXPECT_EQ(OC_STACK_INVALID_QUERY, OCHandleServerRequest(&res, uri, query, req, rsp));
1383 TEST(StackTest, StackTestResourceDiscoverRtFilteringBad) {
1385 uint16_t port = USE_RANDOM_PORT;
1386 uint8_t ifname[] = "eth0";
1387 char uri[] = "/oc/core";
1388 char query[] = "rt";
1389 char req[1024] = {};
1390 char rsp[1024] = {};
1391 //OCServerRequestResult res;
1393 //EXPECT_EQ(OC_STACK_INVALID_QUERY, OCHandleServerRequest(&res, uri, query, req, rsp));
1395 TEST(StackTest, StackTestResourceDiscoverIfFiltering) {
1397 uint16_t port = USE_RANDOM_PORT;
1398 uint8_t ifname[] = "eth0";
1399 char uri[] = "/oc/core";
1400 char query[] = "if=oc.mi.ll";
1401 char req[1024] = {};
1402 char rsp[1024] = {};
1403 //OCServerRequestResult res;
1405 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1408 TEST(StackTest, StackTestResourceDiscoverRtFiltering) {
1410 uint16_t port = USE_RANDOM_PORT;
1411 uint8_t ifname[] = "eth0";
1412 char uri[] = "/oc/core";
1413 char query[] = "rt=core.brightled";
1414 char req[1024] = {};
1415 char rsp[1024] = {};
1416 //OCServerRequestResult res;
1418 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));