From: Jan Olszak Date: Fri, 30 May 2014 16:14:51 +0000 (+0200) Subject: Libvirt network wrapper X-Git-Tag: submit/tizen/20140723.154014~29 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=14b42dbf56d51db4540560a661fa707c93bb4a78;p=platform%2Fcore%2Fsecurity%2Fvasum.git Libvirt network wrapper [Bug/Feature] A wrapper for libvirt' network [Cause] N/A [Solution] N/A [Verification] Build, install, run tests. Change-Id: I0997f846132cc29035b144705ff4a4835a3dad01 --- diff --git a/common/libvirt/domain.cpp b/common/libvirt/domain.cpp index 1c6c52d..ae25a02 100644 --- a/common/libvirt/domain.cpp +++ b/common/libvirt/domain.cpp @@ -27,8 +27,6 @@ #include "libvirt/helpers.hpp" #include "libvirt/exception.hpp" -#include - namespace security_containers { namespace libvirt { diff --git a/common/libvirt/network.cpp b/common/libvirt/network.cpp new file mode 100644 index 0000000..fdb9126 --- /dev/null +++ b/common/libvirt/network.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Jan Olszak + * + * 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 Implementation of the class wrapping libvirt network + */ + +#include "log/logger.hpp" +#include "libvirt/network.hpp" +#include "libvirt/helpers.hpp" +#include "libvirt/exception.hpp" + + +namespace security_containers { +namespace libvirt { + + +LibvirtNetwork::LibvirtNetwork(const std::string& configXML) + : mCon(LIBVIRT_LXC_ADDRESS) +{ + mNet = virNetworkDefineXML(mCon.get(), configXML.c_str()); + + if (mNet == NULL) { + LOGE("Error while defining a network:\n" + << libvirtFormatError()); + throw LibvirtOperationException(); + } +} + +LibvirtNetwork::~LibvirtNetwork() +{ + if (virNetworkUndefine(mNet) < 0) { + LOGE("Error while undefining the network:\n" + << libvirtFormatError()); + } + + if (virNetworkFree(mNet) < 0) { + LOGE("Error while destroying the network object:\n" + << libvirtFormatError()); + } +} + +virNetworkPtr LibvirtNetwork::get() +{ + return mNet; +} + +LibvirtNetwork::operator bool() const +{ + return mNet != NULL; +} + +} // namespace libvirt +} // namespace security_containers diff --git a/common/libvirt/network.hpp b/common/libvirt/network.hpp new file mode 100644 index 0000000..5c5626e --- /dev/null +++ b/common/libvirt/network.hpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Jan Olszak + * + * 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 Declaration of the class wrapping libvirt network + */ + +#ifndef COMMON_LIBVIRT_NETWORK_HPP +#define COMMON_LIBVIRT_NETWORK_HPP + +#include "libvirt/connection.hpp" + +#include + + +namespace security_containers { +namespace libvirt { + + +class LibvirtNetwork { + +public: + LibvirtNetwork(const std::string& configXML); + ~LibvirtNetwork(); + + /** + * @return The libvirt network pointer + */ + virNetworkPtr get(); + + /** + * @return libvirt network pointer is not NULL + */ + operator bool() const; + +private: + LibvirtConnection mCon; + virNetworkPtr mNet = NULL; +}; + + +} // namespace libvirt +} // namespace security_containers + + +#endif // COMMON_LIBVIRT_NETWORK_HPP diff --git a/tests/unit_tests/libvirt/domain.cpp b/tests/unit_tests/libvirt/domain.cpp index b05fa5a..2ecbcc3 100644 --- a/tests/unit_tests/libvirt/domain.cpp +++ b/tests/unit_tests/libvirt/domain.cpp @@ -20,7 +20,7 @@ /** * @file * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com) - * @brief Unit tests of the ContainerAdmin class + * @brief Unit tests of the LibvirtDomain class */ #include "ut.hpp" diff --git a/tests/unit_tests/libvirt/network.cpp b/tests/unit_tests/libvirt/network.cpp new file mode 100644 index 0000000..7f862f7 --- /dev/null +++ b/tests/unit_tests/libvirt/network.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Jan Olszak + * + * 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 the LibvirtNetwork class + */ + +#include "ut.hpp" + +#include "libvirt/network.hpp" +#include "libvirt/exception.hpp" + +#include + +BOOST_AUTO_TEST_SUITE(LibvirtNetworkSuite) + + +using namespace security_containers; +using namespace security_containers::libvirt; + + +namespace { + +const std::string CORRECT_CONFIG_XML = "" + " test" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + +const std::string BUGGY_CONFIG_XML = "<>"; + +} // namespace + +BOOST_AUTO_TEST_CASE(ConstructorDestructorTest) +{ + std::unique_ptr netPtr; + BOOST_REQUIRE_NO_THROW(netPtr.reset(new LibvirtNetwork(CORRECT_CONFIG_XML))); + BOOST_REQUIRE_NO_THROW(netPtr.reset()); +} + +BOOST_AUTO_TEST_CASE(BuggyConfigTest) +{ + BOOST_REQUIRE_THROW(LibvirtNetwork net(BUGGY_CONFIG_XML), LibvirtOperationException); +} + +BOOST_AUTO_TEST_CASE(DefinitionTest) +{ + LibvirtNetwork net(CORRECT_CONFIG_XML); + BOOST_CHECK(net.get() != NULL); +} + +BOOST_AUTO_TEST_CASE(BoolTest) +{ + LibvirtNetwork net(CORRECT_CONFIG_XML); + BOOST_CHECK(net); +} + +BOOST_AUTO_TEST_SUITE_END()