#include <OCPlatform.h>
#include <OCApi.h>
+#include <oic_malloc.h>
#include <gtest/gtest.h>
namespace OCPlatformTest
{
using namespace OC;
+ static const char* SVR_DB_FILE_NAME = "./oic_svr_db_server.dat";
const OCResourceHandle HANDLE_ZERO = 0;
const std::string gResourceTypeName = "core.res";
const std::string gResourceInterface = DEFAULT_INTERFACE;
const uint8_t gResourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
OCResourceHandle resourceHandle;
+ //OCPersistent Storage Handlers
+ static FILE* client_open(const char * /*path*/, const char *mode)
+ {
+ std::cout << "<===Opening SVR DB file = './oic_svr_db_client.dat' with mode = '" << mode
+ << "' " << std::endl;
+ return fopen(SVR_DB_FILE_NAME, mode);
+ }
+ OCPersistentStorage gps {client_open, fread, fwrite, fclose, unlink, NULL, NULL };
+
// Callbacks
- OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
+ OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
{
return OC_EH_OK;
}
- void foundResource(std::shared_ptr<OCResource> resource)
+ void foundResource(std::shared_ptr<OCResource> /*resource*/)
+ {
+ }
+
+ void receivedDeviceInfo(const OCRepresentation& /*rep*/)
{
}
- void receivedDeviceInfo(const OCRepresentation& rep)
+ void presenceHandler(OCStackResult /*result*/,
+ const unsigned int /*nonce*/, const std::string& /*hostAddress*/)
{
}
- void presenceHandler(OCStackResult result,
- const unsigned int nonce, const std::string& hostAddress)
+#ifdef WITH_CLOUD
+ void onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&)
+ {
+ }
+#endif
+
+ void directPairHandler(std::shared_ptr<OCDirectPairing> /*dev*/, OCStackResult /*res*/)
+ {
+ }
+
+ void pairedHandler(const PairedDevices& /*list*/)
{
}
//Helper methods
+ void DeleteStringLL(OCStringLL* ll)
+ {
+ if (!ll)
+ {
+ return;
+ }
+
+ DeleteStringLL(ll->next);
+ delete[] ll->value;
+ OICFree(ll);
+ }
+
void DeleteDeviceInfo(OCDeviceInfo deviceInfo)
{
delete[] deviceInfo.deviceName;
-
+ DeleteStringLL(deviceInfo.types);
+ delete[] deviceInfo.specVersion;
+ DeleteStringLL(deviceInfo.dataModelVersions);
}
void DuplicateString(char ** targetString, std::string sourceString)
strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
}
+ bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, std::string value)
+ {
+ char *dup = NULL;
+ DuplicateString(&dup, value);
+ if (!*stringLL)
+ {
+ *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ (*stringLL)->value = dup;
+ return true;
+ }
+ else
+ {
+ OCStringLL *temp = *stringLL;
+ while(temp->next)
+ {
+ temp = temp->next;
+ }
+ temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ temp->next->value = dup;
+ return true;
+ }
+ return false;
+ }
+
OCResourceHandle RegisterResource(std::string uri, std::string type, std::string iface)
{
- PlatformConfig cfg = {};
+ PlatformConfig cfg
+ { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+ OC::QualityOfService::LowQos, &gps };
OCPlatform::Configure(cfg);
EXPECT_EQ(OC_STACK_OK,OCPlatform::registerResource(
resourceHandle, uri, type,
OCResourceHandle RegisterResource(std::string uri, std::string type)
{
- PlatformConfig cfg = {};
+ PlatformConfig cfg
+ { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+ OC::QualityOfService::LowQos, &gps };
OCPlatform::Configure(cfg);
EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
resourceHandle, uri, type,
OCResourceHandle RegisterResource(std::string uri)
{
- PlatformConfig cfg = {};
+ PlatformConfig cfg
+ { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+ OC::QualityOfService::LowQos, &gps };
OCPlatform::Configure(cfg);
EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
resourceHandle, uri, gResourceTypeName,
TEST(ConfigureTest, ConfigureServerOutProc)
{
- PlatformConfig cfg {
+ PlatformConfig cfg
+ {
OC::ServiceType::OutOfProc,
OC::ModeType::Server,
"0.0.0.0",
0,
- OC::QualityOfService::LowQos
+ OC::QualityOfService::LowQos, &gps
};
std::string uri = "/a/light67";
std::string type = "core.light";
OC::ModeType::Server,
"0.0.0.0",
0,
- OC::QualityOfService::LowQos
+ OC::QualityOfService::LowQos, &gps
};
OCPlatform::Configure(cfg);
std::string uri = "/a/light70";
std::string type = "core.light";
uint8_t gResourceProperty = 0;
- PlatformConfig cfg {
+ PlatformConfig cfg
+ {
OC::ServiceType::InProc,
OC::ModeType::Client,
"0.0.0.0",
0,
- OC::QualityOfService::LowQos
+ OC::QualityOfService::LowQos,
+ &gps
};
- OCPlatform::Configure(cfg);
EXPECT_NO_THROW(OCPlatform::registerResource(
resourceHandle, uri, type,
gResourceInterface, entityHandler, gResourceProperty));
}
+ //PersistentStorageTest
+ TEST(ConfigureTest, ConfigureDefaultNULLPersistentStorage)
+ {
+ PlatformConfig cfg {
+ OC::ServiceType::InProc,
+ OC::ModeType::Both,
+ "0.0.0.0",
+ 0,
+ OC::QualityOfService::LowQos
+ };
+ OCPlatform::Configure(cfg);
+ EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
+ }
+
+ //PersistentStorageTest
+ TEST(ConfigureTest, ConfigureNULLPersistentStorage)
+ {
+ PlatformConfig cfg {
+ OC::ServiceType::InProc,
+ OC::ModeType::Both,
+ "0.0.0.0",
+ 0,
+ OC::QualityOfService::LowQos,
+ nullptr
+ };
+ OCPlatform::Configure(cfg);
+ EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
+ }
+
+ //PersistentStorageTest
+ TEST(ConfigureTest, ConfigurePersistentStorage)
+ {
+ PlatformConfig cfg {
+ OC::ServiceType::InProc,
+ OC::ModeType::Both,
+ "0.0.0.0",
+ 0,
+ OC::QualityOfService::LowQos,
+ &gps
+ };
+ OCPlatform::Configure(cfg);
+ EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
+ }
+
+ //PersistentStorageTest
+ TEST(ConfigureTest, ConfigureNULLHandlersPersistentStorage)
+ {
+ PlatformConfig cfg {
+ OC::ServiceType::InProc,
+ OC::ModeType::Both,
+ "0.0.0.0",
+ 0,
+ OC::QualityOfService::LowQos,
+ &gps
+ };
+ OCPlatform::Configure(cfg);
+ EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
+ }
+
//RegisterResourceTest
TEST(RegisterResourceTest, RegisterSingleResource)
TEST(RegisterDeviceInfoTest, RegisterDeviceInfoWithValidParameters)
{
OCDeviceInfo deviceInfo;
-
DuplicateString(&deviceInfo.deviceName, "myDeviceName");
-
+ deviceInfo.types = NULL;
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
+ DuplicateString(&deviceInfo.specVersion, "mySpecVersion");
+ deviceInfo.dataModelVersions = nullptr;
+ OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, "myDataModelVersions");
EXPECT_EQ(OC_STACK_OK, OCPlatform::registerDeviceInfo(deviceInfo));
EXPECT_NO_THROW(DeleteDeviceInfo(deviceInfo));
}
TEST(RegisterDeviceInfoTest, RegisterDeviceInfoWithEmptyObject)
{
- OCDeviceInfo di = {};
+ OCDeviceInfo di = {0, 0, 0, 0};
EXPECT_ANY_THROW(OCPlatform::registerDeviceInfo(di));
}
EXPECT_EQ(OC_STACK_OK, OCPlatform::unsubscribePresence(presenceHandle));
}
+#ifdef WITH_CLOUD
+ // SubscribeDevicePresence Test
+ TEST(SubscribeDevicePresenceTest, DISABLED_SubscribeDevicePresenceWithValidParameters)
+ {
+ std::string hostAddress = "192.168.1.2:5000";
+ OCPlatform::OCPresenceHandle presenceHandle = nullptr;
+ std::vector<std::string> di;
+
+ EXPECT_EQ(OC_STACK_OK, OCPlatform::subscribeDevicePresence(presenceHandle,
+ hostAddress, di, CT_DEFAULT, &onObserve));
+ }
+
+ TEST(SubscribeDevicePresenceTest, SubscribeDevicePresenceWithNullHost)
+ {
+ OCPlatform::OCPresenceHandle presenceHandle = nullptr;
+ std::vector<std::string> di;
+
+ EXPECT_ANY_THROW(OCPlatform::subscribeDevicePresence(presenceHandle,
+ nullptr, di, CT_DEFAULT, &onObserve));
+ }
+
+ TEST(SubscribeDevicePresenceTest, SubscribeDevicePresenceWithNullOnObserve)
+ {
+ std::string hostAddress = "192.168.1.2:5000";
+ OCPlatform::OCPresenceHandle presenceHandle = nullptr;
+ std::vector<std::string> di;
+
+ EXPECT_ANY_THROW(OCPlatform::subscribeDevicePresence(presenceHandle,
+ hostAddress, di, CT_DEFAULT, NULL));
+ }
+
+ TEST(SubscribeDevicePresenceTest, DISABLED_UnsubscribePresenceWithValidHandle)
+ {
+ std::string hostAddress = "192.168.1.2:5000";
+ OCPlatform::OCPresenceHandle presenceHandle = nullptr;
+ std::vector<std::string> di;
+
+ EXPECT_EQ(OC_STACK_OK, OCPlatform::subscribeDevicePresence(presenceHandle,
+ hostAddress, di, CT_DEFAULT, &onObserve));
+ EXPECT_EQ(OC_STACK_OK, OCPlatform::unsubscribePresence(presenceHandle));
+ }
+#endif
+
+ TEST(FindDirectPairingTest, FindDirectPairingNullCallback)
+ {
+ EXPECT_ANY_THROW(OCPlatform::findDirectPairingDevices(1, nullptr));
+ }
+
+ TEST(FindDirectPairingTest, FindDirectPairingZeroTimeout)
+ {
+ EXPECT_ANY_THROW(OCPlatform::findDirectPairingDevices(0, &pairedHandler));
+ }
+
+ TEST(GetDirectPairedTest, GetDirectPairedNullCallback)
+ {
+ EXPECT_ANY_THROW(OCPlatform::getDirectPairedDevices(nullptr));
+ }
+
+ TEST(DoDirectPairingTest, DoDirectPairingNullCallback)
+ {
+ OCDPDev_t peer;
+ OCPrm_t pmSel = DP_PRE_CONFIGURED;
+ std::string pin("");
+ std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
+ EXPECT_ANY_THROW(OCPlatform::doDirectPairing(s_dp, pmSel, pin, nullptr));
+ }
+
+ TEST(DoDirectPairingTest, DoDirectPairingNullPeer)
+ {
+ OCDPDev_t peer;
+ OCPrm_t pmSel = DP_PRE_CONFIGURED;
+ std::string pin("");
+ std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
+ EXPECT_ANY_THROW(OCPlatform::doDirectPairing(nullptr, pmSel, pin, &directPairHandler));
+ }
+
+ TEST(DoDirectPairingTest, DoDirectPairingNullPeerNullCallback)
+ {
+ OCDPDev_t peer;
+ OCPrm_t pmSel = DP_PRE_CONFIGURED;
+ std::string pin("");
+ std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
+ EXPECT_ANY_THROW(OCPlatform::doDirectPairing(nullptr, pmSel, pin, nullptr));
+ }
}