Line data Source code
1 : /* t-support.cpp
2 :
3 : This file is part of qgpgme, the Qt API binding for gpgme
4 : Copyright (c) 2016 Intevation GmbH
5 :
6 : QGpgME is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : QGpgME is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with this program; if not, write to the Free Software
18 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : In addition, as a special exception, the copyright holders give
21 : permission to link the code of this program with any edition of
22 : the Qt library by Trolltech AS, Norway (or with modified versions
23 : of Qt that use the same license as Qt), and distribute linked
24 : combinations including the two. You must obey the GNU General
25 : Public License in all respects for all of the code used other than
26 : Qt. If you modify this file, you may extend this exception to
27 : your version of the file, but you are not obligated to do so. If
28 : you do not wish to do so, delete this exception statement from
29 : your version.
30 : */
31 :
32 : #ifdef HAVE_CONFIG_H
33 : #include "config.h"
34 : #endif
35 :
36 : #include "t-support.h"
37 :
38 : #include <QTest>
39 :
40 : #include <QProcess>
41 : #include <QCoreApplication>
42 : #include <QObject>
43 : #include <QDir>
44 :
45 8 : void QGpgMETest::initTestCase()
46 : {
47 16 : const QString gpgHome = qgetenv("GNUPGHOME");
48 8 : QVERIFY2(!gpgHome.isEmpty(), "GNUPGHOME environment variable is not set.");
49 : }
50 :
51 8 : void QGpgMETest::cleanupTestCase()
52 : {
53 8 : QCoreApplication::sendPostedEvents();
54 8 : killAgent();
55 8 : }
56 :
57 2 : bool QGpgMETest::copyKeyrings(const QString &src, const QString &dest)
58 : {
59 4 : bool is21dir = QFileInfo(src + QDir::separator() + QStringLiteral("pubring.kbx")).exists();
60 2 : const QString name = is21dir ? QStringLiteral("pubring.kbx") :
61 4 : QStringLiteral("pubring.gpg");
62 2 : if (!QFile::copy(src + QDir::separator() + name, dest + QDir::separator() + name)) {
63 0 : return false;
64 : }
65 2 : if (!is21dir) {
66 0 : return (QFile::copy(src + QDir::separator() + QStringLiteral("secring.gpg"),
67 0 : dest + QDir::separator() + QStringLiteral("secring.gpg")));
68 : }
69 6 : QDir dir (src + QDir::separator() + QStringLiteral("private-keys-v1.d"));
70 4 : QDir target(dest);
71 2 : if (!target.mkdir("private-keys-v1.d")) {
72 0 : return false;
73 : }
74 10 : foreach (QString f, dir.entryList(QDir::Files)) {
75 16 : if (!QFile::copy(dir.path() + QDir::separator() + f,
76 16 : dest + QDir::separator() +
77 40 : QStringLiteral("private-keys-v1.d") + QDir::separator() + f)) {
78 0 : return false;
79 : }
80 : }
81 2 : return true;
82 : }
83 :
84 9 : void killAgent(const QString& dir)
85 : {
86 18 : QProcess proc;
87 18 : proc.setProgram(QStringLiteral("gpg-connect-agent"));
88 18 : QStringList arguments;
89 9 : arguments << "-S " << dir + "/S.gpg-agent";
90 9 : proc.start();
91 9 : proc.waitForStarted();
92 9 : proc.write("KILLAGENT\n");
93 9 : proc.write("BYE\n");
94 9 : proc.closeWriteChannel();
95 9 : proc.waitForFinished();
96 9 : }
97 :
98 :
99 : #include "t-support.hmoc"
|