1 /*
2  * This library is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This library is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this library. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
15 
16 #include "evolution-data-server-config.h"
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include "camel-test.h"
23 
24 #define CAMEL_TEST_SESSION_TYPE     (camel_test_session_get_type ())
25 #define CAMEL_TEST_SESSION(obj)     (G_TYPE_CHECK_INSTANCE_CAST((obj), CAMEL_TEST_SESSION_TYPE, CamelTestSession))
26 #define CAMEL_TEST_SESSION_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), CAMEL_TEST_SESSION_TYPE, CamelTestSessionClass))
27 #define CAMEL_TEST_IS_SESSION(o)    (G_TYPE_CHECK_INSTANCE_TYPE((o), CAMEL_TEST_SESSION_TYPE))
28 
29 typedef struct _CamelTestSession {
30 	CamelSession parent_object;
31 
32 } CamelTestSession;
33 
34 typedef struct _CamelTestSessionClass {
35 	CamelSessionClass parent_class;
36 
37 } CamelTestSessionClass;
38 
39 static gchar *get_password (CamelSession *session, const gchar *prompt,
40 			   guint32 flags, CamelService *service,
41 			   const gchar *item, GError **error);
42 
43 static void
init(CamelTestSession * session)44 init (CamelTestSession *session)
45 {
46 	;
47 }
48 
49 static void
class_init(CamelTestSessionClass * camel_test_session_class)50 class_init (CamelTestSessionClass *camel_test_session_class)
51 {
52 	CamelSessionClass *camel_session_class =
53 		CAMEL_SESSION_CLASS (camel_test_session_class);
54 
55 	camel_session_class->get_password = get_password;
56 }
57 
58 static GType
camel_test_session_get_type(void)59 camel_test_session_get_type (void)
60 {
61 	static GType type = G_TYPE_INVALID;
62 
63 	if (G_UNLIKELY (type == G_TYPE_INVALID))
64 		type = camel_type_register (
65 			CAMEL_TEST_SESSION_TYPE,
66 			"CamelTestSession",
67 			sizeof (CamelTestSession),
68 			sizeof (CamelTestSessionClass),
69 			(GClassInitFunc) class_init,
70 			NULL,
71 			(GInstanceInitFunc) init,
72 			NULL);
73 
74 	return type;
75 }
76 
77 static gchar *
get_password(CamelSession * session,const gchar * prompt,guint32 flags,CamelService * service,const gchar * item,GError ** error)78 get_password (CamelSession *session,
79               const gchar *prompt,
80               guint32 flags,
81               CamelService *service,
82               const gchar *item,
83               GError **error)
84 {
85 	return g_strdup ("S/MIME v3 is rfc263x, now go and read them.");
86 }
87 
88 static CamelSession *
camel_test_session_new(const gchar * path)89 camel_test_session_new (const gchar *path)
90 {
91 	CamelSession *session;
92 
93 	session = g_object_new (CAMEL_TYPE_TEST_SESSION, NULL);
94 	camel_session_construct (session, path);
95 
96 	return session;
97 }
98 
main(gint argc,gchar ** argv)99 gint main (gint argc, gchar **argv)
100 {
101 	CamelSession *session;
102 	CamelSMimeContext *ctx;
103 	GError **error;
104 	CamelCipherValidity *valid;
105 	CamelStream *stream1, *stream2, *stream3;
106 	GPtrArray *recipients;
107 	GByteArray *buf;
108 	gchar *before, *after;
109 
110 	camel_test_init (argc, argv);
111 
112 	ex = camel_exception_new ();
113 
114 	/* clear out any camel-test data */
115 	system ("/bin/rm -rf /tmp/camel-test");
116 
117 	session = camel_test_session_new ("/tmp/camel-test");
118 
119 	ctx = camel_smime_context_new (session);
120 
121 	camel_test_start ("Test of S/MIME PKCS7 functions");
122 
123 	stream1 = camel_stream_mem_new ();
124 	camel_stream_write (stream1, "Hello, I am a test stream.", 25);
125 	g_seekable_seek (G_SEEKABLE (stream1), 0, G_SEEK_SET, NULL, NULL);
126 
127 	stream2 = camel_stream_mem_new ();
128 
129 	camel_test_push ("PKCS7 signing");
130 	camel_smime_sign (
131 		ctx, "smime@xtorshun.org", CAMEL_CIPHER_HASH_SHA256,
132 		stream1, stream2, ex);
133 	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
134 	camel_test_pull ();
135 
136 	camel_exception_clear (ex);
137 
138 	camel_test_push ("PKCS7 verify");
139 	g_seekable_seek (G_SEEKABLE (stream1), 0, G_SEEK_SET, NULL, NULL);
140 	g_seekable_seek (G_SEEKABLE (stream2), 0, G_SEEK_SET, NULL, NULL);
141 	valid = camel_smime_verify (ctx, CAMEL_CIPHER_HASH_SHA256, stream1, stream2, ex);
142 	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
143 	check_msg (camel_cipher_validity_get_valid (valid), "%s", camel_cipher_validity_get_description (valid));
144 	camel_cipher_validity_free (valid);
145 	camel_test_pull ();
146 
147 	g_object_unref (stream1);
148 	g_object_unref (stream2);
149 
150 	stream1 = camel_stream_mem_new ();
151 	stream2 = camel_stream_mem_new ();
152 	stream3 = camel_stream_mem_new ();
153 
154 	camel_stream_write (stream1, "Hello, I am a test of encryption/decryption.", 44);
155 	g_seekable_seek (G_SEEKABLE (stream1), 0, G_SEEK_SET, NULL, NULL);
156 
157 	camel_exception_clear (ex);
158 
159 	camel_test_push ("PKCS7 encrypt");
160 	recipients = g_ptr_array_new ();
161 	g_ptr_array_add (recipients, "smime@xtorshun.org");
162 	camel_smime_encrypt (
163 		ctx, FALSE, "smime@xtorshun.org", recipients,
164 		stream1, stream2, ex);
165 	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
166 	g_ptr_array_free (recipients, TRUE);
167 	camel_test_pull ();
168 
169 	g_seekable_seek (G_SEEKABLE (stream2), 0, G_SEEK_SET, NULL, NULL);
170 	camel_exception_clear (ex);
171 
172 	camel_test_push ("PKCS7 decrypt");
173 	camel_smime_decrypt (ctx, stream2, stream3, ex);
174 	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
175 	buf = CAMEL_STREAM_MEM (stream1)->buffer;
176 	before = g_strndup (buf->data, buf->len);
177 	buf = CAMEL_STREAM_MEM (stream3)->buffer;
178 	after = g_strndup (buf->data, buf->len);
179 	check_msg (string_equal (before, after), "before = '%s', after = '%s'", before, after);
180 	g_free (before);
181 	g_free (after);
182 	camel_test_pull ();
183 
184 	g_object_unref (ctx);
185 	g_object_unref (session);
186 
187 	camel_test_end ();
188 
189 	return 0;
190 }
191