Home | History | Annotate | Download | only in MIMESniffing
      1 /*
      2     Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
      3 
      4     This library is free software; you can redistribute it and/or
      5     modify it under the terms of the GNU Library General Public
      6     License as published by the Free Software Foundation; either
      7     version 2 of the License, or (at your option) any later version.
      8 
      9     This library is distributed in the hope that it will be useful,
     10     but WITHOUT ANY WARRANTY; without even the implied warranty of
     11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12     Library General Public License for more details.
     13 
     14     You should have received a copy of the GNU Library General Public License
     15     along with this library; see the file COPYING.LIB.  If not, write to
     16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17     Boston, MA 02110-1301, USA.
     18 */
     19 
     20 #include "config.h"
     21 #include "MIMESniffing.h"
     22 
     23 #include "TestData.h"
     24 
     25 #include <QtCore/QFile>
     26 #include <QtCore/QString>
     27 #include <QtTest/QtTest>
     28 
     29 class tst_MIMESniffing : public QObject {
     30     Q_OBJECT
     31 
     32 public:
     33     tst_MIMESniffing();
     34 
     35 private Q_SLOTS:
     36     void testCase1();
     37 };
     38 
     39 tst_MIMESniffing::tst_MIMESniffing()
     40 {
     41 }
     42 
     43 static inline const char* errorText(const TestData& data, const char* sniffedType)
     44 {
     45     return QString("file: %1, advertised: %2, image: %3. sniffed mime type was expected to be \"%4\" but instead was \"%5\"").arg(data.file).arg(data.advertisedType).arg(data.isImage).arg(data.sniffedType).arg(sniffedType).toLatin1();
     46 }
     47 
     48 void tst_MIMESniffing::testCase1()
     49 {
     50 
     51     for (int i = 0; i < testListSize; ++i) {
     52         QFile file(testList[i].file);
     53         QVERIFY2(file.open(QIODevice::ReadOnly), QString("unable to open file %1").arg(file.fileName()).toLatin1());
     54 
     55         MIMESniffer sniffer(testList[i].advertisedType, testList[i].isImage);
     56         QByteArray data = file.peek(sniffer.dataSize());
     57 
     58         const char* sniffedType = sniffer.sniff(data.constData(), data.size());
     59 
     60         QVERIFY2(!(sniffedType || testList[i].sniffedType) || (sniffedType && testList[i].sniffedType), errorText(testList[i], sniffedType));
     61 
     62         if (sniffedType)
     63             QVERIFY2(!strcmp(sniffedType, testList[i].sniffedType), errorText(testList[i], sniffedType));
     64 
     65     }
     66 
     67     QVERIFY2(true, "Failure");
     68 }
     69 
     70 QTEST_APPLESS_MAIN(tst_MIMESniffing);
     71 
     72 #include "tst_MIMESniffing.moc"
     73