[gmime-devel] [PATCH 4/5] Divide PGP/MIME tests into message creation and message testing



This sets the stage for being able to try decrypting the message with
a stashed session key the second time around.
---
 tests/test-pgpmime.c | 56 ++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 39 insertions(+), 17 deletions(-)

diff --git a/tests/test-pgpmime.c b/tests/test-pgpmime.c
index 27e6b6b..95d5c71 100644
--- a/tests/test-pgpmime.c
+++ b/tests/test-pgpmime.c
@@ -259,18 +259,15 @@ test_multipart_signed (GMimeCryptoContext *ctx)
 #define MULTIPART_ENCRYPTED_CONTENT "This is a test of multipart/encrypted.\n"
 
 static void
-test_multipart_encrypted (GMimeCryptoContext *ctx, gboolean sign)
+_construct_example (GMimeCryptoContext *ctx, gboolean sign,
+                   GMimeStream **cleartext_out, GMimeStream **stream_out)
 {
        GMimeStream *cleartext, *stream;
        GMimeMultipartEncrypted *mpe;
-       GMimeDecryptResult *result;
        GMimeDataWrapper *content;
-       GMimeObject *decrypted;
        GPtrArray *recipients;
        GMimeMessage *message;
        Exception *ex = NULL;
-       GMimeParser *parser;
-       GByteArray *buf[2];
        GError *err = NULL;
        GMimePart *part;
        
@@ -324,12 +321,32 @@ test_multipart_encrypted (GMimeCryptoContext *ctx, gboolean sign)
        
        stream = g_mime_stream_mem_new ();
        g_mime_object_write_to_stream ((GMimeObject *) message, stream);
-       g_mime_stream_reset (stream);
        g_object_unref (message);
+
+       *stream_out = stream;
+       *cleartext_out = cleartext;
+}      
+
+static void
+test_multipart_encrypted (GMimeCryptoContext *ctx, gboolean sign,
+                         GMimeStream *cleartext, GMimeStream *stream)
+{
+       GMimeStream *test_stream;
+       GMimeMultipartEncrypted *mpe;
+       GMimeDecryptResult *result;
+       GMimeDataWrapper *content;
+       GMimeObject *decrypted;
+       GMimeMessage *message;
+       Exception *ex = NULL;
+       GMimeParser *parser;
+       GByteArray *buf[2];
+       GError *err = NULL;
+       GMimePart *part;
        
+       g_mime_stream_reset (stream);
+       g_mime_stream_reset (cleartext);
        parser = g_mime_parser_new ();
        g_mime_parser_init_with_stream (parser, stream);
-       g_object_unref (stream);
        
        message = g_mime_parser_construct_message (parser);
        g_object_unref (parser);
@@ -346,14 +363,12 @@ test_multipart_encrypted (GMimeCryptoContext *ctx, gboolean sign)
        decrypted = g_mime_multipart_encrypted_decrypt (mpe, ctx, &result, &err);
        if (!decrypted || err != NULL) {
                ex = exception_new ("decryption failed: %s", err->message);
-               g_object_unref (cleartext);
                g_error_free (err);
                throw (ex);
        }
        
        if (!result->session_key) {
                ex = exception_new ("No session key returned!");
-               g_object_unref (cleartext);
                throw (ex);
        }
        
@@ -371,21 +386,19 @@ test_multipart_encrypted (GMimeCryptoContext *ctx, gboolean sign)
        g_object_unref (result);
        
        if (ex != NULL) {
-               g_object_unref (cleartext);
                throw (ex);
        }
        
-       stream = g_mime_stream_mem_new ();
-       g_mime_object_write_to_stream (decrypted, stream);
+       test_stream = g_mime_stream_mem_new ();
+       g_mime_object_write_to_stream (decrypted, test_stream);
        
        buf[0] = GMIME_STREAM_MEM (cleartext)->buffer;
-       buf[1] = GMIME_STREAM_MEM (stream)->buffer;
+       buf[1] = GMIME_STREAM_MEM (test_stream)->buffer;
        
        if (buf[0]->len != buf[1]->len || memcmp (buf[0]->data, buf[1]->data, buf[0]->len) != 0)
                ex = exception_new ("decrypted data does not match original cleartext");
        
-       g_object_unref (cleartext);
-       g_object_unref (stream);
+       g_object_unref (test_stream);
        
        if (ex != NULL)
                throw (ex);
@@ -420,6 +433,7 @@ int main (int argc, char *argv[])
        struct stat st;
        char *key;
        int i;
+       GMimeStream *stream = NULL, *cleartext = NULL;
        
        g_mime_init (0);
        
@@ -470,19 +484,27 @@ int main (int argc, char *argv[])
        
        testsuite_check ("multipart/encrypted");
        try {
-               test_multipart_encrypted (ctx, FALSE);
+               _construct_example (ctx, FALSE, &cleartext, &stream);
+               test_multipart_encrypted (ctx, FALSE, cleartext, stream);
                testsuite_check_passed ();
        } catch (ex) {
                testsuite_check_failed ("multipart/encrypted failed: %s", ex->message);
        } finally;
+       g_object_unref (cleartext);
+       g_object_unref (stream);
+       cleartext = stream = NULL;
        
        testsuite_check ("multipart/encrypted+sign");
        try {
-               test_multipart_encrypted (ctx, TRUE);
+               _construct_example (ctx, TRUE, &cleartext, &stream);
+               test_multipart_encrypted (ctx, TRUE, cleartext, stream);
                testsuite_check_passed ();
        } catch (ex) {
                testsuite_check_failed ("multipart/encrypted+sign failed: %s", ex->message);
        } finally;
+       g_object_unref (cleartext);
+       g_object_unref (stream);
+       cleartext = stream = NULL;
        
        g_object_unref (ctx);
        
-- 
2.10.2



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]