afbb4d1b1f1cc4aee958add3d61263feca10ba10
[platform/upstream/gpgme.git] / lang / qt / tests / t-config.cpp
1 /* t-config.cpp
2
3     This file is part of qgpgme, the Qt API binding for gpgme
4     Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
5     Software engineering by Intevation GmbH
6
7     QGpgME is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License as
9     published by the Free Software Foundation; either version 2 of the
10     License, or (at your option) any later version.
11
12     QGpgME is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21     In addition, as a special exception, the copyright holders give
22     permission to link the code of this program with any edition of
23     the Qt library by Trolltech AS, Norway (or with modified versions
24     of Qt that use the same license as Qt), and distribute linked
25     combinations including the two.  You must obey the GNU General
26     Public License in all respects for all of the code used other than
27     Qt.  If you modify this file, you may extend this exception to
28     your version of the file, but you are not obligated to do so.  If
29     you do not wish to do so, delete this exception statement from
30     your version.
31 */
32 #ifdef HAVE_CONFIG_H
33  #include "config.h"
34 #endif
35
36 #include <QDebug>
37 #include <QTest>
38 #include <QTemporaryDir>
39 #include "t-support.h"
40 #include "protocol.h"
41 #include "cryptoconfig.h"
42 #include "engineinfo.h"
43
44 #include <unistd.h>
45
46 using namespace QGpgME;
47
48 class CryptoConfigTest: public QGpgMETest
49 {
50     Q_OBJECT
51
52 private Q_SLOTS:
53     void testKeyserver()
54     {
55         // Repeatedly set a config value and clear it
56         // this was broken at some point so it gets a
57         // unit test.
58         for (int i = 0; i < 10; i++) {
59             auto conf = cryptoConfig();
60             QVERIFY(conf);
61             auto entry = conf->entry(QStringLiteral("gpg"),
62                     QStringLiteral("Keyserver"),
63                     QStringLiteral("keyserver"));
64             QVERIFY(entry);
65             const QString url(QStringLiteral("hkp://foo.bar.baz"));
66             entry->setStringValue(url);
67             conf->sync(false);
68             conf->clear();
69             entry = conf->entry(QStringLiteral("gpg"),
70                     QStringLiteral("Keyserver"),
71                     QStringLiteral("keyserver"));
72             QCOMPARE (entry->stringValue(), url);
73             entry->setStringValue(QString());
74             conf->sync(false);
75             conf->clear();
76             entry = conf->entry(QStringLiteral("gpg"),
77                     QStringLiteral("Keyserver"),
78                     QStringLiteral("keyserver"));
79             QCOMPARE (entry->stringValue(), QString());
80         }
81     }
82
83     void testDefault()
84     {
85         if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.2.0") {
86             // We are using compliance here and other options might also
87             // be unsupported in older versions.
88             return;
89         }
90         // First set compliance to de-vs
91         auto conf = cryptoConfig();
92         QVERIFY(conf);
93         auto entry = conf->entry(QStringLiteral("gpg"),
94                 QStringLiteral("Configuration"),
95                 QStringLiteral("compliance"));
96         QVERIFY(entry);
97         entry->setStringValue("de-vs");
98         conf->sync(true);
99         conf->clear();
100         entry = conf->entry(QStringLiteral("gpg"),
101                 QStringLiteral("Configuration"),
102                 QStringLiteral("compliance"));
103         QCOMPARE(entry->stringValue(), QStringLiteral("de-vs"));
104         entry->resetToDefault();
105         conf->sync(true);
106         conf->clear();
107         entry = conf->entry(QStringLiteral("gpg"),
108                 QStringLiteral("Configuration"),
109                 QStringLiteral("compliance"));
110         QCOMPARE(entry->stringValue(), QStringLiteral("gnupg"));
111     }
112
113     void initTestCase()
114     {
115         QGpgMETest::initTestCase();
116         const QString gpgHome = qgetenv("GNUPGHOME");
117         qputenv("GNUPGHOME", mDir.path().toUtf8());
118         QVERIFY(mDir.isValid());
119     }
120 private:
121     QTemporaryDir mDir;
122
123 };
124
125 QTEST_MAIN(CryptoConfigTest)
126
127 #include "t-config.moc"