abstractkeylistmodeltest.cpp 8.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
    autotests/abstractkeylistmodeltest.cpp

    This file is part of libkleopatra's test suite.
    SPDX-FileCopyrightText: 2021 g10 Code GmbH
    SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "abstractkeylistmodeltest.h"

#include "kleo/keygroup.h"
#include "models/keylistmodel.h"

#include <gpgme++/key.h>

18
19
#include <gpgme.h>

20
#include <QSet>
21
22
23
24
25
#include <QTest>

using namespace Kleo;
using namespace GpgME;

26
27
Q_DECLARE_METATYPE(KeyGroup)

28
29
30
31
32
33
34
35
36
37
38
39
40
41
namespace
{
Key createTestKey(const char *uid)
{
    static int count = 0;
    count++;

    gpgme_key_t key;
    gpgme_key_from_uid(&key, uid);
    const QByteArray fingerprint = QByteArray::number(count, 16).rightJustified(40, '0');
    key->fpr = strdup(fingerprint.constData());

    return Key(key, false);
}
42
43
44

KeyGroup createGroup(const QString &name,
                     const std::vector<Key> &keys = std::vector<Key>(),
45
                     KeyGroup::Source source = KeyGroup::ApplicationConfig,
46
47
                     const QString &configName = QString())
{
48
49
50
51
    const KeyGroup::Id groupId = (source == KeyGroup::ApplicationConfig) ?
                                 (configName.isEmpty() ? name : configName) :
                                 name;
    KeyGroup g(groupId, name, keys, source);
52
53
    return g;
}
54
55
}

56
57
58
59
60
61
void AbstractKeyListModelTest::testCreation()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());
    QCOMPARE( model->rowCount(), 0 );
}

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
void AbstractKeyListModelTest::testSetKeys()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

    const std::vector<Key> keys = {
        createTestKey("test1@example.net")
    };
    model->setKeys(keys);
    QCOMPARE( model->rowCount(), 1 );
    QVERIFY( model->index(keys[0]).isValid() );

    const std::vector<Key> otherKeys = {
        createTestKey("test2@example.net"),
        createTestKey("test3@example.net")
    };
    model->setKeys(otherKeys);
    QCOMPARE( model->rowCount(), 2 );
    QVERIFY( model->index(otherKeys[0]).isValid() );
    QVERIFY( model->index(otherKeys[1]).isValid() );
    QVERIFY( !model->index(keys[0]).isValid() );
}

84
85
86
87
88
void AbstractKeyListModelTest::testSetGroups()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

    const std::vector<KeyGroup> groups = {
89
        createGroup("test1")
90
91
92
93
94
95
    };
    model->setGroups(groups);
    QCOMPARE( model->rowCount(), 1 );
    QVERIFY( model->index(groups[0]).isValid() );

    const std::vector<KeyGroup> otherGroups = {
96
97
        createGroup("test2"),
        createGroup("test3")
98
99
100
101
102
103
104
105
    };
    model->setGroups(otherGroups);
    QCOMPARE( model->rowCount(), 2 );
    QVERIFY( model->index(otherGroups[0]).isValid() );
    QVERIFY( model->index(otherGroups[1]).isValid() );
    QVERIFY( !model->index(groups[0]).isValid() );
}

106
107
108
109
110
void AbstractKeyListModelTest::testKeys()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

    const Key key = createTestKey("test@example.net");
111
    const KeyGroup group = createGroup("test", {key});
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

    model->setKeys({key});
    model->setGroups({group});

    QCOMPARE( model->rowCount(), 2 );

    const QModelIndex keyIndex = model->index(key);
    QVERIFY( keyIndex.isValid() );
    const QModelIndex groupIndex = model->index(group);
    QVERIFY( groupIndex.isValid() );

    {
        const auto keys = model->keys({});
        QCOMPARE( keys.size(), 0 );
    }

    {
        const auto keys = model->keys({keyIndex});
        QCOMPARE( keys.size(), 1 );
        QCOMPARE( keys[0].userID(0).addrSpec(), UserID::addrSpecFromString("test@example.net") );
    }

    {
        // duplicate keys are removed from result
        const auto keys = model->keys({keyIndex, keyIndex});
        QCOMPARE( keys.size(), 1 );
        QCOMPARE( keys[0].userID(0).addrSpec(), UserID::addrSpecFromString("test@example.net") );
    }

    {
        // null keys are removed from result
        const auto keys = model->keys({groupIndex});
        QCOMPARE( keys.size(), 0 );
    }
}

148
149
150
151
152
void AbstractKeyListModelTest::testIndex()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

    const Key key = createTestKey("test@example.net");
153
    const std::vector<KeyGroup> groups = {
154
155
156
157
        createGroup("test", {key}, KeyGroup::UnknownSource),
        createGroup("test", {key}, KeyGroup::GnuPGConfig),
        createGroup("test", {key}, KeyGroup::ApplicationConfig, "test"),
        createGroup("test", {key}, KeyGroup::ApplicationConfig, "otherConfigName")
158
    };
159
160

    model->setKeys({key});
161
    model->setGroups(groups);
162
163
164
165
166
167
168
169
170
171

    const QModelIndex keyIndex = model->index(0, 0);
    QVERIFY( keyIndex.isValid() );
    QVERIFY( !model->key(keyIndex).isNull() );

    const QModelIndex groupIndex = model->index(1, 0);
    QVERIFY( groupIndex.isValid() );
    QVERIFY( !model->group(groupIndex).isNull() );
}

172
173
174
175
176
177
void AbstractKeyListModelTest::testIndexForGroup()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

    const Key key = createTestKey("test@example.net");
    const std::vector<KeyGroup> groups = {
178
179
180
181
        createGroup("test", {key}, KeyGroup::UnknownSource),
        createGroup("test", {key}, KeyGroup::GnuPGConfig),
        createGroup("test", {key}, KeyGroup::ApplicationConfig, "test"),
        createGroup("test", {key}, KeyGroup::ApplicationConfig, "otherConfigName")
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    };

    model->setKeys({key});
    model->setGroups(groups);

    QSet<int> rows;
    for (const KeyGroup &group : groups) {
        const QModelIndex groupIndex = model->index(group);
        QVERIFY( groupIndex.isValid() );
        rows.insert(groupIndex.row());
    }
    QCOMPARE(rows.size(), 4);
}

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
void AbstractKeyListModelTest::testAddGroup()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

    {
        const QModelIndex resultIndex = model->addGroup(KeyGroup());
        QVERIFY( !resultIndex.isValid() );
        QCOMPARE( model->rowCount(), 0 );
    }

    {
        const KeyGroup group = createGroup("test");
        const QModelIndex resultIndex = model->addGroup(group);
        QVERIFY( resultIndex.isValid() );
        QCOMPARE( resultIndex.row(), 0 );
        QCOMPARE( resultIndex.column(), 0 );
        QVERIFY( !resultIndex.parent().isValid() );
        QCOMPARE( model->rowCount(), 1 );
        const KeyGroup groupInModel = model->group(model->index(0, 0));
        QVERIFY( !groupInModel.isNull() );
        QCOMPARE( groupInModel.id(), group.id() );
        QCOMPARE( groupInModel.source(), group.source() );
        QCOMPARE( groupInModel.name(), group.name() );
        QCOMPARE( groupInModel.keys().size(), group.keys().size() );
    }
}

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
void AbstractKeyListModelTest::testSetData()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

    const Key key = createTestKey("test@example.net");
    const KeyGroup group = createGroup("test");
    model->setKeys({key});
    model->setGroups({group});
    const KeyGroup updatedGroup = createGroup("updated", {key});
    QVERIFY( !model->setData(QModelIndex(), QVariant::fromValue(updatedGroup)) );
    QVERIFY( !model->setData(model->index(key), QVariant::fromValue(updatedGroup)) );

    const QModelIndex groupIndex = model->index(group);
    QVERIFY( model->setData(groupIndex, QVariant::fromValue(updatedGroup)) );
    const KeyGroup groupInModel = model->group(groupIndex);
    QVERIFY( !groupInModel.isNull() );
    QCOMPARE( groupInModel.name(), updatedGroup.name() );
    QCOMPARE( groupInModel.keys().size(), updatedGroup.keys().size() );
}

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
void AbstractKeyListModelTest::testRemoveGroup()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

    const KeyGroup group = createGroup("test");
    model->setGroups({group});

    {
        const bool result = model->removeGroup(KeyGroup());
        QVERIFY( !result );
        QCOMPARE( model->rowCount(), 1 );
    }

    {
        const KeyGroup otherGroup = createGroup("test2");

        const bool result = model->removeGroup(otherGroup);
        QVERIFY( !result );
        QCOMPARE( model->rowCount(), 1 );
    }

    {
        const bool result = model->removeGroup(group);
        QVERIFY( result );
        QCOMPARE( model->rowCount(), 0 );
    }
}

271
272
273
274
void AbstractKeyListModelTest::testClear()
{
    QScopedPointer<AbstractKeyListModel> model(createModel());

275
276
277
    model->setGroups({
        createGroup("test")
    });
278
279
280
281
282
283
284

    model->clear(AbstractKeyListModel::Keys);
    QCOMPARE( model->rowCount(), 1 );

    model->clear(AbstractKeyListModel::Groups);
    QCOMPARE( model->rowCount(), 0 );
}