File indexing completed on 2024-07-21 03:45:08

0001 /*
0002     SPDX-FileCopyrightText: 2018 Pranav Gade <pranavgade20@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "tcsettingtest.h"
0008 
0009 #include "settings/tcsetting.h"
0010 
0011 #include <libnm/NetworkManager.h>
0012 
0013 #include <QTest>
0014 
0015 #if !NM_CHECK_VERSION(1, 10, 0)
0016 #define NM_SETTING_TC_CONFIG_QDISCS "qdiscs"
0017 #define NM_SETTING_TC_CONFIG_TFILTERS "tfilters"
0018 #endif
0019 
0020 void TcSettingTest::testSetting_data()
0021 {
0022     QTest::addColumn<NMVariantMapList>("qdiscs");
0023     QTest::addColumn<NMVariantMapList>("tfilters");
0024 
0025     NMVariantMapList qdiscs;
0026     QVariantMap qdisc;
0027     qdisc["one"] = "1";
0028     qdisc["two"] = 2;
0029     qdiscs.append(qdisc);
0030 
0031     NMVariantMapList tfilters;
0032     QVariantMap tfilter;
0033     tfilter["three"] = "3";
0034     tfilter["four"] = 4;
0035     tfilters.append(tfilter);
0036 
0037     QTest::newRow("setting1") << qdiscs // qdiscs
0038                               << tfilters; // tfilters
0039 }
0040 
0041 void TcSettingTest::testSetting()
0042 {
0043     QFETCH(NMVariantMapList, tfilters);
0044     QFETCH(NMVariantMapList, qdiscs);
0045 
0046     QVariantMap map;
0047 
0048     map.insert(QLatin1String(NM_SETTING_TC_CONFIG_TFILTERS), QVariant::fromValue(tfilters));
0049     map.insert(QLatin1String(NM_SETTING_TC_CONFIG_QDISCS), QVariant::fromValue(qdiscs));
0050 
0051     NetworkManager::TcSetting setting;
0052     setting.fromMap(map);
0053 
0054     QVariantMap map1 = setting.toMap();
0055 
0056     QVariantMap::const_iterator it = map.constBegin();
0057     while (it != map.constEnd()) {
0058         NMVariantMapList list = it.value().value<NMVariantMapList>();
0059         NMVariantMapList list1 = map1.value(it.key()).value<NMVariantMapList>();
0060 
0061         QCOMPARE(list.count(), list1.count());
0062 
0063         int comparedMaps = 0;
0064         for (int i = 0; i < list.size(); ++i) {
0065             QVariantMap varMap = list.at(i);
0066             for (int j = 0; j < list1.size(); ++j) {
0067                 QVariantMap varMap1 = list1.at(j);
0068                 QVariantMap::const_iterator ite = varMap.constBegin();
0069                 int comparedvals = 0;
0070                 while (ite != varMap.constEnd()) {
0071                     QVariantMap::const_iterator val1 = varMap1.constFind(ite.key());
0072                     if (val1 != varMap1.constEnd()) {
0073                         if (varMap.value(ite.key()) == val1.value()) {
0074                             ++comparedvals;
0075                         }
0076                     }
0077                     ++ite;
0078                 }
0079                 if (comparedvals == varMap.size()) {
0080                     comparedMaps++;
0081                 }
0082             }
0083         }
0084         ++it;
0085         QCOMPARE(comparedMaps, list.count());
0086     }
0087 }
0088 
0089 QTEST_MAIN(TcSettingTest)
0090 
0091 #include "moc_tcsettingtest.cpp"