1 /* GLib testing framework examples and tests
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Stef Walter <stefw@collobora.co.uk>
27 #include "gtesttlsbackend.h"
29 static GPtrArray *fixtures = NULL;
32 /* Class virtual interaction methods */
33 gpointer ask_password_func;
34 gpointer ask_password_async_func;
35 gpointer ask_password_finish_func;
36 gpointer request_certificate_func;
37 gpointer request_certificate_async_func;
38 gpointer request_certificate_finish_func;
40 /* Expected results */
41 GTlsInteractionResult result;
44 const gchar *error_message;
48 GTlsInteraction *interaction;
49 GTlsPassword *password;
50 GTlsConnection *connection;
52 GThread *interaction_thread;
55 const Fixture *fixture;
59 GTlsInteraction parent;
64 GTlsInteractionClass parent;
65 } TestInteractionClass;
67 static GType test_interaction_get_type (void);
68 G_DEFINE_TYPE (TestInteraction, test_interaction, G_TYPE_TLS_INTERACTION);
70 #define TEST_TYPE_INTERACTION (test_interaction_get_type ())
71 #define TEST_INTERACTION(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TEST_TYPE_INTERACTION, TestInteraction))
72 #define TEST_IS_INTERACTION(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), TEST_TYPE_INTERACTION))
75 test_interaction_init (TestInteraction *self)
81 test_interaction_class_init (TestInteractionClass *klass)
83 /* By default no virtual methods */
87 test_interaction_ask_password_async_success (GTlsInteraction *interaction,
88 GTlsPassword *password,
89 GCancellable *cancellable,
90 GAsyncReadyCallback callback,
94 TestInteraction *self;
96 g_assert (TEST_IS_INTERACTION (interaction));
97 self = TEST_INTERACTION (interaction);
99 g_assert (g_thread_self () == self->test->interaction_thread);
101 g_assert (G_IS_TLS_PASSWORD (password));
102 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
104 task = g_task_new (self, cancellable, callback, user_data);
106 /* Don't do this in real life. Include a null terminator for testing */
107 g_tls_password_set_value (password, (const guchar *)"the password", 13);
108 g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
109 g_object_unref (task);
113 static GTlsInteractionResult
114 test_interaction_ask_password_finish_success (GTlsInteraction *interaction,
115 GAsyncResult *result,
118 TestInteraction *self;
120 g_assert (TEST_IS_INTERACTION (interaction));
121 self = TEST_INTERACTION (interaction);
123 g_assert (g_thread_self () == self->test->interaction_thread);
125 g_assert (g_task_is_valid (result, interaction));
126 g_assert (error != NULL);
127 g_assert (*error == NULL);
129 return g_task_propagate_int (G_TASK (result), error);
133 test_interaction_ask_password_async_failure (GTlsInteraction *interaction,
134 GTlsPassword *password,
135 GCancellable *cancellable,
136 GAsyncReadyCallback callback,
140 TestInteraction *self;
142 g_assert (TEST_IS_INTERACTION (interaction));
143 self = TEST_INTERACTION (interaction);
145 g_assert (g_thread_self () == self->test->interaction_thread);
147 g_assert (G_IS_TLS_PASSWORD (password));
148 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
150 task = g_task_new (self, cancellable, callback, user_data);
152 g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
153 g_object_unref (task);
156 static GTlsInteractionResult
157 test_interaction_ask_password_finish_failure (GTlsInteraction *interaction,
158 GAsyncResult *result,
161 TestInteraction *self;
163 g_assert (TEST_IS_INTERACTION (interaction));
164 self = TEST_INTERACTION (interaction);
166 g_assert (g_thread_self () == self->test->interaction_thread);
168 g_assert (g_task_is_valid (result, interaction));
169 g_assert (error != NULL);
170 g_assert (*error == NULL);
172 if (g_task_propagate_int (G_TASK (result), error) != -1)
173 g_assert_not_reached ();
175 return G_TLS_INTERACTION_FAILED;
179 static GTlsInteractionResult
180 test_interaction_ask_password_sync_success (GTlsInteraction *interaction,
181 GTlsPassword *password,
182 GCancellable *cancellable,
185 TestInteraction *self;
187 g_assert (TEST_IS_INTERACTION (interaction));
188 self = TEST_INTERACTION (interaction);
190 g_assert (g_thread_self () == self->test->interaction_thread);
192 g_assert (G_IS_TLS_PASSWORD (password));
193 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
194 g_assert (error != NULL);
195 g_assert (*error == NULL);
197 /* Don't do this in real life. Include a null terminator for testing */
198 g_tls_password_set_value (password, (const guchar *)"the password", 13);
199 return G_TLS_INTERACTION_HANDLED;
202 static GTlsInteractionResult
203 test_interaction_ask_password_sync_failure (GTlsInteraction *interaction,
204 GTlsPassword *password,
205 GCancellable *cancellable,
208 TestInteraction *self;
210 g_assert (TEST_IS_INTERACTION (interaction));
211 self = TEST_INTERACTION (interaction);
213 g_assert (g_thread_self () == self->test->interaction_thread);
215 g_assert (G_IS_TLS_PASSWORD (password));
216 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
217 g_assert (error != NULL);
218 g_assert (*error == NULL);
220 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
221 return G_TLS_INTERACTION_FAILED;
225 test_interaction_request_certificate_async_success (GTlsInteraction *interaction,
226 GTlsConnection *connection,
228 GCancellable *cancellable,
229 GAsyncReadyCallback callback,
233 TestInteraction *self;
235 g_assert (TEST_IS_INTERACTION (interaction));
236 self = TEST_INTERACTION (interaction);
238 g_assert (g_thread_self () == self->test->interaction_thread);
240 g_assert (G_IS_TLS_CONNECTION (connection));
241 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
242 g_assert (unused_flags == 0);
244 task = g_task_new (self, cancellable, callback, user_data);
247 * IRL would call g_tls_connection_set_certificate(). But here just touch
248 * the connection in a detectable way.
250 g_object_set_data (G_OBJECT (connection), "chosen-certificate", "my-certificate");
251 g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
252 g_object_unref (task);
255 static GTlsInteractionResult
256 test_interaction_request_certificate_finish_success (GTlsInteraction *interaction,
257 GAsyncResult *result,
260 TestInteraction *self;
262 g_assert (TEST_IS_INTERACTION (interaction));
263 self = TEST_INTERACTION (interaction);
265 g_assert (g_thread_self () == self->test->interaction_thread);
267 g_assert (g_task_is_valid (result, interaction));
268 g_assert (error != NULL);
269 g_assert (*error == NULL);
271 return g_task_propagate_int (G_TASK (result), error);
275 test_interaction_request_certificate_async_failure (GTlsInteraction *interaction,
276 GTlsConnection *connection,
278 GCancellable *cancellable,
279 GAsyncReadyCallback callback,
283 TestInteraction *self;
285 g_assert (TEST_IS_INTERACTION (interaction));
286 self = TEST_INTERACTION (interaction);
288 g_assert (g_thread_self () == self->test->interaction_thread);
290 g_assert (G_IS_TLS_CONNECTION (connection));
291 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
292 g_assert (unused_flags == 0);
294 task = g_task_new (self, cancellable, callback, user_data);
296 g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_NOENT, "Another message");
297 g_object_unref (task);
300 static GTlsInteractionResult
301 test_interaction_request_certificate_finish_failure (GTlsInteraction *interaction,
302 GAsyncResult *result,
305 TestInteraction *self;
307 g_assert (TEST_IS_INTERACTION (interaction));
308 self = TEST_INTERACTION (interaction);
310 g_assert (g_thread_self () == self->test->interaction_thread);
312 g_assert (g_task_is_valid (result, interaction));
313 g_assert (error != NULL);
314 g_assert (*error == NULL);
316 if (g_task_propagate_int (G_TASK (result), error) != -1)
317 g_assert_not_reached ();
319 return G_TLS_INTERACTION_FAILED;
322 static GTlsInteractionResult
323 test_interaction_request_certificate_sync_success (GTlsInteraction *interaction,
324 GTlsConnection *connection,
326 GCancellable *cancellable,
329 TestInteraction *self;
331 g_assert (TEST_IS_INTERACTION (interaction));
332 self = TEST_INTERACTION (interaction);
334 g_assert (g_thread_self () == self->test->interaction_thread);
336 g_assert (G_IS_TLS_CONNECTION (connection));
337 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
338 g_assert (error != NULL);
339 g_assert (*error == NULL);
342 * IRL would call g_tls_connection_set_certificate(). But here just touch
343 * the connection in a detectable way.
345 g_object_set_data (G_OBJECT (connection), "chosen-certificate", "my-certificate");
346 return G_TLS_INTERACTION_HANDLED;
349 static GTlsInteractionResult
350 test_interaction_request_certificate_sync_failure (GTlsInteraction *interaction,
351 GTlsConnection *connection,
353 GCancellable *cancellable,
356 TestInteraction *self;
358 g_assert (TEST_IS_INTERACTION (interaction));
359 self = TEST_INTERACTION (interaction);
361 g_assert (g_thread_self () == self->test->interaction_thread);
363 g_assert (G_IS_TLS_CONNECTION (connection));
364 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
365 g_assert (unused_flags == 0);
366 g_assert (error != NULL);
367 g_assert (*error == NULL);
369 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT, "Another message");
370 return G_TLS_INTERACTION_FAILED;
373 /* ----------------------------------------------------------------------------
378 on_ask_password_async_call (GObject *source,
379 GAsyncResult *result,
382 Test *test = user_data;
383 GTlsInteractionResult res;
384 GError *error = NULL;
386 g_assert (G_IS_TLS_INTERACTION (source));
387 g_assert (G_TLS_INTERACTION (source) == test->interaction);
389 /* Check that this callback is being run in the right place */
390 g_assert (g_thread_self () == test->interaction_thread);
392 res = g_tls_interaction_ask_password_finish (test->interaction, result,
395 /* Check that the results match the fixture */
396 g_assert_cmpuint (test->fixture->result, ==, res);
397 switch (test->fixture->result)
399 case G_TLS_INTERACTION_HANDLED:
400 g_assert_no_error (error);
401 g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
403 case G_TLS_INTERACTION_FAILED:
404 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
405 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
406 g_clear_error (&error);
408 case G_TLS_INTERACTION_UNHANDLED:
409 g_assert_no_error (error);
412 g_assert_not_reached ();
415 /* Signal the end of the test */
416 g_main_loop_quit (test->loop);
420 test_ask_password_async (Test *test,
421 gconstpointer unused)
423 /* This test only works with a main loop */
424 g_assert (test->loop);
426 g_tls_interaction_ask_password_async (test->interaction,
427 test->password, NULL,
428 on_ask_password_async_call,
431 /* teardown waits until g_main_loop_quit(). called from callback */
435 test_invoke_ask_password (Test *test,
436 gconstpointer unused)
438 GTlsInteractionResult res;
439 GError *error = NULL;
441 res = g_tls_interaction_invoke_ask_password (test->interaction, test->password,
444 /* Check that the results match the fixture */
445 g_assert_cmpuint (test->fixture->result, ==, res);
446 switch (test->fixture->result)
448 case G_TLS_INTERACTION_HANDLED:
449 g_assert_no_error (error);
450 g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
452 case G_TLS_INTERACTION_FAILED:
453 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
454 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
455 g_clear_error (&error);
457 case G_TLS_INTERACTION_UNHANDLED:
458 g_assert_no_error (error);
461 g_assert_not_reached ();
464 /* This allows teardown to stop if running with loop */
466 g_main_loop_quit (test->loop);
470 test_ask_password (Test *test,
471 gconstpointer unused)
473 GTlsInteractionResult res;
474 GError *error = NULL;
476 res = g_tls_interaction_ask_password (test->interaction, test->password,
479 /* Check that the results match the fixture */
480 g_assert_cmpuint (test->fixture->result, ==, res);
481 switch (test->fixture->result)
483 case G_TLS_INTERACTION_HANDLED:
484 g_assert_no_error (error);
485 g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
487 case G_TLS_INTERACTION_FAILED:
488 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
489 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
490 g_clear_error (&error);
492 case G_TLS_INTERACTION_UNHANDLED:
493 g_assert_no_error (error);
496 g_assert_not_reached ();
499 /* This allows teardown to stop if running with loop */
501 g_main_loop_quit (test->loop);
505 on_request_certificate_async_call (GObject *source,
506 GAsyncResult *result,
509 Test *test = user_data;
510 GTlsInteractionResult res;
511 GError *error = NULL;
513 g_assert (G_IS_TLS_INTERACTION (source));
514 g_assert (G_TLS_INTERACTION (source) == test->interaction);
516 /* Check that this callback is being run in the right place */
517 g_assert (g_thread_self () == test->interaction_thread);
519 res = g_tls_interaction_request_certificate_finish (test->interaction, result, &error);
521 /* Check that the results match the fixture */
522 g_assert_cmpuint (test->fixture->result, ==, res);
523 switch (test->fixture->result)
525 case G_TLS_INTERACTION_HANDLED:
526 g_assert_no_error (error);
527 g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
529 case G_TLS_INTERACTION_FAILED:
530 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
531 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
532 g_clear_error (&error);
534 case G_TLS_INTERACTION_UNHANDLED:
535 g_assert_no_error (error);
538 g_assert_not_reached ();
541 /* Signal the end of the test */
542 g_main_loop_quit (test->loop);
546 test_request_certificate_async (Test *test,
547 gconstpointer unused)
549 /* This test only works with a main loop */
550 g_assert (test->loop);
552 g_tls_interaction_request_certificate_async (test->interaction,
553 test->connection, 0, NULL,
554 on_request_certificate_async_call,
557 /* teardown waits until g_main_loop_quit(). called from callback */
561 test_invoke_request_certificate (Test *test,
562 gconstpointer unused)
564 GTlsInteractionResult res;
565 GError *error = NULL;
567 res = g_tls_interaction_invoke_request_certificate (test->interaction,
571 /* Check that the results match the fixture */
572 g_assert_cmpuint (test->fixture->result, ==, res);
573 switch (test->fixture->result)
575 case G_TLS_INTERACTION_HANDLED:
576 g_assert_no_error (error);
577 g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
579 case G_TLS_INTERACTION_FAILED:
580 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
581 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
582 g_clear_error (&error);
584 case G_TLS_INTERACTION_UNHANDLED:
585 g_assert_no_error (error);
588 g_assert_not_reached ();
591 /* This allows teardown to stop if running with loop */
593 g_main_loop_quit (test->loop);
597 test_request_certificate (Test *test,
598 gconstpointer unused)
600 GTlsInteractionResult res;
601 GError *error = NULL;
603 res = g_tls_interaction_request_certificate (test->interaction, test->connection,
606 /* Check that the results match the fixture */
607 g_assert_cmpuint (test->fixture->result, ==, res);
608 switch (test->fixture->result)
610 case G_TLS_INTERACTION_HANDLED:
611 g_assert_no_error (error);
612 g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
614 case G_TLS_INTERACTION_FAILED:
615 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
616 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
617 g_clear_error (&error);
619 case G_TLS_INTERACTION_UNHANDLED:
620 g_assert_no_error (error);
623 g_assert_not_reached ();
626 /* This allows teardown to stop if running with loop */
628 g_main_loop_quit (test->loop);
631 /* ----------------------------------------------------------------------------
636 setup_without_loop (Test *test,
637 gconstpointer user_data)
639 const Fixture *fixture = user_data;
640 GTlsInteractionClass *klass;
641 GTlsBackend *backend;
642 GError *error = NULL;
644 test->fixture = fixture;
646 test->interaction = g_object_new (TEST_TYPE_INTERACTION, NULL);
647 g_assert (TEST_IS_INTERACTION (test->interaction));
649 TEST_INTERACTION (test->interaction)->test = test;
651 klass = G_TLS_INTERACTION_GET_CLASS (test->interaction);
652 klass->ask_password = fixture->ask_password_func;
653 klass->ask_password_async = fixture->ask_password_async_func;
654 klass->ask_password_finish = fixture->ask_password_finish_func;
655 klass->request_certificate = fixture->request_certificate_func;
656 klass->request_certificate_async = fixture->request_certificate_async_func;
657 klass->request_certificate_finish = fixture->request_certificate_finish_func;
659 backend = g_object_new (G_TYPE_TEST_TLS_BACKEND, NULL);
660 test->connection = g_object_new (g_tls_backend_get_server_connection_type (backend), NULL);
661 g_assert_no_error (error);
662 g_object_unref (backend);
664 test->password = g_tls_password_new (0, "Description");
665 test->test_thread = g_thread_self ();
668 * If no loop is running then interaction should happen in the same
669 * thread that the tests are running in.
671 test->interaction_thread = test->test_thread;
675 teardown_without_loop (Test *test,
676 gconstpointer unused)
678 gpointer weak_pointer = test->interaction;
680 g_object_add_weak_pointer (weak_pointer, &weak_pointer);
682 g_object_unref (test->connection);
684 g_object_unref (test->password);
686 g_object_unref (test->interaction);
688 g_assert (weak_pointer == NULL);
700 thread_loop (gpointer user_data)
702 GMainContext *context = g_main_context_default ();
703 ThreadLoop *closure = user_data;
704 Test *test = closure->test;
706 g_mutex_lock (&closure->loop_mutex);
708 g_assert (test->loop_thread == g_thread_self ());
709 g_assert (test->loop == NULL);
710 test->loop = g_main_loop_new (context, TRUE);
712 g_main_context_acquire (context);
713 closure->started = TRUE;
714 g_cond_signal (&closure->loop_started);
715 g_mutex_unlock (&closure->loop_mutex);
717 while (g_main_loop_is_running (test->loop))
718 g_main_context_iteration (context, TRUE);
720 g_main_context_release (context);
725 setup_with_thread_loop (Test *test,
726 gconstpointer user_data)
730 setup_without_loop (test, user_data);
732 g_mutex_init (&closure.loop_mutex);
733 g_cond_init (&closure.loop_started);
734 closure.started = FALSE;
737 g_mutex_lock (&closure.loop_mutex);
738 test->loop_thread = g_thread_new ("loop", thread_loop, &closure);
739 while (!closure.started)
740 g_cond_wait (&closure.loop_started, &closure.loop_mutex);
741 g_mutex_unlock (&closure.loop_mutex);
744 * When a loop is running then interaction should always occur in the main
745 * context of that loop.
747 test->interaction_thread = test->loop_thread;
749 g_mutex_clear (&closure.loop_mutex);
750 g_cond_clear (&closure.loop_started);
754 teardown_with_thread_loop (Test *test,
755 gconstpointer unused)
759 g_assert (test->loop_thread);
760 check = g_thread_join (test->loop_thread);
761 g_assert (check == test);
762 test->loop_thread = NULL;
764 g_main_loop_unref (test->loop);
766 teardown_without_loop (test, unused);
770 setup_with_normal_loop (Test *test,
771 gconstpointer user_data)
773 GMainContext *context;
775 setup_without_loop (test, user_data);
777 context = g_main_context_default ();
778 if (!g_main_context_acquire (context))
779 g_assert_not_reached ();
781 test->loop = g_main_loop_new (context, TRUE);
782 g_assert (g_main_loop_is_running (test->loop));
786 teardown_with_normal_loop (Test *test,
787 gconstpointer unused)
789 GMainContext *context;
791 context = g_main_context_default ();
792 while (g_main_loop_is_running (test->loop))
793 g_main_context_iteration (context, TRUE);
795 g_main_context_release (context);
797 /* Run test until complete */
798 g_main_loop_unref (test->loop);
801 teardown_without_loop (test, unused);
804 typedef void (*TestFunc) (Test *test, gconstpointer data);
807 test_with_async_ask_password (const gchar *name,
815 /* Async implementation that succeeds */
816 fixture = g_new0 (Fixture, 1);
817 fixture->ask_password_async_func = test_interaction_ask_password_async_success;
818 fixture->ask_password_finish_func = test_interaction_ask_password_finish_success;
819 fixture->ask_password_func = NULL;
820 fixture->result = G_TLS_INTERACTION_HANDLED;
821 test_name = g_strdup_printf ("%s/async-implementation-success", name);
822 g_test_add (test_name, Test, fixture, setup, func, teardown);
824 g_ptr_array_add (fixtures, fixture);
826 /* Async implementation that fails */
827 fixture = g_new0 (Fixture, 1);
828 fixture->ask_password_async_func = test_interaction_ask_password_async_failure;
829 fixture->ask_password_finish_func = test_interaction_ask_password_finish_failure;
830 fixture->ask_password_func = NULL;
831 fixture->result = G_TLS_INTERACTION_FAILED;
832 fixture->error_domain = G_FILE_ERROR;
833 fixture->error_code = G_FILE_ERROR_ACCES;
834 fixture->error_message = "The message";
835 test_name = g_strdup_printf ("%s/async-implementation-failure", name);
836 g_test_add (test_name, Test, fixture, setup, func, teardown);
838 g_ptr_array_add (fixtures, fixture);
842 test_with_unhandled_ask_password (const gchar *name,
850 /* Unhandled implementation */
851 fixture = g_new0 (Fixture, 1);
852 fixture->ask_password_async_func = NULL;
853 fixture->ask_password_finish_func = NULL;
854 fixture->ask_password_func = NULL;
855 fixture->result = G_TLS_INTERACTION_UNHANDLED;
856 test_name = g_strdup_printf ("%s/unhandled-implementation", name);
857 g_test_add (test_name, Test, fixture, setup, func, teardown);
859 g_ptr_array_add (fixtures, fixture);
863 test_with_sync_ask_password (const gchar *name,
871 /* Sync implementation that succeeds */
872 fixture = g_new0 (Fixture, 1);
873 fixture->ask_password_async_func = NULL;
874 fixture->ask_password_finish_func = NULL;
875 fixture->ask_password_func = test_interaction_ask_password_sync_success;
876 fixture->result = G_TLS_INTERACTION_HANDLED;
877 test_name = g_strdup_printf ("%s/sync-implementation-success", name);
878 g_test_add (test_name, Test, fixture, setup, func, teardown);
880 g_ptr_array_add (fixtures, fixture);
882 /* Async implementation that fails */
883 fixture = g_new0 (Fixture, 1);
884 fixture->ask_password_async_func = NULL;
885 fixture->ask_password_finish_func = NULL;
886 fixture->ask_password_func = test_interaction_ask_password_sync_failure;
887 fixture->result = G_TLS_INTERACTION_FAILED;
888 fixture->error_domain = G_FILE_ERROR;
889 fixture->error_code = G_FILE_ERROR_ACCES;
890 fixture->error_message = "The message";
891 test_name = g_strdup_printf ("%s/sync-implementation-failure", name);
892 g_test_add (test_name, Test, fixture, setup, func, teardown);
894 g_ptr_array_add (fixtures, fixture);
898 test_with_all_ask_password (const gchar *name,
903 test_with_unhandled_ask_password (name, setup, func, teardown);
904 test_with_async_ask_password (name, setup, func, teardown);
905 test_with_sync_ask_password (name, setup, func, teardown);
909 test_with_async_request_certificate (const gchar *name,
917 /* Async implementation that succeeds */
918 fixture = g_new0 (Fixture, 1);
919 fixture->request_certificate_async_func = test_interaction_request_certificate_async_success;
920 fixture->request_certificate_finish_func = test_interaction_request_certificate_finish_success;
921 fixture->request_certificate_func = NULL;
922 fixture->result = G_TLS_INTERACTION_HANDLED;
923 test_name = g_strdup_printf ("%s/async-implementation-success", name);
924 g_test_add (test_name, Test, fixture, setup, func, teardown);
926 g_ptr_array_add (fixtures, fixture);
928 /* Async implementation that fails */
929 fixture = g_new0 (Fixture, 1);
930 fixture->request_certificate_async_func = test_interaction_request_certificate_async_failure;
931 fixture->request_certificate_finish_func = test_interaction_request_certificate_finish_failure;
932 fixture->request_certificate_func = NULL;
933 fixture->result = G_TLS_INTERACTION_FAILED;
934 fixture->error_domain = G_FILE_ERROR;
935 fixture->error_code = G_FILE_ERROR_NOENT;
936 fixture->error_message = "Another message";
937 test_name = g_strdup_printf ("%s/async-implementation-failure", name);
938 g_test_add (test_name, Test, fixture, setup, func, teardown);
940 g_ptr_array_add (fixtures, fixture);
944 test_with_unhandled_request_certificate (const gchar *name,
952 /* Unhandled implementation */
953 fixture = g_new0 (Fixture, 1);
954 fixture->request_certificate_async_func = NULL;
955 fixture->request_certificate_finish_func = NULL;
956 fixture->request_certificate_func = NULL;
957 fixture->result = G_TLS_INTERACTION_UNHANDLED;
958 test_name = g_strdup_printf ("%s/unhandled-implementation", name);
959 g_test_add (test_name, Test, fixture, setup, func, teardown);
961 g_ptr_array_add (fixtures, fixture);
965 test_with_sync_request_certificate (const gchar *name,
973 /* Sync implementation that succeeds */
974 fixture = g_new0 (Fixture, 1);
975 fixture->request_certificate_async_func = NULL;
976 fixture->request_certificate_finish_func = NULL;
977 fixture->request_certificate_func = test_interaction_request_certificate_sync_success;
978 fixture->result = G_TLS_INTERACTION_HANDLED;
979 test_name = g_strdup_printf ("%s/sync-implementation-success", name);
980 g_test_add (test_name, Test, fixture, setup, func, teardown);
982 g_ptr_array_add (fixtures, fixture);
984 /* Async implementation that fails */
985 fixture = g_new0 (Fixture, 1);
986 fixture->request_certificate_async_func = NULL;
987 fixture->request_certificate_finish_func = NULL;
988 fixture->request_certificate_func = test_interaction_request_certificate_sync_failure;
989 fixture->result = G_TLS_INTERACTION_FAILED;
990 fixture->error_domain = G_FILE_ERROR;
991 fixture->error_code = G_FILE_ERROR_NOENT;
992 fixture->error_message = "Another message";
993 test_name = g_strdup_printf ("%s/sync-implementation-failure", name);
994 g_test_add (test_name, Test, fixture, setup, func, teardown);
996 g_ptr_array_add (fixtures, fixture);
1000 test_with_all_request_certificate (const gchar *name,
1005 test_with_unhandled_request_certificate (name, setup, func, teardown);
1006 test_with_async_request_certificate (name, setup, func, teardown);
1007 test_with_sync_request_certificate (name, setup, func, teardown);
1015 g_test_init (&argc, &argv, NULL);
1017 fixtures = g_ptr_array_new_with_free_func (g_free);
1019 /* Tests for g_tls_interaction_invoke_ask_password */
1020 test_with_all_ask_password ("/tls-interaction/ask-password/invoke-with-loop",
1021 setup_with_thread_loop, test_invoke_ask_password, teardown_with_thread_loop);
1022 test_with_all_ask_password ("/tls-interaction/ask-password/invoke-without-loop",
1023 setup_without_loop, test_invoke_ask_password, teardown_without_loop);
1024 test_with_all_ask_password ("/tls-interaction/ask-password/invoke-in-loop",
1025 setup_with_normal_loop, test_invoke_ask_password, teardown_with_normal_loop);
1027 /* Tests for g_tls_interaction_ask_password */
1028 test_with_unhandled_ask_password ("/tls-interaction/ask-password/sync",
1029 setup_without_loop, test_ask_password, teardown_without_loop);
1030 test_with_sync_ask_password ("/tls-interaction/ask-password/sync",
1031 setup_without_loop, test_ask_password, teardown_without_loop);
1033 /* Tests for g_tls_interaction_ask_password_async */
1034 test_with_unhandled_ask_password ("/tls-interaction/ask-password/async",
1035 setup_with_normal_loop, test_ask_password_async, teardown_with_normal_loop);
1036 test_with_async_ask_password ("/tls-interaction/ask-password/async",
1037 setup_with_normal_loop, test_ask_password_async, teardown_with_normal_loop);
1039 /* Tests for g_tls_interaction_invoke_request_certificate */
1040 test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-with-loop",
1041 setup_with_thread_loop, test_invoke_request_certificate, teardown_with_thread_loop);
1042 test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-without-loop",
1043 setup_without_loop, test_invoke_request_certificate, teardown_without_loop);
1044 test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-in-loop",
1045 setup_with_normal_loop, test_invoke_request_certificate, teardown_with_normal_loop);
1047 /* Tests for g_tls_interaction_ask_password */
1048 test_with_unhandled_request_certificate ("/tls-interaction/request-certificate/sync",
1049 setup_without_loop, test_request_certificate, teardown_without_loop);
1050 test_with_sync_request_certificate ("/tls-interaction/request-certificate/sync",
1051 setup_without_loop, test_request_certificate, teardown_without_loop);
1053 /* Tests for g_tls_interaction_ask_password_async */
1054 test_with_unhandled_request_certificate ("/tls-interaction/request-certificate/async",
1055 setup_with_normal_loop, test_request_certificate_async, teardown_with_normal_loop);
1056 test_with_async_request_certificate ("/tls-interaction/request-certificate/async",
1057 setup_with_normal_loop, test_request_certificate_async, teardown_with_normal_loop);
1060 g_ptr_array_free (fixtures, TRUE);