File indexing completed on 2024-10-06 03:43:33
0001 /* 0002 SPDX-FileCopyrightText: 2019 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 "wireguardsettingtest.h" 0008 0009 #include "settings/wireguardsetting.h" 0010 0011 #include <libnm/NetworkManager.h> 0012 0013 #include <QTest> 0014 0015 #if !NM_CHECK_VERSION(1, 16, 0) 0016 #define NM_SETTING_WIREGUARD_FWMARK "fwmark" 0017 #define NM_SETTING_WIREGUARD_LISTEN_PORT "listen-port" 0018 #define NM_SETTING_WIREGUARD_PRIVATE_KEY "private-key" 0019 #define NM_SETTING_WIREGUARD_PRIVATE_KEY_FLAGS "private-key-flags" 0020 #define NM_SETTING_WIREGUARD_PEERS "peers" 0021 #define NM_SETTING_WIREGUARD_MTU "mtu" 0022 #define NM_SETTING_WIREGUARD_PEER_ROUTES "peer-routes" 0023 0024 #define NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY "preshared-key" 0025 #define NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY_FLAGS "preshared-key-flags" 0026 #define NM_WIREGUARD_PEER_ATTR_PUBLIC_KEY "public-key" 0027 #endif 0028 0029 void WireGuardSettingTest::testSetting_data() 0030 { 0031 QTest::addColumn<qint32>("fwmark"); 0032 QTest::addColumn<qint32>("listenPort"); 0033 QTest::addColumn<qint32>("mtu"); 0034 QTest::addColumn<bool>("peerRoutes"); 0035 QTest::addColumn<NMVariantMapList>("peers"); 0036 QTest::addColumn<QString>("privateKey"); 0037 QTest::addColumn<quint32>("privateKeyFlags"); 0038 0039 NMVariantMapList peers; 0040 QVariantMap map; 0041 map.insert(QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY), "preshared-key"); 0042 map.insert(QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY_FLAGS), (uint)2); 0043 map.insert(QLatin1String(NM_WIREGUARD_PEER_ATTR_PUBLIC_KEY), "public-key"); 0044 0045 peers << map; 0046 0047 QVariantMap map1; 0048 map1.insert(QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY), "preshared-key1"); 0049 map1.insert(QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY_FLAGS), (uint)3); 0050 map1.insert(QLatin1String(NM_WIREGUARD_PEER_ATTR_PUBLIC_KEY), "public-key1"); 0051 peers << map1; 0052 0053 QTest::newRow("setting1") << (qint32)100 // fwmark 0054 << (qint32)101 // listenPort 0055 << (qint32)102 // mtu 0056 << false // peerRoutes 0057 << peers // peers 0058 << QString("private-key") // privateKey 0059 << (quint32)2; // privateKeyFlags 0060 } 0061 0062 void WireGuardSettingTest::testSetting() 0063 { 0064 QFETCH(qint32, fwmark); 0065 QFETCH(qint32, listenPort); 0066 QFETCH(qint32, mtu); 0067 QFETCH(bool, peerRoutes); 0068 QFETCH(NMVariantMapList, peers); 0069 QFETCH(QString, privateKey); 0070 QFETCH(quint32, privateKeyFlags); 0071 0072 QVariantMap map; 0073 0074 map.insert(QLatin1String(NM_SETTING_WIREGUARD_FWMARK), fwmark); 0075 map.insert(QLatin1String(NM_SETTING_WIREGUARD_LISTEN_PORT), listenPort); 0076 map.insert(QLatin1String(NM_SETTING_WIREGUARD_MTU), mtu); 0077 map.insert(QLatin1String(NM_SETTING_WIREGUARD_PEER_ROUTES), peerRoutes); 0078 map.insert(QLatin1String(NM_SETTING_WIREGUARD_PEERS), QVariant::fromValue(peers)); 0079 map.insert(QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY), privateKey); 0080 map.insert(QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY_FLAGS), privateKeyFlags); 0081 0082 NetworkManager::WireGuardSetting setting; 0083 setting.fromMap(map); 0084 0085 QVariantMap map1 = setting.toMap(); 0086 0087 // Will fail if set some default values, because they are skipped in toMap() method 0088 QVariantMap::const_iterator it = map.constBegin(); 0089 while (it != map.constEnd()) { 0090 if (it.key() != QLatin1String(NM_SETTING_WIREGUARD_PEERS)) { 0091 QCOMPARE(it.value(), map1.value(it.key())); 0092 } 0093 ++it; 0094 } 0095 0096 NMVariantMapList list = map.value(QLatin1String(NM_SETTING_WIREGUARD_PEERS)).value<NMVariantMapList>(); 0097 NMVariantMapList list1 = map1.value(QLatin1String(NM_SETTING_WIREGUARD_PEERS)).value<NMVariantMapList>(); 0098 0099 QCOMPARE(list.count(), list1.count()); 0100 0101 int comparedMaps = 0; 0102 for (int i = 0; i < list.size(); ++i) { 0103 QVariantMap varMap = list.at(i); 0104 for (int j = 0; j < list1.size(); ++j) { 0105 QVariantMap varMap1 = list1.at(j); 0106 QVariantMap::const_iterator ite = varMap.constBegin(); 0107 int comparedvals = 0; 0108 while (ite != varMap.constEnd()) { 0109 QVariantMap::const_iterator val1 = varMap1.constFind(ite.key()); 0110 if (val1 != varMap1.constEnd()) { 0111 if (varMap.value(ite.key()) == val1.value()) { 0112 ++comparedvals; 0113 } 0114 } 0115 ++ite; 0116 } 0117 if (comparedvals == varMap.size()) { 0118 comparedMaps++; 0119 } 0120 } 0121 } 0122 QCOMPARE(comparedMaps, list.count()); 0123 } 0124 0125 QTEST_MAIN(WireGuardSettingTest) 0126 0127 #include "moc_wireguardsettingtest.cpp"