newkeyapprovaldialogtest.cpp 24.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
    autotests/newkeyapprovaldialogtest.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 <Libkleo/KeySelectionCombo>
#include <Libkleo/NewKeyApprovalDialog>

#include <QObject>
#include <QTest>

#include <gpgme++/key.h>

#include <gpgme.h>

#include <memory>
22
#include <set>
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

using namespace Kleo;

namespace
{
GpgME::Key createTestKey(const char *uid, GpgME::Protocol protocol = GpgME::UnknownProtocol)
{
    static int count = 0;
    count++;

    gpgme_key_t key;
    gpgme_key_from_uid(&key, uid);
    Q_ASSERT(key);
    if (protocol != GpgME::UnknownProtocol) {
        key->protocol = protocol == GpgME::OpenPGP ? GPGME_PROTOCOL_OpenPGP : GPGME_PROTOCOL_CMS;
    }
    const QByteArray fingerprint = QByteArray::number(count, 16).rightJustified(40, '0');
    key->fpr = strdup(fingerprint.constData());

    return GpgME::Key(key, false);
}

45
template <typename T>
46
struct Widgets
47
{
48
49
50
51
52
53
54
55
56
57
58
59
    std::vector<T *> visible;
    std::vector<T *> hidden;
};

template <typename T>
Widgets<T> visibleAndHiddenWidgets(const QList<T *> &widgets)
{
    Widgets<T> result;
    std::partition_copy(std::begin(widgets), std::end(widgets),
                        std::back_inserter(result.visible),
                        std::back_inserter(result.hidden),
                        std::mem_fn(&QWidget::isVisible));
60
61
62
63
64
65
66
67
    return result;
}
}

class NewKeyApprovalDialogTest: public QObject
{
    Q_OBJECT
private Q_SLOTS:
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    void test__both_protocols_allowed__mixed_not_allowed__openpgp_preferred()
    {
        const GpgME::Protocol forcedProtocol = GpgME::UnknownProtocol;
        const bool allowMixed = false;
        const QString sender = QStringLiteral("sender@example.net");
        const KeyResolver::Solution preferredSolution = {
            GpgME::OpenPGP,
            {createTestKey("sender@example.net", GpgME::OpenPGP)},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {createTestKey("Full Trust <prefer-openpgp@example.net>", GpgME::OpenPGP)}},
                {QStringLiteral("prefer-smime@example.net"), {}},
                {QStringLiteral("sender@example.net"), {createTestKey("sender@example.net", GpgME::OpenPGP)}}
            }
        };
        const KeyResolver::Solution alternativeSolution = {
            GpgME::CMS,
            {createTestKey("sender@example.net", GpgME::CMS)},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {}},
                {QStringLiteral("prefer-smime@example.net"), {createTestKey("Trusted S/MIME <prefer-smime@example.net>", GpgME::CMS)}},
                {QStringLiteral("sender@example.net"), {createTestKey("sender@example.net", GpgME::CMS)}}
            }
        };

        const auto dialog = std::make_unique<NewKeyApprovalDialog>(true,
                                                                   true,
                                                                   sender,
                                                                   preferredSolution,
                                                                   alternativeSolution,
                                                                   allowMixed,
                                                                   forcedProtocol);
        dialog->show();

        const auto signingKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("signing key")));
        QCOMPARE(signingKeyWidgets.visible.size(), 1);
        QCOMPARE(signingKeyWidgets.hidden.size(), 1);
        QCOMPARE(signingKeyWidgets.visible[0]->defaultKey(GpgME::OpenPGP),
                 preferredSolution.signingKeys[0].primaryFingerprint());
        QCOMPARE(signingKeyWidgets.hidden[0]->defaultKey(GpgME::CMS),
                 alternativeSolution.signingKeys[0].primaryFingerprint());

        const auto encryptionKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key")));
        QCOMPARE(encryptionKeyWidgets.visible.size(), 3);
        QCOMPARE(encryptionKeyWidgets.hidden.size(), 3);

        // encryption key widgets for sender come first (visible for OpenPGP, hidden for S/MIME)
        QCOMPARE(encryptionKeyWidgets.visible[0]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.visible[0]->defaultKey(GpgME::OpenPGP),
                 preferredSolution.encryptionKeys.value(sender)[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.hidden[0]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.hidden[0]->defaultKey(GpgME::CMS),
                 alternativeSolution.encryptionKeys.value(sender)[0].primaryFingerprint());

        // encryption key widgets for other recipients follow (visible for OpenPGP, hidden for S/MIME)
        QCOMPARE(encryptionKeyWidgets.visible[1]->property("address").toString(), "prefer-openpgp@example.net");
        QCOMPARE(encryptionKeyWidgets.visible[1]->defaultKey(GpgME::OpenPGP),
                 preferredSolution.encryptionKeys.value("prefer-openpgp@example.net")[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.hidden[1]->property("address").toString(), "prefer-openpgp@example.net");
        QVERIFY(encryptionKeyWidgets.hidden[1]->defaultKey(GpgME::CMS).isEmpty());
        QCOMPARE(encryptionKeyWidgets.visible[2]->property("address").toString(), "prefer-smime@example.net");
        QVERIFY(encryptionKeyWidgets.visible[2]->defaultKey(GpgME::OpenPGP).isEmpty());
        QCOMPARE(encryptionKeyWidgets.hidden[2]->property("address").toString(), "prefer-smime@example.net");
        QCOMPARE(encryptionKeyWidgets.hidden[2]->defaultKey(GpgME::CMS),
                 alternativeSolution.encryptionKeys.value("prefer-smime@example.net")[0].primaryFingerprint());
    }

    void test__both_protocols_allowed__mixed_not_allowed__smime_preferred()
135
    {
136
137
        const GpgME::Protocol forcedProtocol = GpgME::UnknownProtocol;
        const bool allowMixed = false;
138
        const QString sender = QStringLiteral("sender@example.net");
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        const KeyResolver::Solution preferredSolution = {
            GpgME::CMS,
            {createTestKey("sender@example.net", GpgME::CMS)},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {}},
                {QStringLiteral("prefer-smime@example.net"), {createTestKey("Trusted S/MIME <prefer-smime@example.net>", GpgME::CMS)}},
                {QStringLiteral("sender@example.net"), {createTestKey("sender@example.net", GpgME::CMS)}}
            }
        };
        const KeyResolver::Solution alternativeSolution = {
            GpgME::OpenPGP,
            {createTestKey("sender@example.net", GpgME::OpenPGP)},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {createTestKey("Full Trust <prefer-openpgp@example.net>", GpgME::OpenPGP)}},
                {QStringLiteral("prefer-smime@example.net"), {}},
                {QStringLiteral("sender@example.net"), {createTestKey("sender@example.net", GpgME::OpenPGP)}}
            }
        };

        const auto dialog = std::make_unique<NewKeyApprovalDialog>(true,
                                                                   true,
160
                                                                   sender,
161
162
                                                                   preferredSolution,
                                                                   alternativeSolution,
163
                                                                   allowMixed,
164
                                                                   forcedProtocol);
165
        dialog->show();
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

        const auto signingKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("signing key")));
        QCOMPARE(signingKeyWidgets.visible.size(), 1);
        QCOMPARE(signingKeyWidgets.hidden.size(), 1);
        QCOMPARE(signingKeyWidgets.visible[0]->defaultKey(GpgME::CMS),
                 preferredSolution.signingKeys[0].primaryFingerprint());
        QCOMPARE(signingKeyWidgets.hidden[0]->defaultKey(GpgME::OpenPGP),
                 alternativeSolution.signingKeys[0].primaryFingerprint());

        const auto encryptionKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key")));
        QCOMPARE(encryptionKeyWidgets.visible.size(), 3);
        QCOMPARE(encryptionKeyWidgets.hidden.size(), 3);

        // encryption key widgets for sender come first (visible for S/MIME, hidden for OpenPGP)
        QCOMPARE(encryptionKeyWidgets.visible[0]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.visible[0]->defaultKey(GpgME::CMS),
                 preferredSolution.encryptionKeys.value(sender)[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.hidden[0]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.hidden[0]->defaultKey(GpgME::OpenPGP),
                 alternativeSolution.encryptionKeys.value(sender)[0].primaryFingerprint());

        // encryption key widgets for other recipients follow (visible for OpenPGP, hidden for S/MIME)
        QCOMPARE(encryptionKeyWidgets.visible[1]->property("address").toString(), "prefer-openpgp@example.net");
        QVERIFY(encryptionKeyWidgets.visible[1]->defaultKey(GpgME::CMS).isEmpty());
        QCOMPARE(encryptionKeyWidgets.hidden[1]->property("address").toString(), "prefer-openpgp@example.net");
        QCOMPARE(encryptionKeyWidgets.hidden[1]->defaultKey(GpgME::OpenPGP),
                 alternativeSolution.encryptionKeys.value("prefer-openpgp@example.net")[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.visible[2]->property("address").toString(), "prefer-smime@example.net");
        QCOMPARE(encryptionKeyWidgets.visible[2]->defaultKey(GpgME::CMS),
                 preferredSolution.encryptionKeys.value("prefer-smime@example.net")[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.hidden[2]->property("address").toString(), "prefer-smime@example.net");
        QVERIFY(encryptionKeyWidgets.hidden[2]->defaultKey(GpgME::OpenPGP).isEmpty());
198
199
    }

200
    void test__openpgp_only()
201
    {
202
203
        const GpgME::Protocol forcedProtocol = GpgME::OpenPGP;
        const bool allowMixed = false;
204
        const QString sender = QStringLiteral("sender@example.net");
205
206
207
208
209
210
211
212
213
214
215
216
217
        const KeyResolver::Solution preferredSolution = {
            GpgME::OpenPGP,
            {createTestKey("sender@example.net", GpgME::OpenPGP)},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {createTestKey("Full Trust <prefer-openpgp@example.net>", GpgME::OpenPGP)}},
                {QStringLiteral("prefer-smime@example.net"), {}},
                {QStringLiteral("sender@example.net"), {createTestKey("sender@example.net", GpgME::OpenPGP)}}
            }
        };
        const KeyResolver::Solution alternativeSolution = {};

        const auto dialog = std::make_unique<NewKeyApprovalDialog>(true,
                                                                   true,
218
                                                                   sender,
219
220
                                                                   preferredSolution,
                                                                   alternativeSolution,
221
                                                                   allowMixed,
222
                                                                   forcedProtocol);
223
        dialog->show();
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

        const auto signingKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("signing key")));
        QCOMPARE(signingKeyWidgets.visible.size(), 1);
        QCOMPARE(signingKeyWidgets.hidden.size(), 0);
        QCOMPARE(signingKeyWidgets.visible[0]->defaultKey(GpgME::OpenPGP),
                 preferredSolution.signingKeys[0].primaryFingerprint());

        const auto encryptionKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key")));
        QCOMPARE(encryptionKeyWidgets.visible.size(), 3);
        QCOMPARE(encryptionKeyWidgets.hidden.size(), 0);

        // encryption key widget for sender comes first
        QCOMPARE(encryptionKeyWidgets.visible[0]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.visible[0]->defaultKey(GpgME::OpenPGP),
                 preferredSolution.encryptionKeys.value(sender)[0].primaryFingerprint());

        // encryption key widgets for other recipients follow
        QCOMPARE(encryptionKeyWidgets.visible[1]->property("address").toString(), "prefer-openpgp@example.net");
        QCOMPARE(encryptionKeyWidgets.visible[1]->defaultKey(GpgME::OpenPGP),
                 preferredSolution.encryptionKeys.value("prefer-openpgp@example.net")[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.visible[2]->property("address").toString(), "prefer-smime@example.net");
        QVERIFY(encryptionKeyWidgets.visible[2]->defaultKey(GpgME::OpenPGP).isEmpty());
246
247
    }

248
    void test__smime_only()
249
    {
250
251
        const GpgME::Protocol forcedProtocol = GpgME::CMS;
        const bool allowMixed = false;
252
        const QString sender = QStringLiteral("sender@example.net");
253
254
255
256
257
258
259
260
261
262
263
264
265
        const KeyResolver::Solution preferredSolution = {
            GpgME::CMS,
            {createTestKey("sender@example.net", GpgME::CMS)},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {}},
                {QStringLiteral("prefer-smime@example.net"), {createTestKey("Trusted S/MIME <prefer-smime@example.net>", GpgME::CMS)}},
                {QStringLiteral("sender@example.net"), {createTestKey("sender@example.net", GpgME::CMS)}}
            }
        };
        const KeyResolver::Solution alternativeSolution = {};

        const auto dialog = std::make_unique<NewKeyApprovalDialog>(true,
                                                                   true,
266
                                                                   sender,
267
268
                                                                   preferredSolution,
                                                                   alternativeSolution,
269
                                                                   allowMixed,
270
                                                                   forcedProtocol);
271
        dialog->show();
272

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
        const auto signingKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("signing key")));
        QCOMPARE(signingKeyWidgets.visible.size(), 1);
        QCOMPARE(signingKeyWidgets.hidden.size(), 0);
        QCOMPARE(signingKeyWidgets.visible[0]->defaultKey(GpgME::CMS),
                 preferredSolution.signingKeys[0].primaryFingerprint());

        const auto encryptionKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key")));
        QCOMPARE(encryptionKeyWidgets.visible.size(), 3);
        QCOMPARE(encryptionKeyWidgets.hidden.size(), 0);

        // encryption key widget for sender comes first
        QCOMPARE(encryptionKeyWidgets.visible[0]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.visible[0]->defaultKey(GpgME::CMS),
                 preferredSolution.encryptionKeys.value(sender)[0].primaryFingerprint());

        // encryption key widgets for other recipients follow
        QCOMPARE(encryptionKeyWidgets.visible[1]->property("address").toString(), "prefer-openpgp@example.net");
        QVERIFY(encryptionKeyWidgets.visible[1]->defaultKey(GpgME::CMS).isEmpty());
        QCOMPARE(encryptionKeyWidgets.visible[2]->property("address").toString(), "prefer-smime@example.net");
        QCOMPARE(encryptionKeyWidgets.visible[2]->defaultKey(GpgME::CMS),
                 preferredSolution.encryptionKeys.value("prefer-smime@example.net")[0].primaryFingerprint());
    }

    void test__both_protocols_allowed__mixed_allowed()
    {
        const GpgME::Protocol forcedProtocol = GpgME::UnknownProtocol;
        const bool allowMixed = true;
        const QString sender = QStringLiteral("sender@example.net");
        const KeyResolver::Solution preferredSolution = {
            GpgME::UnknownProtocol,
            {createTestKey("sender@example.net", GpgME::OpenPGP), createTestKey("sender@example.net", GpgME::CMS)},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {createTestKey("Full Trust <prefer-openpgp@example.net>", GpgME::OpenPGP)}},
                {QStringLiteral("prefer-smime@example.net"), {createTestKey("Trusted S/MIME <prefer-smime@example.net>", GpgME::CMS)}},
                {QStringLiteral("unknown@example.net"), {}},
                {QStringLiteral("sender@example.net"), {createTestKey("sender@example.net", GpgME::OpenPGP), createTestKey("sender@example.net", GpgME::CMS)}}
            }
        };
        const KeyResolver::Solution alternativeSolution = {};

        const auto dialog = std::make_unique<NewKeyApprovalDialog>(true,
                                                                   true,
                                                                   sender,
                                                                   preferredSolution,
                                                                   alternativeSolution,
                                                                   allowMixed,
                                                                   forcedProtocol);
        dialog->show();

        const auto signingKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("signing key")));
        QCOMPARE(signingKeyWidgets.visible.size(), 2);
        QCOMPARE(signingKeyWidgets.hidden.size(), 0);
        QCOMPARE(signingKeyWidgets.visible[0]->defaultKey(GpgME::OpenPGP),
                 preferredSolution.signingKeys[0].primaryFingerprint());
        QCOMPARE(signingKeyWidgets.visible[1]->defaultKey(GpgME::CMS),
                 preferredSolution.signingKeys[1].primaryFingerprint());

        const auto encryptionKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key")));
        QCOMPARE(encryptionKeyWidgets.visible.size(), 5);
        QCOMPARE(encryptionKeyWidgets.hidden.size(), 0);

        // encryption key widgets for sender come first
        QCOMPARE(encryptionKeyWidgets.visible[0]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.visible[0]->defaultKey(GpgME::OpenPGP),
                 preferredSolution.encryptionKeys.value(sender)[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.visible[1]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.visible[1]->defaultKey(GpgME::CMS),
                 preferredSolution.encryptionKeys.value(sender)[1].primaryFingerprint());

        // encryption key widgets for other recipients follow
        QCOMPARE(encryptionKeyWidgets.visible[2]->property("address").toString(), "prefer-openpgp@example.net");
        QCOMPARE(encryptionKeyWidgets.visible[2]->defaultKey(GpgME::UnknownProtocol),
                 preferredSolution.encryptionKeys.value("prefer-openpgp@example.net")[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.visible[3]->property("address").toString(), "prefer-smime@example.net");
        QCOMPARE(encryptionKeyWidgets.visible[3]->defaultKey(GpgME::UnknownProtocol),
                 preferredSolution.encryptionKeys.value("prefer-smime@example.net")[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.visible[4]->property("address").toString(), "unknown@example.net");
        QVERIFY(encryptionKeyWidgets.visible[4]->defaultKey(GpgME::UnknownProtocol).isEmpty());
    }

    void test__both_protocols_allowed__mixed_allowed__no_sender_keys()
    {
        const GpgME::Protocol forcedProtocol = GpgME::UnknownProtocol;
        const bool allowMixed = true;
        const QString sender = QStringLiteral("sender@example.net");
        const KeyResolver::Solution preferredSolution = {
            GpgME::UnknownProtocol,
            {},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {createTestKey("Full Trust <prefer-openpgp@example.net>", GpgME::OpenPGP)}},
                {QStringLiteral("prefer-smime@example.net"), {createTestKey("Trusted S/MIME <prefer-smime@example.net>", GpgME::CMS)}},
                {QStringLiteral("unknown@example.net"), {}},
                {QStringLiteral("sender@example.net"), {}}
            }
        };
        const KeyResolver::Solution alternativeSolution = {};

        const auto dialog = std::make_unique<NewKeyApprovalDialog>(true,
                                                                   true,
                                                                   sender,
                                                                   preferredSolution,
                                                                   alternativeSolution,
                                                                   allowMixed,
                                                                   forcedProtocol);
        dialog->show();

        const auto signingKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("signing key")));
        QCOMPARE(signingKeyWidgets.visible.size(), 2);
        QCOMPARE(signingKeyWidgets.hidden.size(), 0);

        const auto encryptionKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key")));
        QCOMPARE(encryptionKeyWidgets.visible.size(), 5);
        QCOMPARE(encryptionKeyWidgets.hidden.size(), 0);

        // encryption key widgets for sender come first
        QCOMPARE(encryptionKeyWidgets.visible[0]->property("address").toString(), sender);
        QCOMPARE(encryptionKeyWidgets.visible[1]->property("address").toString(), sender);

        // encryption key widgets for other recipients follow
        QCOMPARE(encryptionKeyWidgets.visible[2]->property("address").toString(), "prefer-openpgp@example.net");
        QCOMPARE(encryptionKeyWidgets.visible[2]->defaultKey(GpgME::UnknownProtocol),
                 preferredSolution.encryptionKeys.value("prefer-openpgp@example.net")[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.visible[3]->property("address").toString(), "prefer-smime@example.net");
        QCOMPARE(encryptionKeyWidgets.visible[3]->defaultKey(GpgME::UnknownProtocol),
                 preferredSolution.encryptionKeys.value("prefer-smime@example.net")[0].primaryFingerprint());
        QCOMPARE(encryptionKeyWidgets.visible[4]->property("address").toString(), "unknown@example.net");
        QVERIFY(encryptionKeyWidgets.visible[4]->defaultKey(GpgME::UnknownProtocol).isEmpty());
    }

    void test__both_protocols_allowed__mixed_allowed__encrypt_only()
    {
        const GpgME::Protocol forcedProtocol = GpgME::UnknownProtocol;
        const bool allowMixed = true;
        const QString sender = QStringLiteral("sender@example.net");
        const KeyResolver::Solution preferredSolution = {
            GpgME::UnknownProtocol,
            {createTestKey("sender@example.net", GpgME::OpenPGP), createTestKey("sender@example.net", GpgME::CMS)},
            {
                {QStringLiteral("prefer-openpgp@example.net"), {createTestKey("Full Trust <prefer-openpgp@example.net>", GpgME::OpenPGP)}},
                {QStringLiteral("prefer-smime@example.net"), {createTestKey("Trusted S/MIME <prefer-smime@example.net>", GpgME::CMS)}},
                {QStringLiteral("unknown@example.net"), {}},
                {QStringLiteral("sender@example.net"), {createTestKey("sender@example.net", GpgME::OpenPGP), createTestKey("sender@example.net", GpgME::CMS)}}
            }
        };
        const KeyResolver::Solution alternativeSolution = {};

        const auto dialog = std::make_unique<NewKeyApprovalDialog>(true,
                                                                   false,
                                                                   sender,
                                                                   preferredSolution,
                                                                   alternativeSolution,
                                                                   allowMixed,
                                                                   forcedProtocol);
        dialog->show();

        const auto signingKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("signing key")));
        QCOMPARE(signingKeyWidgets.visible.size(), 0);
        QCOMPARE(signingKeyWidgets.hidden.size(), 0);

        const auto encryptionKeyWidgets = visibleAndHiddenWidgets(dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key")));
        QCOMPARE(encryptionKeyWidgets.visible.size(), 5);
        QCOMPARE(encryptionKeyWidgets.hidden.size(), 0);
435
436
437
438
439
    }
};

QTEST_MAIN(NewKeyApprovalDialogTest)
#include "newkeyapprovaldialogtest.moc"