changeset 202:0861069fd6d0

Fix testing for handling garbage input and generate the data ad hoc.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 26 Mar 2014 12:52:53 +0100
parents 45f6b62f91e7
children 60c5df8e7980
files ui/tests/certlistparsertest.cpp ui/tests/certlistparsertest.h ui/tests/data/garbage_2MB ui/tests/data/garbage_500KB
diffstat 4 files changed, 50 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ui/tests/certlistparsertest.cpp	Wed Mar 26 13:30:33 2014 +0100
+++ b/ui/tests/certlistparsertest.cpp	Wed Mar 26 12:52:53 2014 +0100
@@ -54,6 +54,11 @@
     Certificate cert;
     QVERIFY(!cert.isValid());
 
+    CertificateList *certList2= testWithFile(fname);
+    const QList<Certificate> instList2 = certList2->getInstallCertificates();
+    const QList<Certificate> remoList2 = certList2->getRemoveCertificates();
+    QVERIFY(instLines.size() == instList2.size());
+    QVERIFY(remoLines.size() == remoList2.size());
 
     delete certList;
 }
@@ -97,22 +102,55 @@
 
 void CertListTest::testGarbage()
 {
-    const char *fnames[] = {"garbage_500KB",
-        "list-with-null.txt",
-        NULL};
-    for (int i=0; fnames[i] != NULL; i++) {
-        CertificateList *certList = testWithFile(fnames[i]);
-        QCOMPARE (certList->getStatus(), InvalidFormat);
-        delete certList;
+    const char *fname = "list-with-null.txt";
+    QString fname2 = getRandomDataFile(200);
+    CertificateList *certList = testWithFile(fname);
+    QCOMPARE (certList->getStatus(), InvalidFormat);
+    delete certList;
+    certList = testWithFile(fname2.toLocal8Bit().constData());
+    QVERIFY(QFile::remove(fname2));
+    QCOMPARE (certList->getStatus(), InvalidFormat);
+    delete certList;
+}
+
+QString CertListTest::getRandomDataFile(size_t size)
+{
+    QTemporaryFile tmpfile;
+    tmpfile.setAutoRemove(false);
+    tmpfile.open();
+    size_t bufsize = 1024 * 1024;
+    if (bufsize > size) {
+        bufsize = size;
     }
+    char buf[bufsize];
+
+    for (size_t i = 0; i < bufsize; i++) {
+        buf[i] = (char) qrand() % 255;
+    }
+
+    size_t bytesWritten=0;
+    int retval = 0;
+    do {
+        size_t toWrite = size - bytesWritten;
+        if (toWrite < bufsize) {
+            retval = tmpfile.write(buf, toWrite);
+        } else {
+            retval = tmpfile.write(buf, bufsize);
+        }
+        bytesWritten += retval;
+    } while (retval != -1 && bytesWritten < size);
+
+    tmpfile.close();
+    return tmpfile.fileName();
 }
 
 void CertListTest::testTooLarge()
 {
-    const char *fname = "garbage_2MB";
-    CertificateList *certList = testWithFile(fname);
-    QCOMPARE (certList->getStatus(), TooLarge);
-    QVERIFY (!certList->isValid());
+    QString fname = getRandomDataFile(MAX_LINE_LENGTH * MAX_LINES + 1);
+    CertificateList *certList = testWithFile(fname.toLocal8Bit().constData());
+    QVERIFY(QFile::remove(fname));
+    QCOMPARE(certList->getStatus(), TooLarge);
+    QVERIFY(!certList->isValid());
     delete certList;
 }
 
--- a/ui/tests/certlistparsertest.h	Wed Mar 26 13:30:33 2014 +0100
+++ b/ui/tests/certlistparsertest.h	Wed Mar 26 12:52:53 2014 +0100
@@ -11,6 +11,7 @@
     Q_OBJECT
 
     CertificateList* testWithFile(const char *filename);
+    QString getRandomDataFile(size_t size);
 
 private Q_SLOTS:
     void testInvalidSig();
Binary file ui/tests/data/garbage_2MB has changed
Binary file ui/tests/data/garbage_500KB has changed

http://wald.intevation.org/projects/trustbridge/