Modifying version number for building on tizen 3.0
[platform/upstream/iotivity.git] / resource / csdk / stack / test / stacktests.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 extern "C" {
23     #include "ocstack.h"
24     #include "ocstackinternal.h"
25     #include "logger.h"
26 }
27
28 #include "gtest/gtest.h"
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35
36 //-----------------------------------------------------------------------------
37 // Includes
38 //-----------------------------------------------------------------------------
39 #include <stdio.h>
40 #include <string.h>
41
42 #include <iostream>
43 #include <stdint.h>
44 using namespace std;
45
46 #define DEFAULT_CONTEXT_VALUE 0x99
47 //-----------------------------------------------------------------------------
48 // Private variables
49 //-----------------------------------------------------------------------------
50 static const char TAG[] = "TestHarness";
51
52 //-----------------------------------------------------------------------------
53 // Callback functions
54 //-----------------------------------------------------------------------------
55 extern "C"  OCStackApplicationResult asyncDoResourcesCallback(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
56     OC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
57
58     EXPECT_EQ(OC_STACK_OK, clientResponse->result);
59
60     if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
61         OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
62     }
63     OC_LOG_V(INFO, TAG, "result = %d", clientResponse->result);
64
65     return OC_STACK_KEEP_TRANSACTION;
66 }
67
68
69 //-----------------------------------------------------------------------------
70 //  Tests
71 //-----------------------------------------------------------------------------
72 #if 1
73 TEST(StackTest, StackInitNullAddr) {
74     EXPECT_EQ(OC_STACK_OK, OCInit(0, 5683, OC_SERVER));
75     EXPECT_EQ(OC_STACK_OK, OCStop());
76 }
77
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());
81 }
82
83 TEST(StackTest, StackInitNullAddrAndPort) {
84     EXPECT_EQ(OC_STACK_OK, OCInit(0, 0, OC_SERVER));
85     EXPECT_EQ(OC_STACK_OK, OCStop());
86 }
87
88 TEST(StackTest, StackInitInvalidMode) {
89     EXPECT_EQ(OC_STACK_ERROR, OCInit(0, 0, (OCMode)10));
90     EXPECT_EQ(OC_STACK_ERROR, OCStop());
91 }
92
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());
96 }
97
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());
101 }
102
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());
106 }
107
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());
112 }
113
114 TEST(StackTest, DoResourceDeviceDiscovery) {
115     uint8_t addr[20];
116     uint16_t port = USE_RANDOM_PORT;
117     uint8_t ifname[] = "eth0";
118     OCCallbackData cbData;
119     OCDoHandle handle;
120
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);
125
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;
132     cbData.cd = NULL;
133
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());
137 }
138
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());
143 }
144
145 TEST(StackTest, UpdateResourceNullURI) {
146     uint8_t addr[20];
147     uint16_t port = USE_RANDOM_PORT;
148     uint8_t ifname[] = "eth0";
149     OCCallbackData cbData;
150     OCDoHandle handle;
151
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);
156
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;
163     cbData.cd = NULL;
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());
167 }
168
169 TEST(StackTest, CreateResourceBadParams) {
170     uint8_t addr[20];
171     uint16_t port = USE_RANDOM_PORT;
172     uint8_t ifname[] = "eth0";
173
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));
177
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));
180
181     OCResourceHandle handle;
182
183     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(NULL, //&handle,
184                                             "core.led",
185                                             "core.rw",
186                                             "/a/led",
187                                             0,
188                                             OC_DISCOVERABLE|OC_OBSERVABLE));
189
190     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
191                                             NULL, //"core.led",
192                                             "core.rw",
193                                             "/a/led",
194                                             0,
195                                             OC_DISCOVERABLE|OC_OBSERVABLE));
196
197     // Property bitmask out of range
198     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
199                                             "core.led",
200                                             "core.rw",
201                                             "/a/led",
202                                             0,
203                                             16));//OC_DISCOVERABLE|OC_OBSERVABLE));
204
205     EXPECT_EQ(OC_STACK_OK, OCStop());
206 }
207 #endif
208
209 TEST(StackTest, CreateResourceSuccess) {
210     uint8_t addr[20];
211     uint16_t port = USE_RANDOM_PORT;
212     uint8_t ifname[] = "eth0";
213
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));
217
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));
220
221     OCResourceHandle handle;
222     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
223                                             "core.led",
224                                             "core.rw",
225                                             "/a/led",
226                                             0,
227                                             OC_DISCOVERABLE|OC_OBSERVABLE));
228     const char *url = OCGetResourceUri(handle);
229     EXPECT_STREQ("/a/led", url);
230
231     EXPECT_EQ(OC_STACK_OK, OCStop());
232 }
233
234 TEST(StackTest, CreateResourceFailDuplicateUri) {
235     uint8_t addr[20];
236     uint16_t port = USE_RANDOM_PORT;
237     uint8_t ifname[] = "eth0";
238
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));
242
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));
245
246     OCResourceHandle handle;
247     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
248                                             "core.led",
249                                             "core.rw",
250                                             "/a/led",
251                                             0,
252                                             OC_DISCOVERABLE|OC_OBSERVABLE));
253     const char *url = OCGetResourceUri(handle);
254     EXPECT_STREQ("/a/led", url);
255
256     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
257                                             "core.led",
258                                             "core.rw",
259                                             "/a/led",
260                                             0,
261                                             OC_DISCOVERABLE|OC_OBSERVABLE));
262
263     EXPECT_EQ(OC_STACK_OK, OCStop());
264 }
265
266 TEST(StackTest, CreateResourceMultipleResources) {
267     uint8_t addr[20];
268     uint16_t port = USE_RANDOM_PORT;
269     uint8_t ifname[] = "eth0";
270
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));
274
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));
277
278     OCResourceHandle handle1;
279     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
280                                             "core.led",
281                                             "core.rw",
282                                             "/a/led1",
283                                             0,
284                                             OC_DISCOVERABLE|OC_OBSERVABLE));
285
286     OCResourceHandle handle2;
287     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
288                                             "core.led",
289                                             "core.rw",
290                                             "/a/led2",
291                                             0,
292                                             OC_DISCOVERABLE|OC_OBSERVABLE));
293     OCResourceHandle handle3;
294     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
295                                             "core.led",
296                                             "core.rw",
297                                             "/a/led3",
298                                             0,
299                                             OC_DISCOVERABLE|OC_OBSERVABLE));
300
301     const char *url = OCGetResourceUri(handle1);
302     EXPECT_STREQ("/a/led1", url);
303
304     url = OCGetResourceUri(handle2);
305     EXPECT_STREQ("/a/led2", url);
306
307     url = OCGetResourceUri(handle3);
308     EXPECT_STREQ("/a/led3", url);
309
310     EXPECT_EQ(OC_STACK_OK, OCStop());
311 }
312
313 TEST(StackTest, CreateResourceBadResoureType) {
314     uint8_t addr[20];
315     uint16_t port = USE_RANDOM_PORT;
316     uint8_t ifname[] = "eth0";
317
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));
321
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));
324
325     OCResourceHandle handle;
326     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
327                                             NULL, //"core.led",
328                                             "core.rw",
329                                             "/a/led",
330                                             0,
331                                             OC_DISCOVERABLE|OC_OBSERVABLE));
332
333     EXPECT_EQ(OC_STACK_OK, OCStop());
334 }
335
336 TEST(StackTest, CreateResourceGoodResourceType) {
337     uint8_t addr[20];
338     uint16_t port = USE_RANDOM_PORT;
339     uint8_t ifname[] = "eth0";
340
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));
344
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));
347
348     OCResourceHandle handle;
349     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
350                                             "core.led",
351                                             "core.rw",
352                                             "/a/led",
353                                             0,
354                                             OC_DISCOVERABLE|OC_OBSERVABLE));
355
356     EXPECT_EQ(OC_STACK_OK, OCStop());
357 }
358
359 TEST(StackTest, ResourceTypeName) {
360     uint8_t addr[20];
361     uint16_t port = USE_RANDOM_PORT;
362     uint8_t ifname[] = "eth0";
363
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));
367
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));
370
371     OCResourceHandle handle;
372     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
373                                             "core.led",
374                                             "core.rw",
375                                             "/a/led",
376                                             0,
377                                             OC_DISCOVERABLE|OC_OBSERVABLE));
378
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);
384
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);
391
392     EXPECT_EQ(OC_STACK_OK, OCStop());
393 }
394
395 TEST(StackTest, BindResourceTypeNameBad) {
396     uint8_t addr[20];
397     uint16_t port = USE_RANDOM_PORT;
398     uint8_t ifname[] = "eth0";
399
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));
403
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));
406
407     OCResourceHandle handle;
408     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
409                                             "core.led",
410                                             "core.rw",
411                                             "/a/led",
412                                             0,
413                                             OC_DISCOVERABLE|OC_OBSERVABLE));
414
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);
420
421     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceTypeToResource(handle, NULL));
422
423     EXPECT_EQ(OC_STACK_OK, OCStop());
424 }
425
426 TEST(StackTest, BindResourceTypeNameGood) {
427     uint8_t addr[20];
428     uint16_t port = USE_RANDOM_PORT;
429     uint8_t ifname[] = "eth0";
430
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));
434
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));
437
438     OCResourceHandle handle;
439     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
440                                             "core.led",
441                                             "core.rw",
442                                             "/a/led",
443                                             0,
444                                             OC_DISCOVERABLE|OC_OBSERVABLE));
445
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);
451
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);
457
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);
463
464     EXPECT_EQ(OC_STACK_OK, OCStop());
465 }
466
467 TEST(StackTest, ResourceTypeAttrRepresentation) {
468     uint8_t addr[20];
469     uint16_t port = USE_RANDOM_PORT;
470     uint8_t ifname[] = "eth0";
471
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));
475
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));
478
479     OCResourceHandle handle;
480     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
481                                             "core.led",
482                                             "core.rw",
483                                             "/a/led",
484                                             0,
485                                             OC_DISCOVERABLE|OC_OBSERVABLE));
486
487     uint8_t numResourceTypes;
488     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
489     EXPECT_EQ(1, numResourceTypes);
490
491     EXPECT_EQ(OC_STACK_OK, OCStop());
492 }
493
494 TEST(StackTest, BindResourceTypeAttribRepGood) {
495     uint8_t addr[20];
496     uint16_t port = USE_RANDOM_PORT;
497     uint8_t ifname[] = "eth0";
498
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));
502
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));
505
506     OCResourceHandle handle;
507     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
508                                             "core.led",
509                                             "core.rw",
510                                             "/a/led",
511                                             0,
512                                             OC_DISCOVERABLE|OC_OBSERVABLE));
513
514     uint8_t numResourceTypes;
515     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
516     EXPECT_EQ(1, numResourceTypes);
517
518     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
519     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
520     EXPECT_EQ(2, numResourceTypes);
521
522     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
523     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
524     EXPECT_EQ(3, numResourceTypes);
525
526     EXPECT_EQ(OC_STACK_OK, OCStop());
527 }
528
529 TEST(StackTest, ResourceTypeInterface) {
530     uint8_t addr[20];
531     uint16_t port = USE_RANDOM_PORT;
532     uint8_t ifname[] = "eth0";
533
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));
537
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));
540
541     OCResourceHandle handle;
542     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
543                                             "core.led",
544                                             "core.rw",
545                                             "/a/led",
546                                             0,
547                                             OC_DISCOVERABLE|OC_OBSERVABLE));
548
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);
554
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);
561
562     EXPECT_EQ(OC_STACK_OK, OCStop());
563 }
564
565 TEST(StackTest, BindResourceInterfaceNameBad) {
566     uint8_t addr[20];
567     uint16_t port = USE_RANDOM_PORT;
568     uint8_t ifname[] = "eth0";
569
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));
573
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));
576
577     OCResourceHandle handle;
578     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
579                                             "core.led",
580                                             "core.rw",
581                                             "/a/led",
582                                             0,
583                                             OC_DISCOVERABLE|OC_OBSERVABLE));
584
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);
590
591     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, NULL));
592
593     EXPECT_EQ(OC_STACK_OK, OCStop());
594 }
595
596 TEST(StackTest, BindResourceInterfaceNameGood) {
597     uint8_t addr[20];
598     uint16_t port = USE_RANDOM_PORT;
599     uint8_t ifname[] = "eth0";
600
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));
604
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));
607
608     OCResourceHandle handle;
609     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
610                                             "core.led",
611                                             "core.rw",
612                                             "/a/led",
613                                             0,
614                                             OC_DISCOVERABLE|OC_OBSERVABLE));
615
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);
621
622     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
623
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);
628
629     EXPECT_EQ(OC_STACK_OK, OCStop());
630 }
631
632 TEST(StackTest, ResourceTypeInterfaceMethods) {
633     uint8_t addr[20];
634     uint16_t port = USE_RANDOM_PORT;
635     uint8_t ifname[] = "eth0";
636
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));
640
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));
643
644     OCResourceHandle handle;
645     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
646                                             "core.led",
647                                             "core.rw",
648                                             "/a/led",
649                                             0,
650                                             OC_DISCOVERABLE|OC_OBSERVABLE));
651
652     uint8_t numResourceInterfaces;
653     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
654     EXPECT_EQ(1, numResourceInterfaces);
655
656     EXPECT_EQ(OC_STACK_OK, OCStop());
657 }
658
659 TEST(StackTest, BindResourceInterfaceMethodsBad) {
660     uint8_t addr[20];
661     uint16_t port = USE_RANDOM_PORT;
662     uint8_t ifname[] = "eth0";
663
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));
667
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));
670
671     OCResourceHandle handle;
672     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
673                                             "core.led",
674                                             "core.rw",
675                                             "/a/led",
676                                             0,
677                                             OC_DISCOVERABLE|OC_OBSERVABLE));
678
679     uint8_t numResourceInterfaces;
680     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
681     EXPECT_EQ(1, numResourceInterfaces);
682
683     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, 0));
684
685     EXPECT_EQ(OC_STACK_OK, OCStop());
686 }
687
688 TEST(StackTest, BindResourceInterfaceMethodsGood) {
689     uint8_t addr[20];
690     uint16_t port = USE_RANDOM_PORT;
691     uint8_t ifname[] = "eth0";
692
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));
696
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));
699
700     OCResourceHandle handle;
701     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
702                                             "core.led",
703                                             "core.rw",
704                                             "/a/led",
705                                             0,
706                                             OC_DISCOVERABLE|OC_OBSERVABLE));
707
708     uint8_t numResourceInterfaces;
709     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
710     EXPECT_EQ(1, numResourceInterfaces);
711
712     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
713
714     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
715     EXPECT_EQ(2, numResourceInterfaces);
716
717     EXPECT_EQ(OC_STACK_OK, OCStop());
718 }
719
720 TEST(StackTest, BindContainedResourceBad) {
721     uint8_t addr[20];
722     uint16_t port = USE_RANDOM_PORT;
723     uint8_t ifname[] = "eth0";
724
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));
728
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));
731
732     OCResourceHandle containerHandle;
733     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
734                                             "core.led",
735                                             "core.rw",
736                                             "/a/kitchen",
737                                             0,
738                                             OC_DISCOVERABLE|OC_OBSERVABLE));
739
740     OCResourceHandle handle0;
741     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
742                                             "core.led",
743                                             "core.rw",
744                                             "/a/led",
745                                             0,
746                                             OC_DISCOVERABLE|OC_OBSERVABLE));
747
748     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResource(containerHandle, containerHandle));
749
750     EXPECT_EQ(OC_STACK_ERROR, OCBindResource((OCResourceHandle) 0, handle0));
751
752     EXPECT_EQ(OC_STACK_OK, OCStop());
753 }
754
755 TEST(StackTest, BindContainedResourceGood) {
756     uint8_t addr[20];
757     uint16_t port = USE_RANDOM_PORT;
758     uint8_t ifname[] = "eth0";
759
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));
763
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));
766
767     uint8_t numResources = 0;
768     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
769     EXPECT_EQ(0, numResources);
770
771     OCResourceHandle containerHandle;
772     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
773                                             "core.led",
774                                             "core.rw",
775                                             "/a/kitchen",
776                                             0,
777                                             OC_DISCOVERABLE|OC_OBSERVABLE));
778     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
779     EXPECT_EQ(1, numResources);
780
781     OCResourceHandle handle0;
782     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
783                                             "core.led",
784                                             "core.rw",
785                                             "/a/led0",
786                                             0,
787                                             OC_DISCOVERABLE|OC_OBSERVABLE));
788     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
789     EXPECT_EQ(2, numResources);
790
791     OCResourceHandle handle1;
792     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
793                                             "core.led",
794                                             "core.rw",
795                                             "/a/led1",
796                                             0,
797                                             OC_DISCOVERABLE|OC_OBSERVABLE));
798     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
799     EXPECT_EQ(3, numResources);
800
801     OCResourceHandle handle2;
802     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
803                                             "core.led",
804                                             "core.rw",
805                                             "/a/led2",
806                                             0,
807                                             OC_DISCOVERABLE|OC_OBSERVABLE));
808     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
809     EXPECT_EQ(4, numResources);
810
811     OCResourceHandle handle3;
812     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
813                                             "core.led",
814                                             "core.rw",
815                                             "/a/led3",
816                                             0,
817                                             OC_DISCOVERABLE|OC_OBSERVABLE));
818     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
819     EXPECT_EQ(5, numResources);
820
821     OCResourceHandle handle4;
822     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
823                                             "core.led",
824                                             "core.rw",
825                                             "/a/led4",
826                                             0,
827                                             OC_DISCOVERABLE|OC_OBSERVABLE));
828     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
829     EXPECT_EQ(6, numResources);
830
831     OCResourceHandle handle5;
832     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
833                                             "core.led",
834                                             "core.rw",
835                                             "/a/led5",
836                                             0,
837                                             OC_DISCOVERABLE|OC_OBSERVABLE));
838     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
839     EXPECT_EQ(7, numResources);
840
841
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));
848
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));
854
855     EXPECT_EQ(NULL, OCGetResourceHandleFromCollection(containerHandle, 5));
856
857     EXPECT_EQ(OC_STACK_OK, OCStop());
858 }
859
860 TEST(StackTest, GetResourceByIndex) {
861     uint8_t addr[20];
862     uint16_t port = USE_RANDOM_PORT;
863     uint8_t ifname[] = "eth0";
864
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));
868
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));
871
872     uint8_t numResources = 0;
873     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
874     EXPECT_EQ(0, numResources);
875
876     OCResourceHandle containerHandle;
877     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
878                                             "core.led",
879                                             "core.rw",
880                                             "/a/kitchen",
881                                             0,
882                                             OC_DISCOVERABLE|OC_OBSERVABLE));
883     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
884     EXPECT_EQ(1, numResources);
885
886     OCResourceHandle handle0;
887     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
888                                             "core.led",
889                                             "core.rw",
890                                             "/a/led0",
891                                             0,
892                                             OC_DISCOVERABLE|OC_OBSERVABLE));
893     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
894     EXPECT_EQ(2, numResources);
895
896     OCResourceHandle handle1;
897     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
898                                             "core.led",
899                                             "core.rw",
900                                             "/a/led1",
901                                             0,
902                                             OC_DISCOVERABLE|OC_OBSERVABLE));
903     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
904     EXPECT_EQ(3, numResources);
905
906     OCResourceHandle handle2;
907     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
908                                             "core.led",
909                                             "core.rw",
910                                             "/a/led2",
911                                             0,
912                                             OC_DISCOVERABLE|OC_OBSERVABLE));
913     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
914     EXPECT_EQ(4, numResources);
915
916     OCResourceHandle handle3;
917     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
918                                             "core.led",
919                                             "core.rw",
920                                             "/a/led3",
921                                             0,
922                                             OC_DISCOVERABLE|OC_OBSERVABLE));
923     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
924     EXPECT_EQ(5, numResources);
925
926     OCResourceHandle handle4;
927     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
928                                             "core.led",
929                                             "core.rw",
930                                             "/a/led4",
931                                             0,
932                                             OC_DISCOVERABLE|OC_OBSERVABLE));
933     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
934     EXPECT_EQ(6, numResources);
935
936     OCResourceHandle handle5;
937     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
938                                             "core.led",
939                                             "core.rw",
940                                             "/a/led5",
941                                             0,
942                                             OC_DISCOVERABLE|OC_OBSERVABLE));
943     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
944     EXPECT_EQ(7, numResources);
945
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));
953
954     EXPECT_EQ(OC_STACK_OK, OCStop());
955 }
956
957 TEST(StackTest, BindEntityHandlerBad) {
958     uint8_t addr[20];
959     uint16_t port = USE_RANDOM_PORT;
960     uint8_t ifname[] = "eth0";
961
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));
965
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));
968
969     OCResourceHandle handle;
970     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
971                                             "core.led",
972                                             "core.rw",
973                                             "/a/led",
974                                             0,
975                                             OC_DISCOVERABLE|OC_OBSERVABLE));
976
977     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceHandler(handle, NULL));
978     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceHandler(NULL, NULL));
979
980     EXPECT_EQ(OC_STACK_OK, OCStop());
981 }
982
983
984 OCEntityHandlerResult entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest) {
985     OC_LOG(INFO, TAG, "Entering entityHandler");
986
987     return OC_EH_OK;
988 }
989
990 TEST(StackTest, BindEntityHandlerGood) {
991     uint8_t addr[20];
992     uint16_t port = USE_RANDOM_PORT;
993     uint8_t ifname[] = "eth0";
994
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));
998
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));
1001
1002     OCResourceHandle handle;
1003     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1004                                             "core.led",
1005                                             "core.rw",
1006                                             "/a/led",
1007                                             0,
1008                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1009
1010     OCEntityHandler myHandler = entityHandler;
1011
1012     EXPECT_EQ(OC_STACK_OK, OCBindResourceHandler(handle, myHandler));
1013
1014     EXPECT_EQ(myHandler, OCGetResourceHandler(handle));
1015
1016     EXPECT_EQ(OC_STACK_OK, OCStop());
1017 }
1018
1019 TEST(StackTest, DeleteHeadResource) {
1020     uint8_t addr[20];
1021     uint16_t port = USE_RANDOM_PORT;
1022     uint8_t ifname[] = "eth0";
1023
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));
1027
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));
1030
1031     uint8_t numResources = 0;
1032     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1033     EXPECT_EQ(0, numResources);
1034
1035     OCResourceHandle handle0;
1036     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1037                                             "core.led",
1038                                             "core.rw",
1039                                             "/a/led0",
1040                                             0,
1041                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1042     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1043     EXPECT_EQ(1, numResources);
1044
1045     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1046     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1047     EXPECT_EQ(0, numResources);
1048
1049     EXPECT_EQ(OC_STACK_OK, OCStop());
1050 }
1051
1052 TEST(StackTest, DeleteHeadResource2) {
1053     uint8_t addr[20];
1054     uint16_t port = USE_RANDOM_PORT;
1055     uint8_t ifname[] = "eth0";
1056
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));
1060
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));
1063
1064     uint8_t numResources = 0;
1065     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1066     EXPECT_EQ(0, numResources);
1067
1068     OCResourceHandle handle0;
1069     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1070                                             "core.led",
1071                                             "core.rw",
1072                                             "/a/led0",
1073                                             0,
1074                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1075     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1076     EXPECT_EQ(1, numResources);
1077
1078     OCResourceHandle handle1;
1079     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1080                                             "core.led",
1081                                             "core.rw",
1082                                             "/a/led1",
1083                                             0,
1084                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1085     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1086     EXPECT_EQ(2, numResources);
1087
1088     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
1089     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1090     EXPECT_EQ(1, numResources);
1091
1092     EXPECT_EQ(handle1, OCGetResourceHandle(0));
1093
1094     EXPECT_EQ(OC_STACK_OK, OCStop());
1095 }
1096
1097
1098 TEST(StackTest, DeleteLastResource) {
1099     uint8_t addr[20];
1100     uint16_t port = USE_RANDOM_PORT;
1101     uint8_t ifname[] = "eth0";
1102
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));
1106
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));
1109
1110     uint8_t numResources = 0;
1111     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1112     EXPECT_EQ(0, numResources);
1113
1114     OCResourceHandle handle0;
1115     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1116                                             "core.led",
1117                                             "core.rw",
1118                                             "/a/led0",
1119                                             0,
1120                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1121     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1122     EXPECT_EQ(1, numResources);
1123
1124     OCResourceHandle handle1;
1125     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1126                                             "core.led",
1127                                             "core.rw",
1128                                             "/a/led1",
1129                                             0,
1130                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1131     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1132     EXPECT_EQ(2, numResources);
1133
1134     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1135     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1136     EXPECT_EQ(1, numResources);
1137
1138     EXPECT_EQ(handle0, OCGetResourceHandle(0));
1139
1140     OCResourceHandle handle2;
1141     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1142                                             "core.led",
1143                                             "core.rw",
1144                                             "/a/led2",
1145                                             0,
1146                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1147     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1148     EXPECT_EQ(2, numResources);
1149
1150     EXPECT_EQ(OC_STACK_OK, OCStop());
1151 }
1152
1153 TEST(StackTest, DeleteMiddleResource) {
1154     uint8_t addr[20];
1155     uint16_t port = USE_RANDOM_PORT;
1156     uint8_t ifname[] = "eth0";
1157
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));
1161
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));
1164
1165     uint8_t numResources = 0;
1166     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1167     EXPECT_EQ(0, numResources);
1168
1169     OCResourceHandle handle0;
1170     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1171                                             "core.led",
1172                                             "core.rw",
1173                                             "/a/led0",
1174                                             0,
1175                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1176     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1177     EXPECT_EQ(1, numResources);
1178
1179     OCResourceHandle handle1;
1180     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1181                                             "core.led",
1182                                             "core.rw",
1183                                             "/a/led1",
1184                                             0,
1185                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1186     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1187     EXPECT_EQ(2, numResources);
1188
1189     OCResourceHandle handle2;
1190     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1191                                             "core.led",
1192                                             "core.rw",
1193                                             "/a/led2",
1194                                             0,
1195                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1196     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1197     EXPECT_EQ(3, numResources);
1198
1199     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
1200     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1201     EXPECT_EQ(2, numResources);
1202
1203     EXPECT_EQ(handle0, OCGetResourceHandle(0));
1204     EXPECT_EQ(handle2, OCGetResourceHandle(1));
1205
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);
1212
1213     EXPECT_EQ(OC_STACK_OK, OCStop());
1214 }
1215
1216 TEST(StackTest, GetResourceProperties) {
1217     uint8_t addr[20];
1218     uint16_t port = USE_RANDOM_PORT;
1219     uint8_t ifname[] = "eth0";
1220
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));
1224
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));
1227
1228     OCResourceHandle handle;
1229     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1230                                             "core.led",
1231                                             "core.rw",
1232                                             "/a/led",
1233                                             0,
1234                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1235
1236     EXPECT_EQ(OC_ACTIVE|OC_DISCOVERABLE|OC_OBSERVABLE, OCGetResourceProperties(handle));
1237     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1238
1239     EXPECT_EQ(OC_STACK_OK, OCStop());
1240 }
1241
1242 TEST(StackTest, StackTestResourceDiscoverOneResourceBad) {
1243     uint8_t addr[20];
1244     uint16_t port = USE_RANDOM_PORT;
1245     uint8_t ifname[] = "eth0";
1246
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));
1250
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));
1253
1254     OCResourceHandle handle;
1255     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1256                                             "core.led",
1257                                             "core.rw",
1258                                             "/a1/led",
1259                                             0,
1260                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1261     const char *url = OCGetResourceUri(handle);
1262     EXPECT_STREQ("/a1/led", url);
1263
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);
1269
1270     EXPECT_EQ(OC_STACK_OK, OCStop());
1271 }
1272
1273 TEST(StackTest, StackTestResourceDiscoverOneResource) {
1274     uint8_t addr[20];
1275     uint16_t port = USE_RANDOM_PORT;
1276     uint8_t ifname[] = "eth0";
1277
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));
1281
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));
1284
1285     OCResourceHandle handle;
1286     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1287                                             "core.led",
1288                                             "core.rw",
1289                                             "/a/led",
1290                                             0,
1291                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1292     const char *url = OCGetResourceUri(handle);
1293     EXPECT_STREQ("/a/led", url);
1294
1295     //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1296     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1297
1298     EXPECT_EQ(OC_STACK_OK, OCStop());
1299 }
1300
1301 TEST(StackTest, StackTestResourceDiscoverManyResources) {
1302     uint8_t addr[20];
1303     uint16_t port = USE_RANDOM_PORT;
1304     uint8_t ifname[] = "eth0";
1305
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));
1309
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));
1312
1313     OCResourceHandle handle1;
1314     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1315                                             "core.led",
1316                                             "core.rw",
1317                                             "/a/led1",
1318                                             0,
1319                                             OC_DISCOVERABLE));
1320     const char *url = OCGetResourceUri(handle1);
1321     EXPECT_STREQ("/a/led1", url);
1322
1323     OCResourceHandle handle2;
1324     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1325                                             "core.led",
1326                                             "core.rw",
1327                                             "/a/led2",
1328                                             0,
1329                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1330     url = OCGetResourceUri(handle2);
1331     EXPECT_STREQ("/a/led2", url);
1332
1333     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.brightled"));
1334     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.colorled"));
1335
1336     OCResourceHandle handle3;
1337     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
1338                                             "core.led",
1339                                             "core.rw",
1340                                             "/a/led3",
1341                                             0,
1342                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1343     url = OCGetResourceUri(handle3);
1344     EXPECT_STREQ("/a/led3", url);
1345
1346     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.ll"));
1347     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, "oc.mi.b"));
1348
1349     OCResourceHandle handle4;
1350     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
1351                                             "core.led",
1352                                             "core.rw",
1353                                             "/a/led4",
1354                                             0,
1355                                             OC_DISCOVERABLE));
1356     url = OCGetResourceUri(handle4);
1357     EXPECT_STREQ("/a/led4", url);
1358
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"));
1362
1363     //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1364
1365     EXPECT_EQ(OC_STACK_OK, OCStop());
1366 }
1367
1368
1369 #if 0
1370 TEST(StackTest, StackTestResourceDiscoverIfFilteringBad) {
1371     uint8_t addr[20];
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;
1379
1380     //EXPECT_EQ(OC_STACK_INVALID_QUERY, OCHandleServerRequest(&res, uri, query, req, rsp));
1381 }
1382
1383 TEST(StackTest, StackTestResourceDiscoverRtFilteringBad) {
1384     uint8_t addr[20];
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;
1392
1393     //EXPECT_EQ(OC_STACK_INVALID_QUERY, OCHandleServerRequest(&res, uri, query, req, rsp));
1394 }
1395 TEST(StackTest, StackTestResourceDiscoverIfFiltering) {
1396     uint8_t addr[20];
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;
1404
1405     //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1406 }
1407
1408 TEST(StackTest, StackTestResourceDiscoverRtFiltering) {
1409     uint8_t addr[20];
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;
1417
1418     //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1419 }
1420 #endif