IOT-2258: Allow secure and nonsecure endpoints with OCCreateResource.
[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 {
24     #include "ocpayload.h"
25     #include "ocstack.h"
26     #include "ocstackinternal.h"
27     #include "logger.h"
28     #include "oic_malloc.h"
29     #include "oic_string.h"
30     #include "oic_time.h"
31     #include "ocresourcehandler.h"
32 }
33
34 #include "gtest/gtest.h"
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #include <stdlib.h>
43
44 //-----------------------------------------------------------------------------
45 // Includes
46 //-----------------------------------------------------------------------------
47 #include <stdio.h>
48 #include <string.h>
49
50 #include <iostream>
51 #include <stdint.h>
52
53 #include "gtest_helper.h"
54
55 using namespace std;
56
57 namespace itst = iotivity::test;
58
59 #define DEFAULT_CONTEXT_VALUE 0x99
60 #define INVALID_TPS_FLAGS (250)
61 #define INVALID_TPS_FLAGS_ZERO (0)
62
63 //-----------------------------------------------------------------------------
64 // Private variables
65 //-----------------------------------------------------------------------------
66 #define TAG "TestHarness"
67
68 char gDeviceUUID[] = "fe3f9a68-4931-4cb0-9ea4-81702b43116c";
69 char gDevicePIID[] = "32684bf3-4c44-47b0-99fe-6a9a59b73a8d";
70 char gManufacturerName[] = "myName";
71 static OCPrm_t pmSel;
72 static char pinNumber;
73 static OCDPDev_t peer;
74
75 std::chrono::seconds const SHORT_TEST_TIMEOUT = std::chrono::seconds(5);
76 std::chrono::seconds const LONG_TEST_TIMEOUT = std::chrono::seconds(450);
77
78 //-----------------------------------------------------------------------------
79 // Callback functions
80 //-----------------------------------------------------------------------------
81 extern "C"  OCStackApplicationResult asyncDoResourcesCallback(void* ctx,
82         OCDoHandle /*handle*/, OCClientResponse * clientResponse)
83 {
84     OIC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
85
86     EXPECT_EQ(OC_STACK_OK, clientResponse->result);
87
88     if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
89         OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
90     }
91     OIC_LOG_V(INFO, TAG, "result = %d", clientResponse->result);
92
93     return OC_STACK_KEEP_TRANSACTION;
94 }
95
96 static void resultCallback(void *UNUSED1, OCDPDev_t *UNUSED2, OCStackResult UNUSED3)
97 {
98     (void) (UNUSED1);
99     (void) (UNUSED2);
100     (void) (UNUSED3);
101 }
102
103 extern "C" OCStackApplicationResult discoveryCallback(void* ctx,
104         OCDoHandle /*handle*/, OCClientResponse * clientResponse)
105 {
106     OIC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
107
108     EXPECT_EQ(OC_STACK_OK, clientResponse->result);
109
110     if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
111     {
112         OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
113     }
114
115     OIC_LOG_V(INFO, TAG, "result = %d", clientResponse->result);
116
117     OCDiscoveryPayload *discoveryPayload = ((OCDiscoveryPayload *) clientResponse->payload);
118     EXPECT_TRUE(discoveryPayload != NULL);
119     OCResourcePayload *res = discoveryPayload->resources;
120     int count = 0;
121     for (OCResourcePayload *res1 = discoveryPayload->resources; res1; res1 = res1->next)
122     {
123         count++;
124     }
125     EXPECT_EQ(3, count);
126     EXPECT_EQ("/a/led1", res->uri);
127     res = res->next;
128     EXPECT_EQ("/a/led2", res->uri);
129     res = res->next;
130     EXPECT_EQ("/a/led3", res->uri);
131     res = res->next;
132     EXPECT_TRUE(res == NULL);
133
134     return OC_STACK_KEEP_TRANSACTION;
135 }
136
137 //-----------------------------------------------------------------------------
138 // Entity handler
139 //-----------------------------------------------------------------------------
140 OCEntityHandlerResult entityHandler(OCEntityHandlerFlag /*flag*/,
141         OCEntityHandlerRequest * /*entityHandlerRequest*/,
142         void* /*callbackParam*/)
143 {
144     OIC_LOG(INFO, TAG, "Entering entityHandler");
145
146     return OC_EH_OK;
147 }
148
149 //-----------------------------------------------------------------------------
150 //  Local functions
151 //-----------------------------------------------------------------------------
152 void InitStack(OCMode mode)
153 {
154     OIC_LOG(INFO, TAG, "Entering InitStack");
155
156     EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, mode));
157     OIC_LOG(INFO, TAG, "Leaving InitStack");
158 }
159
160 uint8_t InitNumExpectedResources()
161 {
162 #ifdef WITH_PRESENCE
163     // When presence is enabled, it is a resource and so is (currently) included
164     // in the returned resource count returned by the OCGetNumberOfResources API.
165     return 1;
166 #else
167     return 0;
168 #endif
169 }
170
171 uint8_t InitResourceIndex()
172 {
173 #ifdef WITH_PRESENCE
174     // When presence is enabled, it is a resource and so is (currently) included
175     // in the returned resource count returned by the OCGetNumberOfResources API.
176     // The index of the presence resource is 0, so the first user app resource index
177     // is 1.
178     return 1;
179 #else
180     return 0;
181 #endif
182 }
183
184 extern "C" uint32_t g_ocStackStartCount;
185
186 OCDeviceProperties* getTestDeviceProps()
187 {
188     OCDeviceProperties* deviceProps = (OCDeviceProperties*)OICCalloc(1, sizeof(OCDeviceProperties));
189     if (NULL != deviceProps)
190     {
191         OICStrcpy(deviceProps->protocolIndependentId, UUID_STRING_SIZE, gDevicePIID);
192     }
193
194     return deviceProps;
195 }
196
197 class OCDiscoverTests : public testing::Test
198 {
199     protected:
200         virtual void SetUp()
201         {
202             EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
203         }
204
205         virtual void TearDown()
206         {
207             OCStop();
208         }
209 };
210
211 class OCDevicePropertiesTests : public testing::Test
212 {
213 protected:
214     virtual void SetUp()
215     {
216         itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
217     }
218 };
219 //-----------------------------------------------------------------------------
220 //  Tests
221 //-----------------------------------------------------------------------------
222
223 TEST(StackInit, StackInitNullAddr)
224 {
225     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
226     EXPECT_EQ(OC_STACK_OK, OCInit(0, 5683, OC_SERVER));
227     EXPECT_EQ(1u, g_ocStackStartCount);
228     EXPECT_EQ(OC_STACK_OK, OCStop());
229     EXPECT_EQ(0u, g_ocStackStartCount);
230 }
231
232 TEST(StackInit, StackInitNullPort)
233 {
234     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
235     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 0, OC_SERVER));
236     EXPECT_EQ(OC_STACK_OK, OCStop());
237 }
238
239 TEST(StackInit, StackInitNullAddrAndPort)
240 {
241     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
242     EXPECT_EQ(OC_STACK_OK, OCInit(0, 0, OC_SERVER));
243     EXPECT_EQ(OC_STACK_OK, OCStop());
244 }
245
246 TEST(StackInit, StackInitInvalidMode)
247 {
248     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
249     EXPECT_EQ(OC_STACK_ERROR, OCInit(0, 0, (OCMode)10));
250     EXPECT_EQ(0u, g_ocStackStartCount);
251 }
252
253 TEST(StackStart, StackStartSuccessClient)
254 {
255     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
256     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
257     EXPECT_EQ(OC_STACK_OK, OCStop());
258 }
259
260 TEST(StackStart, StackStartSuccessServer)
261 {
262     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
263     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
264     EXPECT_EQ(OC_STACK_OK, OCStop());
265 }
266
267 TEST(StackStart, StackStartSuccessClientServer)
268 {
269     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
270     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
271     EXPECT_EQ(OC_STACK_OK, OCStop());
272 }
273
274 TEST(StackStart, StackStartSuccessServerThenClient)
275 {
276     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
277     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
278     EXPECT_EQ(OC_STACK_OK, OCStop());
279     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
280     EXPECT_EQ(OC_STACK_OK, OCStop());
281 }
282
283 TEST(StackStart, StackStartSuccessClientThenServer)
284 {
285     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
286     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
287     EXPECT_EQ(OC_STACK_OK, OCStop());
288     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
289     EXPECT_EQ(OC_STACK_OK, OCStop());
290 }
291
292 TEST(StackStart, StackStartSuccessiveInits)
293 {
294     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
295     EXPECT_EQ(0u, g_ocStackStartCount);
296     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
297     EXPECT_EQ(1u, g_ocStackStartCount);
298     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.2", 5683, OC_SERVER));
299     EXPECT_EQ(2u, g_ocStackStartCount);
300     EXPECT_EQ(OC_STACK_OK, OCStop());
301     EXPECT_EQ(1u, g_ocStackStartCount);
302     EXPECT_EQ(OC_STACK_OK, OCStop());
303     EXPECT_EQ(0u, g_ocStackStartCount);
304 }
305
306 TEST(StackStart, SetPlatformInfoValid)
307 {
308     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
309     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
310
311     OCPlatformInfo info =
312     {
313         gDeviceUUID,
314         gManufacturerName,
315         0, 0, 0, 0, 0, 0, 0, 0, 0
316     };
317     EXPECT_EQ(OC_STACK_OK, OCSetPlatformInfo(info));
318     EXPECT_EQ(OC_STACK_OK, OCStop());
319 }
320
321 TEST(StackStart, SetPlatformInfoWithClientMode)
322 {
323     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
324     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
325
326     OCPlatformInfo info =
327     {
328         gDeviceUUID,
329         gManufacturerName,
330         0, 0, 0, 0, 0, 0, 0, 0, 0
331     };
332     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info));
333     EXPECT_EQ(OC_STACK_OK, OCStop());
334 }
335
336 TEST(StackStart, SetPlatformInfoWithNoPlatformID)
337 {
338     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
339     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
340
341     OCPlatformInfo info =
342      {
343          0,
344          gDeviceUUID,
345          0, 0, 0, 0, 0, 0, 0, 0, 0
346      };
347
348     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info));
349     EXPECT_EQ(OC_STACK_OK, OCStop());
350 }
351
352 TEST(StackStart, SetPlatformInfoWithBadPlatformID)
353 {
354     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
355     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
356
357     char invalidId[] = "myDeviceUUID";
358     OCPlatformInfo info =
359      {
360         invalidId,
361         gManufacturerName,
362         0, 0, 0, 0, 0, 0, 0, 0, 0
363      };
364
365     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info));
366     EXPECT_EQ(OC_STACK_OK, OCStop());
367 }
368
369 TEST(StackStart, SetPlatformInfoWithNoManufacturerName)
370 {
371     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
372     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
373
374     OCPlatformInfo info =
375     {
376         gDeviceUUID,
377         0, 0, 0, 0, 0, 0, 0, 0, 0, 0
378     };
379
380     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info));
381     EXPECT_EQ(OC_STACK_OK, OCStop());
382 }
383
384 TEST(StackStart, SetPlatformInfoWithZeroLengthManufacturerName)
385 {
386     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
387     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
388
389     OCPlatformInfo info =
390     {
391         gDeviceUUID,
392         0, 0, 0, 0, 0, 0, 0, 0, 0, 0
393     };
394     info.manufacturerName = (char *) "";
395
396     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info));
397     EXPECT_EQ(OC_STACK_OK, OCStop());
398 }
399
400 TEST(StackStart, SetPlatformInfoWithTooLongManufacName)
401 {
402     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
403     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
404     char gTooLongManufacturerName[MAX_PLATFORM_NAME_LENGTH+2];
405     for (int i = 0; i <= MAX_PLATFORM_NAME_LENGTH; i++ )
406     {
407         gTooLongManufacturerName[i] = 'a';
408     }
409     gTooLongManufacturerName[MAX_PLATFORM_NAME_LENGTH+1] = '\0';
410     OCPlatformInfo info =
411     {
412         gDeviceUUID,
413         gTooLongManufacturerName,
414         0, 0, 0, 0, 0, 0, 0, 0, 0
415     };
416
417     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info));
418     EXPECT_EQ(OC_STACK_OK, OCStop());
419 }
420
421 TEST(StackStart, SetPlatformInfoWithTooLongManufacURL)
422 {
423     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
424     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
425     char gManufacturerUrl[MAX_PLATFORM_URL_LENGTH+2];
426     for (int i = 0; i <= MAX_PLATFORM_URL_LENGTH; i++ )
427     {
428         gManufacturerUrl[i] = 'a';
429     }
430     gManufacturerUrl[MAX_PLATFORM_URL_LENGTH+1] = '\0';
431     OCPlatformInfo info =
432     {
433         gDeviceUUID,
434         gManufacturerName,
435         gManufacturerUrl,
436         0, 0, 0, 0, 0, 0, 0, 0
437     };
438
439     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info));
440     EXPECT_EQ(OC_STACK_OK, OCStop());
441 }
442
443 TEST(StackStart, SetPlatformInfoWithOCSetPropertyValueAPI)
444 {
445     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
446     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
447     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, gDeviceUUID));
448     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, gManufacturerName));
449     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, "http://www.iotivity.org"));
450     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, "S777"));
451     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, "15 Nov, 2016"));
452     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, "1.1"));
453     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, "14"));
454     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, "0.1"));
455     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, "0.1"));
456     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, "http://www.iotivity.org"));
457     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, ""));
458     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, "x.org.iotivity.AAAA", "value"));
459     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, NULL, ""));
460     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPropertyValue(PAYLOAD_TYPE_INVALID, NULL, NULL));
461     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPropertyValue(PAYLOAD_TYPE_INVALID, NULL, NULL));
462     EXPECT_EQ(OC_STACK_OK, OCStop());
463 }
464
465 TEST(StackStart, GetPlatformInfoWithOCGetPropertyValueAPI)
466 {
467     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
468     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
469     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, gDeviceUUID));
470     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, gManufacturerName));
471     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, "http://www.iotivity.org"));
472     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, "S777"));
473     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, "15 Nov, 2016"));
474     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, "1.1"));
475     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, "14"));
476     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, "0.1"));
477     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, "0.1"));
478     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, "http://www.iotivity.org"));
479     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, ""));
480     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, "x.org.iotivity.AAAA", "value"));
481
482     void *value = NULL;
483     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, &value));
484     ASSERT_TRUE(value != NULL);
485     EXPECT_STREQ(gDeviceUUID, (char *)value);
486     OICFree(value);
487     value = NULL;
488
489     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, &value));
490     ASSERT_TRUE(value != NULL);
491     EXPECT_STREQ(gManufacturerName, (char *)value);
492     OICFree(value);
493     value = NULL;
494
495     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, &value));
496     ASSERT_TRUE(value != NULL);
497     EXPECT_STREQ("http://www.iotivity.org", (char *)value);
498     OICFree(value);
499     value = NULL;
500
501     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, &value));
502     ASSERT_TRUE(value != NULL);
503     EXPECT_STREQ("S777", (char *)value);
504     OICFree(value);
505     value = NULL;
506
507     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, &value));
508     ASSERT_TRUE(value != NULL);
509     EXPECT_STREQ("15 Nov, 2016", (char *)value);
510     OICFree(value);
511     value = NULL;
512
513     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, &value));
514     ASSERT_TRUE(value != NULL);
515     EXPECT_STREQ("1.1", (char *)value);
516     OICFree(value);
517     value = NULL;
518
519     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, &value));
520     ASSERT_TRUE(value != NULL);
521     EXPECT_STREQ("14", (char *)value);
522     OICFree(value);
523     value = NULL;
524
525     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, &value));
526     ASSERT_TRUE(value != NULL);
527     EXPECT_STREQ("0.1", (char *)value);
528     OICFree(value);
529     value = NULL;
530
531     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, &value));
532     ASSERT_TRUE(value != NULL);
533     EXPECT_STREQ("0.1", (char *)value);
534     OICFree(value);
535     value = NULL;
536
537     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, &value));
538     ASSERT_TRUE(value != NULL);
539     EXPECT_STREQ("http://www.iotivity.org", (char *)value);
540     OICFree(value);
541     value = NULL;
542
543     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, &value));
544     ASSERT_TRUE(value != NULL);
545     EXPECT_STREQ("", (char *)value);
546     OICFree(value);
547     value = NULL;
548
549     EXPECT_EQ(OC_STACK_OK,OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, "x.org.iotivity.AAAA", &value));
550     ASSERT_TRUE(value != NULL);
551     EXPECT_STREQ("value", (char *)value);
552     OICFree(value);
553
554     EXPECT_EQ(OC_STACK_OK, OCStop());
555 }
556
557 TEST(StackStart, SetDeviceInfoAPI)
558 {
559     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
560     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
561     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, "Sample"));
562     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, "specVersion"));
563     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, "x.org.iotivity.newproperty", "value"));
564     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
565         "Data Model Version"));
566     OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
567     EXPECT_TRUE(handle != NULL);
568     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "oic.wk.tv"));
569     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPropertyValue(PAYLOAD_TYPE_INVALID, NULL, NULL));
570     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPropertyValue(PAYLOAD_TYPE_INVALID, "", NULL));
571     EXPECT_EQ(OC_STACK_OK, OCStop());
572 }
573
574 TEST(StackStart, GetDeviceInfoAPI)
575 {
576     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
577     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
578     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, "Sample"));
579     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, "specVersion"));
580     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, "x.org.iotivity.newproperty", "value"));
581     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
582         "Data Model Version"));
583     OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
584     EXPECT_TRUE(handle != NULL);
585     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "oic.wk.tv"));
586
587     void *value = NULL;
588     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, &value));
589     ASSERT_TRUE(value != NULL);
590     EXPECT_STREQ("Sample", (char *)value);
591     OICFree(value);
592     value = NULL;
593
594     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, &value));
595     ASSERT_TRUE(value != NULL);
596     EXPECT_STREQ("specVersion", (char *)value);
597     OICFree(value);
598     value = NULL;
599
600     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, "x.org.iotivity.newproperty", &value));
601     ASSERT_TRUE(value != NULL);
602     EXPECT_STREQ("value", (char *)value);
603     OICFree(value);
604     value = NULL;
605
606     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, &value));
607     ASSERT_TRUE(value != NULL);
608     ASSERT_TRUE(((OCStringLL *)value)->value != NULL);
609     EXPECT_STREQ("Data Model Version", ((OCStringLL *)value)->value);
610     OCFreeOCStringLL((OCStringLL *) value);
611     value = NULL;
612
613     EXPECT_STREQ("oic.wk.d", OCGetResourceTypeName(handle, 0));
614     EXPECT_STREQ("oic.wk.tv", OCGetResourceTypeName(handle, 1));
615
616     EXPECT_EQ(OC_STACK_OK, OCStop());
617 }
618
619 TEST(StackStart, SetGetDevicePropertyValues)
620 {
621     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
622     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
623     OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
624     EXPECT_TRUE(handle != NULL);
625     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "oic.wk.tv"));
626     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "oic.if.tv"));
627
628     void *value = NULL;
629     OCStringLL *x, *y;
630
631     OCStringLL *rts = NULL;
632     OCResourcePayloadAddStringLL(&rts, "oic.wk.d");
633     OCResourcePayloadAddStringLL(&rts, "oic.wk.tv");
634     EXPECT_NE(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_RESOURCE_TYPE, &value));
635     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_RESOURCE_TYPE, &value));
636     for (x = rts, y = (OCStringLL *) value; x && y; x = x->next, y = y->next)
637     {
638         EXPECT_STREQ(x->value, y->value);
639     }
640     EXPECT_TRUE(!x && !y);
641     OCFreeOCStringLL((OCStringLL *) value);
642     value = NULL;
643     OCFreeOCStringLL(rts);
644
645     OCStringLL *itfs = NULL;
646     OCResourcePayloadAddStringLL(&itfs, "oic.if.baseline");
647     OCResourcePayloadAddStringLL(&itfs, "oic.if.r");
648     OCResourcePayloadAddStringLL(&itfs, "oic.if.tv");
649     EXPECT_NE(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_INTERFACE, &value));
650     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_INTERFACE, &value));
651     for (x = itfs, y = (OCStringLL *) value; x && y; x = x->next, y = y->next)
652     {
653         EXPECT_STREQ(x->value, y->value);
654     }
655     EXPECT_TRUE(!x && !y);
656     OCFreeOCStringLL((OCStringLL *) value);
657     value = NULL;
658     OCFreeOCStringLL(itfs);
659
660     const char *n = "name";
661     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, n));
662     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, &value));
663     EXPECT_STREQ(n, (char *) value);
664     OICFree(value);
665     value = NULL;
666
667     const char *id = "instance-identifier";
668     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_INSTANCE_ID, id));
669     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_INSTANCE_ID, &value));
670     EXPECT_STREQ(id, (char *) value);
671     OICFree(value);
672     value = NULL;
673
674     EXPECT_NE(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_ID, &value));
675     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_ID, &value));
676     EXPECT_STREQ(OCGetServerInstanceIDString(), (char *) value);
677     OICFree(value);
678     value = NULL;
679
680     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, OC_SPEC_VERSION));
681     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, &value));
682     EXPECT_STREQ(OC_SPEC_VERSION, (char *) value);
683     OICFree(value);
684     value = NULL;
685
686     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, OC_DATA_MODEL_VERSION));
687     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, &value));
688     char *dmv = OCCreateString((OCStringLL *) value);
689     EXPECT_STREQ(OC_DATA_MODEL_VERSION, dmv);
690     OICFree(dmv);
691     OCFreeOCStringLL((OCStringLL *) value);
692     value = NULL;
693
694     OCStringLL *ld = NULL;
695     OCResourcePayloadAddStringLL(&ld, "en");
696     OCResourcePayloadAddStringLL(&ld, "Description");
697     OCResourcePayloadAddStringLL(&ld, "de");
698     OCResourcePayloadAddStringLL(&ld, "Beschriebung");
699     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_DESCRIPTION, ld));
700     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_DESCRIPTION, &value));
701     for (x = ld, y = (OCStringLL *) value; x && y; x = x->next, y = y->next)
702     {
703         EXPECT_STREQ(x->value, y->value);
704     }
705     EXPECT_TRUE(!x && !y);
706     OCFreeOCStringLL((OCStringLL *) value);
707     value = NULL;
708     OCFreeOCStringLL(ld);
709
710     const char *sv = "software-version";
711     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SOFTWARE_VERSION, sv));
712     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SOFTWARE_VERSION, &value));
713     EXPECT_STREQ(sv, (char *) value);
714     OICFree(value);
715     value = NULL;
716
717     OCStringLL *dmn = NULL;
718     OCResourcePayloadAddStringLL(&dmn, "en");
719     OCResourcePayloadAddStringLL(&dmn, "Manufacturer");
720     OCResourcePayloadAddStringLL(&dmn, "de");
721     OCResourcePayloadAddStringLL(&dmn, "Hersteller");
722     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_MFG_NAME, dmn));
723     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_MFG_NAME, &value));
724     for (x = dmn, y = (OCStringLL *) value; x && y; x = x->next, y = y->next)
725     {
726         EXPECT_STREQ(x->value, y->value);
727     }
728     EXPECT_TRUE(!x && !y);
729     OCFreeOCStringLL((OCStringLL *) value);
730     value = NULL;
731     OCFreeOCStringLL(dmn);
732
733     const char *dmno = "device-model-number";
734     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_MODEL_NUM, dmno));
735     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_MODEL_NUM, &value));
736     EXPECT_STREQ(dmno, (char *) value);
737     OICFree(value);
738     value = NULL;
739
740     const char *piid = "protocol-independent-identifier";
741     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, piid));
742     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, &value));
743     EXPECT_STREQ(piid, (char *) value);
744     OICFree(value);
745     value = NULL;
746
747     EXPECT_EQ(OC_STACK_OK, OCStop());
748 }
749
750 TEST(StackDiscovery, DISABLED_DoResourceDeviceDiscovery)
751 {
752     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
753     OCCallbackData cbData;
754     OCDoHandle handle;
755
756     OIC_LOG(INFO, TAG, "Starting DoResourceDeviceDiscovery test ");
757     InitStack(OC_CLIENT);
758
759     /* Start a discovery query*/
760     char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
761     strcpy(szQueryUri, OC_RSRVD_WELL_KNOWN_URI);
762     cbData.cb = asyncDoResourcesCallback;
763     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
764     cbData.cd = NULL;
765     EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle,
766                                         OC_REST_GET,
767                                         szQueryUri,
768                                         0,
769                                         0,
770                                         CT_ADAPTER_IP,
771                                         OC_LOW_QOS,
772                                         &cbData,
773                                         NULL,
774                                         0));
775     EXPECT_EQ(OC_STACK_OK, OCStop());
776 }
777
778 TEST(StackResource, DISABLED_UpdateResourceNullURI)
779 {
780     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
781     OCCallbackData cbData;
782     OCDoHandle handle;
783
784     OIC_LOG(INFO, TAG, "Starting UpdateResourceNullURI test");
785     InitStack(OC_CLIENT);
786
787     /* Start a discovery query*/
788     char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
789     strcpy(szQueryUri, OC_RSRVD_WELL_KNOWN_URI);
790     cbData.cb = asyncDoResourcesCallback;
791     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
792     cbData.cd = NULL;
793     EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle,
794                                         OC_REST_GET,
795                                         szQueryUri,
796                                         0,
797                                         0,
798                                         CT_ADAPTER_IP,
799                                         OC_LOW_QOS,
800                                         &cbData,
801                                         NULL,
802                                         0));
803     EXPECT_EQ(OC_STACK_OK, OCStop());
804 }
805
806 TEST(StackResource, CreateResourceBadParams)
807 {
808     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
809     OIC_LOG(INFO, TAG, "Starting CreateResourceBadParams test");
810     InitStack(OC_SERVER);
811
812     OCResourceHandle handle;
813
814     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(NULL, //&handle,
815                                             "core.led",
816                                             "core.rw",
817                                             "/a/led",
818                                             0,
819                                             NULL,
820                                             OC_DISCOVERABLE|OC_OBSERVABLE));
821
822     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
823                                             NULL, //"core.led",
824                                             "core.rw",
825                                             "/a/led",
826                                             0,
827                                             NULL,
828                                             OC_DISCOVERABLE|OC_OBSERVABLE));
829
830     // Property bitmask out of range
831     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
832                                             "core.led",
833                                             "core.rw",
834                                             "/a/led",
835                                             0,
836                                             NULL,
837                                             255));// invalid bitmask for OCResourceProperty
838
839     EXPECT_EQ(OC_STACK_OK, OCStop());
840 }
841
842 TEST(StackResource, CreateResourceBadUri)
843 {
844     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
845     OIC_LOG(INFO, TAG, "Starting CreateResourceBadUri test");
846     InitStack(OC_SERVER);
847
848     const char *uri257 = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW";
849
850     OCResourceHandle handle;
851
852     EXPECT_EQ(OC_STACK_INVALID_URI, OCCreateResource(&handle,
853                                             "core.led",
854                                             "core.rw",
855                                             NULL, //"/a/led",
856                                             0,
857                                             0,
858                                             OC_DISCOVERABLE|OC_OBSERVABLE));
859
860     EXPECT_EQ(OC_STACK_INVALID_URI, OCCreateResource(&handle,
861                                             "core.led",
862                                             "core.rw",
863                                             "", //"/a/led",
864                                             0,
865                                             0,
866                                             OC_DISCOVERABLE|OC_OBSERVABLE));
867
868     EXPECT_EQ(OC_STACK_INVALID_URI, OCCreateResource(&handle,
869                                             "core.led",
870                                             "core.rw",
871                                             uri257, //"/a/led",
872                                             0,
873                                             0,
874                                             OC_DISCOVERABLE|OC_OBSERVABLE));
875
876     EXPECT_EQ(OC_STACK_OK, OCStop());
877 }
878
879 TEST(StackResource, CreateResourceSuccess)
880 {
881     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
882     OIC_LOG(INFO, TAG, "Starting CreateResourceSuccess test");
883     InitStack(OC_SERVER);
884
885     OCResourceHandle handle;
886     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
887                                             "core.led",
888                                             "core.rw",
889                                             "/a/led",
890                                             0,
891                                             NULL,
892                                             OC_DISCOVERABLE|OC_OBSERVABLE));
893     const char *url = OCGetResourceUri(handle);
894     EXPECT_STREQ("/a/led", url);
895
896     EXPECT_EQ(OC_STACK_OK, OCStop());
897 }
898
899 TEST(StackResource, CreateResourceSuccessWithResourcePolicyPropNone)
900 {
901     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
902     OIC_LOG(INFO, TAG, "Starting CreateResourceSuccessWithResourcePolicyPropNone test");
903     InitStack(OC_SERVER);
904
905     OCResourceHandle handle;
906     // the resource is non-discoverable & non-observable by the client.
907     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
908                                             "core.led",
909                                             "core.rw",
910                                             "/a/led",
911                                             0,
912                                             NULL,
913                                             OC_RES_PROP_NONE));// the resource is non-discoverable &
914                                                 // non-observable by the client.
915     const char* url = OCGetResourceUri(handle);
916     EXPECT_STREQ("/a/led", url);
917
918     EXPECT_EQ(OC_STACK_OK, OCStop());
919 }
920
921 TEST(StackResource, CreateResourceWithClientStackMode)
922 {
923     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
924     OIC_LOG(INFO, TAG, "Starting CreateResourceSuccess test");
925     InitStack(OC_CLIENT);
926
927     OCResourceHandle handle;
928     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
929                                             "core.led",
930                                             "core.rw",
931                                             "/a/led",
932                                             0,
933                                             NULL,
934                                             OC_DISCOVERABLE|OC_OBSERVABLE));
935
936     EXPECT_EQ(OC_STACK_OK, OCStop());
937 }
938
939 TEST(StackResource, CreateResourceFailDuplicateUri)
940 {
941     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
942     OIC_LOG(INFO, TAG, "Starting CreateResourceFailDuplicateUri test");
943     InitStack(OC_SERVER);
944
945     OCResourceHandle handle;
946     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
947                                             "core.led",
948                                             "core.rw",
949                                             "/a/led",
950                                             0,
951                                             NULL,
952                                             OC_DISCOVERABLE|OC_OBSERVABLE));
953     const char *url = OCGetResourceUri(handle);
954     EXPECT_STREQ("/a/led", url);
955
956     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
957                                             "core.led",
958                                             "core.rw",
959                                             "/a/led",
960                                             0,
961                                             NULL,
962                                             OC_DISCOVERABLE|OC_OBSERVABLE));
963
964     EXPECT_EQ(OC_STACK_OK, OCStop());
965 }
966
967 TEST(StackResource, CreateResourceMultipleResources)
968 {
969     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
970     OIC_LOG(INFO, TAG, "Starting CreateResourceMultipleResources test");
971     InitStack(OC_SERVER);
972
973     OCResourceHandle handle1;
974     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
975                                             "core.led",
976                                             "core.rw",
977                                             "/a/led1",
978                                             0,
979                                             NULL,
980                                             OC_DISCOVERABLE|OC_OBSERVABLE));
981
982     OCResourceHandle handle2;
983     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
984                                             "core.led",
985                                             "core.rw",
986                                             "/a/led2",
987                                             0,
988                                             NULL,
989                                             OC_DISCOVERABLE|OC_OBSERVABLE));
990     OCResourceHandle handle3;
991     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
992                                             "core.led",
993                                             "core.rw",
994                                             "/a/led3",
995                                             0,
996                                             NULL,
997                                             OC_DISCOVERABLE|OC_OBSERVABLE));
998
999     const char *url = OCGetResourceUri(handle1);
1000     EXPECT_STREQ("/a/led1", url);
1001
1002     url = OCGetResourceUri(handle2);
1003     EXPECT_STREQ("/a/led2", url);
1004
1005     url = OCGetResourceUri(handle3);
1006     EXPECT_STREQ("/a/led3", url);
1007
1008     EXPECT_EQ(OC_STACK_OK, OCStop());
1009 }
1010
1011 TEST(StackResource, CreateResourceBadResoureType)
1012 {
1013     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1014     OIC_LOG(INFO, TAG, "Starting CreateResourceBadResoureType test");
1015     InitStack(OC_SERVER);
1016
1017     OCResourceHandle handle;
1018     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle,
1019                                             NULL, //"core.led",
1020                                             "core.rw",
1021                                             "/a/led",
1022                                             0,
1023                                             NULL,
1024                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1025
1026     OCResourceHandle handle2;
1027     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResource(&handle2,
1028                                             "",
1029                                             "core.rw",
1030                                             "/a/led",
1031                                             0,
1032                                             NULL,
1033                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1034
1035     EXPECT_EQ(OC_STACK_OK, OCStop());
1036 }
1037
1038 TEST(StackResource, CreateResourceGoodResourceType)
1039 {
1040     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1041     OIC_LOG(INFO, TAG, "Starting CreateResourceGoodResourceType test");
1042     InitStack(OC_SERVER);
1043
1044     OCResourceHandle handle;
1045     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1046                                             "core.led",
1047                                             "core.rw",
1048                                             "/a/led",
1049                                             0,
1050                                             NULL,
1051                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1052
1053     EXPECT_EQ(OC_STACK_OK, OCStop());
1054 }
1055
1056 TEST(StackResource, CreateResourceWithBadEndpointsFlags)
1057 {
1058     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1059
1060     OIC_LOG(INFO, TAG, "CreateResourceWithEndpointsFlags test");
1061
1062     InitStack(OC_SERVER);
1063
1064     OCResourceHandle handle;
1065     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCCreateResourceWithEp(&handle,
1066                                             "core.led",
1067                                             "core.rw",
1068                                             "/a/led",
1069                                             0,
1070                                             NULL,
1071                                             OC_DISCOVERABLE|OC_OBSERVABLE,
1072                                             (OCTpsSchemeFlags)INVALID_TPS_FLAGS));
1073
1074     EXPECT_EQ(OC_STACK_OK, OCStop());
1075 }
1076
1077 TEST(StackResource, CreateResourceWithGoodEndpointsFlags)
1078 {
1079     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1080
1081     OIC_LOG(INFO, TAG, "CreateResourceWithEndpointsFlags test");
1082
1083     InitStack(OC_SERVER);
1084
1085     OCResourceHandle handle;
1086     EXPECT_EQ(OC_STACK_OK, OCCreateResourceWithEp(&handle,
1087                                             "core.led",
1088                                             "core.rw",
1089                                             "/a/led",
1090                                             0,
1091                                             NULL,
1092                                             OC_DISCOVERABLE|OC_OBSERVABLE,
1093                                             OC_ALL));
1094     OCResourceHandle handle2;
1095     EXPECT_EQ(OC_STACK_OK, OCCreateResourceWithEp(&handle2,
1096                                             "core.led",
1097                                             "core.rw",
1098                                             "/a/led2",
1099                                             0,
1100                                             NULL,
1101                                             OC_DISCOVERABLE|OC_OBSERVABLE,
1102                                             OC_COAP));
1103 #ifdef TCP_ADAPTER
1104     OCResourceHandle handle3;
1105     EXPECT_EQ(OC_STACK_OK, OCCreateResourceWithEp(&handle3,
1106                                             "core.led",
1107                                             "core.rw",
1108                                             "/a/led3",
1109                                             0,
1110                                             NULL,
1111                                             OC_DISCOVERABLE|OC_OBSERVABLE,
1112                                             OC_COAP_TCP));
1113
1114     OCResourceHandle handle4;
1115     EXPECT_EQ(OC_STACK_OK, OCCreateResourceWithEp(&handle4,
1116                                             "core.led",
1117                                             "core.rw",
1118                                             "/a/led4",
1119                                             0,
1120                                             NULL,
1121                                             OC_DISCOVERABLE|OC_OBSERVABLE,
1122                                             (OCTpsSchemeFlags)(OC_COAP | OC_COAP_TCP)));
1123 #endif
1124
1125     EXPECT_EQ(OC_STACK_OK, OCStop());
1126 }
1127
1128 TEST(StackResource, ResourceTypeName)
1129 {
1130     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1131     OIC_LOG(INFO, TAG, "Starting ResourceTypeName test");
1132     InitStack(OC_SERVER);
1133
1134     OCResourceHandle handle;
1135     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1136                                             "core.led",
1137                                             "core.rw",
1138                                             "/a/led",
1139                                             0,
1140                                             NULL,
1141                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1142
1143     uint8_t numResourceTypes;
1144     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1145     EXPECT_EQ(1, numResourceTypes);
1146     const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
1147     EXPECT_STREQ("core.led", resourceTypeName);
1148
1149     // try getting resource type names with an invalid index
1150     resourceTypeName = OCGetResourceTypeName(handle, 1);
1151     EXPECT_STREQ(NULL, resourceTypeName);
1152     // try getting resource type names with an invalid index
1153     resourceTypeName = OCGetResourceTypeName(handle, 10);
1154     EXPECT_STREQ(NULL, resourceTypeName);
1155
1156     EXPECT_EQ(OC_STACK_OK, OCStop());
1157 }
1158
1159 TEST(StackResource, ResourceTypeAttrRepresentation)
1160 {
1161     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1162     OIC_LOG(INFO, TAG, "Starting ResourceTypeAttrRepresentation test");
1163     InitStack(OC_SERVER);
1164
1165     OCResourceHandle handle;
1166     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1167                                             "core.led",
1168                                             "core.rw",
1169                                             "/a/led",
1170                                             0,
1171                                             NULL,
1172                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1173
1174     uint8_t numResourceTypes;
1175     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1176     EXPECT_EQ(1, numResourceTypes);
1177
1178     EXPECT_EQ(OC_STACK_OK, OCStop());
1179 }
1180
1181 TEST(StackResource, ResourceTypeInterface)
1182 {
1183     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1184     OIC_LOG(INFO, TAG, "Starting ResourceTypeInterface test");
1185     InitStack(OC_SERVER);
1186
1187     OCResourceHandle handle;
1188     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1189                                             "core.led",
1190                                             "core.rw",
1191                                             "/a/led",
1192                                             0,
1193                                             NULL,
1194                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1195
1196     uint8_t numResourceInterfaces;
1197     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1198     EXPECT_EQ(2, numResourceInterfaces);
1199     const char *resourceInterfaceName1 = OCGetResourceInterfaceName(handle, 0);
1200     EXPECT_STREQ(OC_RSRVD_INTERFACE_DEFAULT, resourceInterfaceName1);
1201     const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
1202     EXPECT_STREQ("core.rw", resourceInterfaceName);
1203
1204     // try getting resource interface names with an invalid index
1205     resourceInterfaceName = OCGetResourceInterfaceName(handle, 2);
1206     EXPECT_STREQ(NULL, resourceInterfaceName);
1207     // try getting resource interface names with an invalid index
1208     resourceInterfaceName = OCGetResourceInterfaceName(handle, 10);
1209     EXPECT_STREQ(NULL, resourceInterfaceName);
1210
1211     EXPECT_EQ(OC_STACK_OK, OCStop());
1212 }
1213
1214 TEST(StackResource, ResourceDefaultInterfaceAlwaysFirst)
1215 {
1216     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1217
1218     OIC_LOG(INFO, TAG, "Starting ResourceDefaultInterfaceAlwaysFirst test");
1219
1220     InitStack(OC_SERVER);
1221
1222     OCResourceHandle handle;
1223     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1224                                             "core.led",
1225                                             "core.rw",
1226                                             "/a/led",
1227                                             0,
1228                                             NULL,
1229                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1230     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle,
1231                                         OC_RSRVD_INTERFACE_DEFAULT));
1232     uint8_t numResourceInterfaces;
1233     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1234     EXPECT_EQ(2, numResourceInterfaces);
1235
1236     const char *interfaceName_1 = OCGetResourceInterfaceName(handle, 0);
1237     EXPECT_STREQ(OC_RSRVD_INTERFACE_DEFAULT, interfaceName_1);
1238
1239     const char*interfaceName_2 = OCGetResourceInterfaceName(handle, 1);
1240     EXPECT_STREQ("core.rw", interfaceName_2);
1241
1242     EXPECT_EQ(OC_STACK_OK, OCStop());
1243 }
1244
1245 TEST(StackResource, ResourceDuplicateDefaultInterfaces)
1246 {
1247     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1248
1249     OIC_LOG(INFO, TAG, "Starting ResourceDuplicateDefaultInterfaces test");
1250
1251     InitStack(OC_SERVER);
1252
1253     OCResourceHandle handle;
1254     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1255                                             "core.led",
1256                                             "core.rw",
1257                                             "/a/led",
1258                                             0,
1259                                             NULL,
1260                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1261
1262     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle,
1263                                         OC_RSRVD_INTERFACE_DEFAULT));
1264     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle,
1265                                         OC_RSRVD_INTERFACE_DEFAULT));
1266
1267     uint8_t numResourceInterfaces;
1268     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1269     EXPECT_EQ(2, numResourceInterfaces);
1270
1271     const char *interfaceName_1 = OCGetResourceInterfaceName(handle, 0);
1272     EXPECT_STREQ(OC_RSRVD_INTERFACE_DEFAULT, interfaceName_1);
1273
1274     EXPECT_EQ(OC_STACK_OK, OCStop());
1275 }
1276
1277 TEST(StackResource, ResourceDuplicateNonDefaultInterfaces)
1278 {
1279     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1280
1281     OIC_LOG(INFO, TAG, "Starting ResourceDuplicateInterfaces test");
1282
1283     InitStack(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                                             NULL,
1292                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1293
1294     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle,
1295                                         "core.rw"));
1296     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle,
1297                                         "core.rw"));
1298
1299     uint8_t numResourceInterfaces;
1300     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1301     EXPECT_EQ(2, numResourceInterfaces);
1302
1303     EXPECT_EQ(OC_STACK_OK, OCStop());
1304 }
1305
1306 TEST(StackResource, ResourceTypeInterfaceMethods)
1307 {
1308     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1309     OIC_LOG(INFO, TAG, "Starting ResourceTypeInterfaceMethods test");
1310     InitStack(OC_SERVER);
1311
1312     OCResourceHandle handle;
1313     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1314                                             "core.led",
1315                                             "core.rw",
1316                                             "/a/led",
1317                                             0,
1318                                             NULL,
1319                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1320
1321     uint8_t numResourceInterfaces;
1322     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1323     EXPECT_EQ(2, numResourceInterfaces);
1324
1325     EXPECT_EQ(OC_STACK_OK, OCStop());
1326 }
1327
1328 TEST(StackResource, GetResourceProperties)
1329 {
1330     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1331     OIC_LOG(INFO, TAG, "Starting GetResourceProperties test");
1332     InitStack(OC_SERVER);
1333
1334     uint8_t props = OC_DISCOVERABLE|OC_OBSERVABLE;
1335     OCResourceHandle handle;
1336     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1337                                             "core.led",
1338                                             "core.rw",
1339                                             "/a/led",
1340                                             0,
1341                                             NULL,
1342                                             props));
1343
1344     EXPECT_EQ(props, OCGetResourceProperties(handle) & props);
1345     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1346
1347     EXPECT_EQ(OC_STACK_OK, OCStop());
1348 }
1349
1350 TEST(StackResource, SetResourceProperties)
1351 {
1352     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1353     OIC_LOG(INFO, TAG, "Starting SetResourceProperties test");
1354     InitStack(OC_SERVER);
1355
1356     OCResourceHandle handle;
1357     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1358                                             "core.led",
1359                                             "core.rw",
1360                                             "/a/led",
1361                                             0,
1362                                             NULL,
1363                                             0));
1364
1365     uint8_t props = OC_DISCOVERABLE|OC_OBSERVABLE;
1366     EXPECT_EQ(OC_STACK_OK, OCSetResourceProperties(handle, props));
1367     EXPECT_EQ(props, OCGetResourceProperties(handle) & props);
1368
1369     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1370
1371     EXPECT_EQ(OC_STACK_OK, OCStop());
1372 }
1373
1374 TEST(StackResource, ClearResourceProperties)
1375 {
1376     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1377     OIC_LOG(INFO, TAG, "Starting ClearResourceProperties test");
1378     InitStack(OC_SERVER);
1379
1380     uint8_t props = OC_DISCOVERABLE|OC_OBSERVABLE;
1381     OCResourceHandle handle;
1382     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1383                                             "core.led",
1384                                             "core.rw",
1385                                             "/a/led",
1386                                             0,
1387                                             NULL,
1388                                             props));
1389
1390     EXPECT_EQ(OC_STACK_OK, OCClearResourceProperties(handle, props));
1391     EXPECT_EQ(0, OCGetResourceProperties(handle) & props);
1392
1393     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1394
1395     EXPECT_EQ(OC_STACK_OK, OCStop());
1396 }
1397
1398 TEST(StackResource, StackTestResourceDiscoverOneResourceBad)
1399 {
1400     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1401     OIC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverOneResourceBad test");
1402     InitStack(OC_SERVER);
1403     uint8_t numResources = 0;
1404     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1405
1406     OCResourceHandle handle;
1407     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1408                                             "core.led",
1409                                             "core.rw",
1410                                             "/a1/led",
1411                                             0,
1412                                             NULL,
1413                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1414     const char *url = OCGetResourceUri(handle);
1415     EXPECT_STREQ("/a1/led", url);
1416
1417     //EXPECT_EQ(OC_STACK_INVALID_URI, OCHandleServerRequest(&res, uri, query, req, rsp));
1418     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1419     uint8_t numExpectedResources = 0;
1420
1421     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numExpectedResources));
1422     EXPECT_EQ(numExpectedResources, numResources);
1423
1424     EXPECT_EQ(OC_STACK_OK, OCStop());
1425 }
1426
1427 TEST(StackResource, StackTestResourceDiscoverOneResource)
1428 {
1429     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1430     OIC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverOneResource test");
1431     InitStack(OC_SERVER);
1432
1433     OCResourceHandle handle;
1434     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1435                                             "core.led",
1436                                             "core.rw",
1437                                             "/a/led",
1438                                             0,
1439                                             NULL,
1440                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1441     const char *url = OCGetResourceUri(handle);
1442     EXPECT_STREQ("/a/led", url);
1443
1444     //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1445     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle));
1446
1447     EXPECT_EQ(OC_STACK_OK, OCStop());
1448 }
1449
1450 TEST(StackResource, StackTestResourceDiscoverManyResources)
1451 {
1452     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1453     OIC_LOG(INFO, TAG, "Starting StackTestResourceDiscoverManyResources test");
1454     InitStack(OC_SERVER);
1455
1456     OCResourceHandle handle1;
1457     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1458                                             "core.led",
1459                                             "core.rw",
1460                                             "/a/led1",
1461                                             0,
1462                                             NULL,
1463                                             OC_DISCOVERABLE));
1464     const char *url = OCGetResourceUri(handle1);
1465     EXPECT_STREQ("/a/led1", url);
1466
1467     OCResourceHandle handle2;
1468     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1469                                             "core.led",
1470                                             "core.rw",
1471                                             "/a/led2",
1472                                             0,
1473                                             NULL,
1474                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1475     url = OCGetResourceUri(handle2);
1476     EXPECT_STREQ("/a/led2", url);
1477
1478     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.brightled"));
1479     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle2, "core.colorled"));
1480
1481     OCResourceHandle handle3;
1482     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
1483                                             "core.led",
1484                                             "core.rw",
1485                                             "/a/led3",
1486                                             0,
1487                                             NULL,
1488                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1489     url = OCGetResourceUri(handle3);
1490     EXPECT_STREQ("/a/led3", url);
1491
1492     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, OC_RSRVD_INTERFACE_LL));
1493     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle3, OC_RSRVD_INTERFACE_BATCH));
1494
1495     OCResourceHandle handle4;
1496     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
1497                                             "core.led",
1498                                             "core.rw",
1499                                             "/a/led4",
1500                                             0,
1501                                             NULL,
1502                                             OC_DISCOVERABLE));
1503     url = OCGetResourceUri(handle4);
1504     EXPECT_STREQ("/a/led4", url);
1505
1506     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle4, "core.brightled"));
1507     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, OC_RSRVD_INTERFACE_LL));
1508     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle4, OC_RSRVD_INTERFACE_BATCH));
1509
1510     //EXPECT_EQ(OC_STACK_OK, OCHandleServerRequest(&res, uri, query, req, rsp));
1511
1512     EXPECT_EQ(OC_STACK_OK, OCStop());
1513 }
1514
1515 TEST_F(OCDevicePropertiesTests, DevicePropertiesToCBORPayloadllNULL)
1516 {
1517     EXPECT_EQ(OC_STACK_INVALID_PARAM, DevicePropertiesToCBORPayload(NULL, NULL, NULL));
1518 }
1519
1520 TEST_F(OCDevicePropertiesTests, DevicePropertiesToCBORPayloadNULLPayload)
1521 {
1522     OCDeviceProperties* deviceProps = getTestDeviceProps();
1523     ASSERT_TRUE(NULL != deviceProps);
1524     size_t size = 0;
1525
1526     EXPECT_EQ(OC_STACK_INVALID_PARAM, DevicePropertiesToCBORPayload(deviceProps, NULL, &size));
1527
1528     CleanUpDeviceProperties(&deviceProps);
1529 }
1530
1531 TEST_F(OCDevicePropertiesTests, DevicePropertiesToCBORPayloadNULLSize)
1532 {
1533     OCDeviceProperties* deviceProps = getTestDeviceProps();
1534     ASSERT_TRUE(NULL != deviceProps);
1535     uint8_t* payload = NULL;
1536
1537     EXPECT_EQ(OC_STACK_INVALID_PARAM, DevicePropertiesToCBORPayload(deviceProps, &payload, NULL));
1538
1539     CleanUpDeviceProperties(&deviceProps);
1540 }
1541
1542 TEST_F(OCDevicePropertiesTests, DevicePropertiesToCBORPayloadNULLDeviceProperties)
1543 {
1544     uint8_t* payload = NULL;
1545     size_t size = 0;
1546
1547     EXPECT_EQ(OC_STACK_INVALID_PARAM, DevicePropertiesToCBORPayload(NULL, &payload, &size));
1548 }
1549
1550 TEST_F(OCDevicePropertiesTests, DevicePropertiesToCBORPayloadVALID)
1551 {
1552     OCDeviceProperties* deviceProps = getTestDeviceProps();
1553     ASSERT_TRUE(NULL != deviceProps);
1554     uint8_t* payload = NULL;
1555     size_t size = 0;
1556
1557     EXPECT_EQ(OC_STACK_OK, DevicePropertiesToCBORPayload(deviceProps, &payload, &size));
1558     EXPECT_TRUE(payload != NULL);
1559
1560     CleanUpDeviceProperties(&deviceProps);
1561     OICFree(payload);
1562 }
1563
1564 TEST_F(OCDevicePropertiesTests, CBORPayloadToDevicePropertiesAllNULL)
1565 {
1566     EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToDeviceProperties(NULL, 0, NULL));
1567 }
1568
1569 TEST_F(OCDevicePropertiesTests, CBORPayloadToDevicePropertiesNULLPayload)
1570 {
1571     OCDeviceProperties* deviceProps = NULL;
1572     size_t size = 10;
1573
1574     EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToDeviceProperties(NULL, size, &deviceProps));
1575 }
1576
1577 TEST_F(OCDevicePropertiesTests, CBORPayloadToDevicePropertiesInvalidSize)
1578 {
1579     OCDeviceProperties* deviceProps = NULL;
1580     uint8_t* payload = (uint8_t*)OICCalloc(1, sizeof(uint8_t));
1581     ASSERT_TRUE(NULL != payload);
1582
1583     EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToDeviceProperties(payload, 0, &deviceProps));
1584
1585     OICFree(payload);
1586 }
1587
1588 TEST_F(OCDevicePropertiesTests, CBORPayloadToDevicePropertiesNULLDeviceProperties)
1589 {
1590     uint8_t* payload = (uint8_t*)OICCalloc(1, sizeof(uint8_t));
1591     ASSERT_TRUE(NULL != payload);
1592     size_t size = 10;
1593
1594     EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToDeviceProperties(payload, size, NULL));
1595
1596     OICFree(payload);
1597 }
1598
1599 TEST_F(OCDevicePropertiesTests, CBORPayloadToDevicePropertiesVALID)
1600 {
1601     OCDeviceProperties* controlDeviceProps = getTestDeviceProps();
1602     ASSERT_TRUE(NULL != controlDeviceProps);
1603     OCDeviceProperties* testDeviceProps = NULL;
1604     uint8_t* payload = NULL;
1605     size_t size = 0;
1606
1607     EXPECT_EQ(OC_STACK_OK, DevicePropertiesToCBORPayload(controlDeviceProps, &payload, &size));
1608     EXPECT_TRUE(payload != NULL);
1609
1610     EXPECT_EQ(OC_STACK_OK, CBORPayloadToDeviceProperties(payload, size, &testDeviceProps));
1611     ASSERT_TRUE(testDeviceProps != NULL);
1612     EXPECT_STREQ(gDevicePIID, testDeviceProps->protocolIndependentId);
1613
1614     CleanUpDeviceProperties(&controlDeviceProps);
1615     CleanUpDeviceProperties(&testDeviceProps);
1616     OICFree(payload);
1617 }
1618
1619 TEST_F(OCDevicePropertiesTests, PIIDAvailableAfterInit)
1620 {
1621     void *piid = NULL;
1622
1623     InitStack(OC_SERVER);
1624
1625     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, &piid));
1626     ASSERT_TRUE(piid != NULL);
1627
1628     OICFree(piid);
1629
1630     EXPECT_EQ(OC_STACK_OK, OCStop());
1631 }
1632
1633 TEST_F(OCDevicePropertiesTests, UpdatePIID)
1634 {
1635     void *originalPIID = NULL;
1636     void *newPIID = NULL;
1637
1638     InitStack(OC_SERVER);
1639
1640     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, &originalPIID));
1641     ASSERT_TRUE(originalPIID != NULL);
1642
1643     EXPECT_EQ(OC_STACK_OK, OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, gDevicePIID));
1644
1645     EXPECT_EQ(OC_STACK_OK, OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, &newPIID));
1646     ASSERT_TRUE(newPIID != NULL);
1647     EXPECT_STREQ(gDevicePIID, (char *)newPIID);
1648     EXPECT_STRNE((char *)originalPIID, (char *)newPIID);
1649
1650     OICFree(newPIID);
1651     OICFree(originalPIID);
1652
1653     EXPECT_EQ(OC_STACK_OK, OCStop());
1654 }
1655
1656 TEST(StackBind, BindResourceTypeNameBad)
1657 {
1658     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1659     OIC_LOG(INFO, TAG, "Starting BindResourceTypeNameBad test");
1660     InitStack(OC_SERVER);
1661
1662     OCResourceHandle handle;
1663     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1664                                             "core.led",
1665                                             "core.rw",
1666                                             "/a/led",
1667                                             0,
1668                                             NULL,
1669                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1670
1671     uint8_t numResourceTypes;
1672     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1673     EXPECT_EQ(1, numResourceTypes);
1674     const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
1675     EXPECT_STREQ("core.led", resourceTypeName);
1676
1677     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceTypeToResource(handle, NULL));
1678     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceTypeToResource(handle, "core.nameBad"));
1679     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceTypeToResource(handle, "core.name bad"));
1680
1681     EXPECT_EQ(OC_STACK_OK, OCStop());
1682 }
1683
1684 TEST(StackBind, BindResourceTypeNameGood)
1685 {
1686     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1687     OIC_LOG(INFO, TAG, "Starting BindResourceTypeNameGood test");
1688     InitStack(OC_SERVER);
1689
1690     OCResourceHandle handle;
1691     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1692                                             "core.led",
1693                                             "core.rw",
1694                                             "/a/led",
1695                                             0,
1696                                             NULL,
1697                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1698
1699     uint8_t numResourceTypes;
1700     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1701     EXPECT_EQ(1, numResourceTypes);
1702     const char *resourceTypeName = OCGetResourceTypeName(handle, 0);
1703     EXPECT_STREQ("core.led", resourceTypeName);
1704
1705     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
1706     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1707     EXPECT_EQ(2, numResourceTypes);
1708     resourceTypeName = OCGetResourceTypeName(handle, 1);
1709     EXPECT_STREQ("core.brightled", resourceTypeName);
1710
1711     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
1712     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1713     EXPECT_EQ(3, numResourceTypes);
1714     resourceTypeName = OCGetResourceTypeName(handle, 2);
1715     EXPECT_STREQ("core.reallybrightled", resourceTypeName);
1716
1717     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "x.ReallyReallyBrightLED"));
1718     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1719     EXPECT_EQ(4, numResourceTypes);
1720     resourceTypeName = OCGetResourceTypeName(handle, 3);
1721     EXPECT_STREQ("x.ReallyReallyBrightLED", resourceTypeName);
1722
1723     EXPECT_EQ(OC_STACK_OK, OCStop());
1724 }
1725
1726 TEST(StackBind, BindResourceTypeAttribRepGood)
1727 {
1728     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1729     OIC_LOG(INFO, TAG, "Starting BindResourceTypeAttribRepGood test");
1730     InitStack(OC_SERVER);
1731
1732     OCResourceHandle handle;
1733     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1734                                             "core.led",
1735                                             "core.rw",
1736                                             "/a/led",
1737                                             0,
1738                                             NULL,
1739                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1740
1741     uint8_t numResourceTypes;
1742     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1743     EXPECT_EQ(1, numResourceTypes);
1744
1745     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.brightled"));
1746     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1747     EXPECT_EQ(2, numResourceTypes);
1748
1749     EXPECT_EQ(OC_STACK_OK, OCBindResourceTypeToResource(handle, "core.reallybrightled"));
1750     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceTypes(handle, &numResourceTypes));
1751     EXPECT_EQ(3, numResourceTypes);
1752
1753     EXPECT_EQ(OC_STACK_OK, OCStop());
1754 }
1755
1756
1757 TEST(StackBind, BindResourceInterfaceNameBad)
1758 {
1759     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1760     OIC_LOG(INFO, TAG, "Starting BindResourceInterfaceNameBad test");
1761     InitStack(OC_SERVER);
1762
1763     OCResourceHandle handle;
1764     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1765                                             "core.led",
1766                                             "core.rw",
1767                                             "/a/led",
1768                                             0,
1769                                             NULL,
1770                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1771
1772     uint8_t numResourceInterfaces;
1773     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1774     EXPECT_EQ(2, numResourceInterfaces);
1775     const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
1776     EXPECT_STREQ("core.rw", resourceInterfaceName);
1777
1778     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, NULL));
1779
1780     EXPECT_EQ(OC_STACK_OK, OCStop());
1781 }
1782
1783 TEST(StackBind, BindResourceInterfaceNameGood)
1784 {
1785     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1786     OIC_LOG(INFO, TAG, "Starting BindResourceInterfaceNameGood test");
1787     InitStack(OC_SERVER);
1788
1789     OCResourceHandle handle;
1790     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1791                                             "core.led",
1792                                             "core.rw",
1793                                             "/a/led",
1794                                             0,
1795                                             NULL,
1796                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1797
1798     uint8_t numResourceInterfaces;
1799     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1800     EXPECT_EQ(2, numResourceInterfaces);
1801     const char *resourceInterfaceName = OCGetResourceInterfaceName(handle, 1);
1802     EXPECT_STREQ("core.rw", resourceInterfaceName);
1803
1804     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
1805
1806     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1807     EXPECT_EQ(3, numResourceInterfaces);
1808     resourceInterfaceName = OCGetResourceInterfaceName(handle, 2);
1809     EXPECT_STREQ("core.r", resourceInterfaceName);
1810
1811     EXPECT_EQ(OC_STACK_OK, OCStop());
1812 }
1813
1814 TEST(StackBind, BindResourceInterfaceMethodsBad)
1815 {
1816     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1817     OIC_LOG(INFO, TAG, "Starting BindResourceInterfaceMethodsBad test");
1818     InitStack(OC_SERVER);
1819
1820     OCResourceHandle handle;
1821     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1822                                             "core.led",
1823                                             "core.rw",
1824                                             "/a/led",
1825                                             0,
1826                                             NULL,
1827                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1828
1829     uint8_t numResourceInterfaces;
1830     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1831     EXPECT_EQ(2, numResourceInterfaces);
1832
1833     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceInterfaceToResource(handle, 0));
1834
1835     EXPECT_EQ(OC_STACK_OK, OCStop());
1836 }
1837
1838 TEST(StackBind, BindResourceInterfaceMethodsGood)
1839 {
1840     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1841     OIC_LOG(INFO, TAG, "Starting BindResourceInterfaceMethodsGood test");
1842     InitStack(OC_SERVER);
1843
1844     OCResourceHandle handle;
1845     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
1846                                             "core.led",
1847                                             "core.rw",
1848                                             "/a/led",
1849                                             0,
1850                                             NULL,
1851                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1852
1853     uint8_t numResourceInterfaces;
1854     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1855     EXPECT_EQ(2, numResourceInterfaces);
1856
1857     EXPECT_EQ(OC_STACK_OK, OCBindResourceInterfaceToResource(handle, "core.r"));
1858
1859     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle, &numResourceInterfaces));
1860     EXPECT_EQ(3, numResourceInterfaces);
1861
1862     EXPECT_EQ(OC_STACK_OK, OCStop());
1863 }
1864
1865 TEST(StackBind, BindContainedResourceBad)
1866 {
1867     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1868     OIC_LOG(INFO, TAG, "Starting BindContainedResourceBad test");
1869     InitStack(OC_SERVER);
1870
1871     OCResourceHandle containerHandle;
1872     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
1873                                             "core.led",
1874                                             "core.rw",
1875                                             "/a/kitchen",
1876                                             0,
1877                                             NULL,
1878                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1879
1880     OCResourceHandle handle0;
1881     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1882                                             "core.led",
1883                                             "core.rw",
1884                                             "/a/led",
1885                                             0,
1886                                             NULL,
1887                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1888
1889     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResource(containerHandle, containerHandle));
1890
1891     EXPECT_EQ(OC_STACK_ERROR, OCBindResource((OCResourceHandle) 0, handle0));
1892
1893     EXPECT_EQ(OC_STACK_OK, OCStop());
1894 }
1895
1896 TEST(StackBind, BindContainedResourceGood)
1897 {
1898     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
1899     OIC_LOG(INFO, TAG, "Starting BindContainedResourceGood test");
1900     InitStack(OC_SERVER);
1901
1902     uint8_t numResources = 0;
1903     uint8_t numExpectedResources = 0;
1904
1905     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numExpectedResources));
1906
1907     OCResourceHandle containerHandle;
1908     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
1909                                             "core.led",
1910                                             "core.rw",
1911                                             "/a/kitchen",
1912                                             0,
1913                                             NULL,
1914                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1915     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1916     EXPECT_EQ(++numExpectedResources, numResources);
1917
1918     OCResourceHandle handle0;
1919     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
1920                                             "core.led",
1921                                             "core.rw",
1922                                             "/a/led0",
1923                                             0,
1924                                             NULL,
1925                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1926     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1927     EXPECT_EQ(++numExpectedResources, numResources);
1928
1929     OCResourceHandle handle1;
1930     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
1931                                             "core.led",
1932                                             "core.rw",
1933                                             "/a/led1",
1934                                             0,
1935                                             NULL,
1936                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1937     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1938     EXPECT_EQ(++numExpectedResources, numResources);
1939
1940     OCResourceHandle handle2;
1941     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
1942                                             "core.led",
1943                                             "core.rw",
1944                                             "/a/led2",
1945                                             0,
1946                                             NULL,
1947                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1948     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1949     EXPECT_EQ(++numExpectedResources, numResources);
1950
1951     OCResourceHandle handle3;
1952     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
1953                                             "core.led",
1954                                             "core.rw",
1955                                             "/a/led3",
1956                                             0,
1957                                             NULL,
1958                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1959     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1960     EXPECT_EQ(++numExpectedResources, numResources);
1961
1962     OCResourceHandle handle4;
1963     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
1964                                             "core.led",
1965                                             "core.rw",
1966                                             "/a/led4",
1967                                             0,
1968                                             NULL,
1969                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1970     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1971     EXPECT_EQ(++numExpectedResources, numResources);
1972
1973     OCResourceHandle handle5;
1974     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
1975                                             "core.led",
1976                                             "core.rw",
1977                                             "/a/led5",
1978                                             0,
1979                                             NULL,
1980                                             OC_DISCOVERABLE|OC_OBSERVABLE));
1981     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
1982     EXPECT_EQ(++numExpectedResources, numResources);
1983
1984
1985     EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle0));
1986     EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle1));
1987     EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle2));
1988     EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle3));
1989     EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle4));
1990     EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle5));
1991
1992     EXPECT_EQ(handle0, OCGetResourceHandleFromCollection(containerHandle, 0));
1993     EXPECT_EQ(handle1, OCGetResourceHandleFromCollection(containerHandle, 1));
1994     EXPECT_EQ(handle2, OCGetResourceHandleFromCollection(containerHandle, 2));
1995     EXPECT_EQ(handle3, OCGetResourceHandleFromCollection(containerHandle, 3));
1996     EXPECT_EQ(handle4, OCGetResourceHandleFromCollection(containerHandle, 4));
1997     EXPECT_EQ(handle5, OCGetResourceHandleFromCollection(containerHandle, 5));
1998
1999     EXPECT_EQ(OC_STACK_OK, OCStop());
2000 }
2001
2002
2003 TEST(StackBind, BindEntityHandlerBad)
2004 {
2005     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2006     OIC_LOG(INFO, TAG, "Starting BindEntityHandlerBad test");
2007     InitStack(OC_SERVER);
2008
2009     OCResourceHandle handle;
2010     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
2011                                             "core.led",
2012                                             "core.rw",
2013                                             "/a/led",
2014                                             0,
2015                                             NULL,
2016                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2017
2018     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCBindResourceHandler(NULL, NULL, NULL));
2019
2020     EXPECT_EQ(OC_STACK_OK, OCStop());
2021 }
2022
2023 TEST(StackBind, BindEntityHandlerGood)
2024 {
2025     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2026     OIC_LOG(INFO, TAG, "Starting BindEntityHandlerGood test");
2027     InitStack(OC_SERVER);
2028
2029     OCResourceHandle handle;
2030     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle,
2031                                             "core.led",
2032                                             "core.rw",
2033                                             "/a/led",
2034                                             0,
2035                                             NULL,
2036                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2037
2038     OCEntityHandler myHandler = entityHandler;
2039
2040     EXPECT_EQ(OC_STACK_OK, OCBindResourceHandler(handle, myHandler, NULL));
2041
2042     EXPECT_EQ(myHandler, OCGetResourceHandler(handle));
2043
2044     EXPECT_EQ(OC_STACK_OK, OCStop());
2045 }
2046
2047 TEST(StackResourceAccess, GetResourceByIndex)
2048 {
2049     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2050     OIC_LOG(INFO, TAG, "Starting GetResourceByIndex test");
2051     InitStack(OC_SERVER);
2052
2053     uint8_t numResources = 0;
2054     uint8_t numExpectedResources = 0;
2055     uint8_t resourceIndex = 0;
2056     uint8_t prevResources = 0;
2057     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numExpectedResources));
2058     prevResources = numExpectedResources;
2059     OCResourceHandle containerHandle;
2060     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
2061                                             "core.led",
2062                                             "core.rw",
2063                                             "/a/kitchen",
2064                                             0,
2065                                             NULL,
2066                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2067     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2068     EXPECT_EQ(++numExpectedResources, numResources);
2069
2070     OCResourceHandle handle0;
2071     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
2072                                             "core.led",
2073                                             "core.rw",
2074                                             "/a/led0",
2075                                             0,
2076                                             NULL,
2077                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2078     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2079     EXPECT_EQ(++numExpectedResources, numResources);
2080
2081     OCResourceHandle handle1;
2082     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
2083                                             "core.led",
2084                                             "core.rw",
2085                                             "/a/led1",
2086                                             0,
2087                                             NULL,
2088                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2089     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2090     EXPECT_EQ(++numExpectedResources, numResources);
2091
2092     OCResourceHandle handle2;
2093     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
2094                                             "core.led",
2095                                             "core.rw",
2096                                             "/a/led2",
2097                                             0,
2098                                             NULL,
2099                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2100     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2101     EXPECT_EQ(++numExpectedResources, numResources);
2102
2103     OCResourceHandle handle3;
2104     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
2105                                             "core.led",
2106                                             "core.rw",
2107                                             "/a/led3",
2108                                             0,
2109                                             NULL,
2110                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2111     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2112     EXPECT_EQ(++numExpectedResources, numResources);
2113
2114     OCResourceHandle handle4;
2115     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle4,
2116                                             "core.led",
2117                                             "core.rw",
2118                                             "/a/led4",
2119                                             0,
2120                                             NULL,
2121                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2122     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2123     EXPECT_EQ(++numExpectedResources, numResources);
2124
2125     OCResourceHandle handle5;
2126     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle5,
2127                                             "core.led",
2128                                             "core.rw",
2129                                             "/a/led5",
2130                                             0,
2131                                             NULL,
2132                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2133     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2134     EXPECT_EQ(++numExpectedResources, numResources);
2135     resourceIndex += prevResources;
2136     EXPECT_EQ(containerHandle, OCGetResourceHandle(resourceIndex));
2137     EXPECT_EQ(handle0, OCGetResourceHandle(++resourceIndex));
2138     EXPECT_EQ(handle1, OCGetResourceHandle(++resourceIndex));
2139     EXPECT_EQ(handle2, OCGetResourceHandle(++resourceIndex));
2140     EXPECT_EQ(handle3, OCGetResourceHandle(++resourceIndex));
2141     EXPECT_EQ(handle4, OCGetResourceHandle(++resourceIndex));
2142     EXPECT_EQ(handle5, OCGetResourceHandle(++resourceIndex));
2143
2144     EXPECT_EQ(OC_STACK_OK, OCStop());
2145 }
2146
2147 TEST(StackResourceAccess, DeleteHeadResource)
2148 {
2149     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2150     OIC_LOG(INFO, TAG, "Starting DeleteHeadResource test");
2151     InitStack(OC_SERVER);
2152
2153     uint8_t numResources = 0;
2154     uint8_t numExpectedResources = 0;
2155
2156     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numExpectedResources));
2157
2158     OCResourceHandle handle0;
2159     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
2160                                             "core.led",
2161                                             "core.rw",
2162                                             "/a/led0",
2163                                             0,
2164                                             NULL,
2165                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2166     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2167     EXPECT_EQ(++numExpectedResources, numResources);
2168
2169     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
2170     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2171     EXPECT_EQ(--numExpectedResources, numResources);
2172
2173     EXPECT_EQ(OC_STACK_OK, OCStop());
2174 }
2175
2176 TEST(StackResourceAccess, DeleteHeadResource2)
2177 {
2178     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2179     OIC_LOG(INFO, TAG, "Starting DeleteHeadResource2 test");
2180     InitStack(OC_SERVER);
2181
2182     uint8_t numResources = 0;
2183     uint8_t numExpectedResources = 0;
2184
2185     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numExpectedResources));
2186     OCResourceHandle handle0;
2187     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
2188                                             "core.led",
2189                                             "core.rw",
2190                                             "/a/led0",
2191                                             0,
2192                                             NULL,
2193                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2194     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2195     EXPECT_EQ(++numExpectedResources, numResources);
2196
2197     OCResourceHandle handle1;
2198     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
2199                                             "core.led",
2200                                             "core.rw",
2201                                             "/a/led1",
2202                                             0,
2203                                             NULL,
2204                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2205     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2206     EXPECT_EQ(++numExpectedResources, numResources);
2207
2208     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle0));
2209     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2210     EXPECT_EQ(--numExpectedResources, numResources);
2211
2212     EXPECT_EQ(handle1, OCGetResourceHandle(numResources - 1));
2213
2214     EXPECT_EQ(OC_STACK_OK, OCStop());
2215 }
2216
2217
2218 TEST(StackResourceAccess, DeleteLastResource)
2219 {
2220     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2221     OIC_LOG(INFO, TAG, "Starting DeleteLastResource test");
2222     InitStack(OC_SERVER);
2223
2224     uint8_t numResources = 0;
2225     uint8_t numExpectedResources = 0;
2226
2227     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numExpectedResources));
2228
2229     OCResourceHandle handle0;
2230     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
2231                                             "core.led",
2232                                             "core.rw",
2233                                             "/a/led0",
2234                                             0,
2235                                             NULL,
2236                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2237     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2238     EXPECT_EQ(++numExpectedResources, numResources);
2239
2240     OCResourceHandle handle1;
2241     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
2242                                             "core.led",
2243                                             "core.rw",
2244                                             "/a/led1",
2245                                             0,
2246                                             NULL,
2247                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2248     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2249     EXPECT_EQ(++numExpectedResources, numResources);
2250
2251     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
2252     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2253     EXPECT_EQ(--numExpectedResources, numResources);
2254
2255     EXPECT_EQ(handle0, OCGetResourceHandle(numResources - 1));
2256
2257     OCResourceHandle handle2;
2258     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
2259                                             "core.led",
2260                                             "core.rw",
2261                                             "/a/led2",
2262                                             0,
2263                                             NULL,
2264                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2265     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2266     EXPECT_EQ(++numExpectedResources, numResources);
2267
2268     EXPECT_EQ(OC_STACK_OK, OCStop());
2269 }
2270
2271 TEST(StackResourceAccess, DeleteMiddleResource)
2272 {
2273     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2274     OIC_LOG(INFO, TAG, "Starting DeleteMiddleResource test");
2275     InitStack(OC_SERVER);
2276
2277     uint8_t numResources = 0;
2278     uint8_t numExpectedResources = 0;
2279     uint8_t resourceIndex = InitResourceIndex();
2280
2281     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numExpectedResources));
2282     resourceIndex = numExpectedResources;
2283     OCResourceHandle handle0;
2284     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
2285                                             "core.led",
2286                                             "core.rw",
2287                                             "/a/led0",
2288                                             0,
2289                                             NULL,
2290                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2291     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2292     EXPECT_EQ(++numExpectedResources, numResources);
2293
2294     OCResourceHandle handle1;
2295     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
2296                                             "core.led",
2297                                             "core.rw",
2298                                             "/a/led1",
2299                                             0,
2300                                             NULL,
2301                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2302     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2303     EXPECT_EQ(++numExpectedResources, numResources);
2304
2305     OCResourceHandle handle2;
2306     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
2307                                             "core.led",
2308                                             "core.rw",
2309                                             "/a/led2",
2310                                             0,
2311                                             NULL,
2312                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2313     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2314     EXPECT_EQ(++numExpectedResources, numResources);
2315
2316     EXPECT_EQ(OC_STACK_OK, OCDeleteResource(handle1));
2317     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResources(&numResources));
2318     EXPECT_EQ(--numExpectedResources, numResources);
2319
2320     EXPECT_EQ(handle0, OCGetResourceHandle(resourceIndex));
2321     EXPECT_EQ(handle2, OCGetResourceHandle(++resourceIndex));
2322
2323     // Make sure the resource elements are still correct
2324     uint8_t numResourceInterfaces;
2325     EXPECT_EQ(OC_STACK_OK, OCGetNumberOfResourceInterfaces(handle2, &numResourceInterfaces));
2326     EXPECT_EQ(2, numResourceInterfaces);
2327     const char *resourceInterfaceName = OCGetResourceInterfaceName(handle2, 1);
2328     EXPECT_STREQ("core.rw", resourceInterfaceName);
2329
2330     EXPECT_EQ(OC_STACK_OK, OCStop());
2331 }
2332
2333 // Visual Studio versions earlier than 2015 have bugs in is_pod and report the wrong answer.
2334 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
2335 TEST(PODTests, OCHeaderOption)
2336 {
2337     EXPECT_TRUE(std::is_pod<OCHeaderOption>::value);
2338 }
2339
2340 TEST(PODTests, OCCallbackData)
2341 {
2342     EXPECT_TRUE(std::is_pod<OCCallbackData>::value);
2343 }
2344 #endif
2345
2346 TEST(OCDoDirectPairingTests, Nullpeer)
2347 {
2348     EXPECT_EQ(OC_STACK_INVALID_PARAM,OCDoDirectPairing(NULL, NULL, pmSel, &pinNumber, &resultCallback));
2349 }
2350
2351 TEST(OCDoDirectPairingTests, NullCallback)
2352 {
2353     EXPECT_EQ(OC_STACK_INVALID_CALLBACK,OCDoDirectPairing(NULL, &peer, pmSel, &pinNumber, NULL));
2354 }
2355
2356 TEST(OCDoDirectPairingTests, NullpinNumber)
2357 {
2358     EXPECT_EQ(OC_STACK_INVALID_PARAM,OCDoDirectPairing(NULL, &peer, pmSel, NULL, &resultCallback));
2359 }
2360
2361 TEST(StackResource, MultipleResourcesDiscovery)
2362 {
2363     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2364     OIC_LOG(INFO, TAG, "Starting MultipleResourcesDiscovery test");
2365     InitStack(OC_SERVER);
2366
2367     OCResourceHandle handle1;
2368     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
2369                                             "core.led",
2370                                             "core.rw",
2371                                             "/a/led1",
2372                                             0,
2373                                             NULL,
2374                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2375
2376     OCResourceHandle handle2;
2377     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
2378                                             "core.led",
2379                                             "core.rw",
2380                                             "/a/led2",
2381                                             0,
2382                                             NULL,
2383                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2384     OCResourceHandle handle3;
2385     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
2386                                             "core.led",
2387                                             "core.rw",
2388                                             "/a/led3",
2389                                             0,
2390                                             NULL,
2391                                             OC_DISCOVERABLE|OC_OBSERVABLE));
2392     /* Start a discovery query*/
2393     char szQueryUri[MAX_QUERY_LENGTH] = "/oic/res?if=oic.if.ll";
2394     OCCallbackData cbData;
2395     cbData.cb = discoveryCallback;
2396     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
2397     cbData.cd = NULL;
2398
2399     OCDoHandle handle;
2400     EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle,
2401                                         OC_REST_DISCOVER,
2402                                         szQueryUri,
2403                                         0,
2404                                         0,
2405                                         CT_ADAPTER_IP,
2406                                         OC_LOW_QOS,
2407                                         &cbData,
2408                                         NULL,
2409                                         0));
2410
2411     EXPECT_EQ(OC_STACK_OK, OCStop());
2412 }
2413
2414 TEST(StackPayload, CloneByteString)
2415 {
2416     uint8_t bytes[] = { 0, 1, 2, 3 };
2417     OCByteString byteString;
2418     byteString.bytes = bytes;
2419     byteString.len = sizeof(bytes);
2420
2421     OCRepPayload *original = OCRepPayloadCreate();
2422     ASSERT_TRUE(original != NULL);
2423     EXPECT_TRUE(OCRepPayloadSetPropByteString(original, "name", byteString));
2424
2425     OCRepPayload *clone = OCRepPayloadClone(original);
2426     ASSERT_TRUE(clone != NULL);
2427
2428     OCRepPayloadDestroy(original);
2429
2430     OCByteString cloneByteString;
2431     EXPECT_TRUE(OCRepPayloadGetPropByteString(clone, "name", &cloneByteString));
2432     ASSERT_TRUE(cloneByteString.bytes != NULL);
2433     EXPECT_EQ(sizeof(bytes), cloneByteString.len);
2434     EXPECT_TRUE(0 == memcmp(bytes, cloneByteString.bytes, sizeof(bytes)));
2435     OICFree(cloneByteString.bytes);
2436
2437     OCRepPayloadDestroy(clone);
2438 }
2439
2440 TEST(StackUri, Rfc6874_Noop_1)
2441 {
2442     char validIPv6Address[] = "FF01:0:0:0:0:0:0:FB";
2443     char bytes[100] = {0};
2444     strncpy(bytes, validIPv6Address, sizeof(bytes));
2445
2446     OCStackResult result = OCEncodeAddressForRFC6874(bytes, sizeof(bytes), validIPv6Address);
2447
2448     // No % sign, should do nothing
2449     EXPECT_STREQ(bytes, validIPv6Address);
2450     EXPECT_EQ(OC_STACK_OK, result);
2451 }
2452
2453 TEST(StackUri, Rfc6874_Noop_2)
2454 {
2455     char validIPv6Address[] = "3812:a61::4:1";
2456     char bytes[100] = {0};
2457
2458     OCStackResult result = OCEncodeAddressForRFC6874(bytes, sizeof(bytes), validIPv6Address);
2459
2460     // No % sign, should do nothing
2461     EXPECT_STREQ(bytes, validIPv6Address);
2462     EXPECT_EQ(OC_STACK_OK, result);
2463 }
2464
2465 TEST(StackUri, Rfc6874_WithEncoding)
2466 {
2467     char validIPv6Address[] =        "fe80::dafe:e3ff:fe00:ebfa%wlan0";
2468     char validIPv6AddressEncoded[] = "fe80::dafe:e3ff:fe00:ebfa%25wlan0";
2469     char bytes[100] = "";
2470     strncpy(bytes, validIPv6Address, sizeof(bytes));
2471
2472     OCStackResult result = OCEncodeAddressForRFC6874(bytes, sizeof(bytes), validIPv6Address);
2473
2474     // Encoding should have occured
2475     EXPECT_STREQ(bytes, validIPv6AddressEncoded);
2476     EXPECT_EQ(OC_STACK_OK, result);
2477 }
2478
2479 TEST(StackUri, Rfc6874_WithEncoding_ExtraPercent)
2480 {
2481     char validIPv6Address[] = "fe80::dafe:e3ff:fe00:ebfa%%wlan0";
2482     char bytes[100] = {0};
2483
2484     OCStackResult result = OCEncodeAddressForRFC6874(bytes, sizeof(bytes), validIPv6Address);
2485
2486     // Encoding should have failed due to extra '%' character
2487     EXPECT_STREQ(bytes, "");
2488     EXPECT_EQ(OC_STACK_ERROR, result);
2489 }
2490
2491 TEST(StackUri, Rfc6874_AlreadyEncoded)
2492 {
2493     char validIPv6AddressEncoded[] = "fe80::dafe:e3ff:fe00:ebfa%25wlan0";
2494     char bytes[100] = {0};
2495
2496     OCStackResult result = OCEncodeAddressForRFC6874(bytes, sizeof(bytes), validIPv6AddressEncoded);
2497
2498     // Encoding should have failed due to extra '%' character
2499     EXPECT_STREQ(bytes, "");
2500     EXPECT_EQ(OC_STACK_ERROR, result);
2501 }
2502
2503 TEST(StackUri, Rfc6874_NoOverflow)
2504 {
2505     char validIPv6Address[] = "fe80::dafe:e3ff:fe00:ebfa%wlan0";
2506     char addrBuffer[100];
2507     char bytes[100] = {0};
2508     memset(addrBuffer, sizeof(addrBuffer), '_');
2509
2510     // Just enough room to encode
2511     addrBuffer[sizeof(addrBuffer) - sizeof(validIPv6Address) - 3] = '\0';
2512     strcat(addrBuffer, validIPv6Address);
2513
2514     OCStackResult result = OCEncodeAddressForRFC6874(bytes, sizeof(bytes), addrBuffer);
2515
2516     // Encoding should have succeeded
2517     EXPECT_EQ(OC_STACK_OK, result);
2518 }
2519
2520 TEST(StackUri, Rfc6874_NoOverflow_2)
2521 {
2522     char validIPv6Address[] = "fe80::dafe:e3ff:fe00:ebfa%wlan0";
2523     char addrBuffer[100];
2524     char bytes[100] = {0};
2525     memset(addrBuffer, sizeof(addrBuffer), '_');
2526
2527     // Not enough room to encode
2528     addrBuffer[sizeof(addrBuffer) - sizeof(validIPv6Address) - 1] = '\0';
2529     strcat(addrBuffer, validIPv6Address);
2530
2531     OCStackResult result = OCEncodeAddressForRFC6874(bytes, sizeof(bytes), addrBuffer);
2532
2533     // Encoding should have failed due to output size limitations
2534     EXPECT_STREQ(bytes, "");
2535     EXPECT_EQ(OC_STACK_ERROR, result);
2536 }
2537
2538 TEST(StackHeaderOption, setHeaderOption)
2539 {
2540     uint8_t optionValue1[MAX_HEADER_OPTION_DATA_LENGTH] =
2541     { 1 };
2542     OCHeaderOption options[MAX_HEADER_OPTIONS] =
2543     {
2544     { OC_COAP_ID, 6, 8, optionValue1 }, };
2545     uint8_t optionData = 255;
2546     size_t optionDataSize = sizeof(optionData);
2547     size_t numOptions = 1;
2548     uint16_t optionID = 2048;
2549     EXPECT_EQ(OC_STACK_OK, OCSetHeaderOption(options,
2550                                              &numOptions,
2551                                              optionID,
2552                                              &optionData,
2553                                              optionDataSize));
2554     EXPECT_EQ(options[1].optionID, optionID);
2555     EXPECT_EQ(options[1].optionData[0], 255);
2556 }
2557
2558 TEST(StackHeaderOption, getHeaderOption)
2559 {
2560     uint8_t optionValue1[MAX_HEADER_OPTION_DATA_LENGTH] =
2561     { 1 };
2562     uint8_t optionValue2[MAX_HEADER_OPTION_DATA_LENGTH] =
2563     { 255 };
2564     OCHeaderOption options[MAX_HEADER_OPTIONS] =
2565     {
2566     { OC_COAP_ID, 6, 8, optionValue1 },
2567     { OC_COAP_ID, 2048, 16, optionValue2 }, };
2568     uint8_t optionData[MAX_HEADER_OPTION_DATA_LENGTH];
2569     size_t optionDataSize = sizeof(optionData);
2570     size_t numOptions = 2;
2571     uint16_t optionID = 6;
2572     uint16_t actualDataSize = 0;
2573     EXPECT_EQ(OC_STACK_OK, OCGetHeaderOption(options,
2574                                              numOptions,
2575                                              optionID,
2576                                              optionData,
2577                                              optionDataSize,
2578                                              &actualDataSize));
2579     EXPECT_EQ(1, optionData[0]);
2580     EXPECT_EQ(8, actualDataSize);
2581 }
2582
2583 TEST(StackEndpoints, OCGetSupportedEndpointTpsFlags)
2584 {
2585     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2586
2587     OIC_LOG(INFO, TAG, "OCGetSupportedEndpointTpsFlags test");
2588
2589     InitStack(OC_SERVER);
2590
2591     EXPECT_LE(INVALID_TPS_FLAGS_ZERO, OCGetSupportedEndpointTpsFlags());
2592
2593     EXPECT_EQ(OC_STACK_OK, OCStop());
2594 }
2595
2596 static OCStackApplicationResult DiscoverBaselineResource(void *ctx, OCDoHandle handle,
2597     OCClientResponse *response)
2598 {
2599     OC_UNUSED(ctx);
2600     OC_UNUSED(handle);
2601     EXPECT_EQ(OC_STACK_OK, response->result);
2602     EXPECT_TRUE(NULL != response->payload);
2603     if (NULL != response->payload)
2604     {
2605         EXPECT_EQ(PAYLOAD_TYPE_DISCOVERY, response->payload->type);
2606
2607         OCDiscoveryPayload *payload = (OCDiscoveryPayload *)response->payload;
2608         EXPECT_TRUE(NULL != payload->sid);
2609         EXPECT_STREQ("StackTest", payload->name);
2610         EXPECT_STREQ(OC_RSRVD_RESOURCE_TYPE_RES, payload->type->value);
2611         EXPECT_STREQ(OC_RSRVD_INTERFACE_LL, payload->iface->value);
2612         EXPECT_STREQ(OC_RSRVD_INTERFACE_DEFAULT, payload->iface->next->value);
2613
2614         for (OCResourcePayload *resource = payload->resources; resource; resource = resource->next)
2615         {
2616             if (0 == strcmp("/a/light", resource->uri))
2617             {
2618                 EXPECT_STREQ("/a/light", resource->uri);
2619                 EXPECT_STREQ("core.light", resource->types->value);
2620                 EXPECT_EQ(NULL, resource->types->next);
2621                 EXPECT_STREQ("oic.if.baseline", resource->interfaces->value);
2622                 EXPECT_EQ(NULL, resource->interfaces->next);
2623                 EXPECT_TRUE(resource->bitmap & OC_DISCOVERABLE);
2624                 EXPECT_FALSE(resource->secure);
2625                 EXPECT_EQ(0, resource->port);
2626                 EXPECT_EQ(NULL, resource->next);
2627             }
2628         }
2629     }
2630
2631     return OC_STACK_DELETE_TRANSACTION;
2632 }
2633
2634 static OCStackApplicationResult DiscoverLinkedListResource(void *ctx, OCDoHandle handle,
2635     OCClientResponse *response)
2636 {
2637     OC_UNUSED(ctx);
2638     OC_UNUSED(handle);
2639     EXPECT_EQ(OC_STACK_OK, response->result);
2640     EXPECT_TRUE(NULL != response->payload);
2641     if (NULL != response->payload)
2642     {
2643         EXPECT_EQ(PAYLOAD_TYPE_DISCOVERY, response->payload->type);
2644
2645         OCDiscoveryPayload *payload = (OCDiscoveryPayload *)response->payload;
2646         EXPECT_NE((char *)NULL, payload->sid);
2647         EXPECT_EQ(NULL, payload->name);
2648         EXPECT_EQ(NULL, payload->type);
2649         EXPECT_EQ(NULL, payload->iface);
2650
2651         for (OCResourcePayload *resource = payload->resources; resource; resource = resource->next)
2652         {
2653             if (0 == strcmp("/a/light", resource->uri))
2654             {
2655                 EXPECT_STREQ("/a/light", resource->uri);
2656                 EXPECT_STREQ("core.light", resource->types->value);
2657                 EXPECT_EQ(NULL, resource->types->next);
2658                 EXPECT_STREQ("oic.if.baseline", resource->interfaces->value);
2659                 EXPECT_EQ(NULL, resource->interfaces->next);
2660                 EXPECT_TRUE(resource->bitmap & OC_DISCOVERABLE);
2661                 EXPECT_FALSE(resource->secure);
2662                 EXPECT_EQ(0, resource->port);
2663                 EXPECT_EQ(NULL, resource->next);
2664             }
2665         }
2666     }
2667     return OC_STACK_DELETE_TRANSACTION;
2668 }
2669
2670
2671 static OCStackApplicationResult DiscoverResourceTypeResponse(void *ctx, OCDoHandle handle,
2672     OCClientResponse *response)
2673 {
2674     OC_UNUSED(ctx);
2675     OC_UNUSED(handle);
2676     EXPECT_EQ(OC_STACK_OK, response->result);
2677     EXPECT_TRUE(NULL != response->payload);
2678     if (NULL != response->payload)
2679     {
2680         EXPECT_EQ(PAYLOAD_TYPE_DISCOVERY, response->payload->type);
2681
2682         OCDiscoveryPayload *payload = (OCDiscoveryPayload *)response->payload;
2683         EXPECT_NE((char *)NULL, payload->sid);
2684         EXPECT_EQ(NULL, payload->name);
2685         EXPECT_EQ(NULL, payload->type);
2686         EXPECT_EQ(NULL, payload->iface);
2687         EXPECT_TRUE(NULL != payload->resources);
2688
2689         OCResourcePayload *resource = payload->resources;
2690
2691         if (0 == strcmp("/a/light", resource->uri))
2692         {
2693             EXPECT_STREQ("/a/light", resource->uri);
2694             EXPECT_STREQ("core.light", resource->types->value);
2695             EXPECT_EQ(NULL, resource->types->next);
2696             EXPECT_STREQ("oic.if.baseline", resource->interfaces->value);
2697             EXPECT_EQ(NULL, resource->interfaces->next);
2698             EXPECT_TRUE(resource->bitmap & OC_DISCOVERABLE);
2699             EXPECT_FALSE(resource->secure);
2700             EXPECT_EQ(0, resource->port);
2701             EXPECT_EQ(NULL, resource->next);
2702         }
2703     }
2704
2705     return OC_STACK_DELETE_TRANSACTION;
2706 }
2707
2708 static OCStackApplicationResult DiscoverUnicastErrorResponse(void *ctx, OCDoHandle handle,
2709     OCClientResponse *response)
2710 {
2711     OC_UNUSED(ctx);
2712     OC_UNUSED(handle);
2713     EXPECT_NE(OC_STACK_OK, response->result);
2714     EXPECT_TRUE(NULL == response->payload);
2715
2716     return OC_STACK_DELETE_TRANSACTION;
2717 }
2718
2719 // Disabled to unblock other developers untill IOT-1807 is done.
2720 TEST_F(OCDiscoverTests, DISABLED_DiscoverResourceWithValidQueries)
2721 {
2722     itst::DeadmanTimer killSwitch(LONG_TEST_TIMEOUT);
2723
2724     OCResourceHandle handles;
2725     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handles, "core.light", "oic.if.baseline", "/a/light",
2726         entityHandler, NULL, OC_DISCOVERABLE));
2727     OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, "StackTest");
2728
2729     itst::Callback discoverBaselineCB(&DiscoverBaselineResource);
2730     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?if=oic.if.baseline", NULL,
2731         0, CT_DEFAULT, OC_HIGH_QOS, discoverBaselineCB, NULL, 0));
2732     EXPECT_EQ(OC_STACK_OK, discoverBaselineCB.Wait(100));
2733
2734     // Disabled temporarily on Windows to unblock other developers. Will be enabled in IOT-1806.
2735 #ifndef _MSC_VER
2736     itst::Callback discoverDefaultCB(&DiscoverLinkedListResource);
2737     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res", NULL, 0, CT_DEFAULT,
2738         OC_HIGH_QOS, discoverDefaultCB, NULL, 0));
2739     EXPECT_EQ(OC_STACK_OK, discoverDefaultCB.Wait(100));
2740 #endif
2741
2742     itst::Callback discoverLinkedListCB(&DiscoverLinkedListResource);
2743     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?if=oic.if.ll", NULL, 0,
2744         CT_DEFAULT, OC_HIGH_QOS, discoverLinkedListCB, NULL, 0));
2745     EXPECT_EQ(OC_STACK_OK, discoverLinkedListCB.Wait(100));
2746
2747     itst::Callback discoverRTCB(&DiscoverResourceTypeResponse);
2748     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=core.light", NULL, 0,
2749         CT_DEFAULT, OC_HIGH_QOS, discoverRTCB, NULL, 0));
2750     EXPECT_EQ(OC_STACK_OK, discoverRTCB.Wait(100));
2751 }
2752
2753 // Disabled to unblock other developers untill IOT-1807 is done.
2754 TEST_F(OCDiscoverTests, DISABLED_DiscoverResourceWithInvalidQueries)
2755 {
2756     itst::DeadmanTimer killSwitch(LONG_TEST_TIMEOUT);
2757
2758     OCResourceHandle handles;
2759     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handles, "core.light", "oic.if.baseline", "/a/light",
2760         entityHandler, NULL, OC_DISCOVERABLE));
2761     OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, "deviceName", "StackTest");
2762
2763     itst::Callback discoverRTInvalidCB(&DiscoverUnicastErrorResponse);
2764     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=invalid", NULL, 0,
2765     CT_DEFAULT, OC_HIGH_QOS, discoverRTInvalidCB, NULL, 0));
2766     EXPECT_EQ(OC_STACK_OK, discoverRTInvalidCB.Wait(10));
2767
2768     itst::Callback discoverRTEmptyCB(&DiscoverUnicastErrorResponse);
2769     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=", NULL, 0, CT_DEFAULT,
2770     OC_HIGH_QOS, discoverRTEmptyCB, NULL, 0));
2771     EXPECT_EQ(OC_STACK_OK, discoverRTEmptyCB.Wait(10));
2772
2773     itst::Callback discoverIfInvalidCB(&DiscoverUnicastErrorResponse);
2774     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?if=invalid", NULL, 0,
2775         CT_DEFAULT, OC_HIGH_QOS, discoverIfInvalidCB, NULL, 0));
2776     EXPECT_EQ(OC_STACK_OK, discoverIfInvalidCB.Wait(10));
2777
2778     itst::Callback discoverIfEmptyCB(&DiscoverUnicastErrorResponse);
2779     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?if=", NULL, 0, CT_DEFAULT,
2780     OC_HIGH_QOS, discoverIfEmptyCB, NULL, 0));
2781     EXPECT_EQ(OC_STACK_OK, discoverIfEmptyCB.Wait(10));
2782
2783     // Unicast
2784     char targetUri[MAX_URI_LENGTH * 2] ={ 0, };
2785
2786     itst::Callback discoverUnicastIfInvalidCB(&DiscoverUnicastErrorResponse);
2787     snprintf(targetUri, MAX_URI_LENGTH * 2, "127.0.0.1/oic/res?if=invalid");
2788     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, targetUri, NULL, 0,
2789     CT_DEFAULT, OC_HIGH_QOS, discoverUnicastIfInvalidCB, NULL, 0));
2790     EXPECT_EQ(OC_STACK_OK, discoverUnicastIfInvalidCB.Wait(10));
2791
2792     itst::Callback discoverUnicastIfEmptyCB(&DiscoverUnicastErrorResponse);
2793     snprintf(targetUri, MAX_URI_LENGTH * 2, "127.0.0.1/oic/res?if=");
2794     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, targetUri, NULL, 0, CT_DEFAULT,
2795     OC_HIGH_QOS, discoverUnicastIfEmptyCB, NULL, 0));
2796     EXPECT_EQ(OC_STACK_OK, discoverUnicastIfEmptyCB.Wait(10));
2797
2798     itst::Callback discoverUnicastRTInvalidCB(&DiscoverUnicastErrorResponse);
2799     snprintf(targetUri, MAX_URI_LENGTH * 2, "127.0.0.1/oic/res?rt=invalid");
2800     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, targetUri, NULL, 0,
2801     CT_DEFAULT, OC_HIGH_QOS, discoverUnicastRTInvalidCB, NULL, 0));
2802     EXPECT_EQ(OC_STACK_OK, discoverUnicastRTInvalidCB.Wait(10));
2803
2804     itst::Callback discoverUnicastRTEmptyCB(&DiscoverUnicastErrorResponse);
2805     snprintf(targetUri, MAX_URI_LENGTH * 2, "127.0.0.1/oic/res?rt=");
2806     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, targetUri, NULL, 0, CT_DEFAULT,
2807     OC_HIGH_QOS, discoverUnicastRTEmptyCB, NULL, 0));
2808     EXPECT_EQ(OC_STACK_OK, discoverUnicastRTEmptyCB.Wait(10));
2809 }
2810
2811 class OCEndpointTests : public testing::Test
2812 {
2813     protected:
2814         virtual void SetUp()
2815         {
2816             OCPersistentStorage ps = { fopen, fread, fwrite, fclose, unlink };
2817             EXPECT_EQ(OC_STACK_OK, OCRegisterPersistentStorageHandler(&ps));
2818             EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_CLIENT_SERVER));
2819         }
2820
2821         virtual void TearDown()
2822         {
2823             OCStop();
2824         }
2825 };
2826
2827 static OCStackApplicationResult SecureAndNonsecureEndpoints(void *ctx, OCDoHandle handle,
2828     OCClientResponse *response)
2829 {
2830     OC_UNUSED(ctx);
2831     OC_UNUSED(handle);
2832     EXPECT_EQ(OC_STACK_OK, response->result);
2833     EXPECT_TRUE(NULL != response->payload);
2834     if (NULL != response->payload)
2835     {
2836         EXPECT_EQ(PAYLOAD_TYPE_DISCOVERY, response->payload->type);
2837         OCDiscoveryPayload *payload = (OCDiscoveryPayload *)response->payload;
2838         EXPECT_TRUE(NULL != payload->sid);
2839         for (OCResourcePayload *resource = payload->resources; resource; resource = resource->next)
2840         {
2841             if (!strcmp("/a/default", resource->uri))
2842             {
2843                 for (OCEndpointPayload *ep = resource->eps; ep; ep = ep->next)
2844                 {
2845                     EXPECT_EQ(0, OC_FLAG_SECURE & ep->family);
2846                 }
2847             }
2848             else if (!strcmp("/a/secure", resource->uri))
2849             {
2850                 for (OCEndpointPayload *ep = resource->eps; ep; ep = ep->next)
2851                 {
2852 #ifdef __WITH_DTLS__
2853                     EXPECT_EQ(OC_FLAG_SECURE, OC_FLAG_SECURE & ep->family);
2854 #else
2855                     EXPECT_EQ(0, OC_FLAG_SECURE & ep->family);
2856 #endif
2857                 }
2858             }
2859             else if (!strcmp("/a/nonsecure", resource->uri))
2860             {
2861                 for (OCEndpointPayload *ep = resource->eps; ep; ep = ep->next)
2862                 {
2863                     EXPECT_EQ(0, OC_FLAG_SECURE & ep->family);
2864                 }
2865             }
2866             else if (!strcmp("/a/both", resource->uri))
2867             {
2868                 bool hasSecure = false;
2869                 bool hasNonsecure = false;
2870                 for (OCEndpointPayload *ep = resource->eps; ep; ep = ep->next)
2871                 {
2872                     if (OC_FLAG_SECURE & ep->family)
2873                     {
2874                         hasSecure = true;
2875                     }
2876                     else
2877                     {
2878                         hasNonsecure = true;
2879                     }
2880                 }
2881 #ifdef __WITH_DTLS__
2882                 EXPECT_TRUE(hasSecure);
2883 #else
2884                 EXPECT_FALSE(hasSecure);
2885 #endif
2886                 EXPECT_TRUE(hasNonsecure);
2887             }
2888         }
2889     }
2890     return OC_STACK_DELETE_TRANSACTION;
2891 }
2892
2893 // Disabled until unit tests can run with SECURED=1 builds
2894 TEST_F(OCEndpointTests, DISABLED_SecureAndNonsecureEndpoints)
2895 {
2896     itst::DeadmanTimer killSwitch(LONG_TEST_TIMEOUT);
2897
2898     OCResourceHandle handle;
2899     handle = OCGetResourceHandleAtUri(OC_RSRVD_WELL_KNOWN_URI);
2900     EXPECT_EQ(OC_STACK_OK, OCSetResourceProperties(handle, OC_DISCOVERABLE | OC_OBSERVABLE));
2901     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light", "oic.if.baseline", "/a/default",
2902             entityHandler, NULL, OC_DISCOVERABLE));
2903     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light", "oic.if.baseline", "/a/secure",
2904             entityHandler, NULL, OC_DISCOVERABLE | OC_SECURE));
2905     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light", "oic.if.baseline", "/a/nonsecure",
2906             entityHandler, NULL, OC_DISCOVERABLE | OC_NONSECURE));
2907     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light", "oic.if.baseline", "/a/both",
2908             entityHandler, NULL, OC_DISCOVERABLE | OC_SECURE | OC_NONSECURE));
2909
2910     itst::Callback secureAndNonSecureEndpointsCB(&SecureAndNonsecureEndpoints);
2911     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res", NULL,
2912             0, CT_DEFAULT, OC_HIGH_QOS, secureAndNonSecureEndpointsCB, NULL, 0));
2913     EXPECT_EQ(OC_STACK_OK, secureAndNonSecureEndpointsCB.Wait(100));
2914 }
2915
2916 #ifdef IP_ADAPTER
2917 TEST(StackZoneId, getZoneId)
2918 {
2919     size_t tempSize = 0;
2920     CAEndpoint_t *tempInfo = NULL;
2921     CAGetNetworkInformation(&tempInfo, &tempSize);
2922
2923     for (size_t i = 0; i < tempSize; i++)
2924     {
2925         char *zoneId = NULL;
2926         EXPECT_EQ(OC_STACK_OK, OCGetLinkLocalZoneId(tempInfo[i].ifindex, &zoneId));
2927         OICFree(zoneId);
2928         zoneId = NULL;
2929     }
2930
2931     OICFree(tempInfo);
2932 }
2933
2934 TEST(StackZoneId, getZoneIdWithInvalidParams)
2935 {
2936     char *zoneId = NULL;
2937     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCGetLinkLocalZoneId(0, NULL));
2938     EXPECT_EQ(OC_STACK_ERROR, OCGetLinkLocalZoneId(9999, &zoneId));
2939     EXPECT_EQ(OC_STACK_ERROR, OCGetLinkLocalZoneId(UINT32_MAX, &zoneId));
2940 }
2941 #endif
2942
2943 TEST(LinksPayloadValue, createLinksPayloadValue)
2944 {
2945     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
2946     OIC_LOG(INFO, TAG, "Starting createLinksPayloadValue test");
2947     InitStack(OC_SERVER);
2948
2949     size_t numResources = 0;
2950     uint8_t inBitmap[3] = { OC_DISCOVERABLE | OC_OBSERVABLE,
2951                             OC_DISCOVERABLE | OC_OBSERVABLE,
2952                             OC_DISCOVERABLE };
2953     int64_t outBitmap[3] = {0};
2954
2955     OCResourceHandle containerHandle;
2956     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&containerHandle,
2957         "core.led",
2958         "core.col",
2959         "/a/kitchen",
2960         0,
2961         NULL,
2962         inBitmap[0]));
2963     ++numResources;
2964
2965     OCResourceHandle handle0;
2966     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle0,
2967         "core.led",
2968         "core.rw",
2969         "/a/led0",
2970         0,
2971         NULL,
2972         inBitmap[1]));
2973     ++numResources;
2974
2975     OCResourceHandle handle1;
2976     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
2977         "core.led",
2978         "core.r",
2979         "/a/led1",
2980         0,
2981         NULL,
2982         inBitmap[2]));
2983     ++numResources;
2984
2985     EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle0));
2986     EXPECT_EQ(OC_STACK_OK, OCBindResource(containerHandle, handle1));
2987
2988     EXPECT_EQ(handle0, OCGetResourceHandleFromCollection(containerHandle, 0));
2989     EXPECT_EQ(handle1, OCGetResourceHandleFromCollection(containerHandle, 1));
2990
2991     OCRepPayloadValue* linksRepPayloadValue;
2992     OCDevAddr* devAddr = NULL;
2993     EXPECT_EQ(OC_STACK_OK, OCLinksPayloadValueCreate("/a/kitchen", &linksRepPayloadValue, devAddr));
2994     ASSERT_TRUE(NULL != linksRepPayloadValue);
2995
2996     OCRepPayload *collectionPayload = OCRepPayloadCreate();
2997     ASSERT_TRUE(NULL != collectionPayload);
2998
2999     size_t dim[MAX_REP_ARRAY_DEPTH] = { numResources, 0, 0 };
3000
3001     ASSERT_TRUE(OCRepPayloadSetPropObjectArrayAsOwner(collectionPayload, OC_RSRVD_LINKS,
3002                                               linksRepPayloadValue->arr.objArray, dim));
3003
3004     OCRepPayload *policyMap = NULL;
3005     OCRepPayload **linksMap = NULL;
3006     ASSERT_TRUE(OCRepPayloadGetPropObjectArray(collectionPayload, OC_RSRVD_LINKS, &linksMap, dim));
3007
3008     for (size_t i = 0; i < numResources; i++)
3009     {
3010         ASSERT_TRUE(OCRepPayloadGetPropObject(linksMap[i], OC_RSRVD_POLICY, &policyMap));
3011         ASSERT_TRUE(OCRepPayloadGetPropInt(policyMap, OC_RSRVD_BITMAP, &outBitmap[i]));
3012         EXPECT_EQ(inBitmap[i], outBitmap[i]);
3013
3014         if (devAddr)
3015         {
3016 #ifdef TCP_ADAPTER
3017 #ifdef __WITH_TLS__
3018             // tls
3019             int64_t outTlsPort = 0;
3020             ASSERT_TRUE(OCRepPayloadGetPropInt(policyMap, OC_RSRVD_TLS_PORT, &outTlsPort));
3021
3022             uint16_t tlsPort = 0;
3023             GetTCPPortInfo(devAddr, &tlsPort, true);
3024
3025             EXPECT_EQ(tlsPort, outTlsPort);
3026 #else
3027             // tcp
3028             int64_t outTcpPort = 0;
3029             ASSERT_TRUE(OCRepPayloadGetPropInt(policyMap, OC_RSRVD_TCP_PORT, &outTcpPort));
3030
3031             uint16_t tcpPort = 0;
3032             GetTCPPortInfo(devAddr, &tcpPort, false);
3033
3034             EXPECT_EQ(tcpPort, outTcpPort);
3035 #endif
3036 #endif
3037         }
3038         OCRepPayloadDestroy(linksMap[i]);
3039     }
3040
3041     OICFree(linksMap);
3042     OCRepPayloadDestroy(policyMap);
3043     OCRepPayloadDestroy(collectionPayload);
3044
3045     EXPECT_EQ(OC_STACK_OK, OCStop());
3046 }
3047
3048 TEST(DiagnosticPayload, CreateDestroy)
3049 {
3050     OCDiagnosticPayload *payload;
3051
3052     payload = NULL;
3053     OCDiagnosticPayloadDestroy(payload);
3054
3055     payload = OCDiagnosticPayloadCreate(NULL);
3056     ASSERT_TRUE(payload == NULL);
3057
3058     payload = OCDiagnosticPayloadCreate("message");
3059     ASSERT_TRUE(payload != NULL);
3060     ASSERT_STREQ("message", payload->message);
3061     OCDiagnosticPayloadDestroy(payload);
3062 }
3063
3064 static OCEntityHandlerResult DiagnosticPayloadRequest(OCEntityHandlerFlag flag,
3065         OCEntityHandlerRequest *request, void *ctx)
3066 {
3067     OC_UNUSED(flag);
3068     OC_UNUSED(ctx);
3069     OCEntityHandlerResponse response;
3070     memset(&response, 0, sizeof(response));
3071     response.requestHandle = request->requestHandle;
3072     response.resourceHandle = request->resource;
3073     response.ehResult = OC_EH_BAD_REQ;
3074     response.payload = (OCPayload*) OCDiagnosticPayloadCreate("message");
3075     EXPECT_TRUE(response.payload != NULL);
3076     EXPECT_EQ(OC_STACK_OK, OCDoResponse(&response));
3077     return OC_EH_OK;
3078 }
3079
3080 static OCStackApplicationResult DiagnosticPayloadResponse(void *ctx, OCDoHandle handle,
3081         OCClientResponse *response)
3082 {
3083     OC_UNUSED(ctx);
3084     OC_UNUSED(handle);
3085     EXPECT_EQ(OC_STACK_INVALID_QUERY, response->result);
3086     EXPECT_EQ(PAYLOAD_TYPE_DIAGNOSTIC, response->payload->type);
3087     OCDiagnosticPayload *payload = (OCDiagnosticPayload*) response->payload;
3088     EXPECT_STREQ("message", payload->message);
3089     return OC_STACK_DELETE_TRANSACTION;
3090 }
3091
3092 TEST(DiagnosticPayload, DISABLED_EndToEnd)
3093 {
3094     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
3095     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
3096
3097     OCResourceHandle handle;
3098     EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light", "oic.if.baseline", "/a/light",
3099             DiagnosticPayloadRequest, NULL, OC_DISCOVERABLE));
3100
3101     itst::Callback diagnosticPayloadCB(&DiagnosticPayloadResponse);
3102     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_GET, "127.0.0.1:5683/a/light", NULL,
3103             0, CT_DEFAULT, OC_HIGH_QOS, diagnosticPayloadCB, NULL, 0));
3104     EXPECT_EQ(OC_STACK_OK, diagnosticPayloadCB.Wait(100));
3105
3106     OCStop();
3107 }
3108
3109 // Mostly copy-paste from ca_api_unittest.cpp
3110 TEST(OCIpv6ScopeLevel, getMulticastScope)
3111 {
3112     const char interfaceLocalStart[] = "ff01::";
3113     const char linkLocalStart[]      = "ff02::";
3114     const char realmLocalStart[]     = "ff03::";
3115     const char adminLocalStart[]     = "ff04::";
3116     const char siteLocalStart[]      = "ff05::";
3117     const char orgLocalStart[]       = "ff08::";
3118     const char globalStart[]         = "ff0e::";
3119
3120     const char interfaceLocalMid[] = "ff81:0000:0000:f000:0000:0000:0000:0000";
3121     const char linkLocalMid[]      = "ff82:0000:0000:f000:0000:0000:0000:0000";
3122     const char realmLocalMid[]     = "ff83:0000:0000:f000:0000:0000:0000:0000";