1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
9 #include "ocstackinternal.h"
13 #include "gtest/gtest.h"
14 #include <sys/types.h>
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 static const char TAG[] = "TestHarness";
36 static const char *SERVICE_URI = "coap://127.0.0.1:5683/";
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
41 extern "C" OCStackApplicationResult asyncDoResourcesCallback(void *ctx, OCClientResponse * clientResponse) {
42 OC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
44 EXPECT_EQ(OC_STACK_OK, clientResponse->result);
46 if(ctx == (void*)CTX_VAL) {
47 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
49 OC_LOG_V(INFO, TAG, "result = %d", clientResponse->result);
52 //-----------------------------------------------------------------------------
53 // Resource creation stuff
54 //-----------------------------------------------------------------------------
55 // TODO - remove, temporarily kept for reference purposes
57 const char *LEDrep[] = { "state;b", "power;i", (char *)NULL};
58 CREATE_RESOURCE_TYPE(LED, "core.led", LEDrep);
60 // Create the state and power attributes
61 char stateValue[] = "0";
62 CREATE_RESOURCE_ATTRIBUTE(stateAttr, "state", stateValue);
64 char powerValue[] = "5";
65 CREATE_RESOURCE_ATTRIBUTE(powerAttr, "power", powerValue);
67 // Define the interface
68 CREATE_RESOURCE_INTERFACE(LED, "core.rw", OC_REST_GET|OC_REST_PUT);
70 // Define the resource; the space is allocated; but the resource has not been fully initialized
71 CREATE_RESOURCE(LED, "/a/led", &LED_resourcetype, &LED_resourceinterface) ;
74 // Define the entity handler for the resource
75 void LED_ehandler(OCEntityHandlerFlag flag, const char *request, char *reponse, OCResourceHandle resource) {
76 /* Add code for entity handler */
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 TEST(StackTest, StackInitNullAddr) {
84 EXPECT_EQ(OC_STACK_OK, OCInit(0, 5683, OC_SERVER));
87 TEST(StackTest, StackInitNullPort) {
88 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 0, OC_SERVER));
91 TEST(StackTest, StackInitNullAddrAndPort) {
92 EXPECT_EQ(OC_STACK_OK, OCInit(0, 0, OC_SERVER));
95 TEST(StackTest, StackInitInvalidMode) {
96 EXPECT_EQ(OC_STACK_ERROR, OCInit(0, 0, (OCMode)10));
99 TEST(StackTest, StackStartSuccessClient) {
100 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
103 TEST(StackTest, StackStartSuccessServer) {
104 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
107 TEST(StackTest, StackStartSuccessClientServer) {
108 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
111 TEST(StackTest, StackStartSuccessiveInits) {
112 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
113 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.2", 5683, OC_SERVER));
116 TEST(StackTest, DoResourceDeviceDiscovery) {
118 uint16_t port = USE_RANDOM_PORT;
119 uint8_t ifname[] = "eth0";
120 OCCallbackData cbData;
122 /*Get Ip address on defined interface and initialize coap on it with random port number
123 * this port number will be used as a source port in all coap communications*/
124 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
125 OC_LOG_V(INFO, TAG, "Starting DoResourceDeviceDiscovery test on address %s",addr);
127 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_CLIENT));
128 /* Start a discovery query*/
129 char szQueryUri[64] = { 0 };
130 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
131 cbData.cb = asyncDoResourcesCallback;
132 cbData.context = (void*)CTX_VAL;
133 EXPECT_EQ(OC_STACK_OK, OCDoResource(OC_REST_GET, szQueryUri, 0, 0, OC_NON_CONFIRMABLE, &cbData));
134 //EXPECT_EQ(OC_STACK_OK, OCUpdateResources(SERVICE_URI));
135 EXPECT_EQ(OC_STACK_OK, OCStop());
138 TEST(StackTest, StackStopWithoutInit) {
139 EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
140 EXPECT_EQ(OC_STACK_OK, OCStop());
141 EXPECT_EQ(OC_STACK_ERROR, OCStop());
144 TEST(StackTest, UpdateResourceNullURI) {
146 uint16_t port = USE_RANDOM_PORT;
147 uint8_t ifname[] = "eth0";
148 OCCallbackData cbData;
150 /*Get Ip address on defined interface and initialize coap on it with random port number
151 * this port number will be used as a source port in all coap communications*/
152 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
153 OC_LOG_V(INFO, TAG, "Starting UpdateResourceNullURI test on address %s",addr);
155 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_CLIENT));
156 /* Start a discovery query*/
157 char szQueryUri[64] = { 0 };
158 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
159 cbData.cb = asyncDoResourcesCallback;
160 cbData.context = (void*)CTX_VAL;
161 EXPECT_EQ(OC_STACK_OK, OCDoResource(OC_REST_GET, szQueryUri, 0, 0, OC_NON_CONFIRMABLE, &cbData));
162 //EXPECT_EQ(OC_STACK_INVALID_URI, OCUpdateResources(0));
163 EXPECT_EQ(OC_STACK_OK, OCStop());
166 TEST(StackTest, CreateResourceBadParams) {
168 uint16_t port = USE_RANDOM_PORT;
169 uint8_t ifname[] = "eth0";
171 /*Get Ip address on defined interface and initialize coap on it with random port number
172 * this port number will be used as a source port in all coap communications*/
173 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
175 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
176 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
178 OCResourceHandle handle;
179 EXPECT_EQ(OC_STACK_ERROR, OCCreateResource(NULL, //&handle,
181 "state:oc.bt.b;power:oc.bt.i",
183 OC_REST_GET|OC_REST_PUT,
186 OC_DISCOVERABLE|OC_OBSERVABLE));
188 EXPECT_EQ(OC_STACK_ERROR, OCCreateResource(&handle,
190 "state:oc.bt.b;power:oc.bt.i",
192 OC_REST_GET|OC_REST_PUT,
195 OC_DISCOVERABLE|OC_OBSERVABLE));
197 // Method bitmask out of range
198 EXPECT_EQ(OC_STACK_ERROR, OCCreateResource(&handle,
200 "state:oc.bt.b;power:oc.bt.i",
202 32, //OC_REST_GET|OC_REST_PUT,
205 OC_DISCOVERABLE|OC_OBSERVABLE));
207 EXPECT_EQ(OC_STACK_ERROR, OCCreateResource(&handle,
209 "state:oc.bt.b;power:oc.bt.i",
211 OC_REST_NOMETHOD, //OC_REST_GET|OC_REST_PUT,
214 OC_DISCOVERABLE|OC_OBSERVABLE));
215 // Property bitmask out of range
216 EXPECT_EQ(OC_STACK_ERROR, OCCreateResource(&handle,
218 "state:oc.bt.b;power:oc.bt.i",
220 OC_REST_GET|OC_REST_PUT,
223 16));//OC_DISCOVERABLE|OC_OBSERVABLE));
226 TEST(StackTest, CreateResourceSuccess) {
228 uint16_t port = USE_RANDOM_PORT;
229 uint8_t ifname[] = "eth0";
231 /*Get Ip address on defined interface and initialize coap on it with random port number
232 * this port number will be used as a source port in all coap communications*/
233 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
235 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
236 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
238 OCResourceHandle handle;
239 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
241 "state:oc.bt.b;power:oc.bt.i",
243 OC_REST_GET|OC_REST_PUT,
246 OC_DISCOVERABLE|OC_OBSERVABLE));
247 const char *url = OCGetResourceUri(handle);
248 EXPECT_STREQ("/a/led", url);
251 TEST(StackTest, CreateResourceFailDuplicateUri) {
253 uint16_t port = USE_RANDOM_PORT;
254 uint8_t ifname[] = "eth0";
256 /*Get Ip address on defined interface and initialize coap on it with random port number
257 * this port number will be used as a source port in all coap communications*/
258 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
260 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
261 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
263 OCResourceHandle handle;
264 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
266 "state:oc.bt.b;power:oc.bt.i",
268 OC_REST_GET|OC_REST_PUT,
271 OC_DISCOVERABLE|OC_OBSERVABLE));
272 const char *url = OCGetResourceUri(handle);
273 EXPECT_STREQ("/a/led", url);
275 EXPECT_EQ(OC_STACK_ERROR, OCCreateResource(&handle,
277 "state:oc.bt.b;power:oc.bt.i",
279 OC_REST_GET|OC_REST_PUT,
282 OC_DISCOVERABLE|OC_OBSERVABLE));
285 TEST(StackTest, CreateResourceMultipleResources) {
287 uint16_t port = USE_RANDOM_PORT;
288 uint8_t ifname[] = "eth0";
290 /*Get Ip address on defined interface and initialize coap on it with random port number
291 * this port number will be used as a source port in all coap communications*/
292 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
294 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
295 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
297 OCResourceHandle handle1;
298 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
300 "state:oc.bt.b;power:oc.bt.i",
302 OC_REST_GET|OC_REST_PUT,
305 OC_DISCOVERABLE|OC_OBSERVABLE));
307 OCResourceHandle handle2;
308 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
310 "state:oc.bt.b;power:oc.bt.i",
312 OC_REST_GET|OC_REST_PUT,
315 OC_DISCOVERABLE|OC_OBSERVABLE));
316 OCResourceHandle handle3;
317 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
319 "state:oc.bt.b;power:oc.bt.i",
321 OC_REST_GET|OC_REST_PUT,
324 OC_DISCOVERABLE|OC_OBSERVABLE));
326 const char *url = OCGetResourceUri(handle1);
327 EXPECT_STREQ("/a/led1", url);
329 url = OCGetResourceUri(handle2);
330 EXPECT_STREQ("/a/led2", url);
332 url = OCGetResourceUri(handle3);
333 EXPECT_STREQ("/a/led3", url);
337 TEST(StackTest, CreateResourceBadResoureType) {
339 uint16_t port = USE_RANDOM_PORT;
340 uint8_t ifname[] = "eth0";
342 /*Get Ip address on defined interface and initialize coap on it with random port number
343 * this port number will be used as a source port in all coap communications*/
344 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
346 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
347 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
349 OCResourceHandle handle;
350 EXPECT_EQ(OC_STACK_ERROR, OCCreateResource(&handle,
352 "state:oc.bt.b;power:oc.bt.i",
354 OC_REST_GET|OC_REST_PUT,
357 OC_DISCOVERABLE|OC_OBSERVABLE));
360 TEST(StackTest, CreateResourceGoodResourceType) {
362 uint16_t port = USE_RANDOM_PORT;
363 uint8_t ifname[] = "eth0";
365 /*Get Ip address on defined interface and initialize coap on it with random port number
366 * this port number will be used as a source port in all coap communications*/
367 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
369 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
370 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
372 OCResourceHandle handle;
373 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
375 "state:oc.bt.b;power:oc.bt.i",
377 OC_REST_GET|OC_REST_PUT,
380 OC_DISCOVERABLE|OC_OBSERVABLE));
384 TEST(StackTest, ResourceTypeName) {
386 uint16_t port = USE_RANDOM_PORT;
387 uint8_t ifname[] = "eth0";
389 /*Get Ip address on defined interface and initialize coap on it with random port number
390 * this port number will be used as a source port in all coap communications*/
391 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
393 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
394 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
396 OCResourceHandle handle;
397 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
399 "state:oc.bt.b;power:oc.bt.i",
401 OC_REST_GET|OC_REST_PUT,
404 OC_DISCOVERABLE|OC_OBSERVABLE));
406 uint8_t numResourceTypes;
407 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
408 EXPECT_EQ(1, numResourceTypes);
409 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
410 EXPECT_STREQ("core.led", resourceTypeName);
412 // try getting resource type names with an invalid index
413 resourceTypeName = OCGetResourceTypeName(handle, 1);
414 EXPECT_STREQ(NULL, resourceTypeName);
415 // try getting resource type names with an invalid index
416 resourceTypeName = OCGetResourceTypeName(handle, 10);
417 EXPECT_STREQ(NULL, resourceTypeName);
420 TEST(StackTest, BindResourceTypeNameBad) {
422 uint16_t port = USE_RANDOM_PORT;
423 uint8_t ifname[] = "eth0";
425 /*Get Ip address on defined interface and initialize coap on it with random port number
426 * this port number will be used as a source port in all coap communications*/
427 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
429 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
430 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
432 OCResourceHandle handle;
433 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
435 "state:oc.bt.b;power:oc.bt.i",
437 OC_REST_GET|OC_REST_PUT,
440 OC_DISCOVERABLE|OC_OBSERVABLE));
442 uint8_t numResourceTypes;
443 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
444 EXPECT_EQ(1, numResourceTypes);
445 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
446 EXPECT_STREQ("core.led", resourceTypeName);
448 EXPECT_EQ(OC_STACK_ERROR, OCBindResourceTypeToResource(handle, NULL, "state:oc.bt.b;power:oc.bt.i"));
451 TEST(StackTest, BindResourceTypeNameGood) {
453 uint16_t port = USE_RANDOM_PORT;
454 uint8_t ifname[] = "eth0";
456 /*Get Ip address on defined interface and initialize coap on it with random port number
457 * this port number will be used as a source port in all coap communications*/
458 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
460 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
461 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
463 OCResourceHandle handle;
464 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
466 "state:oc.bt.b;power:oc.bt.i",
468 OC_REST_GET|OC_REST_PUT,
471 OC_DISCOVERABLE|OC_OBSERVABLE));
473 uint8_t numResourceTypes;
474 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
475 EXPECT_EQ(1, numResourceTypes);
476 const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
477 EXPECT_STREQ("core.led", resourceTypeName);
479 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled", "state:oc.bt.b;power:oc.bt.i"));
480 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
481 EXPECT_EQ(2, numResourceTypes);
482 resourceTypeName = OCGetResourceTypeName(handle, 1);
483 EXPECT_STREQ("core.brightled", resourceTypeName);
485 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled", "state:oc.bt.b;power:oc.bt.i"));
486 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
487 EXPECT_EQ(3, numResourceTypes);
488 resourceTypeName = OCGetResourceTypeName(handle, 2);
489 EXPECT_STREQ("core.reallybrightled", resourceTypeName);
493 TEST(StackTest, ResourceTypeAttrRepresentation) {
495 uint16_t port = USE_RANDOM_PORT;
496 uint8_t ifname[] = "eth0";
498 /*Get Ip address on defined interface and initialize coap on it with random port number
499 * this port number will be used as a source port in all coap communications*/
500 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
502 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
503 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
505 OCResourceHandle handle;
506 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
508 "state:oc.bt.b;power:oc.bt.i",
510 OC_REST_GET|OC_REST_PUT,
513 OC_DISCOVERABLE|OC_OBSERVABLE));
515 uint8_t numResourceTypes;
516 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
517 EXPECT_EQ(1, numResourceTypes);
518 const char *resourceTypeAttrRepresentation = OCGetResourceAttributeRepresentation(handle, 0);
519 EXPECT_STREQ("state:oc.bt.b;power:oc.bt.i", resourceTypeAttrRepresentation);
521 // try getting resource type attribute representations with an invalid index
522 resourceTypeAttrRepresentation = OCGetResourceAttributeRepresentation(handle, 1);
523 EXPECT_STREQ(NULL, resourceTypeAttrRepresentation);
524 // try getting resource type names with an invalid index
525 resourceTypeAttrRepresentation = OCGetResourceAttributeRepresentation(handle, 10);
526 EXPECT_STREQ(NULL, resourceTypeAttrRepresentation);
529 TEST(StackTest, BindResourceTypeAttribRepGood) {
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,
546 OC_REST_GET|OC_REST_PUT,
549 OC_DISCOVERABLE|OC_OBSERVABLE));
551 uint8_t numResourceTypes;
552 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
553 EXPECT_EQ(1, numResourceTypes);
554 const char *resourceTypeAttrRepresentation = OCGetResourceAttributeRepresentation(handle, 0);
555 EXPECT_STREQ("state:oc.bt.b", resourceTypeAttrRepresentation);
557 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled", "state:oc.bt.b;power:oc.bt.i"));
558 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
559 EXPECT_EQ(2, numResourceTypes);
560 resourceTypeAttrRepresentation = OCGetResourceAttributeRepresentation(handle, 1);
561 EXPECT_STREQ("state:oc.bt.b;power:oc.bt.i", resourceTypeAttrRepresentation);
563 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled", "state:oc.bt.b;power:oc.bt.i;temp:oc.bt.i"));
564 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
565 EXPECT_EQ(3, numResourceTypes);
566 resourceTypeAttrRepresentation = OCGetResourceAttributeRepresentation(handle, 2);
567 EXPECT_STREQ("state:oc.bt.b;power:oc.bt.i;temp:oc.bt.i", resourceTypeAttrRepresentation);
570 TEST(StackTest, ResourceTypeInterface) {
572 uint16_t port = USE_RANDOM_PORT;
573 uint8_t ifname[] = "eth0";
575 /*Get Ip address on defined interface and initialize coap on it with random port number
576 * this port number will be used as a source port in all coap communications*/
577 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
579 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
580 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
582 OCResourceHandle handle;
583 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
585 "state:oc.bt.b;power:oc.bt.i",
587 OC_REST_GET|OC_REST_PUT,
590 OC_DISCOVERABLE|OC_OBSERVABLE));
592 uint8_t numResourceInterfaces;
593 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
594 EXPECT_EQ(1, numResourceInterfaces);
595 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
596 EXPECT_STREQ("core.rw", resourceInterfaceName);
598 // try getting resource interface names with an invalid index
599 resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
600 EXPECT_STREQ(NULL, resourceInterfaceName);
601 // try getting resource interface names with an invalid index
602 resourceInterfaceName = OCGetResourceInterfaceName(handle, 10);
603 EXPECT_STREQ(NULL, resourceInterfaceName);
606 TEST(StackTest, BindResourceInterfaceNameBad) {
608 uint16_t port = USE_RANDOM_PORT;
609 uint8_t ifname[] = "eth0";
611 /*Get Ip address on defined interface and initialize coap on it with random port number
612 * this port number will be used as a source port in all coap communications*/
613 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
615 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
616 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
618 OCResourceHandle handle;
619 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
621 "state:oc.bt.b;power:oc.bt.i",
623 OC_REST_GET|OC_REST_PUT,
626 OC_DISCOVERABLE|OC_OBSERVABLE));
628 uint8_t numResourceInterfaces;
629 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
630 EXPECT_EQ(1, numResourceInterfaces);
631 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
632 EXPECT_STREQ("core.rw", resourceInterfaceName);
634 EXPECT_EQ(OC_STACK_ERROR, OCBindResourceInterfaceToResource(handle, NULL, OC_REST_GET|OC_REST_PUT));
637 TEST(StackTest, BindResourceInterfaceNameGood) {
639 uint16_t port = USE_RANDOM_PORT;
640 uint8_t ifname[] = "eth0";
642 /*Get Ip address on defined interface and initialize coap on it with random port number
643 * this port number will be used as a source port in all coap communications*/
644 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
646 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
647 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
649 OCResourceHandle handle;
650 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
652 "state:oc.bt.b;power:oc.bt.i",
654 OC_REST_GET|OC_REST_PUT,
657 OC_DISCOVERABLE|OC_OBSERVABLE));
659 uint8_t numResourceInterfaces;
660 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
661 EXPECT_EQ(1, numResourceInterfaces);
662 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 0);
663 EXPECT_STREQ("core.rw", resourceInterfaceName);
665 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r", OC_REST_GET|OC_REST_PUT));
667 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
668 EXPECT_EQ(2, numResourceInterfaces);
669 resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
670 EXPECT_STREQ("core.r", resourceInterfaceName);
673 TEST(StackTest, ResourceTypeInterfaceMethods) {
675 uint16_t port = USE_RANDOM_PORT;
676 uint8_t ifname[] = "eth0";
678 /*Get Ip address on defined interface and initialize coap on it with random port number
679 * this port number will be used as a source port in all coap communications*/
680 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
682 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
683 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
685 OCResourceHandle handle;
686 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
688 "state:oc.bt.b;power:oc.bt.i",
690 OC_REST_GET|OC_REST_PUT,
693 OC_DISCOVERABLE|OC_OBSERVABLE));
695 uint8_t numResourceInterfaces;
696 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
697 EXPECT_EQ(1, numResourceInterfaces);
698 uint8_t resourceInterfaceMethods = OCGetResourceInterfaceAllowedMethods(handle, 0);
699 EXPECT_EQ(OC_REST_GET|OC_REST_PUT, resourceInterfaceMethods);
701 // try getting resource interface methods with an invalid index
702 resourceInterfaceMethods = OCGetResourceInterfaceAllowedMethods(handle, 1);
703 EXPECT_EQ(0, resourceInterfaceMethods);
704 // try getting resource interface methods with an invalid index
705 resourceInterfaceMethods = OCGetResourceInterfaceAllowedMethods(handle, 10);
706 EXPECT_EQ(0, resourceInterfaceMethods);
709 TEST(StackTest, BindResourceInterfaceMethodsBad) {
711 uint16_t port = USE_RANDOM_PORT;
712 uint8_t ifname[] = "eth0";
714 /*Get Ip address on defined interface and initialize coap on it with random port number
715 * this port number will be used as a source port in all coap communications*/
716 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
718 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
719 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
721 OCResourceHandle handle;
722 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
724 "state:oc.bt.b;power:oc.bt.i",
726 OC_REST_GET|OC_REST_PUT,
729 OC_DISCOVERABLE|OC_OBSERVABLE));
731 uint8_t numResourceInterfaces;
732 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
733 EXPECT_EQ(1, numResourceInterfaces);
734 uint8_t resourceInterfaceMethods = OCGetResourceInterfaceAllowedMethods(handle, 0);
735 EXPECT_EQ(OC_REST_GET|OC_REST_PUT, resourceInterfaceMethods);
737 EXPECT_EQ(OC_STACK_ERROR, OCBindResourceInterfaceToResource(handle, "core.rw", OC_REST_NOMETHOD));
740 TEST(StackTest, BindResourceInterfaceMethodsGood) {
742 uint16_t port = USE_RANDOM_PORT;
743 uint8_t ifname[] = "eth0";
745 /*Get Ip address on defined interface and initialize coap on it with random port number
746 * this port number will be used as a source port in all coap communications*/
747 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
749 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
750 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
752 OCResourceHandle handle;
753 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
755 "state:oc.bt.b;power:oc.bt.i",
757 OC_REST_GET|OC_REST_PUT,
760 OC_DISCOVERABLE|OC_OBSERVABLE));
762 uint8_t numResourceInterfaces;
763 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
764 EXPECT_EQ(1, numResourceInterfaces);
765 uint8_t resourceInterfaceMethods = OCGetResourceInterfaceAllowedMethods(handle, 0);
766 EXPECT_EQ(OC_REST_GET|OC_REST_PUT, resourceInterfaceMethods);
768 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r", OC_REST_PUT));
770 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
771 EXPECT_EQ(2, numResourceInterfaces);
772 resourceInterfaceMethods = OCGetResourceInterfaceAllowedMethods(handle, 1);
773 EXPECT_EQ(OC_REST_PUT, resourceInterfaceMethods);
776 TEST(StackTest, BindContainedResourceBad) {
778 uint16_t port = USE_RANDOM_PORT;
779 uint8_t ifname[] = "eth0";
781 /*Get Ip address on defined interface and initialize coap on it with random port number
782 * this port number will be used as a source port in all coap communications*/
783 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
785 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
786 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
788 OCResourceHandle containerHandle;
789 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
791 "state:oc.bt.b;power:oc.bt.i",
793 OC_REST_GET|OC_REST_PUT,
796 OC_DISCOVERABLE|OC_OBSERVABLE));
798 OCResourceHandle handle0;
799 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
801 "state:oc.bt.b;power:oc.bt.i",
803 OC_REST_GET|OC_REST_PUT,
806 OC_DISCOVERABLE|OC_OBSERVABLE));
808 EXPECT_EQ(OC_STACK_ERROR, OCBindContainedResourceToResource(containerHandle, containerHandle));
810 OCResourceHandle handle1;
811 EXPECT_EQ(OC_STACK_ERROR, OCBindContainedResourceToResource(handle1, handle0));
814 TEST(StackTest, BindContainedResourceGood) {
816 uint16_t port = USE_RANDOM_PORT;
817 uint8_t ifname[] = "eth0";
819 /*Get Ip address on defined interface and initialize coap on it with random port number
820 * this port number will be used as a source port in all coap communications*/
821 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
823 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
824 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
826 uint8_t numResources = 0;
827 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
828 EXPECT_EQ(0, numResources);
830 OCResourceHandle containerHandle;
831 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
833 "state:oc.bt.b;power:oc.bt.i",
835 OC_REST_GET|OC_REST_PUT,
838 OC_DISCOVERABLE|OC_OBSERVABLE));
839 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
840 EXPECT_EQ(1, numResources);
842 OCResourceHandle handle0;
843 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
845 "state:oc.bt.b;power:oc.bt.i",
847 OC_REST_GET|OC_REST_PUT,
850 OC_DISCOVERABLE|OC_OBSERVABLE));
851 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
852 EXPECT_EQ(2, numResources);
854 OCResourceHandle handle1;
855 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
857 "state:oc.bt.b;power:oc.bt.i",
859 OC_REST_GET|OC_REST_PUT,
862 OC_DISCOVERABLE|OC_OBSERVABLE));
863 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
864 EXPECT_EQ(3, numResources);
866 OCResourceHandle handle2;
867 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
869 "state:oc.bt.b;power:oc.bt.i",
871 OC_REST_GET|OC_REST_PUT,
874 OC_DISCOVERABLE|OC_OBSERVABLE));
875 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
876 EXPECT_EQ(4, numResources);
878 OCResourceHandle handle3;
879 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
881 "state:oc.bt.b;power:oc.bt.i",
883 OC_REST_GET|OC_REST_PUT,
886 OC_DISCOVERABLE|OC_OBSERVABLE));
887 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
888 EXPECT_EQ(5, numResources);
890 OCResourceHandle handle4;
891 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
893 "state:oc.bt.b;power:oc.bt.i",
895 OC_REST_GET|OC_REST_PUT,
898 OC_DISCOVERABLE|OC_OBSERVABLE));
899 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
900 EXPECT_EQ(6, numResources);
902 OCResourceHandle handle5;
903 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
905 "state:oc.bt.b;power:oc.bt.i",
907 OC_REST_GET|OC_REST_PUT,
910 OC_DISCOVERABLE|OC_OBSERVABLE));
911 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
912 EXPECT_EQ(7, numResources);
915 EXPECT_EQ(OC_STACK_OK, OCBindContainedResourceToResource(containerHandle, handle0));
916 EXPECT_EQ(OC_STACK_OK, OCBindContainedResourceToResource(containerHandle, handle1));
917 EXPECT_EQ(OC_STACK_OK, OCBindContainedResourceToResource(containerHandle, handle2));
918 EXPECT_EQ(OC_STACK_OK, OCBindContainedResourceToResource(containerHandle, handle3));
919 EXPECT_EQ(OC_STACK_OK, OCBindContainedResourceToResource(containerHandle, handle4));
920 EXPECT_EQ(OC_STACK_ERROR, OCBindContainedResourceToResource(containerHandle, handle5));
922 EXPECT_EQ(handle0, OCGetContainedResourceHandle(containerHandle, 0));
923 EXPECT_EQ(handle1, OCGetContainedResourceHandle(containerHandle, 1));
924 EXPECT_EQ(handle2, OCGetContainedResourceHandle(containerHandle, 2));
925 EXPECT_EQ(handle3, OCGetContainedResourceHandle(containerHandle, 3));
926 EXPECT_EQ(handle4, OCGetContainedResourceHandle(containerHandle, 4));
928 EXPECT_EQ(NULL, OCGetContainedResourceHandle(containerHandle, 5));
931 TEST(StackTest, GetResourceByIndex) {
933 uint16_t port = USE_RANDOM_PORT;
934 uint8_t ifname[] = "eth0";
936 /*Get Ip address on defined interface and initialize coap on it with random port number
937 * this port number will be used as a source port in all coap communications*/
938 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
940 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
941 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
943 uint8_t numResources = 0;
944 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
945 EXPECT_EQ(0, numResources);
947 OCResourceHandle containerHandle;
948 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
950 "state:oc.bt.b;power:oc.bt.i",
952 OC_REST_GET|OC_REST_PUT,
955 OC_DISCOVERABLE|OC_OBSERVABLE));
956 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
957 EXPECT_EQ(1, numResources);
959 OCResourceHandle handle0;
960 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
962 "state:oc.bt.b;power:oc.bt.i",
964 OC_REST_GET|OC_REST_PUT,
967 OC_DISCOVERABLE|OC_OBSERVABLE));
968 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
969 EXPECT_EQ(2, numResources);
971 OCResourceHandle handle1;
972 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
974 "state:oc.bt.b;power:oc.bt.i",
976 OC_REST_GET|OC_REST_PUT,
979 OC_DISCOVERABLE|OC_OBSERVABLE));
980 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
981 EXPECT_EQ(3, numResources);
983 OCResourceHandle handle2;
984 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
986 "state:oc.bt.b;power:oc.bt.i",
988 OC_REST_GET|OC_REST_PUT,
991 OC_DISCOVERABLE|OC_OBSERVABLE));
992 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
993 EXPECT_EQ(4, numResources);
995 OCResourceHandle handle3;
996 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
998 "state:oc.bt.b;power:oc.bt.i",
1000 OC_REST_GET|OC_REST_PUT,
1003 OC_DISCOVERABLE|OC_OBSERVABLE));
1004 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1005 EXPECT_EQ(5, numResources);
1007 OCResourceHandle handle4;
1008 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
1010 "state:oc.bt.b;power:oc.bt.i",
1012 OC_REST_GET|OC_REST_PUT,
1015 OC_DISCOVERABLE|OC_OBSERVABLE));
1016 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1017 EXPECT_EQ(6, numResources);
1019 OCResourceHandle handle5;
1020 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
1022 "state:oc.bt.b;power:oc.bt.i",
1024 OC_REST_GET|OC_REST_PUT,
1027 OC_DISCOVERABLE|OC_OBSERVABLE));
1028 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1029 EXPECT_EQ(7, numResources);
1031 EXPECT_EQ(containerHandle, OCGetResourceHandle(0));
1032 EXPECT_EQ(handle0, OCGetResourceHandle(1));
1033 EXPECT_EQ(handle1, OCGetResourceHandle(2));
1034 EXPECT_EQ(handle2, OCGetResourceHandle(3));
1035 EXPECT_EQ(handle3, OCGetResourceHandle(4));
1036 EXPECT_EQ(handle4, OCGetResourceHandle(5));
1037 EXPECT_EQ(handle5, OCGetResourceHandle(6));
1041 TEST(StackTest, BindEntityHandlerBad) {
1043 uint16_t port = USE_RANDOM_PORT;
1044 uint8_t ifname[] = "eth0";
1046 /*Get Ip address on defined interface and initialize coap on it with random port number
1047 * this port number will be used as a source port in all coap communications*/
1048 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1050 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1051 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1053 OCResourceHandle handle;
1054 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1056 "state:oc.bt.b;power:oc.bt.i",
1058 OC_REST_GET|OC_REST_PUT,
1061 OC_DISCOVERABLE|OC_OBSERVABLE));
1063 EXPECT_EQ(OC_STACK_ERROR, OCBindResourceHandler(handle, NULL));
1064 EXPECT_EQ(OC_STACK_ERROR, OCBindResourceHandler(NULL, NULL));
1068 void entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest) {
1069 OC_LOG(INFO, TAG, "Entering entityHandler");
1072 TEST(StackTest, BindEntityHandlerGood) {
1074 uint16_t port = USE_RANDOM_PORT;
1075 uint8_t ifname[] = "eth0";
1077 /*Get Ip address on defined interface and initialize coap on it with random port number
1078 * this port number will be used as a source port in all coap communications*/
1079 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1081 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1082 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1084 OCResourceHandle handle;
1085 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1087 "state:oc.bt.b;power:oc.bt.i",
1089 OC_REST_GET|OC_REST_PUT,
1092 OC_DISCOVERABLE|OC_OBSERVABLE));
1094 OCEntityHandler myHandler = entityHandler;
1096 EXPECT_EQ(OC_STACK_OK, OCBindResourceHandler(handle, myHandler));
1098 EXPECT_EQ(myHandler, OCGetResourceHandler(handle));
1101 TEST(StackTest, DeleteHeadResource) {
1103 uint16_t port = USE_RANDOM_PORT;
1104 uint8_t ifname[] = "eth0";
1106 /*Get Ip address on defined interface and initialize coap on it with random port number
1107 * this port number will be used as a source port in all coap communications*/
1108 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1110 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1111 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1113 uint8_t numResources = 0;
1114 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1115 EXPECT_EQ(0, numResources);
1117 OCResourceHandle handle0;
1118 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1120 "state:oc.bt.b;power:oc.bt.i",
1122 OC_REST_GET|OC_REST_PUT,
1125 OC_DISCOVERABLE|OC_OBSERVABLE));
1126 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1127 EXPECT_EQ(1, numResources);
1129 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1130 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1131 EXPECT_EQ(0, numResources);
1134 TEST(StackTest, DeleteHeadResource2) {
1136 uint16_t port = USE_RANDOM_PORT;
1137 uint8_t ifname[] = "eth0";
1139 /*Get Ip address on defined interface and initialize coap on it with random port number
1140 * this port number will be used as a source port in all coap communications*/
1141 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1143 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1144 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1146 uint8_t numResources = 0;
1147 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1148 EXPECT_EQ(0, numResources);
1150 OCResourceHandle handle0;
1151 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1153 "state:oc.bt.b;power:oc.bt.i",
1155 OC_REST_GET|OC_REST_PUT,
1158 OC_DISCOVERABLE|OC_OBSERVABLE));
1159 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1160 EXPECT_EQ(1, numResources);
1162 OCResourceHandle handle1;
1163 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1165 "state:oc.bt.b;power:oc.bt.i",
1167 OC_REST_GET|OC_REST_PUT,
1170 OC_DISCOVERABLE|OC_OBSERVABLE));
1171 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1172 EXPECT_EQ(2, numResources);
1174 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1175 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1176 EXPECT_EQ(1, numResources);
1178 EXPECT_EQ(handle1, OCGetResourceHandle(0));
1182 TEST(StackTest, DeleteLastResource) {
1184 uint16_t port = USE_RANDOM_PORT;
1185 uint8_t ifname[] = "eth0";
1187 /*Get Ip address on defined interface and initialize coap on it with random port number
1188 * this port number will be used as a source port in all coap communications*/
1189 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1191 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1192 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1194 uint8_t numResources = 0;
1195 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1196 EXPECT_EQ(0, numResources);
1198 OCResourceHandle handle0;
1199 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1201 "state:oc.bt.b;power:oc.bt.i",
1203 OC_REST_GET|OC_REST_PUT,
1206 OC_DISCOVERABLE|OC_OBSERVABLE));
1207 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1208 EXPECT_EQ(1, numResources);
1210 OCResourceHandle handle1;
1211 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1213 "state:oc.bt.b;power:oc.bt.i",
1215 OC_REST_GET|OC_REST_PUT,
1218 OC_DISCOVERABLE|OC_OBSERVABLE));
1219 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1220 EXPECT_EQ(2, numResources);
1222 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1223 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1224 EXPECT_EQ(1, numResources);
1226 EXPECT_EQ(handle0, OCGetResourceHandle(0));
1228 OCResourceHandle handle2;
1229 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1231 "state:oc.bt.b;power:oc.bt.i",
1233 OC_REST_GET|OC_REST_PUT,
1236 OC_DISCOVERABLE|OC_OBSERVABLE));
1237 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1238 EXPECT_EQ(2, numResources);
1241 TEST(StackTest, DeleteMiddleResource) {
1243 uint16_t port = USE_RANDOM_PORT;
1244 uint8_t ifname[] = "eth0";
1246 /*Get Ip address on defined interface and initialize coap on it with random port number
1247 * this port number will be used as a source port in all coap communications*/
1248 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1250 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1251 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1253 uint8_t numResources = 0;
1254 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1255 EXPECT_EQ(0, numResources);
1257 OCResourceHandle handle0;
1258 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1260 "state:oc.bt.b;power:oc.bt.i",
1262 OC_REST_GET|OC_REST_PUT,
1265 OC_DISCOVERABLE|OC_OBSERVABLE));
1266 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1267 EXPECT_EQ(1, numResources);
1269 OCResourceHandle handle1;
1270 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1272 "state:oc.bt.b;power:oc.bt.i",
1274 OC_REST_GET|OC_REST_PUT,
1277 OC_DISCOVERABLE|OC_OBSERVABLE));
1278 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1279 EXPECT_EQ(2, numResources);
1281 OCResourceHandle handle2;
1282 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1284 "state:oc.bt.b;power:oc.bt.i",
1286 OC_REST_GET|OC_REST_PUT,
1289 OC_DISCOVERABLE|OC_OBSERVABLE));
1290 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1291 EXPECT_EQ(3, numResources);
1293 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1294 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1295 EXPECT_EQ(2, numResources);
1297 EXPECT_EQ(handle0, OCGetResourceHandle(0));
1298 EXPECT_EQ(handle2, OCGetResourceHandle(1));
1300 // Make sure the resource elements are still correct
1301 uint8_t numResourceInterfaces;
1302 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle2, &numResourceInterfaces));
1303 EXPECT_EQ(1, numResourceInterfaces);
1304 const char *resourceInterfaceName = OCGetResourceInterfaceName(handle2, 0);
1305 EXPECT_STREQ("core.rw", resourceInterfaceName);
1308 TEST(StackTest, GetResourceProperties) {
1310 uint16_t port = USE_RANDOM_PORT;
1311 uint8_t ifname[] = "eth0";
1313 /*Get Ip address on defined interface and initialize coap on it with random port number
1314 * this port number will be used as a source port in all coap communications*/
1315 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1317 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1318 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1320 OCResourceHandle handle;
1321 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1323 "state:oc.bt.b;power:oc.bt.i",
1325 OC_REST_GET|OC_REST_PUT,
1328 OC_DISCOVERABLE|OC_OBSERVABLE));
1330 EXPECT_EQ(OC_ACTIVE|OC_DISCOVERABLE|OC_OBSERVABLE, OCGetResourceProperties(handle));
1331 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1334 TEST(StackTest, StackTestResourceDiscoverOneResourceBad) {
1335 // Setting URI to null which is an error
1338 char req[1024] = {};
1339 char rsp[1024] = {};
1340 //OCServerRequestResult res;
1342 OCResourceHandle handle;
1343 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1345 "state:oc.bt.b;power:oc.bt.i",
1347 OC_REST_GET|OC_REST_PUT,
1350 OC_DISCOVERABLE|OC_OBSERVABLE));
1351 const char *url = OCGetResourceUri(handle);
1352 EXPECT_STREQ("/a1/led", url);
1354 //EXPECT_EQ(OC_STACK_INVALID_URI, OCHandleServerRequest(&res, uri, query, req, rsp));
1355 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1356 uint8_t numResources = 0;
1357 EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1358 EXPECT_EQ(0, numResources);
1362 TEST(StackTest, StackTestResourceDiscoverOneResource) {
1363 char uri[] = "/oc/core";
1365 char req[1024] = {};
1366 char rsp[1024] = {};
1367 //OCServerRequestResult res;
1369 OCResourceHandle handle;
1370 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1372 "state:oc.bt.b;power:oc.bt.i",
1374 OC_REST_GET|OC_REST_PUT,
1377 OC_DISCOVERABLE|OC_OBSERVABLE));
1378 const char *url = OCGetResourceUri(handle);
1379 EXPECT_STREQ("/a/led", url);
1381 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1382 EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1385 TEST(StackTest, StackTestResourceDiscoverManyResources) {
1387 uint16_t port = USE_RANDOM_PORT;
1388 uint8_t ifname[] = "eth0";
1389 char uri[] = "/oc/core";
1391 char req[1024] = {};
1392 char rsp[1024] = {};
1393 //OCServerRequestResult res;
1395 /*Get Ip address on defined interface and initialize coap on it with random port number
1396 * this port number will be used as a source port in all coap communications*/
1397 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
1399 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
1400 EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, OC_SERVER));
1402 OCResourceHandle handle1;
1403 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1405 "state:oc.bt.b;power:oc.bt.i",
1407 OC_REST_GET|OC_REST_PUT,
1411 const char *url = OCGetResourceUri(handle1);
1412 EXPECT_STREQ("/a/led1", url);
1414 OCResourceHandle handle2;
1415 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1417 "state:oc.bt.b;power:oc.bt.i",
1419 OC_REST_GET|OC_REST_PUT,
1422 OC_DISCOVERABLE|OC_OBSERVABLE));
1423 url = OCGetResourceUri(handle2);
1424 EXPECT_STREQ("/a/led2", url);
1426 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.brightled",
1427 "state:oc.bt.b;power:oc.bt.i"));
1428 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.colorled",
1429 "state:oc.bt.b;power:oc.bt.i"));
1431 OCResourceHandle handle3;
1432 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
1434 "state:oc.bt.b;power:oc.bt.i",
1436 OC_REST_GET|OC_REST_PUT,
1439 OC_DISCOVERABLE|OC_OBSERVABLE));
1440 url = OCGetResourceUri(handle3);
1441 EXPECT_STREQ("/a/led3", url);
1443 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.ll",
1444 OC_REST_GET|OC_REST_PUT));
1445 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.b",
1446 OC_REST_GET|OC_REST_PUT));
1448 OCResourceHandle handle4;
1449 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
1451 "state:oc.bt.b;power:oc.bt.i",
1453 OC_REST_GET|OC_REST_PUT,
1457 url = OCGetResourceUri(handle4);
1458 EXPECT_STREQ("/a/led4", url);
1460 EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle4, "core.brightled",
1461 "state:oc.bt.b;power:oc.bt.i"));
1462 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, "oc.mi.ll",
1463 OC_REST_GET|OC_REST_PUT));
1464 EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, "oc.mi.b",
1465 OC_REST_GET|OC_REST_PUT));
1467 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1470 TEST(StackTest, StackTestResourceDiscoverIfFilteringBad) {
1472 uint16_t port = USE_RANDOM_PORT;
1473 uint8_t ifname[] = "eth0";
1474 char uri[] = "/oc/core";
1475 char query[] = "if";
1476 char req[1024] = {};
1477 char rsp[1024] = {};
1478 //OCServerRequestResult res;
1480 //EXPECT_EQ(OC_STACK_INVALID_QUERY, OCHandleServerRequest(&res, uri, query, req, rsp));
1483 TEST(StackTest, StackTestResourceDiscoverRtFilteringBad) {
1485 uint16_t port = USE_RANDOM_PORT;
1486 uint8_t ifname[] = "eth0";
1487 char uri[] = "/oc/core";
1488 char query[] = "rt";
1489 char req[1024] = {};
1490 char rsp[1024] = {};
1491 //OCServerRequestResult res;
1493 //EXPECT_EQ(OC_STACK_INVALID_QUERY, OCHandleServerRequest(&res, uri, query, req, rsp));
1495 TEST(StackTest, StackTestResourceDiscoverIfFiltering) {
1497 uint16_t port = USE_RANDOM_PORT;
1498 uint8_t ifname[] = "eth0";
1499 char uri[] = "/oc/core";
1500 char query[] = "if=oc.mi.ll";
1501 char req[1024] = {};
1502 char rsp[1024] = {};
1503 //OCServerRequestResult res;
1505 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1508 TEST(StackTest, StackTestResourceDiscoverRtFiltering) {
1510 uint16_t port = USE_RANDOM_PORT;
1511 uint8_t ifname[] = "eth0";
1512 char uri[] = "/oc/core";
1513 char query[] = "rt=core.brightled";
1514 char req[1024] = {};
1515 char rsp[1024] = {};
1516 //OCServerRequestResult res;
1518 //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));