102092679a9d6f568f993240de22d522f4d111aa
[platform/upstream/evolution-data-server.git] / camel / tests / smime / pgp-mime.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 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 General Public License for more details.
16  *
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.
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-multipart-signed.h>
34 #include <camel/camel-multipart-encrypted.h>
35 #include <camel/camel-mime-part.h>
36 #include <camel/camel-stream-mem.h>
37
38 #include "camel-test.h"
39 #include "session.h"
40
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";
44
45
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))
50
51
52 typedef struct _CamelPgpSession {
53         CamelSession parent_object;
54         
55 } CamelPgpSession;
56
57 typedef struct _CamelPgpSessionClass {
58         CamelSessionClass parent_class;
59         
60 } CamelPgpSessionClass;
61
62
63 static char *get_password (CamelSession *session, const char *prompt,
64                            gboolean reprompt, gboolean secret,
65                            CamelService *service, const char *item,
66                            CamelException *ex);
67
68 static void
69 init (CamelPgpSession *session)
70 {
71         ;
72 }
73
74 static void
75 class_init (CamelPgpSessionClass *camel_pgp_session_class)
76 {
77         CamelSessionClass *camel_session_class =
78                 CAMEL_SESSION_CLASS (camel_pgp_session_class);
79         
80         /* virtual method override */
81         camel_session_class->get_password = get_password;
82 }
83
84 static CamelType
85 camel_pgp_session_get_type (void)
86 {
87         static CamelType type = CAMEL_INVALID_TYPE;
88         
89         if (type == CAMEL_INVALID_TYPE) {
90                 type = camel_type_register (
91                         camel_test_session_get_type (),
92                         "CamelPgpSession",
93                         sizeof (CamelPgpSession),
94                         sizeof (CamelPgpSessionClass),
95                         (CamelObjectClassInitFunc) class_init,
96                         NULL,
97                         (CamelObjectInitFunc) init,
98                         NULL);
99         }
100         
101         return type;
102 }
103
104 static char *
105 get_password (CamelSession *session, const char *prompt, gboolean reprompt, gboolean secret,
106               CamelService *service, const char *item, CamelException *ex)
107 {
108         return g_strdup ("no.secret");
109 }
110
111 static CamelSession *
112 camel_pgp_session_new (const char *path)
113 {
114         CamelSession *session;
115         
116         session = CAMEL_SESSION (camel_object_new (CAMEL_PGP_SESSION_TYPE));
117         
118         camel_session_construct (session, path);
119         
120         return session;
121 }
122
123
124 int main (int argc, char **argv)
125 {
126         CamelSession *session;
127         CamelCipherContext *ctx;
128         CamelException *ex;
129         CamelCipherValidity *valid;
130         CamelMimePart *mime_part;
131         CamelMultipartSigned *mps;
132         CamelMultipartEncrypted *mpe;
133         GPtrArray *recipients;
134         int ret;
135
136         camel_test_init (argc, argv);
137         
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);
142         
143         /* import the gpg keys */
144         if ((ret = system ("gpg < /dev/null > /dev/null 2>&1")) == -1)
145                 return 77;
146         else if (WEXITSTATUS (ret) == 127)
147                 return 77;
148         
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");
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/MIME functions");
160         
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");
164         
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));
169         camel_test_pull ();
170         
171         camel_object_unref (mime_part);
172         camel_exception_clear (ex);
173         
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);
179         camel_test_pull ();
180         
181         camel_object_unref (mps);
182         camel_exception_clear (ex);
183         
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");
187         
188         camel_test_push ("PGP/MIME encrypt");
189         recipients = g_ptr_array_new ();
190         g_ptr_array_add (recipients, "no.user@no.domain");
191         
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);
196         camel_test_pull ();
197         
198         camel_exception_clear (ex);
199         camel_object_unref (mime_part);
200         
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);
206         camel_test_pull ();
207         
208         camel_object_unref (CAMEL_OBJECT (ctx));
209         camel_object_unref (CAMEL_OBJECT (session));
210         
211         camel_test_end ();
212         
213         return 0;
214 }