Blame lang/qt/tests/t-config.cpp

Packit d7e8d0
/* t-config.cpp
Packit d7e8d0
Packit d7e8d0
    This file is part of qgpgme, the Qt API binding for gpgme
Packit d7e8d0
    Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
Packit d7e8d0
    Software engineering by Intevation GmbH
Packit d7e8d0
Packit d7e8d0
    QGpgME is free software; you can redistribute it and/or
Packit d7e8d0
    modify it under the terms of the GNU General Public License as
Packit d7e8d0
    published by the Free Software Foundation; either version 2 of the
Packit d7e8d0
    License, or (at your option) any later version.
Packit d7e8d0
Packit d7e8d0
    QGpgME is distributed in the hope that it will be useful,
Packit d7e8d0
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit d7e8d0
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit d7e8d0
    General Public License for more details.
Packit d7e8d0
Packit d7e8d0
    You should have received a copy of the GNU General Public License
Packit d7e8d0
    along with this program; if not, write to the Free Software
Packit d7e8d0
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit d7e8d0
Packit d7e8d0
    In addition, as a special exception, the copyright holders give
Packit d7e8d0
    permission to link the code of this program with any edition of
Packit d7e8d0
    the Qt library by Trolltech AS, Norway (or with modified versions
Packit d7e8d0
    of Qt that use the same license as Qt), and distribute linked
Packit d7e8d0
    combinations including the two.  You must obey the GNU General
Packit d7e8d0
    Public License in all respects for all of the code used other than
Packit d7e8d0
    Qt.  If you modify this file, you may extend this exception to
Packit d7e8d0
    your version of the file, but you are not obligated to do so.  If
Packit d7e8d0
    you do not wish to do so, delete this exception statement from
Packit d7e8d0
    your version.
Packit d7e8d0
*/
Packit d7e8d0
#ifdef HAVE_CONFIG_H
Packit d7e8d0
 #include "config.h"
Packit d7e8d0
#endif
Packit d7e8d0
Packit d7e8d0
#include <QDebug>
Packit d7e8d0
#include <QTest>
Packit d7e8d0
#include <QTemporaryDir>
Packit d7e8d0
#include "t-support.h"
Packit d7e8d0
#include "protocol.h"
Packit d7e8d0
#include "cryptoconfig.h"
Packit Service 30b792
#include "engineinfo.h"
Packit Service 30b792
Packit d7e8d0
#include <unistd.h>
Packit d7e8d0
Packit d7e8d0
using namespace QGpgME;
Packit d7e8d0
Packit d7e8d0
class CryptoConfigTest: public QGpgMETest
Packit d7e8d0
{
Packit d7e8d0
    Q_OBJECT
Packit d7e8d0
Packit d7e8d0
private Q_SLOTS:
Packit d7e8d0
    void testKeyserver()
Packit d7e8d0
    {
Packit d7e8d0
        // Repeatedly set a config value and clear it
Packit Service 30b792
        // this was broken at some point so it gets a
Packit d7e8d0
        // unit test.
Packit d7e8d0
        for (int i = 0; i < 10; i++) {
Packit d7e8d0
            auto conf = cryptoConfig();
Packit d7e8d0
            QVERIFY(conf);
Packit d7e8d0
            auto entry = conf->entry(QStringLiteral("gpg"),
Packit d7e8d0
                    QStringLiteral("Keyserver"),
Packit d7e8d0
                    QStringLiteral("keyserver"));
Packit d7e8d0
            QVERIFY(entry);
Packit d7e8d0
            const QString url(QStringLiteral("hkp://foo.bar.baz"));
Packit d7e8d0
            entry->setStringValue(url);
Packit d7e8d0
            conf->sync(false);
Packit d7e8d0
            conf->clear();
Packit d7e8d0
            entry = conf->entry(QStringLiteral("gpg"),
Packit d7e8d0
                    QStringLiteral("Keyserver"),
Packit d7e8d0
                    QStringLiteral("keyserver"));
Packit d7e8d0
            QCOMPARE (entry->stringValue(), url);
Packit d7e8d0
            entry->setStringValue(QString());
Packit d7e8d0
            conf->sync(false);
Packit d7e8d0
            conf->clear();
Packit d7e8d0
            entry = conf->entry(QStringLiteral("gpg"),
Packit d7e8d0
                    QStringLiteral("Keyserver"),
Packit d7e8d0
                    QStringLiteral("keyserver"));
Packit d7e8d0
            QCOMPARE (entry->stringValue(), QString());
Packit d7e8d0
        }
Packit d7e8d0
    }
Packit d7e8d0
Packit Service 30b792
    void testDefault()
Packit Service 30b792
    {
Packit Service 30b792
        if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.2.0") {
Packit Service 30b792
            // We are using compliance here and other options might also
Packit Service 30b792
            // be unsupported in older versions.
Packit Service 30b792
            return;
Packit Service 30b792
        }
Packit Service 30b792
        // First set compliance to de-vs
Packit Service 30b792
        auto conf = cryptoConfig();
Packit Service 30b792
        QVERIFY(conf);
Packit Service 30b792
        auto entry = conf->entry(QStringLiteral("gpg"),
Packit Service 30b792
                QStringLiteral("Configuration"),
Packit Service 30b792
                QStringLiteral("compliance"));
Packit Service 30b792
        QVERIFY(entry);
Packit Service 30b792
        entry->setStringValue("de-vs");
Packit Service 30b792
        conf->sync(true);
Packit Service 30b792
        conf->clear();
Packit Service 30b792
        entry = conf->entry(QStringLiteral("gpg"),
Packit Service 30b792
                QStringLiteral("Configuration"),
Packit Service 30b792
                QStringLiteral("compliance"));
Packit Service 30b792
        QCOMPARE(entry->stringValue(), QStringLiteral("de-vs"));
Packit Service 30b792
        entry->resetToDefault();
Packit Service 30b792
        conf->sync(true);
Packit Service 30b792
        conf->clear();
Packit Service 30b792
        entry = conf->entry(QStringLiteral("gpg"),
Packit Service 30b792
                QStringLiteral("Configuration"),
Packit Service 30b792
                QStringLiteral("compliance"));
Packit Service 30b792
        QCOMPARE(entry->stringValue(), QStringLiteral("gnupg"));
Packit Service 30b792
    }
Packit Service 30b792
Packit d7e8d0
    void initTestCase()
Packit d7e8d0
    {
Packit d7e8d0
        QGpgMETest::initTestCase();
Packit d7e8d0
        const QString gpgHome = qgetenv("GNUPGHOME");
Packit d7e8d0
        qputenv("GNUPGHOME", mDir.path().toUtf8());
Packit d7e8d0
        QVERIFY(mDir.isValid());
Packit d7e8d0
    }
Packit d7e8d0
private:
Packit d7e8d0
    QTemporaryDir mDir;
Packit d7e8d0
Packit d7e8d0
};
Packit d7e8d0
Packit d7e8d0
QTEST_MAIN(CryptoConfigTest)
Packit d7e8d0
Packit d7e8d0
#include "t-config.moc"