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