diff ui/createcertlistdialog.cpp @ 466:0d71ce440bcc

Factor out list creation to make it more testable
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 23 Apr 2014 15:48:08 +0000
parents 88dfe16a0bb9
children 9cd28df2c4ce
line wrap: on
line diff
--- a/ui/createcertlistdialog.cpp	Wed Apr 23 15:34:53 2014 +0000
+++ b/ui/createcertlistdialog.cpp	Wed Apr 23 15:48:08 2014 +0000
@@ -184,6 +184,44 @@
     }
 }
 
+bool CreateCertListDialog::writeList(const QList<Certificate>& certs,
+                                     const QString& filePath,
+                                     const QDateTime& listDate,
+                                     pk_context *pk)
+{
+    /* Build up the list data */
+    QByteArray listData("F:1\r\n");
+    listData.append(listDate.toString(Qt::ISODate) + "\r\n");
+
+    foreach (const Certificate& cert, certs) {
+        listData.append(QString::fromLatin1("D:") + cert.base64Line() + "\r\n");
+    }
+
+    QByteArray signature = rsaSignSHA256Hash(sha256sum(listData), pk);
+    if (signature.size() != 3072 / 8) {
+        qDebug() << "Signature creation returned signature of invalid size.";
+        return false;
+    }
+    listData.prepend("\r\n");
+    listData.prepend(signature.toBase64());
+    listData.prepend("S:");
+
+    QFile outputFile(filePath);
+
+    if (!outputFile.open(QIODevice::WriteOnly)) {
+        qDebug() << "Failed to open output file: " << filePath;
+        return false;
+    }
+
+    if (outputFile.write(listData) != listData.size()) {
+        qDebug() << "Failed to write list: " << filePath;
+        outputFile.close();
+        return false;
+    }
+    outputFile.close();
+    return true;
+}
+
 void CreateCertListDialog::createList()
 {
     if (!mPk) {
@@ -195,36 +233,18 @@
 
     QDateTime currentDateTimeUtc = QDateTime::currentDateTimeUtc();
 
-    /* Build up the list data */
-    QByteArray listData("F:1\r\n");
-    listData.append(currentDateTimeUtc.toString(Qt::ISODate) + "\r\n");
-
-    foreach (const Certificate& cert, mAdminWindow->certificates()) {
-        listData.append(QString::fromLatin1("D:") + cert.base64Line() + "\r\n");
-    }
-
-    QByteArray signature = rsaSignSHA256Hash(sha256sum(listData), mPk);
-    listData.prepend("\r\n");
-    listData.prepend(signature.toBase64());
-    listData.prepend("S:");
-
     QString fileName = QString::fromLatin1("certificates-")
             .append(currentDateTimeUtc.toString(("yyyyMMddHHmmss")))
             .append(".txt");
 
     QString filePath = mSaveDir->text().append("/").append(fileName);
 
-    QFile outputFile(filePath);
-
-    if (!outputFile.open(QIODevice::WriteOnly)) {
-        showErrorMessage(tr("Failed to open output file %1").arg(filePath));
-        return;
+    if (!writeList(mAdminWindow->certificates(), filePath,
+                currentDateTimeUtc, mPk)) {
+        showErrorMessage(tr("Failed to write list to: %1").arg(filePath));
     }
 
-    if (outputFile.write(listData) != listData.size()) {
-        showErrorMessage(tr("Failed to write certificate list."));
-        return;
-    }
+    QFile outputFile(filePath);
 
     /* Archive the list */
     QDir archiveDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));

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