diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-01-31 12:23:44 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-02-02 12:17:25 -0800 |
commit | 9d2f3e63b86b48fd402aa126569746c9111f5ccc (patch) | |
tree | 472cd9cc6b7ccdbdea41edd2b845b2df9a82d6ae /tests | |
parent | 40dd38813cba4bc8425e846f220e358ffb0d19ac (diff) |
QMimeDatabase: add a test to ensure we can detect Unix specials
Pick-to: 6.4 6.5
Change-Id: I570832c9ac8b4e03bde8fffd173f7e743f42f22b
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 78 | ||||
-rw-r--r-- | tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h | 4 |
2 files changed, 82 insertions, 0 deletions
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 <dirent.h> +#include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #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<QString>("name"); + QTest::addColumn<QString>("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(); |