~ubuntu-branches/ubuntu/wily/qca2/wily-proposed

« back to all changes in this revision

Viewing changes to qca/unittest/pgpunittest/pgpunittest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2007-10-27 18:51:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20071027185154-4ir9ys3h2q9fofrw
Tags: 2.0.0-2
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Copyright (C)  2006  Brad Hards <bradh@frogmouth.net>
3
 
 *
4
 
 * Redistribution and use in source and binary forms, with or without
5
 
 * modification, are permitted provided that the following conditions
6
 
 * are met:
7
 
 *
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.
13
 
 *
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.
24
 
 */
25
 
 
26
 
#include <QtCrypto>
27
 
#include <QtTest/QtTest>
28
 
 
29
 
#include <stdlib.h>
30
 
 
31
 
#ifdef Q_OS_WIN
32
 
static int setenv(const char *name, const char *value, int overwrite)
33
 
{
34
 
    int i, iRet;
35
 
    char * a;
36
 
 
37
 
    if (!overwrite && getenv(name)) return 0;
38
 
 
39
 
    i = strlen(name) + strlen(value) + 2;
40
 
    a = (char*)malloc(i);
41
 
    if (!a) return 1;
42
 
 
43
 
    strcpy(a, name);
44
 
    strcat(a, "=");
45
 
    strcat(a, value);
46
 
 
47
 
    iRet = putenv(a);
48
 
    free(a);
49
 
    return iRet;
50
 
}
51
 
#endif
52
 
 
53
 
class PgpUnitTest : public QObject
54
 
{
55
 
    Q_OBJECT
56
 
 
57
 
private slots:
58
 
    void initTestCase();
59
 
    void cleanupTestCase();
60
 
    void testKeyRing();
61
 
private:
62
 
    QCA::Initializer* m_init;
63
 
};
64
 
 
65
 
void PgpUnitTest::initTestCase()
66
 
{
67
 
    m_init = new QCA::Initializer;
68
 
#include "../fixpaths.include"
69
 
}
70
 
 
71
 
void PgpUnitTest::cleanupTestCase()
72
 
{
73
 
    delete m_init;
74
 
}
75
 
 
76
 
void PgpUnitTest::testKeyRing()
77
 
{
78
 
    // activate the KeyStoreManager and block until ready
79
 
    QCA::keyStoreManager()->start();
80
 
    QCA::keyStoreManager()->waitForBusyFinished();
81
 
 
82
 
    if ( QCA::isSupported( QStringList( QString( "keystorelist" ) ),
83
 
                            QString( "qca-gnupg" ) ) )
84
 
    {
85
 
        QCA::KeyStoreManager *ksm = QCA::keyStoreManager();
86
 
        QStringList stores = ksm->keyStores();
87
 
        QVERIFY( stores.contains( "qca-gnupg-(gpg)" ) );
88
 
 
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 );
99
 
 
100
 
 
101
 
#ifdef __GNUC__
102
 
#warning using setenv is dubious in terms of portability
103
 
#endif
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 ) )
108
 
        {
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 );
121
 
 
122
 
                // We accumulate the names, and check them next
123
 
                nameList << key.name();
124
 
            }
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>" ) );
131
 
 
132
 
            // TODO: We should test removeEntry() and writeEntry() here.
133
 
        } else {
134
 
            QFAIL( "Expected to be able to set the GNUPGHOME environment variable, but couldn't" );
135
 
        }
136
 
 
137
 
        // We now test an empty keyring
138
 
        if ( 0 == setenv( "GNUPGHOME",  "./keys2", 1 ) )
139
 
        {
140
 
            QList<QCA::KeyStoreEntry> keylist = pgpStore.entryList();
141
 
            QCOMPARE( keylist.count(), 0 );
142
 
            // TODO: We should test removeEntry() and writeEntry() here.
143
 
        } else {
144
 
            QFAIL( "Expected to be able to set the GNUPGHOME environment variable, but couldn't" );
145
 
        }
146
 
 
147
 
        if ( false == oldGNUPGHOME.isNull() )
148
 
        {
149
 
            setenv( "GNUPGHOME",  oldGNUPGHOME.data(), 1 );
150
 
        }
151
 
    }
152
 
 
153
 
}
154
 
QTEST_MAIN(PgpUnitTest)
155
 
 
156
 
#include "pgpunittest.moc"