File indexing completed on 2024-05-19 04:02:18

0001 /*
0002     SPDX-FileCopyrightText: 2015 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 "modemtest.h"
0008 
0009 #include "manager.h"
0010 #include "modem.h"
0011 
0012 #include "fakemodem/modem.h"
0013 
0014 #include <QSignalSpy>
0015 #include <QTest>
0016 
0017 void ModemTest::initTestCase()
0018 {
0019     fakeModem = new FakeModem();
0020 }
0021 
0022 void ModemTest::testModems()
0023 {
0024     Modem *modem = new Modem();
0025     modem->setAccessTechnologies(16);
0026     modem->SetCurrentBands({0});
0027     modem->SetCurrentCapabilities(4);
0028     modem->SetCurrentModes({MM_MODEM_MODE_ANY, MM_MODEM_MODE_NONE});
0029     modem->setDevice(QLatin1String("/sys/devices/pci0000:00/0000:00:1d.0/usb4/4-1/4-1.2"));
0030     modem->setDeviceIdentifier(QLatin1String("1c435eb6d74494b5f78d7221e2c5ae9ec526a981"));
0031     modem->setDrivers({QLatin1String("option1")});
0032     modem->setEquipmentIdentifier(QLatin1String("353475021085110"));
0033     modem->setManufacturer(QLatin1String("huawei"));
0034     modem->setMaxActiveBearers(1);
0035     modem->setMaxBearers(1);
0036     modem->setModel(QLatin1String("K2540"));
0037     // modem->setOwnNumbers();
0038     modem->setPlugin(QLatin1String("Huawei"));
0039     modem->setPorts({{QLatin1String("ttyUSB0"), MM_MODEM_PORT_TYPE_AT},
0040                      {QLatin1String("ttyUSB1"), MM_MODEM_PORT_TYPE_QCDM},
0041                      {QLatin1String("ttyUSB2"), MM_MODEM_PORT_TYPE_AT}});
0042     modem->SetPowerState(3);
0043     modem->setPrimaryPort(QLatin1String("ttyUSB2"));
0044     modem->setRevision(QLatin1String("11.001.05.00.11"));
0045     modem->setSignalQuality({93, true});
0046     modem->setSim(QDBusObjectPath("/org/kde/fakemodem/SIM/1"));
0047     modem->setState(8);
0048     modem->setStateFailedReason(0);
0049     modem->setSupportedBands({0});
0050     modem->setSupportedCapabilities({4});
0051     modem->setSupportedIpFamilies(3);
0052     ModemManager::SupportedModesType supportedModes;
0053     ModemManager::CurrentModesType supportedMode1 = {MM_MODEM_MODE_4G & MM_MODEM_MODE_3G & MM_MODEM_MODE_2G, MM_MODEM_MODE_NONE};
0054     ModemManager::CurrentModesType supportedMode2 = {MM_MODEM_MODE_4G & MM_MODEM_MODE_3G & MM_MODEM_MODE_2G, MM_MODEM_MODE_2G};
0055     ModemManager::CurrentModesType supportedMode3 = {MM_MODEM_MODE_4G & MM_MODEM_MODE_3G & MM_MODEM_MODE_2G, MM_MODEM_MODE_3G};
0056     supportedModes << supportedMode1 << supportedMode2 << supportedMode3;
0057     modem->setSupportedModes(supportedModes);
0058     modem->setUnlockRequired(1);
0059     modem->setUnlockRetries({{MM_MODEM_LOCK_SIM_PIN, 3}, {MM_MODEM_LOCK_SIM_PIN2, 3}, {MM_MODEM_LOCK_SIM_PUK, 10}, {MM_MODEM_LOCK_SIM_PUK2, 10}});
0060 
0061     QSignalSpy addModemSpy(ModemManager::notifier(), SIGNAL(modemAdded(QString)));
0062     connect(ModemManager::notifier(), &ModemManager::Notifier::modemAdded, this, &ModemTest::testModemAdded);
0063     fakeModem->addModem(modem);
0064     QVERIFY(addModemSpy.wait());
0065     QCOMPARE(ModemManager::modemDevices().count(), 1);
0066     QCOMPARE(ModemManager::modemDevices().first()->uni(), addModemSpy.at(0).at(0).toString());
0067     const QString addedModemPath = ModemManager::modemDevices().first()->uni();
0068 
0069     Bearer *bearer = new Bearer();
0070     // We need to set some default values
0071     bearer->setConnected(false);
0072     bearer->setInterface(QLatin1String("ttyUSB0"));
0073     bearer->setIp4Config({{QLatin1String("method"), MM_BEARER_IP_METHOD_PPP}});
0074     bearer->setIp6Config({{QLatin1String("method"), MM_BEARER_IP_METHOD_UNKNOWN}});
0075     bearer->setIpTimeout(20);
0076     bearer->setProperties({{QLatin1String("apn"), QLatin1String("internet")}, {QLatin1String("ip-type"), 1}, {QLatin1String("number"), QLatin1String("*99#")}});
0077     bearer->setSuspended(false);
0078 
0079     ModemManager::Modem::Ptr modemInterface = ModemManager::modemDevices().first()->modemInterface();
0080     QCOMPARE(modemInterface->listBearers().count(), 0);
0081 
0082     QSignalSpy bearerAddedSpy(modemInterface.data(), SIGNAL(bearerAdded(QString)));
0083     fakeModem->addBearer(bearer);
0084 #if MM_CHECK_VERSION(1, 2, 0)
0085     QVERIFY(bearerAddedSpy.wait());
0086     QCOMPARE(modemInterface->listBearers().count(), 1);
0087     ModemManager::Bearer::Ptr modemBearer = modemInterface->listBearers().first();
0088     QVERIFY(modemBearer);
0089     QCOMPARE(modemBearer->isConnected(), false);
0090     QCOMPARE(modemBearer->interface(), QLatin1String("ttyUSB0"));
0091     QCOMPARE(modemBearer->ip4Config().method(), MM_BEARER_IP_METHOD_PPP);
0092     QCOMPARE(modemBearer->ip6Config().method(), MM_BEARER_IP_METHOD_UNKNOWN);
0093     QCOMPARE(modemBearer->ipTimeout(), (uint)20);
0094     QVERIFY(modemBearer->properties().contains(QLatin1String("apn")));
0095     QVERIFY(modemBearer->properties().contains(QLatin1String("number")));
0096     QVERIFY(modemBearer->properties().contains(QLatin1String("ip-type")));
0097     QCOMPARE(modemBearer->properties().value(QLatin1String("apn")).toString(), QLatin1String("internet"));
0098     QCOMPARE(modemBearer->properties().value(QLatin1String("ip-type")).toUInt(), (uint)1);
0099     QCOMPARE(modemBearer->properties().value(QLatin1String("number")).toString(), QLatin1String("*99#"));
0100     QCOMPARE(modemBearer->isSuspended(), false);
0101 
0102     QSignalSpy bearerRemovedSpy(modemInterface.data(), SIGNAL(bearerRemoved(QString)));
0103     fakeModem->removeBearer(bearer);
0104     QVERIFY(bearerRemovedSpy.wait());
0105     QCOMPARE(modemInterface->listBearers().count(), 0);
0106 #endif
0107 
0108     QSignalSpy removeModemSpy(ModemManager::notifier(), SIGNAL(modemRemoved(QString)));
0109     fakeModem->removeModem(modem);
0110     QVERIFY(removeModemSpy.wait());
0111     QVERIFY(ModemManager::modemDevices().isEmpty());
0112     QCOMPARE(removeModemSpy.at(0).at(0).toString(), addedModemPath);
0113     delete modem;
0114 }
0115 
0116 void ModemTest::testModemAdded(const QString &dev)
0117 {
0118     ModemManager::ModemDevice::Ptr modemDevice = ModemManager::findModemDevice(dev);
0119     ModemManager::Modem::Ptr modem = modemDevice->modemInterface();
0120 
0121     QCOMPARE(modem->accessTechnologies(), 16);
0122     QList<MMModemBand> bands = {MM_MODEM_BAND_UNKNOWN};
0123     QCOMPARE(modem->currentBands(), bands);
0124     QCOMPARE(modem->currentCapabilities(), 4);
0125     ModemManager::CurrentModesType currentModes = {MM_MODEM_MODE_ANY, MM_MODEM_MODE_NONE};
0126     ModemManager::CurrentModesType modemCurrentModes = modem->currentModes();
0127     QCOMPARE(currentModes.allowed, modemCurrentModes.allowed);
0128     QCOMPARE(currentModes.preferred, modemCurrentModes.preferred);
0129     QCOMPARE(modem->device(), QLatin1String("/sys/devices/pci0000:00/0000:00:1d.0/usb4/4-1/4-1.2"));
0130     QCOMPARE(modem->deviceIdentifier(), QLatin1String("1c435eb6d74494b5f78d7221e2c5ae9ec526a981"));
0131     QCOMPARE(modem->drivers(), QStringList(QLatin1String("option1")));
0132     QCOMPARE(modem->equipmentIdentifier(), QLatin1String("353475021085110"));
0133     QCOMPARE(modem->manufacturer(), QLatin1String("huawei"));
0134     QCOMPARE(modem->maxActiveBearers(), (uint)1);
0135     QCOMPARE(modem->maxBearers(), (uint)1);
0136     QCOMPARE(modem->model(), QLatin1String("K2540"));
0137     // TODO ownNumbers
0138     QCOMPARE(modem->plugin(), QLatin1String("Huawei"));
0139     ModemManager::PortList ports = {{QLatin1String("ttyUSB0"), MM_MODEM_PORT_TYPE_AT},
0140                                     {QLatin1String("ttyUSB1"), MM_MODEM_PORT_TYPE_QCDM},
0141                                     {QLatin1String("ttyUSB2"), MM_MODEM_PORT_TYPE_AT}};
0142     ModemManager::PortList modemPorts = modem->ports();
0143     QCOMPARE(ports.count(), modemPorts.count());
0144     Q_FOREACH (const ModemManager::Port port, ports) {
0145         bool found = false;
0146         Q_FOREACH (const ModemManager::Port modemPort, modemPorts) {
0147             if (port.name == modemPort.name) {
0148                 found = true;
0149                 QCOMPARE(port.type, modemPort.type);
0150             }
0151         }
0152         QVERIFY(found);
0153         found = false;
0154     }
0155     QCOMPARE(modem->powerState(), MM_MODEM_POWER_STATE_ON);
0156     QCOMPARE(modem->primaryPort(), QLatin1String("ttyUSB2"));
0157     QCOMPARE(modem->revision(), QLatin1String("11.001.05.00.11"));
0158     ModemManager::SignalQualityPair signalQuality = {93, true};
0159     ModemManager::SignalQualityPair modemSignalQuality = modem->signalQuality();
0160     QCOMPARE(modemSignalQuality.recent, signalQuality.recent);
0161     QCOMPARE(modemSignalQuality.signal, signalQuality.signal);
0162     QCOMPARE(modem->simPath(), QLatin1String("/org/kde/fakemodem/SIM/1"));
0163     QCOMPARE(modem->state(), MM_MODEM_STATE_REGISTERED);
0164     QCOMPARE(modem->stateFailedReason(), MM_MODEM_STATE_FAILED_REASON_NONE);
0165     QList<MMModemBand> supportedBands = {MM_MODEM_BAND_UNKNOWN};
0166     QCOMPARE(modem->supportedBands(), supportedBands);
0167     QList<MMModemCapability> supportedCapabilities = {(MMModemCapability)4};
0168     QCOMPARE(modem->supportedCapabilities(), supportedCapabilities);
0169     QCOMPARE(modem->supportedIpFamilies(), 3);
0170     ModemManager::SupportedModesType supportedModes;
0171     ModemManager::CurrentModesType supportedMode1 = {MM_MODEM_MODE_4G & MM_MODEM_MODE_3G & MM_MODEM_MODE_2G, MM_MODEM_MODE_NONE};
0172     ModemManager::CurrentModesType supportedMode2 = {MM_MODEM_MODE_4G & MM_MODEM_MODE_3G & MM_MODEM_MODE_2G, MM_MODEM_MODE_2G};
0173     ModemManager::CurrentModesType supportedMode3 = {MM_MODEM_MODE_4G & MM_MODEM_MODE_3G & MM_MODEM_MODE_2G, MM_MODEM_MODE_3G};
0174     supportedModes << supportedMode1 << supportedMode2 << supportedMode3;
0175     ModemManager::SupportedModesType modemSupportedModes = modem->supportedModes();
0176     Q_FOREACH (ModemManager::CurrentModesType currentType, supportedModes) {
0177         bool found = false;
0178         Q_FOREACH (ModemManager::CurrentModesType modemCurrentType, modemSupportedModes) {
0179             if (currentType.allowed == modemCurrentType.allowed && currentType.preferred == modemCurrentType.preferred) {
0180                 found = true;
0181             }
0182         }
0183         QVERIFY(found);
0184         found = false;
0185     }
0186     QCOMPARE(modem->unlockRequired(), MM_MODEM_LOCK_NONE);
0187     ModemManager::UnlockRetriesMap unlockRetries = {{MM_MODEM_LOCK_SIM_PIN, 3},
0188                                                     {MM_MODEM_LOCK_SIM_PIN2, 3},
0189                                                     {MM_MODEM_LOCK_SIM_PUK, 10},
0190                                                     {MM_MODEM_LOCK_SIM_PUK2, 10}};
0191     QCOMPARE(modem->unlockRetries(), unlockRetries);
0192 }
0193 
0194 QTEST_MAIN(ModemTest)
0195 
0196 #include "moc_modemtest.cpp"