From c376317a43463d11bdce6c7c1f646e78fa7bcaf4 Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Wed, 5 Jan 2022 15:50:29 +0100 Subject: [PATCH 1/2] SoundSource: Fix broken file type detection if file suffix is misleading If a file is named `foo.wav` but actually contains MP3 data, `SoundSource::getTypeFromFile` should return "mp3", not "wav". This behavior is expected and already tested in `SoundSourceProxyTest.getTypeFromFile`, but due to a bug in our test file creation code, the test operated on a wrong file name and passed although the behavior was broken and the function would just return "wav" in the example above. The reason for this is that QMimeDatabase only looks at the file name when the file suffix is misleading, and thus cannot detect that the file is actually an MP3 file: > The default matching algorithm looks at both the file name and the > file contents, if necessary. The file extension has priority over the > contents, but the contents will be used if the file extension is > unknown, or matches multiple MIME types. > > Source: https://doc.qt.io/qt-5/qmimedatabase.html#mimeTypeForFile This commit fixes `SoundSource::getTypeFromFile` to work as expected by using the file *content* instead of the file name for determining the file type. --- src/sources/soundsource.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sources/soundsource.cpp b/src/sources/soundsource.cpp index 71c4b5d3de3..e686a4c75ae 100644 --- a/src/sources/soundsource.cpp +++ b/src/sources/soundsource.cpp @@ -48,7 +48,8 @@ QString SoundSource::getTypeFromFile(const QFileInfo& fileInfo) { const QString fileSuffix = fileInfo.suffix(); const QString fileType = fileTypeFromSuffix(fileSuffix); DEBUG_ASSERT(!fileType.isEmpty() || fileType == QString{}); - const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(fileInfo); + const QMimeType mimeType = QMimeDatabase().mimeTypeForFile( + fileInfo, QMimeDatabase::MatchContent); // According to the documentation mimeTypeForFile always returns a valid // type, using the generic type application/octet-stream as a fallback. // This might also occur for missing files as seen on Qt 5.12. From 17a4b16970333dcdd337225295054ce0b80355fa Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Thu, 6 Jan 2022 11:18:21 +0100 Subject: [PATCH 2/2] Revert "test: Disable broken check in SoundProxyTest.getTypeFromFile" This reverts commit 66900632bf2d89073ba3fb74e3c278aa6e51df33 because the underlying issue (i.e. SoundSource::getTypeFromFile() not detecting the correct file type if the file has a known but misleading file extension) has been fixed in commit c376317a43463d11bdce6c7c1f646e78fa7bcaf4. Hence, the check can be re-enabled. --- src/test/soundproxy_test.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/test/soundproxy_test.cpp b/src/test/soundproxy_test.cpp index 0da9c8ade33..fe75fec0e83 100644 --- a/src/test/soundproxy_test.cpp +++ b/src/test/soundproxy_test.cpp @@ -740,11 +740,8 @@ TEST_F(SoundSourceProxyTest, getTypeFromFile) { tempDir.filePath("file_with_empty_suffix."); const QString filePathWithUnknownSuffix = tempDir.filePath("file_with.unknown_suffix"); - // TODO: Currently, our SoundSource::getTypeFromFile() can not detect the - // file type of files with a known but wrong file extension properly, so - // this test needs to be disabled. - //const QString filePathWithWrongSuffix = - // tempDir.filePath("file_with_wrong_suffix.wav"); + const QString filePathWithWrongSuffix = + tempDir.filePath("file_with_wrong_suffix.wav"); const QString filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix = tempDir.filePath("file_with_uppercase_suffix. MP3 "); @@ -753,7 +750,7 @@ TEST_F(SoundSourceProxyTest, getTypeFromFile) { mixxxtest::copyFile(validFilePath, filePathWithoutSuffix); mixxxtest::copyFile(validFilePath, filePathWithEmptySuffix); mixxxtest::copyFile(validFilePath, filePathWithUnknownSuffix); - //mixxxtest::copyFile(validFilePath, filePathWithWrongSuffix); + mixxxtest::copyFile(validFilePath, filePathWithWrongSuffix); mixxxtest::copyFile(validFilePath, filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix); ASSERT_STREQ(qPrintable("mp3"), @@ -769,9 +766,9 @@ TEST_F(SoundSourceProxyTest, getTypeFromFile) { EXPECT_STREQ(qPrintable("mp3"), qPrintable(mixxx::SoundSource::getTypeFromFile( QFileInfo(filePathWithUnknownSuffix)))); - //EXPECT_STREQ(qPrintable("mp3"), - // qPrintable(mixxx::SoundSource::getTypeFromFile( - // QFileInfo(filePathWithWrongSuffix)))); + EXPECT_STREQ(qPrintable("mp3"), + qPrintable(mixxx::SoundSource::getTypeFromFile( + QFileInfo(filePathWithWrongSuffix)))); EXPECT_STREQ(qPrintable("mp3"), qPrintable(mixxx::SoundSource::getTypeFromFile( QFileInfo(filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix))));