File indexing completed on 2024-12-08 12:26:17

0001 /*
0002     SPDX-FileCopyrightText: 2014 Jan Grulich <jgrulich@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "settingstest.h"
0008 
0009 #include "settings.h"
0010 #include "settings/ipv4setting.h"
0011 #include "settings/ipv6setting.h"
0012 #include "settings/wiredsetting.h"
0013 
0014 #include "fakenetwork/settings.h"
0015 
0016 #include <QSignalSpy>
0017 #include <QTest>
0018 
0019 void SettingsTest::initTestCase()
0020 {
0021     fakeNetwork = new FakeNetwork();
0022 }
0023 
0024 void SettingsTest::testConnections()
0025 {
0026     NetworkManager::ConnectionSettings::Ptr connectionSettings =
0027         NetworkManager::ConnectionSettings::Ptr(new NetworkManager::ConnectionSettings(NetworkManager::ConnectionSettings::Wired));
0028     connectionSettings->setId("Wired connection");
0029     connectionSettings->setUuid(QLatin1String("39af79a5-b053-4893-9378-7342a5a30d06"));
0030     NetworkManager::Ipv4Setting::Ptr ipv4Setting = connectionSettings->setting(NetworkManager::Setting::Ipv4).dynamicCast<NetworkManager::Ipv4Setting>();
0031     ipv4Setting->setInitialized(true);
0032     ipv4Setting->setMethod(NetworkManager::Ipv4Setting::Automatic);
0033     NetworkManager::Ipv6Setting::Ptr ipv6Setting = connectionSettings->setting(NetworkManager::Setting::Ipv6).dynamicCast<NetworkManager::Ipv6Setting>();
0034     ipv6Setting->setInitialized(true);
0035     ipv6Setting->setMethod(NetworkManager::Ipv6Setting::Automatic);
0036     NetworkManager::WiredSetting::Ptr wiredSetting = connectionSettings->setting(NetworkManager::Setting::Wired).dynamicCast<NetworkManager::WiredSetting>();
0037     wiredSetting->setInitialized(true);
0038     // Something needs to be set to not use default values, when using default values we get an empty map
0039     wiredSetting->setAutoNegotiate(false);
0040     wiredSetting->setSpeed(100);
0041     wiredSetting->setDuplexType(NetworkManager::WiredSetting::Full);
0042 
0043     NetworkManager::addConnection(connectionSettings->toMap());
0044     connect(NetworkManager::settingsNotifier(), &NetworkManager::SettingsNotifier::connectionAdded, this, &SettingsTest::testConnectionAdded);
0045     QSignalSpy connectionAddedSpy(NetworkManager::settingsNotifier(), SIGNAL(connectionAdded(QString)));
0046     QVERIFY(connectionAddedSpy.wait());
0047     QCOMPARE(NetworkManager::listConnections().count(), 1);
0048     QCOMPARE(NetworkManager::listConnections().first()->path(), connectionAddedSpy.at(0).at(0).toString());
0049 
0050     NetworkManager::Connection::Ptr connection = NetworkManager::listConnections().first();
0051     QSignalSpy connectionRemovedSpy(NetworkManager::settingsNotifier(), SIGNAL(connectionRemoved(QString)));
0052     connection->remove();
0053     QVERIFY(connectionRemovedSpy.wait());
0054     QCOMPARE(NetworkManager::listConnections().count(), 0);
0055     QCOMPARE(connection->path(), connectionRemovedSpy.at(0).at(0).toString());
0056 
0057     // Compare hostname we set in fake network
0058     QCOMPARE(NetworkManager::hostname(), QLatin1String("fake-hostname"));
0059 }
0060 
0061 void SettingsTest::testConnectionAdded(const QString &connection)
0062 {
0063     NetworkManager::Connection::Ptr addedConnection = NetworkManager::findConnection(connection);
0064     QCOMPARE(addedConnection->uuid(), QLatin1String("39af79a5-b053-4893-9378-7342a5a30d06"));
0065     QCOMPARE(addedConnection->path(), connection);
0066 
0067     NetworkManager::ConnectionSettings::Ptr connectionSettings = addedConnection->settings();
0068     NetworkManager::Ipv4Setting::Ptr ipv4Setting = connectionSettings->setting(NetworkManager::Setting::Ipv4).dynamicCast<NetworkManager::Ipv4Setting>();
0069     QCOMPARE(ipv4Setting->method(), NetworkManager::Ipv4Setting::Automatic);
0070     NetworkManager::Ipv6Setting::Ptr ipv6Setting = connectionSettings->setting(NetworkManager::Setting::Ipv6).dynamicCast<NetworkManager::Ipv6Setting>();
0071     QCOMPARE(ipv6Setting->method(), NetworkManager::Ipv6Setting::Automatic);
0072     NetworkManager::WiredSetting::Ptr wiredSetting = connectionSettings->setting(NetworkManager::Setting::Wired).dynamicCast<NetworkManager::WiredSetting>();
0073     QVERIFY(wiredSetting->autoNegotiate() == false);
0074     QVERIFY(wiredSetting->speed() == 100);
0075     QVERIFY(wiredSetting->duplexType() == NetworkManager::WiredSetting::Full);
0076     wiredSetting->setSpeed(10);
0077     wiredSetting->setDuplexType(NetworkManager::WiredSetting::Half);
0078 
0079     addedConnection->update(connectionSettings->toMap());
0080     QSignalSpy connectionUpdatedSpy(addedConnection.data(), SIGNAL(updated()));
0081     QVERIFY(connectionUpdatedSpy.wait());
0082     QVERIFY(wiredSetting->speed() == 10);
0083     QVERIFY(wiredSetting->duplexType() == NetworkManager::WiredSetting::Half);
0084 }
0085 
0086 QTEST_MAIN(SettingsTest)
0087 
0088 #include "moc_settingstest.cpp"