LCOV - code coverage report
Current view: top level - gcr - test-system-prompt.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 415 416 99.8 %
Date: 2022-09-04 10:20:22 Functions: 25 25 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 238 474 50.2 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2                 :            : /*
       3                 :            :    Copyright (C) 2011 Collabora Ltd
       4                 :            : 
       5                 :            :    The Gnome Keyring Library is free software; you can redistribute it and/or
       6                 :            :    modify it under the terms of the GNU Library General Public License as
       7                 :            :    published by the Free Software Foundation; either version 2 of the
       8                 :            :    License, or (at your option) any later version.
       9                 :            : 
      10                 :            :    The Gnome Keyring Library is distributed in the hope that it will be useful,
      11                 :            :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      12                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13                 :            :    Library General Public License for more details.
      14                 :            : 
      15                 :            :    You should have received a copy of the GNU Library General Public
      16                 :            :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      17                 :            :    see <http://www.gnu.org/licenses/>.
      18                 :            : 
      19                 :            :    Author: Stef Walter <stefw@collabora.co.uk>
      20                 :            : */
      21                 :            : 
      22                 :            : #include "config.h"
      23                 :            : 
      24                 :            : #include "gcr/gcr.h"
      25                 :            : #include "gcr/gcr-mock-prompter.h"
      26                 :            : 
      27                 :            : #include "egg/egg-testing.h"
      28                 :            : 
      29                 :            : #include <glib.h>
      30                 :            : 
      31                 :            : #define g_assert_cmpstr_free(a, op, b) G_STMT_START { \
      32                 :            :         char *lhs_str = a;                            \
      33                 :            :         char *rhs_str = b;                            \
      34                 :            :         g_assert_cmpstr (lhs_str, op, rhs_str);       \
      35                 :            :         g_free (lhs_str);                             \
      36                 :            :         g_free (rhs_str);                             \
      37                 :            : } G_STMT_END
      38                 :            : 
      39                 :            : typedef struct {
      40                 :            :         const gchar *prompter_name;
      41                 :            : } Test;
      42                 :            : 
      43                 :            : static void
      44                 :         18 : setup (Test *test,
      45                 :            :        gconstpointer unused)
      46                 :            : {
      47                 :         18 :         test->prompter_name = gcr_mock_prompter_start ();
      48                 :         18 : }
      49                 :            : 
      50                 :            : static void
      51                 :         18 : teardown (Test *test,
      52                 :            :           gconstpointer unused)
      53                 :            : {
      54                 :         18 :         gcr_mock_prompter_stop ();
      55                 :         18 : }
      56                 :            : 
      57                 :            : static void
      58                 :          1 : test_open_prompt (Test *test,
      59                 :            :                   gconstpointer unused)
      60                 :            : {
      61                 :            :         GcrPrompt *prompt;
      62                 :          1 :         GError *error = NULL;
      63                 :            :         gboolean ret;
      64                 :            :         gchar *bus_name;
      65                 :            : 
      66                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
      67         [ -  + ]:          1 :         g_assert_no_error (error);
      68   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
      69                 :            : 
      70                 :          1 :         g_object_get (prompt, "bus-name", &bus_name, NULL);
      71         [ -  + ]:          1 :         g_assert_cmpstr (bus_name, ==, test->prompter_name);
      72                 :            : 
      73                 :          1 :         ret = gcr_system_prompt_close (GCR_SYSTEM_PROMPT (prompt), NULL, &error);
      74         [ -  + ]:          1 :         g_assert_no_error (error);
      75         [ -  + ]:          1 :         g_assert (ret == TRUE);
      76                 :            : 
      77                 :          1 :         g_free (bus_name);
      78                 :          1 :         g_object_unref (prompt);
      79                 :          1 : }
      80                 :            : 
      81                 :            : static void
      82                 :          1 : test_open_failure (Test *test,
      83                 :            :                    gconstpointer unused)
      84                 :            : {
      85                 :            :         GcrPrompt *prompt;
      86                 :            :         GDBusConnection *connection;
      87                 :          1 :         GError *error = NULL;
      88                 :            : 
      89                 :          1 :         connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
      90         [ -  + ]:          1 :         g_assert_no_error (error);
      91                 :            : 
      92                 :            :         /* Try to open a prompt where no prompter is running */
      93                 :            : 
      94                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (g_dbus_connection_get_unique_name (connection),
      95                 :            :                                                       0, NULL, &error);
      96   [ +  -  +  -  :          1 :         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
                   -  + ]
      97         [ -  + ]:          1 :         g_assert (prompt == NULL);
      98                 :            : 
      99                 :          1 :         g_error_free (error);
     100                 :          1 :         g_object_unref (connection);
     101                 :          1 : }
     102                 :            : 
     103                 :            : static void
     104                 :          1 : test_prompt_password (Test *test,
     105                 :            :                       gconstpointer unused)
     106                 :            : {
     107                 :            :         GcrPrompt *prompt;
     108                 :          1 :         GError *error = NULL;
     109                 :            :         const gchar *password;
     110                 :            : 
     111                 :          1 :         gcr_mock_prompter_expect_password_ok ("booo", NULL);
     112                 :            : 
     113                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     114         [ -  + ]:          1 :         g_assert_no_error (error);
     115   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     116                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     117                 :            : 
     118                 :          1 :         password = gcr_prompt_password_run (prompt, NULL, &error);
     119         [ -  + ]:          1 :         g_assert_no_error (error);
     120         [ -  + ]:          1 :         g_assert_cmpstr (password, ==, "booo");
     121                 :            : 
     122                 :          1 :         g_object_unref (prompt);
     123         [ -  + ]:          1 :         g_assert (prompt == NULL);
     124                 :          1 : }
     125                 :            : 
     126                 :            : static void
     127                 :          1 : test_password_in_exchange (Test *test,
     128                 :            :                            gconstpointer unused)
     129                 :            : {
     130                 :            :         GcrPrompt *prompt;
     131                 :          1 :         GError *error = NULL;
     132                 :            :         GcrSecretExchange *exchange;
     133                 :            : 
     134                 :          1 :         gcr_mock_prompter_expect_password_ok ("booo", NULL);
     135                 :            : 
     136                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     137         [ -  + ]:          1 :         g_assert_no_error (error);
     138   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     139                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     140                 :            : 
     141                 :          1 :         gcr_prompt_password_run (prompt, NULL, &error);
     142         [ -  + ]:          1 :         g_assert_no_error (error);
     143                 :            : 
     144                 :          1 :         g_object_get (prompt, "secret-exchange", &exchange, NULL);
     145   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SECRET_EXCHANGE (exchange));
             +  -  -  + ]
     146         [ -  + ]:          1 :         g_assert_cmpstr (gcr_secret_exchange_get_secret (exchange, NULL), ==, "booo");
     147                 :            : 
     148                 :          1 :         g_object_unref (exchange);
     149                 :          1 :         g_object_unref (prompt);
     150         [ -  + ]:          1 :         g_assert (prompt == NULL);
     151                 :          1 : }
     152                 :            : 
     153                 :            : static void
     154                 :          1 : test_password_custom_exchange (Test *test,
     155                 :            :                                gconstpointer unused)
     156                 :            : {
     157                 :            :         GcrSystemPrompt *prompt;
     158                 :          1 :         GError *error = NULL;
     159                 :            :         GcrSecretExchange *exchange;
     160                 :            :         const gchar *password;
     161                 :            : 
     162                 :          1 :         exchange = gcr_secret_exchange_new (NULL);
     163                 :          1 :         gcr_mock_prompter_expect_password_ok ("booo", NULL);
     164                 :            : 
     165                 :          1 :         prompt = g_initable_new (GCR_TYPE_SYSTEM_PROMPT, NULL, &error,
     166                 :            :                                  "timeout-seconds", 0,
     167                 :            :                                  "bus-name", test->prompter_name,
     168                 :            :                                  "secret-exchange", exchange,
     169                 :            :                                  NULL);
     170         [ -  + ]:          1 :         g_assert_no_error (error);
     171   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     172                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     173                 :            : 
     174                 :          1 :         password = gcr_prompt_password_run (GCR_PROMPT (prompt), NULL, &error);
     175         [ -  + ]:          1 :         g_assert_cmpstr (password, ==, "booo");
     176         [ -  + ]:          1 :         g_assert_no_error (error);
     177                 :            : 
     178                 :          1 :         password = gcr_secret_exchange_get_secret (exchange, NULL);
     179         [ -  + ]:          1 :         g_assert_cmpstr (password, ==, "booo");
     180                 :            : 
     181                 :          1 :         g_object_unref (exchange);
     182                 :          1 :         g_object_unref (prompt);
     183         [ -  + ]:          1 :         g_assert (prompt == NULL);
     184                 :          1 : }
     185                 :            : 
     186                 :            : static void
     187                 :          5 : on_async_result (GObject *source,
     188                 :            :                  GAsyncResult *result,
     189                 :            :                  gpointer user_data)
     190                 :            : {
     191                 :          5 :         GAsyncResult **ret = user_data;
     192                 :          5 :         *ret = g_object_ref (result);
     193                 :          5 :         egg_test_wait_stop ();
     194                 :          5 : }
     195                 :            : 
     196                 :            : static void
     197                 :          1 : test_async_password (Test *test,
     198                 :            :                      gconstpointer unused)
     199                 :            : {
     200                 :          1 :         GAsyncResult *result = NULL;
     201                 :            :         GcrPrompt *prompt;
     202                 :          1 :         GError *error = NULL;
     203                 :            :         const gchar *password;
     204                 :            : 
     205                 :          1 :         gcr_mock_prompter_expect_password_ok ("booo", NULL);
     206                 :            : 
     207                 :          1 :         gcr_system_prompt_open_for_prompter_async (test->prompter_name, 0, NULL,
     208                 :            :                                                    on_async_result, &result);
     209         [ -  + ]:          1 :         g_assert (result == NULL);
     210         [ -  + ]:          1 :         egg_test_wait ();
     211                 :            : 
     212         [ -  + ]:          1 :         g_assert (result != NULL);
     213                 :          1 :         prompt = gcr_system_prompt_open_finish (result, &error);
     214         [ -  + ]:          1 :         g_assert_no_error (error);
     215   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     216         [ +  - ]:          1 :         g_clear_object (&result);
     217                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     218                 :            : 
     219                 :          1 :         gcr_prompt_password_async (prompt, NULL,
     220                 :            :                                    on_async_result, &result);
     221         [ -  + ]:          1 :         g_assert (result == NULL);
     222         [ -  + ]:          1 :         egg_test_wait ();
     223                 :            : 
     224                 :          1 :         password = gcr_prompt_password_finish (prompt, result, &error);
     225         [ -  + ]:          1 :         g_assert_no_error (error);
     226         [ -  + ]:          1 :         g_assert_cmpstr (password, ==, "booo");
     227         [ +  - ]:          1 :         g_clear_object (&result);
     228                 :            : 
     229                 :          1 :         g_object_unref (prompt);
     230                 :          1 : }
     231                 :            : 
     232                 :            : static void
     233                 :          1 : test_prompt_confirm (Test *test,
     234                 :            :                      gconstpointer unused)
     235                 :            : {
     236                 :            :         GcrPrompt *prompt;
     237                 :          1 :         GError *error = NULL;
     238                 :            :         gboolean ret;
     239                 :            : 
     240                 :          1 :         gcr_mock_prompter_expect_confirm_ok (NULL);
     241                 :            : 
     242                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     243         [ -  + ]:          1 :         g_assert_no_error (error);
     244   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     245                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     246                 :            : 
     247                 :          1 :         ret = gcr_prompt_confirm_run (prompt, NULL, &error);
     248         [ -  + ]:          1 :         g_assert_no_error (error);
     249         [ -  + ]:          1 :         g_assert (ret == TRUE);
     250                 :            : 
     251                 :          1 :         g_object_unref (prompt);
     252         [ -  + ]:          1 :         g_assert (prompt == NULL);
     253                 :          1 : }
     254                 :            : 
     255                 :            : static void
     256                 :          1 : test_async_confirm (Test *test,
     257                 :            :                     gconstpointer unused)
     258                 :            : {
     259                 :          1 :         GAsyncResult *result = NULL;
     260                 :            :         GcrPrompt *prompt;
     261                 :          1 :         GError *error = NULL;
     262                 :            :         gboolean confirm;
     263                 :            : 
     264                 :          1 :         gcr_mock_prompter_expect_confirm_ok (NULL);
     265                 :            : 
     266                 :          1 :         gcr_system_prompt_open_for_prompter_async (test->prompter_name, 0, NULL,
     267                 :            :                                                    on_async_result, &result);
     268         [ -  + ]:          1 :         g_assert (result == NULL);
     269         [ -  + ]:          1 :         egg_test_wait ();
     270                 :            : 
     271         [ -  + ]:          1 :         g_assert (result != NULL);
     272                 :          1 :         prompt = gcr_system_prompt_open_finish (result, &error);
     273         [ -  + ]:          1 :         g_assert_no_error (error);
     274   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     275         [ +  - ]:          1 :         g_clear_object (&result);
     276                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     277                 :            : 
     278                 :          1 :         gcr_prompt_confirm_async (prompt, NULL, on_async_result, &result);
     279         [ -  + ]:          1 :         g_assert (result == NULL);
     280         [ -  + ]:          1 :         egg_test_wait ();
     281                 :            : 
     282                 :          1 :         confirm = gcr_prompt_confirm_finish (prompt, result, &error);
     283         [ -  + ]:          1 :         g_assert_no_error (error);
     284         [ -  + ]:          1 :         g_assert (confirm == TRUE);
     285         [ +  - ]:          1 :         g_clear_object (&result);
     286                 :            : 
     287                 :          1 :         g_object_unref (prompt);
     288         [ -  + ]:          1 :         g_assert (prompt == NULL);
     289                 :          1 : }
     290                 :            : 
     291                 :            : static void
     292                 :          1 : test_cancel_password (Test *test,
     293                 :            :                       gconstpointer unused)
     294                 :            : {
     295                 :            :         GcrPrompt *prompt;
     296                 :          1 :         GError *error = NULL;
     297                 :            :         const gchar *password;
     298                 :            : 
     299                 :          1 :         gcr_mock_prompter_expect_password_cancel ();
     300                 :            : 
     301                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     302         [ -  + ]:          1 :         g_assert_no_error (error);
     303   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     304                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     305                 :            : 
     306                 :          1 :         password = gcr_prompt_password_run (prompt, NULL, &error);
     307         [ -  + ]:          1 :         g_assert_no_error (error);
     308         [ -  + ]:          1 :         g_assert_cmpstr (password, ==, NULL);
     309                 :            : 
     310                 :          1 :         g_object_unref (prompt);
     311         [ -  + ]:          1 :         g_assert (prompt == NULL);
     312                 :          1 : }
     313                 :            : 
     314                 :            : static void
     315                 :          1 : test_cancel_confirm (Test *test,
     316                 :            :                      gconstpointer unused)
     317                 :            : {
     318                 :            :         GcrPrompt *prompt;
     319                 :          1 :         GError *error = NULL;
     320                 :            :         gboolean ret;
     321                 :            : 
     322                 :          1 :         gcr_mock_prompter_expect_confirm_cancel ();
     323                 :            : 
     324                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     325         [ -  + ]:          1 :         g_assert_no_error (error);
     326   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     327                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     328                 :            : 
     329                 :          1 :         ret = gcr_prompt_confirm_run (prompt, NULL, &error);
     330         [ -  + ]:          1 :         g_assert_no_error (error);
     331         [ -  + ]:          1 :         g_assert (ret == FALSE);
     332                 :            : 
     333                 :          1 :         g_object_unref (prompt);
     334         [ -  + ]:          1 :         g_assert (prompt == NULL);
     335                 :          1 : }
     336                 :            : 
     337                 :            : static void
     338                 :          1 : test_prompt_properties (Test *test,
     339                 :            :                         gconstpointer unused)
     340                 :            : {
     341                 :            :         GcrPrompt *prompt;
     342                 :          1 :         GError *error = NULL;
     343                 :            :         gboolean ret;
     344                 :            : 
     345                 :          1 :         gcr_mock_prompter_expect_confirm_ok ("title", "My Title",
     346                 :            :                                              "description", "My Description",
     347                 :            :                                              "warning", "My Warning",
     348                 :            :                                              "message", "My Message",
     349                 :            :                                              "caller-window", "01010",
     350                 :            :                                              "choice-label", "My Choice",
     351                 :            :                                              "choice-chosen", TRUE,
     352                 :            :                                              "password-new", TRUE,
     353                 :            :                                              "password-strength", 0,
     354                 :            :                                              "continue-label", "My Continue",
     355                 :            :                                              "cancel-label", "My Cancel",
     356                 :            :                                              NULL);
     357                 :            : 
     358                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     359         [ -  + ]:          1 :         g_assert_no_error (error);
     360   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     361                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     362                 :            : 
     363                 :          1 :         g_object_set (prompt,
     364                 :            :                       "title", "Other Title",
     365                 :            :                       "choice-label", "Other Choice",
     366                 :            :                       "description", "Other Description",
     367                 :            :                       "message", "Other Message",
     368                 :            :                       "caller-window", "01012",
     369                 :            :                       "warning", "Other Warning",
     370                 :            :                       "password-new", FALSE,
     371                 :            :                       "choice-chosen", TRUE,
     372                 :            :                       "continue-label", "Other Continue",
     373                 :            :                       "cancel-label", "Other Cancel",
     374                 :            :                       NULL);
     375                 :            : 
     376         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_title (prompt), ==, g_strdup ("Other Title"));
     377         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_choice_label (prompt), ==, g_strdup ("Other Choice"));
     378         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_description (prompt), ==, g_strdup ("Other Description"));
     379         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_message (prompt), ==, g_strdup ("Other Message"));
     380         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_caller_window (prompt), ==, g_strdup ("01012"));
     381         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_warning (prompt), ==, g_strdup ("Other Warning"));
     382         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_continue_label (prompt), ==, g_strdup ("Other Continue"));
     383         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_cancel_label (prompt), ==, g_strdup ("Other Cancel"));
     384         [ -  + ]:          1 :         g_assert (gcr_prompt_get_password_new (prompt) == FALSE);
     385         [ -  + ]:          1 :         g_assert (gcr_prompt_get_choice_chosen (prompt) == TRUE);
     386                 :            : 
     387                 :          1 :         gcr_prompt_set_title (prompt, "My Title");
     388                 :          1 :         gcr_prompt_set_choice_label (prompt, "My Choice");
     389                 :          1 :         gcr_prompt_set_description (prompt, "My Description");
     390                 :          1 :         gcr_prompt_set_message (prompt, "My Message");
     391                 :          1 :         gcr_prompt_set_caller_window (prompt, "01010");
     392                 :          1 :         gcr_prompt_set_warning (prompt, "My Warning");
     393                 :          1 :         gcr_prompt_set_continue_label (prompt, "My Continue");
     394                 :          1 :         gcr_prompt_set_cancel_label (prompt, "My Cancel");
     395                 :          1 :         gcr_prompt_set_password_new (prompt, TRUE);
     396                 :          1 :         gcr_prompt_set_choice_chosen (prompt, TRUE);
     397                 :            : 
     398                 :          1 :         ret = gcr_prompt_confirm_run (prompt, NULL, &error);
     399         [ -  + ]:          1 :         g_assert_no_error (error);
     400         [ -  + ]:          1 :         g_assert (ret == TRUE);
     401                 :            : 
     402         [ -  + ]:          1 :         g_assert (gcr_prompt_get_choice_chosen (prompt) == TRUE);
     403         [ -  + ]:          1 :         g_assert_cmpint (gcr_prompt_get_password_strength (prompt), ==, 0);
     404                 :            : 
     405                 :          1 :         g_object_unref (prompt);
     406         [ -  + ]:          1 :         g_assert (prompt == NULL);
     407                 :          1 : }
     408                 :            : 
     409                 :            : static void
     410                 :          1 : test_prompt_properties_unset (Test *test,
     411                 :            :                               gconstpointer unused)
     412                 :            : {
     413                 :            :         GcrPrompt *prompt;
     414                 :          1 :         GError *error = NULL;
     415                 :            : 
     416                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     417         [ -  + ]:          1 :         g_assert_no_error (error);
     418   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     419                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     420                 :            : 
     421         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_title (prompt), ==, g_strdup (""));
     422         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_choice_label (prompt), ==, NULL);
     423         [ -  + ]:          1 :         g_assert_cmpstr_free(gcr_prompt_get_description (prompt), ==, g_strdup (""));
     424         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_message (prompt), ==, g_strdup (""));
     425         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_caller_window (prompt), ==, NULL);
     426         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_warning (prompt), ==, NULL);
     427         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_continue_label (prompt), ==, g_strdup ("Continue"));
     428         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_cancel_label (prompt), ==, g_strdup ("Cancel"));
     429         [ -  + ]:          1 :         g_assert (gcr_prompt_get_password_new (prompt) == FALSE);
     430         [ -  + ]:          1 :         g_assert (gcr_prompt_get_choice_chosen (prompt) == FALSE);
     431         [ -  + ]:          1 :         g_assert_cmpint (gcr_prompt_get_password_strength (prompt), ==, 0);
     432                 :            : 
     433                 :          1 :         g_object_unref (prompt);
     434         [ -  + ]:          1 :         g_assert (prompt == NULL);
     435                 :          1 : }
     436                 :            : 
     437                 :            : 
     438                 :            : static void
     439                 :          1 : test_prompt_properties_reset (Test *test,
     440                 :            :                               gconstpointer unused)
     441                 :            : {
     442                 :            :         GcrPrompt *prompt;
     443                 :          1 :         GError *error = NULL;
     444                 :            : 
     445                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     446         [ -  + ]:          1 :         g_assert_no_error (error);
     447                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     448                 :            : 
     449                 :          1 :         g_object_set (prompt,
     450                 :            :                       "title", "Other Title",
     451                 :            :                       "choice-label", "Other Choice",
     452                 :            :                       "description", "Other Description",
     453                 :            :                       "message", "Other Message",
     454                 :            :                       "caller-window", "01012",
     455                 :            :                       "warning", "Other Warning",
     456                 :            :                       "password-new", FALSE,
     457                 :            :                       "choice-chosen", TRUE,
     458                 :            :                       "continue-label", "Other Continue",
     459                 :            :                       "cancel-label", "Other Cancel",
     460                 :            :                       NULL);
     461                 :            : 
     462         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_title (prompt), ==, g_strdup ("Other Title"));
     463         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_choice_label (prompt), ==, g_strdup ("Other Choice"));
     464         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_description (prompt), ==, g_strdup ("Other Description"));
     465         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_message (prompt), ==, g_strdup ("Other Message"));
     466         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_caller_window (prompt), ==, g_strdup ("01012"));
     467         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_warning (prompt), ==, g_strdup ("Other Warning"));
     468         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_continue_label (prompt), ==, g_strdup ("Other Continue"));
     469         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_cancel_label (prompt), ==, g_strdup ("Other Cancel"));
     470         [ -  + ]:          1 :         g_assert (gcr_prompt_get_password_new (prompt) == FALSE);
     471         [ -  + ]:          1 :         g_assert (gcr_prompt_get_choice_chosen (prompt) == TRUE);
     472                 :            : 
     473                 :          1 :         gcr_prompt_reset (prompt);
     474                 :            : 
     475         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_title (prompt), ==, g_strdup (""));
     476         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_choice_label (prompt), ==, NULL);
     477         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_description (prompt), ==, g_strdup (""));
     478         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_message (prompt), ==, g_strdup (""));
     479         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_caller_window (prompt), ==, NULL);
     480         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_warning (prompt), ==, NULL);
     481         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_continue_label (prompt), ==, g_strdup ("Continue"));
     482         [ -  + ]:          1 :         g_assert_cmpstr_free (gcr_prompt_get_cancel_label (prompt), ==, g_strdup ("Cancel"));
     483         [ -  + ]:          1 :         g_assert (gcr_prompt_get_password_new (prompt) == FALSE);
     484         [ -  + ]:          1 :         g_assert (gcr_prompt_get_choice_chosen (prompt) == FALSE);
     485         [ -  + ]:          1 :         g_assert_cmpint (gcr_prompt_get_password_strength (prompt), ==, 0);
     486                 :            : 
     487                 :          1 :         g_object_unref (prompt);
     488         [ -  + ]:          1 :         g_assert (prompt == NULL);
     489                 :          1 : }
     490                 :            : 
     491                 :            : static void
     492                 :          1 : test_prompt_close (Test *test,
     493                 :            :                    gconstpointer unused)
     494                 :            : {
     495                 :            :         GcrPrompt *prompt;
     496                 :            :         GcrPrompt *prompt2;
     497                 :          1 :         GError *error = NULL;
     498                 :            :         gboolean ret;
     499                 :            : 
     500                 :          1 :         gcr_mock_prompter_expect_confirm_ok (NULL);
     501                 :            : 
     502                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 1, NULL, &error);
     503         [ -  + ]:          1 :         g_assert_no_error (error);
     504   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     505                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     506                 :            : 
     507                 :          1 :         prompt2 = gcr_system_prompt_open_for_prompter (test->prompter_name, 1, NULL, &error);
     508   [ +  -  +  -  :          1 :         g_assert_error (error, GCR_SYSTEM_PROMPT_ERROR, GCR_SYSTEM_PROMPT_IN_PROGRESS);
                   -  + ]
     509                 :          1 :         g_clear_error (&error);
     510         [ -  + ]:          1 :         g_assert (prompt2 == NULL);
     511                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     512                 :            : 
     513                 :          1 :         ret = gcr_prompt_confirm_run (prompt, NULL, &error);
     514         [ -  + ]:          1 :         g_assert_no_error (error);
     515         [ -  + ]:          1 :         g_assert (ret == TRUE);
     516                 :            : 
     517                 :          1 :         prompt2 = gcr_system_prompt_open_for_prompter (test->prompter_name, 1, NULL, &error);
     518   [ +  -  +  -  :          1 :         g_assert_error (error, GCR_SYSTEM_PROMPT_ERROR, GCR_SYSTEM_PROMPT_IN_PROGRESS);
                   -  + ]
     519                 :          1 :         g_clear_error (&error);
     520         [ -  + ]:          1 :         g_assert (prompt2 == NULL);
     521                 :            : 
     522                 :          1 :         gcr_system_prompt_close (GCR_SYSTEM_PROMPT (prompt), NULL, &error);
     523         [ -  + ]:          1 :         g_assert_no_error (error);
     524                 :            : 
     525                 :          1 :         prompt2 = gcr_system_prompt_open_for_prompter (test->prompter_name, 1, NULL, &error);
     526         [ -  + ]:          1 :         g_assert_no_error (error);
     527   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt2));
             +  -  -  + ]
     528                 :            : 
     529                 :          1 :         g_object_unref (prompt);
     530                 :            : 
     531                 :          1 :         g_object_unref (prompt2);
     532         [ -  + ]:          1 :         g_assert (prompt == NULL);
     533                 :          1 : }
     534                 :            : 
     535                 :            : static void
     536                 :          3 : on_prompt_close (GcrPrompt *prompt,
     537                 :            :                  gpointer user_data)
     538                 :            : {
     539                 :          3 :         gboolean *prompt_closed = (gboolean *)user_data;
     540         [ -  + ]:          3 :         g_assert (*prompt_closed == FALSE);
     541                 :          3 :         *prompt_closed = TRUE;
     542                 :          3 : }
     543                 :            : 
     544                 :            : static void
     545                 :          1 : test_close_cancels (Test *test,
     546                 :            :                     gconstpointer unused)
     547                 :            : {
     548                 :            :         GcrPrompt *prompt;
     549                 :          1 :         GError *error = NULL;
     550                 :          1 :         const gchar *password = NULL;
     551                 :          1 :         GAsyncResult *result = NULL;
     552                 :            :         gboolean prompt_closed;
     553                 :            : 
     554                 :          1 :         gcr_mock_prompter_set_delay_msec (3000);
     555                 :          1 :         gcr_mock_prompter_expect_password_ok ("booo", NULL);
     556                 :            : 
     557                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     558         [ -  + ]:          1 :         g_assert_no_error (error);
     559   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     560                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     561                 :            : 
     562                 :          1 :         prompt_closed = FALSE;
     563                 :          1 :         g_signal_connect_after (prompt, "prompt-close", G_CALLBACK (on_prompt_close), &prompt_closed);
     564                 :            : 
     565                 :          1 :         gcr_prompt_password_async (prompt, NULL, on_async_result, &result);
     566                 :            : 
     567                 :          1 :         gcr_system_prompt_close (GCR_SYSTEM_PROMPT (prompt), NULL, &error);
     568         [ -  + ]:          1 :         g_assert_no_error (error);
     569                 :            : 
     570         [ -  + ]:          1 :         g_assert (prompt_closed == TRUE);
     571         [ -  + ]:          1 :         egg_test_wait ();
     572                 :            : 
     573                 :          1 :         password = gcr_prompt_password_finish (prompt, result, &error);
     574         [ -  + ]:          1 :         g_assert_no_error (error);
     575         [ -  + ]:          1 :         g_assert (password == NULL);
     576         [ +  - ]:          1 :         g_clear_object (&result);
     577                 :            : 
     578                 :          1 :         g_object_unref (prompt);
     579         [ -  + ]:          1 :         g_assert (prompt == NULL);
     580                 :          1 : }
     581                 :            : 
     582                 :            : static void
     583                 :          1 : test_close_from_prompter (Test *test,
     584                 :            :                           gconstpointer unused)
     585                 :            : {
     586                 :            :         GcrPrompt *prompt;
     587                 :          1 :         GError *error = NULL;
     588                 :            :         gboolean ret;
     589                 :            :         const gchar *password;
     590                 :            :         gboolean prompt_closed;
     591                 :            : 
     592                 :          1 :         gcr_mock_prompter_expect_close ();
     593                 :            : 
     594                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 1, NULL, &error);
     595         [ -  + ]:          1 :         g_assert_no_error (error);
     596   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     597                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     598                 :            : 
     599                 :          1 :         prompt_closed = FALSE;
     600                 :          1 :         g_signal_connect_after (prompt, "prompt-close", G_CALLBACK (on_prompt_close), &prompt_closed);
     601                 :            : 
     602                 :          1 :         ret = gcr_prompt_confirm_run (prompt, NULL, &error);
     603         [ -  + ]:          1 :         g_assert_no_error (error);
     604         [ -  + ]:          1 :         g_assert (ret == GCR_PROMPT_REPLY_CANCEL);
     605                 :            : 
     606                 :            :         /* The prompt should be closed now, these shouldn't reach the mock prompter */
     607                 :            : 
     608         [ +  + ]:          3 :         while (!prompt_closed)
     609                 :          2 :                 g_main_context_iteration (NULL, TRUE);
     610                 :            : 
     611                 :          1 :         ret = gcr_prompt_confirm_run (prompt, NULL, &error);
     612         [ -  + ]:          1 :         g_assert_no_error (error);
     613         [ -  + ]:          1 :         g_assert (ret == GCR_PROMPT_REPLY_CANCEL);
     614                 :            : 
     615                 :          1 :         password = gcr_prompt_password_run (prompt, NULL, &error);
     616         [ -  + ]:          1 :         g_assert_no_error (error);
     617         [ -  + ]:          1 :         g_assert (password == NULL);
     618                 :            : 
     619                 :          1 :         g_object_unref (prompt);
     620         [ -  + ]:          1 :         g_assert (prompt == NULL);
     621                 :          1 : }
     622                 :            : 
     623                 :            : static void
     624                 :          1 : test_after_close_dismisses (Test *test,
     625                 :            :                             gconstpointer unused)
     626                 :            : {
     627                 :            :         GcrPrompt *prompt;
     628                 :          1 :         GError *error = NULL;
     629                 :            :         gboolean ret;
     630                 :            :         const gchar *password;
     631                 :            :         gboolean prompt_closed;
     632                 :            : 
     633                 :          1 :         gcr_mock_prompter_expect_confirm_ok (NULL);
     634                 :            : 
     635                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 1, NULL, &error);
     636         [ -  + ]:          1 :         g_assert_no_error (error);
     637   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     638                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     639                 :            : 
     640                 :          1 :         prompt_closed = FALSE;
     641                 :          1 :         g_signal_connect_after (prompt, "prompt-close", G_CALLBACK (on_prompt_close), &prompt_closed);
     642                 :            : 
     643                 :            : 
     644                 :          1 :         ret = gcr_prompt_confirm_run (prompt, NULL, &error);
     645         [ -  + ]:          1 :         g_assert_no_error (error);
     646         [ -  + ]:          1 :         g_assert (ret == GCR_PROMPT_REPLY_CONTINUE);
     647                 :            : 
     648                 :          1 :         gcr_prompt_close (prompt);
     649         [ -  + ]:          1 :         g_assert (prompt_closed);
     650                 :            : 
     651                 :            :         /* These should never even reach the mock prompter */
     652                 :            : 
     653                 :          1 :         ret = gcr_prompt_confirm_run (prompt, NULL, &error);
     654         [ -  + ]:          1 :         g_assert_no_error (error);
     655         [ -  + ]:          1 :         g_assert (ret == GCR_PROMPT_REPLY_CANCEL);
     656                 :            : 
     657                 :          1 :         password = gcr_prompt_password_run (prompt, NULL, &error);
     658         [ -  + ]:          1 :         g_assert_no_error (error);
     659         [ -  + ]:          1 :         g_assert (password == NULL);
     660                 :            : 
     661         [ -  + ]:          1 :         while (g_main_context_iteration (NULL, FALSE));
     662                 :            : 
     663                 :          1 :         g_object_unref (prompt);
     664         [ -  + ]:          1 :         g_assert (prompt == NULL);
     665                 :          1 : }
     666                 :            : 
     667                 :            : typedef struct {
     668                 :            :         GAsyncResult *result1;
     669                 :            :         GAsyncResult *result2;
     670                 :            : } ResultPair;
     671                 :            : 
     672                 :            : static void
     673                 :          1 : on_result_pair_one (GObject *source,
     674                 :            :                     GAsyncResult *result,
     675                 :            :                     gpointer user_data)
     676                 :            : {
     677                 :          1 :         ResultPair *pair = user_data;
     678         [ -  + ]:          1 :         g_assert (pair->result1 == NULL);
     679                 :          1 :         pair->result1 = g_object_ref (result);
     680   [ +  -  -  + ]:          1 :         if (pair->result1 && pair->result2)
     681                 :          0 :                 egg_test_wait_stop ();
     682                 :          1 : }
     683                 :            : 
     684                 :            : static void
     685                 :          1 : on_result_pair_two (GObject *source,
     686                 :            :                     GAsyncResult *result,
     687                 :            :                     gpointer user_data)
     688                 :            : {
     689                 :          1 :         ResultPair *pair = user_data;
     690         [ -  + ]:          1 :         g_assert (pair->result2 == NULL);
     691                 :          1 :         pair->result2 = g_object_ref (result);
     692   [ +  -  +  - ]:          1 :         if (pair->result1 && pair->result2)
     693                 :          1 :                 egg_test_wait_stop ();
     694                 :          1 : }
     695                 :            : 
     696                 :            : static void
     697                 :          1 : test_watch_cancels (Test *test,
     698                 :            :                     gconstpointer unused)
     699                 :            : {
     700                 :            :         GcrPrompt *prompt;
     701                 :            :         GcrPrompt *prompt2;
     702                 :          1 :         GError *error = NULL;
     703                 :            :         const gchar *password;
     704                 :          1 :         ResultPair pair = { NULL, NULL };
     705                 :            : 
     706                 :          1 :         gcr_mock_prompter_set_delay_msec (3000);
     707                 :          1 :         gcr_mock_prompter_expect_password_ok ("booo", NULL);
     708                 :            : 
     709                 :            :         /* This should happen immediately */
     710                 :          1 :         prompt = gcr_system_prompt_open_for_prompter (test->prompter_name, 0, NULL, &error);
     711         [ -  + ]:          1 :         g_assert_no_error (error);
     712   [ -  +  +  -  :          1 :         g_assert (GCR_IS_SYSTEM_PROMPT (prompt));
             +  -  -  + ]
     713                 :            : 
     714                 :            :         /* Show a password prompt */
     715                 :          1 :         gcr_prompt_password_async (prompt, NULL, on_result_pair_one, &pair);
     716                 :            : 
     717                 :            :         /* This prompt should wait, block */
     718                 :          1 :         gcr_system_prompt_open_for_prompter_async (test->prompter_name, 0, NULL,
     719                 :            :                                                    on_result_pair_two, &pair);
     720                 :            : 
     721                 :            :         /* Wait a bit before stopping, so outgoing request is done */
     722                 :          1 :         egg_test_wait_until (1000);
     723                 :            : 
     724                 :            :         /* Kill the mock prompter */
     725                 :          1 :         gcr_mock_prompter_disconnect ();
     726                 :            : 
     727                 :            :         /* Both the above operations should cancel */
     728         [ -  + ]:          1 :         egg_test_wait ();
     729                 :            : 
     730                 :          1 :         prompt2 = gcr_system_prompt_open_finish (pair.result2, &error);
     731   [ +  -  +  -  :          1 :         g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
                   -  + ]
     732                 :          1 :         g_clear_error (&error);
     733         [ -  + ]:          1 :         g_assert (prompt2 == NULL);
     734                 :            : 
     735                 :          1 :         password = gcr_prompt_password_finish (prompt, pair.result1, &error);
     736   [ +  -  +  -  :          1 :         g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
                   -  + ]
     737                 :          1 :         g_clear_error (&error);
     738         [ -  + ]:          1 :         g_assert (password == NULL);
     739                 :            : 
     740                 :          1 :         g_object_unref (prompt);
     741                 :          1 :         g_object_unref (pair.result1);
     742                 :          1 :         g_object_unref (pair.result2);
     743                 :          1 : }
     744                 :            : 
     745                 :            : int
     746                 :          1 : main (int argc, char **argv)
     747                 :            : {
     748                 :          1 :         g_test_init (&argc, &argv, NULL);
     749                 :          1 :         g_set_prgname ("test-system-prompt");
     750                 :            : 
     751                 :          1 :         g_test_add ("/gcr/system-prompt/open", Test, NULL, setup, test_open_prompt, teardown);
     752                 :          1 :         g_test_add ("/gcr/system-prompt/open-failure", Test, NULL, setup, test_open_failure, teardown);
     753                 :          1 :         g_test_add ("/gcr/system-prompt/password", Test, NULL, setup, test_prompt_password, teardown);
     754                 :          1 :         g_test_add ("/gcr/system-prompt/password-async", Test, NULL, setup, test_async_password, teardown);
     755                 :          1 :         g_test_add ("/gcr/system-prompt/password-cancel", Test, NULL, setup, test_cancel_password, teardown);
     756                 :          1 :         g_test_add ("/gcr/system-prompt/password-in-exchange", Test, NULL, setup, test_password_in_exchange, teardown);
     757                 :          1 :         g_test_add ("/gcr/system-prompt/password-custom-exchange", Test, NULL, setup, test_password_custom_exchange, teardown);
     758                 :          1 :         g_test_add ("/gcr/system-prompt/confirm", Test, NULL, setup, test_prompt_confirm, teardown);
     759                 :          1 :         g_test_add ("/gcr/system-prompt/confirm-async", Test, NULL, setup, test_async_confirm, teardown);
     760                 :          1 :         g_test_add ("/gcr/system-prompt/confirm-cancel", Test, NULL, setup, test_cancel_confirm, teardown);
     761                 :          1 :         g_test_add ("/gcr/system-prompt/properties", Test, NULL, setup, test_prompt_properties, teardown);
     762                 :          1 :         g_test_add ("/gcr/system-prompt/properties-unset", Test, NULL, setup, test_prompt_properties_unset, teardown);
     763                 :          1 :         g_test_add ("/gcr/system-prompt/properties-reset", Test, NULL, setup, test_prompt_properties_reset, teardown);
     764                 :          1 :         g_test_add ("/gcr/system-prompt/close", Test, NULL, setup, test_prompt_close, teardown);
     765                 :          1 :         g_test_add ("/gcr/system-prompt/close-cancels", Test, NULL, setup, test_close_cancels, teardown);
     766                 :          1 :         g_test_add ("/gcr/system-prompt/after-close-dismisses", Test, NULL, setup, test_after_close_dismisses, teardown);
     767                 :          1 :         g_test_add ("/gcr/system-prompt/close-from-prompter", Test, NULL, setup, test_close_from_prompter, teardown);
     768                 :          1 :         g_test_add ("/gcr/system-prompt/watch-cancels", Test, NULL, setup, test_watch_cancels, teardown);
     769                 :            : 
     770                 :          1 :         return egg_tests_run_with_loop ();
     771                 :            : }

Generated by: LCOV version 1.14