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.1 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/>.
18 * Author: Stef Walter <stefw@collobora.co.uk>
25 #include "gtesttlsbackend.h"
27 static GPtrArray *fixtures = NULL;
30 /* Class virtual interaction methods */
31 gpointer ask_password_func;
32 gpointer ask_password_async_func;
33 gpointer ask_password_finish_func;
34 gpointer request_certificate_func;
35 gpointer request_certificate_async_func;
36 gpointer request_certificate_finish_func;
38 /* Expected results */
39 GTlsInteractionResult result;
42 const gchar *error_message;
46 GTlsInteraction *interaction;
47 GTlsPassword *password;
48 GTlsConnection *connection;
50 GThread *interaction_thread;
53 const Fixture *fixture;
57 GTlsInteraction parent;
62 GTlsInteractionClass parent;
63 } TestInteractionClass;
65 static GType test_interaction_get_type (void);
66 G_DEFINE_TYPE (TestInteraction, test_interaction, G_TYPE_TLS_INTERACTION)
68 #define TEST_TYPE_INTERACTION (test_interaction_get_type ())
69 #define TEST_INTERACTION(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TEST_TYPE_INTERACTION, TestInteraction))
70 #define TEST_IS_INTERACTION(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), TEST_TYPE_INTERACTION))
73 test_interaction_init (TestInteraction *self)
79 test_interaction_class_init (TestInteractionClass *klass)
81 /* By default no virtual methods */
85 test_interaction_ask_password_async_success (GTlsInteraction *interaction,
86 GTlsPassword *password,
87 GCancellable *cancellable,
88 GAsyncReadyCallback callback,
92 TestInteraction *self;
94 g_assert (TEST_IS_INTERACTION (interaction));
95 self = TEST_INTERACTION (interaction);
97 g_assert (g_thread_self () == self->test->interaction_thread);
99 g_assert (G_IS_TLS_PASSWORD (password));
100 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
102 task = g_task_new (self, cancellable, callback, user_data);
104 /* Don't do this in real life. Include a null terminator for testing */
105 g_tls_password_set_value (password, (const guchar *)"the password", 13);
106 g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
107 g_object_unref (task);
111 static GTlsInteractionResult
112 test_interaction_ask_password_finish_success (GTlsInteraction *interaction,
113 GAsyncResult *result,
116 TestInteraction *self;
118 g_assert (TEST_IS_INTERACTION (interaction));
119 self = TEST_INTERACTION (interaction);
121 g_assert (g_thread_self () == self->test->interaction_thread);
123 g_assert (g_task_is_valid (result, interaction));
124 g_assert (error != NULL);
125 g_assert (*error == NULL);
127 return g_task_propagate_int (G_TASK (result), error);
131 test_interaction_ask_password_async_failure (GTlsInteraction *interaction,
132 GTlsPassword *password,
133 GCancellable *cancellable,
134 GAsyncReadyCallback callback,
138 TestInteraction *self;
140 g_assert (TEST_IS_INTERACTION (interaction));
141 self = TEST_INTERACTION (interaction);
143 g_assert (g_thread_self () == self->test->interaction_thread);
145 g_assert (G_IS_TLS_PASSWORD (password));
146 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
148 task = g_task_new (self, cancellable, callback, user_data);
150 g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
151 g_object_unref (task);
154 static GTlsInteractionResult
155 test_interaction_ask_password_finish_failure (GTlsInteraction *interaction,
156 GAsyncResult *result,
159 TestInteraction *self;
161 g_assert (TEST_IS_INTERACTION (interaction));
162 self = TEST_INTERACTION (interaction);
164 g_assert (g_thread_self () == self->test->interaction_thread);
166 g_assert (g_task_is_valid (result, interaction));
167 g_assert (error != NULL);
168 g_assert (*error == NULL);
170 if (g_task_propagate_int (G_TASK (result), error) != -1)
171 g_assert_not_reached ();
173 return G_TLS_INTERACTION_FAILED;
177 /* Return a copy of @str that is allocated in a silly way, to exercise
178 * custom free-functions. The returned pointer points to a copy of @str
179 * in a buffer of the form "BEFORE \0 str \0 AFTER". */
181 special_dup (const char *str)
183 GString *buf = g_string_new ("BEFORE");
186 g_string_append_c (buf, '\0');
187 g_string_append (buf, str);
188 g_string_append_c (buf, '\0');
189 g_string_append (buf, "AFTER");
190 ret = (guchar *) g_string_free (buf, FALSE);
191 return ret + strlen ("BEFORE") + 1;
195 /* Free a copy of @str that was made with special_dup(), after asserting
196 * that it has not been corrupted. */
198 special_free (gpointer p)
201 gchar *buf = s - strlen ("BEFORE") - 1;
203 g_assert_cmpstr (buf, ==, "BEFORE");
204 g_assert_cmpstr (s + strlen (s) + 1, ==, "AFTER");
209 static GTlsInteractionResult
210 test_interaction_ask_password_sync_success (GTlsInteraction *interaction,
211 GTlsPassword *password,
212 GCancellable *cancellable,
215 TestInteraction *self;
219 g_assert (TEST_IS_INTERACTION (interaction));
220 self = TEST_INTERACTION (interaction);
222 g_assert (g_thread_self () == self->test->interaction_thread);
224 g_assert (G_IS_TLS_PASSWORD (password));
225 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
226 g_assert (error != NULL);
227 g_assert (*error == NULL);
229 /* Exercise different ways to set the value */
230 g_tls_password_set_value (password, (const guchar *) "foo", 4);
232 value = g_tls_password_get_value (password, &len);
233 g_assert_cmpmem (value, len, "foo", 4);
235 g_tls_password_set_value (password, (const guchar *) "bar", -1);
237 value = g_tls_password_get_value (password, &len);
238 g_assert_cmpmem (value, len, "bar", 3);
240 g_tls_password_set_value_full (password, special_dup ("baa"), 4, special_free);
242 value = g_tls_password_get_value (password, &len);
243 g_assert_cmpmem (value, len, "baa", 4);
245 g_tls_password_set_value_full (password, special_dup ("baz"), -1, special_free);
247 value = g_tls_password_get_value (password, &len);
248 g_assert_cmpmem (value, len, "baz", 3);
250 /* Don't do this in real life. Include a null terminator for testing */
251 g_tls_password_set_value (password, (const guchar *)"the password", 13);
252 return G_TLS_INTERACTION_HANDLED;
255 static GTlsInteractionResult
256 test_interaction_ask_password_sync_failure (GTlsInteraction *interaction,
257 GTlsPassword *password,
258 GCancellable *cancellable,
261 TestInteraction *self;
263 g_assert (TEST_IS_INTERACTION (interaction));
264 self = TEST_INTERACTION (interaction);
266 g_assert (g_thread_self () == self->test->interaction_thread);
268 g_assert (G_IS_TLS_PASSWORD (password));
269 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
270 g_assert (error != NULL);
271 g_assert (*error == NULL);
273 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
274 return G_TLS_INTERACTION_FAILED;
278 test_interaction_request_certificate_async_success (GTlsInteraction *interaction,
279 GTlsConnection *connection,
281 GCancellable *cancellable,
282 GAsyncReadyCallback callback,
286 TestInteraction *self;
288 g_assert (TEST_IS_INTERACTION (interaction));
289 self = TEST_INTERACTION (interaction);
291 g_assert (g_thread_self () == self->test->interaction_thread);
293 g_assert (G_IS_TLS_CONNECTION (connection));
294 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
295 g_assert (unused_flags == 0);
297 task = g_task_new (self, cancellable, callback, user_data);
300 * IRL would call g_tls_connection_set_certificate(). But here just touch
301 * the connection in a detectable way.
303 g_object_set_data (G_OBJECT (connection), "chosen-certificate", "my-certificate");
304 g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
305 g_object_unref (task);
308 static GTlsInteractionResult
309 test_interaction_request_certificate_finish_success (GTlsInteraction *interaction,
310 GAsyncResult *result,
313 TestInteraction *self;
315 g_assert (TEST_IS_INTERACTION (interaction));
316 self = TEST_INTERACTION (interaction);
318 g_assert (g_thread_self () == self->test->interaction_thread);
320 g_assert (g_task_is_valid (result, interaction));
321 g_assert (error != NULL);
322 g_assert (*error == NULL);
324 return g_task_propagate_int (G_TASK (result), error);
328 test_interaction_request_certificate_async_failure (GTlsInteraction *interaction,
329 GTlsConnection *connection,
331 GCancellable *cancellable,
332 GAsyncReadyCallback callback,
336 TestInteraction *self;
338 g_assert (TEST_IS_INTERACTION (interaction));
339 self = TEST_INTERACTION (interaction);
341 g_assert (g_thread_self () == self->test->interaction_thread);
343 g_assert (G_IS_TLS_CONNECTION (connection));
344 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
345 g_assert (unused_flags == 0);
347 task = g_task_new (self, cancellable, callback, user_data);
349 g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_NOENT, "Another message");
350 g_object_unref (task);
353 static GTlsInteractionResult
354 test_interaction_request_certificate_finish_failure (GTlsInteraction *interaction,
355 GAsyncResult *result,
358 TestInteraction *self;
360 g_assert (TEST_IS_INTERACTION (interaction));
361 self = TEST_INTERACTION (interaction);
363 g_assert (g_thread_self () == self->test->interaction_thread);
365 g_assert (g_task_is_valid (result, interaction));
366 g_assert (error != NULL);
367 g_assert (*error == NULL);
369 if (g_task_propagate_int (G_TASK (result), error) != -1)
370 g_assert_not_reached ();
372 return G_TLS_INTERACTION_FAILED;
375 static GTlsInteractionResult
376 test_interaction_request_certificate_sync_success (GTlsInteraction *interaction,
377 GTlsConnection *connection,
379 GCancellable *cancellable,
382 TestInteraction *self;
384 g_assert (TEST_IS_INTERACTION (interaction));
385 self = TEST_INTERACTION (interaction);
387 g_assert (g_thread_self () == self->test->interaction_thread);
389 g_assert (G_IS_TLS_CONNECTION (connection));
390 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
391 g_assert (error != NULL);
392 g_assert (*error == NULL);
395 * IRL would call g_tls_connection_set_certificate(). But here just touch
396 * the connection in a detectable way.
398 g_object_set_data (G_OBJECT (connection), "chosen-certificate", "my-certificate");
399 return G_TLS_INTERACTION_HANDLED;
402 static GTlsInteractionResult
403 test_interaction_request_certificate_sync_failure (GTlsInteraction *interaction,
404 GTlsConnection *connection,
406 GCancellable *cancellable,
409 TestInteraction *self;
411 g_assert (TEST_IS_INTERACTION (interaction));
412 self = TEST_INTERACTION (interaction);
414 g_assert (g_thread_self () == self->test->interaction_thread);
416 g_assert (G_IS_TLS_CONNECTION (connection));
417 g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
418 g_assert (unused_flags == 0);
419 g_assert (error != NULL);
420 g_assert (*error == NULL);
422 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT, "Another message");
423 return G_TLS_INTERACTION_FAILED;
426 /* ----------------------------------------------------------------------------
431 on_ask_password_async_call (GObject *source,
432 GAsyncResult *result,
435 Test *test = user_data;
436 GTlsInteractionResult res;
437 GError *error = NULL;
439 g_assert (G_IS_TLS_INTERACTION (source));
440 g_assert (G_TLS_INTERACTION (source) == test->interaction);
442 /* Check that this callback is being run in the right place */
443 g_assert (g_thread_self () == test->interaction_thread);
445 res = g_tls_interaction_ask_password_finish (test->interaction, result,
448 /* Check that the results match the fixture */
449 g_assert_cmpuint (test->fixture->result, ==, res);
450 switch (test->fixture->result)
452 case G_TLS_INTERACTION_HANDLED:
453 g_assert_no_error (error);
454 g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
456 case G_TLS_INTERACTION_FAILED:
457 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
458 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
459 g_clear_error (&error);
461 case G_TLS_INTERACTION_UNHANDLED:
462 g_assert_no_error (error);
465 g_assert_not_reached ();
468 /* Signal the end of the test */
469 g_main_loop_quit (test->loop);
473 test_ask_password_async (Test *test,
474 gconstpointer unused)
476 /* This test only works with a main loop */
477 g_assert (test->loop);
479 g_tls_interaction_ask_password_async (test->interaction,
480 test->password, NULL,
481 on_ask_password_async_call,
484 /* teardown waits until g_main_loop_quit(). called from callback */
488 test_invoke_ask_password (Test *test,
489 gconstpointer unused)
491 GTlsInteractionResult res;
492 GError *error = NULL;
494 res = g_tls_interaction_invoke_ask_password (test->interaction, test->password,
497 /* Check that the results match the fixture */
498 g_assert_cmpuint (test->fixture->result, ==, res);
499 switch (test->fixture->result)
501 case G_TLS_INTERACTION_HANDLED:
502 g_assert_no_error (error);
503 g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
505 case G_TLS_INTERACTION_FAILED:
506 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
507 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
508 g_clear_error (&error);
510 case G_TLS_INTERACTION_UNHANDLED:
511 g_assert_no_error (error);
514 g_assert_not_reached ();
517 /* This allows teardown to stop if running with loop */
519 g_main_loop_quit (test->loop);
523 test_ask_password (Test *test,
524 gconstpointer unused)
526 GTlsInteractionResult res;
527 GError *error = NULL;
529 res = g_tls_interaction_ask_password (test->interaction, test->password,
532 /* Check that the results match the fixture */
533 g_assert_cmpuint (test->fixture->result, ==, res);
534 switch (test->fixture->result)
536 case G_TLS_INTERACTION_HANDLED:
537 g_assert_no_error (error);
538 g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
540 case G_TLS_INTERACTION_FAILED:
541 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
542 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
543 g_clear_error (&error);
545 case G_TLS_INTERACTION_UNHANDLED:
546 g_assert_no_error (error);
549 g_assert_not_reached ();
552 /* This allows teardown to stop if running with loop */
554 g_main_loop_quit (test->loop);
558 on_request_certificate_async_call (GObject *source,
559 GAsyncResult *result,
562 Test *test = user_data;
563 GTlsInteractionResult res;
564 GError *error = NULL;
566 g_assert (G_IS_TLS_INTERACTION (source));
567 g_assert (G_TLS_INTERACTION (source) == test->interaction);
569 /* Check that this callback is being run in the right place */
570 g_assert (g_thread_self () == test->interaction_thread);
572 res = g_tls_interaction_request_certificate_finish (test->interaction, result, &error);
574 /* Check that the results match the fixture */
575 g_assert_cmpuint (test->fixture->result, ==, res);
576 switch (test->fixture->result)
578 case G_TLS_INTERACTION_HANDLED:
579 g_assert_no_error (error);
580 g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
582 case G_TLS_INTERACTION_FAILED:
583 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
584 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
585 g_clear_error (&error);
587 case G_TLS_INTERACTION_UNHANDLED:
588 g_assert_no_error (error);
591 g_assert_not_reached ();
594 /* Signal the end of the test */
595 g_main_loop_quit (test->loop);
599 test_request_certificate_async (Test *test,
600 gconstpointer unused)
602 /* This test only works with a main loop */
603 g_assert (test->loop);
605 g_tls_interaction_request_certificate_async (test->interaction,
606 test->connection, 0, NULL,
607 on_request_certificate_async_call,
610 /* teardown waits until g_main_loop_quit(). called from callback */
614 test_invoke_request_certificate (Test *test,
615 gconstpointer unused)
617 GTlsInteractionResult res;
618 GError *error = NULL;
620 res = g_tls_interaction_invoke_request_certificate (test->interaction,
624 /* Check that the results match the fixture */
625 g_assert_cmpuint (test->fixture->result, ==, res);
626 switch (test->fixture->result)
628 case G_TLS_INTERACTION_HANDLED:
629 g_assert_no_error (error);
630 g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
632 case G_TLS_INTERACTION_FAILED:
633 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
634 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
635 g_clear_error (&error);
637 case G_TLS_INTERACTION_UNHANDLED:
638 g_assert_no_error (error);
641 g_assert_not_reached ();
644 /* This allows teardown to stop if running with loop */
646 g_main_loop_quit (test->loop);
650 test_request_certificate (Test *test,
651 gconstpointer unused)
653 GTlsInteractionResult res;
654 GError *error = NULL;
656 res = g_tls_interaction_request_certificate (test->interaction, test->connection,
659 /* Check that the results match the fixture */
660 g_assert_cmpuint (test->fixture->result, ==, res);
661 switch (test->fixture->result)
663 case G_TLS_INTERACTION_HANDLED:
664 g_assert_no_error (error);
665 g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
667 case G_TLS_INTERACTION_FAILED:
668 g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
669 g_assert_cmpstr (error->message, ==, test->fixture->error_message);
670 g_clear_error (&error);
672 case G_TLS_INTERACTION_UNHANDLED:
673 g_assert_no_error (error);
676 g_assert_not_reached ();
679 /* This allows teardown to stop if running with loop */
681 g_main_loop_quit (test->loop);
684 /* ----------------------------------------------------------------------------
689 setup_without_loop (Test *test,
690 gconstpointer user_data)
692 const Fixture *fixture = user_data;
693 GTlsInteractionClass *klass;
694 GTlsBackend *backend;
695 GError *error = NULL;
697 test->fixture = fixture;
699 test->interaction = g_object_new (TEST_TYPE_INTERACTION, NULL);
700 g_assert (TEST_IS_INTERACTION (test->interaction));
702 TEST_INTERACTION (test->interaction)->test = test;
704 klass = G_TLS_INTERACTION_GET_CLASS (test->interaction);
705 klass->ask_password = fixture->ask_password_func;
706 klass->ask_password_async = fixture->ask_password_async_func;
707 klass->ask_password_finish = fixture->ask_password_finish_func;
708 klass->request_certificate = fixture->request_certificate_func;
709 klass->request_certificate_async = fixture->request_certificate_async_func;
710 klass->request_certificate_finish = fixture->request_certificate_finish_func;
712 backend = g_object_new (G_TYPE_TEST_TLS_BACKEND, NULL);
713 test->connection = g_object_new (g_tls_backend_get_server_connection_type (backend), NULL);
714 g_assert_no_error (error);
715 g_object_unref (backend);
717 test->password = g_tls_password_new (0, "Description");
718 test->test_thread = g_thread_self ();
721 * If no loop is running then interaction should happen in the same
722 * thread that the tests are running in.
724 test->interaction_thread = test->test_thread;
728 teardown_without_loop (Test *test,
729 gconstpointer unused)
731 g_object_unref (test->connection);
732 g_object_unref (test->password);
734 g_assert_finalize_object (test->interaction);
745 thread_loop (gpointer user_data)
747 GMainContext *context = g_main_context_default ();
748 ThreadLoop *closure = user_data;
749 Test *test = closure->test;
751 g_mutex_lock (&closure->loop_mutex);
753 g_assert (test->loop_thread == g_thread_self ());
754 g_assert (test->loop == NULL);
755 test->loop = g_main_loop_new (context, TRUE);
757 g_main_context_acquire (context);
758 closure->started = TRUE;
759 g_cond_signal (&closure->loop_started);
760 g_mutex_unlock (&closure->loop_mutex);
762 while (g_main_loop_is_running (test->loop))
763 g_main_context_iteration (context, TRUE);
765 g_main_context_release (context);
770 setup_with_thread_loop (Test *test,
771 gconstpointer user_data)
775 setup_without_loop (test, user_data);
777 g_mutex_init (&closure.loop_mutex);
778 g_cond_init (&closure.loop_started);
779 closure.started = FALSE;
782 g_mutex_lock (&closure.loop_mutex);
783 test->loop_thread = g_thread_new ("loop", thread_loop, &closure);
784 while (!closure.started)
785 g_cond_wait (&closure.loop_started, &closure.loop_mutex);
786 g_mutex_unlock (&closure.loop_mutex);
789 * When a loop is running then interaction should always occur in the main
790 * context of that loop.
792 test->interaction_thread = test->loop_thread;
794 g_mutex_clear (&closure.loop_mutex);
795 g_cond_clear (&closure.loop_started);
799 teardown_with_thread_loop (Test *test,
800 gconstpointer unused)
804 g_assert (test->loop_thread);
805 check = g_thread_join (test->loop_thread);
806 g_assert (check == test);
807 test->loop_thread = NULL;
809 g_main_loop_unref (test->loop);
811 teardown_without_loop (test, unused);
815 setup_with_normal_loop (Test *test,
816 gconstpointer user_data)
818 GMainContext *context;
820 setup_without_loop (test, user_data);
822 context = g_main_context_default ();
823 if (!g_main_context_acquire (context))
824 g_assert_not_reached ();
826 test->loop = g_main_loop_new (context, TRUE);
827 g_assert (g_main_loop_is_running (test->loop));
831 teardown_with_normal_loop (Test *test,
832 gconstpointer unused)
834 GMainContext *context;
836 context = g_main_context_default ();
837 while (g_main_loop_is_running (test->loop))
838 g_main_context_iteration (context, TRUE);
840 g_main_context_release (context);
842 /* Run test until complete */
843 g_main_loop_unref (test->loop);
846 teardown_without_loop (test, unused);
849 typedef void (*TestFunc) (Test *test, gconstpointer data);
852 test_with_async_ask_password (const gchar *name,
860 /* Async implementation that succeeds */
861 fixture = g_new0 (Fixture, 1);
862 fixture->ask_password_async_func = test_interaction_ask_password_async_success;
863 fixture->ask_password_finish_func = test_interaction_ask_password_finish_success;
864 fixture->ask_password_func = NULL;
865 fixture->result = G_TLS_INTERACTION_HANDLED;
866 test_name = g_strdup_printf ("%s/async-implementation-success", name);
867 g_test_add (test_name, Test, fixture, setup, func, teardown);
869 g_ptr_array_add (fixtures, fixture);
871 /* Async implementation that fails */
872 fixture = g_new0 (Fixture, 1);
873 fixture->ask_password_async_func = test_interaction_ask_password_async_failure;
874 fixture->ask_password_finish_func = test_interaction_ask_password_finish_failure;
875 fixture->ask_password_func = NULL;
876 fixture->result = G_TLS_INTERACTION_FAILED;
877 fixture->error_domain = G_FILE_ERROR;
878 fixture->error_code = G_FILE_ERROR_ACCES;
879 fixture->error_message = "The message";
880 test_name = g_strdup_printf ("%s/async-implementation-failure", name);
881 g_test_add (test_name, Test, fixture, setup, func, teardown);
883 g_ptr_array_add (fixtures, fixture);
887 test_with_unhandled_ask_password (const gchar *name,
895 /* Unhandled implementation */
896 fixture = g_new0 (Fixture, 1);
897 fixture->ask_password_async_func = NULL;
898 fixture->ask_password_finish_func = NULL;
899 fixture->ask_password_func = NULL;
900 fixture->result = G_TLS_INTERACTION_UNHANDLED;
901 test_name = g_strdup_printf ("%s/unhandled-implementation", name);
902 g_test_add (test_name, Test, fixture, setup, func, teardown);
904 g_ptr_array_add (fixtures, fixture);
908 test_with_sync_ask_password (const gchar *name,
916 /* Sync implementation that succeeds */
917 fixture = g_new0 (Fixture, 1);
918 fixture->ask_password_async_func = NULL;
919 fixture->ask_password_finish_func = NULL;
920 fixture->ask_password_func = test_interaction_ask_password_sync_success;
921 fixture->result = G_TLS_INTERACTION_HANDLED;
922 test_name = g_strdup_printf ("%s/sync-implementation-success", name);
923 g_test_add (test_name, Test, fixture, setup, func, teardown);
925 g_ptr_array_add (fixtures, fixture);
927 /* Async implementation that fails */
928 fixture = g_new0 (Fixture, 1);
929 fixture->ask_password_async_func = NULL;
930 fixture->ask_password_finish_func = NULL;
931 fixture->ask_password_func = test_interaction_ask_password_sync_failure;
932 fixture->result = G_TLS_INTERACTION_FAILED;
933 fixture->error_domain = G_FILE_ERROR;
934 fixture->error_code = G_FILE_ERROR_ACCES;
935 fixture->error_message = "The message";
936 test_name = g_strdup_printf ("%s/sync-implementation-failure", name);
937 g_test_add (test_name, Test, fixture, setup, func, teardown);
939 g_ptr_array_add (fixtures, fixture);
943 test_with_all_ask_password (const gchar *name,
948 test_with_unhandled_ask_password (name, setup, func, teardown);
949 test_with_async_ask_password (name, setup, func, teardown);
950 test_with_sync_ask_password (name, setup, func, teardown);
954 test_with_async_request_certificate (const gchar *name,
962 /* Async implementation that succeeds */
963 fixture = g_new0 (Fixture, 1);
964 fixture->request_certificate_async_func = test_interaction_request_certificate_async_success;
965 fixture->request_certificate_finish_func = test_interaction_request_certificate_finish_success;
966 fixture->request_certificate_func = NULL;
967 fixture->result = G_TLS_INTERACTION_HANDLED;
968 test_name = g_strdup_printf ("%s/async-implementation-success", name);
969 g_test_add (test_name, Test, fixture, setup, func, teardown);
971 g_ptr_array_add (fixtures, fixture);
973 /* Async implementation that fails */
974 fixture = g_new0 (Fixture, 1);
975 fixture->request_certificate_async_func = test_interaction_request_certificate_async_failure;
976 fixture->request_certificate_finish_func = test_interaction_request_certificate_finish_failure;
977 fixture->request_certificate_func = NULL;
978 fixture->result = G_TLS_INTERACTION_FAILED;
979 fixture->error_domain = G_FILE_ERROR;
980 fixture->error_code = G_FILE_ERROR_NOENT;
981 fixture->error_message = "Another message";
982 test_name = g_strdup_printf ("%s/async-implementation-failure", name);
983 g_test_add (test_name, Test, fixture, setup, func, teardown);
985 g_ptr_array_add (fixtures, fixture);
989 test_with_unhandled_request_certificate (const gchar *name,
997 /* Unhandled implementation */
998 fixture = g_new0 (Fixture, 1);
999 fixture->request_certificate_async_func = NULL;
1000 fixture->request_certificate_finish_func = NULL;
1001 fixture->request_certificate_func = NULL;
1002 fixture->result = G_TLS_INTERACTION_UNHANDLED;
1003 test_name = g_strdup_printf ("%s/unhandled-implementation", name);
1004 g_test_add (test_name, Test, fixture, setup, func, teardown);
1006 g_ptr_array_add (fixtures, fixture);
1010 test_with_sync_request_certificate (const gchar *name,
1018 /* Sync implementation that succeeds */
1019 fixture = g_new0 (Fixture, 1);
1020 fixture->request_certificate_async_func = NULL;
1021 fixture->request_certificate_finish_func = NULL;
1022 fixture->request_certificate_func = test_interaction_request_certificate_sync_success;
1023 fixture->result = G_TLS_INTERACTION_HANDLED;
1024 test_name = g_strdup_printf ("%s/sync-implementation-success", name);
1025 g_test_add (test_name, Test, fixture, setup, func, teardown);
1027 g_ptr_array_add (fixtures, fixture);
1029 /* Async implementation that fails */
1030 fixture = g_new0 (Fixture, 1);
1031 fixture->request_certificate_async_func = NULL;
1032 fixture->request_certificate_finish_func = NULL;
1033 fixture->request_certificate_func = test_interaction_request_certificate_sync_failure;
1034 fixture->result = G_TLS_INTERACTION_FAILED;
1035 fixture->error_domain = G_FILE_ERROR;
1036 fixture->error_code = G_FILE_ERROR_NOENT;
1037 fixture->error_message = "Another message";
1038 test_name = g_strdup_printf ("%s/sync-implementation-failure", name);
1039 g_test_add (test_name, Test, fixture, setup, func, teardown);
1041 g_ptr_array_add (fixtures, fixture);
1045 test_with_all_request_certificate (const gchar *name,
1050 test_with_unhandled_request_certificate (name, setup, func, teardown);
1051 test_with_async_request_certificate (name, setup, func, teardown);
1052 test_with_sync_request_certificate (name, setup, func, teardown);
1060 g_test_init (&argc, &argv, NULL);
1062 fixtures = g_ptr_array_new_with_free_func (g_free);
1064 /* Tests for g_tls_interaction_invoke_ask_password */
1065 test_with_all_ask_password ("/tls-interaction/ask-password/invoke-with-loop",
1066 setup_with_thread_loop, test_invoke_ask_password, teardown_with_thread_loop);
1067 test_with_all_ask_password ("/tls-interaction/ask-password/invoke-without-loop",
1068 setup_without_loop, test_invoke_ask_password, teardown_without_loop);
1069 test_with_all_ask_password ("/tls-interaction/ask-password/invoke-in-loop",
1070 setup_with_normal_loop, test_invoke_ask_password, teardown_with_normal_loop);
1072 /* Tests for g_tls_interaction_ask_password */
1073 test_with_unhandled_ask_password ("/tls-interaction/ask-password/sync",
1074 setup_without_loop, test_ask_password, teardown_without_loop);
1075 test_with_sync_ask_password ("/tls-interaction/ask-password/sync",
1076 setup_without_loop, test_ask_password, teardown_without_loop);
1078 /* Tests for g_tls_interaction_ask_password_async */
1079 test_with_unhandled_ask_password ("/tls-interaction/ask-password/async",
1080 setup_with_normal_loop, test_ask_password_async, teardown_with_normal_loop);
1081 test_with_async_ask_password ("/tls-interaction/ask-password/async",
1082 setup_with_normal_loop, test_ask_password_async, teardown_with_normal_loop);
1084 /* Tests for g_tls_interaction_invoke_request_certificate */
1085 test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-with-loop",
1086 setup_with_thread_loop, test_invoke_request_certificate, teardown_with_thread_loop);
1087 test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-without-loop",
1088 setup_without_loop, test_invoke_request_certificate, teardown_without_loop);
1089 test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-in-loop",
1090 setup_with_normal_loop, test_invoke_request_certificate, teardown_with_normal_loop);
1092 /* Tests for g_tls_interaction_ask_password */
1093 test_with_unhandled_request_certificate ("/tls-interaction/request-certificate/sync",
1094 setup_without_loop, test_request_certificate, teardown_without_loop);
1095 test_with_sync_request_certificate ("/tls-interaction/request-certificate/sync",
1096 setup_without_loop, test_request_certificate, teardown_without_loop);
1098 /* Tests for g_tls_interaction_ask_password_async */
1099 test_with_unhandled_request_certificate ("/tls-interaction/request-certificate/async",
1100 setup_with_normal_loop, test_request_certificate_async, teardown_with_normal_loop);
1101 test_with_async_request_certificate ("/tls-interaction/request-certificate/async",
1102 setup_with_normal_loop, test_request_certificate_async, teardown_with_normal_loop);
1105 g_ptr_array_free (fixtures, TRUE);