Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / unittests / OCPlatformTest.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 #include <OCPlatform.h>
22 #include <OCApi.h>
23 #include <oic_malloc.h>
24 #include <gtest/gtest.h>
25
26 namespace OCPlatformTest
27 {
28     using namespace OC;
29
30     static const char* SVR_DB_FILE_NAME = "./oic_svr_db_server.dat";
31     const OCResourceHandle HANDLE_ZERO = 0;
32     const std::string gResourceTypeName = "core.res";
33     const std::string gResourceInterface = DEFAULT_INTERFACE;
34     const uint8_t gResourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
35     OCResourceHandle resourceHandle;
36
37     //OCPersistent Storage Handlers
38     static FILE* client_open(const char * /*path*/, const char *mode)
39     {
40         std::cout << "<===Opening SVR DB file = './oic_svr_db_client.dat' with mode = '" << mode
41                 << "' " << std::endl;
42         return fopen(SVR_DB_FILE_NAME, mode);
43     }
44     OCPersistentStorage gps {client_open, fread, fwrite, fclose, unlink };
45
46     // Callbacks
47     OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
48     {
49         return OC_EH_OK;
50     }
51
52     void foundResource(std::shared_ptr<OCResource> /*resource*/)
53     {
54     }
55
56     void receivedDeviceInfo(const OCRepresentation& /*rep*/)
57     {
58     }
59
60     void presenceHandler(OCStackResult /*result*/,
61             const unsigned int /*nonce*/, const std::string& /*hostAddress*/)
62     {
63     }
64
65     void directPairHandler(std::shared_ptr<OCDirectPairing> /*dev*/, OCStackResult /*res*/)
66     {
67     }
68
69     void pairedHandler(const PairedDevices& /*list*/)
70     {
71     }
72
73     //Helper methods
74     void DeleteStringLL(OCStringLL* ll)
75     {
76         if (!ll)
77         {
78             return;
79         }
80
81         DeleteStringLL(ll->next);
82         delete[] ll->value;
83         OICFree(ll);
84     }
85
86     void DeleteDeviceInfo(OCDeviceInfo deviceInfo)
87     {
88         delete[] deviceInfo.deviceName;
89         DeleteStringLL(deviceInfo.types);
90         delete[] deviceInfo.specVersion;
91         DeleteStringLL(deviceInfo.dataModelVersions);
92     }
93
94     void DuplicateString(char ** targetString, std::string sourceString)
95     {
96         *targetString = new char[sourceString.length() + 1];
97         strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
98     }
99
100     bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, std::string value)
101     {
102         char *dup = NULL;
103         DuplicateString(&dup, value);
104         if (!*stringLL)
105         {
106             *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
107             (*stringLL)->value = dup;
108             return true;
109         }
110         else
111         {
112             OCStringLL *temp = *stringLL;
113             while(temp->next)
114             {
115                 temp = temp->next;
116             }
117             temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
118             temp->next->value = dup;
119             return true;
120         }
121         return false;
122     }
123
124     OCResourceHandle RegisterResource(std::string uri, std::string type, std::string iface)
125     {
126         PlatformConfig cfg
127         { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
128                 OC::QualityOfService::LowQos, &gps };
129         OCPlatform::Configure(cfg);
130         EXPECT_EQ(OC_STACK_OK,OCPlatform::registerResource(
131                                         resourceHandle, uri, type,
132                                         iface, entityHandler, gResourceProperty));
133         return resourceHandle;
134     }
135
136     OCResourceHandle RegisterResource(std::string uri, std::string type)
137     {
138         PlatformConfig cfg
139         { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
140                 OC::QualityOfService::LowQos, &gps };
141         OCPlatform::Configure(cfg);
142         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
143                                         resourceHandle, uri, type,
144                                         gResourceInterface, entityHandler, gResourceProperty));
145         return resourceHandle;
146     }
147
148     OCResourceHandle RegisterResource(std::string uri)
149     {
150         PlatformConfig cfg
151         { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
152                 OC::QualityOfService::LowQos, &gps };
153         OCPlatform::Configure(cfg);
154         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
155                                         resourceHandle, uri, gResourceTypeName,
156                                         gResourceInterface, entityHandler, gResourceProperty));
157         return resourceHandle;
158     }
159
160     //Configure
161     // Enable it when the stack throw an exception
162     // https://jira.iotivity.org/browse/IOT-428
163     TEST(ConfigureTest, DISABLED_ConfigureInvalidModeType)
164     {
165         PlatformConfig cfg {
166              OC::ServiceType::InProc,
167             (OC::ModeType)99,
168              "0.0.0.0",
169              0,
170              OC::QualityOfService::LowQos
171          };
172          OCPlatform::Configure(cfg);
173          EXPECT_ANY_THROW(OCPlatform::setDefaultDeviceEntityHandler(NULL));
174      }
175
176     // Enable it when the stack throw an exception
177     // https://jira.iotivity.org/browse/IOT-428
178     TEST(ConfigureTest, DISABLED_ConfigureInvalidServiceType)
179     {
180         PlatformConfig cfg {
181              (OC::ServiceType)99,
182             OC::ModeType::Client,
183              "0.0.0.0",
184              0,
185              OC::QualityOfService::LowQos
186          };
187          OCPlatform::Configure(cfg);
188          EXPECT_ANY_THROW(OCPlatform::setDefaultDeviceEntityHandler(NULL));
189      }
190
191     // Enable it when the stack throw an exception
192     // https://jira.iotivity.org/browse/IOT-428
193     TEST(ConfigureTest, DISABLED_ConfigureClientOutProc)
194     {
195         PlatformConfig cfg {
196             OC::ServiceType::OutOfProc,
197             OC::ModeType::Client,
198             "0.0.0.0",
199             0,
200             OC::QualityOfService::LowQos
201         };
202         std::string uri = "/a/light66";
203         std::string type = "core.light";
204         uint8_t gResourceProperty = 0;
205         OCPlatform::Configure(cfg);
206         EXPECT_ANY_THROW(OCPlatform::registerResource(
207              resourceHandle, uri, type,
208              gResourceInterface, entityHandler, gResourceProperty));
209     }
210
211     TEST(ConfigureTest, ConfigureServerOutProc)
212     {
213         PlatformConfig cfg
214         {
215             OC::ServiceType::OutOfProc,
216             OC::ModeType::Server,
217             "0.0.0.0",
218             0,
219             OC::QualityOfService::LowQos, &gps
220         };
221         std::string uri = "/a/light67";
222         std::string type = "core.light";
223         uint8_t gResourceProperty = 0;
224         OCPlatform::Configure(cfg);
225
226         EXPECT_ANY_THROW(OCPlatform::registerResource(
227              resourceHandle, uri, type,
228              gResourceInterface, entityHandler, gResourceProperty));
229     }
230
231     TEST(ConfigureTest, ConfigureDefault)
232     {
233         std::string uri = "/a/light68";
234         std::string type = "core.light";
235         uint8_t gResourceProperty = 0;
236         PlatformConfig cfg = {};
237         OCPlatform::Configure(cfg);
238
239         EXPECT_NO_THROW(OCPlatform::registerResource(
240                  resourceHandle, uri, type,
241                  gResourceInterface, entityHandler, gResourceProperty));
242     }
243
244     TEST(ConfigureTest, ConfigureServer)
245     {
246         std::string uri = "/a/light69";
247         std::string type = "core.light";
248         uint8_t gResourceProperty = 0;
249         PlatformConfig cfg {
250             OC::ServiceType::InProc,
251             OC::ModeType::Server,
252             "0.0.0.0",
253             0,
254             OC::QualityOfService::LowQos, &gps
255         };
256         OCPlatform::Configure(cfg);
257
258         EXPECT_NO_THROW(OCPlatform::registerResource(
259                  resourceHandle, uri, type,
260                  gResourceInterface, entityHandler, gResourceProperty));
261     }
262
263     TEST(ConfigureTest, ConfigureClient)
264     {
265         std::string uri = "/a/light70";
266         std::string type = "core.light";
267         uint8_t gResourceProperty = 0;
268         PlatformConfig cfg
269         {
270             OC::ServiceType::InProc,
271             OC::ModeType::Client,
272             "0.0.0.0",
273             0,
274             OC::QualityOfService::LowQos,
275             &gps
276         };
277
278         EXPECT_NO_THROW(OCPlatform::registerResource(
279                  resourceHandle, uri, type,
280                  gResourceInterface, entityHandler, gResourceProperty));
281     }
282     //PersistentStorageTest
283     TEST(ConfigureTest, ConfigureDefaultNULLPersistentStorage)
284     {
285         PlatformConfig cfg {
286              OC::ServiceType::InProc,
287              OC::ModeType::Both,
288              "0.0.0.0",
289              0,
290              OC::QualityOfService::LowQos
291          };
292          OCPlatform::Configure(cfg);
293          EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
294      }
295
296     //PersistentStorageTest
297     TEST(ConfigureTest, ConfigureNULLPersistentStorage)
298     {
299         PlatformConfig cfg {
300              OC::ServiceType::InProc,
301              OC::ModeType::Both,
302              "0.0.0.0",
303              0,
304              OC::QualityOfService::LowQos,
305              nullptr
306          };
307          OCPlatform::Configure(cfg);
308          EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
309      }
310
311     //PersistentStorageTest
312     TEST(ConfigureTest, ConfigurePersistentStorage)
313     {
314          PlatformConfig cfg {
315              OC::ServiceType::InProc,
316              OC::ModeType::Both,
317              "0.0.0.0",
318              0,
319              OC::QualityOfService::LowQos,
320              &gps
321          };
322          OCPlatform::Configure(cfg);
323          EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
324      }
325
326     //PersistentStorageTest
327     TEST(ConfigureTest, ConfigureNULLHandlersPersistentStorage)
328     {
329         PlatformConfig cfg {
330              OC::ServiceType::InProc,
331              OC::ModeType::Both,
332              "0.0.0.0",
333              0,
334              OC::QualityOfService::LowQos,
335              &gps
336          };
337          OCPlatform::Configure(cfg);
338          EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
339      }
340
341
342     //RegisterResourceTest
343     TEST(RegisterResourceTest, RegisterSingleResource)
344     {
345         std::string uri = "/a/res2";
346         EXPECT_NE(HANDLE_ZERO, RegisterResource(uri));
347     }
348
349     TEST(RegisterResourceTest, RegisterMultipleResources)
350     {
351         std::string uri = "/a/multi";
352         //Good enough for 5 resources.
353         for(int i=0; i< 5; i++)
354         {
355             uri +=std::to_string(i);
356             EXPECT_NE(HANDLE_ZERO, RegisterResource(uri));
357         }
358     }
359
360     TEST(RegisterResourceTest, ReregisterResource)
361     {
362         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light5"),
363             std::string("core.light"));
364         EXPECT_EQ(OC_STACK_OK, OC::OCPlatform::unregisterResource(resourceHandle));
365
366         EXPECT_NE(HANDLE_ZERO, RegisterResource(std::string("/a/light5"),
367             std::string("core.light")));
368
369     }
370
371     TEST(RegisterResourceTest, RegisterEmptyResource)
372     {
373         // We should not allow empty URI.
374         std::string emptyStr = "";
375         EXPECT_ANY_THROW(OCPlatform::registerResource(resourceHandle, emptyStr, emptyStr,
376                                         emptyStr, entityHandler, gResourceProperty));
377     }
378
379     TEST(RegisterResourceTest, RegisterZeroResourceProperty)
380     {
381         std::string uri = "/a/light6";
382         std::string type = "core.light";
383         uint8_t gResourceProperty = 0;
384         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
385                 resourceHandle, uri, type,
386                 gResourceInterface, entityHandler, gResourceProperty));
387     }
388
389     //UnregisterTest
390     TEST(UnregisterTest, UnregisterZeroHandleValue)
391     {
392         EXPECT_ANY_THROW(OC::OCPlatform::unregisterResource(HANDLE_ZERO));
393     }
394
395     //UnbindResourcesTest
396     TEST(UnbindResourcesTest, UnbindResources)
397     {
398         OCResourceHandle resourceHome = RegisterResource(std::string("a/home"),
399             std::string("core.home"));
400         OCResourceHandle resourceKitchen = RegisterResource(std::string("a/kitchen"),
401             std::string("core.kitchen"), LINK_INTERFACE);
402         OCResourceHandle resourceRoom = RegisterResource(std::string("a/office"),
403             std::string("core.office"), LINK_INTERFACE);
404
405         std::vector<OCResourceHandle> rList;
406         rList.push_back(resourceKitchen);
407         rList.push_back(resourceRoom);
408         EXPECT_EQ(OC_STACK_OK, OCPlatform::bindResources(resourceHome, rList));
409         EXPECT_EQ(OC_STACK_OK, OCPlatform::unbindResources(resourceHome, rList));
410     }
411
412     TEST(UnbindResourcesTest, UnbindResourcesWithZero)
413     {
414         OCResourceHandle resourceHandle1 = 0;
415         OCResourceHandle resourceHandle2 = 0;
416         OCResourceHandle resourceHandle3 = 0;
417
418         std::vector<OCResourceHandle> rList;
419
420         rList.push_back(resourceHandle2);
421         rList.push_back(resourceHandle3);
422
423         EXPECT_ANY_THROW(OCPlatform::unbindResources(resourceHandle1, rList));
424     }
425
426     //BindInterfaceToResourceTest
427     TEST(BindInterfaceToResourceTest, BindResourceInterface)
428     {
429         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light"),
430             std::string("core.light"));
431         OCStackResult result = OC::OCPlatform::bindInterfaceToResource(resourceHandle,
432             BATCH_INTERFACE);
433         EXPECT_EQ(OC_STACK_OK, result);
434     }
435
436     TEST(BindInterfaceToResourceTest, BindZeroResourceInterface)
437     {
438         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light1"),
439             std::string("core.light"));
440         EXPECT_ANY_THROW(OC::OCPlatform::bindInterfaceToResource(resourceHandle, 0));
441     }
442
443     //BindTypeToResourceTest
444     TEST(BindTypeToResourceTest, BindResourceType)
445     {
446         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light3"),
447             std::string("core.light"));
448         OCStackResult result = OC::OCPlatform::bindTypeToResource(resourceHandle,
449             "core.brightlight");
450         EXPECT_EQ(OC_STACK_OK, result);
451     }
452
453     TEST(BindTypeToResourceTest, BindZeroResourceType)
454     {
455         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light4"),
456             std::string("core.light"));
457         EXPECT_ANY_THROW(OC::OCPlatform::bindTypeToResource(resourceHandle, 0));
458     }
459
460     //UnbindResourceTest
461     TEST(UnbindResourceTest, BindAndUnbindResource)
462     {
463         OCResourceHandle resourceHandle1 = RegisterResource(std::string("a/unres"),
464             std::string("core.unres"));
465         OCResourceHandle resourceHandle2 = RegisterResource(std::string("a/unres2"),
466             std::string("core.unres"), LINK_INTERFACE);
467
468         EXPECT_EQ(OC_STACK_OK, OCPlatform::bindResource(resourceHandle1, resourceHandle2));
469         EXPECT_EQ(OC_STACK_OK, OCPlatform::unbindResource(resourceHandle1, resourceHandle2));
470     }
471
472     //PresenceTest
473     TEST(PresenceTest, DISABLED_StartAndStopPresence)
474     {
475         EXPECT_EQ(OC_STACK_OK, OCPlatform::startPresence(30));
476         EXPECT_NE(HANDLE_ZERO, RegisterResource( std::string("/a/Presence"),
477             std::string("core.Presence")));
478         EXPECT_EQ(OC_STACK_OK, OCPlatform::stopPresence());
479     }
480
481     TEST(OCPlatformTest, UnbindZeroRsourceHandleValue)
482     {
483         EXPECT_ANY_THROW(OCPlatform::unbindResource(HANDLE_ZERO, HANDLE_ZERO));
484     }
485
486     //NotifyAllObserverTest
487     TEST(NotifyAllObserverTest, NotifyAllObservers)
488     {
489         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs1"),
490             std::string("core.obs"));
491         EXPECT_EQ(OC_STACK_NO_OBSERVERS, OCPlatform::notifyAllObservers(resourceHome));
492     }
493
494     TEST(NotifyAllObserverTest, NotifyAllObserversWithLowQos)
495     {
496         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs2"),
497             std::string("core.obs"));
498         EXPECT_EQ(OC_STACK_NO_OBSERVERS, OCPlatform::notifyAllObservers(resourceHome,
499                 OC::QualityOfService::LowQos));
500     }
501
502     TEST(NotifyAllObserverTest, NotifyAllObserversWithMidQos)
503     {
504         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs3"),
505             std::string("core.obs"));
506         EXPECT_EQ(OC_STACK_NO_OBSERVERS, OCPlatform::notifyAllObservers(resourceHome,
507                 OC::QualityOfService::MidQos));
508     }
509
510     TEST(NotifyAllObserverTest, NotifyAllObserversWithNaQos)
511     {
512         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs4"),
513             std::string("core.obs"));
514         EXPECT_EQ(OC_STACK_NO_OBSERVERS, OCPlatform::notifyAllObservers(resourceHome,
515                 OC::QualityOfService::NaQos));
516     }
517
518     TEST(NotifyAllObserverTest, NotifyAllObserversWithHighQos)
519     {
520         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs5"),
521             std::string("core.obs"));
522         EXPECT_EQ(OC_STACK_NO_OBSERVERS, OCPlatform::notifyAllObservers(resourceHome,
523                 OC::QualityOfService::HighQos));
524     }
525
526     TEST(NotifyAllObserverTest, NotifyListOfObservers)
527     {
528         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs6"),
529             std::string("core.obs"));
530
531         std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
532         ObservationIds interestedObservers;
533         EXPECT_ANY_THROW(OCPlatform::notifyListOfObservers(resourceHome,
534             interestedObservers, resourceResponse));
535     }
536
537     TEST(NotifyAllObserverTest, NotifyListOfObserversWithLowQos)
538     {
539         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs7"),
540             std::string("core.obs"));
541
542         std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
543         ObservationIds interestedObservers;
544         EXPECT_ANY_THROW(OCPlatform::notifyListOfObservers(resourceHome,
545             interestedObservers, resourceResponse,OC::QualityOfService::LowQos));
546     }
547
548     TEST(NotifyAllObserverTest, NotifyListOfObserversWithMidQos)
549     {
550         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs8"),
551             std::string("core.obs"));
552
553         std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
554         ObservationIds interestedObservers;
555         EXPECT_ANY_THROW(OCPlatform::notifyListOfObservers(resourceHome,
556             interestedObservers, resourceResponse,OC::QualityOfService::MidQos));
557     }
558
559     TEST(NotifyAllObserverTest, NotifyListOfObserversWithNaQos)
560     {
561         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs9"),
562             std::string("core.obs"));
563
564         std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
565         ObservationIds interestedObservers;
566         EXPECT_ANY_THROW(OCPlatform::notifyListOfObservers(resourceHome,
567             interestedObservers, resourceResponse,OC::QualityOfService::NaQos));
568     }
569
570     TEST(NotifyAllObserverTest, NotifyListOfObserversWithHighQos)
571     {
572         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs10"),
573             std::string("core.obs"));
574
575         std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
576         ObservationIds interestedObservers;
577         EXPECT_ANY_THROW(OCPlatform::notifyListOfObservers(resourceHome,
578             interestedObservers, resourceResponse,OC::QualityOfService::HighQos));
579     }
580
581     //DeviceEntityHandlerTest
582     TEST(DeviceEntityHandlerTest, SetDefaultDeviceEntityHandler)
583     {
584         EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(entityHandler));
585         EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(NULL));
586     }
587
588
589     //FindResource test
590     TEST(FindResourceTest, DISABLED_FindResourceValid)
591     {
592       std::ostringstream requestURI;
593       requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
594       EXPECT_EQ(OC_STACK_OK, OCPlatform::findResource("", requestURI.str(),
595               CT_DEFAULT, &foundResource));
596     }
597
598     TEST(FindResourceTest, FindResourceNullResourceURI)
599     {
600       EXPECT_ANY_THROW(OCPlatform::findResource("", nullptr,
601               CT_DEFAULT, &foundResource));
602     }
603
604     TEST(FindResourceTest, FindResourceNullResourceURI1)
605     {
606       std::ostringstream requestURI;
607       requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
608       EXPECT_ANY_THROW(OCPlatform::findResource(nullptr, requestURI.str(),
609               CT_DEFAULT, &foundResource));
610     }
611
612     TEST(FindResourceTest, FindResourceNullHost)
613     {
614       std::ostringstream requestURI;
615       requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
616       EXPECT_ANY_THROW(OCPlatform::findResource(nullptr, requestURI.str(),
617               CT_DEFAULT, &foundResource));
618     }
619
620     TEST(FindResourceTest, FindResourceNullresourceHandler)
621     {
622       std::ostringstream requestURI;
623       requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
624       EXPECT_THROW(OCPlatform::findResource("", requestURI.str(),
625               CT_DEFAULT, NULL), OC::OCException);
626     }
627
628     TEST(FindResourceTest, DISABLED_FindResourceWithLowQoS)
629     {
630         std::ostringstream requestURI;
631         requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
632         EXPECT_EQ(OC_STACK_OK,
633                 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource,
634                         OC::QualityOfService::LowQos));
635     }
636
637     TEST(FindResourceTest, DISABLED_FindResourceWithMidQos)
638     {
639         std::ostringstream requestURI;
640         requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
641         EXPECT_EQ(OC_STACK_OK,
642                 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource,
643                         OC::QualityOfService::MidQos));
644     }
645
646     TEST(FindResourceTest, DISABLED_FindResourceWithHighQos)
647     {
648         std::ostringstream requestURI;
649         requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
650         EXPECT_EQ(OC_STACK_OK,
651                 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource,
652                         OC::QualityOfService::HighQos));
653     }
654
655     TEST(FindResourceTest, DISABLED_FindResourceWithNaQos)
656     {
657         std::ostringstream requestURI;
658         requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
659         EXPECT_EQ(OC_STACK_OK,
660                 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource,
661                         OC::QualityOfService::NaQos));
662     }
663
664     //GetDeviceInfo Test
665     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithValidParameters)
666     {
667         std::string deviceDiscoveryURI = "/oic/d";
668         PlatformConfig cfg;
669         OCPlatform::Configure(cfg);
670         std::ostringstream requestURI;
671         requestURI << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
672         EXPECT_EQ(OC_STACK_OK,
673                 OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, &receivedDeviceInfo));
674     }
675
676     TEST(GetDeviceInfoTest, GetDeviceInfoNullDeviceURI)
677     {
678         PlatformConfig cfg;
679         OCPlatform::Configure(cfg);
680         EXPECT_ANY_THROW(
681                 OCPlatform::getDeviceInfo("", nullptr, CT_DEFAULT, &receivedDeviceInfo));
682     }
683
684     TEST(GetDeviceInfoTest, GetDeviceInfoWithNullDeviceInfoHandler)
685     {
686         std::string deviceDiscoveryURI = "/oic/d";
687         PlatformConfig cfg;
688         OCPlatform::Configure(cfg);
689         std::ostringstream requestURI;
690         requestURI << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
691         EXPECT_THROW(
692                 OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, NULL),
693                 OC::OCException);
694     }
695
696     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithLowQos)
697     {
698         std::string deviceDiscoveryURI = "/oic/d";
699         PlatformConfig cfg;
700         OCPlatform::Configure(cfg);
701         std::ostringstream requestURI;
702         requestURI << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
703         EXPECT_EQ(OC_STACK_OK,
704                 OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, &receivedDeviceInfo,
705                         OC::QualityOfService::LowQos));
706     }
707
708     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithMidQos)
709     {
710         std::string deviceDiscoveryURI = "/oic/d";
711         PlatformConfig cfg;
712         OCPlatform::Configure(cfg);
713         std::ostringstream requestURI;
714         requestURI << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
715         EXPECT_EQ(OC_STACK_OK,
716                 OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, &receivedDeviceInfo,
717                         OC::QualityOfService::MidQos));
718     }
719
720     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithHighQos)
721     {
722         std::string deviceDiscoveryURI = "/oic/d";
723         PlatformConfig cfg;
724         OCPlatform::Configure(cfg);
725         std::ostringstream requestURI;
726         requestURI << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
727         EXPECT_EQ(OC_STACK_OK,
728                 OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, &receivedDeviceInfo,
729                         OC::QualityOfService::HighQos));
730     }
731
732     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithNaQos)
733     {
734         std::string deviceDiscoveryURI = "/oic/d";
735         PlatformConfig cfg;
736         OCPlatform::Configure(cfg);
737         std::ostringstream requestURI;
738         requestURI << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
739         EXPECT_EQ(OC_STACK_OK,
740                 OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, &receivedDeviceInfo,
741                         OC::QualityOfService::NaQos));
742     }
743
744     //RegisterDeviceInfo test
745     TEST(RegisterDeviceInfoTest, RegisterDeviceInfoWithValidParameters)
746     {
747         OCDeviceInfo deviceInfo;
748         DuplicateString(&deviceInfo.deviceName, "myDeviceName");
749         deviceInfo.types = NULL;
750         OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
751         OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
752         DuplicateString(&deviceInfo.specVersion, "mySpecVersion");
753         deviceInfo.dataModelVersions = nullptr;
754         OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, "myDataModelVersions");
755         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerDeviceInfo(deviceInfo));
756         EXPECT_NO_THROW(DeleteDeviceInfo(deviceInfo));
757     }
758
759     TEST(RegisterDeviceInfoTest, RegisterDeviceInfoWithEmptyObject)
760     {
761         OCDeviceInfo di = {0, 0, 0, 0};
762         EXPECT_ANY_THROW(OCPlatform::registerDeviceInfo(di));
763     }
764
765     //SubscribePresence Test
766     TEST(SubscribePresenceTest, DISABLED_SubscribePresenceWithValidParameters)
767     {
768         std::string hostAddress = "192.168.1.2:5000";
769         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
770
771         EXPECT_EQ(OC_STACK_OK, OCPlatform::subscribePresence(presenceHandle, hostAddress,
772                  CT_DEFAULT, &presenceHandler));
773     }
774
775     TEST(SubscribePresenceTest, SubscribePresenceWithNullHost)
776     {
777         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
778
779         EXPECT_ANY_THROW(OCPlatform::subscribePresence(presenceHandle, nullptr,
780                  CT_DEFAULT, &presenceHandler));
781     }
782
783     TEST(SubscribePresenceTest, SubscribePresenceWithNullPresenceHandler)
784     {
785         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
786
787         EXPECT_ANY_THROW(OCPlatform::subscribePresence(presenceHandle, nullptr,
788                  CT_DEFAULT, NULL));
789     }
790
791     TEST(SubscribePresenceTest, DISABLED_SubscribePresenceWithResourceType)
792     {
793         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
794
795         EXPECT_EQ(OC_STACK_OK, OCPlatform::subscribePresence(presenceHandle,
796                 OC_MULTICAST_IP, "core.light", CT_DEFAULT, &presenceHandler));
797     }
798
799     TEST(SubscribePresenceTest, SubscribePresenceWithNullResourceType)
800     {
801         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
802
803         EXPECT_ANY_THROW(OCPlatform::subscribePresence(presenceHandle,
804                 OC_MULTICAST_IP, nullptr, CT_DEFAULT, &presenceHandler));
805     }
806
807     TEST(SubscribePresenceTest, DISABLED_UnsubscribePresenceWithValidHandleAndRT)
808     {
809         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
810
811         EXPECT_EQ(OC_STACK_OK, OCPlatform::subscribePresence(presenceHandle,
812                 OC_MULTICAST_IP, "core.light", CT_DEFAULT, &presenceHandler));
813         EXPECT_EQ(OC_STACK_OK, OCPlatform::unsubscribePresence(presenceHandle));
814     }
815
816     TEST(SubscribePresenceTest, UnsubscribePresenceWithNullHandle)
817     {
818         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
819         EXPECT_ANY_THROW(OCPlatform::unsubscribePresence(presenceHandle));
820     }
821
822     TEST(SubscribePresenceTest, DISABLED_UnsubscribePresenceWithValidHandle)
823     {
824         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
825
826         EXPECT_EQ(OC_STACK_OK, OCPlatform::subscribePresence(presenceHandle,
827                 OC_MULTICAST_IP, CT_DEFAULT, &presenceHandler));
828         EXPECT_EQ(OC_STACK_OK, OCPlatform::unsubscribePresence(presenceHandle));
829     }
830
831     TEST(FindDirectPairingTest, FindDirectPairingNullCallback)
832     {
833         EXPECT_ANY_THROW(OCPlatform::findDirectPairingDevices(1, nullptr));
834     }
835
836     TEST(FindDirectPairingTest, FindDirectPairingZeroTimeout)
837     {
838         EXPECT_ANY_THROW(OCPlatform::findDirectPairingDevices(0, &pairedHandler));
839     }
840
841     TEST(GetDirectPairedTest, GetDirectPairedNullCallback)
842     {
843         EXPECT_ANY_THROW(OCPlatform::getDirectPairedDevices(nullptr));
844     }
845
846     TEST(DoDirectPairingTest, DoDirectPairingNullCallback)
847     {
848         OCDPDev_t peer;
849         OCPrm_t pmSel = DP_PRE_CONFIGURED;
850         std::string pin("");
851         std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
852         EXPECT_ANY_THROW(OCPlatform::doDirectPairing(s_dp, pmSel, pin, nullptr));
853     }
854
855     TEST(DoDirectPairingTest, DoDirectPairingNullPeer)
856     {
857         OCDPDev_t peer;
858         OCPrm_t pmSel = DP_PRE_CONFIGURED;
859         std::string pin("");
860         std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
861         EXPECT_ANY_THROW(OCPlatform::doDirectPairing(nullptr, pmSel, pin, &directPairHandler));
862     }
863
864     TEST(DoDirectPairingTest, DoDirectPairingNullPeerNullCallback)
865     {
866         OCDPDev_t peer;
867         OCPrm_t pmSel = DP_PRE_CONFIGURED;
868         std::string pin("");
869         std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
870         EXPECT_ANY_THROW(OCPlatform::doDirectPairing(nullptr, pmSel, pin, nullptr));
871     }
872 }