File indexing completed on 2024-12-08 09:45:45
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 "managertest.h" 0008 0009 #include "manager.h" 0010 #include "wireddevice.h" 0011 0012 #include "fakenetwork/wireddevice.h" 0013 0014 #include <QSignalSpy> 0015 #include <QTest> 0016 0017 void ManagerTest::initTestCase() 0018 { 0019 fakeNetwork = new FakeNetwork(); 0020 } 0021 0022 void ManagerTest::testDevices() 0023 { 0024 WiredDevice *device = new WiredDevice(); 0025 /* Device properties */ 0026 device->setAutoconnect(true); 0027 device->setCapabilities(3); 0028 device->setDeviceType(1); 0029 device->setDriver(QLatin1String("e1000e")); 0030 device->setDriverVersion(QLatin1String("2.3.2-k")); 0031 device->setFirmwareMissing(false); 0032 device->setFirmwareVersion(QLatin1String("0.13-3")); 0033 device->setInterface(QLatin1String("em1")); 0034 device->setManaged(true); 0035 device->setUdi(QLatin1String("/sys/devices/pci0000:00/0000:00:19.0/net/em1")); 0036 0037 /* Wired device properties */ 0038 device->setCarrier(true); 0039 device->setHwAddress(QLatin1String("F0:DE:F1:FB:30:C1")); 0040 device->setPermanentHwAddress(QLatin1String("F0:DE:F1:FB:30:C1")); 0041 0042 QSignalSpy addDeviceSpy(NetworkManager::notifier(), SIGNAL(deviceAdded(QString))); 0043 connect(NetworkManager::notifier(), &NetworkManager::Notifier::deviceAdded, this, &ManagerTest::testDeviceAdded); 0044 fakeNetwork->addDevice(device); 0045 QVERIFY(addDeviceSpy.wait()); 0046 QCOMPARE(NetworkManager::networkInterfaces().count(), 1); 0047 QCOMPARE(NetworkManager::networkInterfaces().first()->uni(), addDeviceSpy.at(0).at(0).toString()); 0048 const QString addedDevicePath = NetworkManager::networkInterfaces().first()->uni(); 0049 0050 QSignalSpy removeDeviceSpy(NetworkManager::notifier(), SIGNAL(deviceRemoved(QString))); 0051 fakeNetwork->removeDevice(device); 0052 QVERIFY(removeDeviceSpy.wait()); 0053 QVERIFY(NetworkManager::networkInterfaces().isEmpty()); 0054 QCOMPARE(removeDeviceSpy.at(0).at(0).toString(), addedDevicePath); 0055 0056 addDeviceSpy.clear(); 0057 0058 fakeNetwork->addDevice(device); 0059 QVERIFY(addDeviceSpy.wait()); 0060 QCOMPARE(NetworkManager::networkInterfaces().count(), 1); 0061 QCOMPARE(NetworkManager::networkInterfaces().first()->uni(), addDeviceSpy.at(0).at(0).toString()); 0062 0063 addDeviceSpy.clear(); 0064 removeDeviceSpy.clear(); 0065 0066 fakeNetwork->unregisterService(); 0067 QTRY_COMPARE(removeDeviceSpy.count(), 1); 0068 0069 fakeNetwork->registerService(); 0070 QTRY_COMPARE(addDeviceSpy.count(), 1); 0071 0072 // Make sure deviceAdded is emitted only once 0073 addDeviceSpy.wait(100); 0074 QCOMPARE(addDeviceSpy.count(), 1); 0075 0076 delete device; 0077 } 0078 0079 void ManagerTest::testDeviceAdded(const QString &dev) 0080 { 0081 NetworkManager::Device::Ptr device = NetworkManager::findNetworkInterface(dev); 0082 0083 QCOMPARE(device->autoconnect(), true); 0084 QCOMPARE(device->capabilities(), 3); 0085 QCOMPARE(device->type(), NetworkManager::Device::Ethernet); 0086 QCOMPARE(device->driver(), QLatin1String("e1000e")); 0087 QCOMPARE(device->driverVersion(), QLatin1String("2.3.2-k")); 0088 QCOMPARE(device->firmwareMissing(), false); 0089 QCOMPARE(device->firmwareVersion(), QLatin1String("0.13-3")); 0090 QCOMPARE(device->interfaceName(), QLatin1String("em1")); 0091 QCOMPARE(device->managed(), true); 0092 QCOMPARE(device->udi(), QLatin1String("/sys/devices/pci0000:00/0000:00:19.0/net/em1")); 0093 0094 NetworkManager::WiredDevice::Ptr wiredDevice = device.objectCast<NetworkManager::WiredDevice>(); 0095 0096 QCOMPARE(wiredDevice->carrier(), true); 0097 QCOMPARE(wiredDevice->hardwareAddress(), QLatin1String("F0:DE:F1:FB:30:C1")); 0098 QCOMPARE(wiredDevice->permanentHardwareAddress(), QLatin1String("F0:DE:F1:FB:30:C1")); 0099 } 0100 0101 void ManagerTest::testManager() 0102 { 0103 QCOMPARE(NetworkManager::connectivity(), NetworkManager::NoConnectivity); 0104 // QCOMPARE(NetworkManager::status(), NetworkManager::Disconnected); 0105 QCOMPARE(NetworkManager::version(), QLatin1String("0.9.10.0")); 0106 0107 QSignalSpy wirelessEnabledChanged(NetworkManager::notifier(), SIGNAL(wirelessEnabledChanged(bool))); 0108 QCOMPARE(NetworkManager::isWirelessEnabled(), true); 0109 fakeNetwork->setWirelessEnabled(false); 0110 QVERIFY(wirelessEnabledChanged.wait()); 0111 QCOMPARE(wirelessEnabledChanged.count(), 1); 0112 QVERIFY(wirelessEnabledChanged.at(0).at(0).toBool() == false); 0113 QCOMPARE(NetworkManager::isWirelessEnabled(), false); 0114 0115 QSignalSpy wirelessHwEnabledChanged(NetworkManager::notifier(), SIGNAL(wirelessHardwareEnabledChanged(bool))); 0116 QCOMPARE(NetworkManager::isWirelessHardwareEnabled(), true); 0117 fakeNetwork->setWirelessHardwareEnabled(false); 0118 QVERIFY(wirelessHwEnabledChanged.wait()); 0119 QCOMPARE(wirelessHwEnabledChanged.count(), 1); 0120 QVERIFY(wirelessHwEnabledChanged.at(0).at(0).toBool() == false); 0121 QCOMPARE(NetworkManager::isWirelessHardwareEnabled(), false); 0122 0123 QSignalSpy wimaxEnabledChanged(NetworkManager::notifier(), SIGNAL(wimaxEnabledChanged(bool))); 0124 QCOMPARE(NetworkManager::isWimaxEnabled(), true); 0125 fakeNetwork->setWimaxEnabled(false); 0126 QVERIFY(wimaxEnabledChanged.wait()); 0127 QCOMPARE(wimaxEnabledChanged.count(), 1); 0128 QVERIFY(wimaxEnabledChanged.at(0).at(0).toBool() == false); 0129 QCOMPARE(NetworkManager::isWimaxEnabled(), false); 0130 0131 QSignalSpy wimaxHwEnabledChanged(NetworkManager::notifier(), SIGNAL(wimaxHardwareEnabledChanged(bool))); 0132 QCOMPARE(NetworkManager::isWimaxHardwareEnabled(), true); 0133 fakeNetwork->setWimaxHardwareEnabled(false); 0134 QVERIFY(wimaxHwEnabledChanged.wait()); 0135 QCOMPARE(wimaxHwEnabledChanged.count(), 1); 0136 QVERIFY(wimaxHwEnabledChanged.at(0).at(0).toBool() == false); 0137 QCOMPARE(NetworkManager::isWimaxHardwareEnabled(), false); 0138 0139 QSignalSpy wwanEnabledChanged(NetworkManager::notifier(), SIGNAL(wwanEnabledChanged(bool))); 0140 QCOMPARE(NetworkManager::isWwanEnabled(), true); 0141 fakeNetwork->setWwanEnabled(false); 0142 QVERIFY(wwanEnabledChanged.wait()); 0143 QCOMPARE(wwanEnabledChanged.count(), 1); 0144 QVERIFY(wwanEnabledChanged.at(0).at(0).toBool() == false); 0145 QCOMPARE(NetworkManager::isWwanEnabled(), false); 0146 0147 // We make it here, so we can set all values back for futher testing 0148 fakeNetwork->setWirelessEnabled(true); 0149 fakeNetwork->setWirelessHardwareEnabled(true); 0150 fakeNetwork->setWimaxEnabled(true); 0151 fakeNetwork->setWimaxHardwareEnabled(true); 0152 fakeNetwork->setWwanEnabled(true); 0153 } 0154 0155 QTEST_MAIN(ManagerTest) 0156 0157 #include "moc_managertest.cpp"