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 cleanup_snapfiles (const gchar *path)
32 {
33 GDir *dir = NULL;
34 const gchar *entry;
35
36 dir = g_dir_open (path, 0, NULL);
37 if (dir == NULL)
38 {
39 /* Assume it’s a file. Ignore failure. */
40 (void) g_remove (path);
41 return;
42 }
43
44 while ((entry = g_dir_read_name (dir)) != NULL)
45 {
46 gchar *sub_path = g_build_filename (path, entry, NULL);
47 cleanup_snapfiles (sub_path);
48 g_free (sub_path);
49 }
50
51 g_dir_close (dir);
52
53 g_rmdir (path);
54 }
55
56 void
57 create_fake_snapctl (const char *path,
58 const char *supported_op)
59 {
60 GError *error = NULL;
61 char *snapctl_content;
62 char *snapctl;
63
64 snapctl = g_build_filename (path, "snapctl", NULL);
65 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 g_file_set_contents (snapctl, snapctl_content, -1, &error);
76 g_assert_no_error (error);
77 g_assert_cmpint (g_chmod (snapctl, 0500), ==, 0);
78
79 g_test_message ("Created snapctl in %s", snapctl);
80
81 g_clear_error (&error);
82 g_free (snapctl_content);
83 g_free (snapctl);
84 }
85
86 void
87 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 g_assert_nonnull (snap_path);
95
96 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 meta_path = g_build_filename (snap_path, "meta", NULL);
104 g_assert_cmpint (g_mkdir_with_parents (meta_path, 0700), ==, 0);
105
106 yaml_path = g_build_filename (meta_path, "snap.yaml", NULL);
107 g_file_set_contents (yaml_path, yaml_contents, -1, NULL);
108
109 g_test_message ("Created snap.yaml in %s", yaml_path);
110
111 g_free (meta_path);
112 g_free (yaml_path);
113 g_free (yaml_contents);
114 }
115
116 void
117 create_fake_flatpak_info_from_key_file (const char *root_path,
118 GKeyFile *key_file)
119 {
120 GError *error = NULL;
121 char *key_file_path;
122
123 g_assert_nonnull (root_path);
124
125 key_file_path = g_build_filename (root_path, ".flatpak-info", NULL);
126 g_test_message ("Creating .flatpak-info in %s", key_file_path);
127 g_key_file_save_to_file (key_file, key_file_path, &error);
128 g_assert_no_error (error);
129
130 g_free (key_file_path);
131 }
132
133 void
134 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 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 g_key_file_set_string (key_file, "Application", "name",
146 "org.gnome.GLib.Test.Flatpak");
147 g_key_file_set_string (key_file, "Application", "runtime",
148 "org.gnome.Platform/x86_64/44");
149 g_key_file_set_string (key_file, "Application", "sdk",
150 "org.gnome.Sdk/x86_64/44");
151
152 if (shared_context)
153 {
154 g_key_file_set_string_list (key_file, "Context", "shared",
155 (const char * const *) shared_context,
156 g_strv_length (shared_context));
157 }
158
159 if (dconf_dbus_policy)
160 {
161 g_key_file_set_string (key_file, "Session Bus Policy", "ca.desrt.dconf",
162 dconf_dbus_policy);
163 }
164
165 create_fake_flatpak_info_from_key_file (root_path, key_file);
166
167 g_key_file_free (key_file);
168 }