[kdbus] Integrate kdbus core with new API.
[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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "gtesttlsbackend.h"
20
21 #include <glib.h>
22
23 static GType _g_test_tls_certificate_get_type (void);
24 static GType _g_test_tls_connection_get_type (void);
25
26 struct _GTestTlsBackend {
27   GObject parent_instance;
28 };
29
30 static void g_test_tls_backend_iface_init (GTlsBackendInterface *iface);
31
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))
43
44 static void
45 g_test_tls_backend_init (GTestTlsBackend *backend)
46 {
47 }
48
49 static void
50 g_test_tls_backend_class_init (GTestTlsBackendClass *backend_class)
51 {
52 }
53
54 static void
55 g_test_tls_backend_iface_init (GTlsBackendInterface *iface)
56 {
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;
60 }
61
62 /* Test certificate type */
63
64 typedef struct _GTestTlsCertificate      GTestTlsCertificate;
65 typedef struct _GTestTlsCertificateClass GTestTlsCertificateClass;
66
67 struct _GTestTlsCertificate {
68   GTlsCertificate parent_instance;
69   gchar *key_pem;
70   gchar *cert_pem;
71 };
72
73 struct _GTestTlsCertificateClass {
74   GTlsCertificateClass parent_class;
75 };
76
77 enum
78 {
79   PROP_CERTIFICATE_0,
80
81   PROP_CERT_CERTIFICATE,
82   PROP_CERT_CERTIFICATE_PEM,
83   PROP_CERT_PRIVATE_KEY,
84   PROP_CERT_PRIVATE_KEY_PEM,
85   PROP_CERT_ISSUER
86 };
87
88 static void g_test_tls_certificate_initable_iface_init (GInitableIface *iface);
89
90 #define g_test_tls_certificate_get_type _g_test_tls_certificate_get_type
91 G_DEFINE_TYPE_WITH_CODE (GTestTlsCertificate, g_test_tls_certificate, G_TYPE_TLS_CERTIFICATE,
92                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
93                                                 g_test_tls_certificate_initable_iface_init);)
94
95 static GTlsCertificateFlags
96 g_test_tls_certificate_verify (GTlsCertificate     *cert,
97                                GSocketConnectable  *identity,
98                                GTlsCertificate     *trusted_ca)
99 {
100   /* For now, all of the tests expect the certificate to verify */
101   return 0;
102 }
103
104 static void
105 g_test_tls_certificate_get_property (GObject    *object,
106                                       guint       prop_id,
107                                       GValue     *value,
108                                       GParamSpec *pspec)
109 {
110   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
111
112   switch (prop_id)
113     {
114     case PROP_CERT_CERTIFICATE_PEM:
115       g_value_set_string (value, cert->cert_pem);
116       break;
117     case PROP_CERT_PRIVATE_KEY_PEM:
118       g_value_set_string (value, cert->key_pem);
119       break;
120     default:
121       g_assert_not_reached ();
122       break;
123     }
124 }
125
126 static void
127 g_test_tls_certificate_set_property (GObject      *object,
128                                       guint         prop_id,
129                                       const GValue *value,
130                                       GParamSpec   *pspec)
131 {
132   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
133
134   switch (prop_id)
135     {
136     case PROP_CERT_CERTIFICATE_PEM:
137       cert->cert_pem = g_value_dup_string (value);
138       break;
139     case PROP_CERT_PRIVATE_KEY_PEM:
140       cert->key_pem = g_value_dup_string (value);
141       break;
142     case PROP_CERT_CERTIFICATE:
143     case PROP_CERT_PRIVATE_KEY:
144     case PROP_CERT_ISSUER:
145       /* ignore */
146       break;
147     default:
148       g_assert_not_reached ();
149       break;
150     }
151 }
152
153 static void
154 g_test_tls_certificate_finalize (GObject *object)
155 {
156   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
157
158   g_free (cert->cert_pem);
159   g_free (cert->key_pem);
160 }
161
162 static void
163 g_test_tls_certificate_class_init (GTestTlsCertificateClass *test_class)
164 {
165   GObjectClass *gobject_class = G_OBJECT_CLASS (test_class);
166   GTlsCertificateClass *certificate_class = G_TLS_CERTIFICATE_CLASS (test_class);
167
168   gobject_class->get_property = g_test_tls_certificate_get_property;
169   gobject_class->set_property = g_test_tls_certificate_set_property;
170   gobject_class->finalize = g_test_tls_certificate_finalize;
171
172   certificate_class->verify = g_test_tls_certificate_verify;
173
174   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
175   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
176   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
177   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
178   g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
179 }
180
181 static void
182 g_test_tls_certificate_init (GTestTlsCertificate *certificate)
183 {
184 }
185
186 static gboolean
187 g_test_tls_certificate_initable_init (GInitable       *initable,
188                                        GCancellable    *cancellable,
189                                        GError         **error)
190 {
191   return TRUE;
192 }
193
194 static void
195 g_test_tls_certificate_initable_iface_init (GInitableIface  *iface)
196 {
197   iface->init = g_test_tls_certificate_initable_init;
198 }
199
200 /* Dummy connection type; since GTlsClientConnection and
201  * GTlsServerConnection are just interfaces, we can implement them
202  * both on a single object.
203  */
204
205 typedef struct _GTestTlsConnection      GTestTlsConnection;
206 typedef struct _GTestTlsConnectionClass GTestTlsConnectionClass;
207
208 struct _GTestTlsConnection {
209   GTlsConnection parent_instance;
210 };
211
212 struct _GTestTlsConnectionClass {
213   GTlsConnectionClass parent_class;
214 };
215
216 enum
217 {
218   PROP_CONNECTION_0,
219
220   PROP_CONN_BASE_IO_STREAM,
221   PROP_CONN_USE_SYSTEM_CERTDB,
222   PROP_CONN_REQUIRE_CLOSE_NOTIFY,
223   PROP_CONN_REHANDSHAKE_MODE,
224   PROP_CONN_CERTIFICATE,
225   PROP_CONN_PEER_CERTIFICATE,
226   PROP_CONN_PEER_CERTIFICATE_ERRORS,
227   PROP_CONN_VALIDATION_FLAGS,
228   PROP_CONN_SERVER_IDENTITY,
229   PROP_CONN_USE_SSL3,
230   PROP_CONN_ACCEPTED_CAS,
231   PROP_CONN_AUTHENTICATION_MODE
232 };
233
234 static void g_test_tls_connection_initable_iface_init (GInitableIface *iface);
235
236 #define g_test_tls_connection_get_type _g_test_tls_connection_get_type
237 G_DEFINE_TYPE_WITH_CODE (GTestTlsConnection, g_test_tls_connection, G_TYPE_TLS_CONNECTION,
238                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
239                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
240                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
241                                                 g_test_tls_connection_initable_iface_init);)
242
243 static void
244 g_test_tls_connection_get_property (GObject    *object,
245                                      guint       prop_id,
246                                      GValue     *value,
247                                      GParamSpec *pspec)
248 {
249 }
250
251 static void
252 g_test_tls_connection_set_property (GObject      *object,
253                                      guint         prop_id,
254                                      const GValue *value,
255                                      GParamSpec   *pspec)
256 {
257 }
258
259 static gboolean
260 g_test_tls_connection_close (GIOStream     *stream,
261                               GCancellable  *cancellable,
262                               GError       **error)
263 {
264   return TRUE;
265 }
266
267 static void
268 g_test_tls_connection_class_init (GTestTlsConnectionClass *connection_class)
269 {
270   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
271   GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
272
273   gobject_class->get_property = g_test_tls_connection_get_property;
274   gobject_class->set_property = g_test_tls_connection_set_property;
275
276   /* Need to override this because when initable_init fails it will
277    * dispose the connection, which will close it, which would
278    * otherwise try to close its input/output streams, which don't
279    * exist.
280    */
281   io_stream_class->close_fn = g_test_tls_connection_close;
282
283   g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
284   g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
285   g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
286   g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
287   g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
288   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
289   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
290   g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
291   g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
292   g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
293   g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
294   g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
295 }
296
297 static void
298 g_test_tls_connection_init (GTestTlsConnection *connection)
299 {
300 }
301
302 static gboolean
303 g_test_tls_connection_initable_init (GInitable       *initable,
304                                       GCancellable    *cancellable,
305                                       GError         **error)
306 {
307   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
308                        "TLS Connection support is not available");
309   return FALSE;
310 }
311
312 static void
313 g_test_tls_connection_initable_iface_init (GInitableIface  *iface)
314 {
315   iface->init = g_test_tls_connection_initable_init;
316 }
317
318 const gchar *
319 g_test_tls_connection_get_private_key_pem (GTlsCertificate *cert)
320 {
321   return ((GTestTlsCertificate *)cert)->key_pem;
322 }