[gmime: 12/23] Autocrypt: initial test suite



commit 6cce2baca2368ebabcf5062bc4b3160dbf9ca017
Author: Daniel Kahn Gillmor <dkg fifthhorseman net>
Date:   Sun Oct 22 19:48:27 2017 -0400

    Autocrypt: initial test suite
    
    We want to ensure that the code isn't broken (and doesn't get broken).
    Write up an initial simple test suite for it.

 tests/.gitignore       |    1 +
 tests/Makefile.am      |    6 ++
 tests/test-autocrypt.c |  137 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 144 insertions(+), 0 deletions(-)
---
diff --git a/tests/.gitignore b/tests/.gitignore
index 9d1a70f..9004812 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -4,6 +4,7 @@ Makefile
 .libs/
 *.lo
 *.o
+test-autocrypt
 test-best
 test-cat
 test-headers
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e14cbbd..ec62325 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -16,6 +16,7 @@ AUTOMATED_TESTS =     \
        test-headers    \
        test-partial    \
        test-mbox       \
+       test-autocrypt  \
        test-mime
 
 if ENABLE_CRYPTO
@@ -45,6 +46,11 @@ test_best_LDFLAGS =
 test_best_DEPENDENCIES = $(DEPS)
 test_best_LDADD = $(LDADDS)
 
+test_autocrypt_SOURCES = test-autocrypt.c testsuite.c testsuite.h
+test_autocrypt_LDFLAGS = 
+test_autocrypt_DEPENDENCIES = $(DEPS)
+test_autocrypt_LDADD = $(LDADDS)
+
 test_mime_SOURCES = test-mime.c testsuite.c testsuite.h
 test_mime_LDFLAGS = 
 test_mime_DEPENDENCIES = $(DEPS)
diff --git a/tests/test-autocrypt.c b/tests/test-autocrypt.c
new file mode 100644
index 0000000..8dd2853
--- /dev/null
+++ b/tests/test-autocrypt.c
@@ -0,0 +1,137 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*  GMime
+ *  Copyright (C) 2000-2017 Jeffrey Stedfast
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <gmime/gmime.h>
+#include <gmime/gmime-autocrypt.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "testsuite.h"
+
+extern int verbose;
+
+#define d(x)
+#define v(x) if (verbose > 3) x
+
+struct _ah_gen_test {
+       const char *addr;
+       int keydatacount;
+       char keybyte;
+       gint64 timestamp;
+       const char *txt;
+};
+
+static GMimeAutocryptHeader*
+_gen_header (const struct _ah_gen_test *t)
+{
+       GMimeAutocryptHeader *ah = NULL;
+       GByteArray *keydata = NULL;
+       try {
+               if (!(ah = g_mime_autocrypt_header_new()))
+                       throw (exception_new ("failed to make a new header"));
+               if (t->keydatacount)
+                       if (!(keydata = g_byte_array_new_take (g_strnfill (t->keydatacount, t->keybyte), 
t->keydatacount)))
+                               throw (exception_new ("failed to make a new keydata"));
+               g_mime_autocrypt_header_set_address_from_string (ah, t->addr);
+               g_mime_autocrypt_header_set_keydata (ah, keydata);
+               if (t->timestamp) {
+                       GDateTime *ts = g_date_time_new_from_unix_utc (t->timestamp);
+                       g_mime_autocrypt_header_set_effective_date (ah, ts);
+                       g_date_time_unref (ts);
+               }
+       } catch (ex) {
+               if (keydata)
+                       g_byte_array_unref (keydata);
+               if (ah)
+                       g_object_unref (ah);
+               throw (ex);
+       } finally;
+       return ah;
+}
+       
+const static struct _ah_gen_test gen_test_data[] = {
+       { .addr = "test example org",
+         .keydatacount = 102,
+         .keybyte = '\013',
+         .txt = "addr=test example org; type=1; keydata=CwsLCwsLCwsLCwsLCwsL\r\n"
+         " CwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsL\r\n"
+         " CwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsL",
+       }
+};
+
+
+static void
+test_ah_generation (void)
+{
+       GMimeAutocryptHeader *ah = NULL;
+       char *str = NULL;
+       unsigned int i;
+       for (i = 0; i < G_N_ELEMENTS(gen_test_data); i++) {
+               try {
+                       testsuite_check ("Autocrypt header[%u]", i);
+                       const struct _ah_gen_test *test = gen_test_data + i;
+                       ah = _gen_header (test);
+
+                       str = g_mime_autocrypt_header_get_string (ah);
+                       if (strcmp (test->txt, str)) {
+                               fprintf (stderr, "expected[%u]: \n%s\n\ngot:\n%s\n", i,
+                                        test->txt, str);
+                               throw (exception_new ("failed comparison"));
+                       }
+                       GMimeAutocryptHeader *ah2 = g_mime_autocrypt_header_new_from_string (str);
+                       gint cmp = g_mime_autocrypt_header_compare (ah, ah2);
+                       if (cmp) {
+                               char *x = g_mime_autocrypt_header_get_string (ah2);
+                               fprintf (stderr, "after-rebuild[%u] (%d) \nexpected: \n%s\n\ngot:\n%s\n", i,
+                                        cmp, test->txt, x);
+                               g_free(x);
+                               throw (exception_new ("header from string did not match"));
+                       }
+                       
+                       testsuite_check_passed ();
+               } catch (ex) {
+                       testsuite_check_failed ("autocrypt header creation failed: %s", ex->message);
+               } finally;
+               if (ah)
+                       g_object_unref (ah);
+               g_free (str);
+               str = NULL;
+       }
+}
+
+int main (int argc, char **argv)
+{
+       g_mime_init ();
+       
+       testsuite_init (argc, argv);
+       
+       testsuite_start ("Autocrypt: generate headers");
+       test_ah_generation ();
+       testsuite_end ();
+       
+       g_mime_shutdown ();
+       
+       return testsuite_exit ();
+}


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