tests/auto/qzip/tst_qzip.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtTest/QtTest>
       
    43 #include <QDebug>
       
    44 #include <private/qzipwriter_p.h>
       
    45 #include <private/qzipreader_p.h>
       
    46 
       
    47 #ifdef Q_OS_SYMBIAN
       
    48 #define SRCDIR "."
       
    49 #endif
       
    50 
       
    51 class tst_QZip : public QObject
       
    52 {
       
    53     Q_OBJECT
       
    54 public slots:
       
    55     void init();
       
    56     void cleanup();
       
    57 
       
    58 private slots:
       
    59     void basicUnpack();
       
    60     void symlinks();
       
    61     void readTest();
       
    62     void createArchive();
       
    63 };
       
    64 
       
    65 void tst_QZip::init()
       
    66 {
       
    67 }
       
    68 
       
    69 void tst_QZip::cleanup()
       
    70 {
       
    71 }
       
    72 
       
    73 void tst_QZip::basicUnpack()
       
    74 {
       
    75     QZipReader zip(QString(SRCDIR) + "/testdata/test.zip", QIODevice::ReadOnly);
       
    76     QList<QZipReader::FileInfo> files = zip.fileInfoList();
       
    77     QCOMPARE(files.count(), 2);
       
    78 
       
    79     QZipReader::FileInfo fi = files.at(0);
       
    80     QCOMPARE(fi.filePath, QString("test/"));
       
    81     QCOMPARE(uint(fi.isDir), (uint) 1);
       
    82     QCOMPARE(uint(fi.isFile), (uint) 0);
       
    83     QCOMPARE(uint(fi.isSymLink), (uint) 0);
       
    84 
       
    85     QCOMPARE(fi.permissions,QFile::Permissions(  QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner
       
    86                                                  | QFile::ReadUser  | QFile::WriteUser | QFile::ExeUser   ));
       
    87 
       
    88     fi = files.at(1);
       
    89     QCOMPARE(fi.filePath, QString("test/test.txt"));
       
    90     QCOMPARE(uint(fi.isDir), (uint) 0);
       
    91     QCOMPARE(uint(fi.isFile), (uint) 1);
       
    92     QCOMPARE(uint(fi.isSymLink), (uint) 0);
       
    93 
       
    94     QVERIFY(fi.permissions == QFile::Permissions(  QFile::ReadOwner | QFile::WriteOwner
       
    95                                                  | QFile::ReadUser  | QFile::WriteUser ));
       
    96 
       
    97     QCOMPARE(zip.fileData("test/test.txt"), QByteArray("content\n"));
       
    98 }
       
    99 
       
   100 void tst_QZip::symlinks()
       
   101 {
       
   102     QZipReader zip(QString(SRCDIR) + "/testdata/symlink.zip", QIODevice::ReadOnly);
       
   103     QList<QZipReader::FileInfo> files = zip.fileInfoList();
       
   104     QCOMPARE(files.count(), 2);
       
   105 
       
   106     QZipReader::FileInfo fi = files.at(0);
       
   107     QCOMPARE(fi.filePath, QString("symlink"));
       
   108     QVERIFY(!fi.isDir);
       
   109     QVERIFY(!fi.isFile);
       
   110     QVERIFY(fi.isSymLink);
       
   111 
       
   112     QCOMPARE(zip.fileData("symlink"), QByteArray("destination"));
       
   113 
       
   114     fi = files.at(1);
       
   115     QCOMPARE(fi.filePath, QString("destination"));
       
   116     QVERIFY(!fi.isDir);
       
   117     QVERIFY(fi.isFile);
       
   118     QVERIFY(!fi.isSymLink);
       
   119 }
       
   120 
       
   121 void tst_QZip::readTest()
       
   122 {
       
   123     QZipReader zip("foobar.zip", QIODevice::ReadOnly); // non existing file.
       
   124     QList<QZipReader::FileInfo> files = zip.fileInfoList();
       
   125     QCOMPARE(files.count(), 0);
       
   126     QByteArray b = zip.fileData("foobar");
       
   127     QCOMPARE(b.size(), 0);
       
   128 }
       
   129 
       
   130 void tst_QZip::createArchive()
       
   131 {
       
   132     QBuffer buffer;
       
   133     QZipWriter zip(&buffer);
       
   134     QByteArray fileContents("simple file contents\nline2\n");
       
   135     zip.addFile("My Filename", fileContents);
       
   136     zip.close();
       
   137     QByteArray zipFile = buffer.buffer();
       
   138 
       
   139     // QFile f("createArchiveTest.zip"); f.open(QIODevice::WriteOnly); f.write(zipFile); f.close();
       
   140 
       
   141     QBuffer buffer2(&zipFile);
       
   142     QZipReader zip2(&buffer2);
       
   143     QList<QZipReader::FileInfo> files = zip2.fileInfoList();
       
   144     QCOMPARE(files.count(), 1);
       
   145     QZipReader::FileInfo file = files.at(0);
       
   146     QCOMPARE(file.filePath, QString("My Filename"));
       
   147     QCOMPARE(uint(file.isDir), (uint) 0);
       
   148     QCOMPARE(uint(file.isFile), (uint) 1);
       
   149     QCOMPARE(uint(file.isSymLink), (uint) 0);
       
   150     QCOMPARE(file.permissions, QFile::Permissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser) );
       
   151     QCOMPARE(file.size, (long long) 27);
       
   152     QCOMPARE(zip2.fileData("My Filename"), fileContents);
       
   153 }
       
   154 
       
   155 QTEST_MAIN(tst_QZip)
       
   156 #include "tst_qzip.moc"