aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/remotelinux/linuxdevicetester.cpp
blob: 7b7c57895d27fa03e85888a3e3e5c76fe3163a3b (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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.
**
****************************************************************************/

#include "linuxdevicetester.h"

#include "remotelinux_constants.h"
#include "remotelinuxtr.h"

#include <projectexplorer/devicesupport/deviceusedportsgatherer.h>
#include <projectexplorer/devicesupport/filetransfer.h>

#include <utils/algorithm.h>
#include <utils/port.h>
#include <utils/processinterface.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>

using namespace ProjectExplorer;
using namespace Utils;

namespace RemoteLinux {
namespace Internal {
namespace {

enum State { Inactive,
             TestingEcho,
             TestingUname,
             TestingPorts,
             TestingSftp,
             TestingRsync,
             TestingCommands };

} // anonymous namespace

class GenericLinuxDeviceTesterPrivate
{
public:
    IDevice::Ptr device;
    QtcProcess echoProcess;
    QtcProcess unameProcess;
    DeviceUsedPortsGatherer portsGatherer;
    FileTransfer fileTransfer;
    State state = Inactive;
    bool sftpWorks = false;
    int currentCommandIndex = 0;
    bool commandFailed = false;
    QtcProcess commandsProcess;
};

const QStringList s_commandsToTest = {"base64",
                                      "cat",
                                      "chmod",
                                      "cp",
                                      "cut",
                                      "dd",
                                      "df",
                                      "echo",
                                      "eval",
                                      "exit",
                                      "kill",
                                      "ls",
                                      "mkdir",
                                      "mkfifo",
                                      "mktemp",
                                      "mv",
                                      "printf",
                                      "read",
                                      "readlink",
                                      "rm",
                                      "sed",
                                      "sh",
                                      "shift",
                                      "stat",
                                      "tail",
                                      "test",
                                      "trap",
                                      "touch",
                                      "which"};
// other possible commands (checked for qnx):
// "awk", "grep", "netstat", "print", "pidin", "sleep", "uname"

} // namespace Internal

using namespace Internal;

GenericLinuxDeviceTester::GenericLinuxDeviceTester(QObject *parent)
    : DeviceTester(parent), d(new GenericLinuxDeviceTesterPrivate)
{
    connect(&d->echoProcess, &QtcProcess::done, this,
            &GenericLinuxDeviceTester::handleEchoDone);
    connect(&d->unameProcess, &QtcProcess::done, this,
            &GenericLinuxDeviceTester::handleUnameDone);
    connect(&d->portsGatherer, &DeviceUsedPortsGatherer::error,
            this, &GenericLinuxDeviceTester::handlePortsGathererError);
    connect(&d->portsGatherer, &DeviceUsedPortsGatherer::portListReady,
            this, &GenericLinuxDeviceTester::handlePortsGathererDone);
    connect(&d->fileTransfer, &FileTransfer::done,
            this, &GenericLinuxDeviceTester::handleFileTransferDone);
    connect(&d->commandsProcess, &QtcProcess::done,
            this, &GenericLinuxDeviceTester::handleCommandDone);
}

GenericLinuxDeviceTester::~GenericLinuxDeviceTester() = default;

void GenericLinuxDeviceTester::testDevice(const IDevice::Ptr &deviceConfiguration)
{
    QTC_ASSERT(d->state == Inactive, return);

    d->device = deviceConfiguration;

    testEcho();
}

void GenericLinuxDeviceTester::stopTest()
{
    QTC_ASSERT(d->state != Inactive, return);

    switch (d->state) {
    case TestingEcho:
        d->echoProcess.close();
        break;
    case TestingPorts:
        d->portsGatherer.stop();
        break;
    case TestingUname:
        d->unameProcess.close();
        break;
    case TestingSftp:
    case TestingRsync:
        d->fileTransfer.stop();
        break;
    case TestingCommands:
        d->commandsProcess.close();
        break;
    case Inactive:
        break;
    }

    setFinished(TestFailure);
}

static const char s_echoContents[] = "Hello Remote World!";

void GenericLinuxDeviceTester::testEcho()
{
    d->state = TestingEcho;
    emit progressMessage(Tr::tr("Sending echo to device..."));

    d->echoProcess.setCommand({d->device->filePath("echo"), {s_echoContents}});
    d->echoProcess.start();
}

void GenericLinuxDeviceTester::handleEchoDone()
{
    QTC_ASSERT(d->state == TestingEcho, return);
    if (d->echoProcess.result() != ProcessResult::FinishedWithSuccess) {
        const QByteArray stdErrOutput = d->echoProcess.readAllStandardError();
        if (!stdErrOutput.isEmpty())
            emit errorMessage(Tr::tr("echo failed: %1").arg(QString::fromUtf8(stdErrOutput)) + '\n');
        else
            emit errorMessage(Tr::tr("echo failed.") + '\n');
        setFinished(TestFailure);
        return;
    }

    const QString reply = d->echoProcess.cleanedStdOut().chopped(1); // Remove trailing \n
    if (reply != s_echoContents)
        emit errorMessage(Tr::tr("Device replied to echo with unexpected contents.") + '\n');
    else
        emit progressMessage(Tr::tr("Device replied to echo with expected contents.") + '\n');

    testUname();
}

void GenericLinuxDeviceTester::testUname()
{
    d->state = TestingUname;
    emit progressMessage(Tr::tr("Checking kernel version..."));

    d->unameProcess.setCommand({d->device->filePath("uname"), {"-rsm"}});
    d->unameProcess.start();
}

void GenericLinuxDeviceTester::handleUnameDone()
{
    QTC_ASSERT(d->state == TestingUname, return);

    if (!d->unameProcess.errorString().isEmpty() || d->unameProcess.exitCode() != 0) {
        const QByteArray stderrOutput = d->unameProcess.readAllStandardError();
        if (!stderrOutput.isEmpty())
            emit errorMessage(Tr::tr("uname failed: %1").arg(QString::fromUtf8(stderrOutput)) + QLatin1Char('\n'));
        else
            emit errorMessage(Tr::tr("uname failed.") + QLatin1Char('\n'));
    } else {
        emit progressMessage(QString::fromUtf8(d->unameProcess.readAllStandardOutput()));
    }

    testPortsGatherer();
}

void GenericLinuxDeviceTester::testPortsGatherer()
{
    d->state = TestingPorts;
    emit progressMessage(Tr::tr("Checking if specified ports are available..."));

    d->portsGatherer.start(d->device);
}

void GenericLinuxDeviceTester::handlePortsGathererError(const QString &message)
{
    QTC_ASSERT(d->state == TestingPorts, return);

    emit errorMessage(Tr::tr("Error gathering ports: %1").arg(message) + QLatin1Char('\n'));
    setFinished(TestFailure);
}

void GenericLinuxDeviceTester::handlePortsGathererDone()
{
    QTC_ASSERT(d->state == TestingPorts, return);

    if (d->portsGatherer.usedPorts().isEmpty()) {
        emit progressMessage(Tr::tr("All specified ports are available.") + QLatin1Char('\n'));
    } else {
        const QString portList = transform(d->portsGatherer.usedPorts(), [](const Port &port) {
            return QString::number(port.number());
        }).join(", ");
        emit errorMessage(Tr::tr("The following specified ports are currently in use: %1")
            .arg(portList) + QLatin1Char('\n'));
    }

    testFileTransfer(FileTransferMethod::Sftp);
}

void GenericLinuxDeviceTester::testFileTransfer(FileTransferMethod method)
{
    switch (method) {
    case FileTransferMethod::Sftp:  d->state = TestingSftp;  break;
    case FileTransferMethod::Rsync: d->state = TestingRsync; break;
    }
    emit progressMessage(Tr::tr("Checking whether \"%1\" works...")
                         .arg(FileTransfer::transferMethodName(method)));

    d->fileTransfer.setTransferMethod(method);
    d->fileTransfer.test(d->device);
}

void GenericLinuxDeviceTester::handleFileTransferDone(const ProcessResultData &resultData)
{
    QTC_ASSERT(d->state == TestingSftp || d->state == TestingRsync, return);

    bool succeeded = false;
    QString error;
    const QString methodName = FileTransfer::transferMethodName(d->fileTransfer.transferMethod());
    if (resultData.m_error == QProcess::FailedToStart) {
        error = Tr::tr("Failed to start \"%1\": %2\n").arg(methodName, resultData.m_errorString);
    } else if (resultData.m_exitStatus == QProcess::CrashExit) {
        error = Tr::tr("\"%1\" crashed.\n").arg(methodName);
    } else if (resultData.m_exitCode != 0) {
        error = Tr::tr("\"%1\" failed with exit code %2: %3\n")
                .arg(methodName).arg(resultData.m_exitCode).arg(resultData.m_errorString);
    } else {
        succeeded = true;
    }

    if (succeeded)
        emit progressMessage(Tr::tr("\"%1\" is functional.\n").arg(methodName));
    else
        emit errorMessage(error);

    if (d->state == TestingSftp) {
        d->sftpWorks = succeeded;
        testFileTransfer(FileTransferMethod::Rsync);
    } else {
        if (!succeeded) {
            if (d->sftpWorks) {
                emit progressMessage(Tr::tr("SFTP will be used for deployment, because rsync "
                                        "is not available.\n"));
            } else {
                emit errorMessage(Tr::tr("Deployment to this device will not work out of the box.\n"));
            }
        }
        d->device->setExtraData(Constants::SupportsRSync, succeeded);
        if (d->sftpWorks || succeeded)
            testCommands();
        else
            setFinished(TestFailure);
    }
}

void GenericLinuxDeviceTester::testCommands()
{
    d->state = TestingCommands;
    emit progressMessage(Tr::tr("Checking if required commands are available..."));

    d->currentCommandIndex = 0;
    d->commandFailed = false;
    testNextCommand();
}

void GenericLinuxDeviceTester::testNextCommand()
{
    d->commandsProcess.close();
    if (s_commandsToTest.size() == d->currentCommandIndex) {
        setFinished(d->commandFailed ? TestFailure : TestSuccess);
        return;
    }

    const QString commandName = s_commandsToTest[d->currentCommandIndex];
    emit progressMessage(Tr::tr("%1...").arg(commandName));
    CommandLine command{d->device->filePath("/bin/sh"), {"-c"}};
    command.addArgs(QLatin1String("\"command -v %1\"").arg(commandName), CommandLine::Raw);
    d->commandsProcess.setCommand(command);
    d->commandsProcess.start();
}

void GenericLinuxDeviceTester::handleCommandDone()
{
    QTC_ASSERT(d->state == TestingCommands, return);

    const QString command = s_commandsToTest[d->currentCommandIndex];
    if (d->commandsProcess.result() == ProcessResult::FinishedWithSuccess) {
        emit progressMessage(Tr::tr("%1 found.").arg(command));
    } else {
        d->commandFailed = true;
        const QString message = d->commandsProcess.result() == ProcessResult::StartFailed
                ? Tr::tr("An error occurred while checking for %1.").arg(command)
                  + '\n' + d->commandsProcess.errorString()
                : Tr::tr("%1 not found.").arg(command);
        emit errorMessage(message);
    }

    ++d->currentCommandIndex;
    testNextCommand();
}

void GenericLinuxDeviceTester::setFinished(TestResult result)
{
    d->state = Inactive;
    emit finished(result);
}

} // namespace RemoteLinux