gio/tests: fix race when generating code
[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 void
96 g_test_tls_certificate_get_property (GObject    *object,
97                                       guint       prop_id,
98                                       GValue     *value,
99                                       GParamSpec *pspec)
100 {
101   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
102
103   switch (prop_id)
104     {
105     case PROP_CERT_CERTIFICATE_PEM:
106       g_value_set_string (value, cert->cert_pem);
107       break;
108     case PROP_CERT_PRIVATE_KEY_PEM:
109       g_value_set_string (value, cert->key_pem);
110       break;
111     default:
112       g_assert_not_reached ();
113       break;
114     }
115 }
116
117 static void
118 g_test_tls_certificate_set_property (GObject      *object,
119                                       guint         prop_id,
120                                       const GValue *value,
121                                       GParamSpec   *pspec)
122 {
123   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
124
125   switch (prop_id)
126     {
127     case PROP_CERT_CERTIFICATE_PEM:
128       cert->cert_pem = g_value_dup_string (value);
129       break;
130     case PROP_CERT_PRIVATE_KEY_PEM:
131       cert->key_pem = g_value_dup_string (value);
132       break;
133     case PROP_CERT_CERTIFICATE:
134     case PROP_CERT_PRIVATE_KEY:
135     case PROP_CERT_ISSUER:
136       /* ignore */
137       break;
138     default:
139       g_assert_not_reached ();
140       break;
141     }
142 }
143
144 static void
145 g_test_tls_certificate_finalize (GObject *object)
146 {
147   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
148
149   g_free (cert->cert_pem);
150   g_free (cert->key_pem);
151 }
152
153 static void
154 g_test_tls_certificate_class_init (GTestTlsCertificateClass *certificate_class)
155 {
156   GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
157
158   gobject_class->get_property = g_test_tls_certificate_get_property;
159   gobject_class->set_property = g_test_tls_certificate_set_property;
160   gobject_class->finalize = g_test_tls_certificate_finalize;
161
162   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
163   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
164   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
165   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
166   g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
167 }
168
169 static void
170 g_test_tls_certificate_init (GTestTlsCertificate *certificate)
171 {
172 }
173
174 static gboolean
175 g_test_tls_certificate_initable_init (GInitable       *initable,
176                                        GCancellable    *cancellable,
177                                        GError         **error)
178 {
179   return TRUE;
180 }
181
182 static void
183 g_test_tls_certificate_initable_iface_init (GInitableIface  *iface)
184 {
185   iface->init = g_test_tls_certificate_initable_init;
186 }
187
188 /* Dummy connection type; since GTlsClientConnection and
189  * GTlsServerConnection are just interfaces, we can implement them
190  * both on a single object.
191  */
192
193 typedef struct _GTestTlsConnection      GTestTlsConnection;
194 typedef struct _GTestTlsConnectionClass GTestTlsConnectionClass;
195
196 struct _GTestTlsConnection {
197   GTlsConnection parent_instance;
198 };
199
200 struct _GTestTlsConnectionClass {
201   GTlsConnectionClass parent_class;
202 };
203
204 enum
205 {
206   PROP_CONNECTION_0,
207
208   PROP_CONN_BASE_IO_STREAM,
209   PROP_CONN_USE_SYSTEM_CERTDB,
210   PROP_CONN_REQUIRE_CLOSE_NOTIFY,
211   PROP_CONN_REHANDSHAKE_MODE,
212   PROP_CONN_CERTIFICATE,
213   PROP_CONN_PEER_CERTIFICATE,
214   PROP_CONN_PEER_CERTIFICATE_ERRORS,
215   PROP_CONN_VALIDATION_FLAGS,
216   PROP_CONN_SERVER_IDENTITY,
217   PROP_CONN_USE_SSL3,
218   PROP_CONN_ACCEPTED_CAS,
219   PROP_CONN_AUTHENTICATION_MODE
220 };
221
222 static void g_test_tls_connection_initable_iface_init (GInitableIface *iface);
223
224 #define g_test_tls_connection_get_type _g_test_tls_connection_get_type
225 G_DEFINE_TYPE_WITH_CODE (GTestTlsConnection, g_test_tls_connection, G_TYPE_TLS_CONNECTION,
226                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
227                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
228                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
229                                                 g_test_tls_connection_initable_iface_init);)
230
231 static void
232 g_test_tls_connection_get_property (GObject    *object,
233                                      guint       prop_id,
234                                      GValue     *value,
235                                      GParamSpec *pspec)
236 {
237 }
238
239 static void
240 g_test_tls_connection_set_property (GObject      *object,
241                                      guint         prop_id,
242                                      const GValue *value,
243                                      GParamSpec   *pspec)
244 {
245 }
246
247 static gboolean
248 g_test_tls_connection_close (GIOStream     *stream,
249                               GCancellable  *cancellable,
250                               GError       **error)
251 {
252   return TRUE;
253 }
254
255 static void
256 g_test_tls_connection_class_init (GTestTlsConnectionClass *connection_class)
257 {
258   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
259   GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
260
261   gobject_class->get_property = g_test_tls_connection_get_property;
262   gobject_class->set_property = g_test_tls_connection_set_property;
263
264   /* Need to override this because when initable_init fails it will
265    * dispose the connection, which will close it, which would
266    * otherwise try to close its input/output streams, which don't
267    * exist.
268    */
269   io_stream_class->close_fn = g_test_tls_connection_close;
270
271   g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
272   g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
273   g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
274   g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
275   g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
276   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
277   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
278   g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
279   g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
280   g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
281   g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
282   g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
283 }
284
285 static void
286 g_test_tls_connection_init (GTestTlsConnection *connection)
287 {
288 }
289
290 static gboolean
291 g_test_tls_connection_initable_init (GInitable       *initable,
292                                       GCancellable    *cancellable,
293                                       GError         **error)
294 {
295   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
296                        "TLS Connection support is not available");
297   return FALSE;
298 }
299
300 static void
301 g_test_tls_connection_initable_iface_init (GInitableIface  *iface)
302 {
303   iface->init = g_test_tls_connection_initable_init;
304 }
305
306 const gchar *
307 g_test_tls_connection_get_private_key_pem (GTlsCertificate *cert)
308 {
309   return ((GTestTlsCertificate *)cert)->key_pem;
310 }