|
| 1 | +/*************************************************************************** |
| 2 | + testqgsauthmanager.cpp |
| 3 | + ---------------------- |
| 4 | + Date : October 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 | + |
| 17 | +#include <QtTest/QtTest> |
| 18 | +#include <QtTest/QSignalSpy> |
| 19 | +#include <QObject> |
| 20 | +#include <QDir> |
| 21 | +#include <QFile> |
| 22 | +#include <QString> |
| 23 | +#include <QStringList> |
| 24 | +#include <QTextStream> |
| 25 | + |
| 26 | +#include "qgsapplication.h" |
| 27 | +#include "qgsauthmanager.h" |
| 28 | +#include "qgsauthconfig.h" |
| 29 | + |
| 30 | +/** \ingroup UnitTests |
| 31 | + * Unit tests for QgsAuthManager |
| 32 | + */ |
| 33 | +class TestQgsAuthManager: public QObject |
| 34 | +{ |
| 35 | + Q_OBJECT |
| 36 | + |
| 37 | + public: |
| 38 | + TestQgsAuthManager(); |
| 39 | + |
| 40 | + private slots: |
| 41 | + void initTestCase(); |
| 42 | + void cleanupTestCase(); |
| 43 | + void init() {} |
| 44 | + void cleanup() {} |
| 45 | + |
| 46 | + void testMasterPassword(); |
| 47 | + |
| 48 | + private: |
| 49 | + QString mPkiData; |
| 50 | + QString mTempDir; |
| 51 | + const char* mPass; |
| 52 | +}; |
| 53 | + |
| 54 | + |
| 55 | +TestQgsAuthManager::TestQgsAuthManager() |
| 56 | + : mPkiData( QString( TEST_DATA_DIR ) + "/auth_system/certs_keys" ) |
| 57 | + , mTempDir( QDir::tempPath() + "/auth" ) |
| 58 | + , mPass( "pass" ) |
| 59 | +{ |
| 60 | +} |
| 61 | + |
| 62 | +void TestQgsAuthManager::initTestCase() |
| 63 | +{ |
| 64 | + // make QGIS_AUTH_DB_DIR_PATH temp dir for qgis-auth.db and master password file |
| 65 | + QDir tmpDir = QDir::temp(); |
| 66 | + QVERIFY2( tmpDir.mkpath( mTempDir ), "Couldn't make temp directory" ); |
| 67 | + qputenv( "QGIS_AUTH_DB_DIR_PATH", mTempDir.toAscii() ); |
| 68 | + |
| 69 | + // init app and auth manager |
| 70 | + QgsApplication::init(); |
| 71 | + QgsApplication::initQgis(); |
| 72 | + QVERIFY2( !QgsAuthManager::instance()->isDisabled(), |
| 73 | + "Authentication system is DISABLED" ); |
| 74 | + |
| 75 | + // verify QGIS_AUTH_DB_DIR_PATH (temp auth db path) worked |
| 76 | + QString db1( QFileInfo( QgsAuthManager::instance()->authenticationDbPath() ).canonicalFilePath() ); |
| 77 | + QString db2( QFileInfo( mTempDir + "/qgis-auth.db" ).canonicalFilePath() ); |
| 78 | + QVERIFY2( db1 == db2, "Auth db temp path does not match db path of manager" ); |
| 79 | + |
| 80 | + // verify master pass can be set manually |
| 81 | + // (this also creates a fresh password hash in the new temp database) |
| 82 | + QVERIFY2( QgsAuthManager::instance()->setMasterPassword( mPass, true ), |
| 83 | + "Master password could not be set" ); |
| 84 | + QVERIFY2( QgsAuthManager::instance()->masterPasswordIsSet(), |
| 85 | + "Auth master password not set from passed string" ); |
| 86 | + |
| 87 | + // create QGIS_AUTH_PASSWORD_FILE file |
| 88 | + QString passfilepath = mTempDir + "/passfile"; |
| 89 | + QFile passfile( passfilepath ); |
| 90 | + if ( passfile.open( QIODevice::WriteOnly | QIODevice::Text ) ) |
| 91 | + { |
| 92 | + QTextStream fout( &passfile ); |
| 93 | + fout << QString( mPass ) << "\r\n"; |
| 94 | + passfile.close(); |
| 95 | + qputenv( "QGIS_AUTH_PASSWORD_FILE", passfilepath.toAscii() ); |
| 96 | + } |
| 97 | + // qDebug( "QGIS_AUTH_PASSWORD_FILE=%s", qgetenv( "QGIS_AUTH_PASSWORD_FILE" ).constData() ); |
| 98 | + |
| 99 | + // re-init app and auth manager |
| 100 | + QgsApplication::quit(); |
| 101 | + // QTest::qSleep( 3000 ); |
| 102 | + QgsApplication::init(); |
| 103 | + QgsApplication::initQgis(); |
| 104 | + QVERIFY2( !QgsAuthManager::instance()->isDisabled(), |
| 105 | + "Authentication system is DISABLED" ); |
| 106 | + |
| 107 | + // verify QGIS_AUTH_PASSWORD_FILE worked, when compared against hash in db |
| 108 | + QVERIFY2( QgsAuthManager::instance()->masterPasswordIsSet(), |
| 109 | + "Auth master password not set from QGIS_AUTH_PASSWORD_FILE" ); |
| 110 | + |
| 111 | + // all tests should now have a valid qgis-auth.db and stored/set master password |
| 112 | +} |
| 113 | + |
| 114 | +void TestQgsAuthManager::cleanupTestCase() |
| 115 | +{ |
| 116 | + QgsApplication::exitQgis(); |
| 117 | +} |
| 118 | + |
| 119 | +void TestQgsAuthManager::testMasterPassword() |
| 120 | +{ |
| 121 | + // password is already stored/set in initTestCase() |
| 122 | + // NOTE: leave it in the same state when done with this test |
| 123 | + QgsAuthManager *authm = QgsAuthManager::instance(); |
| 124 | + |
| 125 | + QVERIFY( authm->masterPasswordIsSet() ); |
| 126 | + QVERIFY( authm->masterPasswordHashInDb() ); |
| 127 | + QVERIFY( authm->masterPasswordSame( mPass ) ); |
| 128 | + QVERIFY( !authm->masterPasswordSame( "wrongpass" ) ); |
| 129 | + QVERIFY( authm->setMasterPassword() ); |
| 130 | + QVERIFY( authm->verifyMasterPassword() ); |
| 131 | + QVERIFY( authm->verifyMasterPassword( mPass ) ); |
| 132 | + QVERIFY( !authm->verifyMasterPassword( "wrongpass" ) ); |
| 133 | + |
| 134 | + QList<QVariant> spyargs; |
| 135 | + QSignalSpy spy( authm, SIGNAL( masterPasswordVerified( bool ) ) ); |
| 136 | + |
| 137 | + QVERIFY( authm->setMasterPassword( true ) ); |
| 138 | + QCOMPARE( spy.count(), 1 ); |
| 139 | + spyargs = spy.takeFirst(); |
| 140 | + QVERIFY( spyargs.at( 0 ).type() == QVariant::Bool ); |
| 141 | + QVERIFY( spyargs.at( 0 ).toBool() == true ); |
| 142 | + |
| 143 | + authm->clearMasterPassword(); |
| 144 | + QVERIFY( !authm->masterPasswordIsSet() ); |
| 145 | + QVERIFY( !authm->setMasterPassword( "wrongpass" , true ) ); |
| 146 | + QVERIFY( !authm->masterPasswordIsSet() ); |
| 147 | + QCOMPARE( spy.count(), 1 ); |
| 148 | + spyargs = spy.takeFirst(); |
| 149 | + QVERIFY( spyargs.at( 0 ).type() == QVariant::Bool ); |
| 150 | + QVERIFY( spyargs.at( 0 ).toBool() == false ); |
| 151 | + |
| 152 | + authm->clearMasterPassword(); |
| 153 | + QVERIFY( !authm->masterPasswordIsSet() ); |
| 154 | + QVERIFY( authm->setMasterPassword( mPass, true ) ); |
| 155 | + QVERIFY( authm->masterPasswordIsSet() ); |
| 156 | + QCOMPARE( spy.count(), 1 ); |
| 157 | + spyargs = spy.takeFirst(); |
| 158 | + QVERIFY( spyargs.at( 0 ).type() == QVariant::Bool ); |
| 159 | + QVERIFY( spyargs.at( 0 ).toBool() == true ); |
| 160 | +} |
| 161 | + |
| 162 | +QTEST_MAIN( TestQgsAuthManager ) |
| 163 | +#include "testqgsauthmanager.moc" |
0 commit comments