[PATCH 5 of 7] Factor out list creation to make it more testable

Wald Commits scm-commit at wald.intevation.org
Wed Apr 23 18:21:57 CEST 2014


# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1398268088 0
# Node ID 0d71ce440bcc28e82a7359f865a81bc3859ecb72
# Parent  88dfe16a0bb9af15631f62c864d29bef904d7ecf
Factor out list creation to make it more testable

diff -r 88dfe16a0bb9 -r 0d71ce440bcc ui/createcertlistdialog.cpp
--- 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));
diff -r 88dfe16a0bb9 -r 0d71ce440bcc ui/createcertlistdialog.h
--- a/ui/createcertlistdialog.h	Wed Apr 23 15:34:53 2014 +0000
+++ b/ui/createcertlistdialog.h	Wed Apr 23 15:48:08 2014 +0000
@@ -12,6 +12,8 @@
 #include <QMainWindow>
 #include <QLineEdit>
 
+#include "certificate.h"
+
 #include <polarssl/pk.h>
 /**
  * @file createinstallerdialog.h
@@ -31,6 +33,19 @@
     CreateCertListDialog(AdministratorWindow *parent);
     ~CreateCertListDialog();
 
+    /** @brief write a signed certificate list.
+     *
+     * @param [in] certs The certificates to include in the list
+     * @param [in] filePath The path where the list should be written to
+     * @param [in] listDate The date to write in the list
+     * @param [in] pk The private key to sign with.
+     *
+     * @returns true on success false on error.
+     */
+    static bool writeList(const QList<Certificate>& certs,
+            const QString& filePath, const QDateTime& listDate, pk_context *pk);
+
+
 private:
     void setupGUI();
 
diff -r 88dfe16a0bb9 -r 0d71ce440bcc ui/l10n/administrator_de_DE.ts
--- a/ui/l10n/administrator_de_DE.ts	Wed Apr 23 15:34:53 2014 +0000
+++ b/ui/l10n/administrator_de_DE.ts	Wed Apr 23 15:48:08 2014 +0000
@@ -226,6 +226,11 @@
         <translation type="unfinished">Zertifikat auswählen</translation>
     </message>
     <message>
+        <location filename="../createcertlistdialog.cpp" line="244"/>
+        <source>Failed to write list to: %1</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
         <location filename="../createcertlistdialog.cpp" line="137"/>
         <source>Failed to load certificate: %1</source>
         <translation type="unfinished"></translation>
@@ -241,42 +246,32 @@
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../createcertlistdialog.cpp" line="190"/>
+        <location filename="../createcertlistdialog.cpp" line="228"/>
         <source>Please select a valid rsa key.</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../createcertlistdialog.cpp" line="193"/>
+        <location filename="../createcertlistdialog.cpp" line="231"/>
         <source>Please select an output location first.</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../createcertlistdialog.cpp" line="220"/>
-        <source>Failed to open output file %1</source>
-        <translation type="unfinished"></translation>
-    </message>
-    <message>
-        <location filename="../createcertlistdialog.cpp" line="225"/>
-        <source>Failed to write certificate list.</source>
-        <translation type="unfinished"></translation>
-    </message>
-    <message>
-        <location filename="../createcertlistdialog.cpp" line="232"/>
+        <location filename="../createcertlistdialog.cpp" line="252"/>
         <source>Failed to create archive location.</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../createcertlistdialog.cpp" line="237"/>
+        <location filename="../createcertlistdialog.cpp" line="257"/>
         <source>Failed Archive a copy.</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../createcertlistdialog.cpp" line="242"/>
+        <location filename="../createcertlistdialog.cpp" line="262"/>
         <source>Failed to write current_certificates file.</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../createcertlistdialog.cpp" line="246"/>
+        <location filename="../createcertlistdialog.cpp" line="266"/>
         <source>Saved certificate list:
 %1</source>
         <translation type="unfinished"></translation>


More information about the Trustbridge-commits mailing list