tls-interaction.c 36.5 KB
Newer Older
1 2 3 4 5 6 7
/* GLib testing framework examples and tests
 *
 * Copyright (C) 2011 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24
 *
 * Author: Stef Walter <stefw@collobora.co.uk>
 */

#include "config.h"

#include <gio/gio.h>

25 26 27 28
#include "gtesttlsbackend.h"

static GPtrArray *fixtures = NULL;

29 30 31 32 33
typedef struct {
  /* Class virtual interaction methods */
  gpointer ask_password_func;
  gpointer ask_password_async_func;
  gpointer ask_password_finish_func;
34 35 36
  gpointer request_certificate_func;
  gpointer request_certificate_async_func;
  gpointer request_certificate_finish_func;
37 38 39 40 41 42 43 44 45 46 47

  /* Expected results */
  GTlsInteractionResult result;
  GQuark error_domain;
  gint error_code;
  const gchar *error_message;
} Fixture;

typedef struct {
  GTlsInteraction *interaction;
  GTlsPassword *password;
48
  GTlsConnection *connection;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  GMainLoop *loop;
  GThread *interaction_thread;
  GThread *test_thread;
  GThread *loop_thread;
  const Fixture *fixture;
} Test;

typedef struct {
  GTlsInteraction parent;
  Test *test;
} TestInteraction;

typedef struct {
  GTlsInteractionClass parent;
} TestInteractionClass;

65
static GType test_interaction_get_type (void);
66
G_DEFINE_TYPE (TestInteraction, test_interaction, G_TYPE_TLS_INTERACTION)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

#define TEST_TYPE_INTERACTION         (test_interaction_get_type ())
#define TEST_INTERACTION(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), TEST_TYPE_INTERACTION, TestInteraction))
#define TEST_IS_INTERACTION(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), TEST_TYPE_INTERACTION))

static void
test_interaction_init (TestInteraction *self)
{

}

static void
test_interaction_class_init (TestInteractionClass *klass)
{
  /* By default no virtual methods */
}

static void
test_interaction_ask_password_async_success (GTlsInteraction    *interaction,
                                             GTlsPassword       *password,
                                             GCancellable       *cancellable,
                                             GAsyncReadyCallback callback,
                                             gpointer            user_data)
{
91
  GTask *task;
92 93 94 95 96 97 98 99 100 101
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (G_IS_TLS_PASSWORD (password));
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

102
  task = g_task_new (self, cancellable, callback, user_data);
103 104 105

  /* Don't do this in real life. Include a null terminator for testing */
  g_tls_password_set_value (password, (const guchar *)"the password", 13);
106 107
  g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
  g_object_unref (task);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
}


static GTlsInteractionResult
test_interaction_ask_password_finish_success (GTlsInteraction    *interaction,
                                              GAsyncResult       *result,
                                              GError            **error)
{
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

123
  g_assert (g_task_is_valid (result, interaction));
124 125 126
  g_assert (error != NULL);
  g_assert (*error == NULL);

127
  return g_task_propagate_int (G_TASK (result), error);
128 129 130 131 132 133 134 135 136
}

static void
test_interaction_ask_password_async_failure (GTlsInteraction    *interaction,
                                             GTlsPassword       *password,
                                             GCancellable       *cancellable,
                                             GAsyncReadyCallback callback,
                                             gpointer            user_data)
{
137
  GTask *task;
138 139 140 141 142 143 144 145 146 147
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (G_IS_TLS_PASSWORD (password));
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

148
  task = g_task_new (self, cancellable, callback, user_data);
149

150 151
  g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
  g_object_unref (task);
152 153 154 155 156 157 158 159 160 161 162 163 164 165
}

static GTlsInteractionResult
test_interaction_ask_password_finish_failure (GTlsInteraction    *interaction,
                                              GAsyncResult       *result,
                                              GError            **error)
{
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

166
  g_assert (g_task_is_valid (result, interaction));
167 168 169
  g_assert (error != NULL);
  g_assert (*error == NULL);

170
  if (g_task_propagate_int (G_TASK (result), error) != -1)
171
    g_assert_not_reached ();
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  return G_TLS_INTERACTION_FAILED;
}


static GTlsInteractionResult
test_interaction_ask_password_sync_success (GTlsInteraction    *interaction,
                                            GTlsPassword       *password,
                                            GCancellable       *cancellable,
                                            GError            **error)
{
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (G_IS_TLS_PASSWORD (password));
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_assert (error != NULL);
  g_assert (*error == NULL);

  /* Don't do this in real life. Include a null terminator for testing */
  g_tls_password_set_value (password, (const guchar *)"the password", 13);
  return G_TLS_INTERACTION_HANDLED;
}

static GTlsInteractionResult
test_interaction_ask_password_sync_failure (GTlsInteraction    *interaction,
                                            GTlsPassword       *password,
                                            GCancellable       *cancellable,
                                            GError            **error)
{
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (G_IS_TLS_PASSWORD (password));
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_assert (error != NULL);
  g_assert (*error == NULL);

  g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
  return G_TLS_INTERACTION_FAILED;
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
static void
test_interaction_request_certificate_async_success (GTlsInteraction    *interaction,
                                                    GTlsConnection     *connection,
                                                    gint                unused_flags,
                                                    GCancellable       *cancellable,
                                                    GAsyncReadyCallback callback,
                                                    gpointer            user_data)
{
  GTask *task;
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (G_IS_TLS_CONNECTION (connection));
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_assert (unused_flags == 0);

  task = g_task_new (self, cancellable, callback, user_data);

  /*
   * IRL would call g_tls_connection_set_certificate(). But here just touch
   * the connection in a detectable way.
   */
  g_object_set_data (G_OBJECT (connection), "chosen-certificate", "my-certificate");
  g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
  g_object_unref (task);
}

static GTlsInteractionResult
test_interaction_request_certificate_finish_success (GTlsInteraction    *interaction,
                                                     GAsyncResult       *result,
                                                     GError            **error)
{
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (g_task_is_valid (result, interaction));
  g_assert (error != NULL);
  g_assert (*error == NULL);

  return g_task_propagate_int (G_TASK (result), error);
}

static void
test_interaction_request_certificate_async_failure (GTlsInteraction    *interaction,
                                                    GTlsConnection     *connection,
                                                    gint                unused_flags,
                                                    GCancellable       *cancellable,
                                                    GAsyncReadyCallback callback,
                                                    gpointer            user_data)
{
  GTask *task;
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (G_IS_TLS_CONNECTION (connection));
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_assert (unused_flags == 0);

  task = g_task_new (self, cancellable, callback, user_data);

  g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_NOENT, "Another message");
  g_object_unref (task);
}

static GTlsInteractionResult
test_interaction_request_certificate_finish_failure (GTlsInteraction    *interaction,
                                                     GAsyncResult       *result,
                                                     GError            **error)
{
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (g_task_is_valid (result, interaction));
  g_assert (error != NULL);
  g_assert (*error == NULL);

  if (g_task_propagate_int (G_TASK (result), error) != -1)
    g_assert_not_reached ();

  return G_TLS_INTERACTION_FAILED;
}

static GTlsInteractionResult
test_interaction_request_certificate_sync_success (GTlsInteraction    *interaction,
                                                   GTlsConnection      *connection,
                                                   gint                 unused_flags,
                                                   GCancellable        *cancellable,
                                                   GError             **error)
{
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (G_IS_TLS_CONNECTION (connection));
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_assert (error != NULL);
  g_assert (*error == NULL);

  /*
   * IRL would call g_tls_connection_set_certificate(). But here just touch
   * the connection in a detectable way.
   */
  g_object_set_data (G_OBJECT (connection), "chosen-certificate", "my-certificate");
  return G_TLS_INTERACTION_HANDLED;
}

static GTlsInteractionResult
test_interaction_request_certificate_sync_failure (GTlsInteraction    *interaction,
                                                   GTlsConnection     *connection,
                                                   gint                unused_flags,
                                                   GCancellable       *cancellable,
                                                   GError            **error)
{
  TestInteraction *self;

  g_assert (TEST_IS_INTERACTION (interaction));
  self = TEST_INTERACTION (interaction);

  g_assert (g_thread_self () == self->test->interaction_thread);

  g_assert (G_IS_TLS_CONNECTION (connection));
  g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_assert (unused_flags == 0);
  g_assert (error != NULL);
  g_assert (*error == NULL);

  g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT, "Another message");
  return G_TLS_INTERACTION_FAILED;
}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
/* ----------------------------------------------------------------------------
 * ACTUAL TESTS
 */

static void
on_ask_password_async_call (GObject      *source,
                            GAsyncResult *result,
                            gpointer      user_data)
{
  Test *test = user_data;
  GTlsInteractionResult res;
  GError *error = NULL;

  g_assert (G_IS_TLS_INTERACTION (source));
  g_assert (G_TLS_INTERACTION (source) == test->interaction);

  /* Check that this callback is being run in the right place */
  g_assert (g_thread_self () == test->interaction_thread);

  res = g_tls_interaction_ask_password_finish (test->interaction, result,
                                               &error);

  /* Check that the results match the fixture */
  g_assert_cmpuint (test->fixture->result, ==, res);
  switch (test->fixture->result)
    {
      case G_TLS_INTERACTION_HANDLED:
        g_assert_no_error (error);
        g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
        break;
      case G_TLS_INTERACTION_FAILED:
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
        g_clear_error (&error);
        break;
      case G_TLS_INTERACTION_UNHANDLED:
        g_assert_no_error (error);
        break;
      default:
        g_assert_not_reached ();
    }

  /* Signal the end of the test */
  g_main_loop_quit (test->loop);
}

static void
test_ask_password_async (Test            *test,
                         gconstpointer    unused)
{
  /* This test only works with a main loop */
  g_assert (test->loop);

  g_tls_interaction_ask_password_async (test->interaction,
                                        test->password, NULL,
                                        on_ask_password_async_call,
                                        test);

  /* teardown waits until g_main_loop_quit(). called from callback */
}

static void
test_invoke_ask_password (Test         *test,
                          gconstpointer unused)
{
  GTlsInteractionResult res;
  GError *error = NULL;

  res = g_tls_interaction_invoke_ask_password (test->interaction, test->password,
                                               NULL, &error);

  /* Check that the results match the fixture */
  g_assert_cmpuint (test->fixture->result, ==, res);
  switch (test->fixture->result)
    {
      case G_TLS_INTERACTION_HANDLED:
        g_assert_no_error (error);
        g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
        break;
      case G_TLS_INTERACTION_FAILED:
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
        g_clear_error (&error);
        break;
      case G_TLS_INTERACTION_UNHANDLED:
        g_assert_no_error (error);
        break;
      default:
        g_assert_not_reached ();
    }

  /* This allows teardown to stop if running with loop */
  if (test->loop)
    g_main_loop_quit (test->loop);
}

static void
test_ask_password (Test         *test,
                   gconstpointer unused)
{
  GTlsInteractionResult res;
  GError *error = NULL;

  res = g_tls_interaction_ask_password (test->interaction, test->password,
                                        NULL, &error);

  /* Check that the results match the fixture */
  g_assert_cmpuint (test->fixture->result, ==, res);
  switch (test->fixture->result)
    {
      case G_TLS_INTERACTION_HANDLED:
        g_assert_no_error (error);
        g_assert_cmpstr ((const gchar *)g_tls_password_get_value (test->password, NULL), ==, "the password");
        break;
      case G_TLS_INTERACTION_FAILED:
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
        g_clear_error (&error);
        break;
      case G_TLS_INTERACTION_UNHANDLED:
        g_assert_no_error (error);
        break;
      default:
        g_assert_not_reached ();
    }

  /* This allows teardown to stop if running with loop */
  if (test->loop)
    g_main_loop_quit (test->loop);
}

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
static void
on_request_certificate_async_call (GObject      *source,
                                   GAsyncResult *result,
                                   gpointer      user_data)
{
  Test *test = user_data;
  GTlsInteractionResult res;
  GError *error = NULL;

  g_assert (G_IS_TLS_INTERACTION (source));
  g_assert (G_TLS_INTERACTION (source) == test->interaction);

  /* Check that this callback is being run in the right place */
  g_assert (g_thread_self () == test->interaction_thread);

  res = g_tls_interaction_request_certificate_finish (test->interaction, result, &error);

  /* Check that the results match the fixture */
  g_assert_cmpuint (test->fixture->result, ==, res);
  switch (test->fixture->result)
    {
      case G_TLS_INTERACTION_HANDLED:
        g_assert_no_error (error);
        g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
        break;
      case G_TLS_INTERACTION_FAILED:
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
        g_clear_error (&error);
        break;
      case G_TLS_INTERACTION_UNHANDLED:
        g_assert_no_error (error);
        break;
      default:
        g_assert_not_reached ();
    }

  /* Signal the end of the test */
  g_main_loop_quit (test->loop);
}

static void
test_request_certificate_async (Test            *test,
                                gconstpointer    unused)
{
  /* This test only works with a main loop */
  g_assert (test->loop);

  g_tls_interaction_request_certificate_async (test->interaction,
                                               test->connection, 0, NULL,
                                               on_request_certificate_async_call,
                                               test);

  /* teardown waits until g_main_loop_quit(). called from callback */
}

static void
test_invoke_request_certificate (Test         *test,
                                 gconstpointer unused)
{
  GTlsInteractionResult res;
  GError *error = NULL;

  res = g_tls_interaction_invoke_request_certificate (test->interaction,
                                                      test->connection,
                                                      0, NULL, &error);

  /* Check that the results match the fixture */
  g_assert_cmpuint (test->fixture->result, ==, res);
  switch (test->fixture->result)
    {
      case G_TLS_INTERACTION_HANDLED:
        g_assert_no_error (error);
        g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
        break;
      case G_TLS_INTERACTION_FAILED:
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
        g_clear_error (&error);
        break;
      case G_TLS_INTERACTION_UNHANDLED:
        g_assert_no_error (error);
        break;
      default:
        g_assert_not_reached ();
    }

  /* This allows teardown to stop if running with loop */
  if (test->loop)
    g_main_loop_quit (test->loop);
}

static void
test_request_certificate (Test         *test,
                          gconstpointer unused)
{
  GTlsInteractionResult res;
  GError *error = NULL;

  res = g_tls_interaction_request_certificate (test->interaction, test->connection,
                                               0, NULL, &error);

  /* Check that the results match the fixture */
  g_assert_cmpuint (test->fixture->result, ==, res);
  switch (test->fixture->result)
    {
      case G_TLS_INTERACTION_HANDLED:
        g_assert_no_error (error);
        g_assert_cmpstr (g_object_get_data (G_OBJECT (test->connection), "chosen-certificate"), ==, "my-certificate");
        break;
      case G_TLS_INTERACTION_FAILED:
        g_assert_error (error, test->fixture->error_domain, test->fixture->error_code);
        g_assert_cmpstr (error->message, ==, test->fixture->error_message);
        g_clear_error (&error);
        break;
      case G_TLS_INTERACTION_UNHANDLED:
        g_assert_no_error (error);
        break;
      default:
        g_assert_not_reached ();
    }

  /* This allows teardown to stop if running with loop */
  if (test->loop)
    g_main_loop_quit (test->loop);
}

629 630 631 632 633 634 635 636 637 638
/* ----------------------------------------------------------------------------
 * TEST SETUP
 */

static void
setup_without_loop (Test           *test,
                    gconstpointer   user_data)
{
  const Fixture *fixture = user_data;
  GTlsInteractionClass *klass;
639 640 641
  GTlsBackend *backend;
  GError *error = NULL;

642 643 644 645 646 647 648 649 650 651 652
  test->fixture = fixture;

  test->interaction = g_object_new (TEST_TYPE_INTERACTION, NULL);
  g_assert (TEST_IS_INTERACTION (test->interaction));

  TEST_INTERACTION (test->interaction)->test = test;

  klass =  G_TLS_INTERACTION_GET_CLASS (test->interaction);
  klass->ask_password = fixture->ask_password_func;
  klass->ask_password_async = fixture->ask_password_async_func;
  klass->ask_password_finish = fixture->ask_password_finish_func;
653 654 655 656 657 658 659 660
  klass->request_certificate = fixture->request_certificate_func;
  klass->request_certificate_async = fixture->request_certificate_async_func;
  klass->request_certificate_finish = fixture->request_certificate_finish_func;

  backend = g_object_new (G_TYPE_TEST_TLS_BACKEND, NULL);
  test->connection = g_object_new (g_tls_backend_get_server_connection_type (backend), NULL);
  g_assert_no_error (error);
  g_object_unref (backend);
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

  test->password = g_tls_password_new (0, "Description");
  test->test_thread = g_thread_self ();

  /*
   * If no loop is running then interaction should happen in the same
   * thread that the tests are running in.
   */
  test->interaction_thread = test->test_thread;
}

static void
teardown_without_loop (Test            *test,
                       gconstpointer    unused)
{
676 677 678 679
  gpointer weak_pointer = test->interaction;

  g_object_add_weak_pointer (weak_pointer, &weak_pointer);

680 681
  g_object_unref (test->connection);

682 683 684
  g_object_unref (test->password);

  g_object_unref (test->interaction);
685 686

  g_assert (weak_pointer == NULL);
687 688 689 690

}

typedef struct {
691 692 693
  GMutex loop_mutex;
  GCond loop_started;
  gboolean started;
694 695 696 697 698 699 700 701 702 703
  Test *test;
} ThreadLoop;

static gpointer
thread_loop (gpointer user_data)
{
  GMainContext *context = g_main_context_default ();
  ThreadLoop *closure = user_data;
  Test *test = closure->test;

704
  g_mutex_lock (&closure->loop_mutex);
705 706 707 708 709

  g_assert (test->loop_thread == g_thread_self ());
  g_assert (test->loop == NULL);
  test->loop = g_main_loop_new (context, TRUE);

710
  g_main_context_acquire (context);
711 712 713
  closure->started = TRUE;
  g_cond_signal (&closure->loop_started);
  g_mutex_unlock (&closure->loop_mutex);
714 715 716 717

  while (g_main_loop_is_running (test->loop))
    g_main_context_iteration (context, TRUE);

718
  g_main_context_release (context);
719 720 721 722 723 724 725 726 727 728 729
  return test;
}

static void
setup_with_thread_loop (Test            *test,
                        gconstpointer    user_data)
{
  ThreadLoop closure;

  setup_without_loop (test, user_data);

730 731 732
  g_mutex_init (&closure.loop_mutex);
  g_cond_init (&closure.loop_started);
  closure.started = FALSE;
733 734
  closure.test = test;

735
  g_mutex_lock (&closure.loop_mutex);
736
  test->loop_thread = g_thread_new ("loop", thread_loop, &closure);
737 738 739
  while (!closure.started)
    g_cond_wait (&closure.loop_started, &closure.loop_mutex);
  g_mutex_unlock (&closure.loop_mutex);
740 741 742 743 744 745 746

  /*
   * When a loop is running then interaction should always occur in the main
   * context of that loop.
   */
  test->interaction_thread = test->loop_thread;

747 748
  g_mutex_clear (&closure.loop_mutex);
  g_cond_clear (&closure.loop_started);
749 750 751 752 753 754 755 756 757 758 759 760 761
}

static void
teardown_with_thread_loop (Test            *test,
                           gconstpointer    unused)
{
  gpointer check;

  g_assert (test->loop_thread);
  check = g_thread_join (test->loop_thread);
  g_assert (check == test);
  test->loop_thread = NULL;

762
  g_main_loop_unref (test->loop);
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

  teardown_without_loop (test, unused);
}

static void
setup_with_normal_loop (Test            *test,
                        gconstpointer    user_data)
{
  GMainContext *context;

  setup_without_loop (test, user_data);

  context = g_main_context_default ();
  if (!g_main_context_acquire (context))
    g_assert_not_reached ();

  test->loop = g_main_loop_new (context, TRUE);
  g_assert (g_main_loop_is_running (test->loop));
}

static void
teardown_with_normal_loop (Test            *test,
                           gconstpointer    unused)
{
  GMainContext *context;

  context = g_main_context_default ();
  while (g_main_loop_is_running (test->loop))
    g_main_context_iteration (context, TRUE);

  g_main_context_release (context);

  /* Run test until complete */
  g_main_loop_unref (test->loop);
  test->loop = NULL;

  teardown_without_loop (test, unused);
}

typedef void (*TestFunc) (Test *test, gconstpointer data);

static void
805 806 807 808
test_with_async_ask_password (const gchar *name,
                              TestFunc     setup,
                              TestFunc     func,
                              TestFunc     teardown)
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
{
  gchar *test_name;
  Fixture *fixture;

  /* Async implementation that succeeds */
  fixture = g_new0 (Fixture, 1);
  fixture->ask_password_async_func = test_interaction_ask_password_async_success;
  fixture->ask_password_finish_func = test_interaction_ask_password_finish_success;
  fixture->ask_password_func = NULL;
  fixture->result = G_TLS_INTERACTION_HANDLED;
  test_name = g_strdup_printf ("%s/async-implementation-success", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);

  /* Async implementation that fails */
  fixture = g_new0 (Fixture, 1);
  fixture->ask_password_async_func = test_interaction_ask_password_async_failure;
  fixture->ask_password_finish_func = test_interaction_ask_password_finish_failure;
  fixture->ask_password_func = NULL;
  fixture->result = G_TLS_INTERACTION_FAILED;
  fixture->error_domain = G_FILE_ERROR;
  fixture->error_code = G_FILE_ERROR_ACCES;
  fixture->error_message = "The message";
  test_name = g_strdup_printf ("%s/async-implementation-failure", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);
}
838

839
static void
840 841 842 843
test_with_unhandled_ask_password (const gchar *name,
                                  TestFunc     setup,
                                  TestFunc     func,
                                  TestFunc     teardown)
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
{
  gchar *test_name;
  Fixture *fixture;

  /* Unhandled implementation */
  fixture = g_new0 (Fixture, 1);
  fixture->ask_password_async_func = NULL;
  fixture->ask_password_finish_func = NULL;
  fixture->ask_password_func = NULL;
  fixture->result = G_TLS_INTERACTION_UNHANDLED;
  test_name = g_strdup_printf ("%s/unhandled-implementation", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);
}

static void
861
test_with_sync_ask_password (const gchar *name,
862 863
                                             TestFunc     setup,
                                             TestFunc     func,
864
                                             TestFunc     teardown)
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
{
  gchar *test_name;
  Fixture *fixture;

  /* Sync implementation that succeeds */
  fixture = g_new0 (Fixture, 1);
  fixture->ask_password_async_func = NULL;
  fixture->ask_password_finish_func = NULL;
  fixture->ask_password_func = test_interaction_ask_password_sync_success;
  fixture->result = G_TLS_INTERACTION_HANDLED;
  test_name = g_strdup_printf ("%s/sync-implementation-success", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);

  /* Async implementation that fails */
  fixture = g_new0 (Fixture, 1);
  fixture->ask_password_async_func = NULL;
  fixture->ask_password_finish_func = NULL;
  fixture->ask_password_func = test_interaction_ask_password_sync_failure;
  fixture->result = G_TLS_INTERACTION_FAILED;
  fixture->error_domain = G_FILE_ERROR;
  fixture->error_code = G_FILE_ERROR_ACCES;
  fixture->error_message = "The message";
  test_name = g_strdup_printf ("%s/sync-implementation-failure", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);
}

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
static void
test_with_all_ask_password (const gchar *name,
                            TestFunc setup,
                            TestFunc func,
                            TestFunc teardown)
{
  test_with_unhandled_ask_password (name, setup, func, teardown);
  test_with_async_ask_password (name, setup, func, teardown);
  test_with_sync_ask_password (name, setup, func, teardown);
}

static void
test_with_async_request_certificate (const gchar *name,
                                     TestFunc     setup,
                                     TestFunc     func,
                                     TestFunc     teardown)
{
  gchar *test_name;
  Fixture *fixture;

  /* Async implementation that succeeds */
  fixture = g_new0 (Fixture, 1);
  fixture->request_certificate_async_func = test_interaction_request_certificate_async_success;
  fixture->request_certificate_finish_func = test_interaction_request_certificate_finish_success;
  fixture->request_certificate_func = NULL;
  fixture->result = G_TLS_INTERACTION_HANDLED;
  test_name = g_strdup_printf ("%s/async-implementation-success", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);

  /* Async implementation that fails */
  fixture = g_new0 (Fixture, 1);
  fixture->request_certificate_async_func = test_interaction_request_certificate_async_failure;
  fixture->request_certificate_finish_func = test_interaction_request_certificate_finish_failure;
  fixture->request_certificate_func = NULL;
  fixture->result = G_TLS_INTERACTION_FAILED;
  fixture->error_domain = G_FILE_ERROR;
  fixture->error_code = G_FILE_ERROR_NOENT;
  fixture->error_message = "Another message";
  test_name = g_strdup_printf ("%s/async-implementation-failure", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);
}

static void
test_with_unhandled_request_certificate (const gchar *name,
                                         TestFunc     setup,
                                         TestFunc     func,
                                         TestFunc     teardown)
{
  gchar *test_name;
  Fixture *fixture;

  /* Unhandled implementation */
  fixture = g_new0 (Fixture, 1);
  fixture->request_certificate_async_func = NULL;
  fixture->request_certificate_finish_func = NULL;
  fixture->request_certificate_func = NULL;
  fixture->result = G_TLS_INTERACTION_UNHANDLED;
  test_name = g_strdup_printf ("%s/unhandled-implementation", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);
}

static void
test_with_sync_request_certificate (const gchar *name,
                                    TestFunc     setup,
                                    TestFunc     func,
                                    TestFunc     teardown)
{
  gchar *test_name;
  Fixture *fixture;

  /* Sync implementation that succeeds */
  fixture = g_new0 (Fixture, 1);
  fixture->request_certificate_async_func = NULL;
  fixture->request_certificate_finish_func = NULL;
  fixture->request_certificate_func = test_interaction_request_certificate_sync_success;
  fixture->result = G_TLS_INTERACTION_HANDLED;
  test_name = g_strdup_printf ("%s/sync-implementation-success", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);

  /* Async implementation that fails */
  fixture = g_new0 (Fixture, 1);
  fixture->request_certificate_async_func = NULL;
  fixture->request_certificate_finish_func = NULL;
  fixture->request_certificate_func = test_interaction_request_certificate_sync_failure;
  fixture->result = G_TLS_INTERACTION_FAILED;
  fixture->error_domain = G_FILE_ERROR;
  fixture->error_code = G_FILE_ERROR_NOENT;
  fixture->error_message = "Another message";
  test_name = g_strdup_printf ("%s/sync-implementation-failure", name);
  g_test_add (test_name, Test, fixture, setup, func, teardown);
  g_free (test_name);
  g_ptr_array_add (fixtures, fixture);
}

static void
test_with_all_request_certificate (const gchar *name,
                                   TestFunc setup,
                                   TestFunc func,
                                   TestFunc teardown)
{
  test_with_unhandled_request_certificate (name, setup, func, teardown);
  test_with_async_request_certificate (name, setup, func, teardown);
  test_with_sync_request_certificate (name, setup, func, teardown);
}
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
int
main (int   argc,
      char *argv[])
{
  gint ret;

  g_test_init (&argc, &argv, NULL);

  fixtures = g_ptr_array_new_with_free_func (g_free);

  /* Tests for g_tls_interaction_invoke_ask_password */
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
  test_with_all_ask_password ("/tls-interaction/ask-password/invoke-with-loop",
                              setup_with_thread_loop, test_invoke_ask_password, teardown_with_thread_loop);
  test_with_all_ask_password ("/tls-interaction/ask-password/invoke-without-loop",
                              setup_without_loop, test_invoke_ask_password, teardown_without_loop);
  test_with_all_ask_password ("/tls-interaction/ask-password/invoke-in-loop",
                                              setup_with_normal_loop, test_invoke_ask_password, teardown_with_normal_loop);

  /* Tests for g_tls_interaction_ask_password */
  test_with_unhandled_ask_password ("/tls-interaction/ask-password/sync",
                                    setup_without_loop, test_ask_password, teardown_without_loop);
  test_with_sync_ask_password ("/tls-interaction/ask-password/sync",
                               setup_without_loop, test_ask_password, teardown_without_loop);
1030

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  /* Tests for g_tls_interaction_ask_password_async */
  test_with_unhandled_ask_password ("/tls-interaction/ask-password/async",
                                    setup_with_normal_loop, test_ask_password_async, teardown_with_normal_loop);
  test_with_async_ask_password ("/tls-interaction/ask-password/async",
                                setup_with_normal_loop, test_ask_password_async, teardown_with_normal_loop);

  /* Tests for g_tls_interaction_invoke_request_certificate */
  test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-with-loop",
                                     setup_with_thread_loop, test_invoke_request_certificate, teardown_with_thread_loop);
  test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-without-loop",
                                     setup_without_loop, test_invoke_request_certificate, teardown_without_loop);
  test_with_all_request_certificate ("/tls-interaction/request-certificate/invoke-in-loop",
                              setup_with_normal_loop, test_invoke_request_certificate, teardown_with_normal_loop);
1044 1045

  /* Tests for g_tls_interaction_ask_password */
1046 1047 1048 1049
  test_with_unhandled_request_certificate ("/tls-interaction/request-certificate/sync",
                                           setup_without_loop, test_request_certificate, teardown_without_loop);
  test_with_sync_request_certificate ("/tls-interaction/request-certificate/sync",
                                      setup_without_loop, test_request_certificate, teardown_without_loop);
1050 1051

  /* Tests for g_tls_interaction_ask_password_async */
1052 1053 1054 1055
  test_with_unhandled_request_certificate ("/tls-interaction/request-certificate/async",
                                           setup_with_normal_loop, test_request_certificate_async, teardown_with_normal_loop);
  test_with_async_request_certificate ("/tls-interaction/request-certificate/async",
                                       setup_with_normal_loop, test_request_certificate_async, teardown_with_normal_loop);
1056 1057 1058 1059 1060

  ret = g_test_run();
  g_ptr_array_free (fixtures, TRUE);
  return ret;
}