File indexing completed on 2024-10-06 03:43:32

0001 /*
0002     SPDX-FileCopyrightText: 2012-2013 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 "ipv4settingtest.h"
0008 
0009 #include "generictypes.h"
0010 #include "settings/ipv4setting.h"
0011 
0012 #include <arpa/inet.h>
0013 
0014 #include <libnm/NetworkManager.h>
0015 
0016 #include <QTest>
0017 
0018 void IPv4SettingTest::testSetting_data()
0019 {
0020     QTest::addColumn<QString>("method");
0021     QTest::addColumn<UIntList>("dns");
0022     QTest::addColumn<QStringList>("dnsSearch");
0023     QTest::addColumn<UIntListList>("addresses");
0024     QTest::addColumn<UIntListList>("routes");
0025     QTest::addColumn<int>("routeMetric");
0026     QTest::addColumn<bool>("ignoreAutoRoutes");
0027     QTest::addColumn<bool>("ignoreAutoDns");
0028     QTest::addColumn<QString>("dhcpClientId");
0029     QTest::addColumn<bool>("dhcpSendHostname");
0030     QTest::addColumn<QString>("dhcpHostname");
0031     QTest::addColumn<bool>("neverDefault");
0032     QTest::addColumn<bool>("mayFail");
0033     QTest::addColumn<qint32>("dadTimeout");
0034     QTest::addColumn<QString>("dhcpFqdn");
0035     QTest::addColumn<QStringList>("dnsOptions");
0036     QTest::addColumn<qint32>("dnsPriority");
0037     QTest::addColumn<QString>("gateway");
0038     QTest::addColumn<NMVariantMapList>("addressData");
0039     QTest::addColumn<NMVariantMapList>("routeData");
0040 
0041     QList<uint> dns;
0042     dns << inet_addr("10.0.0.1");
0043     dns << inet_addr("10.0.1.1");
0044 
0045     QStringList dnsSearch;
0046     dnsSearch << "foo.com";
0047     dnsSearch << "foo.bar";
0048 
0049     QList<QList<uint>> addresses;
0050     QList<uint> addr;
0051     addr << inet_addr("192.168.1.1");
0052     addr << 25;
0053     addr << 0;
0054     addresses << addr;
0055 
0056     QList<QList<uint>> routes;
0057     QList<uint> routeAddr;
0058     routeAddr << inet_addr("192.168.1.1");
0059     routeAddr << 25;
0060     routeAddr << inet_addr("192.169.1.1");
0061     routeAddr << 25;
0062     routes << routeAddr;
0063 
0064     QStringList dnsOptions;
0065     dnsOptions << "opt1";
0066     dnsOptions << "opt2";
0067 
0068     NMVariantMapList addressData;
0069     QVariantMap addressMap;
0070     addressMap["address"] = "192.168.1.1";
0071     addressMap["prefix"] = 25;
0072     addressData.append(addressMap);
0073 
0074     NMVariantMapList routeData;
0075     QVariantMap routeMap;
0076     routeMap["dest"] = "192.168.1.1";
0077     routeMap["prefix"] = 25;
0078     routeData.append(routeMap);
0079     routeMap.clear();
0080     routeMap["dest"] = "192.168.1.2";
0081     routeMap["prefix"] = 25;
0082     routeData.append(routeMap);
0083 
0084     QTest::newRow("setting1") << QString("auto") // method
0085                               << dns // dns
0086                               << dnsSearch // dnsSearch
0087                               << addresses // addresses
0088                               << routes // routes
0089                               << 100 // routeMetric
0090                               << true // ignoreAutoRoutes
0091                               << true // ignoreAutoDns
0092                               << QString("home-test") // dhcpClientId
0093                               << false // dhcpSendHostname
0094                               << QString("home-test") // dhcpHostname
0095                               << true // neverDefault
0096                               << false // mayFail
0097                               << 100 // dadTimeout
0098                               << QString("foo.com") // dhcpFqdn
0099                               << dnsOptions // dnsOptions
0100                               << 100 // dnsPriority
0101                               << QString("1.1.1.1") // gateway
0102                               << addressData // addressData
0103                               << routeData; // routeData
0104 }
0105 
0106 void IPv4SettingTest::testSetting()
0107 {
0108     QFETCH(QString, method);
0109     QFETCH(UIntList, dns);
0110     QFETCH(QStringList, dnsSearch);
0111     QFETCH(UIntListList, addresses);
0112     QFETCH(UIntListList, routes);
0113     QFETCH(int, routeMetric);
0114     QFETCH(bool, ignoreAutoRoutes);
0115     QFETCH(bool, ignoreAutoDns);
0116     QFETCH(QString, dhcpClientId);
0117     QFETCH(bool, dhcpSendHostname);
0118     QFETCH(QString, dhcpHostname);
0119     QFETCH(bool, neverDefault);
0120     QFETCH(bool, mayFail);
0121     QFETCH(qint32, dadTimeout);
0122     QFETCH(QString, dhcpFqdn);
0123     QFETCH(QStringList, dnsOptions);
0124     QFETCH(qint32, dnsPriority);
0125     QFETCH(QString, gateway);
0126     QFETCH(NMVariantMapList, addressData);
0127     QFETCH(NMVariantMapList, routeData);
0128 
0129     QVariantMap map;
0130 
0131     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_METHOD), method);
0132     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS), QVariant::fromValue(dns));
0133     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_SEARCH), dnsSearch);
0134     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESSES), QVariant::fromValue(addresses));
0135     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTES), QVariant::fromValue(routes));
0136     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_METRIC), routeMetric);
0137     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES), ignoreAutoRoutes);
0138     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS), ignoreAutoDns);
0139     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_CLIENT_ID), dhcpClientId);
0140     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME), dhcpSendHostname);
0141     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_HOSTNAME), dhcpHostname);
0142     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_NEVER_DEFAULT), neverDefault);
0143     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_MAY_FAIL), mayFail);
0144     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DAD_TIMEOUT), dadTimeout);
0145     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_FQDN), dhcpFqdn);
0146     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_OPTIONS), dnsOptions);
0147     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_PRIORITY), dnsPriority);
0148     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_GATEWAY), gateway);
0149     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESS_DATA), QVariant::fromValue(addressData));
0150     map.insert(QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_DATA), QVariant::fromValue(routeData));
0151 
0152     NetworkManager::Ipv4Setting setting;
0153     setting.fromMap(map);
0154 
0155     QVariantMap map1 = setting.toMap();
0156 
0157     // Will fail if set some default values, because they are skipped in toMap() method
0158     QVariantMap::const_iterator it = map.constBegin();
0159     while (it != map.constEnd()) {
0160         /* clang-format off */
0161         if (it.key() != QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS)
0162             && it.key() != QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESSES)
0163             && it.key() != QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTES)
0164             && it.key() != QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESS_DATA)
0165             && it.key() != QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_DATA)) { /* clang-format on */
0166             QCOMPARE(it.value(), map1.value(it.key()));
0167         }
0168         ++it;
0169     }
0170 
0171     QCOMPARE(map.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS)).value<UIntList>(), //
0172              map1.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS)).value<UIntList>());
0173     QCOMPARE(map.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESSES)).value<UIntListList>(),
0174              map1.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESSES)).value<UIntListList>());
0175     QCOMPARE(map.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTES)).value<UIntListList>(),
0176              map1.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTES)).value<UIntListList>());
0177     QCOMPARE(qdbus_cast<NMVariantMapList>(map.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESS_DATA))),
0178              qdbus_cast<NMVariantMapList>(map1.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESS_DATA))));
0179     QCOMPARE(qdbus_cast<NMVariantMapList>(map.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_DATA))),
0180              qdbus_cast<NMVariantMapList>(map1.value(QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_DATA))));
0181 }
0182 
0183 QTEST_MAIN(IPv4SettingTest)
0184 
0185 #include "moc_ipv4settingtest.cpp"