#include "libvirt/helpers.hpp"
#include "libvirt/exception.hpp"
-#include <cassert>
-
namespace security_containers {
namespace libvirt {
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jan Olszak <j.olszak@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 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
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jan Olszak <j.olszak@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 Declaration of the class wrapping libvirt network
+ */
+
+#ifndef COMMON_LIBVIRT_NETWORK_HPP
+#define COMMON_LIBVIRT_NETWORK_HPP
+
+#include "libvirt/connection.hpp"
+
+#include <libvirt/libvirt.h>
+
+
+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
/**
* @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"
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jan Olszak <j.olszak@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 the LibvirtNetwork class
+ */
+
+#include "ut.hpp"
+
+#include "libvirt/network.hpp"
+#include "libvirt/exception.hpp"
+
+#include <memory>
+
+BOOST_AUTO_TEST_SUITE(LibvirtNetworkSuite)
+
+
+using namespace security_containers;
+using namespace security_containers::libvirt;
+
+
+namespace {
+
+const std::string CORRECT_CONFIG_XML = "<network>"
+ " <name>test</name>"
+ " <forward mode='nat'>"
+ " <nat>"
+ " <port start='1024' end='65535'/>"
+ " </nat>"
+ " </forward>"
+ " <bridge name='test-virbr0' stp='on' delay='0'/>"
+ " <ip address='192.168.122.1' netmask='255.255.255.0'>"
+ " <dhcp>"
+ " <range start='192.168.122.2' end='192.168.122.254'/>"
+ " </dhcp>"
+ " </ip>"
+ "</network>";
+
+const std::string BUGGY_CONFIG_XML = "<><TRASH>";
+
+} // namespace
+
+BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
+{
+ std::unique_ptr<LibvirtNetwork> 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()