1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright 2003 Ximian, Inc. (www.ximian.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
32 #include <camel/camel-gpg-context.h>
33 #include <camel/camel-multipart-signed.h>
34 #include <camel/camel-multipart-encrypted.h>
35 #include <camel/camel-mime-part.h>
36 #include <camel/camel-stream-mem.h>
38 #include "camel-test.h"
41 static char test_msg[] = "Since we need to make sure that\nFrom lines work okay, we should test that"
42 "as well as test 8bit chars and other fun stuff? 8bit chars: Dra¾en Kaèar\n\nOkay, I guess that covers"
43 "the basics at least...\n";
46 #define CAMEL_PGP_SESSION_TYPE (camel_pgp_session_get_type ())
47 #define CAMEL_PGP_SESSION(obj) (CAMEL_CHECK_CAST((obj), CAMEL_PGP_SESSION_TYPE, CamelPgpSession))
48 #define CAMEL_PGP_SESSION_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), CAMEL_PGP_SESSION_TYPE, CamelPgpSessionClass))
49 #define CAMEL_PGP_IS_SESSION(o) (CAMEL_CHECK_TYPE((o), CAMEL_PGP_SESSION_TYPE))
52 typedef struct _CamelPgpSession {
53 CamelSession parent_object;
57 typedef struct _CamelPgpSessionClass {
58 CamelSessionClass parent_class;
60 } CamelPgpSessionClass;
63 static char *get_password (CamelSession *session, const char *prompt,
64 gboolean reprompt, gboolean secret,
65 CamelService *service, const char *item,
69 init (CamelPgpSession *session)
75 class_init (CamelPgpSessionClass *camel_pgp_session_class)
77 CamelSessionClass *camel_session_class =
78 CAMEL_SESSION_CLASS (camel_pgp_session_class);
80 /* virtual method override */
81 camel_session_class->get_password = get_password;
85 camel_pgp_session_get_type (void)
87 static CamelType type = CAMEL_INVALID_TYPE;
89 if (type == CAMEL_INVALID_TYPE) {
90 type = camel_type_register (
91 camel_test_session_get_type (),
93 sizeof (CamelPgpSession),
94 sizeof (CamelPgpSessionClass),
95 (CamelObjectClassInitFunc) class_init,
97 (CamelObjectInitFunc) init,
105 get_password (CamelSession *session, const char *prompt, gboolean reprompt, gboolean secret,
106 CamelService *service, const char *item, CamelException *ex)
108 return g_strdup ("no.secret");
111 static CamelSession *
112 camel_pgp_session_new (const char *path)
114 CamelSession *session;
116 session = CAMEL_SESSION (camel_object_new (CAMEL_PGP_SESSION_TYPE));
118 camel_session_construct (session, path);
124 int main (int argc, char **argv)
126 CamelSession *session;
127 CamelCipherContext *ctx;
129 CamelCipherValidity *valid;
130 CamelMimePart *mime_part;
131 CamelMultipartSigned *mps;
132 CamelMultipartEncrypted *mpe;
133 GPtrArray *recipients;
136 camel_test_init (argc, argv);
138 /* clear out any camel-test data */
139 system ("/bin/rm -rf /tmp/camel-test");
140 system ("/bin/mkdir /tmp/camel-test");
141 setenv ("GNUPGHOME", "/tmp/camel-test/.gnupg", 1);
143 /* import the gpg keys */
144 if ((ret = system ("gpg < /dev/null > /dev/null 2>&1")) == -1)
146 else if (WEXITSTATUS (ret) == 127)
149 system ("gpg --import ../data/camel-test.gpg.pub > /dev/null 2>&1");
150 system ("gpg --import ../data/camel-test.gpg.sec > /dev/null 2>&1");
152 session = camel_pgp_session_new ("/tmp/camel-test");
154 ex = camel_exception_new ();
156 ctx = camel_gpg_context_new (session);
157 camel_gpg_context_set_always_trust (CAMEL_GPG_CONTEXT (ctx), TRUE);
159 camel_test_start ("Test of PGP/MIME functions");
161 mime_part = camel_mime_part_new ();
162 camel_mime_part_set_content (mime_part, test_msg, strlen (test_msg), "text/plain");
163 camel_mime_part_set_description (mime_part, "Test of PGP/MIME multipart/signed stuff");
165 camel_test_push ("PGP/MIME signing");
166 mps = camel_multipart_signed_new ();
167 camel_multipart_signed_sign (mps, ctx, mime_part, "no.user@no.domain", CAMEL_CIPHER_HASH_SHA1, ex);
168 check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
171 camel_object_unref (mime_part);
172 camel_exception_clear (ex);
174 camel_test_push ("PGP/MIME verify");
175 valid = camel_multipart_signed_verify (mps, ctx, ex);
176 check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
177 check_msg (camel_cipher_validity_get_valid (valid), "%s", camel_cipher_validity_get_description (valid));
178 camel_cipher_validity_free (valid);
181 camel_object_unref (mps);
182 camel_exception_clear (ex);
184 mime_part = camel_mime_part_new ();
185 camel_mime_part_set_content (mime_part, test_msg, strlen (test_msg), "text/plain");
186 camel_mime_part_set_description (mime_part, "Test of PGP/MIME multipart/encrypted stuff");
188 camel_test_push ("PGP/MIME encrypt");
189 recipients = g_ptr_array_new ();
190 g_ptr_array_add (recipients, "no.user@no.domain");
192 mpe = camel_multipart_encrypted_new ();
193 camel_multipart_encrypted_encrypt (mpe, mime_part, ctx, "no.user@no.domain", recipients, ex);
194 check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
195 g_ptr_array_free (recipients, TRUE);
198 camel_exception_clear (ex);
199 camel_object_unref (mime_part);
201 camel_test_push ("PGP/MIME decrypt");
202 mime_part = camel_multipart_encrypted_decrypt (mpe, ctx, ex);
203 check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
204 camel_object_unref (mime_part);
205 camel_object_unref (mpe);
208 camel_object_unref (CAMEL_OBJECT (ctx));
209 camel_object_unref (CAMEL_OBJECT (session));