LCOV - code coverage report
Current view: top level - glib/gio/tests - portal-support-utils.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 58 58 100.0 %
Date: 2024-04-23 05:16:05 Functions: 5 5 100.0 %
Branches: 12 12 100.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * GIO - GLib Input, Output and Streaming Library
       3                 :            :  *
       4                 :            :  * Copyright (C) 2022 Canonical Ltd.
       5                 :            :  *
       6                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       7                 :            :  *
       8                 :            :  * This library is free software; you can redistribute it and/or
       9                 :            :  * modify it under the terms of the GNU Lesser General Public
      10                 :            :  * License as published by the Free Software Foundation; either
      11                 :            :  * version 2.1 of the License, or (at your option) any later version.
      12                 :            :  *
      13                 :            :  * This library is distributed in the hope that it will be useful,
      14                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16                 :            :  * Lesser General Public License for more details.
      17                 :            :  *
      18                 :            :  * You should have received a copy of the GNU Lesser General
      19                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      20                 :            :  *
      21                 :            :  * Author: Marco Trevisan <marco.trevisan@canonical.com>
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "portal-support-utils.h"
      25                 :            : 
      26                 :            : #include <glib.h>
      27                 :            : #include <glib/gstdio.h>
      28                 :            : 
      29                 :            : 
      30                 :            : void
      31                 :         96 : cleanup_snapfiles (const gchar *path)
      32                 :            : {
      33                 :         96 :   GDir *dir = NULL;
      34                 :            :   const gchar *entry;
      35                 :            : 
      36                 :         96 :   dir = g_dir_open (path, 0, NULL);
      37         [ +  + ]:         96 :   if (dir == NULL)
      38                 :            :     {
      39                 :            :       /* Assume it’s a file. Ignore failure. */
      40                 :         27 :       (void) g_remove (path);
      41                 :         27 :       return;
      42                 :            :     }
      43                 :            : 
      44         [ +  + ]:        145 :   while ((entry = g_dir_read_name (dir)) != NULL)
      45                 :            :     {
      46                 :         76 :       gchar *sub_path = g_build_filename (path, entry, NULL);
      47                 :         76 :       cleanup_snapfiles (sub_path);
      48                 :         76 :       g_free (sub_path);
      49                 :            :     }
      50                 :            : 
      51                 :         69 :   g_dir_close (dir);
      52                 :            : 
      53                 :         69 :   g_rmdir (path);
      54                 :            : }
      55                 :            : 
      56                 :            : void
      57                 :         14 : create_fake_snapctl (const char *path,
      58                 :            :                      const char *supported_op)
      59                 :            : {
      60                 :         14 :   GError *error = NULL;
      61                 :            :   char *snapctl_content;
      62                 :            :   char *snapctl;
      63                 :            : 
      64                 :         14 :   snapctl = g_build_filename (path, "snapctl", NULL);
      65         [ +  + ]:         14 :   snapctl_content = g_strdup_printf ("#!/bin/sh\n" \
      66                 :            :                                      "[ \"$1\" != 'is-connected' ] && exit 2\n"
      67                 :            :                                      "[ -z \"$2\" ] && exit 3\n"
      68                 :            :                                      "[ -n \"$3\" ] && exit 4\n"
      69                 :            :                                      "case \"$2\" in\n"
      70                 :            :                                      "  %s) exit 0;;\n"
      71                 :            :                                      "  *) exit 1;;\n"
      72                 :            :                                      "esac\n",
      73                 :            :                                      supported_op ? supported_op : "<invalid>");
      74                 :            : 
      75                 :         14 :   g_file_set_contents (snapctl, snapctl_content, -1, &error);
      76                 :         14 :   g_assert_no_error (error);
      77                 :         14 :   g_assert_cmpint (g_chmod (snapctl, 0500), ==, 0);
      78                 :            : 
      79                 :         14 :   g_test_message ("Created snapctl in %s", snapctl);
      80                 :            : 
      81                 :         14 :   g_clear_error (&error);
      82                 :         14 :   g_free (snapctl_content);
      83                 :         14 :   g_free (snapctl);
      84                 :         14 : }
      85                 :            : 
      86                 :            : void
      87                 :         11 : create_fake_snap_yaml (const char *snap_path,
      88                 :            :                        gboolean is_classic)
      89                 :            : {
      90                 :            :   char *meta_path;
      91                 :            :   char *yaml_path;
      92                 :            :   char *yaml_contents;
      93                 :            : 
      94                 :         11 :   g_assert_nonnull (snap_path);
      95                 :            : 
      96         [ +  + ]:         11 :   yaml_contents = g_strconcat ("name: glib-test-portal-support\n"
      97                 :            :                                "title: GLib Portal Support Test\n"
      98                 :            :                                "version: 2.76\n"
      99                 :            :                                "summary: Test it works\n",
     100                 :            :                                is_classic ?
     101                 :            :                                 "confinement: classic\n" : NULL, NULL);
     102                 :            : 
     103                 :         11 :   meta_path = g_build_filename (snap_path, "meta", NULL);
     104                 :         11 :   g_assert_cmpint (g_mkdir_with_parents (meta_path, 0700), ==, 0);
     105                 :            : 
     106                 :         11 :   yaml_path = g_build_filename (meta_path, "snap.yaml", NULL);
     107                 :         11 :   g_file_set_contents (yaml_path, yaml_contents, -1, NULL);
     108                 :            : 
     109                 :         11 :   g_test_message ("Created snap.yaml in %s", yaml_path);
     110                 :            : 
     111                 :         11 :   g_free (meta_path);
     112                 :         11 :   g_free (yaml_path);
     113                 :         11 :   g_free (yaml_contents);
     114                 :         11 : }
     115                 :            : 
     116                 :            : void
     117                 :          5 : create_fake_flatpak_info_from_key_file (const char *root_path,
     118                 :            :                                         GKeyFile   *key_file)
     119                 :            : {
     120                 :          5 :   GError *error = NULL;
     121                 :            :   char *key_file_path;
     122                 :            : 
     123                 :          5 :   g_assert_nonnull (root_path);
     124                 :            : 
     125                 :          5 :   key_file_path = g_build_filename (root_path, ".flatpak-info", NULL);
     126                 :          5 :   g_test_message ("Creating .flatpak-info in %s", key_file_path);
     127                 :          5 :   g_key_file_save_to_file (key_file, key_file_path, &error);
     128                 :          5 :   g_assert_no_error (error);
     129                 :            : 
     130                 :          5 :   g_free (key_file_path);
     131                 :          5 : }
     132                 :            : 
     133                 :            : void
     134                 :          5 : create_fake_flatpak_info (const char  *root_path,
     135                 :            :                           const GStrv shared_context,
     136                 :            :                           const char  *dconf_dbus_policy)
     137                 :            : {
     138                 :            :   GKeyFile *key_file;
     139                 :            : 
     140                 :          5 :   key_file = g_key_file_new ();
     141                 :            : 
     142                 :            :   /* File format is defined at:
     143                 :            :    *  https://docs.flatpak.org/en/latest/flatpak-command-reference.html
     144                 :            :    */
     145                 :          5 :   g_key_file_set_string (key_file, "Application", "name",
     146                 :            :                          "org.gnome.GLib.Test.Flatpak");
     147                 :          5 :   g_key_file_set_string (key_file, "Application", "runtime",
     148                 :            :                          "org.gnome.Platform/x86_64/44");
     149                 :          5 :   g_key_file_set_string (key_file, "Application", "sdk",
     150                 :            :                          "org.gnome.Sdk/x86_64/44");
     151                 :            : 
     152         [ +  + ]:          5 :   if (shared_context)
     153                 :            :     {
     154                 :          3 :       g_key_file_set_string_list (key_file, "Context", "shared",
     155                 :            :                                   (const char * const *) shared_context,
     156                 :          3 :                                   g_strv_length (shared_context));
     157                 :            :     }
     158                 :            : 
     159         [ +  + ]:          5 :   if (dconf_dbus_policy)
     160                 :            :     {
     161                 :          3 :       g_key_file_set_string (key_file, "Session Bus Policy", "ca.desrt.dconf",
     162                 :            :                              dconf_dbus_policy);
     163                 :            :     }
     164                 :            : 
     165                 :          5 :   create_fake_flatpak_info_from_key_file (root_path, key_file);
     166                 :            : 
     167                 :          5 :   g_key_file_free (key_file);
     168                 :          5 : }

Generated by: LCOV version 1.14