lxcpp: Added logger, tests and changed exceptions 65/44865/6
authorJan Olszak <j.olszak@samsung.com>
Tue, 28 Jul 2015 11:12:10 +0000 (13:12 +0200)
committerJan Olszak <j.olszak@samsung.com>
Wed, 29 Jul 2015 09:05:06 +0000 (11:05 +0200)
[Feature]       Using libLogger in lxcpp in an example clone function.
                Set up unit-tests
                Changed exception names
[Cause]         N/A
[Solution]      N/A
[Verification]  N/A

Change-Id: I039bf3e17d791ccf1a18f17a9b2a180ee2081722

libs/lxcpp/CMakeLists.txt
libs/lxcpp/container-impl.cpp
libs/lxcpp/container-impl.hpp
libs/lxcpp/container.hpp
libs/lxcpp/exception.hpp
libs/lxcpp/lxcpp.cpp
libs/lxcpp/lxcpp.hpp
libs/lxcpp/process.cpp [new file with mode: 0644]
libs/lxcpp/process.hpp [new file with mode: 0644]
tests/unit_tests/CMakeLists.txt
tests/unit_tests/lxcpp/ut-container.cpp [new file with mode: 0644]

index 74522a3..0d49df9 100644 (file)
@@ -37,6 +37,7 @@ SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES
 
 ## Link libraries ##############################################################
 INCLUDE_DIRECTORIES(${LIBS_FOLDER})
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} Logger)
 
 ## Generate the pc file ########################################################
 CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY)
index a7f404e..e4f290d 100644 (file)
@@ -21,8 +21,8 @@
  * @brief   ContainerImpl definition
  */
 
-#include <lxcpp/container-impl.hpp>
-#include <lxcpp/exception.hpp>
+#include "lxcpp/container-impl.hpp"
+#include "lxcpp/exception.hpp"
 
 namespace lxcpp {
 
@@ -36,62 +36,62 @@ ContainerImpl::~ContainerImpl()
 
 std::string ContainerImpl::getName()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::setName(const std::string& /* name */)
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::start()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::stop()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::freeze()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::unfreeze()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::reboot()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 int ContainerImpl::getInitPid()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::create()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::destroy()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 void ContainerImpl::setRootPath(const std::string& /* path */)
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
-std::string getRootPath()
+std::string ContainerImpl::getRootPath()
 {
-    throw NotImplemented();
+    throw NotImplementedException();
 }
 
 } // namespace lxcpp
index 9b9b1ea..f5d9547 100644 (file)
@@ -24,7 +24,7 @@
 #ifndef LXCPP_CONTAINER_IMPL_HPP
 #define LXCPP_CONTAINER_IMPL_HPP
 
-#include <lxcpp/container.hpp>
+#include "lxcpp/container.hpp"
 
 namespace lxcpp {
 
index 36e877e..74caa51 100644 (file)
@@ -30,7 +30,7 @@ namespace lxcpp {
 
 class Container {
 public:
-    virtual ~Container();
+    virtual ~Container() {};
 
     virtual std::string getName() = 0;
     virtual void setName(const std::string& name) = 0;
index 1e3ce4c..9d59101 100644 (file)
 
 namespace lxcpp {
 
-class ContainerException : std::runtime_error
-{
-public:
-    ContainerException(const std::string& what) : std::runtime_error(what) {}
+/**
+ * Base class for exceptions in lxcpp
+ */
+struct Exception: public std::runtime_error {
+    Exception(const std::string& message)
+        : std::runtime_error(message) {}
 };
 
-class NotImplemented : ContainerException
-{
-public:
-    NotImplemented() : ContainerException(std::string()) {}
+struct NotImplementedException: public Exception {
+    NotImplementedException(const std::string& message = "Functionality not yet implemented")
+        : Exception(message) {}
 };
 
+struct ProcessSetupException: public Exception {
+    ProcessSetupException(const std::string& message = "Error during setting up a process")
+        : Exception(message) {}
+};
 } // namespace lxcpp
 
 #endif // LXCPP_EXCEPTION_HPP
index 3cb906c..97c3866 100644 (file)
@@ -21,7 +21,7 @@
  * @brief   lxcpp container factory definition
  */
 
-#include <lxcpp/container-impl.hpp>
+#include "lxcpp/container-impl.hpp"
 
 namespace lxcpp {
 
index 8037d3c..5eb3f57 100644 (file)
 /**
  * @file
  * @author  Mateusz Malicki (m.malicki2@samsung.com)
- * @brief   LXCpp factory declaration
+ * @brief   lxcpp container factory
  */
 
 #ifndef LXCPP_LXCPP_HPP
 #define LXCPP_LXCPP_HPP
 
-#include <lxcpp/container.hpp>
+#include "lxcpp/container.hpp"
 
 namespace lxcpp {
 
diff --git a/libs/lxcpp/process.cpp b/libs/lxcpp/process.cpp
new file mode 100644 (file)
index 0000000..200d12e
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License version 2.1 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   process handling routines
+ */
+
+#include "lxcpp/process.hpp"
+#include "lxcpp/exception.hpp"
+#include "logger/logger.hpp"
+
+#include <alloca.h>
+#include <sched.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+namespace lxcpp {
+
+pid_t clone(int (*function)(void *), int flags, void *args) {
+    // Won't fail, well known resource name
+    size_t stackSize = ::sysconf(_SC_PAGESIZE);
+
+    // PAGESIZE is enough, it'll exec after this
+    char *stack = static_cast<char*>(::alloca(stackSize));
+
+    pid_t ret;
+    ret = ::clone(function, stack  + stackSize, flags | SIGCHLD, args);
+    if (ret < 0) {
+        LOGE("clone() failed");
+        throw ProcessSetupException("clone() failed");
+    }
+
+    return ret;
+}
+
+} // namespace lxcpp
\ No newline at end of file
diff --git a/libs/lxcpp/process.hpp b/libs/lxcpp/process.hpp
new file mode 100644 (file)
index 0000000..8ca8aaf
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License version 2.1 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   process handling routines
+ */
+
+#ifndef LXCPP_PROCESS_HPP
+#define LXCPP_PROCESS_HPP
+
+#include <sys/types.h>
+
+namespace lxcpp {
+
+pid_t clone(int (*function)(void *), int flags, void *args);
+
+} // namespace lxcpp
+
+#endif // LXCPP_PROCESS_HPP
\ No newline at end of file
index aa652c7..956082d 100644 (file)
@@ -61,7 +61,7 @@ SET_TARGET_PROPERTIES(${UT_SERVER_CODENAME} PROPERTIES
 )
 
 TARGET_LINK_LIBRARIES(${UT_SERVER_CODENAME} ${UT_SERVER_DEPS_LIBRARIES} ${Boost_LIBRARIES}
-                      Logger Config Ipc)
+                      Logger Config Ipc lxcpp)
 IF(NOT WITHOUT_DBUS)
 TARGET_LINK_LIBRARIES(${UT_SERVER_CODENAME} SimpleDbus)
 ENDIF(NOT WITHOUT_DBUS)
diff --git a/tests/unit_tests/lxcpp/ut-container.cpp b/tests/unit_tests/lxcpp/ut-container.cpp
new file mode 100644 (file)
index 0000000..74ebbfe
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Piotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+
+/**
+ * @file
+ * @author  Jan Olszak(j.olszak@samsung.com)
+ * @brief   Unit tests of lxcpp Container class
+ */
+
+#include "config.hpp"
+#include "ut.hpp"
+
+#include "lxcpp/lxcpp.hpp"
+#include "lxcpp/exception.hpp"
+
+#include <memory>
+
+namespace {
+
+struct Fixture {
+    Fixture() {}
+    ~Fixture() {}
+};
+
+} // namespace
+
+BOOST_FIXTURE_TEST_SUITE(LxcppContainerSuite, Fixture)
+
+using namespace lxcpp;
+
+BOOST_AUTO_TEST_CASE(ConstructorDestructor)
+{
+    auto c = createContainer();
+    delete c;
+}
+
+BOOST_AUTO_TEST_SUITE_END()