Add some more cases to the app-id unit tests
[glib.git] / gio / gdummytlsbackend.c
blob016dd21f6ae0790bd9623c9e7a6ba1448b95e1ea
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright © 2015 Collabora, Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "config.h"
22 #include "gdummytlsbackend.h"
24 #include <glib.h>
26 #include "gasyncresult.h"
27 #include "gcancellable.h"
28 #include "ginitable.h"
29 #include "gdtlsclientconnection.h"
30 #include "gdtlsconnection.h"
31 #include "gdtlsserverconnection.h"
32 #include "gtlsbackend.h"
33 #include "gtlscertificate.h"
34 #include "gtlsclientconnection.h"
35 #include "gtlsdatabase.h"
36 #include "gtlsfiledatabase.h"
37 #include "gtlsserverconnection.h"
39 #include "giomodule.h"
40 #include "giomodule-priv.h"
42 #include "glibintl.h"
44 static GType _g_dummy_tls_certificate_get_type (void);
45 static GType _g_dummy_tls_connection_get_type (void);
46 static GType _g_dummy_dtls_connection_get_type (void);
47 static GType _g_dummy_tls_database_get_type (void);
49 struct _GDummyTlsBackend {
50 GObject parent_instance;
51 GTlsDatabase *database;
54 static void g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface);
56 #define g_dummy_tls_backend_get_type _g_dummy_tls_backend_get_type
57 G_DEFINE_TYPE_WITH_CODE (GDummyTlsBackend, g_dummy_tls_backend, G_TYPE_OBJECT,
58 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
59 g_dummy_tls_backend_iface_init)
60 _g_io_modules_ensure_extension_points_registered ();
61 g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
62 g_define_type_id,
63 "dummy",
64 -100))
66 static void
67 g_dummy_tls_backend_init (GDummyTlsBackend *dummy)
71 static void
72 g_dummy_tls_backend_finalize (GObject *object)
74 GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (object);
76 g_clear_object (&dummy->database);
78 G_OBJECT_CLASS (g_dummy_tls_backend_parent_class)->finalize (object);
81 static void
82 g_dummy_tls_backend_class_init (GDummyTlsBackendClass *backend_class)
84 GObjectClass *object_class = G_OBJECT_CLASS (backend_class);
86 object_class->finalize = g_dummy_tls_backend_finalize;
89 static GTlsDatabase *
90 g_dummy_tls_backend_get_default_database (GTlsBackend *backend)
92 GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (backend);
94 if (g_once_init_enter (&dummy->database))
96 GTlsDatabase *tlsdb;
98 tlsdb = g_object_new (_g_dummy_tls_database_get_type (), NULL);
99 g_once_init_leave (&dummy->database, tlsdb);
102 return g_object_ref (dummy->database);
105 static void
106 g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface)
108 iface->get_certificate_type = _g_dummy_tls_certificate_get_type;
109 iface->get_client_connection_type = _g_dummy_tls_connection_get_type;
110 iface->get_server_connection_type = _g_dummy_tls_connection_get_type;
111 iface->get_dtls_client_connection_type = _g_dummy_dtls_connection_get_type;
112 iface->get_dtls_server_connection_type = _g_dummy_dtls_connection_get_type;
113 iface->get_file_database_type = _g_dummy_tls_database_get_type;
114 iface->get_default_database = g_dummy_tls_backend_get_default_database;
117 /* Dummy certificate type */
119 typedef struct _GDummyTlsCertificate GDummyTlsCertificate;
120 typedef struct _GDummyTlsCertificateClass GDummyTlsCertificateClass;
122 struct _GDummyTlsCertificate {
123 GTlsCertificate parent_instance;
126 struct _GDummyTlsCertificateClass {
127 GTlsCertificateClass parent_class;
130 enum
132 PROP_CERTIFICATE_0,
134 PROP_CERT_CERTIFICATE,
135 PROP_CERT_CERTIFICATE_PEM,
136 PROP_CERT_PRIVATE_KEY,
137 PROP_CERT_PRIVATE_KEY_PEM,
138 PROP_CERT_ISSUER
141 static void g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface);
143 #define g_dummy_tls_certificate_get_type _g_dummy_tls_certificate_get_type
144 G_DEFINE_TYPE_WITH_CODE (GDummyTlsCertificate, g_dummy_tls_certificate, G_TYPE_TLS_CERTIFICATE,
145 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
146 g_dummy_tls_certificate_initable_iface_init);)
148 static void
149 g_dummy_tls_certificate_get_property (GObject *object,
150 guint prop_id,
151 GValue *value,
152 GParamSpec *pspec)
154 /* We need to define this method to make GObject happy, but it will
155 * never be possible to construct a working GDummyTlsCertificate, so
156 * it doesn't have to do anything useful.
160 static void
161 g_dummy_tls_certificate_set_property (GObject *object,
162 guint prop_id,
163 const GValue *value,
164 GParamSpec *pspec)
166 /* Just ignore all attempts to set properties. */
169 static void
170 g_dummy_tls_certificate_class_init (GDummyTlsCertificateClass *certificate_class)
172 GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
174 gobject_class->get_property = g_dummy_tls_certificate_get_property;
175 gobject_class->set_property = g_dummy_tls_certificate_set_property;
177 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
178 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
179 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
180 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
181 g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
184 static void
185 g_dummy_tls_certificate_init (GDummyTlsCertificate *certificate)
189 static gboolean
190 g_dummy_tls_certificate_initable_init (GInitable *initable,
191 GCancellable *cancellable,
192 GError **error)
194 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
195 _("TLS support is not available"));
196 return FALSE;
199 static void
200 g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface)
202 iface->init = g_dummy_tls_certificate_initable_init;
205 /* Dummy connection type; since GTlsClientConnection and
206 * GTlsServerConnection are just interfaces, we can implement them
207 * both on a single object.
210 typedef struct _GDummyTlsConnection GDummyTlsConnection;
211 typedef struct _GDummyTlsConnectionClass GDummyTlsConnectionClass;
213 struct _GDummyTlsConnection {
214 GTlsConnection parent_instance;
217 struct _GDummyTlsConnectionClass {
218 GTlsConnectionClass parent_class;
221 enum
223 PROP_CONNECTION_0,
225 PROP_CONN_BASE_IO_STREAM,
226 PROP_CONN_USE_SYSTEM_CERTDB,
227 PROP_CONN_REQUIRE_CLOSE_NOTIFY,
228 PROP_CONN_REHANDSHAKE_MODE,
229 PROP_CONN_CERTIFICATE,
230 PROP_CONN_DATABASE,
231 PROP_CONN_INTERACTION,
232 PROP_CONN_PEER_CERTIFICATE,
233 PROP_CONN_PEER_CERTIFICATE_ERRORS,
234 PROP_CONN_VALIDATION_FLAGS,
235 PROP_CONN_SERVER_IDENTITY,
236 PROP_CONN_USE_SSL3,
237 PROP_CONN_ACCEPTED_CAS,
238 PROP_CONN_AUTHENTICATION_MODE
241 static void g_dummy_tls_connection_initable_iface_init (GInitableIface *iface);
243 #define g_dummy_tls_connection_get_type _g_dummy_tls_connection_get_type
244 G_DEFINE_TYPE_WITH_CODE (GDummyTlsConnection, g_dummy_tls_connection, G_TYPE_TLS_CONNECTION,
245 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
246 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
247 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
248 g_dummy_tls_connection_initable_iface_init);)
250 static void
251 g_dummy_tls_connection_get_property (GObject *object,
252 guint prop_id,
253 GValue *value,
254 GParamSpec *pspec)
258 static void
259 g_dummy_tls_connection_set_property (GObject *object,
260 guint prop_id,
261 const GValue *value,
262 GParamSpec *pspec)
266 static gboolean
267 g_dummy_tls_connection_close (GIOStream *stream,
268 GCancellable *cancellable,
269 GError **error)
271 return TRUE;
274 static void
275 g_dummy_tls_connection_class_init (GDummyTlsConnectionClass *connection_class)
277 GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
278 GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
280 gobject_class->get_property = g_dummy_tls_connection_get_property;
281 gobject_class->set_property = g_dummy_tls_connection_set_property;
283 /* Need to override this because when initable_init fails it will
284 * dispose the connection, which will close it, which would
285 * otherwise try to close its input/output streams, which don't
286 * exist.
288 io_stream_class->close_fn = g_dummy_tls_connection_close;
290 g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
291 g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
292 g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
293 g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
294 g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
295 g_object_class_override_property (gobject_class, PROP_CONN_DATABASE, "database");
296 g_object_class_override_property (gobject_class, PROP_CONN_INTERACTION, "interaction");
297 g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
298 g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
299 g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
300 g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
301 g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
302 g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
303 g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
306 static void
307 g_dummy_tls_connection_init (GDummyTlsConnection *connection)
311 static gboolean
312 g_dummy_tls_connection_initable_init (GInitable *initable,
313 GCancellable *cancellable,
314 GError **error)
316 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
317 _("TLS support is not available"));
318 return FALSE;
321 static void
322 g_dummy_tls_connection_initable_iface_init (GInitableIface *iface)
324 iface->init = g_dummy_tls_connection_initable_init;
327 /* Dummy DTLS connection type; since GDtlsClientConnection and
328 * GDtlsServerConnection are just interfaces, we can implement them
329 * both on a single object.
332 typedef struct _GDummyDtlsConnection GDummyDtlsConnection;
333 typedef struct _GDummyDtlsConnectionClass GDummyDtlsConnectionClass;
335 struct _GDummyDtlsConnection {
336 GObject parent_instance;
339 struct _GDummyDtlsConnectionClass {
340 GObjectClass parent_class;
343 enum
345 PROP_DTLS_CONN_BASE_SOCKET = 1,
346 PROP_DTLS_CONN_REQUIRE_CLOSE_NOTIFY,
347 PROP_DTLS_CONN_REHANDSHAKE_MODE,
348 PROP_DTLS_CONN_CERTIFICATE,
349 PROP_DTLS_CONN_DATABASE,
350 PROP_DTLS_CONN_INTERACTION,
351 PROP_DTLS_CONN_PEER_CERTIFICATE,
352 PROP_DTLS_CONN_PEER_CERTIFICATE_ERRORS,
353 PROP_DTLS_CONN_VALIDATION_FLAGS,
354 PROP_DTLS_CONN_SERVER_IDENTITY,
355 PROP_DTLS_CONN_ENABLE_NEGOTIATION,
356 PROP_DTLS_CONN_ACCEPTED_CAS,
357 PROP_DTLS_CONN_AUTHENTICATION_MODE,
360 static void g_dummy_dtls_connection_initable_iface_init (GInitableIface *iface);
362 #define g_dummy_dtls_connection_get_type _g_dummy_dtls_connection_get_type
363 G_DEFINE_TYPE_WITH_CODE (GDummyDtlsConnection, g_dummy_dtls_connection, G_TYPE_OBJECT,
364 G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_CONNECTION, NULL);
365 G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_CLIENT_CONNECTION, NULL);
366 G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_SERVER_CONNECTION, NULL);
367 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
368 g_dummy_dtls_connection_initable_iface_init);)
370 static void
371 g_dummy_dtls_connection_get_property (GObject *object,
372 guint prop_id,
373 GValue *value,
374 GParamSpec *pspec)
378 static void
379 g_dummy_dtls_connection_set_property (GObject *object,
380 guint prop_id,
381 const GValue *value,
382 GParamSpec *pspec)
386 static void
387 g_dummy_dtls_connection_class_init (GDummyDtlsConnectionClass *connection_class)
389 GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
391 gobject_class->get_property = g_dummy_dtls_connection_get_property;
392 gobject_class->set_property = g_dummy_dtls_connection_set_property;
394 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_BASE_SOCKET, "base-socket");
395 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
396 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
397 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_CERTIFICATE, "certificate");
398 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_DATABASE, "database");
399 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_INTERACTION, "interaction");
400 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_PEER_CERTIFICATE, "peer-certificate");
401 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
402 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_VALIDATION_FLAGS, "validation-flags");
403 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_SERVER_IDENTITY, "server-identity");
404 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_ACCEPTED_CAS, "accepted-cas");
405 g_object_class_override_property (gobject_class, PROP_DTLS_CONN_AUTHENTICATION_MODE, "authentication-mode");
408 static void
409 g_dummy_dtls_connection_init (GDummyDtlsConnection *connection)
413 static gboolean
414 g_dummy_dtls_connection_initable_init (GInitable *initable,
415 GCancellable *cancellable,
416 GError **error)
418 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
419 _("DTLS support is not available"));
420 return FALSE;
423 static void
424 g_dummy_dtls_connection_initable_iface_init (GInitableIface *iface)
426 iface->init = g_dummy_dtls_connection_initable_init;
429 /* Dummy database type.
432 typedef struct _GDummyTlsDatabase GDummyTlsDatabase;
433 typedef struct _GDummyTlsDatabaseClass GDummyTlsDatabaseClass;
435 struct _GDummyTlsDatabase {
436 GTlsDatabase parent_instance;
439 struct _GDummyTlsDatabaseClass {
440 GTlsDatabaseClass parent_class;
443 enum
445 PROP_DATABASE_0,
447 PROP_ANCHORS,
450 static void g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface);
451 static void g_dummy_tls_database_initable_iface_init (GInitableIface *iface);
453 #define g_dummy_tls_database_get_type _g_dummy_tls_database_get_type
454 G_DEFINE_TYPE_WITH_CODE (GDummyTlsDatabase, g_dummy_tls_database, G_TYPE_TLS_DATABASE,
455 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_FILE_DATABASE,
456 g_dummy_tls_database_file_database_iface_init);
457 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
458 g_dummy_tls_database_initable_iface_init);)
461 static void
462 g_dummy_tls_database_get_property (GObject *object,
463 guint prop_id,
464 GValue *value,
465 GParamSpec *pspec)
467 /* We need to define this method to make GObject happy, but it will
468 * never be possible to construct a working GDummyTlsDatabase, so
469 * it doesn't have to do anything useful.
473 static void
474 g_dummy_tls_database_set_property (GObject *object,
475 guint prop_id,
476 const GValue *value,
477 GParamSpec *pspec)
479 /* Just ignore all attempts to set properties. */
482 static void
483 g_dummy_tls_database_class_init (GDummyTlsDatabaseClass *database_class)
485 GObjectClass *gobject_class = G_OBJECT_CLASS (database_class);
487 gobject_class->get_property = g_dummy_tls_database_get_property;
488 gobject_class->set_property = g_dummy_tls_database_set_property;
490 g_object_class_override_property (gobject_class, PROP_ANCHORS, "anchors");
493 static void
494 g_dummy_tls_database_init (GDummyTlsDatabase *database)
498 static void
499 g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface)
503 static gboolean
504 g_dummy_tls_database_initable_init (GInitable *initable,
505 GCancellable *cancellable,
506 GError **error)
508 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
509 _("TLS support is not available"));
510 return FALSE;
513 static void
514 g_dummy_tls_database_initable_iface_init (GInitableIface *iface)
516 iface->init = g_dummy_tls_database_initable_init;