Commit c5f0aa65 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Do not remove all keys if OpenPGP only and S/MIME only are both possible

This commit also adds a first autotest for KeyResolver including a bunch
of test keys.

GnuPG-bug-id: 5283
parent ec81b2b5
Pipeline #55215 passed with stage
in 10 minutes and 55 seconds
......@@ -23,6 +23,13 @@ ecm_add_test(
LINK_LIBRARIES KF5::Libkleo Qt5::Test
)
ecm_add_test(
keyresolvertest.cpp
keyresolvertest.qrc
TEST_NAME keyresolvertest
LINK_LIBRARIES KF5::Libkleo Qt5::Widgets Qt5::Test
)
ecm_add_tests(
newkeyapprovaldialogtest.cpp
LINK_LIBRARIES KF5::Libkleo Qt5::Widgets Qt5::Test
......
openpgp-revocs.d/
random_seed
tofu.db
Created: 20210322T134216
Key: (private-key (ecc (curve Ed25519)(flags eddsa)(q
#402338211D73F530D81F8FD2D906EEC3E127C549A031E3514EF408F26119FC3BB0#)
(d #89520701F0C40E2FCE89DF7378000358557BAF9765588464D2A434C60577636E#)
))
Created: 20210322T135004
Key: (protected-private-key (rsa (n #00D93727139ED5044AEEB02BD9D2096F2C
76BBEBFB7E64E51592C81D11A44AE96B14E6DD2F333ABA41FCD24860932859CBEFA0C5
FA22EC3F5085CB0F7BC1D61F1C8FA1CADC04A916ADECB576250E02D6FB0FB8AA8C6347
08B2ABDFDB0059D634B045AADF69E20E3E97A13110CB6436CA0A1881070ED14D2EC482
09BEAB0DE56B6DE178236AAA0314A7474EC85314857856633BC572A864EE2571C157B8
B944B5ECB3C85B5CEEBB4FF42928DA57DA45658D9268DED792B818F0CF83ECB5ED97AC
DB5A10F1F1E800565D70D5FE175C93EF3E4468765015DEBB768A5F53C061756F128181
D6DA2B08ED396E7765C58659B4EF37173CE33E6CD7DD0BAD2793C4F88B75#)(e
#010001#)(protected openpgp-s2k3-ocb-aes ((sha1 "\fT5g?"
"53686272")#D98B1FCB502A730F10473897#)#146BB2167B12E4703EDD7F8A7C5F63
C68DC63536E6C98DCB8A16BC1D948F366D74F9B991C6050B79BC521B767E9A67BB095E
C117900C945DAC62EC4913DAFEC40D1E2B6C698F0028981D9E927D2BC0D84305DBA84B
480BE98E7B438BB304A96596A21B4FDC7A2BC65E8A0428D2A48D99D3845FE4E31E5B81
655F89F551873526B1FF4BB3DA0DA81959F64D4730E09DBF3A20D739AF09AEF8D39E70
51D422A0692DF157D14879D66BF5A6463EC9CF1249B2382D954CC1C2C7944C22DC3E06
7E7752D1D38CAE872AF357373FA69279B392EFC3134ADA09D3DD18B38D1AFABDD434AD
E6E8C83FE4A0B7451591C785983D1FCB949B4C1070D6DF84B34241E08E75549FC4C3E6
F983D729BC5F3577830EAD736EA8A8A05ED2E95D855C4083DC6396B252CA23C807D0AC
66AD91EE4003D0EBF979A316663A14B3D175D5E270308C68E0E708E7813A070ECC6404
688E9944EC055669D7B1CC07B2A50096C515B6F8E9EB8A928DBAB0037056062D816410
100EF8A74B01D988D51D917D758185C51E423B49E23545A959A8E9678265ADAFFB0670
4C168C384280ADC4990356A7EF01DD0AC0FACADB447F57AD7CF3F71DA694A1CB23E9D1
DF514921D0B903BEA56EC990F2B4A351B172671873D4CB375800E4E41DC5FD889B9CBC
DC868AEE0298C07BA5E1D0B88F4808014D743E138B883905634EB0A1184ACA435EFA4E
7152C9DA0D87E1B37255AFF2B0F44E73ACB3690BD0A54469C6F27A0FAD8C8D6D36E143
EB44742F94DC0B5974443967434CEAFF0832D922EA3CDE600459297DCE28EEBB833A66
1E31140A826561F9713DD3BF0E4B5A82A69A89F1A98D69F06DC9719520F714FE7D008A
F0D3307B857B39484C0AEB602B9C61D9A5844A0AD9C166C0B6E9626938F13F93A9FC87
A8D4F0E06F919D16E0D4ABB252AB729E217619AD33D5644A7803CBFB2D29C87BB2BA5F
12F0850DFD2BEE9FAB592A3529DDEE9E9A4D#)(protected-at
"20210322T135004")))
Created: 20210322T134145
Key: (private-key (ecc (curve Ed25519)(flags eddsa)(q
#400832EBDB9C115F9295D28F2C485857D039F96E713DB82A7938812598594C7B84#)
(d #5EE2CD85C4E935B6C966526D0ED6B6A75B68DA8F8A97FE700A779A5F1DDDCC81#)
))
Created: 20210322T134956
Key: (protected-private-key (rsa (n #00D787B9940C8083BA526A58C9CB55FA34
9BD8267C30D1FA6D116DBEE5CDC1BD21B27D54EF5AEBE253B64A3CA518375716B2A48E
DF23A737A227251802AE43BB1839ADA0443F6EFB02040D21D0C753B1B3581F469B2BCD
22FBBF7A7A108E56CE80ECC85D2CD8A7158903887E8AEF0D3E8BB2524752F8D5CDB285
EFAA90EBA0E9CCB039DCFBC7AD3B618047077599E28A9424DBF20DC8212B57E7F0906A
0AB48678462C1FEBA6E5B0B1DE9902F52C56CB46AFCF8BCACC61D4891424C59FF04514
B0E36BD14A45E1982DF6A480649BF4FAF41759B83624909DE64CA0CF478CBEC6BB389A
128D70DE51564BCB0F7EF2D89546E7FBB99DF66985B06BFF8DC811B67BC5#)(e
#010001#)(protected openpgp-s2k3-ocb-aes ((sha1 #1FF718CDDA024F83#
"53686272")#F8F33DD09D5D50563E0940D7#)#987A0F3D878875F6F9E1B62237DBC1
D888212F697E9AAB7F5177B2F832086181B2571F151FB2461B13C5EFEC499707E7B1A8
C380BD05DCFA2D899056ECCF25B2F32D167C0B79C7390CB2D94637701ACDE7B1D67C3D
AE479504319BD6844C1B2671263A676F862B5A9636FC56C0EF5DCDDB534F985A8B705E
E1D6BEC02F1CBBB197E2953666FE802918E9888323947B8677CF5E67F426D41F0C10E5
9699C1FE5A1D6F9A242C6D26C7AACB86BF54B163CE9189A961093E49665B9C2603BD4E
08225A58378E88420E6EB366B15025414010D2FD235CC935A76661DEF928E8110FF07D
9B0CC1E1133D1D7686AFD24C85487363F63C7B7816B887672F74922590B54306DE2F15
C29F3CED3B3C64E641F30207590DD5DE1853141493115CD946E010A9E8E55E9D4B7C6B
438B0C78835807FE7309AC8A8BF17E9951BA9707575D7CDDADA684DECD446D9DF4263E
5803716FAF31B279FB8214209458EE055A04247C21F5298C73C6037AECA2624071F0AE
A8817979908EB4CA190D069F65B214CC6ACA88A48D0C09874644E13904C6A53948F601
77D9C15C450CEFA75E1AC7E851239E1374012E89B21F6FF9B038F7034E210D989F2462
69E914A75AE3449C6C2B95E1CE5C3F956322074AF0FB78ADDDB1FACDB7D0A6641EE436
5BE00472B246431A25018E5D6DAA8CBF419D7CDAE1997DA67715A8BC403EB7EFA81C7E
0C8018FA9156B2B5BA310BF5C8AF5AFF4E0F7F3D80D25855D4F0AB571580D66297C682
D4913E9B51F1B1CFE1E8D4CAEFB42632728EDF802C4666200E334EBE590577D9D866EC
D7B972D8DFE348C1E47C5F71D1324AC5064217709319487255E9C231A06B69F39DD107
47322B6926F8CEAFE16F47ECDA4177B6FB7319B1E14BDBAA8AAF349C824D9C78932EB4
8C2201D7698F798D1812D53FAB7F4DC967C64DF5AE582EF218BEE1BCF0C1832A5E823E
3BA133AD82E86270320619686CCD0B915C8C#)(protected-at
"20210322T134956")))
Created: 20210322T135027
Key: (protected-private-key (rsa (n #00CB691B6AF9496B3B30D1262C5924A5F4
75AB3A0BBBDED7283254DAAF66AEAA20D3B142883959E1D382D7D9480A79B6070CAF44
E60861A0939C191302B0BBF9837821B141EBEAF8DCD17C074B13A5459D90E7BD8F3E82
BD98F542CC76E4F8038726C1671023CF49CBCEAE937DCCA14A2CDD8C12EA1269898229
4108D76E6432D17AA5CB50C2D94F57ED312B1B8C352873E543867E019C93AC26EADB8F
1047939C2C8BF7C9A5FD42F7BDAFB48ED267F729980B35D8448A64B7BAD0FD8885041A
6728AAAF1155D328F01F11ADBE0FBC591D08723E5B0270FC703DE3B075D072FEDC420C
FE8901339C133E333A697424A8F530C4A6A79CA9B0734E066068915F4E07#)(e
#010001#)(protected openpgp-s2k3-ocb-aes ((sha1 #E5CD258D08FA0B17#
"53686272")#E359E322AE99FE55AB12C20B#)#12425AFB59FFBD153A29D61B063756
CA14F2FA802485A108C01BECCD21DDFE2192E4B14ECDF644D229AF6427644B86493544
D55CFFE2E992EEC419EFCCC641A0B4431D9EB3090C4034A9F9E51486E30D1892DE64F8
EB365EC0D65C245E3030A9D9724D3F26A7DE980CF1074D1574D3B982C6C4FDEC4C20CA
93CF315325210E00B9F6916AF4175A100D8D67A94833757470C8EACA75FDF611B81245
0B8F0A8E8BA6E7DFA4BA937B750C5E5F863AB28612D50A330450D6EB7534AEF23BCD5C
7D9E74D7B71B1201921909295FC30F82512F6D610F267A5F971ABE5D156114B1A4A2E8
945A8E19CBA734EDCD461E4D593BEC3C01B068041079233FF3F63A919030AE8C32ED4E
4FE58DC350D32ADE953C0A6481DAC100ECEF27FE6BB96AAF96B2D49541825A4266A85F
6CB9E8E004E7E6C87A3980D0EB513ED06A622C8B14EEC2C1DF473EB5BF133B29DA54C9
22060A4369E17288D8048B5725A413548541BF4EB656843C2C9B2120CD1C49B5DAA4FB
6892E9795F06508303539A5F345584E0E44A7B323CCEF80C6CFC0CFE3342E929344986
8D84B82F7CB2B005B72C1A2A85CB13ADDDFE00207F63B306464539D1CEE017C9E7F9C1
C4DD789F98F5306975015076DC752840F71B654A6F23ED0BFA1F5D06F1F760B97E30FE
89560A8BE3D5BFB76B3476D9B58D5BA61C7F9145395CD248905206958D4579AD1444F9
591CE5ADBD61330D3D5883293AD142CB8D5C01AF30B0F83AE3C90042848BF0ABF6A937
A8D000DA8ECEA9037E31BB13090C498A148CD8E032649A96E4D9A0E23360FDC52D4E00
CA020455B3D117B9D3DA198D34C0C9F7C445436E7CD248EE4C4B3F04B5E0A98DA0DF08
E946494D683E0BDB25E49FAAD397B7B76BF855FED0D7ED72E1747C331ED923A2C76B11
77DE39C6AF783A61CD46CCD73DC529EC30176C7F3900410944240B91C492C8BAAF3CF6
55B7848230F5A76B7AF90E44AC825C4645B0#)(protected-at
"20210322T135027")))
Created: 20210322T134216
Key: (private-key (ecc (curve Curve25519)(flags djb-tweak)(q
#40E78024E41F2911080AE20FD6B04B2BBD598C3EBE6197C9D60ABECFB216D74375#)
(d #69297679FA3D383441D41FD08A4DCD39B8CC9E68985FDD39E33F8DF9DCF15DB0#)
))
Created: 20210322T134220
Key: (private-key (ecc (curve Curve25519)(flags djb-tweak)(q
#4006C1724E47BD4F9F9EB41EEE699D8FDA330C632C4E6214ED831388A4ED556003#)
(d #7116799C23A8F148750B68B24DE6857C24BCC8F43046A8C532A51AB03DB065C0#)
))
Created: 20210322T134151
Key: (private-key (ecc (curve Curve25519)(flags djb-tweak)(q
#4023F662E532E75530DE11A91FCF0E3012E3C6DDC7D7AC9D66C9BD2AFDB1C32D2E#)
(d #77E8EFAA560CB50690FE40582036AE44C913D2F9BF4DD549B144D97EE2371B00#)
))
Created: 20210322T134157
Key: (private-key (ecc (curve Curve25519)(flags djb-tweak)(q
#40DD580043C3B59DF4D899B597496151AC7E0182C73C83681447972B0FEFE7676D#)
(d #7144E019242E5312B714CECFDBB47A9A02AD0BAED257CBAFD6E1B071838020C0#)
))
Created: 20210322T134151
Key: (private-key (ecc (curve Ed25519)(flags eddsa)(q
#4010C64C4A284C4B81CA883590BF10E986F3D3C1D5BF45FAC60C8728B565753342#)
(d #E5ADD71A3A0B6C993A012EF970D55DA7EB87B78DA6CAF882CC94CF70EF9E6AFE#)
))
Created: 20210322T134220
Key: (private-key (ecc (curve Ed25519)(flags eddsa)(q
#40CD91F555AD506E9564604BD0F20246D4E279894F370DACAD05F578016E0FC4E2#)
(d #3D5FE89878F6133E58EAC83EFBBFF6DFBA9382DF7CDA6F5BAE05185AD0D1C7AD#)
))
Created: 20210322T134157
Key: (private-key (ecc (curve Ed25519)(flags eddsa)(q
#400480F599560A51C9E0232E151D77339E98EEA9FE1FC141C96D0D42042C391269#)
(d #F665058FB22AD3B4B5A9C804B942DEC97C4834C45017917D9FCD0029701A7AA2#)
))
Created: 20210322T134145
Key: (private-key (ecc (curve Curve25519)(flags djb-tweak)(q
#40B8CCC2EFFDEF6E956DD72BB5D8A0DEE5890D7600263396F6D68356274C3C107C#)
(d #6CB1D7A33CE014ABCEFFC57BD3D580056D14E0821468009066635E4D40068550#)
))
# Fixture for KeyResolverTest
## Setup
Set the `GNUPGHOME` environment variable to this folder:
```
export GNUPGHOME=$(pwd)
```
## Generate OpenPGP test keys
Note: gpg 2.3 is needed for the --no-auto-trust-new-key option.
```
# Create an ultimately trusted CA key
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" "Ultimately trusted CA <ca-ultimate@example.net>" default default never
# Create a fully trusted CA key
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" "Fully trusted CA <ca-full@example.net>" default default never
gpg --edit-key --command-fd 0 ca-full@example.net <<eof
trust
4
save
eof
gpg --quick-sign-key --default-key $(gpg -K --batch --with-colons ca-ultimate@example.net | grep fpr | head -1 | cut -d ':' -f 10) --batch --pinentry-mode loopback --passphrase "" $(gpg -k --batch --with-colons ca-full@example.net | grep fpr | head -1 | cut -d ':' -f 10)
# Create a marginally trusted CA key
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" "Marginally trusted CA <ca-marginal@example.net>" default default never
gpg --edit-key --command-fd 0 ca-marginal@example.net <<eof
trust
3
save
eof
gpg --quick-sign-key --default-key $(gpg -K --batch --with-colons ca-ultimate@example.net | grep fpr | head -1 | cut -d ':' -f 10) --batch --pinentry-mode loopback --passphrase "" $(gpg -k --batch --with-colons ca-marginal@example.net | grep fpr | head -1 | cut -d ':' -f 10)
# Sender with OpenPGP and S/MIME key
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" sender-mixed@example.net default default never
# Sender with OpenPGP key only
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" sender-openpgp@example.net default default never
# Recipient with full validity
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" --no-auto-trust-new-key "prefer-openpgp@example.net" default default never
gpg --delete-secret-keys --batch --yes $(gpg -K --batch --with-colons prefer-openpgp@example.net | grep fpr | head -1 | cut -d ':' -f 10)
gpg --quick-sign-key --default-key $(gpg -K --batch --with-colons ca-full@example.net | grep fpr | head -1 | cut -d ':' -f 10) --batch --pinentry-mode loopback --passphrase "" $(gpg -k --batch --with-colons prefer-openpgp@example.net | grep fpr | head -1 | cut -d ':' -f 10)
# Recipient with marginal validity
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" --no-auto-trust-new-key "prefer-smime@example.net" default default never
gpg --delete-secret-keys --batch --yes $(gpg -K --batch --with-colons prefer-smime@example.net | grep fpr | head -1 | cut -d ':' -f 10)
gpg --quick-sign-key --default-key $(gpg -K --batch --with-colons ca-marginal@example.net | grep fpr | head -1 | cut -d ':' -f 10) --batch --pinentry-mode loopback --passphrase "" $(gpg -k --batch --with-colons prefer-smime@example.net | grep fpr | head -1 | cut -d ':' -f 10)
```
### Unused OpenPGP keys
```
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" "Untrusted OpenPGP 1 <untrusted-openpgp@example.net>" default default never
gpg --delete-secret-keys --batch --yes $(gpg -K --batch --with-colons "Untrusted OpenPGP 1 <untrusted-openpgp@example.net>" | grep fpr | head -1 | cut -d ':' -f 10)
gpg --edit-key --command-fd 0 "Untrusted OpenPGP 1 <untrusted-openpgp@example.net>" <<eof
trust
1
save
eof
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" "Untrusted OpenPGP 2 <untrusted-openpgp@example.net>" default default never
gpg --delete-secret-keys --batch --yes $(gpg -K --batch --with-colons "Untrusted OpenPGP 2 <untrusted-openpgp@example.net>" | grep fpr | head -1 | cut -d ':' -f 10)
gpg --edit-key --command-fd 0 "Untrusted OpenPGP 2 <untrusted-openpgp@example.net>" <<eof
trust
1
save
eof
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" "Untrusted Mixed OpenPGP <untrusted-mixed@example.net>" default default never
gpg --delete-secret-keys --batch --yes $(gpg -K --batch --with-colons untrusted-mixed@example.net | grep fpr | head -1 | cut -d ':' -f 10)
gpg --edit-key --command-fd 0 untrusted-mixed@example.net <<eof
trust
1
save
eof
gpg --quick-gen-key --batch --pinentry-mode loopback --passphrase "" "Expired <expired@example.net>" default default seconds=1
gpg --delete-secret-keys --batch --yes $(gpg -K --batch --with-colons expired@example.net | grep fpr | head -1 | cut -d ':' -f 10)
```
## Generate S/MIME test keys
```
gpgsm --gen-key --batch --pinentry-mode loopback --passphrase "" <<eof | gpgsm --import
dummy
Key-Type: RSA
Key-Length: 2048
Key-Usage: sign, encrypt
Serial: random
Name-DN: CN=Sender Mixed,O=example,C=DE
Name-Email: sender-mixed@example.net
eof
gpgsm -k sender-mixed@example.net | grep 'sha1 fpr' | sed 's/\s*sha1 fpr:\s*\([0-9A-F].*\)/\1 S relax/' >>trustlist.txt
gpgsm --gen-key --batch --pinentry-mode loopback --passphrase "" <<eof | gpgsm --import
dummy
Key-Type: RSA
Key-Length: 2048
Key-Usage: sign, encrypt
Serial: random
Name-DN: CN=Sender S/MIME,O=example,C=DE
Name-Email: sender-smime@example.net
eof
gpgsm -k sender-smime@example.net | grep 'sha1 fpr' | sed 's/\s*sha1 fpr:\s*\([0-9A-F].*\)/\1 S relax/' >>trustlist.txt
gpgsm --gen-key --batch --pinentry-mode loopback --passphrase "" <<eof | gpgsm --import
dummy
Key-Type: RSA
Key-Length: 2048
Key-Usage: sign, encrypt
Serial: random
Name-DN: CN=Trusted S/MIME,O=example,C=DE
Name-Email: prefer-smime@example.net
eof
gpgsm -k prefer-smime@example.net | grep 'sha1 fpr' | sed 's/\s*sha1 fpr:\s*\([0-9A-F].*\)/\1 S relax/' >>trustlist.txt
```
60:E1:96:4D:9C:EA:44:8E:76:E7:66:42:47:3D:56:7F:FE:95:8E:8A S relax
06:A2:67:BE:B6:04:6A:13:DC:D2:26:56:A6:F4:9D:8B:6E:B3:7A:16 S relax
B8:26:00:8F:4F:5E:8A:86:E3:8A:CD:45:AF:FC:81:AD:01:36:BD:A0 S relax
/*
autotests/keyresolvertest.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/KeyCache>
#include <Libkleo/KeyResolver>
#include <QObject>
#include <QSignalSpy>
#include <QTest>
#include <gpgme++/key.h>
#include <memory>
using namespace Kleo;
using namespace GpgME;
class KeyResolverTest: public QObject
{
Q_OBJECT
private Q_SLOTS:
void init()
{
mGnupgHome = QTest::qExtractTestData("/fixtures/keyresolvertest");
qputenv("GNUPGHOME", mGnupgHome->path().toLocal8Bit());
qDebug() << "Using GNUPGHOME" << qgetenv("GNUPGHOME");
// hold a reference to the key cache to avoid rebuilding while the test is running
mKeyCache = KeyCache::instance();
}
void cleanup()
{
mKeysResolvedSpy.reset();
// verify that nobody else holds a reference to the key cache
QVERIFY(mKeyCache.use_count() == 1);
mKeyCache.reset();
mGnupgHome.reset();
}
void test_verify_test_keys()
{
{
const auto keys = KeyCache::instance()->findByEMailAddress("sender-mixed@example.net");
QCOMPARE(keys.size(), 2);
QVERIFY((keys[0].protocol() == OpenPGP && keys[1].protocol() == CMS) ||
(keys[1].protocol() == OpenPGP && keys[0].protocol() == CMS));
QVERIFY(keys[0].hasSecret() && keys[0].canEncrypt() && keys[0].canSign());
QCOMPARE(keys[0].userID(0).validity(), UserID::Ultimate);
QVERIFY(keys[1].hasSecret() && keys[1].canEncrypt() && keys[1].canSign());
QCOMPARE(keys[1].userID(0).validity(), UserID::Ultimate);
}
{
const auto keys = KeyCache::instance()->findByEMailAddress("sender-openpgp@example.net");
QCOMPARE(keys.size(), 1);
QVERIFY(keys[0].protocol() == OpenPGP);
QVERIFY(keys[0].hasSecret() && keys[0].canEncrypt() && keys[0].canSign());
QCOMPARE(keys[0].userID(0).validity(), UserID::Ultimate);
}
{
const auto keys = KeyCache::instance()->findByEMailAddress("prefer-openpgp@example.net");
QCOMPARE(keys.size(), 1);
QVERIFY(keys[0].protocol() == OpenPGP);
QVERIFY(keys[0].canEncrypt());
QCOMPARE(keys[0].userID(0).validity(), UserID::Full);
}
{
const auto keys = KeyCache::instance()->findByEMailAddress("prefer-smime@example.net");
QCOMPARE(keys.size(), 2);
const Key openpgp = keys[0].protocol() == OpenPGP ? keys[0] : keys[1];
QVERIFY(openpgp.protocol() == OpenPGP);
QVERIFY(openpgp.canEncrypt());
QCOMPARE(openpgp.userID(0).validity(), UserID::Marginal);
const Key smime = keys[0].protocol() == CMS ? keys[0] : keys[1];
QVERIFY(smime.protocol() == CMS);
QVERIFY(smime.canEncrypt());
QVERIFY(smime.userID(0).validity() >= UserID::Full);
}
}
void test_openpgp_is_preferred_if_openpgp_only_and_smime_only_are_both_possible()
{
KeyResolver resolver(/*encrypt=*/ true, /*sign=*/ true);
resolver.setSender(QStringLiteral("sender-mixed@example.net"));
spyOnKeysResolvedSignal(&resolver);
resolver.start(/*showApproval=*/ false);
verifyKeysResolvedSignalEmittedWith(/*success=*/ true, /*sendUnencrypted=*/ false);
QCOMPARE(resolver.signingKeys().value(OpenPGP).size(), 1);
QCOMPARE(resolver.signingKeys().value(CMS).size(), 0);
QCOMPARE(resolver.encryptionKeys().value(OpenPGP).size(), 1);
QCOMPARE(resolver.encryptionKeys().value(OpenPGP).value("sender-mixed@example.net").size(), 1);
QCOMPARE(resolver.encryptionKeys().value(CMS).size(), 0);
}
void spyOnKeysResolvedSignal(KeyResolver *resolver)
{
mKeysResolvedSpy = std::make_unique<QSignalSpy>(resolver, &KeyResolver::keysResolved);
QVERIFY(mKeysResolvedSpy->isValid());
}
void verifyKeysResolvedSignalEmittedWith(bool success, bool sendUnencrypted)
{
QCOMPARE(mKeysResolvedSpy->count(), 1);
const QList<QVariant> arguments = mKeysResolvedSpy->takeFirst();
QCOMPARE(arguments.at(0).toBool(), success);
QCOMPARE(arguments.at(1).toBool(), sendUnencrypted);
}
private:
QSharedPointer<QTemporaryDir> mGnupgHome;
std::shared_ptr<const KeyCache> mKeyCache;
std::unique_ptr<QSignalSpy> mKeysResolvedSpy;
};
QTEST_MAIN(KeyResolverTest)
#include "keyresolvertest.moc"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment