|
| 1 | +/*************************************************************************** |
| 2 | + testqgsauthconfig.cpp |
| 3 | + ---------------------- |
| 4 | + Date : September 2015 |
| 5 | + Copyright : (C) 2015 by Boundless Spatial, Inc. USA |
| 6 | + Author : Larry Shaffer |
| 7 | + Email : lshaffer at boundlessgeo dot com |
| 8 | + *************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | +#include <QtTest/QtTest> |
| 17 | +#include <QObject> |
| 18 | +#include <QString> |
| 19 | +#include <QStringList> |
| 20 | + |
| 21 | +#include "qgsapplication.h" |
| 22 | +#include "qgsauthconfig.h" |
| 23 | + |
| 24 | +/** \ingroup UnitTests |
| 25 | + * Unit tests for QgsAuthConfig |
| 26 | + */ |
| 27 | +class TestQgsAuthConfig: public QObject |
| 28 | +{ |
| 29 | + Q_OBJECT |
| 30 | + |
| 31 | + private slots: |
| 32 | + void initTestCase(); |
| 33 | + void cleanupTestCase(); |
| 34 | + void init() {} |
| 35 | + void cleanup() {} |
| 36 | + |
| 37 | + void testMethodConfig(); |
| 38 | + void testPkiBundle(); |
| 39 | + void testPkiConfigBundle(); |
| 40 | + void testConfigSslServer(); |
| 41 | + |
| 42 | + private: |
| 43 | + static QString smPkiData; |
| 44 | +}; |
| 45 | + |
| 46 | +QString TestQgsAuthConfig::smPkiData = QString( TEST_DATA_DIR ) + "/auth_system/certs_keys"; |
| 47 | + |
| 48 | + |
| 49 | +void TestQgsAuthConfig::initTestCase() |
| 50 | +{ |
| 51 | + QgsApplication::init(); |
| 52 | + QgsApplication::initQgis(); |
| 53 | +} |
| 54 | + |
| 55 | +void TestQgsAuthConfig::cleanupTestCase() |
| 56 | +{ |
| 57 | + QgsApplication::exitQgis(); |
| 58 | +} |
| 59 | + |
| 60 | +void TestQgsAuthConfig::testMethodConfig() |
| 61 | +{ |
| 62 | + QgsAuthMethodConfig mconfig; |
| 63 | + Q_ASSERT( !mconfig.isValid() ); |
| 64 | + |
| 65 | + mconfig.setName( "Some Name" ); |
| 66 | + mconfig.setMethod( "MethodKey" ); |
| 67 | + Q_ASSERT( mconfig.isValid() ); |
| 68 | + |
| 69 | + mconfig.setId( "0000000" ); |
| 70 | + Q_ASSERT( mconfig.isValid( true ) ); |
| 71 | + |
| 72 | + mconfig.setVersion( 1 ); |
| 73 | + mconfig.setUri( "http://example.com" ); |
| 74 | + |
| 75 | + QCOMPARE( mconfig.name(), QString( "Some Name" ) ); |
| 76 | + QCOMPARE( mconfig.method(), QString( "MethodKey" ) ); |
| 77 | + QCOMPARE( mconfig.id(), QString( "0000000" ) ); |
| 78 | + QCOMPARE( mconfig.version(), 1 ); |
| 79 | + QCOMPARE( mconfig.uri(), QString( "http://example.com" ) ); |
| 80 | + |
| 81 | + QString confstr( "key1:::value1|||key2:::value2|||key3:::value3a```value3b```value3c" ); |
| 82 | + QgsStringMap confmap; |
| 83 | + confmap.insert( "key1", "value1" ); |
| 84 | + confmap.insert( "key2", "value2" ); |
| 85 | + confmap.insert( "key3", "value3a```value3b```value3c" ); |
| 86 | + |
| 87 | + mconfig.setConfigMap( confmap ); |
| 88 | + QCOMPARE( mconfig.configMap(), confmap ); |
| 89 | + QCOMPARE( mconfig.configString(), confstr ); |
| 90 | + |
| 91 | + mconfig.clearConfigMap(); |
| 92 | + Q_ASSERT( mconfig.configMap().isEmpty() ); |
| 93 | + |
| 94 | + mconfig.setConfig( "key1", "value1" ); |
| 95 | + mconfig.setConfig( "key2", "value2" ); |
| 96 | + QStringList key3list; |
| 97 | + key3list << "value3a" << "value3b" << "value3c"; |
| 98 | + mconfig.setConfigList( "key3", key3list ); |
| 99 | + QCOMPARE( mconfig.configMap(), confmap ); |
| 100 | + QCOMPARE( mconfig.configString(), confstr ); |
| 101 | + |
| 102 | + QCOMPARE( mconfig.config( "key1" ), QString( "value1" ) ); |
| 103 | + QCOMPARE( mconfig.configList( "key3" ), key3list ); |
| 104 | + |
| 105 | + Q_ASSERT( mconfig.hasConfig( "key2" ) ); |
| 106 | + mconfig.removeConfig( "key2" ); |
| 107 | + Q_ASSERT( !mconfig.hasConfig( "key2" ) ); |
| 108 | + |
| 109 | + mconfig.loadConfigString( confstr ); |
| 110 | + QCOMPARE( mconfig.configMap(), confmap ); |
| 111 | + QCOMPARE( mconfig.configString(), confstr ); |
| 112 | + |
| 113 | + QgsAuthMethodConfig mconfig2( mconfig ); |
| 114 | + Q_ASSERT( mconfig2 == mconfig ); |
| 115 | + |
| 116 | + mconfig.setMethod( "MethodKey2" ); |
| 117 | + Q_ASSERT( mconfig2 != mconfig ); |
| 118 | +} |
| 119 | + |
| 120 | +void TestQgsAuthConfig::testPkiBundle() |
| 121 | +{ |
| 122 | + QgsPkiBundle bundle; |
| 123 | + Q_ASSERT( bundle.isNull() ); |
| 124 | + Q_ASSERT( !bundle.isValid() ); |
| 125 | + |
| 126 | + QList<QSslCertificate> cacerts( QSslCertificate::fromPath( smPkiData + "/chain_subissuer-issuer-root.pem" ) ); |
| 127 | + Q_ASSERT( !cacerts.isEmpty() ); |
| 128 | + QCOMPARE( cacerts.size(), 3 ); |
| 129 | + QgsPkiBundle bundle2( QgsPkiBundle::fromPemPaths( smPkiData + "/fra_cert.pem", |
| 130 | + smPkiData + "/fra_key_w-pass.pem", |
| 131 | + "password", |
| 132 | + cacerts ) ); |
| 133 | + Q_ASSERT( !bundle2.isNull() ); |
| 134 | + Q_ASSERT( bundle2.isValid() ); |
| 135 | + QCOMPARE( bundle2.certId(), QString( "c3633c428d441853973e5081ba9be39f667f5af6" ) ); |
| 136 | + |
| 137 | + QSslCertificate clientcert( bundle2.clientCert() ); |
| 138 | + Q_ASSERT( !clientcert.isNull() ); |
| 139 | + QSslKey clientkey( bundle2.clientKey( true ) ); |
| 140 | + Q_ASSERT( !clientkey.isNull() ); |
| 141 | + QString keypass( bundle2.keyPassphrase() ); |
| 142 | + Q_ASSERT( !keypass.isEmpty() ); |
| 143 | + QList<QSslCertificate> cachain( bundle2.caChain() ); |
| 144 | + Q_ASSERT( !cachain.isEmpty() ); |
| 145 | + QCOMPARE( cachain.size(), 3 ); |
| 146 | + |
| 147 | + QgsPkiBundle bundle3( clientcert, clientkey, keypass, cachain ); |
| 148 | + Q_ASSERT( !bundle3.isNull() ); |
| 149 | + Q_ASSERT( bundle3.isValid() ); |
| 150 | + |
| 151 | + bundle.setClientCert( clientcert ); |
| 152 | + bundle.setClientKey( clientkey ); |
| 153 | + bundle.setKeyPassphrase( keypass ); |
| 154 | + bundle.setCaChain( cachain ); |
| 155 | + Q_ASSERT( !bundle.isNull() ); |
| 156 | + Q_ASSERT( bundle.isValid() ); |
| 157 | + |
| 158 | + QgsPkiBundle bundle4( QgsPkiBundle::fromPkcs12Paths( smPkiData + "/fra_w-chain.p12", |
| 159 | + "password" ) ); |
| 160 | + Q_ASSERT( !bundle4.isNull() ); |
| 161 | + Q_ASSERT( bundle4.isValid() ); |
| 162 | + QList<QSslCertificate> cachain4( bundle2.caChain() ); |
| 163 | + Q_ASSERT( !cachain4.isEmpty() ); |
| 164 | + QCOMPARE( cachain4.size(), 3 ); |
| 165 | +} |
| 166 | + |
| 167 | +void TestQgsAuthConfig::testPkiConfigBundle() |
| 168 | +{ |
| 169 | + QgsAuthMethodConfig mconfig; |
| 170 | + mconfig.setName( "Some Name" ); |
| 171 | + mconfig.setMethod( "MethodKey" ); |
| 172 | + mconfig.setId( "0000000" ); |
| 173 | + mconfig.setVersion( 1 ); |
| 174 | + mconfig.setUri( "http://example.com" ); |
| 175 | + Q_ASSERT( mconfig.isValid( true ) ); |
| 176 | + |
| 177 | + QSslCertificate clientcert( QSslCertificate::fromPath( smPkiData + "/gerardus_cert.pem" ).first() ); |
| 178 | + QByteArray keydata; |
| 179 | + QFile file( smPkiData + "/gerardus_key.pem" ); |
| 180 | + if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) |
| 181 | + keydata = file.readAll(); |
| 182 | + file.close(); |
| 183 | + QSslKey clientkey( keydata, QSsl::Rsa ); |
| 184 | + |
| 185 | + QgsPkiConfigBundle bundle( mconfig, clientcert, clientkey ); |
| 186 | + Q_ASSERT( bundle.isValid() ); |
| 187 | + QCOMPARE( bundle.config(), mconfig ); |
| 188 | + |
| 189 | + QCOMPARE( bundle.clientCert(), clientcert ); |
| 190 | + QCOMPARE( bundle.clientCertKey(), clientkey ); |
| 191 | + bundle.setConfig( mconfig ); |
| 192 | + bundle.setClientCert( clientcert ); |
| 193 | + bundle.setClientCertKey( clientkey ); |
| 194 | + Q_ASSERT( bundle.isValid() ); |
| 195 | + QCOMPARE( bundle.config(), mconfig ); |
| 196 | + QCOMPARE( bundle.clientCert(), clientcert ); |
| 197 | + QCOMPARE( bundle.clientCertKey(), clientkey ); |
| 198 | +} |
| 199 | + |
| 200 | +void TestQgsAuthConfig::testConfigSslServer() |
| 201 | +{ |
| 202 | + QString hostport( "localhost:443" ); |
| 203 | + QString confstr( "2|||470|||2|||10~~19|||0~~2" ); |
| 204 | + QSslCertificate sslcert( QSslCertificate::fromPath( smPkiData + "/localhost_ssl_cert.pem" ).first() ); |
| 205 | + |
| 206 | + QgsAuthConfigSslServer sslconfig; |
| 207 | + Q_ASSERT( sslconfig.isNull() ); |
| 208 | + QCOMPARE( sslconfig.qtVersion(), 480 ); |
| 209 | + QCOMPARE( sslconfig.version(), 1 ); |
| 210 | + QCOMPARE( sslconfig.sslPeerVerifyMode(), QSslSocket::VerifyPeer ); |
| 211 | + |
| 212 | + sslconfig.setSslCertificate( sslcert ); |
| 213 | + sslconfig.setSslHostPort( hostport ); |
| 214 | + sslconfig.setSslProtocol( QSsl::TlsV1 ); |
| 215 | + sslconfig.setVersion( 2 ); |
| 216 | + sslconfig.setQtVersion( 470 ); |
| 217 | + sslconfig.setSslPeerVerifyMode( QSslSocket::VerifyNone ); |
| 218 | + sslconfig.setSslPeerVerifyDepth( 2 ); |
| 219 | + QList<QSslError::SslError> sslerrenums; |
| 220 | + sslerrenums << QSslError::SelfSignedCertificateInChain << QSslError::SubjectIssuerMismatch; |
| 221 | + sslconfig.setSslIgnoredErrorEnums( sslerrenums ); |
| 222 | + Q_ASSERT( !sslconfig.isNull() ); |
| 223 | + |
| 224 | + QCOMPARE( sslconfig.configString(), confstr ); |
| 225 | + QCOMPARE( sslconfig.sslHostPort(), hostport ); |
| 226 | + QCOMPARE( sslconfig.sslCertificate(), sslcert ); |
| 227 | + QCOMPARE( sslconfig.sslProtocol(), QSsl::TlsV1 ); |
| 228 | + QCOMPARE( sslconfig.version(), 2 ); |
| 229 | + QCOMPARE( sslconfig.qtVersion(), 470 ); |
| 230 | + QCOMPARE( sslconfig.sslPeerVerifyMode(), QSslSocket::VerifyNone ); |
| 231 | + QCOMPARE( sslconfig.sslPeerVerifyDepth(), 2 ); |
| 232 | + QCOMPARE( sslconfig.sslIgnoredErrorEnums(), sslerrenums ); |
| 233 | + |
| 234 | + QgsAuthConfigSslServer sslconfig2; |
| 235 | + sslconfig2.loadConfigString( confstr ); |
| 236 | + QCOMPARE( sslconfig2.sslProtocol(), QSsl::TlsV1 ); |
| 237 | + QCOMPARE( sslconfig2.version(), 2 ); |
| 238 | + QCOMPARE( sslconfig2.qtVersion(), 470 ); |
| 239 | + QCOMPARE( sslconfig2.sslPeerVerifyMode(), QSslSocket::VerifyNone ); |
| 240 | + QCOMPARE( sslconfig2.sslPeerVerifyDepth(), 2 ); |
| 241 | + QCOMPARE( sslconfig2.sslIgnoredErrorEnums(), sslerrenums ); |
| 242 | + QCOMPARE( sslconfig2.configString(), confstr ); |
| 243 | +} |
| 244 | + |
| 245 | +QTEST_MAIN( TestQgsAuthConfig ) |
| 246 | +#include "testqgsauthconfig.moc" |
0 commit comments