LCOV - code coverage report
Current view: top level - libsecret - test-prompt.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 218 218 100.0 %
Date: 2024-02-08 14:44:34 Functions: 18 18 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 58 116 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* libsecret - GLib wrapper for Secret Service
       2                 :            :  *
       3                 :            :  * Copyright 2011 Red Hat Inc.
       4                 :            :  *
       5                 :            :  * This program is free software: you can redistribute it and/or modify
       6                 :            :  * it under the terms of the GNU Lesser General Public License as published
       7                 :            :  * by the Free Software Foundation; either version 2 of the licence or (at
       8                 :            :  * your option) any later version.
       9                 :            :  *
      10                 :            :  * See the included COPYING file for more information.
      11                 :            :  *
      12                 :            :  * Author: Stef Walter <stefw@gnome.org>
      13                 :            :  */
      14                 :            : 
      15                 :            : 
      16                 :            : #include "config.h"
      17                 :            : 
      18                 :            : #undef G_DISABLE_ASSERT
      19                 :            : 
      20                 :            : #include "secret-item.h"
      21                 :            : #include "secret-service.h"
      22                 :            : #include "secret-private.h"
      23                 :            : #include "secret-prompt.h"
      24                 :            : 
      25                 :            : #include "mock-service.h"
      26                 :            : 
      27                 :            : #include "egg/egg-testing.h"
      28                 :            : 
      29                 :            : #include <glib.h>
      30                 :            : 
      31                 :            : #include <errno.h>
      32                 :            : #include <stdlib.h>
      33                 :            : 
      34                 :            : typedef struct {
      35                 :            :         SecretService *service;
      36                 :            : } Test;
      37                 :            : 
      38                 :            : static void
      39                 :         12 : setup (Test *test,
      40                 :            :        gconstpointer data)
      41                 :            : {
      42                 :         12 :         GError *error = NULL;
      43                 :         12 :         const gchar *mock_script = data;
      44                 :            : 
      45                 :         12 :         mock_service_start (mock_script, &error);
      46         [ -  + ]:         12 :         g_assert_no_error (error);
      47                 :            : 
      48                 :         12 :         test->service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
      49         [ -  + ]:         12 :         g_assert_no_error (error);
      50                 :         12 :         g_object_add_weak_pointer (G_OBJECT (test->service), (gpointer *)&test->service);
      51                 :         12 : }
      52                 :            : 
      53                 :            : static void
      54                 :         12 : teardown (Test *test,
      55                 :            :           gconstpointer unused)
      56                 :            : {
      57                 :         12 :         g_object_unref (test->service);
      58                 :         12 :         secret_service_disconnect ();
      59         [ -  + ]:         12 :         g_assert_null (test->service);
      60                 :            : 
      61                 :         12 :         mock_service_stop ();
      62                 :         12 : }
      63                 :            : 
      64                 :            : static void
      65                 :          5 : on_async_result (GObject *source,
      66                 :            :                  GAsyncResult *result,
      67                 :            :                  gpointer user_data)
      68                 :            : {
      69                 :          5 :         GAsyncResult **ret = user_data;
      70         [ -  + ]:          5 :         g_assert_nonnull (ret);
      71         [ -  + ]:          5 :         g_assert_null (*ret);
      72                 :          5 :         *ret = g_object_ref (result);
      73                 :          5 :         egg_test_wait_stop ();
      74                 :          5 : }
      75                 :            : 
      76                 :            : static gboolean
      77                 :       1988 : on_idle_increment (gpointer user_data)
      78                 :            : {
      79                 :       1988 :         guint *value = user_data;
      80                 :       1988 :         ++(*value);
      81                 :       1988 :         return TRUE;
      82                 :            : }
      83                 :            : 
      84                 :            : static void
      85                 :          1 : test_perform_sync (Test *test,
      86                 :            :                    gconstpointer unused)
      87                 :            : {
      88                 :            :         SecretPrompt *prompt;
      89                 :          1 :         GError *error = NULL;
      90                 :            :         GVariant *retval;
      91                 :          1 :         guint value = 0;
      92                 :            :         guint increment_id;
      93                 :            : 
      94                 :            :         /* Verify that main loop does not run during this call */
      95                 :          1 :         increment_id = g_idle_add (on_idle_increment, &value);
      96                 :            : 
      97                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
      98                 :            : 
      99                 :          1 :         retval = secret_prompt_perform_sync (prompt, NULL, NULL, NULL, &error);
     100         [ -  + ]:          1 :         g_assert_no_error (error);
     101         [ -  + ]:          1 :         g_assert_nonnull (retval);
     102                 :          1 :         g_variant_unref (retval);
     103                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     104                 :            : 
     105         [ -  + ]:          1 :         g_assert_cmpuint (value, ==, 0);
     106                 :          1 :         g_source_remove (increment_id);
     107                 :            : 
     108                 :          1 :         g_object_unref (prompt);
     109         [ -  + ]:          1 :         g_assert_null (prompt);
     110                 :          1 : }
     111                 :            : 
     112                 :            : static void
     113                 :          1 : test_perform_run (Test *test,
     114                 :            :                   gconstpointer unused)
     115                 :            : {
     116                 :            :         SecretPrompt *prompt;
     117                 :          1 :         GError *error = NULL;
     118                 :            :         GVariant *retval;
     119                 :          1 :         guint value = 0;
     120                 :            :         guint increment_id;
     121                 :            : 
     122                 :            :         /* Verify that main loop does run during this call */
     123                 :          1 :         increment_id = g_idle_add (on_idle_increment, &value);
     124                 :            : 
     125                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
     126                 :            : 
     127                 :          1 :         retval = secret_prompt_run (prompt, 0, NULL, NULL, &error);
     128         [ -  + ]:          1 :         g_assert_no_error (error);
     129         [ -  + ]:          1 :         g_assert_nonnull (retval);
     130                 :          1 :         g_variant_unref (retval);
     131                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     132                 :            : 
     133         [ -  + ]:          1 :         g_assert_cmpuint (value, >, 0);
     134                 :          1 :         g_source_remove (increment_id);
     135                 :            : 
     136                 :            :         /* Make sure everything completes */
     137                 :          1 :         egg_test_wait_idle ();
     138                 :            : 
     139                 :          1 :         g_object_unref (prompt);
     140         [ -  + ]:          1 :         g_assert_null (prompt);
     141                 :          1 : }
     142                 :            : 
     143                 :            : static void
     144                 :          1 : test_perform_async (Test *test,
     145                 :            :                     gconstpointer unused)
     146                 :            : {
     147                 :            :         SecretPrompt *prompt;
     148                 :          1 :         GError *error = NULL;
     149                 :          1 :         GAsyncResult *result = NULL;
     150                 :            :         GVariant *retval;
     151                 :            : 
     152                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
     153                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     154                 :            : 
     155                 :          1 :         secret_prompt_perform (prompt, 0, NULL, NULL, on_async_result, &result);
     156         [ -  + ]:          1 :         g_assert_null (result);
     157                 :            : 
     158         [ -  + ]:          1 :         egg_test_wait ();
     159                 :            : 
     160                 :          1 :         retval = secret_prompt_perform_finish (prompt, result, &error);
     161         [ -  + ]:          1 :         g_assert_no_error (error);
     162         [ -  + ]:          1 :         g_assert_nonnull (retval);
     163                 :          1 :         g_variant_unref (retval);
     164                 :          1 :         g_object_unref (result);
     165                 :            : 
     166                 :            :         /* Make sure everything completes */
     167                 :          1 :         egg_test_wait_idle ();
     168                 :            : 
     169                 :          1 :         g_object_unref (prompt);
     170         [ -  + ]:          1 :         g_assert_null (prompt);
     171                 :          1 : }
     172                 :            : 
     173                 :            : static void
     174                 :          1 : test_perform_cancel (Test *test,
     175                 :            :                      gconstpointer unused)
     176                 :            : {
     177                 :            :         SecretPrompt *prompt;
     178                 :          1 :         GError *error = NULL;
     179                 :          1 :         GAsyncResult *result = NULL;
     180                 :            :         GCancellable *cancellable;
     181                 :            :         GVariant *retval;
     182                 :            : 
     183                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/delay");
     184                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     185                 :            : 
     186                 :          1 :         cancellable = g_cancellable_new ();
     187                 :          1 :         secret_prompt_perform (prompt, 0, NULL, cancellable, on_async_result, &result);
     188         [ -  + ]:          1 :         g_assert_null (result);
     189                 :            : 
     190                 :          1 :         g_cancellable_cancel (cancellable);
     191                 :          1 :         g_object_unref (cancellable);
     192                 :            : 
     193         [ -  + ]:          1 :         egg_test_wait ();
     194                 :            : 
     195                 :          1 :         retval = secret_prompt_perform_finish (prompt, result, &error);
     196         [ -  + ]:          1 :         g_assert_no_error (error);
     197         [ -  + ]:          1 :         g_assert_nonnull (retval);
     198                 :          1 :         g_variant_unref (retval);
     199                 :            : 
     200                 :          1 :         g_object_unref (result);
     201                 :          1 :         g_object_unref (prompt);
     202                 :            : 
     203                 :            :         /* Due to GDBus threading races */
     204                 :          1 :         egg_test_wait_until (100);
     205                 :            : 
     206         [ -  + ]:          1 :         g_assert_null (prompt);
     207                 :          1 : }
     208                 :            : 
     209                 :            : static void
     210                 :          1 : test_perform_fail (Test *test,
     211                 :            :                    gconstpointer unused)
     212                 :            : {
     213                 :            :         SecretPrompt *prompt;
     214                 :          1 :         GError *error = NULL;
     215                 :            :         GVariant *retval;
     216                 :            : 
     217                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/error");
     218                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     219                 :            : 
     220                 :          1 :         retval = secret_prompt_perform_sync (prompt, NULL, NULL, NULL, &error);
     221   [ +  -  +  -  :          1 :         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED);
                   -  + ]
     222         [ -  + ]:          1 :         g_assert_null (retval);
     223                 :          1 :         g_clear_error (&error);
     224                 :            : 
     225                 :          1 :         g_object_unref (prompt);
     226         [ -  + ]:          1 :         g_assert_null (prompt);
     227                 :          1 : }
     228                 :            : 
     229                 :            : static void
     230                 :          1 : test_perform_vanish (Test *test,
     231                 :            :                      gconstpointer unused)
     232                 :            : {
     233                 :            :         SecretPrompt *prompt;
     234                 :          1 :         GError *error = NULL;
     235                 :            :         GVariant *retval;
     236                 :            : 
     237                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/vanish");
     238                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     239                 :            : 
     240                 :          1 :         retval = secret_prompt_perform_sync (prompt, NULL, NULL, NULL, &error);
     241         [ -  + ]:          1 :         g_assert_no_error (error);
     242         [ -  + ]:          1 :         g_assert_null (retval);
     243                 :            : 
     244                 :          1 :         g_object_unref (prompt);
     245         [ -  + ]:          1 :         g_assert_null (prompt);
     246                 :          1 : }
     247                 :            : 
     248                 :            : static void
     249                 :          1 : test_prompt_result (Test *test,
     250                 :            :                     gconstpointer unused)
     251                 :            : {
     252                 :            :         SecretPrompt *prompt;
     253                 :          1 :         GError *error = NULL;
     254                 :            :         GVariant *retval;
     255                 :            : 
     256                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/result");
     257                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     258                 :            : 
     259                 :          1 :         retval = secret_prompt_perform_sync (prompt, 0, NULL, G_VARIANT_TYPE_STRING, &error);
     260         [ -  + ]:          1 :         g_assert_no_error (error);
     261         [ -  + ]:          1 :         g_assert_nonnull (retval);
     262         [ -  + ]:          1 :         g_assert_cmpstr (g_variant_get_string (retval, NULL), ==, "Special Result");
     263                 :          1 :         g_variant_unref (retval);
     264                 :            : 
     265                 :          1 :         g_object_unref (prompt);
     266         [ -  + ]:          1 :         g_assert_null (prompt);
     267                 :          1 : }
     268                 :            : 
     269                 :            : static void
     270                 :          1 : test_prompt_window_id (Test *test,
     271                 :            :                        gconstpointer unused)
     272                 :            : {
     273                 :            :         SecretPrompt *prompt;
     274                 :          1 :         GError *error = NULL;
     275                 :            :         GVariant *retval;
     276                 :            : 
     277                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/window");
     278                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     279                 :            : 
     280                 :          1 :         retval = secret_prompt_perform_sync (prompt, "555", NULL, G_VARIANT_TYPE_STRING, &error);
     281         [ -  + ]:          1 :         g_assert_no_error (error);
     282         [ -  + ]:          1 :         g_assert_nonnull (retval);
     283         [ -  + ]:          1 :         g_assert_cmpstr (g_variant_get_string (retval, NULL), ==, "555");
     284                 :          1 :         g_variant_unref (retval);
     285                 :            : 
     286                 :          1 :         g_object_unref (prompt);
     287         [ -  + ]:          1 :         g_assert_null (prompt);
     288                 :          1 : }
     289                 :            : 
     290                 :            : static void
     291                 :          1 : test_service_sync (Test *test,
     292                 :            :                    gconstpointer unused)
     293                 :            : {
     294                 :            :         SecretPrompt *prompt;
     295                 :          1 :         GError *error = NULL;
     296                 :            :         GVariant *retval;
     297                 :            : 
     298                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
     299                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     300                 :            : 
     301                 :          1 :         retval = secret_service_prompt_sync (test->service, prompt, NULL, NULL, &error);
     302         [ -  + ]:          1 :         g_assert_no_error (error);
     303         [ -  + ]:          1 :         g_assert_nonnull (retval);
     304                 :          1 :         g_variant_unref (retval);
     305                 :            : 
     306                 :          1 :         g_object_unref (prompt);
     307         [ -  + ]:          1 :         g_assert_null (prompt);
     308                 :          1 : }
     309                 :            : 
     310                 :            : static void
     311                 :          1 : test_service_async (Test *test,
     312                 :            :                     gconstpointer unused)
     313                 :            : {
     314                 :            :         SecretPrompt *prompt;
     315                 :          1 :         GError *error = NULL;
     316                 :          1 :         GAsyncResult *result = NULL;
     317                 :            :         GVariant *retval;
     318                 :            : 
     319                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
     320                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     321                 :            : 
     322                 :          1 :         secret_service_prompt (test->service, prompt, NULL, NULL, on_async_result, &result);
     323         [ -  + ]:          1 :         g_assert_null (result);
     324                 :            : 
     325         [ -  + ]:          1 :         egg_test_wait ();
     326                 :            : 
     327                 :          1 :         retval = secret_service_prompt_finish (test->service, result, &error);
     328         [ -  + ]:          1 :         g_assert_no_error (error);
     329         [ -  + ]:          1 :         g_assert_nonnull (retval);
     330                 :          1 :         g_variant_unref (retval);
     331                 :          1 :         g_object_unref (result);
     332                 :            : 
     333                 :            :         /* Make sure everything completes */
     334                 :          1 :         egg_test_wait_idle ();
     335                 :            : 
     336                 :          1 :         g_object_unref (prompt);
     337         [ -  + ]:          1 :         g_assert_null (prompt);
     338                 :          1 : }
     339                 :            : 
     340                 :            : static void
     341                 :          1 : test_service_fail (Test *test,
     342                 :            :                     gconstpointer unused)
     343                 :            : {
     344                 :            :         SecretPrompt *prompt;
     345                 :          1 :         GError *error = NULL;
     346                 :          1 :         GAsyncResult *result = NULL;
     347                 :            :         GVariant *retval;
     348                 :            : 
     349                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/error");
     350                 :          1 :         g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
     351                 :            : 
     352                 :          1 :         secret_service_prompt (test->service, prompt, NULL, NULL, on_async_result, &result);
     353         [ -  + ]:          1 :         g_assert_null (result);
     354                 :            : 
     355         [ -  + ]:          1 :         egg_test_wait ();
     356                 :            : 
     357                 :          1 :         retval = secret_service_prompt_finish (test->service, result, &error);
     358   [ +  -  +  -  :          1 :         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED);
                   -  + ]
     359         [ -  + ]:          1 :         g_assert_null (retval);
     360                 :          1 :         g_object_unref (result);
     361                 :          1 :         g_clear_error (&error);
     362                 :            : 
     363                 :            :         /* Make sure everything completes */
     364                 :          1 :         egg_test_wait_idle ();
     365                 :            : 
     366                 :          1 :         g_object_unref (prompt);
     367         [ -  + ]:          1 :         g_assert_null (prompt);
     368                 :          1 : }
     369                 :            : 
     370                 :            : static void
     371                 :          1 : test_service_path (Test *test,
     372                 :            :                     gconstpointer unused)
     373                 :            : {
     374                 :          1 :         GError *error = NULL;
     375                 :          1 :         GAsyncResult *result = NULL;
     376                 :            :         SecretPrompt *prompt;
     377                 :            :         GVariant *retval;
     378                 :            : 
     379                 :          1 :         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
     380                 :            : 
     381                 :          1 :         secret_service_prompt (test->service, prompt, NULL, NULL, on_async_result, &result);
     382         [ -  + ]:          1 :         g_assert_null (result);
     383                 :            : 
     384                 :          1 :         g_object_unref (prompt);
     385         [ -  + ]:          1 :         egg_test_wait ();
     386                 :            : 
     387                 :          1 :         retval = secret_service_prompt_finish (test->service, result, &error);
     388         [ -  + ]:          1 :         g_assert_no_error (error);
     389         [ -  + ]:          1 :         g_assert_nonnull (retval);
     390                 :          1 :         g_variant_unref (retval);
     391                 :          1 :         g_object_unref (result);
     392                 :            : 
     393                 :            :         /* Make sure everything completes */
     394                 :          1 :         egg_test_wait_idle ();
     395                 :          1 : }
     396                 :            : 
     397                 :            : static void
     398                 :          2 : null_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
     399                 :            :                   const gchar *message, gpointer user_data)
     400                 :            : {
     401                 :            : 
     402                 :          2 : }
     403                 :            : 
     404                 :            : int
     405                 :          1 : main (int argc, char **argv)
     406                 :            : {
     407                 :          1 :         g_test_init (&argc, &argv, NULL);
     408                 :          1 :         g_set_prgname ("test-prompt");
     409                 :            : 
     410                 :            :         /* Suppress these messages in tests */
     411                 :          1 :         g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
     412                 :            :                            null_log_handler, NULL);
     413                 :            : 
     414                 :          1 :         g_test_add ("/prompt/run", Test, "mock-service-prompt.py", setup, test_perform_run, teardown);
     415                 :          1 :         g_test_add ("/prompt/perform-sync", Test, "mock-service-prompt.py", setup, test_perform_sync, teardown);
     416                 :          1 :         g_test_add ("/prompt/perform-async", Test, "mock-service-prompt.py", setup, test_perform_async, teardown);
     417                 :          1 :         g_test_add ("/prompt/perform-cancel", Test, "mock-service-prompt.py", setup, test_perform_cancel, teardown);
     418                 :          1 :         g_test_add ("/prompt/perform-fail", Test, "mock-service-prompt.py", setup, test_perform_fail, teardown);
     419                 :          1 :         g_test_add ("/prompt/perform-vanish", Test, "mock-service-prompt.py", setup, test_perform_vanish, teardown);
     420                 :          1 :         g_test_add ("/prompt/result", Test, "mock-service-prompt.py", setup, test_prompt_result, teardown);
     421                 :          1 :         g_test_add ("/prompt/window-id", Test, "mock-service-prompt.py", setup, test_prompt_window_id, teardown);
     422                 :            : 
     423                 :          1 :         g_test_add ("/prompt/service-sync", Test, "mock-service-prompt.py", setup, test_service_sync, teardown);
     424                 :          1 :         g_test_add ("/prompt/service-async", Test, "mock-service-prompt.py", setup, test_service_async, teardown);
     425                 :          1 :         g_test_add ("/prompt/service-path", Test, "mock-service-prompt.py", setup, test_service_path, teardown);
     426                 :          1 :         g_test_add ("/prompt/service-fail", Test, "mock-service-prompt.py", setup, test_service_fail, teardown);
     427                 :            : 
     428                 :          1 :         return egg_tests_run_with_loop ();
     429                 :            : }

Generated by: LCOV version 1.14