summaryrefslogtreecommitdiffstats
path: root/src/network/main.cpp
blob: dd93d1fd32ed656109c1dcf5fb6459e69658ea6c (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "networktest.h"
#include <QCoreApplication>
#include <QCommandLineParser>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QCoreApplication::setApplicationName(NetworkTest::applicationName());
    QCoreApplication::setApplicationVersion(NetworkTest::versionString());

    QCommandLineParser parser;
    parser.addVersionOption();
    parser.addHelpOption();
    const QCommandLineOption inputOption({"input-file", "i"},
                                         "JSON input file to parse", "jsonFile");
    const QCommandLineOption timeoutOption({"timeout", "to", "t"}, "Overall timeout in milliseconds", "timeout");
    const QCommandLineOption warnOnlyOption({"warn-only", "wo"}, "Just warn, exit 0 on error.");
    const QCommandLineOption verbosityOption({"verbosity", "d"}, NetworkTest::verbosityStrings().join("\n"), "verbosity");
    const QCommandLineOption copyOption({"copy-default-file", "o"},
                                        "Write a copy of the default file to the given path",
                                        "file");
    const QCommandLineOption showProgressOption({"show-progress", "p"}, "Show progress");
    const QCommandLineOption fileNameOption({"file-name", "n"}, "Output file name", "extension", "tgz");

    parser.addOption(inputOption);
    parser.addOption(timeoutOption);
    parser.addOption(warnOnlyOption);
    parser.addOption(verbosityOption);
    parser.addOption(copyOption);
    parser.addOption(showProgressOption);
    parser.addOption(fileNameOption);
    parser.process(a);

    if (parser.isSet(fileNameOption)) {
        const QString extension = parser.value(fileNameOption);
        qInfo().noquote() << NetworkTest::packageName(extension);
        return 0;
    }

    constexpr QLatin1StringView defaultFile(":/tests/DNSLookup.json");
    const QString input = parser.isSet(inputOption) ? parser.value(inputOption) : defaultFile;
    const int timeout = parser.value(timeoutOption).toInt();
    const bool warnOnly = parser.isSet(warnOnlyOption);
    const bool showProgress = parser.isSet(showProgressOption);

    NetworkTest::Verbosity verbosity = NetworkTest::Verbosity::Summary;
    if (parser.isSet(verbosityOption)) {
        bool ok;
        const QString vString = parser.value(verbosityOption);
        const int v = vString.toInt(&ok);
        if (ok)
            verbosity = NetworkTest::toVerbosity(v, &ok);
        if (!ok)
            qWarning() << "Illegal verbosity value:" << vString << ". Falling back to" << verbosity;
    }

    if (verbosity != NetworkTest::Verbosity::Silent)
        NetworkTest::printNetworkConfig();

    if (parser.isSet(copyOption)) {
        const QString oFile = parser.value(copyOption);
        if (!QFile::copy(defaultFile, oFile))
            qWarning() << "Could not create" << oFile;
    }

    NetworkTest test(input, warnOnly, showProgress, timeout, verbosity);
    const bool success = test.test();
    return success ? 0 : 1;
}