# HG changeset patch # User Raimund Renkert # Date 1395843770 -3600 # Node ID e35983a322d6ee87bdd657b083365f69523159f2 # Parent d587d075eddc4afeea4af2b106d339347be44ac4# Parent 825b42da1855cd628c5cc233a439203067d569c5 merged. diff -r d587d075eddc -r e35983a322d6 ui/certificate.cpp --- a/ui/certificate.cpp Wed Mar 26 15:22:14 2014 +0100 +++ b/ui/certificate.cpp Wed Mar 26 15:22:50 2014 +0100 @@ -15,6 +15,10 @@ x509_crt x509cert; + if (b64Line.isEmpty()) { + return; + } + /* Cut of the first two chars (e.g. I: and decode) */ QByteArray asn1data = QByteArray::fromBase64( b64Line.right(b64Line.size() - 2).toLatin1()); @@ -25,6 +29,7 @@ asn1data.size()); if (ret != 0) { qDebug() << "Parsing certificate failed with error: " << ret; + qDebug() << "Failed cert: " << asn1data.toBase64(); x509_crt_free(&x509cert); return; } diff -r d587d075eddc -r e35983a322d6 ui/certificatelist.cpp --- a/ui/certificatelist.cpp Wed Mar 26 15:22:14 2014 +0100 +++ b/ui/certificatelist.cpp Wed Mar 26 15:22:50 2014 +0100 @@ -13,6 +13,11 @@ char *data = NULL; size_t size = 0; + mCertificatesRemove.clear(); + mCertificatesInstall.clear(); + mDate = QDateTime(); + mData = QString(); + mStatus = read_and_verify_list(fileName, &data, &size); if (!isValid()) { diff -r d587d075eddc -r e35983a322d6 ui/certificatelist.h --- a/ui/certificatelist.h Wed Mar 26 15:22:14 2014 +0100 +++ b/ui/certificatelist.h Wed Mar 26 15:22:50 2014 +0100 @@ -54,7 +54,6 @@ const QString& rawData() const {return mData;} private: - QString mFileName; QList mCertificatesInstall; QList mCertificatesRemove; QString mData; diff -r d587d075eddc -r e35983a322d6 ui/tests/certlistparsertest.cpp --- a/ui/tests/certlistparsertest.cpp Wed Mar 26 15:22:14 2014 +0100 +++ b/ui/tests/certlistparsertest.cpp Wed Mar 26 15:22:50 2014 +0100 @@ -51,9 +51,16 @@ QVERIFY(instLines.size() == instList.size()); QVERIFY(remoLines.size() == remoList.size()); + /* Check that a default certificate is invalid */ Certificate cert; QVERIFY(!cert.isValid()); + certList->readList(fileName.toLocal8Bit().data()); + + const QList instList2 = certList->getInstallCertificates(); + const QList remoList2 = certList->getRemoveCertificates(); + QVERIFY(instLines.size() == instList2.size()); + QVERIFY(remoLines.size() == remoList2.size()); delete certList; } @@ -90,29 +97,61 @@ { const char *fname = "empty_file"; CertificateList *certList = testWithFile(fname); - printf("Status :%i\n ",certList->getStatus()); QCOMPARE (certList->getStatus(), SeekFailed); delete certList; } 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; } diff -r d587d075eddc -r e35983a322d6 ui/tests/certlistparsertest.h --- a/ui/tests/certlistparsertest.h Wed Mar 26 15:22:14 2014 +0100 +++ b/ui/tests/certlistparsertest.h Wed Mar 26 15:22:50 2014 +0100 @@ -11,6 +11,7 @@ Q_OBJECT CertificateList* testWithFile(const char *filename); + QString getRandomDataFile(size_t size); private Q_SLOTS: void testInvalidSig(); diff -r d587d075eddc -r e35983a322d6 ui/tests/data/garbage_2MB Binary file ui/tests/data/garbage_2MB has changed diff -r d587d075eddc -r e35983a322d6 ui/tests/data/garbage_500KB Binary file ui/tests/data/garbage_500KB has changed