[gmime] Added more OpenPGP detection filter tests
- From: Jeffrey Stedfast <fejj src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gmime] Added more OpenPGP detection filter tests
- Date: Wed, 20 Dec 2017 17:09:52 +0000 (UTC)
commit 145c3ab364313973826db416066f47b168575a9b
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date: Wed Dec 13 10:37:16 2017 -0500
Added more OpenPGP detection filter tests
tests/data/pgp/gmime.gpg.pub.openpgp-block | 30 +++++
tests/data/pgp/gmime.gpg.sec.openpgp-block | 33 +++++
tests/data/pgp/signed-message.txt | 19 +++
tests/data/pgp/signed-message.txt.openpgp-block | 10 ++
tests/test-pgp.c | 148 ++++++++++++++++++-----
5 files changed, 210 insertions(+), 30 deletions(-)
---
diff --git a/tests/data/pgp/gmime.gpg.pub.openpgp-block b/tests/data/pgp/gmime.gpg.pub.openpgp-block
new file mode 100644
index 0000000..6b49475
--- /dev/null
+++ b/tests/data/pgp/gmime.gpg.pub.openpgp-block
@@ -0,0 +1,30 @@
+-----BEGIN PGP PUBLIC KEY BLOCK-----
+Version: GnuPG v1.4.3 (GNU/Linux)
+
+mQGiBEYEoWIRBACswLR/XlggowfUdzxMnI0s9Hi16t3ZLDTzpO1E8HUgSqbc+8OJ
+zU6kpU8BnDYuGHoGAZ64GWKk2QG1WF0QIMpc20q1wwUXxwVpvxwxeMSOmKQbBBZt
++BuRWseAxBVHCX04ZD/IcuCayfgFyVU2eRxRFK4pd6X7jP5sNV2VnhBavwCg573h
+jsI4UsGUp/EQ9n+yKCW33PsD/0uCN8SaDzjyMK8Gz19T3izd+p2q75RWIHqdmRde
+3/Po6fzxdgidUSTEDmv4q28rb9VEBeOIez6mwo4P23ypt8HxfbYMtJYcyN7/7HJr
+VE3lPPDqx/TW0T7MdStsDBmRsdSdqVuIIHzuMOLP+RIrw4SxBJK2GXoCyoNooERu
+GY9EA/9gnW4cmqZJKFXWN/nkjFN4eDjhfEus3vKxAMA6hXgATXpVilDJ6G93xHD/
+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qcAniqxtyYaiJqE
+xxou+3Cxr5iL4z4P
+=wAiv
+-----END PGP PUBLIC KEY BLOCK-----
diff --git a/tests/data/pgp/gmime.gpg.sec.openpgp-block b/tests/data/pgp/gmime.gpg.sec.openpgp-block
new file mode 100644
index 0000000..a63e8ec
--- /dev/null
+++ b/tests/data/pgp/gmime.gpg.sec.openpgp-block
@@ -0,0 +1,33 @@
+-----BEGIN PGP PRIVATE KEY BLOCK-----
+Version: GnuPG v1.4.3 (GNU/Linux)
+
+lQHhBEYEoWIRBACswLR/XlggowfUdzxMnI0s9Hi16t3ZLDTzpO1E8HUgSqbc+8OJ
+zU6kpU8BnDYuGHoGAZ64GWKk2QG1WF0QIMpc20q1wwUXxwVpvxwxeMSOmKQbBBZt
++BuRWseAxBVHCX04ZD/IcuCayfgFyVU2eRxRFK4pd6X7jP5sNV2VnhBavwCg573h
+jsI4UsGUp/EQ9n+yKCW33PsD/0uCN8SaDzjyMK8Gz19T3izd+p2q75RWIHqdmRde
+3/Po6fzxdgidUSTEDmv4q28rb9VEBeOIez6mwo4P23ypt8HxfbYMtJYcyN7/7HJr
+VE3lPPDqx/TW0T7MdStsDBmRsdSdqVuIIHzuMOLP+RIrw4SxBJK2GXoCyoNooERu
+GY9EA/9gnW4cmqZJKFXWN/nkjFN4eDjhfEus3vKxAMA6hXgATXpVilDJ6G93xHD/
+Bw3/kgzTlnxG37tcIn7PUKezTObLSiR+KUfzgwfHiTorIc6OABc8bQLohMeUV26V
+em80TS+zgqO6SfSQr1M+TFjItC4toIiezx3IUoJAEPCOGXNdef4DAwKchAfGO2Ra
+rmC21QHkTOlWooe5RtOUNZ6HJrqzppIbmPNl4GMvMm700Q+L1umV8FjR5vsMZ35L
+vNjBFrQobm8udXNlciAobm8uY29tbWVudCkgPG5vLnVzZXJAbm8uZG9tYWluPohg
+BBMRAgAgBQJGBKFiAhsDBgsJCAcDAgQVAggDBBYCAwECHgECF4AACgkQtHdg6VGC
+wgBsXQCbBCqWTViahP6LAt0YLPdao+kl5wUAnieHpjhwWX6+GIcJmIN/8pb6oODD
+nQJjBEYEoXIQCADNzXBAgK2yBqreuJrybUyIt7aedABgUtfYPsWWLkMeViSZWsmE
+/P2izlz5bYZRC3yu5hSFI/L6uaHx2EZAE5/nF0P+XtO4NamjZd6prSRYym3ME+rS
+tliRjFjHDc1ycKIjSJbu19YOqDSRGYJopXWPzZXGFBG1g/o0VbBh7SqxTygAZwfd
+Ri9BaMMK0MRjCvWPeHmCkl4UX7U7piyMwP4lo+ebJSxXjXYjOEGLsHksbJRI47Uk
+OzfpLsxlOmzkqRLLQh5GG5UJsd4i2GkpSIEsOobmi/yKVjx5Nmt9rZJ3240YPffp
+HoUaA5Hy/9m9/BjksPranbftAVSLPnJq9XunAAMFB/95SoPOHcrizV+LWp8FuMSy
+jp+grPLBf6LpuYkP9hbTQgEJGhcWu8zbameApBVrn96Kgr39BHo+be4GA0Zgmu82
+3zbhJ9IRNoeaplvie2DhOfp5JQrNBTBwEPNPel4QWl9GBcfC7qmSFEn4GtmWanCM
+r5bCztlVXw0KptMrLLyvMNrmmgDavxfk4+apVK5LNxLApZSS40mtfyB4vIN82z3Z
+DEuqrTWVwC7FO2NiPUM6iM/NE+7i4Jz46ySW12ITBRxgUpSlkABTR2XlUjPpTXDG
+6pa/X/QH720wu5o/7DTRMBF5QHk3k0na03Omu+WkMwe04r8gXyNdZW3NTIC3aSEr
+/gMDApyEB8Y7ZFquYN/McAifDXADC4bfdCnN04hWSzt0vQRsmQ/zN3akX2BnWNV2
+mJl0NU+o8LT9Hp0bEgHwiGNBqGk+qQIPaWR8qhoZ+15UmjnIr3CISQQYEQIACQUC
+RgShcgIbDAAKCRC0d2DpUYLCAB2UAKCO/Rd/mSxOiDUlJ1+gz/4bEQvFfQCgxa7e
+ffuMgHfn0yBQsk9y4diO7XM=
+=78cc
+-----END PGP PRIVATE KEY BLOCK-----
diff --git a/tests/data/pgp/signed-message.txt b/tests/data/pgp/signed-message.txt
new file mode 100644
index 0000000..3eb3c9c
--- /dev/null
+++ b/tests/data/pgp/signed-message.txt
@@ -0,0 +1,19 @@
+From: GMime Unit Tests <no.user@no.domain>
+To: GMime Unit Tests <no.user@no.domain>
+Subject: PGP signed message
+MIME-Version: 1.0
+Content-Type: text/plain
+
+-----BEGIN PGP SIGNED MESSAGE-----
+Hash: SHA1
+
+This is the message body.
+-----BEGIN PGP SIGNATURE-----
+
+iF0EARECAB0WIQTaJtq3VyocXyqxp0u0d2DpUYLCAAUCWjFEowAKCRC0d2DpUYLC
+APVoAKC2edJUAUrqf8oniTOvPqLaf7jA0wCgxUDCwXL3Vh4zmZEiyKlCGzWhabY=
+=Iqan
+-----END PGP SIGNATURE-----
+
+--
+And this is the email signature...
diff --git a/tests/data/pgp/signed-message.txt.openpgp-block b/tests/data/pgp/signed-message.txt.openpgp-block
new file mode 100644
index 0000000..22572ea
--- /dev/null
+++ b/tests/data/pgp/signed-message.txt.openpgp-block
@@ -0,0 +1,10 @@
+-----BEGIN PGP SIGNED MESSAGE-----
+Hash: SHA1
+
+This is the message body.
+-----BEGIN PGP SIGNATURE-----
+
+iF0EARECAB0WIQTaJtq3VyocXyqxp0u0d2DpUYLCAAUCWjFEowAKCRC0d2DpUYLC
+APVoAKC2edJUAUrqf8oniTOvPqLaf7jA0wCgxUDCwXL3Vh4zmZEiyKlCGzWhabY=
+=Iqan
+-----END PGP SIGNATURE-----
diff --git a/tests/test-pgp.c b/tests/test-pgp.c
index 3f7811c..bc22f28 100644
--- a/tests/test-pgp.c
+++ b/tests/test-pgp.c
@@ -330,22 +330,108 @@ import_key (GMimeCryptoContext *ctx, const char *path)
}
static void
-pump_data_through_filter (GMimeFilter *filter, const char *path)
+pump_data_through_filter (GMimeFilter *filter, const char *path, GMimeStream *ostream)
{
+ GMimeFilter *unix2dos, *dos2unix;
GMimeStream *filtered, *stream;
- stream = g_mime_stream_null_new ();
- filtered = g_mime_stream_filter_new (stream);
- g_object_unref (stream);
+ filtered = g_mime_stream_filter_new (ostream);
+
+ /* convert to DOS format before piping through the OpenPGP filter to maximize testing area */
+ unix2dos = g_mime_filter_unix2dos_new (FALSE);
+ g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, unix2dos);
+ g_object_unref (unix2dos);
g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter);
+ /* convert back to UNIX format after filtering */
+ dos2unix = g_mime_filter_dos2unix_new (FALSE);
+ g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, dos2unix);
+ g_object_unref (dos2unix);
+
stream = g_mime_stream_fs_open (path, O_RDONLY, 0644, NULL);
g_mime_stream_write_to_stream (stream, filtered);
+ g_mime_stream_flush (filtered);
g_object_unref (filtered);
g_object_unref (stream);
}
+static char *openpgp_data_types[] = {
+ "GMIME_OPENPGP_DATA_NONE",
+ "GMIME_OPENPGP_DATA_ENCRYPTED",
+ "GMIME_OPENPGP_DATA_SIGNED",
+ "GMIME_OPENPGP_DATA_PUBLIC_KEY",
+ "GMIME_OPENPGP_DATA_PRIVATE_KEY"
+};
+
+static void
+test_openpgp_filter (GMimeFilterOpenPGP *filter, const char *path, GMimeOpenPGPData data_type, gint64 begin,
gint64 end)
+{
+ GMimeStream *filtered, *stream, *expected, *ostream;
+ GMimeFilter *dos2unix;
+ GMimeOpenPGPData type;
+ Exception *ex = NULL;
+ GByteArray *buf[2];
+ char *filename;
+ struct stat st;
+ gint64 offset;
+
+ ostream = g_mime_stream_mem_new ();
+
+ pump_data_through_filter ((GMimeFilter *) filter, path, ostream);
+
+ if ((type = g_mime_filter_openpgp_get_data_type (filter)) != data_type) {
+ g_object_unref (ostream);
+ throw (exception_new ("Incorrect OpenPGP data type detected: %s", openpgp_data_types[type]));
+ }
+
+ if ((offset = g_mime_filter_openpgp_get_begin_offset (filter)) != begin) {
+ g_object_unref (ostream);
+ throw (exception_new ("Incorrect begin offset: %ld", (long) offset));
+ }
+
+ if ((offset = g_mime_filter_openpgp_get_end_offset (filter)) != end) {
+ g_object_unref (ostream);
+ throw (exception_new ("Incorrect end offset: %ld", (long) offset));
+ }
+
+ filename = g_strdup_printf ("%s.openpgp-block", path);
+ if (stat (filename, &st) == -1) {
+ stream = g_mime_stream_fs_open (filename, O_RDWR | O_CREAT, 0644, NULL);
+ g_mime_stream_reset (ostream);
+ g_mime_stream_write_to_stream (ostream, stream);
+ g_mime_stream_flush (stream);
+ g_mime_stream_reset (stream);
+ } else {
+ stream = g_mime_stream_fs_open (filename, O_RDONLY, 0644, NULL);
+ }
+ g_free (filename);
+
+ /* make sure the data is in UNIX format before comparing (might be running tests on Windows) */
+ expected = g_mime_stream_mem_new ();
+ filtered = g_mime_stream_filter_new (expected);
+ dos2unix = g_mime_filter_dos2unix_new (FALSE);
+ g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, dos2unix);
+ g_object_unref (dos2unix);
+
+ g_mime_stream_write_to_stream (stream, filtered);
+ g_mime_stream_flush (filtered);
+ g_object_unref (filtered);
+ g_object_unref (stream);
+
+ buf[0] = GMIME_STREAM_MEM (expected)->buffer;
+ buf[1] = GMIME_STREAM_MEM (ostream)->buffer;
+
+ if (buf[0]->len != buf[1]->len || memcmp (buf[0]->data, buf[1]->data, buf[0]->len) != 0)
+ ex = exception_new ("filtered data does not match the expected result");
+
+ g_object_unref (expected);
+ g_object_unref (ostream);
+
+ if (ex != NULL)
+ throw (ex);
+}
+
int main (int argc, char **argv)
{
#ifdef ENABLE_CRYPTO
@@ -498,41 +584,43 @@ int main (int argc, char **argv)
g_object_unref (istream);
g_object_unref (ostream);
g_object_unref (ctx);
+
+ filter = (GMimeFilterOpenPGP *) g_mime_filter_openpgp_new ();
- what = "GMimeFilterOpenPGP";
+ what = "GMimeFilterOpenPGP::public key block";
testsuite_check ("%s", what);
try {
- gint64 offset;
-
- filter = (GMimeFilterOpenPGP *) g_mime_filter_openpgp_new ();
-
key = g_build_filename (datadir, "gmime.gpg.pub", NULL);
- pump_data_through_filter ((GMimeFilter *) filter, key);
+ test_openpgp_filter (filter, key, GMIME_OPENPGP_DATA_PUBLIC_KEY, 0, 1720);
g_free (key);
- if (g_mime_filter_openpgp_get_data_type (filter) != GMIME_OPENPGP_DATA_PUBLIC_KEY)
- throw (exception_new ("Failed to detect public key block"));
-
- if ((offset = g_mime_filter_openpgp_get_begin_offset (filter)) != 0)
- throw (exception_new ("Incorrect public key block begin offset: %ld", (long) offset));
-
- if ((offset = g_mime_filter_openpgp_get_end_offset (filter)) != 1690)
- throw (exception_new ("Incorrect public key block end offset: %ld", (long) offset));
-
- g_mime_filter_reset ((GMimeFilter *) filter);
-
+ testsuite_check_passed ();
+ } catch (ex) {
+ testsuite_check_failed ("%s failed: %s", what, ex->message);
+ } finally;
+
+ g_mime_filter_reset ((GMimeFilter *) filter);
+
+ what = "GMimeFilterOpenPGP::private key block";
+ testsuite_check ("%s", what);
+ try {
key = g_build_filename (datadir, "gmime.gpg.sec", NULL);
- pump_data_through_filter ((GMimeFilter *) filter, key);
+ test_openpgp_filter (filter, key, GMIME_OPENPGP_DATA_PRIVATE_KEY, 0, 1928);
g_free (key);
- if (g_mime_filter_openpgp_get_data_type (filter) != GMIME_OPENPGP_DATA_PRIVATE_KEY)
- throw (exception_new ("Failed to detect private key block"));
-
- if ((offset = g_mime_filter_openpgp_get_begin_offset (filter)) != 0)
- throw (exception_new ("Incorrect private key block begin offset: %ld", (long)
offset));
-
- if ((offset = g_mime_filter_openpgp_get_end_offset (filter)) != 1895)
- throw (exception_new ("Incorrect private key block end offset: %ld", (long) offset));
+ testsuite_check_passed ();
+ } catch (ex) {
+ testsuite_check_failed ("%s failed: %s", what, ex->message);
+ } finally;
+
+ g_mime_filter_reset ((GMimeFilter *) filter);
+
+ what = "GMimeFilterOpenPGP::signed message block";
+ testsuite_check ("%s", what);
+ try {
+ key = g_build_filename (datadir, "signed-message.txt", NULL);
+ test_openpgp_filter (filter, key, GMIME_OPENPGP_DATA_SIGNED, 162, 440);
+ g_free (key);
testsuite_check_passed ();
} catch (ex) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]