Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / tests / smime / pgp.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *
5  *  Copyright 2003 Ximian, Inc. (www.ximian.com)
6  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/wait.h>
32 #include <camel/camel-gpg-context.h>
33 #include <camel/camel-stream-mem.h>
34 #include <camel/camel-mime-part.h>
35
36 #include "camel-test.h"
37 #include "session.h"
38
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))
43
44
45 typedef struct _CamelPgpSession {
46         CamelSession parent_object;
47         
48 } CamelPgpSession;
49
50 typedef struct _CamelPgpSessionClass {
51         CamelSessionClass parent_class;
52         
53 } CamelPgpSessionClass;
54
55
56 static char *get_password (CamelSession *session, const char *prompt,
57                            guint32 flags,
58                            CamelService *service, const char *item,
59                            CamelException *ex);
60
61 static void
62 init (CamelPgpSession *session)
63 {
64         ;
65 }
66
67 static void
68 class_init (CamelPgpSessionClass *camel_pgp_session_class)
69 {
70         CamelSessionClass *camel_session_class =
71                 CAMEL_SESSION_CLASS (camel_pgp_session_class);
72         
73         /* virtual method override */
74         camel_session_class->get_password = get_password;
75 }
76
77 static CamelType
78 camel_pgp_session_get_type (void)
79 {
80         static CamelType type = CAMEL_INVALID_TYPE;
81         
82         if (type == CAMEL_INVALID_TYPE) {
83                 type = camel_type_register (
84                         camel_test_session_get_type (),
85                         "CamelPgpSession",
86                         sizeof (CamelPgpSession),
87                         sizeof (CamelPgpSessionClass),
88                         (CamelObjectClassInitFunc) class_init,
89                         NULL,
90                         (CamelObjectInitFunc) init,
91                         NULL);
92         }
93         
94         return type;
95 }
96
97 static char *
98 get_password (CamelSession *session, const char *prompt, guint32 flags,
99               CamelService *service, const char *item, CamelException *ex)
100 {
101         return g_strdup ("no.secret");
102 }
103
104 static CamelSession *
105 camel_pgp_session_new (const char *path)
106 {
107         CamelSession *session;
108         
109         session = CAMEL_SESSION (camel_object_new (CAMEL_PGP_SESSION_TYPE));
110         
111         camel_session_construct (session, path);
112         
113         return session;
114 }
115
116
117 int main (int argc, char **argv)
118 {
119         CamelSession *session;
120         CamelCipherContext *ctx;
121         CamelException *ex;
122         CamelCipherValidity *valid;
123         CamelStream *stream1, *stream2;
124         struct _CamelMimePart *sigpart, *conpart, *encpart, *outpart;
125         CamelDataWrapper *dw;
126         GPtrArray *recipients;
127         GByteArray *buf;
128         char *before, *after;
129         int ret;
130
131         if (getenv("CAMEL_TEST_GPG") == NULL)
132                 return 77;
133         
134         camel_test_init (argc, argv);
135         
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);
140         
141         /* import the gpg keys */
142         if ((ret = system ("gpg < /dev/null > /dev/null 2>&1")) == -1)
143                 return 77;
144         else if (WEXITSTATUS (ret) == 127)
145                 return 77;
146
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");
151
152         session = camel_pgp_session_new ("/tmp/camel-test");
153         
154         ex = camel_exception_new ();
155         
156         ctx = camel_gpg_context_new (session);
157         camel_gpg_context_set_always_trust (CAMEL_GPG_CONTEXT (ctx), TRUE);
158         
159         camel_test_start ("Test of PGP functions");
160
161         stream1 = camel_stream_mem_new ();
162         camel_stream_write (stream1, "Hello, I am a test stream.\n", 27);
163         camel_stream_reset (stream1);
164
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);
171
172         sigpart = camel_mime_part_new();
173
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));
178                 camel_test_pull();
179                 return 77;
180         }
181         camel_test_pull ();
182         
183         camel_exception_clear (ex);
184         
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);
190         camel_test_pull ();
191         
192         camel_object_unref(conpart);
193         camel_object_unref(sigpart);
194         
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);
198
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);
206
207         encpart = camel_mime_part_new();
208         
209         camel_exception_clear (ex);
210         
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);
217         camel_test_pull ();
218
219         camel_exception_clear (ex);
220         
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);
226
227         stream1 = camel_stream_mem_new();
228         stream2 = camel_stream_mem_new();
229
230         camel_data_wrapper_write_to_stream((CamelDataWrapper *)conpart, stream1);
231         camel_data_wrapper_write_to_stream((CamelDataWrapper *)outpart, stream2);
232
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);
238         g_free (before);
239         g_free (after);
240
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);
246
247         camel_test_pull ();
248         
249         camel_object_unref (CAMEL_OBJECT (ctx));
250         camel_object_unref (CAMEL_OBJECT (session));
251         
252         camel_test_end ();
253         
254         return 0;
255 }