From 9d2f3e63b86b48fd402aa126569746c9111f5ccc Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 31 Jan 2023 12:23:44 -0800 Subject: QMimeDatabase: add a test to ensure we can detect Unix specials Pick-to: 6.4 6.5 Change-Id: I570832c9ac8b4e03bde8fffd173f7e743f42f22b Reviewed-by: David Faure --- .../mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 78 ++++++++++++++++++++++ .../mimetypes/qmimedatabase/tst_qmimedatabase.h | 4 ++ 2 files changed, 82 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 4145219bde..e5a1067310 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -7,6 +7,8 @@ #include "qstandardpaths.h" #ifdef Q_OS_UNIX +#include +#include #include #include #endif @@ -624,6 +626,82 @@ void tst_QMimeDatabase::mimeTypeForFileNameAndData() QCOMPARE(buffer.pos(), qint64(0)); } +#ifdef Q_OS_UNIX +void tst_QMimeDatabase::mimeTypeForUnixSpecials_data() +{ + QTest::addColumn("name"); + QTest::addColumn("expected"); + + static const char * const mimeTypes[] = { + "inode/blockdevice", + "inode/chardevice", + "inode/fifo", + "inode/socket", + }; + enum SpecialType { + FoundBlock = 0, + FoundChar = 1, + FoundFifo = 2, + FoundSocket = 3, + }; + uint found = 0; + auto nothingfound = []() { + QSKIP("No special Unix inode types found!"); + }; + + // on a standard Linux system (systemd), /dev/log is a symlink to a socket + // and /dev/initctl is a symlink to a FIFO + int devfd = open("/dev", O_RDONLY); + DIR *devdir = fdopendir(devfd); // takes ownership + if (!devdir) + return nothingfound(); + + while (struct dirent *ent = readdir(devdir)) { + struct stat statbuf; + if (fstatat(devfd, ent->d_name, &statbuf, 0) < 0) + continue; + + SpecialType type; + if (S_ISBLK(statbuf.st_mode)) { + type = FoundBlock; + } else if (S_ISCHR(statbuf.st_mode)) { + type = FoundChar; + } else if (S_ISFIFO(statbuf.st_mode)) { + type = FoundFifo; + } else if (S_ISSOCK(statbuf.st_mode)) { + type = FoundSocket; + } else { + if (!S_ISREG(statbuf.st_mode) && !S_ISDIR(statbuf.st_mode)) + qWarning("Could not tell what file type '%s' is: %#o'", + ent->d_name, statbuf.st_mode); + continue; + } + + if (found & (1U << type)) + continue; // we've already seen such a type + + const char *mimeType = mimeTypes[type]; + QTest::addRow("%s", mimeType) + << u"/dev/"_s + QFile::decodeName(ent->d_name) << mimeType; + found |= (1U << type); + } + closedir(devdir); + + if (!found) + nothingfound(); +} + +void tst_QMimeDatabase::mimeTypeForUnixSpecials() +{ + QFETCH(QString, name); + QFETCH(QString, expected); + + qInfo() << "Testing that" << name << "is" << expected; + QMimeDatabase db; + QCOMPARE(db.mimeTypeForFile(name).name(), expected); +} +#endif + void tst_QMimeDatabase::allMimeTypes() { QMimeDatabase db; diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h index f6d5a172bc..8a4d8ef124 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h @@ -37,6 +37,10 @@ private slots: void mimeTypeForData(); void mimeTypeForFileNameAndData_data(); void mimeTypeForFileNameAndData(); +#ifdef Q_OS_UNIX + void mimeTypeForUnixSpecials_data(); + void mimeTypeForUnixSpecials(); +#endif void allMimeTypes(); void suffixes_data(); void suffixes(); -- cgit v1.2.3