File indexing completed on 2024-12-01 09:58:03

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 "activeconnectiontest.h"
0008 
0009 #include "device.h"
0010 #include "manager.h"
0011 #include "settings.h"
0012 #include "settings/ipv4setting.h"
0013 #include "settings/ipv6setting.h"
0014 #include "settings/wiredsetting.h"
0015 
0016 #include "fakenetwork/settings.h"
0017 #include "fakenetwork/wireddevice.h"
0018 
0019 #include <QSignalSpy>
0020 #include <QTest>
0021 
0022 void ActiveConnectionTest::initTestCase()
0023 {
0024     fakeNetwork = new FakeNetwork();
0025 
0026     WiredDevice *device = new WiredDevice();
0027     /* Device properties */
0028     device->setAutoconnect(true);
0029     device->setCapabilities(3);
0030     device->setDeviceType(1);
0031     device->setDriver(QLatin1String("e1000e"));
0032     device->setDriverVersion(QLatin1String("2.3.2-k"));
0033     device->setFirmwareMissing(false);
0034     device->setFirmwareVersion(QLatin1String("0.13-3"));
0035     device->setInterface(QLatin1String("em1"));
0036     device->setManaged(true);
0037     device->setUdi(QLatin1String("/sys/devices/pci0000:00/0000:00:19.0/net/em1"));
0038 
0039     /* Wired device properties */
0040     device->setCarrier(true);
0041     device->setHwAddress(QLatin1String("F0:DE:F1:FB:30:C1"));
0042     device->setPermanentHwAddress(QLatin1String("F0:DE:F1:FB:30:C1"));
0043 
0044     fakeNetwork->addDevice(device);
0045 
0046     NetworkManager::ConnectionSettings::Ptr connectionSettings =
0047         NetworkManager::ConnectionSettings::Ptr(new NetworkManager::ConnectionSettings(NetworkManager::ConnectionSettings::Wired));
0048     connectionSettings->setId("Wired connection");
0049     connectionSettings->setUuid(QLatin1String("39af79a5-b053-4893-9378-7342a5a30d06"));
0050     NetworkManager::Ipv4Setting::Ptr ipv4Setting = connectionSettings->setting(NetworkManager::Setting::Ipv4).dynamicCast<NetworkManager::Ipv4Setting>();
0051     ipv4Setting->setInitialized(true);
0052     ipv4Setting->setMethod(NetworkManager::Ipv4Setting::Automatic);
0053     NetworkManager::Ipv6Setting::Ptr ipv6Setting = connectionSettings->setting(NetworkManager::Setting::Ipv6).dynamicCast<NetworkManager::Ipv6Setting>();
0054     ipv6Setting->setInitialized(true);
0055     ipv6Setting->setMethod(NetworkManager::Ipv6Setting::Automatic);
0056     NetworkManager::WiredSetting::Ptr wiredSetting = connectionSettings->setting(NetworkManager::Setting::Wired).dynamicCast<NetworkManager::WiredSetting>();
0057     wiredSetting->setInitialized(true);
0058     // Something needs to be set to not use default values, when using default values we get an empty map
0059     wiredSetting->setSpeed(100);
0060 
0061     NetworkManager::Device::Ptr wiredDevice = NetworkManager::networkInterfaces().first().objectCast<NetworkManager::Device>();
0062     QSignalSpy availableConnectionAppearedSpy(wiredDevice.data(), SIGNAL(availableConnectionAppeared(QString)));
0063     NetworkManager::addConnection(connectionSettings->toMap());
0064     QVERIFY(availableConnectionAppearedSpy.wait());
0065 }
0066 
0067 void ActiveConnectionTest::testActiveConnection()
0068 {
0069     qRegisterMetaType<NetworkManager::ActiveConnection::State>("NetworkManager::ActiveConnection::State");
0070     qRegisterMetaType<NetworkManager::ActiveConnection::Reason>("NetworkManager::ActiveConnection::Reason");
0071     qRegisterMetaType<NetworkManager::Device::State>("NetworkManager::Device::State");
0072     qRegisterMetaType<NetworkManager::Device::StateChangeReason>("NetworkManager::Device::StateChangeReason");
0073     qRegisterMetaType<NetworkManager::Connectivity>("NetworkManager::Connectivity");
0074     qRegisterMetaType<NetworkManager::Status>("NetworkManager::Status");
0075 
0076     NetworkManager::Device::Ptr device = NetworkManager::networkInterfaces().first();
0077     QSignalSpy deviceActiveConnectionChangedSpy(device.data(), SIGNAL(activeConnectionChanged()));
0078     QSignalSpy deviceIpInterfaceChangedSpy(device.data(), SIGNAL(ipInterfaceChanged()));
0079     QSignalSpy deviceStateChangedSpy(
0080         device.data(),
0081         SIGNAL(stateChanged(NetworkManager::Device::State, NetworkManager::Device::State, NetworkManager::Device::StateChangeReason)));
0082 
0083     NetworkManager::Connection::Ptr connection = device->availableConnections().first();
0084 
0085     QSignalSpy activeConnectionAddedSpy(NetworkManager::notifier(), SIGNAL(activeConnectionAdded(QString)));
0086     QSignalSpy activatingConnectionChangedSpy(NetworkManager::notifier(), SIGNAL(activatingConnectionChanged(QString)));
0087     QSignalSpy connectivityChangedSpy(NetworkManager::notifier(), SIGNAL(connectivityChanged(NetworkManager::Connectivity)));
0088     QSignalSpy primaryConnectionChangedSpy(NetworkManager::notifier(), SIGNAL(primaryConnectionChanged(QString)));
0089     QSignalSpy stateChangedSpy(NetworkManager::notifier(), SIGNAL(statusChanged(NetworkManager::Status)));
0090 
0091     NetworkManager::activateConnection(connection->path(), device->uni(), QString());
0092     QVERIFY(activeConnectionAddedSpy.wait());
0093 
0094     NetworkManager::ActiveConnection::Ptr activeConnection = NetworkManager::findActiveConnection(activeConnectionAddedSpy.at(0).at(0).toString());
0095     QSignalSpy activeConnectionStateChangedSpy(activeConnection.data(), SIGNAL(stateChanged(NetworkManager::ActiveConnection::State)));
0096     QSignalSpy activeConnectionStateChangedReasonSpy(
0097         activeConnection.data(),
0098         SIGNAL(stateChangedReason(NetworkManager::ActiveConnection::State, NetworkManager::ActiveConnection::Reason)));
0099 
0100     QCOMPARE(activeConnection->devices().first(), device->uni());
0101     QCOMPARE(activeConnection->state(), NetworkManager::ActiveConnection::Activating);
0102     QCOMPARE(activeConnection->uuid(), connection->uuid());
0103 
0104     QCOMPARE(NetworkManager::activatingConnection()->uuid(), connection->uuid());
0105 
0106     // Wait until device goes through all states (PREPARE, CONFIG, NEED_AUTH, IP_CONFIG, IP_CHECK, ACTIVATED)
0107     QTest::qWait(800);
0108 
0109     QCOMPARE(activeConnectionStateChangedSpy.count(), 1);
0110     QCOMPARE(activeConnectionStateChangedReasonSpy.count(), 1);
0111 
0112     QCOMPARE(deviceStateChangedSpy.count(), 6);
0113     QCOMPARE(device->ipInterfaceName(), device->interfaceName());
0114     QCOMPARE(device->state(), NetworkManager::Device::Activated);
0115     QCOMPARE(deviceActiveConnectionChangedSpy.count(), 1);
0116     QCOMPARE(deviceIpInterfaceChangedSpy.count(), 1);
0117 
0118     QCOMPARE(NetworkManager::connectivity(), NetworkManager::Full);
0119     QCOMPARE(NetworkManager::primaryConnection()->uuid(), connection->uuid());
0120     QCOMPARE(NetworkManager::status(), NetworkManager::Status::Connected);
0121     QCOMPARE(activatingConnectionChangedSpy.count(), 2);
0122     QCOMPARE(connectivityChangedSpy.count(), 1);
0123     QCOMPARE(primaryConnectionChangedSpy.count(), 1);
0124     QCOMPARE(stateChangedSpy.count(), 1);
0125 
0126     NetworkManager::deactivateConnection(activeConnection->path());
0127 
0128     // Wait until we are disconnected
0129     QTest::qWait(300);
0130 
0131     QVERIFY(device->ipInterfaceName().isEmpty());
0132     QCOMPARE(device->state(), NetworkManager::Device::Disconnected);
0133 
0134     QCOMPARE(NetworkManager::activeConnections().count(), 0);
0135     QCOMPARE(NetworkManager::connectivity(), NetworkManager::NoConnectivity);
0136     QVERIFY(NetworkManager::primaryConnection().isNull());
0137     QCOMPARE(NetworkManager::status(), NetworkManager::Disconnected);
0138 }
0139 
0140 QTEST_MAIN(ActiveConnectionTest)
0141 
0142 #include "moc_activeconnectiontest.cpp"