gio/tests/giomodule.c: Use G_MODULE_SUFFIX
[platform/upstream/glib.git] / gio / tests / gtesttlsbackend.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2011 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "gtesttlsbackend.h"
22
23 #include <glib.h>
24
25 static GType _g_test_tls_certificate_get_type (void);
26 static GType _g_test_tls_connection_get_type (void);
27
28 struct _GTestTlsBackend {
29   GObject parent_instance;
30 };
31
32 static void g_test_tls_backend_iface_init (GTlsBackendInterface *iface);
33
34 #define g_test_tls_backend_get_type _g_test_tls_backend_get_type
35 G_DEFINE_TYPE_WITH_CODE (GTestTlsBackend, g_test_tls_backend, G_TYPE_OBJECT,
36                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
37                                                 g_test_tls_backend_iface_init)
38                          g_io_extension_point_set_required_type (
39                            g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME),
40                            G_TYPE_TLS_BACKEND);
41                          g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
42                                                          g_define_type_id,
43                                                          "test",
44                                                          999))
45
46 static void
47 g_test_tls_backend_init (GTestTlsBackend *backend)
48 {
49 }
50
51 static void
52 g_test_tls_backend_class_init (GTestTlsBackendClass *backend_class)
53 {
54 }
55
56 static void
57 g_test_tls_backend_iface_init (GTlsBackendInterface *iface)
58 {
59   iface->get_certificate_type = _g_test_tls_certificate_get_type;
60   iface->get_client_connection_type = _g_test_tls_connection_get_type;
61   iface->get_server_connection_type = _g_test_tls_connection_get_type;
62 }
63
64 /* Test certificate type */
65
66 typedef struct _GTestTlsCertificate      GTestTlsCertificate;
67 typedef struct _GTestTlsCertificateClass GTestTlsCertificateClass;
68
69 struct _GTestTlsCertificate {
70   GTlsCertificate parent_instance;
71   gchar *key_pem;
72   gchar *cert_pem;
73 };
74
75 struct _GTestTlsCertificateClass {
76   GTlsCertificateClass parent_class;
77 };
78
79 enum
80 {
81   PROP_CERTIFICATE_0,
82
83   PROP_CERT_CERTIFICATE,
84   PROP_CERT_CERTIFICATE_PEM,
85   PROP_CERT_PRIVATE_KEY,
86   PROP_CERT_PRIVATE_KEY_PEM,
87   PROP_CERT_ISSUER
88 };
89
90 static void g_test_tls_certificate_initable_iface_init (GInitableIface *iface);
91
92 #define g_test_tls_certificate_get_type _g_test_tls_certificate_get_type
93 G_DEFINE_TYPE_WITH_CODE (GTestTlsCertificate, g_test_tls_certificate, G_TYPE_TLS_CERTIFICATE,
94                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
95                                                 g_test_tls_certificate_initable_iface_init);)
96
97 static void
98 g_test_tls_certificate_get_property (GObject    *object,
99                                       guint       prop_id,
100                                       GValue     *value,
101                                       GParamSpec *pspec)
102 {
103   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
104
105   switch (prop_id)
106     {
107     case PROP_CERT_CERTIFICATE_PEM:
108       g_value_set_string (value, cert->cert_pem);
109       break;
110     case PROP_CERT_PRIVATE_KEY_PEM:
111       g_value_set_string (value, cert->key_pem);
112       break;
113     default:
114       g_assert_not_reached ();
115       break;
116     }
117 }
118
119 static void
120 g_test_tls_certificate_set_property (GObject      *object,
121                                       guint         prop_id,
122                                       const GValue *value,
123                                       GParamSpec   *pspec)
124 {
125   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
126
127   switch (prop_id)
128     {
129     case PROP_CERT_CERTIFICATE_PEM:
130       cert->cert_pem = g_value_dup_string (value);
131       break;
132     case PROP_CERT_PRIVATE_KEY_PEM:
133       cert->key_pem = g_value_dup_string (value);
134       break;
135     case PROP_CERT_CERTIFICATE:
136     case PROP_CERT_PRIVATE_KEY:
137     case PROP_CERT_ISSUER:
138       /* ignore */
139       break;
140     default:
141       g_assert_not_reached ();
142       break;
143     }
144 }
145
146 static void
147 g_test_tls_certificate_finalize (GObject *object)
148 {
149   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
150
151   g_free (cert->cert_pem);
152   g_free (cert->key_pem);
153 }
154
155 static void
156 g_test_tls_certificate_class_init (GTestTlsCertificateClass *certificate_class)
157 {
158   GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
159
160   gobject_class->get_property = g_test_tls_certificate_get_property;
161   gobject_class->set_property = g_test_tls_certificate_set_property;
162   gobject_class->finalize = g_test_tls_certificate_finalize;
163
164   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
165   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
166   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
167   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
168   g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
169 }
170
171 static void
172 g_test_tls_certificate_init (GTestTlsCertificate *certificate)
173 {
174 }
175
176 static gboolean
177 g_test_tls_certificate_initable_init (GInitable       *initable,
178                                        GCancellable    *cancellable,
179                                        GError         **error)
180 {
181   return TRUE;
182 }
183
184 static void
185 g_test_tls_certificate_initable_iface_init (GInitableIface  *iface)
186 {
187   iface->init = g_test_tls_certificate_initable_init;
188 }
189
190 /* Dummy connection type; since GTlsClientConnection and
191  * GTlsServerConnection are just interfaces, we can implement them
192  * both on a single object.
193  */
194
195 typedef struct _GTestTlsConnection      GTestTlsConnection;
196 typedef struct _GTestTlsConnectionClass GTestTlsConnectionClass;
197
198 struct _GTestTlsConnection {
199   GTlsConnection parent_instance;
200 };
201
202 struct _GTestTlsConnectionClass {
203   GTlsConnectionClass parent_class;
204 };
205
206 enum
207 {
208   PROP_CONNECTION_0,
209
210   PROP_CONN_BASE_IO_STREAM,
211   PROP_CONN_USE_SYSTEM_CERTDB,
212   PROP_CONN_REQUIRE_CLOSE_NOTIFY,
213   PROP_CONN_REHANDSHAKE_MODE,
214   PROP_CONN_CERTIFICATE,
215   PROP_CONN_PEER_CERTIFICATE,
216   PROP_CONN_PEER_CERTIFICATE_ERRORS,
217   PROP_CONN_VALIDATION_FLAGS,
218   PROP_CONN_SERVER_IDENTITY,
219   PROP_CONN_USE_SSL3,
220   PROP_CONN_ACCEPTED_CAS,
221   PROP_CONN_AUTHENTICATION_MODE
222 };
223
224 static void g_test_tls_connection_initable_iface_init (GInitableIface *iface);
225
226 #define g_test_tls_connection_get_type _g_test_tls_connection_get_type
227 G_DEFINE_TYPE_WITH_CODE (GTestTlsConnection, g_test_tls_connection, G_TYPE_TLS_CONNECTION,
228                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
229                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
230                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
231                                                 g_test_tls_connection_initable_iface_init);)
232
233 static void
234 g_test_tls_connection_get_property (GObject    *object,
235                                      guint       prop_id,
236                                      GValue     *value,
237                                      GParamSpec *pspec)
238 {
239 }
240
241 static void
242 g_test_tls_connection_set_property (GObject      *object,
243                                      guint         prop_id,
244                                      const GValue *value,
245                                      GParamSpec   *pspec)
246 {
247 }
248
249 static gboolean
250 g_test_tls_connection_close (GIOStream     *stream,
251                               GCancellable  *cancellable,
252                               GError       **error)
253 {
254   return TRUE;
255 }
256
257 static void
258 g_test_tls_connection_class_init (GTestTlsConnectionClass *connection_class)
259 {
260   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
261   GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
262
263   gobject_class->get_property = g_test_tls_connection_get_property;
264   gobject_class->set_property = g_test_tls_connection_set_property;
265
266   /* Need to override this because when initable_init fails it will
267    * dispose the connection, which will close it, which would
268    * otherwise try to close its input/output streams, which don't
269    * exist.
270    */
271   io_stream_class->close_fn = g_test_tls_connection_close;
272
273   g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
274   g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
275   g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
276   g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
277   g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
278   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
279   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
280   g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
281   g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
282   g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
283   g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
284   g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
285 }
286
287 static void
288 g_test_tls_connection_init (GTestTlsConnection *connection)
289 {
290 }
291
292 static gboolean
293 g_test_tls_connection_initable_init (GInitable       *initable,
294                                       GCancellable    *cancellable,
295                                       GError         **error)
296 {
297   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
298                        "TLS Connection support is not available");
299   return FALSE;
300 }
301
302 static void
303 g_test_tls_connection_initable_iface_init (GInitableIface  *iface)
304 {
305   iface->init = g_test_tls_connection_initable_init;
306 }
307
308 const gchar *
309 g_test_tls_connection_get_private_key_pem (GTlsCertificate *cert)
310 {
311   return ((GTestTlsCertificate *)cert)->key_pem;
312 }