File: smburltest.cpp

package info (click to toggle)
kio-extras 4%3A22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,768 kB
  • sloc: cpp: 26,898; ansic: 4,443; perl: 1,058; xml: 97; sh: 69; python: 28; makefile: 9
file content (156 lines) | stat: -rw-r--r-- 5,839 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
146
147
148
149
150
151
152
153
154
155
156
/*
    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
    SPDX-FileCopyrightText: 2020-2021 Harald Sitter <sitter@kde.org>
*/

#include <QTest>
#include <QAbstractItemModelTester>

#include "smburl.h"

class SMBUrlTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void testMinimalToSmbcValid()
    {
        // libsmbclient is a bit picky. make sure we convert to minimal applicable form
        {
            SMBUrl url(QUrl("smb:/"));
            QCOMPARE(url.toSmbcUrl(), "smb://");
        }

        // But at the same time it will happily deal with smb:
        {
            SMBUrl url(QUrl("smb:"));
            QCOMPARE(url.toSmbcUrl(), "smb:");
        }
    }

    void testType()
    {
        QCOMPARE(SMBUrl(QUrl("smb://")).getType(), SMBURLTYPE_ENTIRE_NETWORK);
        QCOMPARE(SMBUrl(QUrl("smb://host")).getType(), SMBURLTYPE_WORKGROUP_OR_SERVER);
        QCOMPARE(SMBUrl(QUrl("smb://host/share/file")).getType(), SMBURLTYPE_SHARE_OR_PATH);
        QCOMPARE(SMBUrl(QUrl()).getType(), SMBURLTYPE_UNKNOWN);
    }

    void testPart()
    {
        SMBUrl url(QUrl("smb://host/share/file"));
        QCOMPARE(url.partUrl().toString(), "smb://host/share/file.part");
    }

    void testUp()
    {
        SMBUrl url(QUrl("smb://host/share/file"));
        url.cdUp();
        QCOMPARE(url.toSmbcUrl(), "smb://host/share");
    }

    void testAddPath()
    {
        SMBUrl url(QUrl("smb://host/share"));
        url.addPath("file");
        QCOMPARE(url.toSmbcUrl(), "smb://host/share/file");
    }

    void testCifs()
    {
        // We treat cifs as an alias but need to translate it to smb.
        // https://bugs.kde.org/show_bug.cgi?id=327295
        SMBUrl url(QUrl("cifs://host/share/file"));
        QCOMPARE(url.toSmbcUrl(), "smb://host/share/file");
    }

    void testIPv6Literal()
    {
        // https://bugs.kde.org/show_bug.cgi?id=417682
        // Samba cannot deal with RFC5952 IPv6 notation (e.g. ::1%lo)
        // to work around we convert to windows ipv6 literals.

        // The actual represented URL should not change!
        // i.e. towards the KIO client we do not leak the IPv6
        // literal when returning an URL.
        QCOMPARE(SMBUrl(QUrl("smb://[::1]/share")).toString(),
                 "smb://[::1]/share");

        // The internal smbc representation should be literal though:
        // :: prefix
        QCOMPARE(SMBUrl(QUrl("smb://[::1]/share")).toSmbcUrl(),
                 "smb://0--1.ipv6-literal.net/share");
        // :: suffix
        QCOMPARE(SMBUrl(QUrl("smb://[fe80::]/share")).toSmbcUrl(),
                 "smb://fe80--0.ipv6-literal.net/share");
        // %lo scope
        QCOMPARE(SMBUrl(QUrl("smb://[::1%lo]/share")).toSmbcUrl(),
                 "smb://0--1slo.ipv6-literal.net/share");
        // random valid addr
        QCOMPARE(SMBUrl(QUrl("smb://[fe80::9cd7:32c7:faeb:f23d]/share")).toSmbcUrl(),
                 "smb://fe80--9cd7-32c7-faeb-f23d.ipv6-literal.net/share");
    }

    void testWorkgroupWithSpaces()
    {
        // Workgroups can have spaces but QUrls cannot, so we have a hack
        // that puts the workgroup info into a query.
        // Only applicable to SMB1 pretty much, we do not do workgroup browsing
        // for 2+.
        // https://bugs.kde.org/show_bug.cgi?id=204423

        // wg
        QCOMPARE(SMBUrl(QUrl("smb://?kio-workgroup=hax max")).toSmbcUrl(),
                 "smb://hax max/");
        // wg and query
        QCOMPARE(SMBUrl(QUrl("smb://?kio-workgroup=hax max&q=a")).toSmbcUrl(),
                 "smb://hax max/?q=a");
        // host and wg and query
        QCOMPARE(SMBUrl(QUrl("smb://host/?kio-workgroup=hax max&q=a")).toSmbcUrl(),
                 "smb://hax max/host?q=a");
        // host and wg and query
        QCOMPARE(SMBUrl(QUrl("smb://host/share?kio-workgroup=hax max")).toSmbcUrl(),
                 "smb://hax max/host/share");
        // Non-empty path. libsmbc hates unclean paths
        QCOMPARE(SMBUrl(QUrl("smb:///////?kio-workgroup=hax max")).toSmbcUrl(),
                 "smb://hax max/");
        // % character - run through .url() to simulate behavior of our listDir()
        QCOMPARE(SMBUrl(QUrl(QUrl("smb://?kio-workgroup=HAX%25MAX").url())).toSmbcUrl(),
                 "smb://HAX%25MAX/");
        // !ascii - run through .url() to simulate behavior of our listDir()
        QCOMPARE(SMBUrl(QUrl(QUrl("smb:///?kio-workgroup=DOMÄNE A").url())).toSmbcUrl(),
                 "smb://DOMÄNE A/"); // works as-is with smbc.

        // Also make sure type detection knows about this
        QCOMPARE(SMBUrl(QUrl("smb:/?kio-workgroup=hax max")).getType(),
                 SMBURLTYPE_WORKGROUP_OR_SERVER);
    }

    void testNonSmb()
    {
        // In the kdirnotify integration we load arbitrary urls into smburl,
        // make sure they get reported as unknown.
        QCOMPARE(SMBUrl(QUrl("file:///")).getType(), SMBURLTYPE_UNKNOWN);
        QCOMPARE(SMBUrl(QUrl("file:///home/foo/bar")).getType(), SMBURLTYPE_UNKNOWN);
        QCOMPARE(SMBUrl(QUrl("sftp://me@localhost/foo/bar")).getType(), SMBURLTYPE_UNKNOWN);
    }

    void testFileParts()
    {
        // We use SMBUrl for transfers from and to local files as well, make sure it behaves accordingly.
        SMBUrl url(QUrl("file:///foo"));
        QCOMPARE(QUrl("file:///foo"), url);
        QCOMPARE(QUrl("file:///foo.part"), url.partUrl());
        // Clearly not a file should not work
        QCOMPARE(QUrl(), SMBUrl(QUrl("file:///")).partUrl());
    }

    void testPrinter()
    {
        // We tag printers as such cause otherwise we have no way of knowing it was a printer.
        QCOMPARE(SMBUrl(QUrl("smb://host/printer?kio-printer=true")).getType(), SMBURLTYPE_PRINTER);
    }
};

QTEST_GUILESS_MAIN(SMBUrlTest)

#include "smburltest.moc"