Added pgp-mime.c to the tests.
2001-04-11 Jeffrey Stedfast <fejj@ximian.com> * tests/smime/Makefile.am: Added pgp-mime.c to the tests. * tests/smime/pgp-mime.c: Test suite for camel-pgp-mime.c functions. * Makefile.am: Add camel-pgp-mime.[c,h] to the build. * camel-pgp-mime.c: Made a number of fixes to get it to compile and also fixed a few logic errors (mostly forgetting to reset streams) so that it worked (thanks to the pgp-mime test program). svn path=/trunk/; revision=9247
This commit is contained in:
committed by
Jeffrey Stedfast
parent
414da9ba35
commit
55bb947869
@ -1,3 +1,15 @@
|
||||
2001-04-11 Jeffrey Stedfast <fejj@ximian.com>
|
||||
|
||||
* tests/smime/Makefile.am: Added pgp-mime.c to the tests.
|
||||
|
||||
* tests/smime/pgp-mime.c: Test suite for camel-pgp-mime.c functions.
|
||||
|
||||
* Makefile.am: Add camel-pgp-mime.[c,h] to the build.
|
||||
|
||||
* camel-pgp-mime.c: Made a number of fixes to get it to compile
|
||||
and also fixed a few logic errors (mostly forgetting to reset
|
||||
streams) so that it worked (thanks to the pgp-mime test program).
|
||||
|
||||
2001-04-11 JP Rosevear <jpr@ximian.com>
|
||||
|
||||
* providers/imap/Makefile.am: user GNOME_INCLUDEDIR since gnome
|
||||
|
||||
@ -55,6 +55,7 @@ libcamel_la_SOURCES = \
|
||||
camel-object.c \
|
||||
camel-operation.c \
|
||||
camel-pgp-context.c \
|
||||
camel-pgp-mime.c \
|
||||
camel-provider.c \
|
||||
camel-remote-store.c \
|
||||
camel-sasl.c \
|
||||
@ -128,6 +129,7 @@ libcamelinclude_HEADERS = \
|
||||
camel-object.h \
|
||||
camel-operation.h \
|
||||
camel-pgp-context.h \
|
||||
camel-pgp-mime.h \
|
||||
camel-provider.h \
|
||||
camel-remote-store.h \
|
||||
camel-sasl.h \
|
||||
|
||||
@ -26,11 +26,17 @@
|
||||
#endif
|
||||
|
||||
#include "camel-pgp-mime.h"
|
||||
#inlcude "camel-mime-filter-from.h"
|
||||
#include "camel-mime-filter-from.h"
|
||||
#include "camel-mime-filter-crlf.h"
|
||||
#include "camel-stream-filter.h"
|
||||
#include "camel-stream-mem.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define d(x) x
|
||||
|
||||
/** rfc2015 stuff (aka PGP/MIME) *******************************/
|
||||
|
||||
gboolean
|
||||
@ -198,8 +204,8 @@ pgp_mime_part_sign_prepare_part (CamelMimePart *mime_part, GSList **encodings)
|
||||
* @ex will be set and #part will remain untouched.
|
||||
**/
|
||||
void
|
||||
camel_pgp_mime_part_sign (CamelPgpContext *context, CamelMimePart **mime_part, const gchar *userid,
|
||||
PgpHashType hash, CamelException *ex)
|
||||
camel_pgp_mime_part_sign (CamelPgpContext *context, CamelMimePart **mime_part, const char *userid,
|
||||
CamelPgpHashType hash, CamelException *ex)
|
||||
{
|
||||
CamelMimePart *part, *signed_part;
|
||||
CamelMultipart *multipart;
|
||||
@ -213,7 +219,6 @@ camel_pgp_mime_part_sign (CamelPgpContext *context, CamelMimePart **mime_part, c
|
||||
g_return_if_fail (*mime_part != NULL);
|
||||
g_return_if_fail (CAMEL_IS_MIME_PART (*mime_part));
|
||||
g_return_if_fail (userid != NULL);
|
||||
g_return_if_fail (hash != PGP_HASH_TYPE_NONE);
|
||||
|
||||
part = *mime_part;
|
||||
|
||||
@ -249,28 +254,31 @@ camel_pgp_mime_part_sign (CamelPgpContext *context, CamelMimePart **mime_part, c
|
||||
g_slist_free (encodings);
|
||||
return;
|
||||
}
|
||||
|
||||
camel_object_unref (CAMEL_OBJECT (stream));
|
||||
camel_stream_reset (sigstream);
|
||||
|
||||
/* we don't need these anymore... */
|
||||
g_slist_free (encodings);
|
||||
|
||||
/* construct the pgp-signature mime part */
|
||||
d(fprintf (stderr, "signature:\n%*.s\n", CAMEL_STREAM_MEM (sigstream)->buffer->len,
|
||||
CAMEL_STREAM_MEM (sigstream)->buffer->data));
|
||||
signed_part = camel_mime_part_new ();
|
||||
camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (signed_part), sigstream);
|
||||
camel_mime_part_set_content (signed_part, CAMEL_STREAM_MEM (sigstream)->buffer->data,
|
||||
CAMEL_STREAM_MEM (sigstream)->buffer->len,
|
||||
"application/pgp-signature");
|
||||
camel_object_unref (CAMEL_OBJECT (sigstream));
|
||||
camel_mime_part_set_content_type (signed_part, "application/pgp-signature");
|
||||
|
||||
/* construct the container multipart/signed */
|
||||
switch (hash) {
|
||||
case PGP_HASH_TYPE_MD5:
|
||||
case CAMEL_PGP_HASH_TYPE_MD5:
|
||||
hash_type = "pgp-md5";
|
||||
break;
|
||||
case PGP_HASH_TYPE_SHA1:
|
||||
case CAMEL_PGP_HASH_TYPE_SHA1:
|
||||
hash_type = "pgp-sha1";
|
||||
break;
|
||||
default:
|
||||
/* set a reasonable default */
|
||||
hash = CAMEL_PGP_HASH_TYPE_SHA1;
|
||||
hash_type = "pgp-sha1";
|
||||
break;
|
||||
}
|
||||
@ -305,18 +313,18 @@ camel_pgp_mime_part_sign (CamelPgpContext *context, CamelMimePart **mime_part, c
|
||||
* @mime_part: a multipart/signed MIME Part
|
||||
* @ex: exception
|
||||
*
|
||||
* Returns a PgpValidity on success or NULL on fail.
|
||||
* Returns a CamelPgpValidity on success or NULL on fail.
|
||||
**/
|
||||
PgpValidity *
|
||||
CamelPgpValidity *
|
||||
camel_pgp_mime_part_verify (CamelPgpContext *context, CamelMimePart *mime_part, CamelException *ex)
|
||||
{
|
||||
CamelDataWrapper *wrapper;
|
||||
CamelMultipart *multipart;
|
||||
CamelMimePart *part, *sigpart;
|
||||
CamelStreamFilter *filtered_stream;
|
||||
CamelMimeFilter *crlf_filter;
|
||||
CamelMimeFilter *crlf_filter, *from_filter;
|
||||
CamelStream *stream, *sigstream;
|
||||
PgpValidity *valid;
|
||||
CamelPgpValidity *valid;
|
||||
|
||||
g_return_val_if_fail (mime_part != NULL, NULL);
|
||||
g_return_val_if_fail (CAMEL_IS_MIME_PART (mime_part), NULL);
|
||||
@ -332,9 +340,12 @@ camel_pgp_mime_part_verify (CamelPgpContext *context, CamelMimePart *mime_part,
|
||||
stream = camel_stream_mem_new ();
|
||||
crlf_filter = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_ENCODE,
|
||||
CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY);
|
||||
from_filter = CAMEL_MIME_FILTER (camel_mime_filter_from_new ());
|
||||
filtered_stream = camel_stream_filter_new_with_stream (stream);
|
||||
camel_stream_filter_add (filtered_stream, CAMEL_MIME_FILTER (crlf_filter));
|
||||
camel_object_unref (CAMEL_OBJECT (crlf_filter));
|
||||
camel_stream_filter_add (filtered_stream, CAMEL_MIME_FILTER (from_filter));
|
||||
camel_object_unref (CAMEL_OBJECT (from_filter));
|
||||
camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (part), CAMEL_STREAM (filtered_stream));
|
||||
camel_object_unref (CAMEL_OBJECT (filtered_stream));
|
||||
camel_stream_reset (stream);
|
||||
@ -369,7 +380,7 @@ camel_pgp_mime_part_verify (CamelPgpContext *context, CamelMimePart *mime_part,
|
||||
**/
|
||||
void
|
||||
camel_pgp_mime_part_encrypt (CamelPgpContext *context, CamelMimePart **mime_part,
|
||||
const GPtrArray *recipients, CamelException *ex)
|
||||
GPtrArray *recipients, CamelException *ex)
|
||||
{
|
||||
CamelMultipart *multipart;
|
||||
CamelMimePart *part, *version_part, *encrypted_part;
|
||||
@ -414,12 +425,13 @@ camel_pgp_mime_part_encrypt (CamelPgpContext *context, CamelMimePart **mime_part
|
||||
|
||||
/* construct the pgp-encrypted mime part */
|
||||
encrypted_part = camel_mime_part_new ();
|
||||
camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (encrypted_part), ciphertext);
|
||||
camel_mime_part_set_content (encrypted_part, CAMEL_STREAM_MEM (ciphertext)->buffer->data,
|
||||
CAMEL_STREAM_MEM (ciphertext)->buffer->len,
|
||||
"application/octet-stream");
|
||||
camel_object_unref (CAMEL_OBJECT (ciphertext));
|
||||
camel_mime_part_set_content_type (encrypted_part, "application/octet-stream");
|
||||
camel_mime_part_set_encoding (encrypted_part, CAMEL_MIME_PART_ENCODING_7BIT);
|
||||
|
||||
/* construct the container multipart/signed */
|
||||
/* construct the container multipart/encrypted */
|
||||
multipart = camel_multipart_new ();
|
||||
|
||||
mime_type = header_content_type_new ("multipart", "encrypted");
|
||||
@ -480,6 +492,7 @@ camel_pgp_mime_part_decrypt (CamelPgpContext *context, CamelMimePart *mime_part,
|
||||
/* get the ciphertext */
|
||||
ciphertext = camel_stream_mem_new ();
|
||||
camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (encrypted_part), ciphertext);
|
||||
camel_stream_reset (ciphertext);
|
||||
|
||||
/* get the cleartext */
|
||||
stream = camel_stream_mem_new ();
|
||||
|
||||
@ -25,8 +25,10 @@
|
||||
#define CAMEL_PGP_MIME_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <camel/camel-multipart.h>
|
||||
#include <camel/camel-mime-part.h>
|
||||
#include <camel/camel-pgp-context.h>
|
||||
#include <camel/camel-exception.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -38,17 +40,17 @@ gboolean camel_pgp_mime_is_rfc2015_encrypted (CamelMimePart *part);
|
||||
|
||||
void camel_pgp_mime_part_sign (CamelPgpContext *context,
|
||||
CamelMimePart **mime_part,
|
||||
const gchar *userid,
|
||||
const char *userid,
|
||||
CamelPgpHashType hash,
|
||||
CamelException *ex);
|
||||
|
||||
CamelPgpValidity *camel_pgp_mime_part_verify (CamelPgpContext,
|
||||
CamelPgpValidity *camel_pgp_mime_part_verify (CamelPgpContext *context,
|
||||
CamelMimePart *mime_part,
|
||||
CamelException *ex);
|
||||
|
||||
void camel_pgp_mime_part_encrypt (CamelPgpContext *context,
|
||||
CamelMimePart **mime_part,
|
||||
const GPtrArray *recipients,
|
||||
GPtrArray *recipients,
|
||||
CamelException *ex);
|
||||
|
||||
CamelMimePart *camel_pgp_mime_part_decrypt (CamelPgpContext *context,
|
||||
|
||||
@ -2,3 +2,4 @@
|
||||
.libs
|
||||
Makefile*
|
||||
pgp
|
||||
pgp-mime
|
||||
|
||||
@ -14,9 +14,7 @@ LDADD = \
|
||||
$(BONOBO_GNOME_LIBS)
|
||||
|
||||
check_PROGRAMS = \
|
||||
pgp
|
||||
|
||||
TESTS = pgp
|
||||
|
||||
|
||||
pgp \
|
||||
pgp-mime
|
||||
|
||||
TESTS = pgp pgp-mime
|
||||
|
||||
101
camel/tests/smime/pgp-mime.c
Normal file
101
camel/tests/smime/pgp-mime.c
Normal file
@ -0,0 +1,101 @@
|
||||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <camel/camel-pgp-mime.h>
|
||||
#include <camel/camel-stream-mem.h>
|
||||
|
||||
#include "camel-test.h"
|
||||
|
||||
static char test_msg[] = "Since we need to make sure that\nFrom lines work okay, we should test that"
|
||||
"as well as test 8bit chars and other fun stuff? 8bit chars: Dra<72>en Ka<4B>ar\n\nOkay, I guess that covers"
|
||||
"the basics at least...\n";
|
||||
|
||||
/* god, who designed this horrid interface */
|
||||
static gpointer auth_callback (CamelAuthCallbackMode mode,
|
||||
char *data, gboolean secret,
|
||||
CamelService *service, char *item,
|
||||
CamelException *ex)
|
||||
{
|
||||
return g_strdup ("PGP/MIME is rfc2015, now go and read it.");
|
||||
}
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
CamelSession *session;
|
||||
CamelPgpContext *ctx;
|
||||
CamelException *ex;
|
||||
CamelPgpValidity *valid;
|
||||
CamelMimePart *mime_part, *part;
|
||||
GPtrArray *recipients;
|
||||
|
||||
camel_test_init (argc, argv);
|
||||
|
||||
ex = camel_exception_new ();
|
||||
|
||||
/* clear out any camel-test data */
|
||||
system("/bin/rm -rf /tmp/camel-test");
|
||||
|
||||
session = camel_session_new ("/tmp/camel-test",
|
||||
auth_callback, NULL, NULL);
|
||||
|
||||
ctx = camel_pgp_context_new (session, CAMEL_PGP_TYPE_GPG, "/usr/bin/gpg");
|
||||
|
||||
camel_test_start ("Test of PGP/MIME functions");
|
||||
|
||||
mime_part = camel_mime_part_new ();
|
||||
camel_mime_part_set_content (mime_part, test_msg, strlen (test_msg), "text/plain");
|
||||
camel_mime_part_set_description (mime_part, "Test of PGP/MIME multipart/signed stuff");
|
||||
|
||||
camel_test_push ("PGP/MIME signing");
|
||||
camel_pgp_mime_part_sign (ctx, &mime_part, "pgp-mime@xtorshun.org", CAMEL_PGP_HASH_TYPE_SHA1, ex);
|
||||
check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
|
||||
check_msg (camel_pgp_mime_is_rfc2015_signed (mime_part),
|
||||
"Huh, the MIME part does not seem to be a valid multipart/signed part");
|
||||
camel_test_pull ();
|
||||
|
||||
camel_exception_clear (ex);
|
||||
|
||||
camel_test_push ("PGP/MIME verify");
|
||||
valid = camel_pgp_mime_part_verify (ctx, mime_part, ex);
|
||||
check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
|
||||
check_msg (camel_pgp_validity_get_valid (valid), "%s", camel_pgp_validity_get_description (valid));
|
||||
camel_pgp_validity_free (valid);
|
||||
camel_test_pull ();
|
||||
|
||||
camel_object_unref (CAMEL_OBJECT (mime_part));
|
||||
|
||||
camel_exception_clear (ex);
|
||||
|
||||
mime_part = camel_mime_part_new ();
|
||||
camel_mime_part_set_content (mime_part, test_msg, strlen (test_msg), "text/plain");
|
||||
camel_mime_part_set_description (mime_part, "Test of PGP/MIME multipart/encrypted stuff");
|
||||
|
||||
camel_test_push ("PGP/MIME encrypt");
|
||||
recipients = g_ptr_array_new ();
|
||||
g_ptr_array_add (recipients, "pgp-mime@xtorshun.org");
|
||||
camel_pgp_mime_part_encrypt (ctx, &mime_part, recipients, ex);
|
||||
check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
|
||||
check_msg (camel_pgp_mime_is_rfc2015_encrypted (mime_part),
|
||||
"Huh, the MIME part does not seem to be a valid multipart/encrypted part");
|
||||
g_ptr_array_free (recipients, TRUE);
|
||||
camel_test_pull ();
|
||||
|
||||
camel_exception_clear (ex);
|
||||
|
||||
camel_test_push ("PGP/MIME decrypt");
|
||||
part = camel_pgp_mime_part_decrypt (ctx, mime_part, ex);
|
||||
check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
|
||||
camel_object_unref (CAMEL_OBJECT (part));
|
||||
camel_test_pull ();
|
||||
|
||||
camel_object_unref (CAMEL_OBJECT (mime_part));
|
||||
|
||||
camel_object_unref (CAMEL_OBJECT (ctx));
|
||||
camel_object_unref (CAMEL_OBJECT (session));
|
||||
|
||||
camel_test_end ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user