From 1930eeb33cec78550d7fda2f5664d5ff9febf3fd Mon Sep 17 00:00:00 2001 From: arcan1s Date: Sun, 17 Aug 2014 14:54:16 +0400 Subject: [PATCH] add testnetctlprofile --- README.md | 2 +- sources/test/CMakeLists.txt | 11 ++ sources/test/testnetctl.cpp | 26 +-- sources/test/testnetctl.h | 1 + sources/test/testnetctlauto.cpp | 7 + sources/test/testnetctlauto.h | 1 + sources/test/testnetctlprofile.cpp | 246 +++++++++++++++++++++++++++++ sources/test/testnetctlprofile.h | 49 ++++++ 8 files changed, 323 insertions(+), 20 deletions(-) create mode 100644 sources/test/testnetctlprofile.cpp create mode 100644 sources/test/testnetctlprofile.h diff --git a/README.md b/README.md index 7a3760a..7a73054 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ TODO (wish list) * add helper polkit-qt integration ? * test several profiles support -* autotests +* autotests (QCOMPARE(gui, lib)); Links ----- diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index 61011b1..52a8783 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -15,6 +15,9 @@ set (NETCTL_SOURCES testnetctl.cpp) # netctl-auto set (NETCTLAUTO_HEADERS testnetctlauto.h) set (NETCTLAUTO_SOURCES testnetctlauto.cpp) +# netctl profile +set (PROFILE_HEADERS testnetctlprofile.h) +set (PROFILE_SOURCES testnetctlprofile.cpp) # include_path include_directories (${CMAKE_CURRENT_SOURCE_DIR}/../${PROJECT_LIBRARY}/include/ @@ -33,12 +36,14 @@ if (USE_QT5) set (QT_NEEDED_LIBS ${Qt5Core_LIBRARIES} ${Qt5Test_LIBRARIES}) qt5_wrap_cpp (NETCTL_MOC_SOURCES ${NETCTL_HEADERS}) qt5_wrap_cpp (NETCTLAUTO_MOC_SOURCES ${NETCTLAUTO_HEADERS}) + qt5_wrap_cpp (PROFILE_MOC_SOURCES ${PROFILE_HEADERS}) else () find_package (Qt4 COMPONENTS QtCore QtTest REQUIRED) include (${QT_USE_FILE}) set (QT_NEEDED_LIBS ${QT_QTCORE_LIBRARY} ${QT_QTTEST_LIBRARY}) qt4_wrap_cpp (NETCTL_MOC_SOURCES ${NETCTL_HEADERS}) qt4_wrap_cpp (NETCTLAUTO_MOC_SOURCES ${NETCTLAUTO_HEADERS}) + qt4_wrap_cpp (PROFILE_MOC_SOURCES ${PROFILE_HEADERS}) endif() # netctl @@ -47,9 +52,15 @@ target_link_libraries (${SUBPROJECT}-netctl ${PROJECT_LIBRARY} ${QT_NEEDED_LIBS} # netctl-auto add_executable (${SUBPROJECT}-netctlauto ${NETCTLAUTO_HEADERS} ${NETCTLAUTO_SOURCES} ${NETCTLAUTO_MOC_SOURCES}) target_link_libraries (${SUBPROJECT}-netctlauto ${PROJECT_LIBRARY} ${QT_NEEDED_LIBS}) +# netctl profile +add_executable (${SUBPROJECT}-profile ${PROFILE_HEADERS} ${PROFILE_SOURCES} ${PROFILE_MOC_SOURCES}) +target_link_libraries (${SUBPROJECT}-profile ${PROJECT_LIBRARY} ${QT_NEEDED_LIBS}) + # install properties add_test (NAME Netctl COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-netctl "-o" "../Testing/output-netctl.log") add_test (NAME NetctlAuto COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-netctlauto "-o" "../Testing/output-netctlauto.log") +add_test (NAME Profile COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-profile + "-o" "../Testing/output-profile.log") diff --git a/sources/test/testnetctl.cpp b/sources/test/testnetctl.cpp index 1b967b6..edb5e1e 100644 --- a/sources/test/testnetctl.cpp +++ b/sources/test/testnetctl.cpp @@ -27,7 +27,6 @@ Netctl *TestNetctl::createNetctlObj() { QMap settings = Netctl::getRecommendedConfiguration(); settings[QString("FORCE_SUDO")] = QString("true"); - // to test netctl-auto with dummy profiles settings[QString("PREFERED_IFACE")] = QString("ngtest"); Netctl *netctl = new Netctl(false, settings); @@ -73,6 +72,13 @@ void TestNetctl::initTestCase() qDebug() << "TODO: unfortunately, some functions which is required to work"; qDebug() << "with the working profile isn't tested here (including netctl-auto)"; QWARN("Some functions requires root privileges"); + createTestProfile(); +} + + +void TestNetctl::cleanupTestCase() +{ + removeTestProfile(); } @@ -107,11 +113,9 @@ void TestNetctl::test_getRecommendedConfiguration() void TestNetctl::test_getActiveProfile() { Netctl *netctl = createNetctlObj(); - createTestProfile(); netctl->startProfile(QString("netctlgui-test-dummy")); QStringList result = netctl->getActiveProfile(); netctl->startProfile(QString("netctlgui-test-dummy")); - removeTestProfile(); delete netctl; QVERIFY(result.contains(QString("netctlgui-test-dummy"))); @@ -121,10 +125,8 @@ void TestNetctl::test_getActiveProfile() void TestNetctl::test_getProfileDescription() { Netctl *netctl = createNetctlObj(); - createTestProfile(); QString original = QString("Simple test profile"); QString result = netctl->getProfileDescription(QString("netctlgui-test-dummy")); - removeTestProfile(); delete netctl; QCOMPARE(result, original); @@ -134,7 +136,6 @@ void TestNetctl::test_getProfileDescription() void TestNetctl::test_getProfileStatus() { Netctl *netctl = createNetctlObj(); - createTestProfile(); QStringList original; original.append(QString("inactive (static)")); original.append(QString("active (static)")); @@ -151,7 +152,6 @@ void TestNetctl::test_getProfileStatus() result.append(netctl->getProfileStatus(QString("netctlgui-test-dummy"))); netctl->enableProfile(QString("netctlgui-test-dummy")); result.append(netctl->getProfileStatus(QString("netctlgui-test-dummy"))); - removeTestProfile(); delete netctl; QCOMPARE(result, original); @@ -161,13 +161,10 @@ void TestNetctl::test_getProfileStatus() void TestNetctl::test_isProfileActive() { Netctl *netctl = createNetctlObj(); - createTestProfile(); QVERIFY(!netctl->isProfileActive(QString("netctlgui-test-dummy"))); netctl->startProfile(QString("netctlgui-test-dummy")); QVERIFY(netctl->isProfileActive(QString("netctlgui-test-dummy"))); netctl->startProfile(QString("netctlgui-test-dummy")); - - removeTestProfile(); delete netctl; } @@ -175,13 +172,10 @@ void TestNetctl::test_isProfileActive() void TestNetctl::test_isProfileEnabled() { Netctl *netctl = createNetctlObj(); - createTestProfile(); QVERIFY(!netctl->isProfileEnabled(QString("netctlgui-test-dummy"))); netctl->enableProfile(QString("netctlgui-test-dummy")); QVERIFY(netctl->isProfileEnabled(QString("netctlgui-test-dummy"))); netctl->enableProfile(QString("netctlgui-test-dummy")); - - removeTestProfile(); delete netctl; } @@ -189,15 +183,12 @@ void TestNetctl::test_isProfileEnabled() void TestNetctl::test_reenableProfile() { Netctl *netctl = createNetctlObj(); - createTestProfile(); QVERIFY(!netctl->isProfileEnabled(QString("netctlgui-test-dummy"))); netctl->enableProfile(QString("netctlgui-test-dummy")); QVERIFY(netctl->isProfileEnabled(QString("netctlgui-test-dummy"))); QVERIFY(netctl->reenableProfile(QString("netctlgui-test-dummy"))); QVERIFY(netctl->isProfileEnabled(QString("netctlgui-test-dummy"))); netctl->enableProfile(QString("netctlgui-test-dummy")); - - removeTestProfile(); delete netctl; } @@ -205,15 +196,12 @@ void TestNetctl::test_reenableProfile() void TestNetctl::test_restartProfile() { Netctl *netctl = createNetctlObj(); - createTestProfile(); QVERIFY(!netctl->isProfileActive(QString("netctlgui-test-dummy"))); netctl->startProfile(QString("netctlgui-test-dummy")); QVERIFY(netctl->isProfileActive(QString("netctlgui-test-dummy"))); QVERIFY(netctl->restartProfile(QString("netctlgui-test-dummy"))); QVERIFY(netctl->isProfileActive(QString("netctlgui-test-dummy"))); netctl->startProfile(QString("netctlgui-test-dummy")); - - removeTestProfile(); delete netctl; } diff --git a/sources/test/testnetctl.h b/sources/test/testnetctl.h index 90d1f2a..a632106 100644 --- a/sources/test/testnetctl.h +++ b/sources/test/testnetctl.h @@ -31,6 +31,7 @@ class TestNetctl : public QObject private slots: // initialization void initTestCase(); + void cleanupTestCase(); // netctl void test_getRecommendedConfiguration(); void test_getActiveProfile(); diff --git a/sources/test/testnetctlauto.cpp b/sources/test/testnetctlauto.cpp index 144324c..e06421d 100644 --- a/sources/test/testnetctlauto.cpp +++ b/sources/test/testnetctlauto.cpp @@ -80,6 +80,13 @@ void TestNetctlAuto::initTestCase() qDebug() << "TODO: unfortunately, some functions which is required to work"; qDebug() << "with the working profile isn't tested here (including netctl-auto)"; QWARN("Some functions requires root privileges"); + createTestProfiles(); +} + + +void TestNetctlAuto::cleanupTestCase() +{ + removeTestProfiles(); } diff --git a/sources/test/testnetctlauto.h b/sources/test/testnetctlauto.h index 08c5cf2..e1322b1 100644 --- a/sources/test/testnetctlauto.h +++ b/sources/test/testnetctlauto.h @@ -31,6 +31,7 @@ class TestNetctlAuto : public QObject private slots: // initialization void initTestCase(); + void cleanupTestCase(); // netctl-auto private: diff --git a/sources/test/testnetctlprofile.cpp b/sources/test/testnetctlprofile.cpp new file mode 100644 index 0000000..d0c6f6f --- /dev/null +++ b/sources/test/testnetctlprofile.cpp @@ -0,0 +1,246 @@ +/*************************************************************************** + * This file is part of netctl-gui * + * * + * netctl-gui is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * netctl-gui 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with netctl-gui. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#include "testnetctlprofile.h" + +#include + +#include + + +NetctlProfile *TestNetctlProfile::createNetctlProfileObj() +{ + QMap settings = NetctlProfile::getRecommendedConfiguration(); + settings[QString("FORCE_SUDO")] = QString("true"); + NetctlProfile *netctl = new NetctlProfile(false, settings); + + return netctl; +} + + +bool TestNetctlProfile::createTestProfile() +{ + NetctlProfile *netctl = createNetctlProfileObj(); + QMap profileSettings; + profileSettings["Connection"] = QString("dummy"); + profileSettings["Description"] = QString("\"Simple test profile\""); + profileSettings["IP"] = QString("no"); + profileSettings["IP6"] = QString("no"); + profileSettings["Interface"] = QString("ngtest"); + bool status = netctl->copyProfile(netctl->createProfile(QString("netctlgui-test-dummy"), profileSettings)); + delete netctl; + + return status; +} + + +bool TestNetctlProfile::removeTestProfile() +{ + NetctlProfile *netctl = createNetctlProfileObj(); + bool status = netctl->removeProfile(QString("netctlgui-test-dummy")); + delete netctl; + + return status; +} + + +void TestNetctlProfile::initTestCase() +{ + qDebug() << "netctlgui library tests"; + qDebug() << "TODO: unfortunately, some functions which is required to work"; + qDebug() << "with the working profile isn't tested here (including netctl-auto)"; + QWARN("Some functions requires root privileges"); + // arent needed +// createTestProfile(); +} + + +void TestNetctlProfile::cleanupTestCase() +{ + // arent needed +// removeTestProfile(); +} + + +void TestNetctlProfile::test_getRecommendedConfiguration() +{ + QStringList original; + original.append(QString("FORCE_SUDO==false")); + original.append(QString("PROFILE_DIR==/etc/netctl")); + original.append(QString("SUDO_PATH==/usr/bin/sudo")); + QMap resultMap = NetctlProfile::getRecommendedConfiguration(); + QStringList result; + for (int i=0; igetValueFromProfile(QString("netctlgui-test-dummy"), + QString("Connection"))); + result.append(netctl->getValueFromProfile(QString("netctlgui-test-dummy"), + QString("Description"))); + result.append(netctl->getValueFromProfile(QString("netctlgui-test-dummy"), + QString("IP"))); + result.append(netctl->getValueFromProfile(QString("netctlgui-test-dummy"), + QString("IP6"))); + result.append(netctl->getValueFromProfile(QString("netctlgui-test-dummy"), + QString("Interface"))); + delete netctl; + + QCOMPARE(result, original); +} + + +void TestNetctlProfile::test_createProfile() +{ + NetctlProfile *netctl = createNetctlProfileObj(); + QMap profileSettings; + // cat /etc/netctl/examples/* | sort | uniq | grep -v '^#' | less + profileSettings["AccessPointName"] = QString("apn"); + profileSettings["Address6"] = QString("'2001:470:1f08:d87::2/64'"); + profileSettings["Address"] = QString("'192.168.1.23/24' '192.168.1.87/24'"); + profileSettings["BindsToInterfaces"] = QString("eth0 eth1 tap0"); + profileSettings["Connection"] = QString("bond"); + profileSettings["DNS"] = QString("'192.168.1.1'"); + profileSettings["DNSDomain"] = QString("\"mydomain.com\""); + profileSettings["DNSSearch"] = QString("\"mydomain.com\""); + profileSettings["Description"] = QString("\"netctlgui full test profile\""); + profileSettings["ESSID"] = QString("'MyNetwork'"); + profileSettings["Gateway"] = QString("'192.168.1.1'"); + profileSettings["Group"] = QString("'nobody'"); + profileSettings["Hostname"] = QString("\"ponyhost\""); + profileSettings["IP6"] = QString("static"); + profileSettings["IP"] = QString("dhcp"); + profileSettings["IPCustom"] = QString("'addr add dev eth0 192.168.1.23/24 brd +' 'route add default via 192.168.1.1'"); + profileSettings["Interface"] = QString("eth0"); + profileSettings["Key"] = QString("\\\"1234567890abcdef"); + profileSettings["MACAddress"] = QString("\"12:34:56:78:9a:bc\""); + profileSettings["Mode"] = QString("'sit'"); + profileSettings["Password"] = QString("'very secret'"); + profileSettings["Remote"] = QString("'216.66.80.26'"); + profileSettings["Routes6"] = QString("'::/0'"); + profileSettings["Security"] = QString("none"); + profileSettings["User"] = QString("'example@yourprovider.com'"); + profileSettings["VLANID"] = QString("11"); + profileSettings["WPAConfigFile"] = QString("'/etc/wpa_supplicant.conf'"); + profileSettings["WPAConfigSection"] = QString("\n\ +'ssid=\"University\"'\n\ +'key_mgmt=WPA-EAP'\n\ +'eap=TTLS'\n\ +'group=TKIP'\n\ +'pairwise=TKIP CCMP'\n\ +'anonymous_identity=\"anonymous\"'\n\ +'identity=\"myusername\"'\n\ +'password=\"mypassword\"'\n\ +'priority=1'\n\ +'phase2=\"auth=PAP\"'\n\ +"); + QVERIFY(netctl->copyProfile(netctl->createProfile(QString("netctlgui-test-full"), profileSettings))); + delete netctl; +} + + +void TestNetctlProfile::test_getSettingsFromProfile() +{ + NetctlProfile *netctl = createNetctlProfileObj(); + QStringList original; + // cat /etc/netctl/examples/* | sort | uniq | grep -v '^#' | less + original.append("AccessPointName==apn"); + original.append("Address==192.168.1.23/24\n\ +192.168.1.87/24"); + original.append("Address6==2001:470:1f08:d87::2/64"); + original.append("BindsToInterfaces==eth0\n\ +eth1\n\ +tap0"); + original.append("Connection==bond"); + original.append("DNS==192.168.1.1"); + original.append("DNSDomain==mydomain.com"); + original.append("DNSSearch==mydomain.com"); + original.append("Description==netctlgui full test profile"); + original.append("ESSID==MyNetwork"); + original.append("Gateway==192.168.1.1"); + original.append("Group==nobody"); + original.append("Hostname==ponyhost"); + original.append("IP==dhcp"); + original.append("IP6==static"); + original.append("IPCustom==addr add dev eth0 192.168.1.23/24 brd +\n\ +route add default via 192.168.1.1"); + original.append("Interface==eth0"); + original.append("Key==\"1234567890abcdef"); + original.append("MACAddress==12:34:56:78:9a:bc"); + original.append("Mode==sit"); + original.append("Password==very secret"); + original.append("Remote==216.66.80.26"); + original.append("Routes6==::/0"); + original.append("Security==none"); + original.append("User==example@yourprovider.com"); + original.append("VLANID==11"); + original.append("WPAConfigFile==/etc/wpa_supplicant.conf"); + original.append("WPAConfigSection==ssid=\"University\"\n\ +key_mgmt=WPA-EAP\n\ +eap=TTLS\n\ +group=TKIP\n\ +pairwise=TKIP CCMP\n\ +anonymous_identity=\"anonymous\"\n\ +identity=\"myusername\"\n\ +password=\"mypassword\"\n\ +priority=1\n\ +phase2=\"auth=PAP\"\ +"); + QStringList result; + QMap resultMap = netctl->getSettingsFromProfile(QString("netctlgui-test-full")); + for (int i=0; iremoveProfile(QString("netctlgui-test-full")); + delete netctl; + + QCOMPARE(result, original); +} + + +void TestNetctlProfile::test_removeProfile() +{ + NetctlProfile *netctl = createNetctlProfileObj(); + QVERIFY(removeTestProfile()); + delete netctl; +} + + +QTEST_MAIN(TestNetctlProfile); diff --git a/sources/test/testnetctlprofile.h b/sources/test/testnetctlprofile.h new file mode 100644 index 0000000..df9ec57 --- /dev/null +++ b/sources/test/testnetctlprofile.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * This file is part of netctl-gui * + * * + * netctl-gui is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * netctl-gui 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with netctl-gui. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#ifndef TESTNETCTLPROFILE_H +#define TESTNETCTLPROFILE_H + +#include + + +class NetctlProfile; + +class TestNetctlProfile : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // netctl profile + void test_getRecommendedConfiguration(); + void test_copyProfile(); + void test_createProfile(); + void test_getSettingsFromProfile(); + void test_getValueFromProfile(); + void test_removeProfile(); + +private: + NetctlProfile *createNetctlProfileObj(); + bool createTestProfile(); + bool removeTestProfile(); +}; + + +#endif /* TESTNETCTLPROFILE_H */