File indexing completed on 2024-04-28 11:40:32
0001 /* 0002 SPDX-FileCopyrightText: 2002-2005 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #include <QSignalSpy> 0008 #include <QTest> 0009 0010 #include "httpfilter.h" 0011 #include <KCompressionDevice> 0012 #include <KFilterBase> 0013 #include <QDebug> 0014 #include <QDir> 0015 #include <QFile> 0016 #include <zlib.h> 0017 0018 class HTTPFilterTest : public QObject 0019 { 0020 Q_OBJECT 0021 0022 private Q_SLOTS: 0023 void initTestCase(); 0024 void test_deflateWithZlibHeader(); 0025 void test_httpFilterGzip(); 0026 0027 private: 0028 void test_block_write(const QString &fileName, const QByteArray &data); 0029 void test_block_read(const QString &fileName); 0030 void test_getch(const QString &fileName); 0031 void test_textstream(const QString &fileName); 0032 void test_readall(const QString &fileName, const QString &mimeType, const QByteArray &expectedData); 0033 0034 protected Q_SLOTS: 0035 void slotFilterOutput(const QByteArray &data); 0036 0037 private: 0038 QString pathgz; 0039 QByteArray testData; 0040 QByteArray m_filterOutput; 0041 }; 0042 0043 QTEST_MAIN(HTTPFilterTest) 0044 0045 void HTTPFilterTest::initTestCase() 0046 { 0047 qRegisterMetaType<KCompressionDevice::CompressionType>(); 0048 const QString currentdir = QDir::currentPath(); 0049 pathgz = currentdir + "/test.gz"; 0050 0051 testData = "hello world\n"; 0052 0053 // Create the gz file 0054 0055 KCompressionDevice dev(pathgz); 0056 QVERIFY(dev.open(QIODevice::WriteOnly)); 0057 const int ret = dev.write(testData); 0058 QCOMPARE(ret, testData.size()); 0059 dev.close(); 0060 } 0061 0062 static void getCompressedData(QByteArray &data, QByteArray &compressedData) 0063 { 0064 data = "Hello world, this is a test for deflate, from bug 114830 / 117683"; 0065 compressedData.resize(long(data.size() * 1.1f) + 12L); // requirements of zlib::compress2 0066 unsigned long out_bufferlen = compressedData.size(); 0067 const int ret = compress2((Bytef *)compressedData.data(), &out_bufferlen, (const Bytef *)data.constData(), data.size(), 1); 0068 QCOMPARE(ret, Z_OK); 0069 compressedData.resize(out_bufferlen); 0070 } 0071 0072 void HTTPFilterTest::test_deflateWithZlibHeader() 0073 { 0074 QByteArray data; 0075 QByteArray deflatedData; 0076 getCompressedData(data, deflatedData); 0077 0078 { 0079 HTTPFilterDeflate filter; 0080 QSignalSpy spyOutput(&filter, &HTTPFilterBase::output); 0081 QSignalSpy spyError(&filter, &HTTPFilterBase::error); 0082 filter.slotInput(deflatedData); 0083 QCOMPARE(spyOutput.count(), 2); 0084 QCOMPARE(spyOutput[0][0].toByteArray(), data); 0085 QCOMPARE(spyOutput[1][0].toByteArray(), QByteArray()); 0086 QCOMPARE(spyError.count(), 0); 0087 } 0088 { 0089 // Now a test for giving raw deflate data to HTTPFilter 0090 HTTPFilterDeflate filter; 0091 QSignalSpy spyOutput(&filter, &HTTPFilterBase::output); 0092 QSignalSpy spyError(&filter, &HTTPFilterBase::error); 0093 QByteArray rawDeflate = deflatedData.mid(2); // remove CMF+FLG 0094 rawDeflate.chop(4); // remove trailing Adler32. 0095 filter.slotInput(rawDeflate); 0096 QCOMPARE(spyOutput.count(), 2); 0097 QCOMPARE(spyOutput[0][0].toByteArray(), data); 0098 QCOMPARE(spyOutput[1][0].toByteArray(), QByteArray()); 0099 QCOMPARE(spyError.count(), 0); 0100 } 0101 } 0102 0103 void HTTPFilterTest::test_httpFilterGzip() 0104 { 0105 QFile file(pathgz); 0106 QVERIFY(file.open(QIODevice::ReadOnly)); 0107 const QByteArray compressed = file.readAll(); 0108 0109 // Test sending the whole data in one go 0110 { 0111 HTTPFilterGZip filter; 0112 QSignalSpy spyOutput(&filter, &HTTPFilterBase::output); 0113 QSignalSpy spyError(&filter, &HTTPFilterBase::error); 0114 filter.slotInput(compressed); 0115 QCOMPARE(spyOutput.count(), 2); 0116 QCOMPARE(spyOutput[0][0].toByteArray(), testData); 0117 QCOMPARE(spyOutput[1][0].toByteArray(), QByteArray()); 0118 QCOMPARE(spyError.count(), 0); 0119 } 0120 0121 // Test sending the data byte by byte 0122 { 0123 m_filterOutput.clear(); 0124 HTTPFilterGZip filter; 0125 QSignalSpy spyOutput(&filter, &HTTPFilterBase::output); 0126 connect(&filter, &HTTPFilterBase::output, this, &HTTPFilterTest::slotFilterOutput); 0127 QSignalSpy spyError(&filter, &HTTPFilterBase::error); 0128 for (int i = 0; i < compressed.size(); ++i) { 0129 // qDebug() << "sending byte number" << i << ":" << (uchar)compressed[i]; 0130 filter.slotInput(QByteArray(compressed.constData() + i, 1)); 0131 QCOMPARE(spyError.count(), 0); 0132 } 0133 QCOMPARE(m_filterOutput, testData); 0134 QCOMPARE(spyOutput[spyOutput.count() - 1][0].toByteArray(), QByteArray()); // last one was empty 0135 } 0136 } 0137 0138 void HTTPFilterTest::slotFilterOutput(const QByteArray &data) 0139 { 0140 m_filterOutput += data; 0141 } 0142 0143 #include "httpfiltertest.moc"