From ad06ab955c647d19f537ea853140a396969b68f7 Mon Sep 17 00:00:00 2001 From: Minji Park Date: Fri, 7 Aug 2015 11:25:49 +0900 Subject: [PATCH] resource-manipulation: modify container tests to get config path information add logic to get app execution path to container-test to set config file path when initializing container. Change-Id: I4edc64caccfd6ce4c7c614673be53c139bbd2516 Signed-off-by: Minji Park Reviewed-on: https://gerrit.iotivity.org/gerrit/2132 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../resourceContainer/examples/ContainerSample.cpp | 32 +++++++- .../src/resourceContainer/include/Configuration.h | 1 - .../src/ResourceContainerImpl.cpp | 3 +- .../unittests/ResourceContainerTest.cpp | 94 ++++++++++++++++++---- 4 files changed, 112 insertions(+), 18 deletions(-) diff --git a/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp index 9b84abe..3be5edf 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp @@ -18,6 +18,10 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#if defined(__linux__) +#include +#endif + #include "RCSResourceContainer.h" #include "RCSBundleInfo.h" #include "oc_logger.hpp" @@ -27,6 +31,8 @@ using namespace std; using namespace OIC::Service; using OC::oc_log_stream; +#define MAX_PATH 2048 + /* Annother way to create a context: */ auto info_logger = []() -> boost::iostreams::stream & { @@ -36,6 +42,26 @@ auto info_logger = []() -> boost::iostreams::stream & return os; }; +void getCurrentPath(std::string *pPath) +{ + char buffer[MAX_PATH]; + +#if defined(__linux__) + char *strPath = NULL; + int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1); + + if (length != -1) + { + buffer[length] = '\0'; + strPath = strrchr(buffer, '/'); + + if (strPath != NULL) + *strPath = '\0'; + } +#endif + pPath->append(buffer); +} + int main() { info_logger()->set_module("ContainerTest"); @@ -43,8 +69,12 @@ int main() info_logger() << "Starting container test." << std::flush; + std::string strConfigPath; + getCurrentPath(&strConfigPath); + strConfigPath.append("/examples/ResourceContainerConfig.xml"); + RCSResourceContainer *container = RCSResourceContainer::getInstance(); - container->startContainer("examples/ResourceContainerConfig.xml"); + container->startContainer(strConfigPath); /*so bundle add test*/ cout << "++++++++++++++++++++++++++" << endl; diff --git a/service/resource-encapsulation/src/resourceContainer/include/Configuration.h b/service/resource-encapsulation/src/resourceContainer/include/Configuration.h index 9f9872c..827823c 100644 --- a/service/resource-encapsulation/src/resourceContainer/include/Configuration.h +++ b/service/resource-encapsulation/src/resourceContainer/include/Configuration.h @@ -63,7 +63,6 @@ namespace OIC private: void getConfigDocument(string pathConfigFile); - void getCurrentPath(string *pPath); bool m_loaded; string m_pathConfigFile; diff --git a/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp b/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp index 09d40c3..25164cf 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp @@ -153,7 +153,8 @@ namespace OIC m_mapBundleResources.clear(); } - delete m_config; + if (m_config) + delete m_config; } void ResourceContainerImpl::activateBundle(RCSBundleInfo *bundleInfo) diff --git a/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp b/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp index f672046..5823093 100644 --- a/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp +++ b/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp @@ -18,6 +18,10 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#if defined(__linux__) +#include +#endif + #include #include #include @@ -40,8 +44,29 @@ using namespace std; using namespace testing; using namespace OIC::Service; +#define MAX_PATH 2048 + string CONFIG_FILE = "ResourceContainerTestConfig.xml"; +void getCurrentPath(std::string *pPath) +{ + char buffer[MAX_PATH]; + +#if defined(__linux__) + char *strPath = NULL; + int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1); + + if (length != -1) + { + buffer[length] = '\0'; + strPath = strrchr(buffer, '/'); + + if (strPath != NULL) + *strPath = '\0'; + } +#endif + pPath->append(buffer); +} /*Fake bundle resource class for testing*/ class TestBundleResource: public BundleResource @@ -68,18 +93,22 @@ class ResourceContainerTest: public TestWithMock public: RCSResourceContainer *m_pResourceContainer; + std::string m_strConfigPath; protected: void SetUp() { TestWithMock::SetUp(); m_pResourceContainer = RCSResourceContainer::getInstance(); + getCurrentPath(&m_strConfigPath); + m_strConfigPath.append("/"); + m_strConfigPath.append(CONFIG_FILE); } }; TEST_F(ResourceContainerTest, BundleRegisteredWhenContainerStartedWithValidConfigFile) { - m_pResourceContainer->startContainer(CONFIG_FILE); + m_pResourceContainer->startContainer(m_strConfigPath); EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0); EXPECT_STREQ("oic.bundle.test", @@ -93,7 +122,7 @@ TEST_F(ResourceContainerTest, BundleRegisteredWhenContainerStartedWithValidConfi TEST_F(ResourceContainerTest, BundleLoadedWhenContainerStartedWithValidConfigFile) { - m_pResourceContainer->startContainer(CONFIG_FILE); + m_pResourceContainer->startContainer(m_strConfigPath); EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0); EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded()); @@ -105,7 +134,7 @@ TEST_F(ResourceContainerTest, BundleLoadedWhenContainerStartedWithValidConfigFil TEST_F(ResourceContainerTest, BundleActivatedWhenContainerStartedWithValidConfigFile) { - m_pResourceContainer->startContainer(CONFIG_FILE); + m_pResourceContainer->startContainer(m_strConfigPath); EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0); EXPECT_TRUE( @@ -132,7 +161,7 @@ TEST_F(ResourceContainerTest, BundleNotRegisteredWhenContainerStartedWithEmptyCo TEST_F(ResourceContainerTest, BundleUnregisteredWhenContainerStopped) { - m_pResourceContainer->startContainer(CONFIG_FILE); + m_pResourceContainer->startContainer(m_strConfigPath); m_pResourceContainer->stopContainer(); EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size()); @@ -140,7 +169,7 @@ TEST_F(ResourceContainerTest, BundleUnregisteredWhenContainerStopped) TEST_F(ResourceContainerTest, BundleStoppedWithStartBundleAPI) { - m_pResourceContainer->startContainer(CONFIG_FILE); + m_pResourceContainer->startContainer(m_strConfigPath); m_pResourceContainer->stopBundle("oic.bundle.test"); EXPECT_FALSE( @@ -151,7 +180,7 @@ TEST_F(ResourceContainerTest, BundleStoppedWithStartBundleAPI) TEST_F(ResourceContainerTest, BundleStartedWithStartBundleAPI) { - m_pResourceContainer->startContainer(CONFIG_FILE); + m_pResourceContainer->startContainer(m_strConfigPath); m_pResourceContainer->stopBundle("oic.bundle.test"); m_pResourceContainer->startBundle("oic.bundle.test"); @@ -202,7 +231,7 @@ TEST_F(ResourceContainerTest, AddAndRemoveSoBundleResource) std::map resourceParams; resourceParams["resourceType"] = "oic.test"; - m_pResourceContainer->startContainer(CONFIG_FILE); + m_pResourceContainer->startContainer(m_strConfigPath); resources = m_pResourceContainer->listBundleResources("oic.bundle.test"); m_pResourceContainer->addResourceConfig("oic.bundle.test", "/test_resource", resourceParams); @@ -233,6 +262,7 @@ class ResourceContainerBundleAPITest: public TestWithMock RCSResourceObject *m_pResourceObject; ResourceContainerBundleAPI *m_pResourceContainer; TestBundleResource *m_pBundleResource; + std::string m_strConfigPath; protected: void SetUp() @@ -241,6 +271,10 @@ class ResourceContainerBundleAPITest: public TestWithMock m_pResourceObject = mocks.Mock(); m_pResourceContainer = ResourceContainerBundleAPI::getInstance(); + getCurrentPath(&m_strConfigPath); + m_strConfigPath.append("/"); + m_strConfigPath.append(CONFIG_FILE); + m_pBundleResource = new TestBundleResource(); m_pBundleResource->m_bundleId = "oic.bundle.test"; m_pBundleResource->m_uri = "/test_resource"; @@ -316,7 +350,7 @@ TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleI configInfo bundle; map< string, string > results; - ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(CONFIG_FILE); + ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(m_strConfigPath); m_pResourceContainer->getBundleConfiguration("oic.bundle.test", &bundle); results = *bundle.begin(); @@ -333,7 +367,7 @@ TEST_F(ResourceContainerBundleAPITest, BundleResourceConfigurationListParsed) vector< resourceInfo > resourceConfig; resourceInfo result; - ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(CONFIG_FILE); + ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(m_strConfigPath); m_pResourceContainer->getResourceConfiguration("oic.bundle.test", &resourceConfig); result = *resourceConfig.begin(); @@ -503,7 +537,12 @@ TEST_F(ResourceContainerImplTest, SoBundleDeactivatedWithBundleID) /* Test for Configuration */ TEST(ConfigurationTest, ConfigFileLoadedWithValidPath) { - Configuration *config = new Configuration(CONFIG_FILE); + std::string strConfigPath; + getCurrentPath(&strConfigPath); + strConfigPath.append("/"); + strConfigPath.append(CONFIG_FILE); + + Configuration *config = new Configuration(strConfigPath); EXPECT_TRUE(config->isLoaded()); } @@ -517,7 +556,12 @@ TEST(ConfigurationTest, ConfigFileNotLoadedWithInvalidPath) TEST(ConfigurationTest, BundleConfigurationListParsed) { - Configuration *config = new Configuration(CONFIG_FILE); + std::string strConfigPath; + getCurrentPath(&strConfigPath); + strConfigPath.append("/"); + strConfigPath.append(CONFIG_FILE); + + Configuration *config = new Configuration(strConfigPath); configInfo bundles; map< string, string > results; @@ -533,7 +577,12 @@ TEST(ConfigurationTest, BundleConfigurationListParsed) TEST(ConfigurationTest, BundleConfigurationParsedWithValidBundleId) { - Configuration *config = new Configuration(CONFIG_FILE); + std::string strConfigPath; + getCurrentPath(&strConfigPath); + strConfigPath.append("/"); + strConfigPath.append(CONFIG_FILE); + + Configuration *config = new Configuration(strConfigPath); configInfo bundle; map< string, string > results; @@ -549,7 +598,12 @@ TEST(ConfigurationTest, BundleConfigurationParsedWithValidBundleId) TEST(ConfigurationTest, BundleConfigurationNotParsedWithInvalidBundleId) { - Configuration *config = new Configuration(CONFIG_FILE); + std::string strConfigPath; + getCurrentPath(&strConfigPath); + strConfigPath.append("/"); + strConfigPath.append(CONFIG_FILE); + + Configuration *config = new Configuration(strConfigPath); configInfo bundles; config->getBundleConfiguration("test", &bundles); @@ -559,7 +613,12 @@ TEST(ConfigurationTest, BundleConfigurationNotParsedWithInvalidBundleId) TEST(ConfigurationTest, BundleResourceConfigurationListParsed) { - Configuration *config = new Configuration(CONFIG_FILE); + std::string strConfigPath; + getCurrentPath(&strConfigPath); + strConfigPath.append("/"); + strConfigPath.append(CONFIG_FILE); + + Configuration *config = new Configuration(strConfigPath); vector< resourceInfo > resourceConfig; resourceInfo result; @@ -574,7 +633,12 @@ TEST(ConfigurationTest, BundleResourceConfigurationListParsed) TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId) { - Configuration *config = new Configuration(CONFIG_FILE); + std::string strConfigPath; + getCurrentPath(&strConfigPath); + strConfigPath.append("/"); + strConfigPath.append(CONFIG_FILE); + + Configuration *config = new Configuration(strConfigPath); configInfo bundles; vector< resourceInfo > resourceConfig; -- 2.7.4