| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2022 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Alex Char. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the ICNS autotests in the Qt ImageFormats module. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include <QtTest/QtTest> |
| 31 | #include <QtGui/QtGui> |
| 32 | |
| 33 | class tst_qicns: public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | private slots: |
| 38 | void initTestCase(); |
| 39 | void readIcons_data(); |
| 40 | void readIcons(); |
| 41 | void writeIcons_data(); |
| 42 | void writeIcons(); |
| 43 | void ossFuzz_data(); |
| 44 | void ossFuzz(); |
| 45 | }; |
| 46 | |
| 47 | void tst_qicns::initTestCase() |
| 48 | { |
| 49 | if (!QImageReader::supportedImageFormats().contains("icns")) |
| 50 | QSKIP("The image format handler is not installed."); |
| 51 | } |
| 52 | |
| 53 | void tst_qicns::readIcons_data() |
| 54 | { |
| 55 | QTest::addColumn<QString>("fileName"); |
| 56 | QTest::addColumn<QSize>("size"); |
| 57 | QTest::addColumn<int>("imageCount"); |
| 58 | QTest::addColumn<QByteArray>("format"); |
| 59 | |
| 60 | QTest::newRow("1") << QStringLiteral("test-png") << QSize(128, 128) << 7 << QByteArrayLiteral("png"); |
| 61 | QTest::newRow("2") << QStringLiteral("test-jp2") << QSize(128, 128) << 7 << QByteArrayLiteral("jp2"); |
| 62 | QTest::newRow("3") << QStringLiteral("test-32bit") << QSize(128, 128) << 4 << QByteArray(); |
| 63 | QTest::newRow("4") << QStringLiteral("test-legacy") << QSize(48, 48) << 12 << QByteArray(); |
| 64 | QTest::newRow("5") << QStringLiteral("test-variants") << QSize(128, 128) << 5 << QByteArrayLiteral("png"); |
| 65 | } |
| 66 | |
| 67 | void tst_qicns::readIcons() |
| 68 | { |
| 69 | QFETCH(QString, fileName); |
| 70 | QFETCH(QSize, size); |
| 71 | QFETCH(int, imageCount); |
| 72 | QFETCH(QByteArray, format); |
| 73 | |
| 74 | if (!format.isEmpty() && !QImageReader::supportedImageFormats().contains(format)) |
| 75 | QSKIP("This test requires another image format plugin"); |
| 76 | const QString path = QStringLiteral(":/icns/") + fileName + QStringLiteral(".icns"); |
| 77 | QImageReader reader(path); |
| 78 | QVERIFY(reader.canRead()); |
| 79 | QCOMPARE(reader.imageCount(), imageCount); |
| 80 | |
| 81 | for (int i = 0; i < reader.imageCount(); ++i) { |
| 82 | QVERIFY2(reader.jumpToImage(i), qPrintable(reader.errorString())); |
| 83 | QImage image = reader.read(); |
| 84 | if (i == 0) |
| 85 | QCOMPARE(image.size(), size); |
| 86 | QVERIFY2(!image.isNull(), qPrintable(reader.errorString())); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void tst_qicns::writeIcons_data() |
| 91 | { |
| 92 | QTest::addColumn<QString>("fileName"); |
| 93 | QTest::addColumn<QSize>("size"); |
| 94 | |
| 95 | QTest::newRow("1") << QStringLiteral("test-write-16") << QSize(16, 16); |
| 96 | QTest::newRow("2") << QStringLiteral("test-write-32") << QSize(32, 32); |
| 97 | QTest::newRow("3") << QStringLiteral("test-write-64") << QSize(64, 64); |
| 98 | QTest::newRow("4") << QStringLiteral("test-write-128") << QSize(128, 128); |
| 99 | QTest::newRow("5") << QStringLiteral("test-write-512") << QSize(512, 512); |
| 100 | QTest::newRow("6") << QStringLiteral("test-write-1024") << QSize(1024, 1024); |
| 101 | } |
| 102 | |
| 103 | void tst_qicns::writeIcons() |
| 104 | { |
| 105 | QTemporaryDir temp(QDir::tempPath() + QStringLiteral("/tst_qincs")); |
| 106 | QVERIFY2(temp.isValid(), "Unable to create temp dir."); |
| 107 | |
| 108 | QFETCH(QString, fileName); |
| 109 | QFETCH(QSize, size); |
| 110 | |
| 111 | const QString distPath = QStringLiteral("%1/%2.icns").arg(temp.path()).arg(fileName); |
| 112 | const QString sourcePath = QStringLiteral(":/icns/%1.png").arg(fileName); |
| 113 | |
| 114 | QImage image(sourcePath); |
| 115 | QVERIFY(!image.isNull()); |
| 116 | QVERIFY(image.size() == size); |
| 117 | |
| 118 | QImageWriter writer(distPath, QByteArrayLiteral("icns")); |
| 119 | QVERIFY2(writer.canWrite(), qPrintable(writer.errorString())); |
| 120 | QVERIFY2(writer.write(image), qPrintable(writer.errorString())); |
| 121 | |
| 122 | QVERIFY(image == QImage(distPath)); |
| 123 | } |
| 124 | |
| 125 | void tst_qicns::ossFuzz_data() |
| 126 | { |
| 127 | QTest::addColumn<QByteArray>("data"); |
| 128 | QTest::addColumn<QByteArrayList>("ignoredMessages"); |
| 129 | QTest::newRow("47415") << QByteArray::fromRawData("icns\0\0\0\0", 8) |
| 130 | << QByteArrayList({"QICNSHandler::scanDevice(): Failed, bad header at " |
| 131 | "pos 8. OSType \"icns\", length 0", |
| 132 | "QICNSHandler::read(): The device wasn't parsed " |
| 133 | "properly!"}); |
| 134 | } |
| 135 | |
| 136 | void tst_qicns::ossFuzz() |
| 137 | { |
| 138 | QFETCH(QByteArray, data); |
| 139 | QFETCH(QByteArrayList, ignoredMessages); |
| 140 | for (auto msg: ignoredMessages) |
| 141 | QTest::ignoreMessage(QtWarningMsg, msg.data()); |
| 142 | QImage().loadFromData(data); |
| 143 | } |
| 144 | |
| 145 | QTEST_MAIN(tst_qicns) |
| 146 | #include "tst_qicns.moc" |