File indexing completed on 2024-11-10 03:42:53

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 "ipv6settingtest.h"
0008 
0009 #include "generictypes.h"
0010 #include "settings/ipv6setting.h"
0011 #include "utils.h"
0012 
0013 #include <arpa/inet.h>
0014 
0015 #include <libnm/NetworkManager.h>
0016 
0017 #include <QHostAddress>
0018 #include <QTest>
0019 
0020 using namespace NetworkManager;
0021 
0022 // TODO: Test DNS,IPv6Addresses and IPv6Routes
0023 void IPv6SettingTest::testSetting_data()
0024 {
0025     QTest::addColumn<QString>("method");
0026     QTest::addColumn<IpV6DBusNameservers>("dns");
0027     QTest::addColumn<QStringList>("dnsSearch");
0028     QTest::addColumn<IpV6DBusAddressList>("addresses");
0029     QTest::addColumn<IpV6DBusRouteList>("routes");
0030     QTest::addColumn<int>("routeMetric");
0031     QTest::addColumn<bool>("ignoreAutoRoutes");
0032     QTest::addColumn<bool>("ignoreAutoDns");
0033     QTest::addColumn<bool>("neverDefault");
0034     QTest::addColumn<bool>("mayFail");
0035     QTest::addColumn<quint32>("privacy");
0036     QTest::addColumn<quint32>("dadTimeout");
0037     QTest::addColumn<quint32>("addressGenMode");
0038     QTest::addColumn<quint32>("dhcpTimeout");
0039     QTest::addColumn<QString>("dhcpHostname");
0040     QTest::addColumn<QString>("dhcpDuid");
0041     QTest::addColumn<QString>("token");
0042     QTest::addColumn<QStringList>("dnsOptions");
0043     QTest::addColumn<NMVariantMapList>("addressData");
0044     QTest::addColumn<NMVariantMapList>("routeData");
0045     QTest::addColumn<quint32>("routeTable");
0046 
0047     QStringList dnsSearch;
0048     dnsSearch << "foo.com";
0049     dnsSearch << "foo.bar";
0050 
0051     IpV6DBusNameservers dns;
0052     QByteArray dnsAddr1 = ipv6AddressFromHostAddress(QHostAddress("2607:f0d0:1002:0051:0000:0000:0000:0004"));
0053     dns << dnsAddr1;
0054 
0055     IpV6DBusAddressList addresses;
0056     IpV6DBusAddress address;
0057     address.address = ipv6AddressFromHostAddress(QHostAddress("2001:0db8:0000:0000:0000::1428:57ab"));
0058     address.prefix = 64;
0059     address.gateway = ipv6AddressFromHostAddress(QHostAddress("2001:0db8:0:f101::1"));
0060     addresses << address;
0061 
0062     IpV6DBusRouteList routes;
0063     IpV6DBusRoute route;
0064     route.destination = ipv6AddressFromHostAddress(QHostAddress("2001:0db8:0000:0000:0000::1428:57ab"));
0065     route.prefix = 48;
0066     route.nexthop = ipv6AddressFromHostAddress(QHostAddress("2001:638:500:101:2e0:81ff:fe24:37c6"));
0067     route.metric = 1024;
0068     routes << route;
0069 
0070     NMVariantMapList addressData;
0071     QVariantMap addressMap;
0072     addressMap["address"] = "192.168.1.1";
0073     addressMap["prefix"] = 25;
0074     addressData.append(addressMap);
0075 
0076     NMVariantMapList routeData;
0077     QVariantMap routeMap;
0078     routeMap["dest"] = "192.168.1.1";
0079     routeMap["prefix"] = 25;
0080     routeData.append(routeMap);
0081     routeMap.clear();
0082     routeMap["dest"] = "192.168.1.2";
0083     routeMap["prefix"] = 25;
0084     routeData.append(routeMap);
0085 
0086     QTest::newRow("setting1") << QString("auto") // method
0087                               << dns // dns
0088                               << dnsSearch // dnsSearch
0089                               << addresses // addresses
0090                               << routes // routes
0091                               << 100 // routeMetric
0092                               << true // ignoreAutoRoutes
0093                               << true // ignoreAutoDns
0094                               << true // neverDefault
0095                               << false // mayFail
0096                               << (quint32)0 // privacy
0097                               << (quint32)1101 // dadTimeout
0098                               << (quint32)0 // addressGenMode
0099                               << (quint32)110 // dhcpTimeout
0100                               << QString("dhcp-hostname") // dhcpHostname
0101                               << QString("dhcp-duid") // dhcpDuid
0102                               << QString("token") // token
0103                               << QStringList{QString("option1"), QString("option2")} // dnsOptions
0104                               << addressData // addressData
0105                               << routeData // routeData
0106                               << (quint32)1; // routeTable
0107 }
0108 
0109 void IPv6SettingTest::testSetting()
0110 {
0111     QFETCH(QString, method);
0112     QFETCH(IpV6DBusNameservers, dns);
0113     QFETCH(QStringList, dnsSearch);
0114     QFETCH(IpV6DBusAddressList, addresses);
0115     QFETCH(IpV6DBusRouteList, routes);
0116     QFETCH(int, routeMetric);
0117     QFETCH(bool, ignoreAutoRoutes);
0118     QFETCH(bool, ignoreAutoDns);
0119     QFETCH(bool, neverDefault);
0120     QFETCH(bool, mayFail);
0121     QFETCH(quint32, privacy);
0122     QFETCH(quint32, dadTimeout);
0123     QFETCH(quint32, addressGenMode);
0124     QFETCH(quint32, dhcpTimeout);
0125     QFETCH(QString, dhcpHostname);
0126     QFETCH(QString, dhcpDuid);
0127     QFETCH(QString, token);
0128     QFETCH(QStringList, dnsOptions);
0129     QFETCH(NMVariantMapList, addressData);
0130     QFETCH(NMVariantMapList, routeData);
0131     QFETCH(quint32, routeTable);
0132 
0133     QVariantMap map;
0134 
0135     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_METHOD), method);
0136     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_DNS), QVariant::fromValue(dns));
0137     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_DNS_SEARCH), dnsSearch);
0138     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESSES), QVariant::fromValue(addresses));
0139     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_ROUTES), QVariant::fromValue(routes));
0140     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_ROUTE_METRIC), routeMetric);
0141     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES), ignoreAutoRoutes);
0142     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS), ignoreAutoDns);
0143     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_NEVER_DEFAULT), neverDefault);
0144     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_MAY_FAIL), mayFail);
0145     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_IP6_PRIVACY), privacy);
0146     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_DAD_TIMEOUT), dadTimeout);
0147     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESS_GEN_MODE), addressGenMode);
0148     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_DHCP_TIMEOUT), dhcpTimeout);
0149     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_DHCP_HOSTNAME), dhcpHostname);
0150     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_DHCP_DUID), dhcpDuid);
0151     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_TOKEN), token);
0152     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_DNS_OPTIONS), dnsOptions);
0153     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESS_DATA), QVariant::fromValue(addressData));
0154     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_ROUTE_DATA), QVariant::fromValue(routeData));
0155     map.insert(QLatin1String(NMQT_SETTING_IP6_CONFIG_ROUTE_TABLE), routeTable);
0156 
0157     NetworkManager::Ipv6Setting setting;
0158     setting.fromMap(map);
0159 
0160     QVariantMap map1 = setting.toMap();
0161 
0162     // Will fail if set some default values, because they are skipped in toMap() method
0163     QVariantMap::const_iterator it = map.constBegin();
0164     while (it != map.constEnd()) {
0165         /* clang-format off */
0166         if (it.key() != QLatin1String(NMQT_SETTING_IP6_CONFIG_DNS)
0167             && it.key() != QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESSES)
0168             && it.key() != QLatin1String(NMQT_SETTING_IP6_CONFIG_ROUTES)
0169             && it.key() != QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESS_DATA)
0170             && it.key() != QLatin1String(NMQT_SETTING_IP6_CONFIG_ROUTE_DATA)) { /* clang-format on */
0171             QCOMPARE(it.value(), map1.value(it.key()));
0172         }
0173         ++it;
0174     }
0175 
0176     IpV6DBusNameservers nameServers1 = map.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_DNS)).value<IpV6DBusNameservers>();
0177     IpV6DBusNameservers nameServers2 = map1.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_DNS)).value<IpV6DBusNameservers>();
0178 
0179     for (int i = 0; i < nameServers1.size(); i++) {
0180         QHostAddress dnsAddr1 = ipv6AddressAsHostAddress(nameServers1.at(i));
0181         QHostAddress dnsAddr2 = ipv6AddressAsHostAddress(nameServers2.at(i));
0182         QCOMPARE(dnsAddr1.protocol(), QAbstractSocket::IPv6Protocol);
0183         QCOMPARE(dnsAddr2.protocol(), QAbstractSocket::IPv6Protocol);
0184         QCOMPARE(dnsAddr1, dnsAddr2);
0185     }
0186 
0187     IpV6DBusAddressList addresses1 = map.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESSES)).value<IpV6DBusAddressList>();
0188     IpV6DBusAddressList addresses2 = map1.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESSES)).value<IpV6DBusAddressList>();
0189 
0190     for (int i = 0; i < addresses1.size(); i++) {
0191         QHostAddress addr1(QString(addresses1.at(i).address));
0192         QHostAddress addr2(QString(addresses2.at(i).address));
0193         QHostAddress gateway1(QString(addresses1.at(i).gateway));
0194         QHostAddress gateway2(QString(addresses2.at(i).gateway));
0195 
0196         QCOMPARE(addr1, addr2);
0197         QCOMPARE(addresses1.at(i).prefix, addresses2.at(i).prefix);
0198         QCOMPARE(gateway1, gateway2);
0199     }
0200 
0201     IpV6DBusRouteList routes1 = map.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES)).value<IpV6DBusRouteList>();
0202     IpV6DBusRouteList routes2 = map1.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES)).value<IpV6DBusRouteList>();
0203 
0204     for (int i = 0; i < routes1.size(); i++) {
0205         QHostAddress destination1(QString(routes1.at(i).destination));
0206         QHostAddress destination2(QString(routes2.at(i).destination));
0207         QHostAddress nexthop1(QString(routes1.at(i).nexthop));
0208         QHostAddress nexthop2(QString(routes2.at(i).nexthop));
0209 
0210         QCOMPARE(destination1, destination2);
0211         QCOMPARE(routes1.at(i).prefix, routes2.at(i).prefix);
0212         QCOMPARE(nexthop1, nexthop2);
0213         QCOMPARE(routes1.at(i).metric, routes2.at(i).metric);
0214     }
0215 
0216     QCOMPARE(qdbus_cast<NMVariantMapList>(map.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESS_DATA))),
0217              qdbus_cast<NMVariantMapList>(map1.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_ADDRESS_DATA))));
0218     QCOMPARE(qdbus_cast<NMVariantMapList>(map.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_ROUTE_DATA))),
0219              qdbus_cast<NMVariantMapList>(map1.value(QLatin1String(NMQT_SETTING_IP6_CONFIG_ROUTE_DATA))));
0220 }
0221 
0222 QTEST_MAIN(IPv6SettingTest)
0223 
0224 #include "moc_ipv6settingtest.cpp"