Changeset View
Changeset View
Standalone View
Standalone View
autotests/tofutest.cpp
- This file was added.
1 | /* This file is part of Kleopatra, the KDE keymanager | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2016 Klarälvdalens Datakonsult AB | ||||
3 | | ||||
4 | Kleopatra is free software; you can redistribute it and/or modify | ||||
5 | it under the terms of the GNU General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or | ||||
7 | (at your option) any later version. | ||||
8 | | ||||
9 | Kleopatra is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program; if not, write to the Free Software | ||||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
17 | */ | ||||
18 | | ||||
19 | #include <QObject> | ||||
20 | #include <QTest> | ||||
21 | #include <QTemporaryDir> | ||||
22 | #include <QScopedPointer> | ||||
23 | #include <QFile> | ||||
24 | | ||||
25 | #define GPGCONF_EXECUTABLE QFINDTESTDATA("gpgconf.py") | ||||
26 | #include "../src/kleo/tofu.cpp" | ||||
27 | | ||||
28 | class TOFUTest : public QObject | ||||
29 | { | ||||
30 | Q_OBJECT | ||||
31 | | ||||
32 | private Q_SLOTS: | ||||
33 | void testIsSupported_data() | ||||
34 | { | ||||
35 | QTest::addColumn<QString>("version"); | ||||
36 | QTest::addColumn<bool>("supported"); | ||||
37 | | ||||
38 | QTest::newRow("old version") << QStringLiteral("1.1.1") << false;; | ||||
39 | QTest::newRow("old version beta") << QStringLiteral("1.2.3-beta296") << false; | ||||
40 | QTest::newRow("new version") << QStringLiteral("2.1.16") << true; | ||||
41 | QTest::newRow("new version beta") << QStringLiteral("2.1.16-beta296") << true; | ||||
42 | | ||||
43 | const QString minVersion = QString::fromLatin1("%1.%2.%3").arg(MinVersion >> 16) | ||||
44 | .arg((MinVersion >> 8) & 0xff) | ||||
45 | .arg(MinVersion & 0xff); | ||||
46 | QTest::newRow("min version") << minVersion << true; | ||||
47 | QTest::newRow("min version beta") << minVersion + QStringLiteral("-beta1235") << true; | ||||
48 | } | ||||
49 | | ||||
50 | void testIsSupported() | ||||
51 | { | ||||
52 | QFETCH(QString, version); | ||||
53 | QFETCH(bool, supported); | ||||
54 | | ||||
55 | qputenv("FAKEGPG_VERSION", version.toLatin1()); | ||||
56 | | ||||
57 | QCOMPARE(TOFU::isSupported(), supported); | ||||
58 | } | ||||
59 | | ||||
60 | | ||||
61 | void testIsEnabled_data() | ||||
62 | { | ||||
63 | QTest::addColumn<bool>("dirExists"); | ||||
64 | QTest::addColumn<bool>("fileExists"); | ||||
65 | QTest::addColumn<bool>("supported"); | ||||
66 | QTest::addColumn<QString>("sample"); | ||||
67 | QTest::addColumn<bool>("enabled"); | ||||
68 | | ||||
69 | QTest::newRow("unsupported") << false << false << false << QString() << false; | ||||
70 | QTest::newRow("no dir") << false << false << true << QString() << false; | ||||
71 | QTest::newRow("no file") << true << false << true << QString() << false; | ||||
72 | QTest::newRow("disabled") << true << true << true << QStringLiteral("disabled") << false; | ||||
73 | QTest::newRow("trust-model: auto") << true << true << true << QStringLiteral("trust-model-auto") << false; | ||||
74 | QTest::newRow("trust-model: tofu") << true << true << true << QStringLiteral("trust-model-tofu") << true; | ||||
75 | QTest::newRow("trust-model: tofu+pgp") << true << true << true << QStringLiteral("trust-model-tofu-pgp") << true; | ||||
76 | } | ||||
77 | | ||||
78 | void testIsEnabled() | ||||
79 | { | ||||
80 | QFETCH(bool, dirExists); | ||||
81 | QFETCH(bool, fileExists); | ||||
82 | QFETCH(bool, supported); | ||||
83 | QFETCH(QString, sample); | ||||
84 | QFETCH(bool, enabled); | ||||
85 | | ||||
86 | QScopedPointer<QTemporaryDir> tmpDir; | ||||
87 | QString gnupgHome; | ||||
88 | if (dirExists) { | ||||
89 | tmpDir.reset(new QTemporaryDir()); | ||||
90 | gnupgHome = tmpDir->path() + QStringLiteral("/.gnupg"); | ||||
91 | QVERIFY(QDir().mkpath(gnupgHome)); | ||||
92 | } | ||||
93 | | ||||
94 | if (dirExists && fileExists) { | ||||
95 | QVERIFY(QFile::copy(QFINDTESTDATA(QStringLiteral("fakehomes/%1/gpg.conf").arg(sample)), | ||||
96 | gnupgHome + QStringLiteral("/gpg.conf"))); | ||||
97 | } | ||||
98 | | ||||
99 | qputenv("FAKEGPG_VERSION", supported ? "2.1.16" : "1.4.21"); | ||||
100 | qputenv("FAKEGPG_HOME", gnupgHome.toLatin1()); | ||||
101 | | ||||
102 | QCOMPARE(TOFU::isEnabled(), enabled); | ||||
103 | } | ||||
104 | | ||||
105 | void testSetEnabled_data() | ||||
106 | { | ||||
107 | QTest::addColumn<bool>("dirExists"); | ||||
108 | QTest::addColumn<bool>("fileExists"); | ||||
109 | QTest::addColumn<QString>("sample"); | ||||
110 | QTest::addColumn<bool>("enable"); | ||||
111 | | ||||
112 | QTest::newRow("no dir, disable") << false << false << QString() << false; | ||||
113 | QTest::newRow("no dir, enable") << false << false << QStringLiteral("new") << true; | ||||
114 | QTest::newRow("no file, disable") << true << false << QString() << false; | ||||
115 | QTest::newRow("no file, enable") << true << false << QStringLiteral("new") << true; | ||||
116 | QTest::newRow("disabled, enable") << true << true << QStringLiteral("disabled") << true; | ||||
117 | QTest::newRow("disabled, disable") << true << true << QStringLiteral("disabled") << false; | ||||
118 | QTest::newRow("trust-model-auto, enable") << true << true << QStringLiteral("trust-model-auto") << true; | ||||
119 | QTest::newRow("trust-model-auto, disable") << true << true << QStringLiteral("trust-model-auto") << false; | ||||
120 | QTest::newRow("trust-model-tofu, enable") << true << true << QStringLiteral("trust-model-tofu") << true; | ||||
121 | QTest::newRow("trust-model-tofu, disable") << true << true << QStringLiteral("trust-model-tofu") << false; | ||||
122 | QTest::newRow("trust-model-tofu-pgp, enable") << true << true << QStringLiteral("trust-model-tofu-pgp") << true; | ||||
123 | QTest::newRow("trust-model-tofu-pgp, disable") << true << true << QStringLiteral("trust-model-tofu-pgp") << false; | ||||
124 | } | ||||
125 | | ||||
126 | void testSetEnabled() | ||||
127 | { | ||||
128 | QFETCH(bool, dirExists); | ||||
129 | QFETCH(bool, fileExists); | ||||
130 | QFETCH(QString, sample); | ||||
131 | QFETCH(bool, enable); | ||||
132 | | ||||
133 | QTemporaryDir tmpDir; | ||||
134 | const QString gnupgHome = tmpDir.path() + QStringLiteral("/.gnupg"); | ||||
135 | if (dirExists) { | ||||
136 | QVERIFY(QDir().mkpath(gnupgHome)); | ||||
137 | } else { | ||||
138 | tmpDir.remove(); | ||||
139 | } | ||||
140 | | ||||
141 | if (dirExists && fileExists) { | ||||
142 | QVERIFY(QFile::copy(QFINDTESTDATA(QStringLiteral("fakehomes/%1/gpg.conf").arg(sample)), | ||||
143 | gnupgHome + QStringLiteral("/gpg.conf"))); | ||||
144 | } | ||||
145 | | ||||
146 | qputenv("FAKEGPG_HOME", gnupgHome.toLatin1()); | ||||
147 | | ||||
148 | QCOMPARE(TOFU::setEnabled(enable), true); | ||||
149 | QCOMPARE(TOFU::isEnabled(), enable); | ||||
150 | | ||||
151 | QFile sourceFile(gnupgHome + QStringLiteral("/gpg.conf")); | ||||
152 | QCOMPARE(sourceFile.exists(), (dirExists && fileExists) || enable); | ||||
153 | if (!sourceFile.exists()) { | ||||
154 | return; | ||||
155 | } | ||||
156 | | ||||
157 | QVERIFY(sourceFile.open(QIODevice::ReadOnly)); | ||||
158 | const QByteArray sourceData = sourceFile.readAll(); | ||||
159 | | ||||
160 | QFile referenceFile(QFINDTESTDATA(QStringLiteral("fakehomes/%1/gpg.conf.%2") | ||||
161 | .arg(sample, | ||||
162 | enable ? QStringLiteral("enabled") : QStringLiteral("disabled")))); | ||||
163 | QVERIFY(referenceFile.exists()); | ||||
164 | QVERIFY(referenceFile.open(QIODevice::ReadOnly)); | ||||
165 | const QByteArray referenceData = referenceFile.readAll(); | ||||
166 | | ||||
167 | if (sourceData != referenceData) { | ||||
168 | qDebug() << sourceData; | ||||
169 | qDebug() << referenceData; | ||||
170 | } | ||||
171 | QCOMPARE(sourceData, referenceData); | ||||
172 | } | ||||
173 | }; | ||||
174 | | ||||
175 | QTEST_MAIN(TOFUTest) | ||||
176 | | ||||
177 | #include "tofutest.moc" |