File indexing completed on 2024-05-19 08:50:36

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003     SPDX-FileCopyrightText: 2020-2021 Harald Sitter <sitter@kde.org>
0004 */
0005 
0006 #include <QAbstractItemModelTester>
0007 #include <QTest>
0008 
0009 #include "smburl.h"
0010 
0011 class SMBUrlTest : public QObject
0012 {
0013     Q_OBJECT
0014 private Q_SLOTS:
0015     void testMinimalToSmbcValid()
0016     {
0017         // libsmbclient is a bit picky. make sure we convert to minimal applicable form
0018         {
0019             SMBUrl url(QUrl("smb:/"));
0020             QCOMPARE(url.toSmbcUrl(), "smb://");
0021         }
0022 
0023         // But at the same time it will happily deal with smb:
0024         {
0025             SMBUrl url(QUrl("smb:"));
0026             QCOMPARE(url.toSmbcUrl(), "smb:");
0027         }
0028     }
0029 
0030     void testType()
0031     {
0032         QCOMPARE(SMBUrl(QUrl("smb://")).getType(), SMBURLTYPE_ENTIRE_NETWORK);
0033         QCOMPARE(SMBUrl(QUrl("smb://host")).getType(), SMBURLTYPE_WORKGROUP_OR_SERVER);
0034         QCOMPARE(SMBUrl(QUrl("smb://host/share/file")).getType(), SMBURLTYPE_SHARE_OR_PATH);
0035         QCOMPARE(SMBUrl(QUrl()).getType(), SMBURLTYPE_UNKNOWN);
0036     }
0037 
0038     void testPart()
0039     {
0040         SMBUrl url(QUrl("smb://host/share/file"));
0041         QCOMPARE(url.partUrl().toString(), "smb://host/share/file.part");
0042     }
0043 
0044     void testUp()
0045     {
0046         SMBUrl url(QUrl("smb://host/share/file"));
0047         url.cdUp();
0048         QCOMPARE(url.toSmbcUrl(), "smb://host/share");
0049     }
0050 
0051     void testAddPath()
0052     {
0053         SMBUrl url(QUrl("smb://host/share"));
0054         url.addPath("file");
0055         QCOMPARE(url.toSmbcUrl(), "smb://host/share/file");
0056     }
0057 
0058     void testCifs()
0059     {
0060         // We treat cifs as an alias but need to translate it to smb.
0061         // https://bugs.kde.org/show_bug.cgi?id=327295
0062         SMBUrl url(QUrl("cifs://host/share/file"));
0063         QCOMPARE(url.toSmbcUrl(), "smb://host/share/file");
0064     }
0065 
0066     void testIPv6Literal()
0067     {
0068         // https://bugs.kde.org/show_bug.cgi?id=417682
0069         // Samba cannot deal with RFC5952 IPv6 notation (e.g. ::1%lo)
0070         // to work around we convert to windows ipv6 literals.
0071 
0072         // The actual represented URL should not change!
0073         // i.e. towards the KIO client we do not leak the IPv6
0074         // literal when returning an URL.
0075         QCOMPARE(SMBUrl(QUrl("smb://[::1]/share")).toString(), "smb://[::1]/share");
0076 
0077         // The internal smbc representation should be literal though:
0078         // :: prefix
0079         QCOMPARE(SMBUrl(QUrl("smb://[::1]/share")).toSmbcUrl(), "smb://0--1.ipv6-literal.net/share");
0080         // :: suffix
0081         QCOMPARE(SMBUrl(QUrl("smb://[fe80::]/share")).toSmbcUrl(), "smb://fe80--0.ipv6-literal.net/share");
0082         // %lo scope
0083         QCOMPARE(SMBUrl(QUrl("smb://[::1%lo]/share")).toSmbcUrl(), "smb://0--1slo.ipv6-literal.net/share");
0084         // random valid addr
0085         QCOMPARE(SMBUrl(QUrl("smb://[fe80::9cd7:32c7:faeb:f23d]/share")).toSmbcUrl(), "smb://fe80--9cd7-32c7-faeb-f23d.ipv6-literal.net/share");
0086     }
0087 
0088     void testWorkgroupWithSpaces()
0089     {
0090         // Workgroups can have spaces but QUrls cannot, so we have a hack
0091         // that puts the workgroup info into a query.
0092         // Only applicable to SMB1 pretty much, we do not do workgroup browsing
0093         // for 2+.
0094         // https://bugs.kde.org/show_bug.cgi?id=204423
0095 
0096         // wg
0097         QCOMPARE(SMBUrl(QUrl("smb://?kio-workgroup=hax max")).toSmbcUrl(), "smb://hax max/");
0098         // wg and query
0099         QCOMPARE(SMBUrl(QUrl("smb://?kio-workgroup=hax max&q=a")).toSmbcUrl(), "smb://hax max/?q=a");
0100         // host and wg and query
0101         QCOMPARE(SMBUrl(QUrl("smb://host/?kio-workgroup=hax max&q=a")).toSmbcUrl(), "smb://hax max/host?q=a");
0102         // host and wg and query
0103         QCOMPARE(SMBUrl(QUrl("smb://host/share?kio-workgroup=hax max")).toSmbcUrl(), "smb://hax max/host/share");
0104         // Non-empty path. libsmbc hates unclean paths
0105         QCOMPARE(SMBUrl(QUrl("smb:///////?kio-workgroup=hax max")).toSmbcUrl(), "smb://hax max/");
0106         // % character - run through .url() to simulate behavior of our listDir()
0107         QCOMPARE(SMBUrl(QUrl(QUrl("smb://?kio-workgroup=HAX%25MAX").url())).toSmbcUrl(), "smb://HAX%25MAX/");
0108         // !ascii - run through .url() to simulate behavior of our listDir()
0109         QCOMPARE(SMBUrl(QUrl(QUrl("smb:///?kio-workgroup=DOMÄNE A").url())).toSmbcUrl(),
0110                  "smb://DOMÄNE A/"); // works as-is with smbc.
0111 
0112         // Also make sure type detection knows about this
0113         QCOMPARE(SMBUrl(QUrl("smb:/?kio-workgroup=hax max")).getType(), SMBURLTYPE_WORKGROUP_OR_SERVER);
0114     }
0115 
0116     void testNonSmb()
0117     {
0118         // In the kdirnotify integration we load arbitrary urls into smburl,
0119         // make sure they get reported as unknown.
0120         QCOMPARE(SMBUrl(QUrl("file:///")).getType(), SMBURLTYPE_UNKNOWN);
0121         QCOMPARE(SMBUrl(QUrl("file:///home/foo/bar")).getType(), SMBURLTYPE_UNKNOWN);
0122         QCOMPARE(SMBUrl(QUrl("sftp://me@localhost/foo/bar")).getType(), SMBURLTYPE_UNKNOWN);
0123     }
0124 
0125     void testFileParts()
0126     {
0127         // We use SMBUrl for transfers from and to local files as well, make sure it behaves accordingly.
0128         SMBUrl url(QUrl("file:///foo"));
0129         QCOMPARE(QUrl("file:///foo"), url);
0130         QCOMPARE(QUrl("file:///foo.part"), url.partUrl());
0131         // Clearly not a file should not work
0132         QCOMPARE(QUrl(), SMBUrl(QUrl("file:///")).partUrl());
0133     }
0134 
0135     void testPrinter()
0136     {
0137         // We tag printers as such cause otherwise we have no way of knowing it was a printer.
0138         QCOMPARE(SMBUrl(QUrl("smb://host/printer?kio-printer=true")).getType(), SMBURLTYPE_PRINTER);
0139     }
0140 };
0141 
0142 QTEST_GUILESS_MAIN(SMBUrlTest)
0143 
0144 #include "smburltest.moc"