Debian Codegraph @

exec-tool/exec-tool-test.c

raw
1 /*
2 * Copyright (C) 2022 Guido Berhoerster <[email protected]>
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19
20 #include <glib.h>
21 #include <json-glib/json-glib.h>
22 #include <lomiri-app-launch.h>
23
24 #include "find-helper.h"
25
26 typedef struct {
27 gchar *tmpdir;
28 gchar *datafile;
29 } LpsFindHelperFixture;
30
31 static void
32 lps_find_helper_fixture_set_up(LpsFindHelperFixture *fixture,
33 gconstpointer user_data)
34 {
35 const gchar tmpl[] = "lps-find-helper-test-XXXXXX";
36 GError *error = NULL;
37 gchar *datadir;
38
39 fixture->tmpdir = g_dir_make_tmp(tmpl, &error);
40 if (fixture->tmpdir == NULL) {
41 g_error("failed to create temporary directory: %s",
42 error->message);
43 }
44 datadir = g_build_path("/", fixture->tmpdir, "lomiri-push-service",
45 NULL);
46 fixture->datafile = g_build_path("/", datadir, "helpers_data.json",
47 NULL);
48
49 if (g_mkdir_with_parents(datadir, 0700) < 0) {
50 g_error("failed to create %s: %s", datadir, g_strerror(errno));
51 }
52
53 g_setenv("XDG_DATA_HOME", fixture->tmpdir, TRUE);
54
55 g_free(datadir);
56 }
57
58 static void
59 lps_find_helper_fixture_tear_down(LpsFindHelperFixture *fixture,
60 gconstpointer user_data)
61 {
62 gchar *datadir;
63
64 unlink(fixture->datafile);
65 datadir = g_build_path("/", fixture->tmpdir, "lomiri-push-service",
66 NULL);
67 rmdir(datadir);
68
69 g_unsetenv("XDG_DATA_HOME");
70
71 g_free(datadir);
72 g_free(fixture->datafile);
73 g_free(fixture->tmpdir);
74 }
75
76 static void
77 lps_find_helper_fixture_create_datafile(LpsFindHelperFixture *fixture,
78 const char *content)
79 {
80 GError *error = NULL;
81
82 if (!g_file_set_contents(fixture->datafile, content, -1, &error)) {
83 g_error("%s", error->message);
84 }
85 }
86
87 static void
88 test_find_helper_basic(LpsFindHelperFixture *fixture, gconstpointer user_data)
89 {
90 gchar *package = NULL;
91 gchar *app = NULL;
92 gchar *helper = NULL;
93
94 lps_find_helper_fixture_create_datafile(fixture,
95 "{\"com.example.test\": {\"helper_id\": \"com.example.test_"
96 "test-helper_1\", \"exec\": \"tsthlpr\"}}");
97 g_assert_true(lomiri_app_launch_app_id_parse(
98 "com.example.test_test-app_1", &package, &app, NULL));
99 helper = find_helper(package, NULL, NULL);
100 g_assert_cmpstr(helper, ==, "tsthlpr");
101
102 g_free(app);
103 g_free(package);
104 g_free(helper);
105 }
106
107 static void
108 test_find_helper_finds_specific(LpsFindHelperFixture *fixture,
109 gconstpointer user_data)
110 {
111 gchar *package = NULL;
112 gchar *app = NULL;
113 gchar *helper = NULL;
114
115 lps_find_helper_fixture_create_datafile(fixture,
116 "{\"com.example.test_test-other-app\": {\"exec\": \"aaaaaaa\", "
117 "\"helper_id\": \"com.example.test_aaaa-helper_1\"}, "
118 "\"com.example.test_test-app\": {\"exec\": \"tsthlpr\", "
119 "\"helper_id\": \"com.example.test_test-helper_1\"}}");
120 g_assert_true(lomiri_app_launch_app_id_parse(
121 "com.example.test_test-app_1", &package, &app, NULL));
122 helper = find_helper(package, app, NULL);
123 g_assert_cmpstr(helper, ==, "tsthlpr");
124
125 g_free(app);
126 g_free(package);
127 g_free(helper);
128 }
129
130 static void
131 test_find_helper_can_fail(LpsFindHelperFixture *fixture,
132 gconstpointer user_data)
133 {
134 gchar *package = NULL;
135 gchar *app = NULL;
136 gchar *helper = NULL;
137 GError *error = NULL;
138
139 lps_find_helper_fixture_create_datafile(fixture,
140 "{\"com.example.test_test-other-app\": {\"exec\": \"aaaaaaa\", "
141 "\"helper_id\": \"com.example.test_aaaa-helper_1\"}}");
142 g_assert_true(lomiri_app_launch_app_id_parse(
143 "com.example.test_test-app_1", &package, &app, NULL));
144 helper = find_helper(package, app, &error);
145 g_assert_null(helper);
146 g_assert_error(error, LPS_FIND_HELPER_ERROR,
147 LPS_FIND_HELPER_ERROR_NO_HELPER);
148
149 g_error_free(error);
150 g_free(app);
151 g_free(package);
152 g_free(helper);
153 }
154
155 static void
156 test_find_helper_fail_invalid_json(LpsFindHelperFixture *fixture,
157 gconstpointer user_data)
158 {
159 gchar *package = NULL;
160 gchar *app = NULL;
161 gchar *helper = NULL;
162 GError *error = NULL;
163
164 lps_find_helper_fixture_create_datafile(fixture,
165 "{invalid json\"com.example.test_test-other-app\": {\"exec\": "
166 "\"aaaaaaa\", \"helper_id\": \"com.example.test_aaaa-helper_1\"}}");
167 g_assert_true(lomiri_app_launch_app_id_parse(
168 "com.example.test_test-app_1", &package, &app, NULL));
169 helper = find_helper(package, app, &error);
170 g_assert_null(helper);
171 g_assert_error(error, JSON_PARSER_ERROR,
172 JSON_PARSER_ERROR_INVALID_BAREWORD);
173
174 g_error_free(error);
175 g_free(app);
176 g_free(package);
177 g_free(helper);
178 }
179
180 static void
181 test_find_helper_fail_missing_exec(LpsFindHelperFixture *fixture,
182 gconstpointer user_data)
183 {
184 gchar *package = NULL;
185 gchar *app = NULL;
186 gchar *helper = NULL;
187 GError *error = NULL;
188
189 lps_find_helper_fixture_create_datafile(fixture,
190 "{\"com.example.test_test-app\": {\"helper_id\": "
191 "\"com.example.test_aaaa-helper_1\"}}");
192 g_assert_true(lomiri_app_launch_app_id_parse(
193 "com.example.test_test-app_1", &package, &app, NULL));
194 helper = find_helper(package, app, &error);
195 g_assert_null(helper);
196 g_assert_error(error, LPS_FIND_HELPER_ERROR,
197 LPS_FIND_HELPER_ERROR_NO_HELPER);
198
199 g_error_free(error);
200 g_free(app);
201 g_free(package);
202 g_free(helper);
203 }
204
205 int
206 main(int argc, char *argv[])
207 {
208 g_test_init (&argc, &argv, NULL);
209
210 g_test_add("/lps-find-helper/basic", LpsFindHelperFixture, NULL,
211 lps_find_helper_fixture_set_up, test_find_helper_basic,
212 lps_find_helper_fixture_tear_down);
213
214 g_test_add("/lps-find-helper/finds_specific", LpsFindHelperFixture,
215 NULL, lps_find_helper_fixture_set_up,
216 test_find_helper_finds_specific,
217 lps_find_helper_fixture_tear_down);
218
219 g_test_add("/lps-find-helper/can_fail", LpsFindHelperFixture, NULL,
220 lps_find_helper_fixture_set_up, test_find_helper_can_fail,
221 lps_find_helper_fixture_tear_down);
222
223 g_test_add("/lps-find-helper/fail_invalid_json", LpsFindHelperFixture,
224 NULL, lps_find_helper_fixture_set_up,
225 test_find_helper_fail_invalid_json,
226 lps_find_helper_fixture_tear_down);
227
228 g_test_add("/lps-find-helper/fail_missing_exec", LpsFindHelperFixture,
229 NULL, lps_find_helper_fixture_set_up,
230 test_find_helper_fail_missing_exec,
231 lps_find_helper_fixture_tear_down);
232
233 exit(g_test_run());
234 }