File: launcheruiiptest.cpp

package info (click to toggle)
gammaray 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,620 kB
  • sloc: cpp: 94,712; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 32
file content (145 lines) | stat: -rw-r--r-- 5,485 bytes parent folder | download
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
/*
  launcheruiiptest.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Christoph Sterz <christoph.sterz@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include <config-gammaray.h>

#include <launcher/ui/connectpage.h>

#include <QDir>
#include <QLineEdit>
#include <QLocalServer>
#include <QSignalSpy>
#include <QTest>

using namespace GammaRay;

class LauncherUiIPTest : public QObject
{
    Q_OBJECT
private slots:

    void initTestCase()
    {
        // create socket
#ifdef Q_OS_UNIX
        m_localServer.listen("/tmp/socketfile");
#endif
    }

    void cleanupTestCase()
    {
        m_localServer.close();
    }

    static void testUrl_data()
    {
        QTest::addColumn<QString>("userInput", nullptr);
        QTest::addColumn<QString>("expectedParsed", nullptr);
        QTest::addColumn<bool>("isValid", nullptr);
        QTest::addColumn<bool>("autoPortWarning", nullptr);

#ifdef Q_OS_UNIX
        QTest::newRow("/tmp/socketfile") << "/tmp/socketfile"
                                         << "local:/tmp/socketfile" << true << false;
        QTest::newRow("local:///tmp/socketfile") << "local:///tmp/socketfile"
                                                 << "local:/tmp/socketfile" << true << false;
#endif

        QTest::newRow("192.168.42.1") << "192.168.42.1"
                                      << "tcp://192.168.42.1:11732" << true << true;
        QTest::newRow("tcp://192.168.42.1") << "tcp://192.168.42.1"
                                            << "tcp://192.168.42.1:11732" << true << true;
        QTest::newRow("192.168.42") << "192.168.42"
                                    << "" << false << true;
        QTest::newRow("192.168.42.1:2342") << "192.168.42.1:2342"
                                           << "tcp://192.168.42.1:2342" << true << false;
        QTest::newRow("192.168.42.1:66666") << "192.168.42.1:66666"
                                            << "" << false << false;

        QTest::newRow("::1") << "::1"
                             << "tcp://[::1]:11732" << true << true;
        QTest::newRow("fe80::9c0e:f1f4:d51d:a557") << "fe80::9c0e:f1f4:d51d:a557"
                                                   << "tcp://[fe80::9c0e:f1f4:d51d:a557]:11732" << true << true;
        QTest::newRow("fe80::9c0e:f1f4:d51d:a557%enp0s31f6") << "fe80::9c0e:f1f4:d51d:a557%enp0s31f6"
                                                             << "tcp://[fe80::9c0e:f1f4:d51d:a557]:11732" << true << true;
        QTest::newRow("fe80::9c0e:f1f4:d51d:a557%enp0s31f6:2342") << "fe80::9c0e:f1f4:d51d:a557%enp0s31f6:2342"
                                                                  << "tcp://[fe80::9c0e:f1f4:d51d:a557]:2342" << true << false;
        QTest::newRow("[fe80::9c0e:f1f4:d51d:a557]") << "[fe80::9c0e:f1f4:d51d:a557]"
                                                     << "tcp://[fe80::9c0e:f1f4:d51d:a557]:11732" << true << true;
        QTest::newRow("tcp://[fe80::9c0e:f1f4:d51d:a557]:2342") << "tcp://[fe80::9c0e:f1f4:d51d:a557]:2342"
                                                                << "tcp://[fe80::9c0e:f1f4:d51d:a557]:2342" << true << false;
        QTest::newRow("::ffff:192.168.15.2") << "::ffff:192.168.15.2"
                                             << "tcp://[::ffff:192.168.15.2]:11732" << true << true;
    }

    static void testUrl()
    {
        QFETCH(QString, userInput);
        QFETCH(QString, expectedParsed);
        QFETCH(bool, isValid);
        QFETCH(bool, autoPortWarning);

        ConnectPage connectPage;
        QSignalSpy addressParsedSpy(&connectPage, &ConnectPage::userInputParsed);
        auto lineEdit = connectPage.findChild<QLineEdit *>("host");
        QVERIFY(lineEdit);
        lineEdit->setText(userInput);
        for (size_t i = 0; i < 500; i++) {
            if (addressParsedSpy.size() > 0)
                break;
            QTest::qWait(1);
        }
        QCOMPARE(connectPage.isValid(), isValid);
        if (!isValid)
            return;
        QCOMPARE(connectPage.m_currentUrl.toString(), expectedParsed);
        QCOMPARE(lineEdit->actions().contains(connectPage.m_implicitPortWarningSign), autoPortWarning);
    }

    static void testHostNames_data()
    {
        QTest::addColumn<QString>("userInput", nullptr);
        QTest::addColumn<bool>("isValid", nullptr);

        QTest::newRow("localhost") << "localhost" << true;
        QTest::newRow("tcp://localhost") << "tcp://localhost" << true;
        QTest::newRow("tcp://localhost:11732") << "tcp://localhost:11732" << true;
    }

    static void testHostNames()
    {
        QFETCH(QString, userInput);
        QFETCH(bool, isValid);
        ConnectPage connectPage;
        QSignalSpy dnsDoneSpy(&connectPage, &ConnectPage::dnsResolved);
        auto lineEdit = connectPage.findChild<QLineEdit *>("host");
        QVERIFY(lineEdit);
        lineEdit->setText(userInput);
        for (size_t i = 0; i < 1000; i++) {
            if (dnsDoneSpy.size() > 0)
                break;
            QTest::qWait(1);
            if (i == 999)
                return;
        }
        QCOMPARE(connectPage.isValid(), isValid);
    }


private:
    QLocalServer m_localServer;
};

QTEST_MAIN(LauncherUiIPTest)

#include "launcheruiiptest.moc"