File indexing completed on 2024-05-12 05:22:34
0001 /* 0002 autotests/abstractkeylistmodeltest.cpp 0003 0004 This file is part of libkleopatra's test suite. 0005 SPDX-FileCopyrightText: 2021 g10 Code GmbH 0006 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "abstractkeylistmodeltest.h" 0012 0013 #include <Libkleo/KeyGroup> 0014 #include <Libkleo/KeyListModel> 0015 0016 #include <QSet> 0017 #include <QTest> 0018 0019 #include <gpgme++/key.h> 0020 0021 #include <gpgme.h> 0022 0023 using namespace Kleo; 0024 using namespace GpgME; 0025 0026 Q_DECLARE_METATYPE(KeyGroup) 0027 0028 namespace 0029 { 0030 Key createTestKey(const char *uid) 0031 { 0032 static int count = 0; 0033 count++; 0034 0035 gpgme_key_t key; 0036 gpgme_key_from_uid(&key, uid); 0037 const QByteArray fingerprint = QByteArray::number(count, 16).rightJustified(40, '0'); 0038 key->fpr = strdup(fingerprint.constData()); 0039 0040 return Key(key, false); 0041 } 0042 0043 KeyGroup createGroup(const QString &name, 0044 const std::vector<Key> &keys = std::vector<Key>(), 0045 KeyGroup::Source source = KeyGroup::ApplicationConfig, 0046 const QString &configName = QString()) 0047 { 0048 const KeyGroup::Id groupId = ((source == KeyGroup::ApplicationConfig) // 0049 ? (configName.isEmpty() ? name : configName) 0050 : name); 0051 KeyGroup g(groupId, name, keys, source); 0052 return g; 0053 } 0054 } 0055 0056 void AbstractKeyListModelTest::testCreation() 0057 { 0058 QScopedPointer<AbstractKeyListModel> model(createModel()); 0059 QCOMPARE(model->rowCount(), 0); 0060 } 0061 0062 void AbstractKeyListModelTest::testSetKeys() 0063 { 0064 QScopedPointer<AbstractKeyListModel> model(createModel()); 0065 0066 const std::vector<Key> keys = { 0067 createTestKey("test1@example.net"), 0068 }; 0069 model->setKeys(keys); 0070 QCOMPARE(model->rowCount(), 1); 0071 QVERIFY(model->index(keys[0]).isValid()); 0072 0073 const std::vector<Key> otherKeys = { 0074 createTestKey("test2@example.net"), 0075 createTestKey("test3@example.net"), 0076 }; 0077 model->setKeys(otherKeys); 0078 QCOMPARE(model->rowCount(), 2); 0079 QVERIFY(model->index(otherKeys[0]).isValid()); 0080 QVERIFY(model->index(otherKeys[1]).isValid()); 0081 QVERIFY(!model->index(keys[0]).isValid()); 0082 } 0083 0084 void AbstractKeyListModelTest::testSetGroups() 0085 { 0086 QScopedPointer<AbstractKeyListModel> model(createModel()); 0087 0088 const std::vector<KeyGroup> groups = { 0089 createGroup("test1"), 0090 }; 0091 model->setGroups(groups); 0092 QCOMPARE(model->rowCount(), 1); 0093 QVERIFY(model->index(groups[0]).isValid()); 0094 0095 const std::vector<KeyGroup> otherGroups = { 0096 createGroup("test2"), 0097 createGroup("test3"), 0098 }; 0099 model->setGroups(otherGroups); 0100 QCOMPARE(model->rowCount(), 2); 0101 QVERIFY(model->index(otherGroups[0]).isValid()); 0102 QVERIFY(model->index(otherGroups[1]).isValid()); 0103 QVERIFY(!model->index(groups[0]).isValid()); 0104 } 0105 0106 void AbstractKeyListModelTest::testKeys() 0107 { 0108 QScopedPointer<AbstractKeyListModel> model(createModel()); 0109 0110 const Key key = createTestKey("test@example.net"); 0111 const KeyGroup group = createGroup(QStringLiteral("test"), {key}); 0112 0113 model->setKeys({key}); 0114 model->setGroups({group}); 0115 0116 QCOMPARE(model->rowCount(), 2); 0117 0118 const QModelIndex keyIndex = model->index(key); 0119 QVERIFY(keyIndex.isValid()); 0120 const QModelIndex groupIndex = model->index(group); 0121 QVERIFY(groupIndex.isValid()); 0122 0123 { 0124 const auto keys = model->keys({}); 0125 QCOMPARE(keys.size(), 0); 0126 } 0127 0128 { 0129 const auto keys = model->keys({keyIndex}); 0130 QCOMPARE(keys.size(), 1); 0131 QCOMPARE(keys[0].userID(0).addrSpec(), UserID::addrSpecFromString("test@example.net")); 0132 } 0133 0134 { 0135 // duplicate keys are removed from result 0136 const auto keys = model->keys({keyIndex, keyIndex}); 0137 QCOMPARE(keys.size(), 1); 0138 QCOMPARE(keys[0].userID(0).addrSpec(), UserID::addrSpecFromString("test@example.net")); 0139 } 0140 0141 { 0142 // null keys are removed from result 0143 const auto keys = model->keys({groupIndex}); 0144 QCOMPARE(keys.size(), 0); 0145 } 0146 } 0147 0148 void AbstractKeyListModelTest::testIndex() 0149 { 0150 QScopedPointer<AbstractKeyListModel> model(createModel()); 0151 0152 const Key key = createTestKey("test@example.net"); 0153 const std::vector<KeyGroup> groups = { 0154 createGroup("test", {key}, KeyGroup::UnknownSource), 0155 createGroup("test", {key}, KeyGroup::GnuPGConfig), 0156 createGroup("test", {key}, KeyGroup::ApplicationConfig, "test"), 0157 createGroup("test", {key}, KeyGroup::ApplicationConfig, "otherConfigName"), 0158 }; 0159 0160 model->setKeys({key}); 0161 model->setGroups(groups); 0162 0163 const QModelIndex keyIndex = model->index(0, 0); 0164 QVERIFY(keyIndex.isValid()); 0165 QVERIFY(!model->key(keyIndex).isNull()); 0166 0167 const QModelIndex groupIndex = model->index(1, 0); 0168 QVERIFY(groupIndex.isValid()); 0169 QVERIFY(!model->group(groupIndex).isNull()); 0170 } 0171 0172 void AbstractKeyListModelTest::testIndexForGroup() 0173 { 0174 QScopedPointer<AbstractKeyListModel> model(createModel()); 0175 0176 const Key key = createTestKey("test@example.net"); 0177 const std::vector<KeyGroup> groups = { 0178 createGroup("test", {key}, KeyGroup::UnknownSource), 0179 createGroup("test", {key}, KeyGroup::GnuPGConfig), 0180 createGroup("test", {key}, KeyGroup::ApplicationConfig, "test"), 0181 createGroup("test", {key}, KeyGroup::ApplicationConfig, "otherConfigName"), 0182 }; 0183 0184 model->setKeys({key}); 0185 model->setGroups(groups); 0186 0187 QSet<int> rows; 0188 for (const KeyGroup &group : groups) { 0189 const QModelIndex groupIndex = model->index(group); 0190 QVERIFY(groupIndex.isValid()); 0191 rows.insert(groupIndex.row()); 0192 } 0193 QCOMPARE(rows.size(), 4); 0194 } 0195 0196 void AbstractKeyListModelTest::testAddGroup() 0197 { 0198 QScopedPointer<AbstractKeyListModel> model(createModel()); 0199 0200 { 0201 const QModelIndex resultIndex = model->addGroup(KeyGroup()); 0202 QVERIFY(!resultIndex.isValid()); 0203 QCOMPARE(model->rowCount(), 0); 0204 } 0205 0206 { 0207 const KeyGroup group = createGroup(QStringLiteral("test")); 0208 const QModelIndex resultIndex = model->addGroup(group); 0209 QVERIFY(resultIndex.isValid()); 0210 QCOMPARE(resultIndex.row(), 0); 0211 QCOMPARE(resultIndex.column(), 0); 0212 QVERIFY(!resultIndex.parent().isValid()); 0213 QCOMPARE(model->rowCount(), 1); 0214 const KeyGroup groupInModel = model->group(model->index(0, 0)); 0215 QVERIFY(!groupInModel.isNull()); 0216 QCOMPARE(groupInModel.id(), group.id()); 0217 QCOMPARE(groupInModel.source(), group.source()); 0218 QCOMPARE(groupInModel.name(), group.name()); 0219 QCOMPARE(groupInModel.keys().size(), group.keys().size()); 0220 } 0221 } 0222 0223 void AbstractKeyListModelTest::testSetData() 0224 { 0225 QScopedPointer<AbstractKeyListModel> model(createModel()); 0226 0227 const Key key = createTestKey("test@example.net"); 0228 const KeyGroup group = createGroup(QStringLiteral("test")); 0229 model->setKeys({key}); 0230 model->setGroups({group}); 0231 const KeyGroup updatedGroup = createGroup(QStringLiteral("updated"), {key}); 0232 QVERIFY(!model->setData(QModelIndex(), QVariant::fromValue(updatedGroup))); 0233 QVERIFY(!model->setData(model->index(key), QVariant::fromValue(updatedGroup))); 0234 0235 const QModelIndex groupIndex = model->index(group); 0236 QVERIFY(model->setData(groupIndex, QVariant::fromValue(updatedGroup))); 0237 const KeyGroup groupInModel = model->group(groupIndex); 0238 QVERIFY(!groupInModel.isNull()); 0239 QCOMPARE(groupInModel.name(), updatedGroup.name()); 0240 QCOMPARE(groupInModel.keys().size(), updatedGroup.keys().size()); 0241 } 0242 0243 void AbstractKeyListModelTest::testRemoveGroup() 0244 { 0245 QScopedPointer<AbstractKeyListModel> model(createModel()); 0246 0247 const KeyGroup group = createGroup(QStringLiteral("test")); 0248 model->setGroups({group}); 0249 0250 { 0251 const bool result = model->removeGroup(KeyGroup()); 0252 QVERIFY(!result); 0253 QCOMPARE(model->rowCount(), 1); 0254 } 0255 0256 { 0257 const KeyGroup otherGroup = createGroup(QStringLiteral("test2")); 0258 0259 const bool result = model->removeGroup(otherGroup); 0260 QVERIFY(!result); 0261 QCOMPARE(model->rowCount(), 1); 0262 } 0263 0264 { 0265 const bool result = model->removeGroup(group); 0266 QVERIFY(result); 0267 QCOMPARE(model->rowCount(), 0); 0268 } 0269 } 0270 0271 void AbstractKeyListModelTest::testClear() 0272 { 0273 QScopedPointer<AbstractKeyListModel> model(createModel()); 0274 0275 model->setGroups({ 0276 createGroup("test"), 0277 }); 0278 0279 model->clear(AbstractKeyListModel::Keys); 0280 QCOMPARE(model->rowCount(), 1); 0281 0282 model->clear(AbstractKeyListModel::Groups); 0283 QCOMPARE(model->rowCount(), 0); 0284 } 0285 0286 #include "moc_abstractkeylistmodeltest.cpp"