summaryrefslogtreecommitdiffstats
path: root/tests/auto/installer/copyoperationtest/tst_copyoperationtest.cpp
blob: 1f42bcad25995a552af57f72874a5d00e0e67c58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
**************************************************************************/

#include <init.h>
#include <updateoperations.h>
#include <utils.h>
#include <binarycontent.h>
#include <packagemanagercore.h>
#include <settings.h>
#include <fileutils.h>

#include <QDir>
#include <QObject>
#include <QTest>
#include <QFile>
#include <QDebug>

using namespace KDUpdater;
using namespace QInstaller;

class tst_copyoperationtest : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase()
    {
        //QInstaller::init();
        m_testDestinationPath = qApp->applicationDirPath() + QDir::toNativeSeparators("/test");
        m_testDestinationFilePath = QDir(m_testDestinationPath).absoluteFilePath(QFileInfo(
            qApp->applicationFilePath()).fileName());
        if (QDir(m_testDestinationPath).exists()) {
            QFAIL("Remove test folder first!");
        }
    }

    void testMissingArguments()
    {
        CopyOperation op;

        QVERIFY(op.testOperation());
        QVERIFY(!op.performOperation());

        QCOMPARE(UpdateOperation::Error(op.error()), UpdateOperation::InvalidArguments);
        QCOMPARE(op.errorString(), QString("Invalid arguments in Copy: "
                                           "0 arguments given, exactly 2 arguments expected."));

    }

    void testCopySomething_data()
    {
         QTest::addColumn<QString>("source");
         QTest::addColumn<QString>("destination");
         QTest::newRow("full path syntax") << qApp->applicationFilePath() << m_testDestinationFilePath;
         QTest::newRow("short destination syntax") << qApp->applicationFilePath() << m_testDestinationPath;
         QTest::newRow("short destination syntax with ending separator") << qApp->applicationFilePath()
            << m_testDestinationPath + QDir::separator();
    }

    void testCopySomething()
    {
        QFETCH(QString, source);
        QFETCH(QString, destination);

        QVERIFY2(QFileInfo(source).exists(), QString("Source file \"%1\" does not exist.").arg(source).toLatin1());
        CopyOperation op;
        op.setArguments(QStringList() << source << destination);
        op.backup();
        QVERIFY2(op.performOperation(), op.errorString().toLatin1());

        QVERIFY2(QFileInfo(m_testDestinationFilePath).exists(), QString("Copying from \"%1\" to \"%2\" was "
            "not working: '%3' does not exist").arg(source, destination, m_testDestinationFilePath).toLatin1());
        QVERIFY2(op.undoOperation(), op.errorString().toLatin1());
        QVERIFY2(!QFileInfo(m_testDestinationFilePath).exists(), QString("Undo of copying from \"%1\" to "
            "\"%2\" was not working.").toLatin1());
    }

    void testCopyIfDestinationExist_data()
    {
        testCopySomething_data();
    }

    void testCopyIfDestinationExist()
    {
        QFETCH(QString, source);
        QFETCH(QString, destination);

        QByteArray testString("This file is generated by QTest\n");
        QFile testFile(m_testDestinationFilePath);
        testFile.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream out(&testFile);
        out << testString;
        testFile.close();

        QByteArray testFileHash = QInstaller::calculateHash(m_testDestinationFilePath, QCryptographicHash::Sha1);

        QVERIFY2(QFileInfo(source).exists(), QString("Source file \"%1\" does not exist.").arg(source).toLatin1());
        CopyOperation op;
        op.setArguments(QStringList() << source << destination);
        op.backup();
        QVERIFY2(!op.value("backupOfExistingDestination").toString().isEmpty(), "The CopyOperation didn't saved any backup.");
        QVERIFY2(op.performOperation(), op.errorString().toLatin1());

        // checking that perform did something
        QByteArray currentFileHash = QInstaller::calculateHash(m_testDestinationFilePath, QCryptographicHash::Sha1);
        QVERIFY(testFileHash != currentFileHash);

        QVERIFY2(QFileInfo(m_testDestinationFilePath).exists(), QString("Copying from \"%1\" to \"%2\" was "
            "not working: \"%3\" does not exist").arg(source, destination, m_testDestinationFilePath).toLatin1());

        // undo should replace the new one with the old backuped one
        QVERIFY2(op.undoOperation(), op.errorString().toLatin1());
        currentFileHash = QInstaller::calculateHash(m_testDestinationFilePath, QCryptographicHash::Sha1);
        QVERIFY(testFileHash == currentFileHash);
    }

    void testPerformingFromCLI()
    {
        QInstaller::init(); //This will eat debug output
        PackageManagerCore *core = new PackageManagerCore(BinaryContent::MagicInstallerMarker, QList<OperationBlob> ());
        core->setAllowedRunningProcesses(QStringList() << QCoreApplication::applicationFilePath());
        QSet<Repository> repoList;
        Repository repo = Repository::fromUserInput(":///data/repository");
        repoList.insert(repo);
        core->settings().setDefaultRepositories(repoList);

        QString installDir = QInstaller::generateTemporaryFileName();
        QDir().mkpath(installDir);
        core->setValue(scTargetDir, installDir);
        core->installDefaultComponentsSilently();

        QFile copiedFile(installDir + QDir::separator() + "AnotherFolder/A.txt");
        QVERIFY(copiedFile.exists());
        QFile originalFile(installDir + QDir::separator() + "A.txt");
        QVERIFY(originalFile.exists());

        QByteArray destinationFileHash = QInstaller::calculateHash(copiedFile.fileName(), QCryptographicHash::Sha1);
        QByteArray testFileHash = QInstaller::calculateHash(originalFile.fileName(), QCryptographicHash::Sha1);
        QVERIFY(testFileHash == destinationFileHash);

        core->setPackageManager();
        core->commitSessionOperations();
        core->uninstallComponentsSilently(QStringList() << "A");
        QVERIFY(!copiedFile.exists());

        QDir dir(installDir);
        QVERIFY(dir.removeRecursively());
        core->deleteLater();
    }

    void init()
    {
        QVERIFY2(!QFileInfo(m_testDestinationFilePath).exists(), QString("Destination \"%1\" should not exist "
            "to test the copy operation.").arg(m_testDestinationFilePath).toLatin1());
        QDir().mkpath(m_testDestinationPath);
    }

    void cleanup()
    {
        QFile(m_testDestinationFilePath).remove();
        QDir().rmpath(m_testDestinationPath);
    }
private:
    QString m_testDestinationPath;
    QString m_testDestinationFilePath;
};

QTEST_MAIN(tst_copyoperationtest)

#include "tst_copyoperationtest.moc"