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 Lesser 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 Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 #include <camel/camel-gpg-context.h>
33 #include <camel/camel-stream-mem.h>
34 #include <camel/camel-mime-part.h>
36 #include "camel-test.h"
39 #define CAMEL_PGP_SESSION_TYPE (camel_pgp_session_get_type ())
40 #define CAMEL_PGP_SESSION(obj) (CAMEL_CHECK_CAST((obj), CAMEL_PGP_SESSION_TYPE, CamelPgpSession))
41 #define CAMEL_PGP_SESSION_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), CAMEL_PGP_SESSION_TYPE, CamelPgpSessionClass))
42 #define CAMEL_PGP_IS_SESSION(o) (CAMEL_CHECK_TYPE((o), CAMEL_PGP_SESSION_TYPE))
45 typedef struct _CamelPgpSession {
46 CamelSession parent_object;
50 typedef struct _CamelPgpSessionClass {
51 CamelSessionClass parent_class;
53 } CamelPgpSessionClass;
56 static char *get_password (CamelSession *session, const char *prompt,
58 CamelService *service, const char *item,
62 init (CamelPgpSession *session)
68 class_init (CamelPgpSessionClass *camel_pgp_session_class)
70 CamelSessionClass *camel_session_class =
71 CAMEL_SESSION_CLASS (camel_pgp_session_class);
73 /* virtual method override */
74 camel_session_class->get_password = get_password;
78 camel_pgp_session_get_type (void)
80 static CamelType type = CAMEL_INVALID_TYPE;
82 if (type == CAMEL_INVALID_TYPE) {
83 type = camel_type_register (
84 camel_test_session_get_type (),
86 sizeof (CamelPgpSession),
87 sizeof (CamelPgpSessionClass),
88 (CamelObjectClassInitFunc) class_init,
90 (CamelObjectInitFunc) init,
98 get_password (CamelSession *session, const char *prompt, guint32 flags,
99 CamelService *service, const char *item, CamelException *ex)
101 return g_strdup ("no.secret");
104 static CamelSession *
105 camel_pgp_session_new (const char *path)
107 CamelSession *session;
109 session = CAMEL_SESSION (camel_object_new (CAMEL_PGP_SESSION_TYPE));
111 camel_session_construct (session, path);
117 int main (int argc, char **argv)
119 CamelSession *session;
120 CamelCipherContext *ctx;
122 CamelCipherValidity *valid;
123 CamelStream *stream1, *stream2;
124 struct _CamelMimePart *sigpart, *conpart, *encpart, *outpart;
125 CamelDataWrapper *dw;
126 GPtrArray *recipients;
128 char *before, *after;
131 if (getenv("CAMEL_TEST_GPG") == NULL)
134 camel_test_init (argc, argv);
136 /* clear out any camel-test data */
137 system ("/bin/rm -rf /tmp/camel-test");
138 system ("/bin/mkdir /tmp/camel-test");
139 setenv ("GNUPGHOME", "/tmp/camel-test/.gnupg", 1);
141 /* import the gpg keys */
142 if ((ret = system ("gpg < /dev/null > /dev/null 2>&1")) == -1)
144 else if (WEXITSTATUS (ret) == 127)
147 g_message ("gpg --import " TEST_DATA_DIR "/camel-test.gpg.pub > /dev/null 2>&1");
148 system ("gpg --import " TEST_DATA_DIR "/camel-test.gpg.pub > /dev/null 2>&1");
149 g_message ("gpg --import " TEST_DATA_DIR "/camel-test.gpg.sec > /dev/null 2>&1");
150 system ("gpg --import " TEST_DATA_DIR "/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 functions");
161 stream1 = camel_stream_mem_new ();
162 camel_stream_write (stream1, "Hello, I am a test stream.\n", 27);
163 camel_stream_reset (stream1);
165 conpart = camel_mime_part_new();
166 dw = camel_data_wrapper_new();
167 camel_data_wrapper_construct_from_stream(dw, stream1);
168 camel_medium_set_content_object((CamelMedium *)conpart, dw);
169 camel_object_unref(stream1);
170 camel_object_unref(dw);
172 sigpart = camel_mime_part_new();
174 camel_test_push ("PGP signing");
175 camel_cipher_sign (ctx, "no.user@no.domain", CAMEL_CIPHER_HASH_SHA1, conpart, sigpart, ex);
176 if (camel_exception_is_set(ex)) {
177 printf("PGP signing failed assuming non-functional environment\n%s", camel_exception_get_description (ex));
183 camel_exception_clear (ex);
185 camel_test_push ("PGP verify");
186 valid = camel_cipher_verify (ctx, sigpart, ex);
187 check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
188 check_msg (camel_cipher_validity_get_valid (valid), "%s", camel_cipher_validity_get_description (valid));
189 camel_cipher_validity_free (valid);
192 camel_object_unref(conpart);
193 camel_object_unref(sigpart);
195 stream1 = camel_stream_mem_new ();
196 camel_stream_write (stream1, "Hello, I am a test of encryption/decryption.", 44);
197 camel_stream_reset (stream1);
199 conpart = camel_mime_part_new();
200 dw = camel_data_wrapper_new();
201 camel_stream_reset(stream1);
202 camel_data_wrapper_construct_from_stream(dw, stream1);
203 camel_medium_set_content_object((CamelMedium *)conpart, dw);
204 camel_object_unref(stream1);
205 camel_object_unref(dw);
207 encpart = camel_mime_part_new();
209 camel_exception_clear (ex);
211 camel_test_push ("PGP encrypt");
212 recipients = g_ptr_array_new ();
213 g_ptr_array_add (recipients, "no.user@no.domain");
214 camel_cipher_encrypt (ctx, "no.user@no.domain", recipients, conpart, encpart, ex);
215 check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
216 g_ptr_array_free (recipients, TRUE);
219 camel_exception_clear (ex);
221 camel_test_push ("PGP decrypt");
222 outpart = camel_mime_part_new();
223 valid = camel_cipher_decrypt (ctx, encpart, outpart, ex);
224 check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
225 check_msg (valid->encrypt.status == CAMEL_CIPHER_VALIDITY_ENCRYPT_ENCRYPTED, "%s", valid->encrypt.description);
227 stream1 = camel_stream_mem_new();
228 stream2 = camel_stream_mem_new();
230 camel_data_wrapper_write_to_stream((CamelDataWrapper *)conpart, stream1);
231 camel_data_wrapper_write_to_stream((CamelDataWrapper *)outpart, stream2);
233 buf = CAMEL_STREAM_MEM (stream1)->buffer;
234 before = g_strndup (buf->data, buf->len);
235 buf = CAMEL_STREAM_MEM (stream2)->buffer;
236 after = g_strndup (buf->data, buf->len);
237 check_msg (string_equal (before, after), "before = '%s', after = '%s'", before, after);
241 camel_object_unref(stream1);
242 camel_object_unref(stream2);
243 camel_object_unref(conpart);
244 camel_object_unref(encpart);
245 camel_object_unref(outpart);
249 camel_object_unref (CAMEL_OBJECT (ctx));
250 camel_object_unref (CAMEL_OBJECT (session));