~ubuntu-branches/ubuntu/precise/kde4libs/precise-security

1.1.2 by Jonathan Riddell
Import upstream version 3.91.0
1
/*
2
 *  Copyright (C) 2006 David Faure   <faure@kde.org>
3
 *
4
 *  This library is free software; you can redistribute it and/or
5
 *  modify it under the terms of the GNU Library General Public
6
 *  License version 2 as published by the Free Software Foundation;
7
 *
8
 *  This library is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 *  Library General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU Library General Public License
14
 *  along with this library; see the file COPYING.LIB.  If not, write to
15
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
 *  Boston, MA 02110-1301, USA.
17
 */
18
#include "kdesktopfiletest.h"
1.1.5 by Jonathan Riddell
Import upstream version 3.94.0
19
#include <kconfiggroup.h>
20
#include <ktemporaryfile.h>
1.1.30 by Jonathan Riddell
Import upstream version 4.2.85
21
#include <kstandarddirs.h>
1.1.2 by Jonathan Riddell
Import upstream version 3.91.0
22
#include "kdesktopfiletest.moc"
23
24
#include "kdesktopfile.h"
25
26
#include <qtest_kde.h>
27
28
QTEST_KDEMAIN_CORE( KDesktopFileTest )
29
30
void KDesktopFileTest::testRead()
31
{
32
    const QString fileName = QFile::decodeName(KDESRCDIR "/../services/kplugininfo.desktop");
33
    QVERIFY(QFile::exists(fileName));
34
    QVERIFY(KDesktopFile::isDesktopFile(fileName));
35
    KDesktopFile df(fileName);
36
    QCOMPARE(df.readType(), QString::fromLatin1("ServiceType"));
37
    QCOMPARE(df.readIcon(), QString());
38
    QCOMPARE(df.readName(), QString::fromLatin1("KDE Plugin Information"));
39
    QCOMPARE(df.hasLinkType(), false);
40
    QCOMPARE(df.hasMimeTypeType(), false);
41
    QCOMPARE(df.hasApplicationType(), false);
1.13.1 by Jonathan Riddell
Import upstream version 4.5.90
42
    QCOMPARE(df.fileName(), KStandardDirs::realFilePath(fileName));
1.1.5 by Jonathan Riddell
Import upstream version 3.94.0
43
    QCOMPARE(df.resource(), "apps"); // default for .desktop files
44
}
45
1.14.6 by Philip Muškovac
Import upstream version 4.6.90
46
void KDesktopFileTest::testSuccessfulTryExec()
47
{
48
    KTemporaryFile file;
49
    file.setPrefix("test1");
50
    QVERIFY( file.open() );
51
    const QString fileName = file.fileName();
52
    QTextStream ts( &file );
53
    ts <<
54
        "[Desktop Entry]\n"
55
        "TryExec=/bin/ls\n"
56
        "\n";
57
    file.close();
58
    QVERIFY(QFile::exists(fileName));
59
    KDesktopFile df(fileName);
60
    QCOMPARE(df.tryExec(), true);
61
}
62
63
void KDesktopFileTest::testUnsuccessfulTryExec()
64
{
65
    KTemporaryFile file;
66
    file.setPrefix("test1");
67
    QVERIFY( file.open() );
68
    const QString fileName = file.fileName();
69
    QTextStream ts( &file );
70
    ts <<
71
        "[Desktop Entry]\n"
72
        "TryExec=/does/not/exist\n"
73
        "\n";
74
    file.close();
75
    QVERIFY(QFile::exists(fileName));
76
    KDesktopFile df(fileName);
77
    QCOMPARE(df.tryExec(), false);
78
}
79
1.1.5 by Jonathan Riddell
Import upstream version 3.94.0
80
void KDesktopFileTest::testActionGroup()
81
{
82
    KTemporaryFile file;
83
    file.setPrefix("test1");
84
    QVERIFY( file.open() );
85
    const QString fileName = file.fileName();
86
    QTextStream ts( &file );
87
    ts <<
88
        "[Desktop Entry]\n"
89
        "Actions=encrypt;\n"
90
        "[Desktop Action encrypt]\n"
91
        "Name=Encrypt file\n"
92
        "\n";
93
    file.close();
94
    QVERIFY(QFile::exists(fileName));
95
    KDesktopFile df(fileName);
96
    QCOMPARE(df.readType(), QString());
97
    QCOMPARE(df.fileName(), fileName);
98
    QCOMPARE(df.readActions(), QStringList() << "encrypt");
99
    QCOMPARE(df.hasActionGroup("encrypt"), true);
100
    QCOMPARE(df.hasActionGroup("doesnotexist"), false);
101
    KConfigGroup cg = df.actionGroup("encrypt");
102
    QVERIFY(cg.hasKey("Name"));
103
    QCOMPARE(cg.readEntry("Name"), QString("Encrypt file"));
1.1.2 by Jonathan Riddell
Import upstream version 3.91.0
104
}
1.1.30 by Jonathan Riddell
Import upstream version 4.2.85
105
106
void KDesktopFileTest::testIsAuthorizedDesktopFile()
107
{
108
    const QString fileName = QFile::decodeName(KDESRCDIR "../../kioslave/http/http_cache_cleaner.desktop");
109
    QVERIFY(QFile::exists(fileName));
110
    QVERIFY(!KDesktopFile::isAuthorizedDesktopFile(fileName));
111
112
    const QString installedFile = KGlobal::dirs()->locate("services", "http_cache_cleaner.desktop");
113
    if (!installedFile.isEmpty()) {
114
        QVERIFY(KDesktopFile::isAuthorizedDesktopFile(installedFile));
115
    } else {
116
        qWarning("Skipping test for http_cache_cleaner.desktop, not found. kdelibs not installed?");
117
    }
118
119
    const QString autostartFile = KStandardDirs::locate("autostart", "plasma-desktop.desktop");
120
    if (!autostartFile.isEmpty()) {
121
        QVERIFY(KDesktopFile::isAuthorizedDesktopFile(autostartFile));
122
    } else {
123
        qWarning("Skipping test for plasma-desktop.desktop, not found. kdebase not installed?");
124
    }
125
126
}