diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 (patch) | |
tree | 67208f7c145782a7e90b123b982ca78d88cc2c87 /certmanager/lib/tests | |
download | tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.tar.gz tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'certmanager/lib/tests')
-rw-r--r-- | certmanager/lib/tests/Makefile.am | 20 | ||||
-rw-r--r-- | certmanager/lib/tests/gnupg_home/pubring.gpg | bin | 0 -> 1233 bytes | |||
-rw-r--r-- | certmanager/lib/tests/gnupg_home/secring.gpg | bin | 0 -> 1381 bytes | |||
-rw-r--r-- | certmanager/lib/tests/gnupg_home/trustdb.gpg | bin | 0 -> 1280 bytes | |||
-rw-r--r-- | certmanager/lib/tests/gnupgviewer.h | 64 | ||||
-rw-r--r-- | certmanager/lib/tests/test.data | 295 | ||||
-rw-r--r-- | certmanager/lib/tests/test.data.gpg | bin | 0 -> 3010 bytes | |||
-rw-r--r-- | certmanager/lib/tests/test.data.sig | bin | 0 -> 65 bytes | |||
-rw-r--r-- | certmanager/lib/tests/test_cryptoconfig.cpp | 365 | ||||
-rw-r--r-- | certmanager/lib/tests/test_gnupgprocessbase.cpp | 143 | ||||
-rw-r--r-- | certmanager/lib/tests/test_jobs.cpp | 108 | ||||
-rw-r--r-- | certmanager/lib/tests/test_keygen.cpp | 160 | ||||
-rw-r--r-- | certmanager/lib/tests/test_keygen.h | 62 | ||||
-rw-r--r-- | certmanager/lib/tests/test_keylister.cpp | 146 | ||||
-rw-r--r-- | certmanager/lib/tests/test_keylister.h | 52 | ||||
-rw-r--r-- | certmanager/lib/tests/test_keyselectiondialog.cpp | 67 | ||||
-rw-r--r-- | certmanager/lib/tests/test_verify.cpp | 99 |
17 files changed, 1581 insertions, 0 deletions
diff --git a/certmanager/lib/tests/Makefile.am b/certmanager/lib/tests/Makefile.am new file mode 100644 index 000000000..3fa819f76 --- /dev/null +++ b/certmanager/lib/tests/Makefile.am @@ -0,0 +1,20 @@ +AM_CPPFLAGS = -I$(top_srcdir)/certmanager/lib \ + -I$(top_srcdir)/libkdenetwork \ + $(GPGME_CFLAGS) $(all_includes) -DKDESRCDIR=\"$(srcdir)\" + +check_PROGRAMS = test_keylister test_keygen test_keyselectiondialog \ + test_cryptoconfig test_gnupgprocessbase test_jobs test_verify +TESTS = test_verify + +test_keylister_SOURCES = test_keylister.cpp +test_keygen_SOURCES = test_keygen.cpp +test_keyselectiondialog_SOURCES = test_keyselectiondialog.cpp +test_cryptoconfig_SOURCES = test_cryptoconfig.cpp +test_gnupgprocessbase_SOURCES = test_gnupgprocessbase.cpp +test_jobs_SOURCES = test_jobs.cpp +test_verify_SOURCES = test_verify.cpp + +LDADD = ../libkleopatra.la + +METASOURCES = AUTO + diff --git a/certmanager/lib/tests/gnupg_home/pubring.gpg b/certmanager/lib/tests/gnupg_home/pubring.gpg Binary files differnew file mode 100644 index 000000000..5b9b35b1b --- /dev/null +++ b/certmanager/lib/tests/gnupg_home/pubring.gpg diff --git a/certmanager/lib/tests/gnupg_home/secring.gpg b/certmanager/lib/tests/gnupg_home/secring.gpg Binary files differnew file mode 100644 index 000000000..1556bbdac --- /dev/null +++ b/certmanager/lib/tests/gnupg_home/secring.gpg diff --git a/certmanager/lib/tests/gnupg_home/trustdb.gpg b/certmanager/lib/tests/gnupg_home/trustdb.gpg Binary files differnew file mode 100644 index 000000000..37cc64fd9 --- /dev/null +++ b/certmanager/lib/tests/gnupg_home/trustdb.gpg diff --git a/certmanager/lib/tests/gnupgviewer.h b/certmanager/lib/tests/gnupgviewer.h new file mode 100644 index 000000000..efba1760b --- /dev/null +++ b/certmanager/lib/tests/gnupgviewer.h @@ -0,0 +1,64 @@ +/* + gnupgviewer.h + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifndef __KLEO_TESTS_GNUPGVIEWER_H__ +#define __KLEO_TESTS_GNUPGVIEWER_H__ + +#include <qtextedit.h> +#include <qstring.h> + +namespace Kleo { + class GnuPGProcessBase; +} +class KProcess; +class QStringList; + +class GnuPGViewer : public QTextEdit { + Q_OBJECT +public: + GnuPGViewer( QWidget * parent=0, const char * name=0 ); + ~GnuPGViewer(); + + void setProcess( Kleo::GnuPGProcessBase * process ); + +private slots: + void slotStdout( KProcess *, char *, int ); + void slotStderr( KProcess *, char *, int ); + void slotStatus( Kleo::GnuPGProcessBase *, const QString &, const QStringList & ); + void slotProcessExited( KProcess * ); + +private: + Kleo::GnuPGProcessBase * mProcess; + QString mLastStdout, mLastStderr, mLastStatus; +}; + +#endif // __KLEO_TESTS_GNUPGVIEWER_H__ diff --git a/certmanager/lib/tests/test.data b/certmanager/lib/tests/test.data new file mode 100644 index 000000000..b4bce1a93 --- /dev/null +++ b/certmanager/lib/tests/test.data @@ -0,0 +1,295 @@ +/* -*- mode: c++; c-basic-offset:4 -*- + tests/test_uiserver.cpp + + This file is part of Kleopatra, the KDE keymanager + Copyright (c) 2007 Klarälvdalens Datakonsult AB + + Kleopatra is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + Kleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +// +// Usage: test_uiserver <socket> --verify-detached <signed data> <signature> +// +#ifndef _ASSUAN_ONLY_GPG_ERRORS +#define _ASSUAN_ONLY_GPG_ERRORS +#endif +#include "../uiserver/kleo-assuan.h" +#include <gpg-error.h> + +#include <QtCore> + +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <assert.h> + +#include <iostream> +#include <map> +#include <string> +#include <vector> + +using namespace Kleo; + +static std::vector<int> inFDs, outFDs, msgFDs; +static std::vector<std::string> inFiles, outFiles, msgFiles; +static std::map<std::string,std::string> inquireData; + +static std::string hexencode( const std::string & in ) { + std::string result; + result.reserve( 3 * in.size() ); + + static const char hex[] = "0123456789ABCDEF"; + + for ( std::string::const_iterator it = in.begin(), end = in.end() ; it != end ; ++it ) + switch ( const unsigned char ch = *it ) { + default: + if ( ch >= '!' && ch <= '~' || ch > 0xA0 ) { + result += ch; + break; + } + // else fall through + case ' ': + result += '+'; + break; + case '"': + case '#': + case '$': + case '%': + case '\'': + case '+': + case '=': + result += '%'; + result += hex[ (ch & 0xF0) >> 4 ]; + result += hex[ (ch & 0x0F) ]; + break; + } + + return result; +} + +static void usage( const std::string & msg=std::string() ) { + std::cerr << msg << std::endl << + "\n" + "Usage: test_uiserver <socket> [<io>] [<options>] [<inquire>] command [<args>]\n" + "where:\n" + " <io>: [--input[-fd] <file>] [--output[-fd] <file>] [--message[-fd] <file>]\n" + " <options>: *[--option name=value]\n" + " <inquire>: [--inquire keyword=<file>]\n"; + exit( 1 ); +} + +static int data( void * void_ctx, const void * buffer, size_t len ) { + (void)void_ctx; (void)buffer; (void)len; + return 0; // ### implement me +} + +static int status( void * void_ctx, const char * line ) { + (void)void_ctx; (void)line; + return 0; +} + +static int inquire( void * void_ctx, const char * keyword ) { + assuan_context_t ctx = (assuan_context_t)void_ctx; + assert( ctx ); + const std::map<std::string,std::string>::const_iterator it = inquireData.find( keyword ); + if ( it == inquireData.end() ) + return gpg_error( GPG_ERR_UNKNOWN_COMMAND ); + + if ( !it->second.empty() && it->second[0] == '@' ) + return gpg_error( GPG_ERR_NOT_IMPLEMENTED ); + + if ( const gpg_error_t err = assuan_send_data( ctx, it->second.c_str(), it->second.size() ) ) { + qDebug( "assuan_write_data: %s", gpg_strerror( err ) ); + return err; + } + + return 0; +} + +int main( int argc, char * argv[] ) { + + assuan_set_assuan_err_source( GPG_ERR_SOURCE_DEFAULT ); + + if ( argc < 3 ) + usage(); // need socket and command, at least + + const char * socket = argv[1]; + + std::vector<const char*> options; + + std::string command; + for ( int optind = 2 ; optind < argc ; ++optind ) { + const char * const arg = argv[optind]; + if ( qstrcmp( arg, "--input-fd" ) == 0 ) { + int inFD; + if ( (inFD = open( argv[++optind], O_RDONLY )) == -1 ) { + perror( "--input-fd open()" ); + return 1; + } + inFDs.push_back( inFD ); + } else if ( qstrcmp( arg, "--output-fd" ) == 0 ) { + int outFD; + if ( (outFD = open( argv[++optind], O_WRONLY|O_CREAT )) == -1 ) { + perror( "--output-fd open()" ); + return 1; + } + outFDs.push_back( outFD ); + } else if ( qstrcmp( arg, "--message-fd" ) == 0 ) { + int msgFD; + if ( (msgFD = open( argv[++optind], O_RDONLY )) == -1 ) { + perror( "--message-fd open()" ); + return 1; + } + msgFDs.push_back( msgFD ); + } else if ( qstrcmp( arg, "--input" ) == 0 ) { + const std::string file = argv[++optind]; + inFiles.push_back( file ); + } else if ( qstrcmp( arg, "--output" ) == 0 ) { + const std::string file = argv[++optind]; + outFiles.push_back( file ); + } else if ( qstrcmp( arg, "--message" ) == 0 ) { + const std::string file = argv[++optind]; + msgFiles.push_back( file ); + } else if ( qstrcmp( arg, "--option" ) == 0 ) { + options.push_back( argv[++optind] ); + } else if ( qstrcmp( arg, "--inquire" ) == 0 ) { + const std::string inqval = argv[++optind]; + const size_t pos = inqval.find( '=' ); + // ### implement indirection with "@file"... + inquireData[inqval.substr( 0, pos )] = inqval.substr( pos+1 ); + } else { + while ( optind < argc ) { + if ( !command.empty() ) + command += ' '; + command += argv[optind++]; + } + } + } + if ( command.empty() ) + usage( "Command expected, but only options found" ); + + assuan_context_t ctx = 0; + + if ( const gpg_error_t err = assuan_socket_connect_ext( &ctx, socket, -1, 1 ) ) { + qDebug( "%s", assuan_exception( err, "assuan_socket_connect_ext" ).what() ); + return 1; + } + + assuan_set_log_stream( ctx, stderr ); + + for ( std::vector<int>::const_iterator it = inFDs.begin(), end = inFDs.end() ; it != end ; ++it ) { + if ( const gpg_error_t err = assuan_sendfd( ctx, *it ) ) { + qDebug( "%s", assuan_exception( err, "assuan_sendfd( inFD )" ).what() ); + return 1; + } + + if ( const gpg_error_t err = assuan_transact( ctx, "INPUT FD", 0, 0, 0, 0, 0, 0 ) ) { + qDebug( "%s", assuan_exception( err, "INPUT FD" ).what() ); + return 1; + } + } + + + for ( std::vector<std::string>::const_iterator it = inFiles.begin(), end = inFiles.end() ; it != end ; ++it ) { + char buffer[1024]; + sprintf( buffer, "INPUT FILE=%s", hexencode( *it ).c_str() ); + + if ( const gpg_error_t err = assuan_transact( ctx, buffer, 0, 0, 0, 0, 0, 0 ) ) { + qDebug( "%s", assuan_exception( err, buffer ).what() ); + return 1; + } + } + + + for ( std::vector<int>::const_iterator it = msgFDs.begin(), end = msgFDs.end() ; it != end ; ++it ) { + if ( const gpg_error_t err = assuan_sendfd( ctx, *it ) ) { + qDebug( "%s", assuan_exception( err, "assuan_sendfd( msgFD )" ).what() ); + return 1; + } + + if ( const gpg_error_t err = assuan_transact( ctx, "MESSAGE FD", 0, 0, 0, 0, 0, 0 ) ) { + qDebug( "%s", assuan_exception( err, "MESSAGE FD" ).what() ); + return 1; + } + } + + + for ( std::vector<std::string>::const_iterator it = msgFiles.begin(), end = msgFiles.end() ; it != end ; ++it ) { + char buffer[1024]; + sprintf( buffer, "MESSAGE FILE=%s", hexencode( *it ).c_str() ); + + if ( const gpg_error_t err = assuan_transact( ctx, buffer, 0, 0, 0, 0, 0, 0 ) ) { + qDebug( "%s", assuan_exception( err, buffer ).what() ); + return 1; + } + } + + + for ( std::vector<int>::const_iterator it = outFDs.begin(), end = outFDs.end() ; it != end ; ++it ) { + if ( const gpg_error_t err = assuan_sendfd( ctx, *it ) ) { + qDebug( "%s", assuan_exception( err, "assuan_sendfd( outFD )" ).what() ); + return 1; + } + + if ( const gpg_error_t err = assuan_transact( ctx, "OUTPUT FD", 0, 0, 0, 0, 0, 0 ) ) { + qDebug( "%s", assuan_exception( err, "OUTPUT FD" ).what() ); + return 1; + } + } + + + for ( std::vector<std::string>::const_iterator it = outFiles.begin(), end = outFiles.end() ; it != end ; ++it ) { + char buffer[1024]; + sprintf( buffer, "OUTPUT FILE=%s", hexencode( *it ).c_str() ); + + if ( const gpg_error_t err = assuan_transact( ctx, buffer, 0, 0, 0, 0, 0, 0 ) ) { + qDebug( "%s", assuan_exception( err, buffer ).what() ); + return 1; + } + } + + + + Q_FOREACH( const char * opt, options ) { + std::string line = "OPTION "; + line += opt; + if ( const gpg_error_t err = assuan_transact( ctx, line.c_str(), 0, 0, 0, 0, 0, 0 ) ) { + qDebug( "%s", assuan_exception( err, line ).what() ); + return 1; + } + } + + if ( const gpg_error_t err = assuan_transact( ctx, command.c_str(), data, ctx, inquire, ctx, status, ctx ) ) { + qDebug( "%s", assuan_exception( err, command ).what() ); + return 1; + } + + assuan_disconnect( ctx ); + + return 0; +} diff --git a/certmanager/lib/tests/test.data.gpg b/certmanager/lib/tests/test.data.gpg Binary files differnew file mode 100644 index 000000000..2e1d7d161 --- /dev/null +++ b/certmanager/lib/tests/test.data.gpg diff --git a/certmanager/lib/tests/test.data.sig b/certmanager/lib/tests/test.data.sig Binary files differnew file mode 100644 index 000000000..4e2713752 --- /dev/null +++ b/certmanager/lib/tests/test.data.sig diff --git a/certmanager/lib/tests/test_cryptoconfig.cpp b/certmanager/lib/tests/test_cryptoconfig.cpp new file mode 100644 index 000000000..17d1b4471 --- /dev/null +++ b/certmanager/lib/tests/test_cryptoconfig.cpp @@ -0,0 +1,365 @@ +/* + test_cryptoconfig.cpp + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include <backends/qgpgme/qgpgmecryptoconfig.h> + +#include <kapplication.h> +#include <kaboutdata.h> +#include <kcmdlineargs.h> +#include <iostream> +using namespace std; + +#include <stdlib.h> +#include <assert.h> + +int main( int argc, char** argv ) { + + KAboutData aboutData( "test_cryptoconfig", "CryptoConfig Test", "0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app( false, false ); + + Kleo::CryptoConfig * config = new QGpgMECryptoConfig(); + + // Dynamic querying of the options + cout << "Components:" << endl; + QStringList components = config->componentList(); + + for( QStringList::Iterator compit = components.begin(); compit != components.end(); ++compit ) { + cout << "Component " << (*compit).local8Bit() << ":" << endl; + const Kleo::CryptoConfigComponent* comp = config->component( *compit ); + assert( comp ); + QStringList groups = comp->groupList(); + for( QStringList::Iterator groupit = groups.begin(); groupit != groups.end(); ++groupit ) { + const Kleo::CryptoConfigGroup* group = comp->group( *groupit ); + assert( group ); + cout << " Group " << (*groupit).local8Bit() << ": descr=\"" << group->description().local8Bit() << "\"" + << " level=" << group->level() << endl; + QStringList entries = group->entryList(); + for( QStringList::Iterator entryit = entries.begin(); entryit != entries.end(); ++entryit ) { + const Kleo::CryptoConfigEntry* entry = group->entry( *entryit ); + assert( entry ); + cout << " Entry " << (*entryit).local8Bit() << ":" + << " descr=\"" << entry->description().local8Bit() << "\"" + << " " << ( entry->isSet() ? "is set" : "is not set" ); + if ( !entry->isList() ) + switch( entry->argType() ) { + case Kleo::CryptoConfigEntry::ArgType_None: + break; + case Kleo::CryptoConfigEntry::ArgType_Int: + cout << " int value=" << entry->intValue(); + break; + case Kleo::CryptoConfigEntry::ArgType_UInt: + cout << " uint value=" << entry->uintValue(); + break; + case Kleo::CryptoConfigEntry::ArgType_LDAPURL: + case Kleo::CryptoConfigEntry::ArgType_URL: + cout << " URL value=" << entry->urlValue().prettyURL().local8Bit(); + // fallthrough + case Kleo::CryptoConfigEntry::ArgType_Path: + // fallthrough + case Kleo::CryptoConfigEntry::ArgType_DirPath: + // fallthrough + case Kleo::CryptoConfigEntry::ArgType_String: + + cout << " string value=" << entry->stringValue().local8Bit(); + break; + } + else // lists + { + switch( entry->argType() ) { + case Kleo::CryptoConfigEntry::ArgType_None: { + cout << " set " << entry->numberOfTimesSet() << " times"; + break; + } + case Kleo::CryptoConfigEntry::ArgType_Int: { + assert( entry->isOptional() ); // empty lists must be allowed (see issue121) + QValueList<int> lst = entry->intValueList(); + QString str; + for( QValueList<int>::Iterator it = lst.begin(); it != lst.end(); ++it ) { + str += QString::number( *it ); + } + cout << " int values=" << str.local8Bit(); + break; + } + case Kleo::CryptoConfigEntry::ArgType_UInt: { + assert( entry->isOptional() ); // empty lists must be allowed (see issue121) + QValueList<uint> lst = entry->uintValueList(); + QString str; + for( QValueList<uint>::Iterator it = lst.begin(); it != lst.end(); ++it ) { + str += QString::number( *it ); + } + cout << " uint values=" << str.local8Bit(); + break; + } + case Kleo::CryptoConfigEntry::ArgType_LDAPURL: + case Kleo::CryptoConfigEntry::ArgType_URL: { + assert( entry->isOptional() ); // empty lists must be allowed (see issue121) + KURL::List urls = entry->urlValueList(); + cout << " url values=" << urls.toStringList().join(" ").local8Bit() << "\n "; + } + // fallthrough + case Kleo::CryptoConfigEntry::ArgType_Path: + // fallthrough + case Kleo::CryptoConfigEntry::ArgType_DirPath: + // fallthrough + case Kleo::CryptoConfigEntry::ArgType_String: { + assert( entry->isOptional() ); // empty lists must be allowed (see issue121) + QStringList lst = entry->stringValueList(); + cout << " string values=" << lst.join(" ").local8Bit(); + break; + } + } + } + cout << endl; + } + // ... + } + } + + { + // Static querying of a single boolean option + static const char* s_groupName = "Monitor"; + static const char* s_entryName = "quiet"; + Kleo::CryptoConfigEntry* entry = config->entry( "dirmngr", s_groupName, s_entryName ); + if ( entry ) { + assert( entry->argType() == Kleo::CryptoConfigEntry::ArgType_None ); + bool val = entry->boolValue(); + cout << "quiet option initially: " << ( val ? "is set" : "is not set" ) << endl; + + entry->setBoolValue( !val ); + assert( entry->isDirty() ); + config->sync( true ); + + // Clear cached values! + config->clear(); + + // Check new value + Kleo::CryptoConfigEntry* entry = config->entry( "dirmngr", s_groupName, s_entryName ); + assert( entry ); + assert( entry->argType() == Kleo::CryptoConfigEntry::ArgType_None ); + cout << "quiet option now: " << ( val ? "is set" : "is not set" ) << endl; + assert( entry->boolValue() == !val ); + + // Set to default + entry->resetToDefault(); + assert( entry->boolValue() == false ); // that's the default + assert( entry->isDirty() ); + assert( !entry->isSet() ); + config->sync( true ); + config->clear(); + + // Check value + entry = config->entry( "dirmngr", s_groupName, s_entryName ); + assert( !entry->isDirty() ); + assert( !entry->isSet() ); + cout << "quiet option reset to default: " << ( entry->boolValue() ? "is set" : "is not set" ) << endl; + assert( entry->boolValue() == false ); + + // Reset old value + entry->setBoolValue( val ); + assert( entry->isDirty() ); + config->sync( true ); + + cout << "quiet option reset to initial: " << ( val ? "is set" : "is not set" ) << endl; + } + else + cout << "Entry 'dirmngr/" << s_groupName << "/" << s_entryName << "' not found" << endl; + } + + { + // Static querying and setting of a single int option + static const char* s_groupName = "LDAP"; + static const char* s_entryName = "ldaptimeout"; + Kleo::CryptoConfigEntry* entry = config->entry( "dirmngr", s_groupName, s_entryName ); + if ( entry ) { + assert( entry->argType() == Kleo::CryptoConfigEntry::ArgType_UInt ); + uint val = entry->uintValue(); + cout << "LDAP timeout initially: " << val << " seconds." << endl; + + // Test setting the option directly, then querying again + //system( "echo 'ldaptimeout:0:101' | gpgconf --change-options dirmngr" ); + // Now let's do it with the C++ API instead + entry->setUIntValue( 101 ); + assert( entry->isDirty() ); + config->sync( true ); + + // Clear cached values! + config->clear(); + + // Check new value + Kleo::CryptoConfigEntry* entry = config->entry( "dirmngr", s_groupName, s_entryName ); + assert( entry ); + assert( entry->argType() == Kleo::CryptoConfigEntry::ArgType_UInt ); + cout << "LDAP timeout now: " << entry->uintValue() << " seconds." << endl; + assert( entry->uintValue() == 101 ); + + // Set to default + entry->resetToDefault(); + assert( entry->uintValue() == 100 ); + assert( entry->isDirty() ); + assert( !entry->isSet() ); + config->sync( true ); + config->clear(); + + // Check value + entry = config->entry( "dirmngr", s_groupName, s_entryName ); + assert( !entry->isDirty() ); + assert( !entry->isSet() ); + cout << "LDAP timeout reset to default, " << entry->uintValue() << " seconds." << endl; + assert( entry->uintValue() == 100 ); + + // Reset old value + entry->setUIntValue( val ); + assert( entry->isDirty() ); + config->sync( true ); + + cout << "LDAP timeout reset to initial " << val << " seconds." << endl; + } + else + cout << "Entry 'dirmngr/" << s_groupName << "/" << s_entryName << "' not found" << endl; + } + + { + // Static querying and setting of a single string option + static const char* s_groupName = "Debug"; + static const char* s_entryName = "log-file"; + Kleo::CryptoConfigEntry* entry = config->entry( "dirmngr", s_groupName, s_entryName ); + if ( entry ) { + assert( entry->argType() == Kleo::CryptoConfigEntry::ArgType_Path ); + QString val = entry->stringValue(); + cout << "Log-file initially: " << val.local8Bit() << endl; + + // Test setting the option, sync'ing, then querying again + entry->setStringValue( QString::fromUtf8( "/tmp/test:%e5ä" ) ); + assert( entry->isDirty() ); + config->sync( true ); + + // Let's see how it prints it + system( "gpgconf --list-options dirmngr | grep log-file" ); + + // Clear cached values! + config->clear(); + + // Check new value + Kleo::CryptoConfigEntry* entry = config->entry( "dirmngr", s_groupName, s_entryName ); + assert( entry ); + assert( entry->argType() == Kleo::CryptoConfigEntry::ArgType_Path ); + cout << "Log-file now: " << entry->stringValue().local8Bit() << endl; + assert( entry->stringValue() == QString::fromUtf8( "/tmp/test:%e5ä" ) ); // (or even with %e5 decoded) + + // Reset old value +#if 0 + QString arg( val ); + if ( !arg.isEmpty() ) + arg.prepend( '"' ); + QCString sys; + sys.sprintf( "echo 'log-file:%s' | gpgconf --change-options dirmngr", arg.local8Bit().data() ); + system( sys.data() ); +#endif + entry->setStringValue( val ); + assert( entry->isDirty() ); + config->sync( true ); + + cout << "Log-file reset to initial " << val.local8Bit() << endl; + } + else + cout << "Entry 'dirmngr/" << s_groupName << "/" << s_entryName << "' not found" << endl; + } + + { + // Static querying and setting of the LDAP URL list option + static const char* s_groupName = "LDAP"; + static const char* s_entryName = "LDAP Server"; + Kleo::CryptoConfigEntry* entry = config->entry( "dirmngr", s_groupName, s_entryName ); + if ( entry ) { + assert( entry->argType() == Kleo::CryptoConfigEntry::ArgType_LDAPURL ); + assert( entry->isList() ); + KURL::List val = entry->urlValueList(); + cout << "URL list initially: " << val.toStringList().join(", ").local8Bit() << endl; + + // Test setting the option, sync'ing, then querying again + KURL::List lst; + // We use non-empty paths to workaround a bug in KURL (kdelibs-3.2) + lst << KURL( "ldap://a:389/?b" ); + // Test with query containing a litteral ':' (KURL supports this) + // and a ' ' (KURL will escape it, see issue119) + lst << KURL( "ldap://foo:389/?a:b c" ); + lst << KURL( "ldap://server:389/?a%3db,c=DE" ); // the query contains a litteral ',' + //cout << " trying to set: " << lst.toStringList().join(", ").local8Bit() << endl; + assert( lst[0].query() == "?b" ); + assert( lst[1].query() == "?a:b%20c" ); // see, the space got escaped + entry->setURLValueList( lst ); + assert( entry->isDirty() ); + config->sync( true ); + + // Let's see how it prints it + system( "gpgconf --list-options dirmngr | grep 'LDAP Server'" ); + + // Clear cached values! + config->clear(); + + // Check new value + Kleo::CryptoConfigEntry* entry = config->entry( "dirmngr", s_groupName, s_entryName ); + assert( entry ); + assert( entry->argType() == Kleo::CryptoConfigEntry::ArgType_LDAPURL ); + assert( entry->isList() ); + // Get raw a:b:c:d:e form + QStringList asStringList = entry->stringValueList(); + assert( asStringList.count() == 3 ); + cout << "asStringList[0]=" << asStringList[0].local8Bit() << endl; + cout << "asStringList[1]=" << asStringList[1].local8Bit() << endl; + cout << "asStringList[2]=" << asStringList[2].local8Bit() << endl; + assert( asStringList[0] == "a:389:::b" ); + assert( asStringList[1] == "foo:389:::a%3ab c" ); // the space must be decoded (issue119) + assert( asStringList[2] == "server:389:::a=b,c=DE" ); // all decoded + // Get KURL form + KURL::List newlst = entry->urlValueList(); + cout << "URL list now: " << newlst.toStringList().join(", ").local8Bit() << endl; + assert( newlst.count() == 3 ); + //cout << "newlst[0]=" << newlst[0].url().local8Bit() << endl; + //cout << "lst[0]=" << lst[0].url().local8Bit() << endl; + assert( newlst[0] == lst[0] ); + assert( newlst[1] == lst[1] ); + assert( newlst[2].url() == "ldap://server:389/?a=b,c=DE" ); // != lst[2] due to the encoded = + + // Reset old value + entry->setURLValueList( val ); + assert( entry->isDirty() ); + config->sync( true ); + + cout << "URL list reset to initial: " << val.toStringList().join(", ").local8Bit() << endl; + } + else + cout << "Entry 'dirmngr/" << s_groupName << "/" << s_entryName << "' not found" << endl; + } + + cout << "Done." << endl; +} diff --git a/certmanager/lib/tests/test_gnupgprocessbase.cpp b/certmanager/lib/tests/test_gnupgprocessbase.cpp new file mode 100644 index 000000000..37e961801 --- /dev/null +++ b/certmanager/lib/tests/test_gnupgprocessbase.cpp @@ -0,0 +1,143 @@ +/* + gnupgviewer.cpp + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "gnupgviewer.h" + +#include <backends/qgpgme/gnupgprocessbase.h> + +#include <kapplication.h> +#include <kaboutdata.h> +#include <kcmdlineargs.h> +#include <kmessagebox.h> +#include <kdebug.h> + +#include <qstringlist.h> + +GnuPGViewer::GnuPGViewer( QWidget * parent, const char * name ) + : QTextEdit( parent, name ), mProcess( 0 ) +{ + setTextFormat( LogText ); + setMaxLogLines( 10000 ); +} + +GnuPGViewer::~GnuPGViewer() { + if ( mProcess ) + mProcess->kill(); +} + +void GnuPGViewer::setProcess( Kleo::GnuPGProcessBase * process ) { + if ( !process ) + return; + mProcess = process; + connect( mProcess, SIGNAL(processExited(KProcess*)), + SLOT(slotProcessExited(KProcess*)) ); + connect( mProcess, SIGNAL(receivedStdout(KProcess*,char*,int)), + SLOT(slotStdout(KProcess*,char*,int)) ); + connect( mProcess, SIGNAL(receivedStderr(KProcess*,char*,int)), + SLOT(slotStderr(KProcess*,char*,int)) ); + connect( mProcess, SIGNAL(status(Kleo::GnuPGProcessBase*,const QString&,const QStringList&)), + SLOT(slotStatus(Kleo::GnuPGProcessBase*,const QString&,const QStringList&)) ); +} + +static QStringList split( char * buffer, int buflen, QString & old ) { + // when done right, this would need to use QTextCodec... + const QString str = old + QString::fromLocal8Bit( buffer, buflen ); + QStringList l = QStringList::split( '\n', str, true ); + if ( l.empty() ) + return l; + if ( str.endsWith( "\n" ) ) { + old = QString::null; + } else { + old = l.back(); + l.pop_back(); + } + return l; +} + +static QString escape( QString str ) { + return str.replace( '&', "&" ).replace( '<', "<" ).replace( '>', ">" ); +} + +void GnuPGViewer::slotStdout( KProcess *, char * buffer, int buflen ) { + const QStringList l = split( buffer, buflen, mLastStdout ); + for ( QStringList::const_iterator it = l.begin() ; it != l.end() ; ++it ) + append( "stdout: " + escape( *it ) ); +} + +void GnuPGViewer::slotStderr( KProcess *, char * buffer, int buflen ) { + const QStringList l = split( buffer, buflen, mLastStderr ); + for ( QStringList::const_iterator it = l.begin() ; it != l.end() ; ++it ) + append( "<b>stderr: " + escape( *it ) + "</b>" ); +} +void GnuPGViewer::slotStatus( Kleo::GnuPGProcessBase *, const QString & type, const QStringList & args ) { + append( "<b><font color=\"red\">status: " + escape( type + ' ' + args.join( " " ) ) + "</font></b>" ); +} +void GnuPGViewer::slotProcessExited( KProcess * proc ) { + if ( !proc ) + return; + if ( proc->normalExit() ) + append( QString( "<b>Process exit: return code %1</b>" ).arg ( proc->exitStatus() ) ); + else + append( "<b>Process exit: killed</b>" ); +} + +int main( int argc, char** argv ) { + if ( argc < 3 ) { + kdDebug() << "Need at least two arguments" << endl; + return 1; + } + KAboutData aboutData( "test_gnupgprocessbase", "GnuPGProcessBase Test", "0.1" ); + KCmdLineArgs::init( &aboutData ); + KApplication app; + + Kleo::GnuPGProcessBase gpg; + for ( int i = 1 ; i < argc ; ++i ) + gpg << argv[i]; + + gpg.setUseStatusFD( true ); + + GnuPGViewer * gv = new GnuPGViewer(); + gv->setProcess( &gpg ); + + app.setMainWidget( gv ); + gv->show(); + + gpg.start( KProcess::NotifyOnExit, KProcess::AllOutput ); + + return app.exec(); +} + +#include "gnupgviewer.moc" diff --git a/certmanager/lib/tests/test_jobs.cpp b/certmanager/lib/tests/test_jobs.cpp new file mode 100644 index 000000000..22d9136d4 --- /dev/null +++ b/certmanager/lib/tests/test_jobs.cpp @@ -0,0 +1,108 @@ +/* + test_jobs.cpp + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <kleo/cryptobackendfactory.h> +#include <kleo/signjob.h> +#include <kleo/keylistjob.h> + +#include <gpgmepp/key.h> +#include <gpgmepp/signingresult.h> +#include <gpgmepp/keylistresult.h> + +#include <kdebug.h> +#include <assert.h> +#include <kaboutdata.h> +#include <kcmdlineargs.h> +#include <kapplication.h> + +#include <memory> + +static const char * protocol = 0; + +static void testSign() +{ + const Kleo::CryptoBackend::Protocol * proto = protocol == "openpgp" ? Kleo::CryptoBackendFactory::instance()->openpgp() : Kleo::CryptoBackendFactory::instance()->smime() ; + assert( proto ); + + kdDebug() << "Using protocol " << proto->name() << endl; + + + std::vector<GpgME::Key> signingKeys; + + std::auto_ptr<Kleo::KeyListJob> listJob( proto->keyListJob( false, false, true ) ); // use validating keylisting + if ( listJob.get() ) { + // ##### Adjust this to your own identity + listJob->exec( "faure@kde.org", true /*secret*/, signingKeys ); + assert( !signingKeys.empty() ); + } else { + assert( 0 ); // job failed + } + + Kleo::SignJob* job = proto->signJob( true, true ); + + QCString cText = "Hallo Leute\n"; // like gpgme's t-sign.c + QByteArray plainText; + plainText.duplicate( cText.data(), cText.length() ); // hrmpf... + kdDebug() << k_funcinfo << "plainText=" << cText.data() << endl; + + kdDebug() << k_funcinfo << " signing with " << signingKeys[0].primaryFingerprint() << endl; + + QByteArray signature; + const GpgME::SigningResult res = + job->exec( signingKeys, plainText, GpgME::Context::Clearsigned, signature ); + if ( res.error().isCanceled() ) { + kdDebug() << "signing was canceled by user" << endl; + return; + } + if ( res.error() ) { + kdDebug() << "signing failed: " << res.error().asString() << endl; + return; + } + kdDebug() << k_funcinfo << "signing resulted in signature=" + << QCString( signature.data(), signature.size() + 1 ) << endl; +} + +int main( int argc, char** argv ) { + protocol = "openpgp"; + if ( argc == 2 ) { + protocol = argv[1]; + argc = 1; // hide from KDE + } + KAboutData aboutData( "test_jobs", "Signing Job Test", "0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app; + + testSign(); +} diff --git a/certmanager/lib/tests/test_keygen.cpp b/certmanager/lib/tests/test_keygen.cpp new file mode 100644 index 000000000..9303b331c --- /dev/null +++ b/certmanager/lib/tests/test_keygen.cpp @@ -0,0 +1,160 @@ +/* + test_keygen.cpp + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "test_keygen.h" + +#include <kleo/keylistjob.h> +#include <kleo/keygenerationjob.h> +#include <kleo/cryptobackendfactory.h> + +#include <ui/progressdialog.h> + +#include <gpgmepp/keygenerationresult.h> + +#include <kapplication.h> +#include <kaboutdata.h> +#include <kcmdlineargs.h> +#include <kmessagebox.h> +#include <kdebug.h> + +#include <qmessagebox.h> +#include <qstringlist.h> +#include <qtimer.h> +#include <qlayout.h> +#include <qlineedit.h> +#include <qlabel.h> + +#include <assert.h> + +static const char * keyParams[] = { + "Key-Type", "Key-Length", + "Subkey-Type", "Subkey-Length", + "Name-Real", "Name-Comment", "Name-Email", "Name-DN", + "Expire-Date", + "Passphrase" +}; +static const int numKeyParams = sizeof keyParams / sizeof *keyParams; + +static const char * protocol = 0; + +KeyGenerator::KeyGenerator( QWidget * parent, const char * name, WFlags ) + : KDialogBase( parent, name, true, "KeyGenerationJob test", + Close|User1, User1, true, KGuiItem( "Create" ) ) +{ + QWidget * w = new QWidget( this ); + setMainWidget( w ); + + QGridLayout * glay = new QGridLayout( w, numKeyParams+3, 2, marginHint(), spacingHint() ); + + int row = -1; + + ++row; + glay->addMultiCellWidget( new QLabel( "<GnupgKeyParms format=\"internal\">", w ), + row, row, 0, 1 ); + for ( int i = 0 ; i < numKeyParams ; ++i ) { + ++row; + glay->addWidget( new QLabel( keyParams[i], w ), row, 0 ); + glay->addWidget( mLineEdits[i] = new QLineEdit( w ), row, 1 ); + } + + ++row; + glay->addMultiCellWidget( new QLabel( "</GnupgKeyParms>", w ), + row, row, 0, 1 ); + ++row; + glay->setRowStretch( row, 1 ); + glay->setColStretch( 1, 1 ); + + connect( this, SIGNAL(user1Clicked()), SLOT(slotStartKeyGeneration()) ); +} + +KeyGenerator::~KeyGenerator() {} + +void KeyGenerator::slotStartKeyGeneration() { + QString params = "<GnupgKeyParms format=\"internal\">\n"; + for ( int i = 0 ; i < numKeyParams ; ++i ) + if ( mLineEdits[i] && !mLineEdits[i]->text().stripWhiteSpace().isEmpty() ) + params += keyParams[i] + ( ": " + mLineEdits[i]->text().stripWhiteSpace() ) + '\n'; + params += "</GnupgKeyParms>\n"; + + const Kleo::CryptoBackend::Protocol * proto = protocol == "openpgp" ? Kleo::CryptoBackendFactory::instance()->openpgp() : Kleo::CryptoBackendFactory::instance()->smime() ; + if ( !proto ) + proto = Kleo::CryptoBackendFactory::instance()->smime(); + assert( proto ); + + kdDebug() << "Using protocol " << proto->name() << endl; + + Kleo::KeyGenerationJob * job = proto->keyGenerationJob(); + assert( job ); + + connect( job, SIGNAL(result(const GpgME::KeyGenerationResult&,const QByteArray&)), + SLOT(slotResult(const GpgME::KeyGenerationResult&,const QByteArray&)) ); + + const GpgME::Error err = job->start( params ); + if ( err ) + showError( err ); + else + (void)new Kleo::ProgressDialog( job, "Generating key", this ); +} + +void KeyGenerator::showError( const GpgME::Error & err ) { + KMessageBox::error( this, "Could not start key generation: " + QString::fromLocal8Bit( err.asString() ), + "Key Generation Error" ); +} + +void KeyGenerator::slotResult( const GpgME::KeyGenerationResult & res, const QByteArray & keyData ) { + if ( res.error() ) + showError( res.error() ); + else + KMessageBox::information( this, QString("Key generated successfully, %1 bytes long").arg( keyData.size() ), + "Key Generation Finished" ); +} + +int main( int argc, char** argv ) { + if ( argc == 2 ) { + protocol = argv[1]; + argc = 1; // hide from KDE + } + KAboutData aboutData( "test_keygen", "KeyGenerationJob Test", "0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app; + + KeyGenerator * keygen = new KeyGenerator( 0, "KeyGenerator top-level" ); + app.setMainWidget( keygen ); + keygen->show(); + + return app.exec(); +} + +#include "test_keygen.moc" diff --git a/certmanager/lib/tests/test_keygen.h b/certmanager/lib/tests/test_keygen.h new file mode 100644 index 000000000..0ba713823 --- /dev/null +++ b/certmanager/lib/tests/test_keygen.h @@ -0,0 +1,62 @@ +/* + test_keygen.h + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifndef __KLEO_TEST_KEYGEN_H__ +#define __KLEO_TEST_KEYGEN_H__ + +#include <kdialogbase.h> + +#include <qcstring.h> + +namespace GpgME { + class Error; + class KeyGenerationResult; +} + +class QLineEdit; + +class KeyGenerator : public KDialogBase { + Q_OBJECT +public: + KeyGenerator( QWidget * parent=0, const char * name=0, WFlags f=0 ); + ~KeyGenerator(); + +public slots: + void slotStartKeyGeneration(); + void slotResult( const GpgME::KeyGenerationResult & res, const QByteArray & keyData ); +private: + void showError( const GpgME::Error & err ); + +private: + QLineEdit * mLineEdits[20]; +}; + +#endif // __KLEO_TEST_KEYGEN_H__ diff --git a/certmanager/lib/tests/test_keylister.cpp b/certmanager/lib/tests/test_keylister.cpp new file mode 100644 index 000000000..9507221e7 --- /dev/null +++ b/certmanager/lib/tests/test_keylister.cpp @@ -0,0 +1,146 @@ +/* + test_keylister.cpp + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include <config.h> + +#include "test_keylister.h" + +#include <kleo/keylistjob.h> +#include <cryptplugwrapper.h> +#include <kleo/cryptobackendfactory.h> + +#include <gpgmepp/keylistresult.h> +#include <gpgmepp/key.h> + +#include <kapplication.h> +#include <kaboutdata.h> +#include <kcmdlineargs.h> +#include <kdebug.h> + +#include <qmessagebox.h> +#include <qstringlist.h> +#include <qtimer.h> + +#include <assert.h> + +namespace { + class TestColumnStrategy : public Kleo::KeyListView::ColumnStrategy { + public: + ~TestColumnStrategy() {} + QString title( int col ) const; + QString toolTip( const GpgME::Key & key, int col ) const; + QString text( const GpgME::Key & key, int col ) const; + }; + + QString TestColumnStrategy::title( int col ) const { + switch ( col ) { + case 0: return "Subject"; + case 1: return "EMail"; + case 2: return "Issuer"; + case 3: return "Serial"; + case 4: return "Protocol"; + case 5: return "Validity"; + default: return QString::null; + } + } + + QString TestColumnStrategy::toolTip( const GpgME::Key & key, int ) const { + return "Fingerprint: " + QString::fromUtf8( key.primaryFingerprint() ); + } + + QString TestColumnStrategy::text( const GpgME::Key & key, int col ) const { + if ( key.isNull() ) + return "<null>"; + switch ( col ) { + case 0: return QString::fromUtf8( key.userID(0).id() ); + case 1: return QString::fromUtf8( key.userID(0).email() ); + case 2: return QString::fromUtf8( key.issuerName() ); + case 3: return key.issuerSerial(); + case 4: return key.protocolAsString(); + case 5: return QChar( key.userID(0).validityAsString() ); + default: return QString::null; + } + } +} + +CertListView::CertListView( QWidget * parent, const char * name, WFlags f ) + : Kleo::KeyListView( new TestColumnStrategy(), 0, parent, name, f ) +{ + setHierarchical( true ); + setRootIsDecorated( true ); +} + +void CertListView::slotResult( const GpgME::KeyListResult & result ) { + kdDebug() << "CertListView::slotResult()" << endl; + if ( result.isNull() ) + QMessageBox::information( this, "Key Listing Result", "KeyListResult is null!" ); + else if ( result.error() ) + QMessageBox::critical( this, "Key Listing Result", + QString("KeyListResult Error: %1").arg( result.error().asString() ) ); + else if ( result.isTruncated() ) + QMessageBox::information( this, "Key Listing Result", "KeyListResult is truncated!" ); + else + QMessageBox::information( this, "Key Listing Result", "Key listing successful" ); +} + +void CertListView::slotStart() { + kdDebug() << "CertListView::slotStart()" << endl; + Kleo::KeyListJob * job = Kleo::CryptoBackendFactory::instance()->smime()->keyListJob( false ); + assert( job ); + QObject::connect( job, SIGNAL(nextKey(const GpgME::Key&)), + this, SLOT(slotAddKey(const GpgME::Key&)) ); + QObject::connect( job, SIGNAL(result(const GpgME::KeyListResult&)), + this, SLOT(slotResult(const GpgME::KeyListResult&)) ); +#if 0 + QStringList l; + l << "Marc"; + job->start( l, false ); +#else + job->start( QStringList(), false ); +#endif +} + +int main( int argc, char** argv ) { + + KAboutData aboutData( "test_keylister", "KeyLister Test", "0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app; + + CertListView * clv = new CertListView( 0, "CertListView top-level" ); + app.setMainWidget( clv ); + clv->show(); + + QTimer::singleShot( 5000, clv, SLOT(slotStart()) ); + + return app.exec(); +} + +#include "test_keylister.moc" diff --git a/certmanager/lib/tests/test_keylister.h b/certmanager/lib/tests/test_keylister.h new file mode 100644 index 000000000..348ec262f --- /dev/null +++ b/certmanager/lib/tests/test_keylister.h @@ -0,0 +1,52 @@ +/* + test_keylister.h + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifndef __KLEO_TEST_KEYLISTER_H__ +#define __KLEO_TEST_KEYLISTER_H__ + +#include <ui/keylistview.h> + +namespace GpgME { + class Key; + class KeyListResult; +} + +class CertListView : public Kleo::KeyListView { + Q_OBJECT +public: + CertListView( QWidget * parent=0, const char * name=0, WFlags f=0 ); + +public slots: + void slotResult( const GpgME::KeyListResult & result ); + void slotStart(); +}; + +#endif // __KLEO_TEST_KEYLISTER_H__ diff --git a/certmanager/lib/tests/test_keyselectiondialog.cpp b/certmanager/lib/tests/test_keyselectiondialog.cpp new file mode 100644 index 000000000..e6c85fca0 --- /dev/null +++ b/certmanager/lib/tests/test_keyselectiondialog.cpp @@ -0,0 +1,67 @@ +/* + test_keygen.cpp + + This file is part of libkleopatra's test suite. + Copyright (c) 2004 Klarälvdalens Datakonsult AB + + Libkleopatra is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + Libkleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include <config.h> + +#include <ui/keyselectiondialog.h> +#include <gpgmepp/key.h> + +#include <kapplication.h> +#include <kcmdlineargs.h> +#include <kaboutdata.h> +#include <kglobal.h> +#include <kiconloader.h> +#include <kdebug.h> + +#include <vector> + +int main( int argc, char ** argv ) { + KAboutData aboutData( "test_keyselectiondialog", "KeySelectionDialog Test", "0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app; + + KGlobal::iconLoader()->addAppDir( "libkleopatra" ); + + Kleo::KeySelectionDialog dlg( "Kleo::KeySelectionDialog Test", + "Please select a key:", + std::vector<GpgME::Key>(), + Kleo::KeySelectionDialog::AllKeys, true, true ); + + if ( dlg.exec() == QDialog::Accepted ) { + kdDebug() << "accepted; selected key: " << (dlg.selectedKey().userID(0).id() ? dlg.selectedKey().userID(0).id() : "<null>") << "\nselected _keys_:" << endl; + for ( std::vector<GpgME::Key>::const_iterator it = dlg.selectedKeys().begin() ; it != dlg.selectedKeys().end() ; ++it ) + kdDebug() << (it->userID(0).id() ? it->userID(0).id() : "<null>") << endl; + } else { + kdDebug() << "rejected" << endl; + } + + return 0; +} diff --git a/certmanager/lib/tests/test_verify.cpp b/certmanager/lib/tests/test_verify.cpp new file mode 100644 index 000000000..a81e50466 --- /dev/null +++ b/certmanager/lib/tests/test_verify.cpp @@ -0,0 +1,99 @@ +/* + This file is part of Kleopatra's test suite. + Copyright (c) 2007 Klarälvdalens Datakonsult AB + + Kleopatra is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + Kleopatra is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include <config.h> + +#include <kleo/cryptobackendfactory.h> +#include <kleo/verifydetachedjob.h> +#include <kleo/verifyopaquejob.h> + +#include <gpgmepp/verificationresult.h> +#include <gpgmepp/key.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kcmdlineargs.h> + +#include <qdir.h> +#include <qfile.h> + +#include <assert.h> + +int main( int argc, char **argv ) +{ + setenv("GNUPGHOME", KDESRCDIR "/gnupg_home", 1 ); + setenv("LC_ALL", "C", 1); + setenv("KDEHOME", QFile::encodeName( QDir::homeDirPath() + "/.kde-unit-test" ), 1); + + KAboutData aboutData( "test_verify", "verify job test", "0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app( false, false ); + + const QString sigFileName = KDESRCDIR "/test.data.sig"; + const QString dataFileName = KDESRCDIR "/test.data"; + + QFile sigFile( sigFileName ); + assert( sigFile.open( IO_ReadOnly ) ); + QFile dataFile( dataFileName ); + assert( dataFile.open( IO_ReadOnly ) ); + + const Kleo::CryptoBackend::Protocol * const backend = Kleo::CryptoBackendFactory::instance()->protocol( "openpgp" ); + + Kleo::VerifyDetachedJob *job = backend->verifyDetachedJob(); + GpgME::VerificationResult result = job->exec( sigFile.readAll(), dataFile.readAll() ); + assert( !result.error() ); + assert( result.signatures().size() == 1 ); + + GpgME::Signature sig = result.signature( 0 ); + assert( (sig.summary() & GpgME::Signature::KeyMissing) == 0 ); + assert( sig.creationTime() == 1189650248L ); + assert( sig.validity() == GpgME::Signature::Full ); + + const QString opaqueFileName = KDESRCDIR "/test.data.gpg"; + QFile opaqueFile( opaqueFileName ); + assert( opaqueFile.open( IO_ReadOnly ) ); + QByteArray clearText; + + Kleo::VerifyOpaqueJob *job2 = backend->verifyOpaqueJob(); + result = job2->exec( opaqueFile.readAll(), clearText ); + assert( !result.error() ); + assert( result.signatures().size() == 1 ); + + sig = result.signature( 0 ); + assert( (sig.summary() & GpgME::Signature::KeyMissing) == 0 ); + assert( (sig.summary() & GpgME::Signature::Green ) ); + assert( sig.creationTime() > 0 ); + assert( sig.validity() == GpgME::Signature::Full ); + + dataFile.reset(); + assert( clearText == dataFile.readAll() ); + + return 0; +} |