Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gtesttlsbackend.c
blobb97e4efd40191725aac57118b270b3119d320982
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2011 Collabora Ltd.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "gtesttlsbackend.h"
21 #include <glib.h>
23 static GType _g_test_tls_certificate_get_type (void);
24 static GType _g_test_tls_connection_get_type (void);
26 struct _GTestTlsBackend {
27 GObject parent_instance;
30 static void g_test_tls_backend_iface_init (GTlsBackendInterface *iface);
32 #define g_test_tls_backend_get_type _g_test_tls_backend_get_type
33 G_DEFINE_TYPE_WITH_CODE (GTestTlsBackend, g_test_tls_backend, G_TYPE_OBJECT,
34 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
35 g_test_tls_backend_iface_init)
36 g_io_extension_point_set_required_type (
37 g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME),
38 G_TYPE_TLS_BACKEND);
39 g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
40 g_define_type_id,
41 "test",
42 999))
44 static void
45 g_test_tls_backend_init (GTestTlsBackend *backend)
49 static void
50 g_test_tls_backend_class_init (GTestTlsBackendClass *backend_class)
54 static void
55 g_test_tls_backend_iface_init (GTlsBackendInterface *iface)
57 iface->get_certificate_type = _g_test_tls_certificate_get_type;
58 iface->get_client_connection_type = _g_test_tls_connection_get_type;
59 iface->get_server_connection_type = _g_test_tls_connection_get_type;
62 /* Test certificate type */
64 typedef struct _GTestTlsCertificate GTestTlsCertificate;
65 typedef struct _GTestTlsCertificateClass GTestTlsCertificateClass;
67 struct _GTestTlsCertificate {
68 GTlsCertificate parent_instance;
69 gchar *key_pem;
70 gchar *cert_pem;
71 GTlsCertificate *issuer;
74 struct _GTestTlsCertificateClass {
75 GTlsCertificateClass parent_class;
78 enum
80 PROP_CERTIFICATE_0,
82 PROP_CERT_CERTIFICATE,
83 PROP_CERT_CERTIFICATE_PEM,
84 PROP_CERT_PRIVATE_KEY,
85 PROP_CERT_PRIVATE_KEY_PEM,
86 PROP_CERT_ISSUER
89 static void g_test_tls_certificate_initable_iface_init (GInitableIface *iface);
91 #define g_test_tls_certificate_get_type _g_test_tls_certificate_get_type
92 G_DEFINE_TYPE_WITH_CODE (GTestTlsCertificate, g_test_tls_certificate, G_TYPE_TLS_CERTIFICATE,
93 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
94 g_test_tls_certificate_initable_iface_init);)
96 static GTlsCertificateFlags
97 g_test_tls_certificate_verify (GTlsCertificate *cert,
98 GSocketConnectable *identity,
99 GTlsCertificate *trusted_ca)
101 /* For now, all of the tests expect the certificate to verify */
102 return 0;
105 static void
106 g_test_tls_certificate_get_property (GObject *object,
107 guint prop_id,
108 GValue *value,
109 GParamSpec *pspec)
111 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
113 switch (prop_id)
115 case PROP_CERT_CERTIFICATE_PEM:
116 g_value_set_string (value, cert->cert_pem);
117 break;
118 case PROP_CERT_PRIVATE_KEY_PEM:
119 g_value_set_string (value, cert->key_pem);
120 break;
121 case PROP_CERT_ISSUER:
122 g_value_set_object (value, cert->issuer);
123 break;
124 default:
125 g_assert_not_reached ();
126 break;
130 static void
131 g_test_tls_certificate_set_property (GObject *object,
132 guint prop_id,
133 const GValue *value,
134 GParamSpec *pspec)
136 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
138 switch (prop_id)
140 case PROP_CERT_CERTIFICATE_PEM:
141 cert->cert_pem = g_value_dup_string (value);
142 break;
143 case PROP_CERT_PRIVATE_KEY_PEM:
144 cert->key_pem = g_value_dup_string (value);
145 break;
146 case PROP_CERT_ISSUER:
147 cert->issuer = g_value_dup_object (value);
148 break;
149 case PROP_CERT_CERTIFICATE:
150 case PROP_CERT_PRIVATE_KEY:
151 /* ignore */
152 break;
153 default:
154 g_assert_not_reached ();
155 break;
159 static void
160 g_test_tls_certificate_finalize (GObject *object)
162 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
164 g_free (cert->cert_pem);
165 g_free (cert->key_pem);
166 g_clear_object (&cert->issuer);
169 static void
170 g_test_tls_certificate_class_init (GTestTlsCertificateClass *test_class)
172 GObjectClass *gobject_class = G_OBJECT_CLASS (test_class);
173 GTlsCertificateClass *certificate_class = G_TLS_CERTIFICATE_CLASS (test_class);
175 gobject_class->get_property = g_test_tls_certificate_get_property;
176 gobject_class->set_property = g_test_tls_certificate_set_property;
177 gobject_class->finalize = g_test_tls_certificate_finalize;
179 certificate_class->verify = g_test_tls_certificate_verify;
181 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
182 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
183 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
184 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
185 g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
188 static void
189 g_test_tls_certificate_init (GTestTlsCertificate *certificate)
193 static gboolean
194 g_test_tls_certificate_initable_init (GInitable *initable,
195 GCancellable *cancellable,
196 GError **error)
198 return TRUE;
201 static void
202 g_test_tls_certificate_initable_iface_init (GInitableIface *iface)
204 iface->init = g_test_tls_certificate_initable_init;
207 /* Dummy connection type; since GTlsClientConnection and
208 * GTlsServerConnection are just interfaces, we can implement them
209 * both on a single object.
212 typedef struct _GTestTlsConnection GTestTlsConnection;
213 typedef struct _GTestTlsConnectionClass GTestTlsConnectionClass;
215 struct _GTestTlsConnection {
216 GTlsConnection parent_instance;
219 struct _GTestTlsConnectionClass {
220 GTlsConnectionClass parent_class;
223 enum
225 PROP_CONNECTION_0,
227 PROP_CONN_BASE_IO_STREAM,
228 PROP_CONN_USE_SYSTEM_CERTDB,
229 PROP_CONN_REQUIRE_CLOSE_NOTIFY,
230 PROP_CONN_REHANDSHAKE_MODE,
231 PROP_CONN_CERTIFICATE,
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_test_tls_connection_initable_iface_init (GInitableIface *iface);
243 #define g_test_tls_connection_get_type _g_test_tls_connection_get_type
244 G_DEFINE_TYPE_WITH_CODE (GTestTlsConnection, g_test_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_test_tls_connection_initable_iface_init);)
250 static void
251 g_test_tls_connection_get_property (GObject *object,
252 guint prop_id,
253 GValue *value,
254 GParamSpec *pspec)
258 static void
259 g_test_tls_connection_set_property (GObject *object,
260 guint prop_id,
261 const GValue *value,
262 GParamSpec *pspec)
266 static gboolean
267 g_test_tls_connection_close (GIOStream *stream,
268 GCancellable *cancellable,
269 GError **error)
271 return TRUE;
274 static void
275 g_test_tls_connection_class_init (GTestTlsConnectionClass *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_test_tls_connection_get_property;
281 gobject_class->set_property = g_test_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_test_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_PEER_CERTIFICATE, "peer-certificate");
296 g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
297 g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
298 g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
299 g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
300 g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
301 g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
304 static void
305 g_test_tls_connection_init (GTestTlsConnection *connection)
309 static gboolean
310 g_test_tls_connection_initable_init (GInitable *initable,
311 GCancellable *cancellable,
312 GError **error)
314 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
315 "TLS Connection support is not available");
316 return FALSE;
319 static void
320 g_test_tls_connection_initable_iface_init (GInitableIface *iface)
322 iface->init = g_test_tls_connection_initable_init;
325 const gchar *
326 g_test_tls_connection_get_private_key_pem (GTlsCertificate *cert)
328 return ((GTestTlsCertificate *)cert)->key_pem;