mailtransport
servertest.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "servertest.h"
00023 #include "socket.h"
00024
00025 #include <mailtransport/transportbase.h>
00026 #include <mailtransport/mailtransport_defs.h>
00027
00028
00029 #include <QHostInfo>
00030 #include <QProgressBar>
00031 #include <QTimer>
00032
00033
00034 #include <klocale.h>
00035 #include <kdebug.h>
00036
00037 using namespace MailTransport;
00038
00039 namespace MailTransport
00040 {
00041
00042 class ServerTestPrivate
00043 {
00044 public:
00045 ServerTestPrivate( ServerTest* test);
00046
00047 ServerTest* const q;
00048 QString server;
00049 QString fakeHostname;
00050 QString testProtocol;
00051
00052 MailTransport::Socket* normalSocket;
00053 MailTransport::Socket* secureSocket;
00054
00055 QList< int > connectionResults;
00056 QHash< int, QList<int> > authenticationResults;
00057 QTimer* normalSocketTimer;
00058 QTimer* secureSocketTimer;
00059 QTimer* progressTimer;
00060
00061 QProgressBar* testProgress;
00062
00063 bool secureSocketFinished;
00064 bool normalSocketFinished;
00065
00066 void finalResult();
00067 void read( int type, const QString& text );
00068
00069
00070 void slotNormalPossible();
00071 void slotNormalNotPossible();
00072 void slotSslPossible();
00073 void slotSslNotPossible();
00074 void slotReadNormal( const QString& text );
00075 void slotReadSecure( const QString& text );
00076 void slotUpdateProgress();
00077 };
00078
00079 }
00080
00081 ServerTestPrivate::ServerTestPrivate( ServerTest* test)
00082 : q( test )
00083 {
00084 }
00085
00086 void ServerTestPrivate::finalResult()
00087 {
00088 if ( !secureSocketFinished || !normalSocketFinished )
00089 return;
00090
00091 kDebug( 5324 ) << connectionResults;
00092
00093 testProgress->hide();
00094 progressTimer->stop();
00095
00096 emit q->finished( connectionResults );
00097 }
00098
00099 void ServerTestPrivate::read( int type, const QString& text )
00100 {
00101 kDebug( 5324 ) << text;
00102
00103 if ( !text.contains( QLatin1String( "AUTH" ), Qt::CaseInsensitive ) )
00104 return;
00105
00106 QStringList protocols;
00107 protocols << QLatin1String( "LOGIN" ) << QLatin1String( "PLAIN" )
00108 << QLatin1String( "CRAM-MD5" ) << QLatin1String("DIGEST-MD5")
00109 << QLatin1String( "NTLM" ) << QLatin1String( "GSSAPI" );
00110
00111 QStringList results;
00112 for ( int i = 0 ; i < protocols.count(); ++i ) {
00113 if ( text.contains( protocols.at( i ), Qt::CaseInsensitive ) )
00114 results.append( protocols.at( i ) );
00115 }
00116
00117 QList<int> result;
00118 for ( QStringList::ConstIterator it = results.begin() ;
00119 it != results.end() ; ++it ) {
00120 if ( *it == QLatin1String("LOGIN") )
00121 result << Transport::EnumAuthenticationType::LOGIN;
00122 else if ( *it == QLatin1String("PLAIN") )
00123 result << Transport::EnumAuthenticationType::PLAIN;
00124 else if ( *it == QLatin1String("CRAM-MD5") )
00125 result << Transport::EnumAuthenticationType::CRAM_MD5;
00126 else if ( *it == QLatin1String("DIGEST-MD5") )
00127 result << Transport::EnumAuthenticationType::DIGEST_MD5;
00128 else if ( *it == QLatin1String("NTLM") )
00129 result << Transport::EnumAuthenticationType::NTLM;
00130 else if ( *it == QLatin1String("GSSAPI") )
00131 result << Transport::EnumAuthenticationType::GSSAPI;
00132 }
00133
00134
00135
00136
00137 if ( result.contains( Transport::EnumAuthenticationType::PLAIN ) )
00138 result.removeAll( Transport::EnumAuthenticationType::LOGIN );
00139
00140 authenticationResults[type] = result;
00141 }
00142
00143 void ServerTestPrivate::slotNormalPossible()
00144 {
00145 normalSocketTimer->stop();
00146 connectionResults << Transport::EnumEncryption::None;
00147 }
00148
00149 void ServerTestPrivate::slotReadNormal( const QString& text )
00150 {
00151 static bool first = true;
00152 if ( first ) {
00153
00154 if ( testProtocol == IMAP_PROTOCOL )
00155 normalSocket->write( QLatin1String( "1 CAPABILITY" ) );
00156
00157 else if ( testProtocol == SMTP_PROTOCOL ) {
00158
00159
00160
00161
00162
00163 QString hostname;
00164 if ( !fakeHostname.isNull() ) {
00165 hostname = fakeHostname;
00166 }
00167 else {
00168 hostname = QHostInfo::localHostName();
00169 if( hostname.isEmpty() ) {
00170 hostname = QLatin1String( "localhost.invalid" );
00171 }
00172 else if ( !hostname.contains( QChar::fromAscii( '.' ) ) ) {
00173 hostname += QLatin1String( ".localnet" );
00174 }
00175 }
00176 kDebug( 5324 ) << "Hostname for EHLO is" << hostname;
00177
00178 normalSocket->write( QLatin1String( "EHLO " ) + hostname );
00179 }
00180 first = false;
00181 return;
00182 }
00183
00184 if ( text.contains( QLatin1String("STARTTLS" ), Qt::CaseInsensitive) )
00185 connectionResults << Transport::EnumEncryption::TLS;
00186
00187 read( Transport::EnumEncryption::None, text );
00188
00189 normalSocketFinished = true;
00190 first = true;
00191 finalResult();
00192 }
00193
00194 void ServerTestPrivate::slotReadSecure( const QString& text )
00195 {
00196 static bool first = true;
00197 if ( first ) {
00198 if ( testProtocol == IMAP_PROTOCOL )
00199 secureSocket->write( QLatin1String( "1 CAPABILITY" ) );
00200 else if ( testProtocol == SMTP_PROTOCOL )
00201 secureSocket->write( QLatin1String( "EHLO localhost" ) );
00202 first = false;
00203 return;
00204 }
00205
00206 read( Transport::EnumEncryption::SSL, text );
00207
00208 secureSocketFinished = true;
00209 first = true;
00210 finalResult();
00211 }
00212
00213 void ServerTestPrivate::slotNormalNotPossible()
00214 {
00215 normalSocketFinished = true;
00216 finalResult();
00217 }
00218
00219 void ServerTestPrivate::slotSslPossible()
00220 {
00221 secureSocketTimer->stop();
00222 connectionResults << Transport::EnumEncryption::SSL;
00223 }
00224
00225 void ServerTestPrivate::slotSslNotPossible()
00226 {
00227 secureSocketFinished = true;
00228 finalResult();
00229 }
00230
00231 void ServerTestPrivate::slotUpdateProgress()
00232 {
00233 testProgress->setValue( testProgress->value() + 1 );
00234 }
00235
00236
00237
00238 ServerTest::ServerTest( QWidget* parent )
00239 : QWidget( parent ), d( new ServerTestPrivate( this ) )
00240 {
00241 d->normalSocketTimer = new QTimer( this );
00242 d->normalSocketTimer->setSingleShot( true );
00243 connect( d->normalSocketTimer, SIGNAL( timeout() ), SLOT( slotNormalNotPossible() ) );
00244
00245 d->secureSocketTimer = new QTimer( this );
00246 d->secureSocketTimer->setSingleShot( true );
00247 connect( d->secureSocketTimer, SIGNAL( timeout() ), SLOT( slotSslNotPossible() ) );
00248
00249 d->progressTimer = new QTimer( this );
00250 connect( d->progressTimer, SIGNAL( timeout() ), SLOT( slotUpdateProgress() ) );
00251 }
00252
00253 ServerTest::~ServerTest()
00254 {
00255 delete d;
00256 }
00257
00258 void ServerTest::start()
00259 {
00260 kDebug( 5324 ) << d;
00261
00262 d->connectionResults.clear();
00263 d->authenticationResults.clear();
00264
00265 d->testProgress->setMaximum( 20 );
00266 d->testProgress->setValue( 0 );
00267 d->testProgress->setTextVisible( true );
00268 d->testProgress->show();
00269 d->progressTimer->start( 1000 );
00270
00271 d->normalSocket = new MailTransport::Socket( this );
00272 d->normalSocket->setObjectName( QLatin1String( "normal" ) );
00273 d->normalSocket->setServer( d->server );
00274 d->normalSocket->setProtocol( d->testProtocol );
00275 if ( d->testProtocol == IMAP_PROTOCOL )
00276 d->normalSocket->setPort( IMAP_PORT );
00277 else
00278 d->normalSocket->setPort( SMTP_PORT );
00279 connect( d->normalSocket, SIGNAL( connected() ), SLOT( slotNormalPossible() ) );
00280 connect( d->normalSocket, SIGNAL( failed() ), SLOT( slotNormalNotPossible() ) );
00281 connect( d->normalSocket, SIGNAL( data( const QString& ) ),
00282 SLOT( slotReadNormal( const QString& ) ) );
00283 d->normalSocket->reconnect();
00284 d->normalSocketTimer->start( 10000 );
00285
00286 d->secureSocket = new MailTransport::Socket( this );
00287 d->secureSocket->setObjectName( QLatin1String( "secure" ) );
00288 d->secureSocket->setServer( d->server );
00289 d->secureSocket->setProtocol( d->testProtocol + QLatin1Char( 's' ) );
00290 if ( d->testProtocol == IMAP_PROTOCOL)
00291 d->secureSocket->setPort( IMAPS_PORT );
00292 else
00293 d->secureSocket->setPort( SMTPS_PORT );
00294 d->secureSocket->setSecure( true );
00295 connect( d->secureSocket, SIGNAL( connected() ), SLOT( slotSslPossible() ) );
00296 connect( d->secureSocket, SIGNAL( failed() ), SLOT( slotSslNotPossible() ) );
00297 connect( d->secureSocket, SIGNAL( data( const QString& ) ),
00298 SLOT( slotReadSecure( const QString& ) ) );
00299 d->secureSocket->reconnect();
00300 d->secureSocketTimer->start( 10000 );
00301 }
00302
00303 void ServerTest::setFakeHostname( const QString& fakeHostname )
00304 {
00305 d->fakeHostname = fakeHostname;
00306 }
00307
00308 QString ServerTest::fakeHostname()
00309 {
00310 return d->fakeHostname;
00311 }
00312
00313 void ServerTest::setServer( const QString& server )
00314 {
00315 d->server = server;
00316 }
00317
00318 void ServerTest::setProgressBar( QProgressBar* pb )
00319 {
00320 d->testProgress = pb;
00321 }
00322
00323 void ServerTest::setProtocol( const QString& protocol )
00324 {
00325 d->testProtocol = protocol;
00326 }
00327
00328 QString ServerTest::protocol()
00329 {
00330 return d->testProtocol;
00331 }
00332
00333 QString ServerTest::server()
00334 {
00335 return d->server;
00336 }
00337
00338 QProgressBar* ServerTest::progressBar()
00339 {
00340 return d->testProgress;
00341 }
00342
00343 QList< int > ServerTest::normalProtocols()
00344 {
00345 return d->authenticationResults[TransportBase::EnumEncryption::None];
00346 }
00347
00348 QList< int > ServerTest::secureProtocols()
00349 {
00350 return d->authenticationResults[Transport::EnumEncryption::SSL];
00351 }
00352
00353
00354 #include "servertest.moc"