LCOV - code coverage report
Current view: top level - egg - egg-testing.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 73 96 76.0 %
Date: 2024-02-08 14:44:34 Functions: 10 12 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 19 54 35.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * gnome-keyring
       3                 :            :  *
       4                 :            :  * Copyright (C) 2011 Collabora Ltd.
       5                 :            :  * 
       6                 :            :  * This program is free software; you can redistribute it and/or modify 
       7                 :            :  * it under the terms of the GNU Lesser General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2.1 of
       9                 :            :  * the License, or (at your option) any later version.
      10                 :            :  *  
      11                 :            :  * This program is distributed in the hope that it will be useful, but
      12                 :            :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *  
      16                 :            :  * You should have received a copy of the GNU Lesser General Public
      17                 :            :  * License along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
      19                 :            :  * MA 02110-1301 USA
      20                 :            :  *
      21                 :            :  * Author: Stef Walter <stefw@collabora.co.uk>
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "config.h"
      25                 :            : 
      26                 :            : #include "egg-testing.h"
      27                 :            : 
      28                 :            : #include <glib-object.h>
      29                 :            : 
      30                 :            : #include <glib.h>
      31                 :            : #include <glib/gstdio.h>
      32                 :            : 
      33                 :            : #include <errno.h>
      34                 :            : #include <fcntl.h>
      35                 :            : #include <unistd.h>
      36                 :            : 
      37                 :            : static const char HEXC[] = "0123456789ABCDEF";
      38                 :            : 
      39                 :            : gchar *
      40                 :          0 : egg_test_escape_data (const guchar *data,
      41                 :            :                       gsize n_data)
      42                 :            : {
      43                 :            :         GString *result;
      44                 :            :         gchar c;
      45                 :            :         gsize i;
      46                 :            :         guchar j;
      47                 :            : 
      48         [ #  # ]:          0 :         g_assert_nonnull (data);
      49                 :            : 
      50                 :          0 :         result = g_string_sized_new (n_data * 2 + 1);
      51         [ #  # ]:          0 :         for (i = 0; i < n_data; ++i) {
      52                 :          0 :                 c = data[i];
      53   [ #  #  #  # ]:          0 :                 if (g_ascii_isprint (c) && !strchr ("\n\r\v", c)) {
      54         [ #  # ]:          0 :                         g_string_append_c (result, c);
      55                 :            :                 } else {
      56         [ #  # ]:          0 :                         g_string_append (result, "\\x");
      57                 :          0 :                         j = c >> 4 & 0xf;
      58         [ #  # ]:          0 :                         g_string_append_c (result, HEXC[j]);
      59                 :          0 :                         j = c & 0xf;
      60         [ #  # ]:          0 :                         g_string_append_c (result, HEXC[j]);
      61                 :            :                 }
      62                 :            :         }
      63                 :            : 
      64                 :          0 :         return g_string_free (result, FALSE);
      65                 :            : }
      66                 :            : 
      67                 :            : void
      68                 :          0 : egg_assertion_message_cmpmem (const char     *domain,
      69                 :            :                               const char     *file,
      70                 :            :                               int             line,
      71                 :            :                               const char     *func,
      72                 :            :                               const char     *expr,
      73                 :            :                               gconstpointer   arg1,
      74                 :            :                               gsize           n_arg1,
      75                 :            :                               const char     *cmp,
      76                 :            :                               gconstpointer   arg2,
      77                 :            :                               gsize           n_arg2)
      78                 :            : {
      79                 :            :   char *a1, *a2, *s;
      80         [ #  # ]:          0 :   a1 = arg1 ? egg_test_escape_data (arg1, n_arg1) : g_strdup ("NULL");
      81         [ #  # ]:          0 :   a2 = arg2 ? egg_test_escape_data (arg2, n_arg2) : g_strdup ("NULL");
      82                 :          0 :   s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
      83                 :          0 :   g_free (a1);
      84                 :          0 :   g_free (a2);
      85                 :          0 :   g_assertion_message (domain, file, line, func, s);
      86                 :          0 :   g_free (s);
      87                 :          0 : }
      88                 :            : 
      89                 :            : static void (*wait_stop_impl) (void);
      90                 :            : static gboolean (*wait_until_impl) (int timeout);
      91                 :            : 
      92                 :            : void
      93                 :         71 : egg_test_wait_stop (void)
      94                 :            : {
      95         [ -  + ]:         71 :         g_assert_nonnull (wait_stop_impl);
      96                 :         71 :         (wait_stop_impl) ();
      97                 :         71 : }
      98                 :            : 
      99                 :            : gboolean
     100                 :         72 : egg_test_wait_until (int timeout)
     101                 :            : {
     102         [ -  + ]:         72 :         g_assert_nonnull (wait_until_impl);
     103                 :         72 :         return (wait_until_impl) (timeout);
     104                 :            : }
     105                 :            : 
     106                 :            : void
     107                 :         50 : egg_test_wait_idle (void)
     108                 :            : {
     109                 :            :         GMainContext *context;
     110                 :            : 
     111         [ -  + ]:         50 :         g_assert_nonnull (wait_until_impl);
     112                 :            : 
     113                 :         50 :         context = g_main_context_get_thread_default ();
     114         [ +  + ]:        101 :         while (g_main_context_iteration (context, FALSE));
     115                 :         50 : }
     116                 :            : 
     117                 :            : static GMainLoop *wait_loop = NULL;
     118                 :            : 
     119                 :            : static void
     120                 :         71 : loop_wait_stop (void)
     121                 :            : {
     122         [ -  + ]:         71 :         g_assert_nonnull (wait_loop);
     123                 :         71 :         g_main_loop_quit (wait_loop);
     124                 :         71 : }
     125                 :            : 
     126                 :            : static gboolean
     127                 :          1 : on_loop_wait_timeout (gpointer data)
     128                 :            : {
     129                 :          1 :         gboolean *timed_out = data;
     130                 :          1 :         *timed_out = TRUE;
     131                 :            : 
     132         [ -  + ]:          1 :         g_assert_nonnull (wait_loop);
     133                 :          1 :         g_main_loop_quit (wait_loop);
     134                 :            : 
     135                 :          1 :         return TRUE; /* we remove this source later */
     136                 :            : }
     137                 :            : 
     138                 :            : static gboolean
     139                 :         72 : loop_wait_until (int timeout)
     140                 :            : {
     141                 :         72 :         gboolean timed_out = FALSE;
     142                 :            :         guint source;
     143                 :            : 
     144         [ -  + ]:         72 :         g_assert_null (wait_loop);
     145                 :         72 :         wait_loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
     146                 :            : 
     147                 :         72 :         source = g_timeout_add (timeout, on_loop_wait_timeout, &timed_out);
     148                 :            : 
     149                 :         72 :         g_main_loop_run (wait_loop);
     150                 :            : 
     151                 :         72 :         g_source_remove (source);
     152                 :         72 :         g_main_loop_unref (wait_loop);
     153                 :         72 :         wait_loop = NULL;
     154                 :         72 :         return !timed_out;
     155                 :            : }
     156                 :            : 
     157                 :            : gint
     158                 :         10 : egg_tests_run_with_loop (void)
     159                 :            : {
     160                 :            :         gint ret;
     161                 :            : 
     162                 :         10 :         wait_stop_impl = loop_wait_stop;
     163                 :         10 :         wait_until_impl = loop_wait_until;
     164                 :            : 
     165                 :         10 :         ret = g_test_run ();
     166                 :            : 
     167                 :         10 :         wait_stop_impl = NULL;
     168                 :         10 :         wait_until_impl = NULL;
     169                 :            : 
     170         [ -  + ]:         10 :         while (g_main_context_iteration (NULL, FALSE));
     171                 :            : 
     172                 :         10 :         return ret;
     173                 :            : }
     174                 :            : 
     175                 :            : void
     176                 :          1 : egg_tests_copy_scratch_file (const gchar *directory,
     177                 :            :                              const gchar *filename)
     178                 :            : {
     179                 :          1 :         GError *error = NULL;
     180                 :            :         gchar *basename;
     181                 :            :         gchar *contents;
     182                 :            :         gchar *destination;
     183                 :            :         gsize length;
     184                 :            : 
     185         [ -  + ]:          1 :         g_assert (directory);
     186                 :            : 
     187                 :          1 :         g_file_get_contents (filename, &contents, &length, &error);
     188         [ -  + ]:          1 :         g_assert_no_error (error);
     189                 :            : 
     190                 :          1 :         basename = g_path_get_basename (filename);
     191                 :          1 :         destination = g_build_filename (directory, basename, NULL);
     192                 :          1 :         g_free (basename);
     193                 :            : 
     194                 :          1 :         g_file_set_contents (destination, contents, length, &error);
     195         [ -  + ]:          1 :         g_assert_no_error (error);
     196                 :          1 :         g_free (destination);
     197                 :          1 :         g_free (contents);
     198                 :          1 : }
     199                 :            : 
     200                 :            : gchar *
     201                 :          7 : egg_tests_create_scratch_directory (const gchar *file_to_copy,
     202                 :            :                                     ...)
     203                 :            : {
     204                 :            :         gchar *basename;
     205                 :            :         gchar *directory;
     206                 :            :         va_list va;
     207                 :            : 
     208                 :          7 :         basename = g_path_get_basename (g_get_prgname ());
     209                 :          7 :         directory = g_strdup_printf ("/tmp/scratch-%s.XXXXXX", basename);
     210                 :          7 :         g_free (basename);
     211                 :            : 
     212         [ -  + ]:          7 :         if (!g_mkdtemp (directory))
     213                 :          0 :                 g_assert_not_reached ();
     214                 :            : 
     215                 :          7 :         va_start (va, file_to_copy);
     216                 :            : 
     217         [ +  + ]:          8 :         while (file_to_copy != NULL) {
     218                 :          1 :                 egg_tests_copy_scratch_file (directory, file_to_copy);
     219                 :          1 :                 file_to_copy = va_arg (va, const gchar *);
     220                 :            :         }
     221                 :            : 
     222                 :          7 :         va_end (va);
     223                 :            : 
     224                 :          7 :         return directory;
     225                 :            : }
     226                 :            : 
     227                 :            : void
     228                 :          7 : egg_tests_remove_scratch_directory (const gchar *directory)
     229                 :            : {
     230                 :          7 :         gchar *argv[] = { "rm", "-rf", (gchar *)directory, NULL };
     231                 :          7 :         GError *error = NULL;
     232                 :            :         gint rm_status;
     233                 :            : 
     234         [ -  + ]:          7 :         g_assert_cmpstr (directory, !=, "");
     235         [ -  + ]:          7 :         g_assert_cmpstr (directory, !=, "/");
     236                 :            : 
     237                 :          7 :         g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
     238                 :            :                       NULL, NULL, NULL, &rm_status, &error);
     239         [ -  + ]:          7 :         g_assert_no_error (error);
     240         [ -  + ]:          7 :         g_assert_cmpint (rm_status, ==, 0);
     241                 :          7 : }

Generated by: LCOV version 1.14