Blame gio/tests/tls-interaction.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2011 Collabora Ltd.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Stef Walter <stefw@collobora.co.uk>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
Packit ae235b
#include "gtesttlsbackend.h"
Packit ae235b
Packit ae235b
static GPtrArray *fixtures = NULL;
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  /* Class virtual interaction methods */
Packit ae235b
  gpointer ask_password_func;
Packit ae235b
  gpointer ask_password_async_func;
Packit ae235b
  gpointer ask_password_finish_func;
Packit ae235b
  gpointer request_certificate_func;
Packit ae235b
  gpointer request_certificate_async_func;
Packit ae235b
  gpointer request_certificate_finish_func;
Packit ae235b
Packit ae235b
  /* Expected results */
Packit ae235b
  GTlsInteractionResult result;
Packit ae235b
  GQuark error_domain;
Packit ae235b
  gint error_code;
Packit ae235b
  const gchar *error_message;
Packit ae235b
} Fixture;
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GTlsInteraction *interaction;
Packit ae235b
  GTlsPassword *password;
Packit ae235b
  GTlsConnection *connection;
Packit ae235b
  GMainLoop *loop;
Packit ae235b
  GThread *interaction_thread;
Packit ae235b
  GThread *test_thread;
Packit ae235b
  GThread *loop_thread;
Packit ae235b
  const Fixture *fixture;
Packit ae235b
} Test;
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GTlsInteraction parent;
Packit ae235b
  Test *test;
Packit ae235b
} TestInteraction;
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GTlsInteractionClass parent;
Packit ae235b
} TestInteractionClass;
Packit ae235b
Packit ae235b
static GType test_interaction_get_type (void);
Packit ae235b
G_DEFINE_TYPE (TestInteraction, test_interaction, G_TYPE_TLS_INTERACTION)
Packit ae235b
Packit ae235b
#define TEST_TYPE_INTERACTION         (test_interaction_get_type ())
Packit ae235b
#define TEST_INTERACTION(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), TEST_TYPE_INTERACTION, TestInteraction))
Packit ae235b
#define TEST_IS_INTERACTION(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), TEST_TYPE_INTERACTION))
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_interaction_init (TestInteraction *self)
Packit ae235b
{
Packit ae235b
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_interaction_class_init (TestInteractionClass *klass)
Packit ae235b
{
Packit ae235b
  /* By default no virtual methods */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_interaction_ask_password_async_success (GTlsInteraction    *interaction,
Packit ae235b
                                             GTlsPassword       *password,
Packit ae235b
                                             GCancellable       *cancellable,
Packit ae235b
                                             GAsyncReadyCallback callback,
Packit ae235b
                                             gpointer            user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_PASSWORD (password));
Packit ae235b
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
Packit ae235b
  task = g_task_new (self, cancellable, callback, user_data);
Packit ae235b
Packit ae235b
  /* Don't do this in real life. Include a null terminator for testing */
Packit ae235b
  g_tls_password_set_value (password, (const guchar *)"the password", 13);
Packit ae235b
  g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
test_interaction_ask_password_finish_success (GTlsInteraction    *interaction,
Packit ae235b
                                              GAsyncResult       *result,
Packit ae235b
                                              GError            **error)
Packit ae235b
{
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (g_task_is_valid (result, interaction));
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (*error == NULL);
Packit ae235b
Packit ae235b
  return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_interaction_ask_password_async_failure (GTlsInteraction    *interaction,
Packit ae235b
                                             GTlsPassword       *password,
Packit ae235b
                                             GCancellable       *cancellable,
Packit ae235b
                                             GAsyncReadyCallback callback,
Packit ae235b
                                             gpointer            user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_PASSWORD (password));
Packit ae235b
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
Packit ae235b
  task = g_task_new (self, cancellable, callback, user_data);
Packit ae235b
Packit ae235b
  g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
test_interaction_ask_password_finish_failure (GTlsInteraction    *interaction,
Packit ae235b
                                              GAsyncResult       *result,
Packit ae235b
                                              GError            **error)
Packit ae235b
{
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (g_task_is_valid (result, interaction));
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (*error == NULL);
Packit ae235b
Packit ae235b
  if (g_task_propagate_int (G_TASK (result), error) != -1)
Packit ae235b
    g_assert_not_reached ();
Packit ae235b
Packit ae235b
  return G_TLS_INTERACTION_FAILED;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
test_interaction_ask_password_sync_success (GTlsInteraction    *interaction,
Packit ae235b
                                            GTlsPassword       *password,
Packit ae235b
                                            GCancellable       *cancellable,
Packit ae235b
                                            GError            **error)
Packit ae235b
{
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_PASSWORD (password));
Packit ae235b
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (*error == NULL);
Packit ae235b
Packit ae235b
  /* Don't do this in real life. Include a null terminator for testing */
Packit ae235b
  g_tls_password_set_value (password, (const guchar *)"the password", 13);
Packit ae235b
  return G_TLS_INTERACTION_HANDLED;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
test_interaction_ask_password_sync_failure (GTlsInteraction    *interaction,
Packit ae235b
                                            GTlsPassword       *password,
Packit ae235b
                                            GCancellable       *cancellable,
Packit ae235b
                                            GError            **error)
Packit ae235b
{
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_PASSWORD (password));
Packit ae235b
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (*error == NULL);
Packit ae235b
Packit ae235b
  g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
Packit ae235b
  return G_TLS_INTERACTION_FAILED;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_interaction_request_certificate_async_success (GTlsInteraction    *interaction,
Packit ae235b
                                                    GTlsConnection     *connection,
Packit ae235b
                                                    gint                unused_flags,
Packit ae235b
                                                    GCancellable       *cancellable,
Packit ae235b
                                                    GAsyncReadyCallback callback,
Packit ae235b
                                                    gpointer            user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_CONNECTION (connection));
Packit ae235b
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
  g_assert (unused_flags == 0);
Packit ae235b
Packit ae235b
  task = g_task_new (self, cancellable, callback, user_data);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * IRL would call g_tls_connection_set_certificate(). But here just touch
Packit ae235b
   * the connection in a detectable way.
Packit ae235b
   */
Packit ae235b
  g_object_set_data (G_OBJECT (connection), "chosen-certificate", "my-certificate");
Packit ae235b
  g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
test_interaction_request_certificate_finish_success (GTlsInteraction    *interaction,
Packit ae235b
                                                     GAsyncResult       *result,
Packit ae235b
                                                     GError            **error)
Packit ae235b
{
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (g_task_is_valid (result, interaction));
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (*error == NULL);
Packit ae235b
Packit ae235b
  return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_interaction_request_certificate_async_failure (GTlsInteraction    *interaction,
Packit ae235b
                                                    GTlsConnection     *connection,
Packit ae235b
                                                    gint                unused_flags,
Packit ae235b
                                                    GCancellable       *cancellable,
Packit ae235b
                                                    GAsyncReadyCallback callback,
Packit ae235b
                                                    gpointer            user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_CONNECTION (connection));
Packit ae235b
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
  g_assert (unused_flags == 0);
Packit ae235b
Packit ae235b
  task = g_task_new (self, cancellable, callback, user_data);
Packit ae235b
Packit ae235b
  g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_NOENT, "Another message");
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
test_interaction_request_certificate_finish_failure (GTlsInteraction    *interaction,
Packit ae235b
                                                     GAsyncResult       *result,
Packit ae235b
                                                     GError            **error)
Packit ae235b
{
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (g_task_is_valid (result, interaction));
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (*error == NULL);
Packit ae235b
Packit ae235b
  if (g_task_propagate_int (G_TASK (result), error) != -1)
Packit ae235b
    g_assert_not_reached ();
Packit ae235b
Packit ae235b
  return G_TLS_INTERACTION_FAILED;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
test_interaction_request_certificate_sync_success (GTlsInteraction    *interaction,
Packit ae235b
                                                   GTlsConnection      *connection,
Packit ae235b
                                                   gint                 unused_flags,
Packit ae235b
                                                   GCancellable        *cancellable,
Packit ae235b
                                                   GError             **error)
Packit ae235b
{
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_CONNECTION (connection));
Packit ae235b
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (*error == NULL);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * IRL would call g_tls_connection_set_certificate(). But here just touch
Packit ae235b
   * the connection in a detectable way.
Packit ae235b
   */
Packit ae235b
  g_object_set_data (G_OBJECT (connection), "chosen-certificate", "my-certificate");
Packit ae235b
  return G_TLS_INTERACTION_HANDLED;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
test_interaction_request_certificate_sync_failure (GTlsInteraction    *interaction,
Packit ae235b
                                                   GTlsConnection     *connection,
Packit ae235b
                                                   gint                unused_flags,
Packit ae235b
                                                   GCancellable       *cancellable,
Packit ae235b
                                                   GError            **error)
Packit ae235b
{
Packit ae235b
  TestInteraction *self;
Packit ae235b
Packit ae235b
  g_assert (TEST_IS_INTERACTION (interaction));
Packit ae235b
  self = TEST_INTERACTION (interaction);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == self->test->interaction_thread);
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_CONNECTION (connection));
Packit ae235b
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
  g_assert (unused_flags == 0);
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (*error == NULL);
Packit ae235b
Packit ae235b
  g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT, "Another message");
Packit ae235b
  return G_TLS_INTERACTION_FAILED;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ----------------------------------------------------------------------------
Packit ae235b
 * ACTUAL TESTS
Packit ae235b
 */
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_ask_password_async_call (GObject      *source,
Packit ae235b
                            GAsyncResult *result,
Packit ae235b
                            gpointer      user_data)
Packit ae235b
{
Packit ae235b
  Test *test = user_data;
Packit ae235b
  GTlsInteractionResult res;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_INTERACTION (source));
Packit ae235b
  g_assert (G_TLS_INTERACTION (source) == test->interaction);
Packit ae235b
Packit ae235b
  /* Check that this callback is being run in the right place */
Packit ae235b
  g_assert (g_thread_self () == test->interaction_thread);
Packit ae235b
Packit ae235b
  res = g_tls_interaction_ask_password_finish (test->interaction, result,
Packit ae235b
                                               &error);
Packit ae235b
Packit ae235b
  /* Check that the results match the fixture */
Packit ae235b
  g_assert_cmpuint (test->fixture->result, ==, res);
Packit ae235b
  switch (test->fixture->result)
Packit ae235b
    {
Packit ae235b
      case G_TLS_INTERACTION_HANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_FAILED:
Packit ae235b
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
Packit ae235b
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
Packit ae235b
        g_clear_error (&error);
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_UNHANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Signal the end of the test */
Packit ae235b
  g_main_loop_quit (test->loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_ask_password_async (Test            *test,
Packit ae235b
                         gconstpointer    unused)
Packit ae235b
{
Packit ae235b
  /* This test only works with a main loop */
Packit ae235b
  g_assert (test->loop);
Packit ae235b
Packit ae235b
  g_tls_interaction_ask_password_async (test->interaction,
Packit ae235b
                                        test->password, NULL,
Packit ae235b
                                        on_ask_password_async_call,
Packit ae235b
                                        test);
Packit ae235b
Packit ae235b
  /* teardown waits until g_main_loop_quit(). called from callback */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_invoke_ask_password (Test         *test,
Packit ae235b
                          gconstpointer unused)
Packit ae235b
{
Packit ae235b
  GTlsInteractionResult res;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  res = g_tls_interaction_invoke_ask_password (test->interaction, test->password,
Packit ae235b
                                               NULL, &error);
Packit ae235b
Packit ae235b
  /* Check that the results match the fixture */
Packit ae235b
  g_assert_cmpuint (test->fixture->result, ==, res);
Packit ae235b
  switch (test->fixture->result)
Packit ae235b
    {
Packit ae235b
      case G_TLS_INTERACTION_HANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_FAILED:
Packit ae235b
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
Packit ae235b
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
Packit ae235b
        g_clear_error (&error);
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_UNHANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* This allows teardown to stop if running with loop */
Packit ae235b
  if (test->loop)
Packit ae235b
    g_main_loop_quit (test->loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_ask_password (Test         *test,
Packit ae235b
                   gconstpointer unused)
Packit ae235b
{
Packit ae235b
  GTlsInteractionResult res;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  res = g_tls_interaction_ask_password (test->interaction, test->password,
Packit ae235b
                                        NULL, &error);
Packit ae235b
Packit ae235b
  /* Check that the results match the fixture */
Packit ae235b
  g_assert_cmpuint (test->fixture->result, ==, res);
Packit ae235b
  switch (test->fixture->result)
Packit ae235b
    {
Packit ae235b
      case G_TLS_INTERACTION_HANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_FAILED:
Packit ae235b
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
Packit ae235b
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
Packit ae235b
        g_clear_error (&error);
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_UNHANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* This allows teardown to stop if running with loop */
Packit ae235b
  if (test->loop)
Packit ae235b
    g_main_loop_quit (test->loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_request_certificate_async_call (GObject      *source,
Packit ae235b
                                   GAsyncResult *result,
Packit ae235b
                                   gpointer      user_data)
Packit ae235b
{
Packit ae235b
  Test *test = user_data;
Packit ae235b
  GTlsInteractionResult res;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_assert (G_IS_TLS_INTERACTION (source));
Packit ae235b
  g_assert (G_TLS_INTERACTION (source) == test->interaction);
Packit ae235b
Packit ae235b
  /* Check that this callback is being run in the right place */
Packit ae235b
  g_assert (g_thread_self () == test->interaction_thread);
Packit ae235b
Packit ae235b
  res = g_tls_interaction_request_certificate_finish (test->interaction, result, &error);
Packit ae235b
Packit ae235b
  /* Check that the results match the fixture */
Packit ae235b
  g_assert_cmpuint (test->fixture->result, ==, res);
Packit ae235b
  switch (test->fixture->result)
Packit ae235b
    {
Packit ae235b
      case G_TLS_INTERACTION_HANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_FAILED:
Packit ae235b
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
Packit ae235b
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
Packit ae235b
        g_clear_error (&error);
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_UNHANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Signal the end of the test */
Packit ae235b
  g_main_loop_quit (test->loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_request_certificate_async (Test            *test,
Packit ae235b
                                gconstpointer    unused)
Packit ae235b
{
Packit ae235b
  /* This test only works with a main loop */
Packit ae235b
  g_assert (test->loop);
Packit ae235b
Packit ae235b
  g_tls_interaction_request_certificate_async (test->interaction,
Packit ae235b
                                               test->connection, 0, NULL,
Packit ae235b
                                               on_request_certificate_async_call,
Packit ae235b
                                               test);
Packit ae235b
Packit ae235b
  /* teardown waits until g_main_loop_quit(). called from callback */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_invoke_request_certificate (Test         *test,
Packit ae235b
                                 gconstpointer unused)
Packit ae235b
{
Packit ae235b
  GTlsInteractionResult res;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  res = g_tls_interaction_invoke_request_certificate (test->interaction,
Packit ae235b
                                                      test->connection,
Packit ae235b
                                                      0, NULL, &error);
Packit ae235b
Packit ae235b
  /* Check that the results match the fixture */
Packit ae235b
  g_assert_cmpuint (test->fixture->result, ==, res);
Packit ae235b
  switch (test->fixture->result)
Packit ae235b
    {
Packit ae235b
      case G_TLS_INTERACTION_HANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_FAILED:
Packit ae235b
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
Packit ae235b
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
Packit ae235b
        g_clear_error (&error);
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_UNHANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* This allows teardown to stop if running with loop */
Packit ae235b
  if (test->loop)
Packit ae235b
    g_main_loop_quit (test->loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_request_certificate (Test         *test,
Packit ae235b
                          gconstpointer unused)
Packit ae235b
{
Packit ae235b
  GTlsInteractionResult res;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  res = g_tls_interaction_request_certificate (test->interaction, test->connection,
Packit ae235b
                                               0, NULL, &error);
Packit ae235b
Packit ae235b
  /* Check that the results match the fixture */
Packit ae235b
  g_assert_cmpuint (test->fixture->result, ==, res);
Packit ae235b
  switch (test->fixture->result)
Packit ae235b
    {
Packit ae235b
      case G_TLS_INTERACTION_HANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_FAILED:
Packit ae235b
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
Packit ae235b
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
Packit ae235b
        g_clear_error (&error);
Packit ae235b
        break;
Packit ae235b
      case G_TLS_INTERACTION_UNHANDLED:
Packit ae235b
        g_assert_no_error (error);
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* This allows teardown to stop if running with loop */
Packit ae235b
  if (test->loop)
Packit ae235b
    g_main_loop_quit (test->loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ----------------------------------------------------------------------------
Packit ae235b
 * TEST SETUP
Packit ae235b
 */
Packit ae235b
Packit ae235b
static void
Packit ae235b
setup_without_loop (Test           *test,
Packit ae235b
                    gconstpointer   user_data)
Packit ae235b
{
Packit ae235b
  const Fixture *fixture = user_data;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
  GTlsBackend *backend;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  test->fixture = fixture;
Packit ae235b
Packit ae235b
  test->interaction = g_object_new (TEST_TYPE_INTERACTION, NULL);
Packit ae235b
  g_assert (TEST_IS_INTERACTION (test->interaction));
Packit ae235b
Packit ae235b
  TEST_INTERACTION (test->interaction)->test = test;
Packit ae235b
Packit ae235b
  klass =  G_TLS_INTERACTION_GET_CLASS (test->interaction);
Packit ae235b
  klass->ask_password = fixture->ask_password_func;
Packit ae235b
  klass->ask_password_async = fixture->ask_password_async_func;
Packit ae235b
  klass->ask_password_finish = fixture->ask_password_finish_func;
Packit ae235b
  klass->request_certificate = fixture->request_certificate_func;
Packit ae235b
  klass->request_certificate_async = fixture->request_certificate_async_func;
Packit ae235b
  klass->request_certificate_finish = fixture->request_certificate_finish_func;
Packit ae235b
Packit ae235b
  backend = g_object_new (G_TYPE_TEST_TLS_BACKEND, NULL);
Packit ae235b
  test->connection = g_object_new (g_tls_backend_get_server_connection_type (backend), NULL);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_object_unref (backend);
Packit ae235b
Packit ae235b
  test->password = g_tls_password_new (0, "Description");
Packit ae235b
  test->test_thread = g_thread_self ();
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * If no loop is running then interaction should happen in the same
Packit ae235b
   * thread that the tests are running in.
Packit ae235b
   */
Packit ae235b
  test->interaction_thread = test->test_thread;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
teardown_without_loop (Test            *test,
Packit ae235b
                       gconstpointer    unused)
Packit ae235b
{
Packit ae235b
  gpointer weak_pointer = test->interaction;
Packit ae235b
Packit ae235b
  g_object_add_weak_pointer (weak_pointer, &weak_pointer);
Packit ae235b
Packit ae235b
  g_object_unref (test->connection);
Packit ae235b
Packit ae235b
  g_object_unref (test->password);
Packit ae235b
Packit ae235b
  g_object_unref (test->interaction);
Packit ae235b
Packit ae235b
  g_assert (weak_pointer == NULL);
Packit ae235b
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GMutex loop_mutex;
Packit ae235b
  GCond loop_started;
Packit ae235b
  gboolean started;
Packit ae235b
  Test *test;
Packit ae235b
} ThreadLoop;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
thread_loop (gpointer user_data)
Packit ae235b
{
Packit ae235b
  GMainContext *context = g_main_context_default ();
Packit ae235b
  ThreadLoop *closure = user_data;
Packit ae235b
  Test *test = closure->test;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure->loop_mutex);
Packit ae235b
Packit ae235b
  g_assert (test->loop_thread == g_thread_self ());
Packit ae235b
  g_assert (test->loop == NULL);
Packit ae235b
  test->loop = g_main_loop_new (context, TRUE);
Packit ae235b
Packit ae235b
  g_main_context_acquire (context);
Packit ae235b
  closure->started = TRUE;
Packit ae235b
  g_cond_signal (&closure->loop_started);
Packit ae235b
  g_mutex_unlock (&closure->loop_mutex);
Packit ae235b
Packit ae235b
  while (g_main_loop_is_running (test->loop))
Packit ae235b
    g_main_context_iteration (context, TRUE);
Packit ae235b
Packit ae235b
  g_main_context_release (context);
Packit ae235b
  return test;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
setup_with_thread_loop (Test            *test,
Packit ae235b
                        gconstpointer    user_data)
Packit ae235b
{
Packit ae235b
  ThreadLoop closure;
Packit ae235b
Packit ae235b
  setup_without_loop (test, user_data);
Packit ae235b
Packit ae235b
  g_mutex_init (&closure.loop_mutex);
Packit ae235b
  g_cond_init (&closure.loop_started);
Packit ae235b
  closure.started = FALSE;
Packit ae235b
  closure.test = test;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure.loop_mutex);
Packit ae235b
  test->loop_thread = g_thread_new ("loop", thread_loop, &closure);
Packit ae235b
  while (!closure.started)
Packit ae235b
    g_cond_wait (&closure.loop_started, &closure.loop_mutex);
Packit ae235b
  g_mutex_unlock (&closure.loop_mutex);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * When a loop is running then interaction should always occur in the main
Packit ae235b
   * context of that loop.
Packit ae235b
   */
Packit ae235b
  test->interaction_thread = test->loop_thread;
Packit ae235b
Packit ae235b
  g_mutex_clear (&closure.loop_mutex);
Packit ae235b
  g_cond_clear (&closure.loop_started);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
teardown_with_thread_loop (Test            *test,
Packit ae235b
                           gconstpointer    unused)
Packit ae235b
{
Packit ae235b
  gpointer check;
Packit ae235b
Packit ae235b
  g_assert (test->loop_thread);
Packit ae235b
  check = g_thread_join (test->loop_thread);
Packit ae235b
  g_assert (check == test);
Packit ae235b
  test->loop_thread = NULL;
Packit ae235b
Packit ae235b
  g_main_loop_unref (test->loop);
Packit ae235b
Packit ae235b
  teardown_without_loop (test, unused);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
setup_with_normal_loop (Test            *test,
Packit ae235b
                        gconstpointer    user_data)
Packit ae235b
{
Packit ae235b
  GMainContext *context;
Packit ae235b
Packit ae235b
  setup_without_loop (test, user_data);
Packit ae235b
Packit ae235b
  context = g_main_context_default ();
Packit ae235b
  if (!g_main_context_acquire (context))
Packit ae235b
    g_assert_not_reached ();
Packit ae235b
Packit ae235b
  test->loop = g_main_loop_new (context, TRUE);
Packit ae235b
  g_assert (g_main_loop_is_running (test->loop));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
teardown_with_normal_loop (Test            *test,
Packit ae235b
                           gconstpointer    unused)
Packit ae235b
{
Packit ae235b
  GMainContext *context;
Packit ae235b
Packit ae235b
  context = g_main_context_default ();
Packit ae235b
  while (g_main_loop_is_running (test->loop))
Packit ae235b
    g_main_context_iteration (context, TRUE);
Packit ae235b
Packit ae235b
  g_main_context_release (context);
Packit ae235b
Packit ae235b
  /* Run test until complete */
Packit ae235b
  g_main_loop_unref (test->loop);
Packit ae235b
  test->loop = NULL;
Packit ae235b
Packit ae235b
  teardown_without_loop (test, unused);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef void (*TestFunc) (Test *test, gconstpointer data);
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_with_async_ask_password (const gchar *name,
Packit ae235b
                              TestFunc     setup,
Packit ae235b
                              TestFunc     func,
Packit ae235b
                              TestFunc     teardown)
Packit ae235b
{
Packit ae235b
  gchar *test_name;
Packit ae235b
  Fixture *fixture;
Packit ae235b
Packit ae235b
  /* Async implementation that succeeds */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->ask_password_async_func = test_interaction_ask_password_async_success;
Packit ae235b
  fixture->ask_password_finish_func = test_interaction_ask_password_finish_success;
Packit ae235b
  fixture->ask_password_func = NULL;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_HANDLED;
Packit ae235b
  test_name = g_strdup_printf ("%s/async-implementation-success", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
Packit ae235b
  /* Async implementation that fails */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->ask_password_async_func = test_interaction_ask_password_async_failure;
Packit ae235b
  fixture->ask_password_finish_func = test_interaction_ask_password_finish_failure;
Packit ae235b
  fixture->ask_password_func = NULL;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_FAILED;
Packit ae235b
  fixture->error_domain = G_FILE_ERROR;
Packit ae235b
  fixture->error_code = G_FILE_ERROR_ACCES;
Packit ae235b
  fixture->error_message = "The message";
Packit ae235b
  test_name = g_strdup_printf ("%s/async-implementation-failure", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_with_unhandled_ask_password (const gchar *name,
Packit ae235b
                                  TestFunc     setup,
Packit ae235b
                                  TestFunc     func,
Packit ae235b
                                  TestFunc     teardown)
Packit ae235b
{
Packit ae235b
  gchar *test_name;
Packit ae235b
  Fixture *fixture;
Packit ae235b
Packit ae235b
  /* Unhandled implementation */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->ask_password_async_func = NULL;
Packit ae235b
  fixture->ask_password_finish_func = NULL;
Packit ae235b
  fixture->ask_password_func = NULL;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_UNHANDLED;
Packit ae235b
  test_name = g_strdup_printf ("%s/unhandled-implementation", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_with_sync_ask_password (const gchar *name,
Packit ae235b
                                             TestFunc     setup,
Packit ae235b
                                             TestFunc     func,
Packit ae235b
                                             TestFunc     teardown)
Packit ae235b
{
Packit ae235b
  gchar *test_name;
Packit ae235b
  Fixture *fixture;
Packit ae235b
Packit ae235b
  /* Sync implementation that succeeds */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->ask_password_async_func = NULL;
Packit ae235b
  fixture->ask_password_finish_func = NULL;
Packit ae235b
  fixture->ask_password_func = test_interaction_ask_password_sync_success;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_HANDLED;
Packit ae235b
  test_name = g_strdup_printf ("%s/sync-implementation-success", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
Packit ae235b
  /* Async implementation that fails */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->ask_password_async_func = NULL;
Packit ae235b
  fixture->ask_password_finish_func = NULL;
Packit ae235b
  fixture->ask_password_func = test_interaction_ask_password_sync_failure;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_FAILED;
Packit ae235b
  fixture->error_domain = G_FILE_ERROR;
Packit ae235b
  fixture->error_code = G_FILE_ERROR_ACCES;
Packit ae235b
  fixture->error_message = "The message";
Packit ae235b
  test_name = g_strdup_printf ("%s/sync-implementation-failure", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_with_all_ask_password (const gchar *name,
Packit ae235b
                            TestFunc setup,
Packit ae235b
                            TestFunc func,
Packit ae235b
                            TestFunc teardown)
Packit ae235b
{
Packit ae235b
  test_with_unhandled_ask_password (name, setup, func, teardown);
Packit ae235b
  test_with_async_ask_password (name, setup, func, teardown);
Packit ae235b
  test_with_sync_ask_password (name, setup, func, teardown);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_with_async_request_certificate (const gchar *name,
Packit ae235b
                                     TestFunc     setup,
Packit ae235b
                                     TestFunc     func,
Packit ae235b
                                     TestFunc     teardown)
Packit ae235b
{
Packit ae235b
  gchar *test_name;
Packit ae235b
  Fixture *fixture;
Packit ae235b
Packit ae235b
  /* Async implementation that succeeds */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->request_certificate_async_func = test_interaction_request_certificate_async_success;
Packit ae235b
  fixture->request_certificate_finish_func = test_interaction_request_certificate_finish_success;
Packit ae235b
  fixture->request_certificate_func = NULL;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_HANDLED;
Packit ae235b
  test_name = g_strdup_printf ("%s/async-implementation-success", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
Packit ae235b
  /* Async implementation that fails */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->request_certificate_async_func = test_interaction_request_certificate_async_failure;
Packit ae235b
  fixture->request_certificate_finish_func = test_interaction_request_certificate_finish_failure;
Packit ae235b
  fixture->request_certificate_func = NULL;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_FAILED;
Packit ae235b
  fixture->error_domain = G_FILE_ERROR;
Packit ae235b
  fixture->error_code = G_FILE_ERROR_NOENT;
Packit ae235b
  fixture->error_message = "Another message";
Packit ae235b
  test_name = g_strdup_printf ("%s/async-implementation-failure", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_with_unhandled_request_certificate (const gchar *name,
Packit ae235b
                                         TestFunc     setup,
Packit ae235b
                                         TestFunc     func,
Packit ae235b
                                         TestFunc     teardown)
Packit ae235b
{
Packit ae235b
  gchar *test_name;
Packit ae235b
  Fixture *fixture;
Packit ae235b
Packit ae235b
  /* Unhandled implementation */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->request_certificate_async_func = NULL;
Packit ae235b
  fixture->request_certificate_finish_func = NULL;
Packit ae235b
  fixture->request_certificate_func = NULL;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_UNHANDLED;
Packit ae235b
  test_name = g_strdup_printf ("%s/unhandled-implementation", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_with_sync_request_certificate (const gchar *name,
Packit ae235b
                                    TestFunc     setup,
Packit ae235b
                                    TestFunc     func,
Packit ae235b
                                    TestFunc     teardown)
Packit ae235b
{
Packit ae235b
  gchar *test_name;
Packit ae235b
  Fixture *fixture;
Packit ae235b
Packit ae235b
  /* Sync implementation that succeeds */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->request_certificate_async_func = NULL;
Packit ae235b
  fixture->request_certificate_finish_func = NULL;
Packit ae235b
  fixture->request_certificate_func = test_interaction_request_certificate_sync_success;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_HANDLED;
Packit ae235b
  test_name = g_strdup_printf ("%s/sync-implementation-success", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
Packit ae235b
  /* Async implementation that fails */
Packit ae235b
  fixture = g_new0 (Fixture, 1);
Packit ae235b
  fixture->request_certificate_async_func = NULL;
Packit ae235b
  fixture->request_certificate_finish_func = NULL;
Packit ae235b
  fixture->request_certificate_func = test_interaction_request_certificate_sync_failure;
Packit ae235b
  fixture->result = G_TLS_INTERACTION_FAILED;
Packit ae235b
  fixture->error_domain = G_FILE_ERROR;
Packit ae235b
  fixture->error_code = G_FILE_ERROR_NOENT;
Packit ae235b
  fixture->error_message = "Another message";
Packit ae235b
  test_name = g_strdup_printf ("%s/sync-implementation-failure", name);
Packit ae235b
  g_test_add (test_name, Test, fixture, setup, func, teardown);
Packit ae235b
  g_free (test_name);
Packit ae235b
  g_ptr_array_add (fixtures, fixture);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_with_all_request_certificate (const gchar *name,
Packit ae235b
                                   TestFunc setup,
Packit ae235b
                                   TestFunc func,
Packit ae235b
                                   TestFunc teardown)
Packit ae235b
{
Packit ae235b
  test_with_unhandled_request_certificate (name, setup, func, teardown);
Packit ae235b
  test_with_async_request_certificate (name, setup, func, teardown);
Packit ae235b
  test_with_sync_request_certificate (name, setup, func, teardown);
Packit ae235b
}
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  gint ret;
Packit ae235b
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  fixtures = g_ptr_array_new_with_free_func (g_free);
Packit ae235b
Packit ae235b
  /* Tests for g_tls_interaction_invoke_ask_password */
Packit ae235b
  test_with_all_ask_password ("/tls-interaction/ask-password/invoke-with-loop",
Packit ae235b
                              setup_with_thread_loop, test_invoke_ask_password, teardown_with_thread_loop);
Packit ae235b
  test_with_all_ask_password ("/tls-interaction/ask-password/invoke-without-loop",
Packit ae235b
                              setup_without_loop, test_invoke_ask_password, teardown_without_loop);
Packit ae235b
  test_with_all_ask_password ("/tls-interaction/ask-password/invoke-in-loop",
Packit ae235b
                                              setup_with_normal_loop, test_invoke_ask_password, teardown_with_normal_loop);
Packit ae235b
Packit ae235b
  /* Tests for g_tls_interaction_ask_password */
Packit ae235b
  test_with_unhandled_ask_password ("/tls-interaction/ask-password/sync",
Packit ae235b
                                    setup_without_loop, test_ask_password, teardown_without_loop);
Packit ae235b
  test_with_sync_ask_password ("/tls-interaction/ask-password/sync",
Packit ae235b
                               setup_without_loop, test_ask_password, teardown_without_loop);
Packit ae235b
Packit ae235b
  /* Tests for g_tls_interaction_ask_password_async */
Packit ae235b
  test_with_unhandled_ask_password ("/tls-interaction/ask-password/async",
Packit ae235b
                                    setup_with_normal_loop, test_ask_password_async, teardown_with_normal_loop);
Packit ae235b
  test_with_async_ask_password ("/tls-interaction/ask-password/async",
Packit ae235b
                                setup_with_normal_loop, test_ask_password_async, teardown_with_normal_loop);
Packit ae235b
Packit ae235b
  /* Tests for g_tls_interaction_invoke_request_certificate */
Packit ae235b
  test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-with-loop",
Packit ae235b
                                     setup_with_thread_loop, test_invoke_request_certificate, teardown_with_thread_loop);
Packit ae235b
  test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-without-loop",
Packit ae235b
                                     setup_without_loop, test_invoke_request_certificate, teardown_without_loop);
Packit ae235b
  test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-in-loop",
Packit ae235b
                              setup_with_normal_loop, test_invoke_request_certificate, teardown_with_normal_loop);
Packit ae235b
Packit ae235b
  /* Tests for g_tls_interaction_ask_password */
Packit ae235b
  test_with_unhandled_request_certificate ("/tls-interaction/request-certificate/sync",
Packit ae235b
                                           setup_without_loop, test_request_certificate, teardown_without_loop);
Packit ae235b
  test_with_sync_request_certificate ("/tls-interaction/request-certificate/sync",
Packit ae235b
                                      setup_without_loop, test_request_certificate, teardown_without_loop);
Packit ae235b
Packit ae235b
  /* Tests for g_tls_interaction_ask_password_async */
Packit ae235b
  test_with_unhandled_request_certificate ("/tls-interaction/request-certificate/async",
Packit ae235b
                                           setup_with_normal_loop, test_request_certificate_async, teardown_with_normal_loop);
Packit ae235b
  test_with_async_request_certificate ("/tls-interaction/request-certificate/async",
Packit ae235b
                                       setup_with_normal_loop, test_request_certificate_async, teardown_with_normal_loop);
Packit ae235b
Packit ae235b
  ret = g_test_run();
Packit ae235b
  g_ptr_array_free (fixtures, TRUE);
Packit ae235b
  return ret;
Packit ae235b
}