diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-01-31 12:23:44 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-03-10 12:20:25 -0800 |
commit | 63895f0c9d4192f5aa881ee962f35b4b03eca374 (patch) | |
tree | ee98482035f46be1fa83a8ac0c0f457e3a580033 | |
parent | bf87df0d98708129b01d816bb9a6cca0d9f1f1bb (diff) | |
download | qtbase-63895f0c9d4192f5aa881ee962f35b4b03eca374.tar.gz |
QMimeDatabase: add a test to ensure we can detect Unix specials
Change-Id: I570832c9ac8b4e03bde8fffd173f7e743f42f22b
Reviewed-by: David Faure <david.faure@kdab.com>
(cherry picked from commit 9d2f3e63b86b48fd402aa126569746c9111f5ccc)
-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 4f9008aa0f..ce0d9c8047 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -6,6 +6,8 @@ #include "qstandardpaths.h" #ifdef Q_OS_UNIX +#include <dirent.h> +#include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #endif @@ -623,6 +625,82 @@ void tst_QMimeDatabase::mimeTypeForFileAndContent() 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 5818e1b6eb..4622a72536 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 mimeTypeForFileAndContent_data(); void mimeTypeForFileAndContent(); +#ifdef Q_OS_UNIX + void mimeTypeForUnixSpecials_data(); + void mimeTypeForUnixSpecials(); +#endif void allMimeTypes(); void suffixes_data(); void suffixes(); |