Improve test coverage for GSubprocess
[platform/upstream/glib.git] / gio / gdummytlsbackend.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
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 "config.h"
22
23 #include "gdummytlsbackend.h"
24
25 #include <glib.h>
26
27 #include "gasyncresult.h"
28 #include "gcancellable.h"
29 #include "ginitable.h"
30 #include "gtlsbackend.h"
31 #include "gtlscertificate.h"
32 #include "gtlsclientconnection.h"
33 #include "gtlsdatabase.h"
34 #include "gtlsfiledatabase.h"
35 #include "gtlsserverconnection.h"
36 #include "gsimpleasyncresult.h"
37
38 #include "giomodule.h"
39 #include "giomodule-priv.h"
40
41 #include "glibintl.h"
42
43 static GType _g_dummy_tls_certificate_get_type (void);
44 static GType _g_dummy_tls_connection_get_type (void);
45 static GType _g_dummy_tls_database_get_type (void);
46
47 struct _GDummyTlsBackend {
48   GObject       parent_instance;
49   GTlsDatabase *database;
50 };
51
52 static void g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface);
53
54 #define g_dummy_tls_backend_get_type _g_dummy_tls_backend_get_type
55 G_DEFINE_TYPE_WITH_CODE (GDummyTlsBackend, g_dummy_tls_backend, G_TYPE_OBJECT,
56                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
57                                                 g_dummy_tls_backend_iface_init)
58                          _g_io_modules_ensure_extension_points_registered ();
59                          g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
60                                                          g_define_type_id,
61                                                          "dummy",
62                                                          -100))
63
64 static void
65 g_dummy_tls_backend_init (GDummyTlsBackend *dummy)
66 {
67 }
68
69 static void
70 g_dummy_tls_backend_finalize (GObject *object)
71 {
72   GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (object);
73
74   g_clear_object (&dummy->database);
75
76   G_OBJECT_CLASS (g_dummy_tls_backend_parent_class)->finalize (object);
77 }
78
79 static void
80 g_dummy_tls_backend_class_init (GDummyTlsBackendClass *backend_class)
81 {
82   GObjectClass *object_class = G_OBJECT_CLASS (backend_class);
83
84   object_class->finalize = g_dummy_tls_backend_finalize;
85 }
86
87 static GTlsDatabase *
88 g_dummy_tls_backend_get_default_database (GTlsBackend *backend)
89 {
90   GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (backend);
91
92   if (g_once_init_enter (&dummy->database))
93     {
94       GTlsDatabase *tlsdb;
95
96       tlsdb = g_object_new (_g_dummy_tls_database_get_type (), NULL);
97       g_once_init_leave (&dummy->database, tlsdb);
98     }
99
100   return g_object_ref (dummy->database);
101 }
102
103 static void
104 g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface)
105 {
106   iface->get_certificate_type = _g_dummy_tls_certificate_get_type;
107   iface->get_client_connection_type = _g_dummy_tls_connection_get_type;
108   iface->get_server_connection_type = _g_dummy_tls_connection_get_type;
109   iface->get_file_database_type = _g_dummy_tls_database_get_type;
110   iface->get_default_database = g_dummy_tls_backend_get_default_database;
111 }
112
113 /* Dummy certificate type */
114
115 typedef struct _GDummyTlsCertificate      GDummyTlsCertificate;
116 typedef struct _GDummyTlsCertificateClass GDummyTlsCertificateClass;
117
118 struct _GDummyTlsCertificate {
119   GTlsCertificate parent_instance;
120 };
121
122 struct _GDummyTlsCertificateClass {
123   GTlsCertificateClass parent_class;
124 };
125
126 enum
127 {
128   PROP_CERTIFICATE_0,
129
130   PROP_CERT_CERTIFICATE,
131   PROP_CERT_CERTIFICATE_PEM,
132   PROP_CERT_PRIVATE_KEY,
133   PROP_CERT_PRIVATE_KEY_PEM,
134   PROP_CERT_ISSUER
135 };
136
137 static void g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface);
138
139 #define g_dummy_tls_certificate_get_type _g_dummy_tls_certificate_get_type
140 G_DEFINE_TYPE_WITH_CODE (GDummyTlsCertificate, g_dummy_tls_certificate, G_TYPE_TLS_CERTIFICATE,
141                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
142                                                 g_dummy_tls_certificate_initable_iface_init);)
143
144 static void
145 g_dummy_tls_certificate_get_property (GObject    *object,
146                                       guint       prop_id,
147                                       GValue     *value,
148                                       GParamSpec *pspec)
149 {
150   /* We need to define this method to make GObject happy, but it will
151    * never be possible to construct a working GDummyTlsCertificate, so
152    * it doesn't have to do anything useful.
153    */
154 }
155
156 static void
157 g_dummy_tls_certificate_set_property (GObject      *object,
158                                       guint         prop_id,
159                                       const GValue *value,
160                                       GParamSpec   *pspec)
161 {
162   /* Just ignore all attempts to set properties. */
163 }
164
165 static void
166 g_dummy_tls_certificate_class_init (GDummyTlsCertificateClass *certificate_class)
167 {
168   GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
169
170   gobject_class->get_property = g_dummy_tls_certificate_get_property;
171   gobject_class->set_property = g_dummy_tls_certificate_set_property;
172
173   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
174   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
175   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
176   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
177   g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
178 }
179
180 static void
181 g_dummy_tls_certificate_init (GDummyTlsCertificate *certificate)
182 {
183 }
184
185 static gboolean
186 g_dummy_tls_certificate_initable_init (GInitable       *initable,
187                                        GCancellable    *cancellable,
188                                        GError         **error)
189 {
190   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
191                        _("TLS support is not available"));
192   return FALSE;
193 }
194
195 static void
196 g_dummy_tls_certificate_initable_iface_init (GInitableIface  *iface)
197 {
198   iface->init = g_dummy_tls_certificate_initable_init;
199 }
200
201 /* Dummy connection type; since GTlsClientConnection and
202  * GTlsServerConnection are just interfaces, we can implement them
203  * both on a single object.
204  */
205
206 typedef struct _GDummyTlsConnection      GDummyTlsConnection;
207 typedef struct _GDummyTlsConnectionClass GDummyTlsConnectionClass;
208
209 struct _GDummyTlsConnection {
210   GTlsConnection parent_instance;
211 };
212
213 struct _GDummyTlsConnectionClass {
214   GTlsConnectionClass parent_class;
215 };
216
217 enum
218 {
219   PROP_CONNECTION_0,
220
221   PROP_CONN_BASE_IO_STREAM,
222   PROP_CONN_USE_SYSTEM_CERTDB,
223   PROP_CONN_REQUIRE_CLOSE_NOTIFY,
224   PROP_CONN_REHANDSHAKE_MODE,
225   PROP_CONN_CERTIFICATE,
226   PROP_CONN_DATABASE,
227   PROP_CONN_PEER_CERTIFICATE,
228   PROP_CONN_PEER_CERTIFICATE_ERRORS,
229   PROP_CONN_VALIDATION_FLAGS,
230   PROP_CONN_SERVER_IDENTITY,
231   PROP_CONN_USE_SSL3,
232   PROP_CONN_ACCEPTED_CAS,
233   PROP_CONN_AUTHENTICATION_MODE
234 };
235
236 static void g_dummy_tls_connection_initable_iface_init (GInitableIface *iface);
237
238 #define g_dummy_tls_connection_get_type _g_dummy_tls_connection_get_type
239 G_DEFINE_TYPE_WITH_CODE (GDummyTlsConnection, g_dummy_tls_connection, G_TYPE_TLS_CONNECTION,
240                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
241                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
242                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
243                                                 g_dummy_tls_connection_initable_iface_init);)
244
245 static void
246 g_dummy_tls_connection_get_property (GObject    *object,
247                                      guint       prop_id,
248                                      GValue     *value,
249                                      GParamSpec *pspec)
250 {
251 }
252
253 static void
254 g_dummy_tls_connection_set_property (GObject      *object,
255                                      guint         prop_id,
256                                      const GValue *value,
257                                      GParamSpec   *pspec)
258 {
259 }
260
261 static gboolean
262 g_dummy_tls_connection_close (GIOStream     *stream,
263                               GCancellable  *cancellable,
264                               GError       **error)
265 {
266   return TRUE;
267 }
268
269 static void
270 g_dummy_tls_connection_class_init (GDummyTlsConnectionClass *connection_class)
271 {
272   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
273   GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
274
275   gobject_class->get_property = g_dummy_tls_connection_get_property;
276   gobject_class->set_property = g_dummy_tls_connection_set_property;
277
278   /* Need to override this because when initable_init fails it will
279    * dispose the connection, which will close it, which would
280    * otherwise try to close its input/output streams, which don't
281    * exist.
282    */
283   io_stream_class->close_fn = g_dummy_tls_connection_close;
284
285   g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
286   g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
287   g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
288   g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
289   g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
290   g_object_class_override_property (gobject_class, PROP_CONN_DATABASE, "database");
291   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
292   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
293   g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
294   g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
295   g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
296   g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
297   g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
298 }
299
300 static void
301 g_dummy_tls_connection_init (GDummyTlsConnection *connection)
302 {
303 }
304
305 static gboolean
306 g_dummy_tls_connection_initable_init (GInitable       *initable,
307                                       GCancellable    *cancellable,
308                                       GError         **error)
309 {
310   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
311                        _("TLS support is not available"));
312   return FALSE;
313 }
314
315 static void
316 g_dummy_tls_connection_initable_iface_init (GInitableIface  *iface)
317 {
318   iface->init = g_dummy_tls_connection_initable_init;
319 }
320
321 /* Dummy database type.
322  */
323
324 typedef struct _GDummyTlsDatabase      GDummyTlsDatabase;
325 typedef struct _GDummyTlsDatabaseClass GDummyTlsDatabaseClass;
326
327 struct _GDummyTlsDatabase {
328   GTlsDatabase parent_instance;
329 };
330
331 struct _GDummyTlsDatabaseClass {
332   GTlsDatabaseClass parent_class;
333 };
334
335 enum
336 {
337   PROP_DATABASE_0,
338
339   PROP_ANCHORS,
340 };
341
342 static void g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface);
343 static void g_dummy_tls_database_initable_iface_init (GInitableIface *iface);
344
345 #define g_dummy_tls_database_get_type _g_dummy_tls_database_get_type
346 G_DEFINE_TYPE_WITH_CODE (GDummyTlsDatabase, g_dummy_tls_database, G_TYPE_TLS_DATABASE,
347                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_FILE_DATABASE,
348                                                 g_dummy_tls_database_file_database_iface_init);
349                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
350                                                 g_dummy_tls_database_initable_iface_init);)
351
352
353 static void
354 g_dummy_tls_database_get_property (GObject    *object,
355                                    guint       prop_id,
356                                    GValue     *value,
357                                    GParamSpec *pspec)
358 {
359   /* We need to define this method to make GObject happy, but it will
360    * never be possible to construct a working GDummyTlsDatabase, so
361    * it doesn't have to do anything useful.
362    */
363 }
364
365 static void
366 g_dummy_tls_database_set_property (GObject      *object,
367                                    guint         prop_id,
368                                    const GValue *value,
369                                    GParamSpec   *pspec)
370 {
371   /* Just ignore all attempts to set properties. */
372 }
373
374 static void
375 g_dummy_tls_database_class_init (GDummyTlsDatabaseClass *database_class)
376 {
377   GObjectClass *gobject_class = G_OBJECT_CLASS (database_class);
378
379   gobject_class->get_property = g_dummy_tls_database_get_property;
380   gobject_class->set_property = g_dummy_tls_database_set_property;
381
382   g_object_class_override_property (gobject_class, PROP_ANCHORS, "anchors");
383 }
384
385 static void
386 g_dummy_tls_database_init (GDummyTlsDatabase *database)
387 {
388 }
389
390 static void
391 g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface  *iface)
392 {
393 }
394
395 static gboolean
396 g_dummy_tls_database_initable_init (GInitable       *initable,
397                                     GCancellable    *cancellable,
398                                     GError         **error)
399 {
400   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
401                        _("TLS support is not available"));
402   return FALSE;
403 }
404
405 static void
406 g_dummy_tls_database_initable_iface_init (GInitableIface  *iface)
407 {
408   iface->init = g_dummy_tls_database_initable_init;
409 }