aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/utils/deviceshell/tst_deviceshell.cpp
blob: ceba7a90eefae4855ed8e375b37004fe01f74f3f (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include <app/app_version.h>

#include <utils/deviceshell.h>
#include <utils/environment.h>
#include <utils/hostosinfo.h>
#include <utils/launcherinterface.h>
#include <utils/mapreduce.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h>
#include <utils/temporarydirectory.h>

#include <QObject>
#include <QtTest>

using namespace Utils;

class TestShell : public DeviceShell
{
public:
    TestShell(CommandLine cmdLine, bool failScript = false)
        : DeviceShell(failScript)
        , m_cmdLine(std::move(cmdLine))
    {
        start();
    }

private:
    void setupShellProcess(QtcProcess *shellProcess) override
    {
        shellProcess->setCommand(m_cmdLine);
    }

    CommandLine m_cmdLine;
};

bool testDocker(const FilePath &executable)
{
    QtcProcess p;
    p.setCommand({executable, {"info", "--format", "{{.OSType}}"}});
    p.runBlocking();
    const QString platform = p.cleanedStdOut().trimmed();
    return p.result() == ProcessResult::FinishedWithSuccess && platform == "linux";
}

class tst_DeviceShell : public QObject
{
    Q_OBJECT
private:
    QByteArray m_asciiTestData{256, Qt::Uninitialized};

    QList<CommandLine> m_availableShells;
    bool m_dockerSetupCheckOk{false};

private:
    QString testString(int length)
    {
        QRandomGenerator generator;
        QString result;
        for (int i = 0; i < length; ++i)
            result.append(QChar{generator.bounded('a', 'z')});

        return result;
    }

private slots:
    void initTestCase()
    {
        TemporaryDirectory::setMasterTemporaryDirectory(
            QDir::tempPath() + "/" + Core::Constants::IDE_CASED_ID + "-XXXXXX");

        const QString libExecPath(qApp->applicationDirPath() + '/'
                                  + QLatin1String(TEST_RELATIVE_LIBEXEC_PATH));
        LauncherInterface::setPathToLauncher(libExecPath);

        std::iota(m_asciiTestData.begin(), m_asciiTestData.end(), 0);

        const FilePath dockerExecutable = Environment::systemEnvironment()
                                              .searchInPath("docker", {"/usr/local/bin"});

        if (dockerExecutable.exists()) {
            m_availableShells.append({dockerExecutable, {"run", "-i", "--rm", "alpine"}});
            if (testDocker(dockerExecutable)) {
                m_dockerSetupCheckOk = true;
            } else {
                // On linux, docker needs some post-install steps: https://docs.docker.com/engine/install/linux-postinstall/
                // Also check if you can start a simple container from the command line: "docker run -it alpine".
                qWarning() << "Checking docker failed, tests will be skipped.";
            }
        }

        // On older versions of macOS, base64 does not understand "-d" ( only "-D" ), so we skip the tests here.
        const QVersionNumber osVersionNumber = QVersionNumber::fromString(
            QSysInfo::productVersion());

        const bool isOldMacOS = Utils::HostOsInfo::isMacHost()
                                && osVersionNumber.majorVersion() <= 10
                                && osVersionNumber.minorVersion() <= 14;

        if (isOldMacOS)
            qWarning() << "Skipping local tests, as macOS version is <= 10.14";

        if (!Utils::HostOsInfo::isWindowsHost() && !isOldMacOS) {
            // Windows by default has bash.exe, which does not work unless a working wsl is installed.
            // Therefore we only test shells on linux / mac hosts.
            const auto shells = {"dash", "bash", "sh", "zsh"};

            for (const auto &shell : shells) {
                const FilePath executable = Environment::systemEnvironment()
                                                .searchInPath(shell, {"/usr/local/bin"});
                if (executable.exists())
                    m_availableShells.append({executable, {}});
            }
        }

        if (m_availableShells.isEmpty()) {
            QSKIP("Skipping deviceshell tests, as no compatible shell could be found");
        }
    }

    void cleanupTestCase() { Singleton::deleteAll(); }

    void testArguments_data()
    {
        QTest::addColumn<CommandLine>("cmdLine");
        QTest::addColumn<QString>("testData");

        for (const auto &cmdLine : std::as_const(m_availableShells)) {
            QTest::newRow((cmdLine.executable().baseName() + " : simple").toUtf8())
                << cmdLine << "Hallo Welt!";
            QTest::newRow((cmdLine.executable().baseName() + " : japanese").toUtf8())
                << cmdLine
                << QString::fromUtf8(u8"\xe8\xac\x9d\xe3\x81\x8d\xe3\x82\x81\xe9\x80\x80\x31\x30"
                                     u8"\xe8\x89\xaf\xe3\x81\x9a\xe3"
                                     u8"\x82\xa4\xe3\x81\xb5\xe3\x81\x8b\xe7\x89\x88\xe8\x84\xb3"
                                     u8"\xe3\x83\xa9\xe3\x83\xaf\xe6"
                                     u8"\xad\xa2\xe9\x80\x9a\xe3\x83\xa8\xe3\x83\xb2\xe3\x82\xad");
            QTest::newRow((cmdLine.executable().baseName() + " : german").toUtf8())
                << cmdLine
                << QString::fromUtf8(u8"\x48\x61\x6c\x6c\xc3\xb6\x2c\x20\x77\x69\x65\x20\x67\xc3"
                                     u8"\xa4\x68\x74\x20\x65\x73\x20"
                                     u8"\x64\xc3\xbc\x72");

            QTest::newRow((cmdLine.executable().baseName() + " : long").toUtf8())
                << cmdLine << testString(4096 * 16);
        }
    }

    void testArguments()
    {
        QFETCH(CommandLine, cmdLine);
        QFETCH(QString, testData);

        if (cmdLine.executable().toString().contains("docker") && !m_dockerSetupCheckOk) {
            QSKIP("Docker was found, but does not seem to be set up correctly, skipping.");
        }

        TestShell shell(cmdLine);
        QCOMPARE(shell.state(), DeviceShell::State::Succeeded);

        QRandomGenerator generator;

        const RunResult result = shell.runInShell({"echo", {testData}});
        QCOMPARE(result.exitCode, 0);
        const QString expected = testData + "\n";
        const QString resultAsUtf8 = QString::fromUtf8(result.stdOut);
        QCOMPARE(resultAsUtf8.size(), expected.size());
        QCOMPARE(resultAsUtf8, expected);
    }

    void testStdin_data()
    {
        QTest::addColumn<CommandLine>("cmdLine");
        QTest::addColumn<QString>("testData");

        for (const auto &cmdLine : std::as_const(m_availableShells)) {
            QTest::newRow((cmdLine.executable().baseName() + " : simple").toUtf8())
                << cmdLine << "Hallo Welt!";
            QTest::newRow((cmdLine.executable().baseName() + " : japanese").toUtf8())
                << cmdLine
                << QString::fromUtf8(u8"\xe8\xac\x9d\xe3\x81\x8d\xe3\x82\x81\xe9\x80\x80\x31\x30"
                                     u8"\xe8\x89\xaf\xe3\x81\x9a\xe3"
                                     u8"\x82\xa4\xe3\x81\xb5\xe3\x81\x8b\xe7\x89\x88\xe8\x84\xb3"
                                     u8"\xe3\x83\xa9\xe3\x83\xaf\xe6"
                                     u8"\xad\xa2\xe9\x80\x9a\xe3\x83\xa8\xe3\x83\xb2\xe3\x82\xad");
            QTest::newRow((cmdLine.executable().baseName() + " : german").toUtf8())
                << cmdLine
                << QString::fromUtf8(u8"\x48\x61\x6c\x6c\xc3\xb6\x2c\x20\x77\x69\x65\x20\x67\xc3"
                                     u8"\xa4\x68\x74\x20\x65\x73\x20"
                                     u8"\x64\xc3\xbc\x72");

            QTest::newRow((cmdLine.executable().baseName() + " : long").toUtf8())
                << cmdLine << testString(4096 * 16);
        }
    }

    void testStdin()
    {
        QFETCH(CommandLine, cmdLine);
        QFETCH(QString, testData);

        if (cmdLine.executable().toString().contains("docker") && !m_dockerSetupCheckOk) {
            QSKIP("Docker was found, but does not seem to be set up correctly, skipping.");
        }

        TestShell shell(cmdLine);
        QCOMPARE(shell.state(), DeviceShell::State::Succeeded);

        QRandomGenerator generator;

        const RunResult result = shell.runInShell({"cat", {}}, testData.toUtf8());
        QCOMPARE(result.exitCode, 0);
        const QString resultAsUtf8 = QString::fromUtf8(result.stdOut);
        QCOMPARE(resultAsUtf8.size(), testData.size());
        QCOMPARE(resultAsUtf8, testData);
    }

    void testAscii_data()
    {
        QTest::addColumn<CommandLine>("cmdLine");
        for (const auto &cmdLine : std::as_const(m_availableShells)) {
            QTest::newRow(cmdLine.executable().baseName().toUtf8()) << cmdLine;
        }
    }

    void testAscii()
    {
        QFETCH(CommandLine, cmdLine);

        if (cmdLine.executable().toString().contains("docker") && !m_dockerSetupCheckOk) {
            QSKIP("Docker was found, but does not seem to be set up correctly, skipping.");
        }

        TestShell shell(cmdLine);
        QCOMPARE(shell.state(), DeviceShell::State::Succeeded);

        const RunResult result = shell.runInShell({"cat", {}}, m_asciiTestData);
        QCOMPARE(result.stdOut, m_asciiTestData);
    }

    void testStdErr_data()
    {
        QTest::addColumn<CommandLine>("cmdLine");
        for (const auto &cmdLine : m_availableShells) {
            QTest::newRow(cmdLine.executable().baseName().toUtf8()) << cmdLine;
        }
    }

    void testStdErr()
    {
        QFETCH(CommandLine, cmdLine);

        if (cmdLine.executable().toString().contains("docker") && !m_dockerSetupCheckOk) {
            QSKIP("Docker was found, but does not seem to be set up correctly, skipping.");
        }

        TestShell shell(cmdLine);
        QCOMPARE(shell.state(), DeviceShell::State::Succeeded);

        const RunResult result = shell.runInShell({"cat", {}}, m_asciiTestData);
        QCOMPARE(result.stdOut, m_asciiTestData);
        QVERIFY(result.stdErr.isEmpty());

        const RunResult result2 = shell.runInShell(
            {"cat", {"/tmp/i-do-not-exist.none"}});
        QVERIFY(!result2.stdErr.isEmpty());
        QVERIFY(result2.exitCode != 0);
    }

    void testNoCommand_data()
    {
        QTest::addColumn<CommandLine>("cmdLine");
        for (const auto &cmdLine : m_availableShells) {
            QTest::newRow(cmdLine.executable().baseName().toUtf8()) << cmdLine;
        }
    }

    void testNoCommand()
    {
        QFETCH(CommandLine, cmdLine);

        if (cmdLine.executable().toString().contains("docker") && !m_dockerSetupCheckOk) {
            QSKIP("Docker was found, but does not seem to be set up correctly, skipping.");
        }

        TestShell shell(cmdLine);
        QCOMPARE(shell.state(), DeviceShell::State::Succeeded);

        const RunResult result = shell.runInShell({}, {});

        QVERIFY(result.exitCode == 255);
    }

    void testMultiThreadedFind_data()
    {
        QTest::addColumn<CommandLine>("cmdLine");
        for (const auto &cmdLine : m_availableShells) {
            QTest::newRow(cmdLine.executable().baseName().toUtf8()) << cmdLine;
        }
    }

    void testMultiThreadedFind()
    {
        QFETCH(CommandLine, cmdLine);

        if (cmdLine.executable().toString().contains("docker") && !m_dockerSetupCheckOk) {
            QSKIP("Docker was found, but does not seem to be set up correctly, skipping.");
        }

        TestShell shell(cmdLine);
        QCOMPARE(shell.state(), DeviceShell::State::Succeeded);

        QList<int> runs{1,2,3,4,5,6,7,8,9};

        int maxDepth = 4;
        int numMs = 0;

        while (true) {
            QElapsedTimer t;
            t.start();
            RunResult result = shell.runInShell({"find", {"/usr", "-maxdepth", QString::number(maxDepth)}});
            numMs = t.elapsed();
            qDebug() << "adjusted maxDepth" << maxDepth << "took" << numMs << "ms";
            if (numMs < 100 || maxDepth == 1) {
                break;
            }
            maxDepth--;
        }

        QList<QByteArray> results = Utils::mapped<QList>(runs, [&shell, maxDepth](const int i) -> QByteArray{
            QElapsedTimer t;
            t.start();
            RunResult result = shell.runInShell({"find", {"/usr", "-maxdepth", QString::number(maxDepth)}});
            qDebug() << i << "took" << t.elapsed() << "ms";
            return result.stdOut;
        });

        QVERIFY (!Utils::anyOf(results, [&results](const QByteArray r){ return r != results[0]; }));
    }

    void testNoScript_data()
    {
        QTest::addColumn<CommandLine>("cmdLine");
        for (const auto &cmdLine : m_availableShells) {
            QTest::newRow(cmdLine.executable().baseName().toUtf8()) << cmdLine;
        }
    }

    void testNoScript()
    {
        QFETCH(CommandLine, cmdLine);

        TestShell shell(cmdLine, true);
        QCOMPARE(shell.state(), DeviceShell::State::NoScript);

        const RunResult result = shell.runInShell({"echo", {"Hello"}});
        QCOMPARE(result.exitCode, 0);
    }
};

QTEST_GUILESS_MAIN(tst_DeviceShell)

#include "tst_deviceshell.moc"