2
* Copyright (C) 2006 Brad Hards <bradh@frogmouth.net>
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#include <QtTest/QtTest>
32
static int setenv(const char *name, const char *value, int overwrite)
37
if (!overwrite && getenv(name)) return 0;
39
i = strlen(name) + strlen(value) + 2;
53
class PgpUnitTest : public QObject
59
void cleanupTestCase();
62
QCA::Initializer* m_init;
65
void PgpUnitTest::initTestCase()
67
m_init = new QCA::Initializer;
68
#include "../fixpaths.include"
71
void PgpUnitTest::cleanupTestCase()
76
void PgpUnitTest::testKeyRing()
78
// activate the KeyStoreManager and block until ready
79
QCA::keyStoreManager()->start();
80
QCA::keyStoreManager()->waitForBusyFinished();
82
if ( QCA::isSupported( QStringList( QString( "keystorelist" ) ),
83
QString( "qca-gnupg" ) ) )
85
QCA::KeyStoreManager *ksm = QCA::keyStoreManager();
86
QStringList stores = ksm->keyStores();
87
QVERIFY( stores.contains( "qca-gnupg-(gpg)" ) );
89
QCA::KeyStore pgpStore( "qca-gnupg-(gpg)" );
90
QVERIFY( pgpStore.isValid() );
91
QCOMPARE( pgpStore.name(), QString( "GnuPG Keyring" ) );
92
QCOMPARE( pgpStore.type(), QCA::KeyStore::PGPKeyring );
93
QCOMPARE( pgpStore.id(), QString( "qca-gnupg-(gpg)" ) );
94
QEXPECT_FAIL( "", "writing entries isn't yet supported (TODO)", Continue );
95
QCOMPARE( pgpStore.isReadOnly(), false );
96
QCOMPARE( pgpStore.holdsTrustedCertificates(), false );
97
QCOMPARE( pgpStore.holdsIdentities(), true );
98
QCOMPARE( pgpStore.holdsPGPPublicKeys(), true );
102
#warning using setenv is dubious in terms of portability
104
QByteArray oldGNUPGHOME = qgetenv( "GNUPGHOME" );
105
// We test a small keyring - I downloaded a publically available one from
106
// the Amsterdam Internet Exchange.
107
if ( 0 == setenv( "GNUPGHOME", "./keys1", 1 ) )
109
QList<QCA::KeyStoreEntry> keylist = pgpStore.entryList();
110
QCOMPARE( keylist.count(), 6 );
111
QStringList nameList;
112
foreach( const QCA::KeyStoreEntry key, keylist ) {
113
QCOMPARE( key.isNull(), false );
114
QCOMPARE( key.type(), QCA::KeyStoreEntry::TypePGPPublicKey );
115
QCOMPARE( key.id().length(), 16 ); // 16 hex digits
116
QVERIFY( key.keyBundle().isNull() );
117
QVERIFY( key.certificate().isNull() );
118
QVERIFY( key.crl().isNull() );
119
QVERIFY( key.pgpSecretKey().isNull() );
120
QCOMPARE( key.pgpPublicKey().isNull(), false );
122
// We accumulate the names, and check them next
123
nameList << key.name();
125
QVERIFY( nameList.contains( "Steven Bakker <steven.bakker@ams-ix.net>" ) );
126
QVERIFY( nameList.contains( "Romeo Zwart <rz@ams-ix.net>" ) );
127
QVERIFY( nameList.contains( "Arien Vijn <arien.vijn@ams-ix.net>" ) );
128
QVERIFY( nameList.contains( "Niels Bakker <niels.bakker@ams-ix.net>" ) );
129
QVERIFY( nameList.contains( "Henk Steenman <Henk.Steenman@ams-ix.net>" ) );
130
QVERIFY( nameList.contains( "Geert Nijpels <geert.nijpels@ams-ix.net>" ) );
132
// TODO: We should test removeEntry() and writeEntry() here.
134
QFAIL( "Expected to be able to set the GNUPGHOME environment variable, but couldn't" );
137
// We now test an empty keyring
138
if ( 0 == setenv( "GNUPGHOME", "./keys2", 1 ) )
140
QList<QCA::KeyStoreEntry> keylist = pgpStore.entryList();
141
QCOMPARE( keylist.count(), 0 );
142
// TODO: We should test removeEntry() and writeEntry() here.
144
QFAIL( "Expected to be able to set the GNUPGHOME environment variable, but couldn't" );
147
if ( false == oldGNUPGHOME.isNull() )
149
setenv( "GNUPGHOME", oldGNUPGHOME.data(), 1 );
154
QTEST_MAIN(PgpUnitTest)
156
#include "pgpunittest.moc"