Debian Codegraph @

tests/test-repo-finder-config.c

raw symbols SCIP
1 /*
2 * Copyright © 2017 Endless Mobile, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.0+
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but 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 library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors:
20 * - Philip Withnall <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include <gio/gio.h>
26 #include <glib-object.h>
27 #include <glib.h>
28 #include <libglnx.h>
29 #include <locale.h>
30 #include <string.h>
31
32 #include "libostreetest.h"
33 #include "ostree-autocleanups.h"
34 #include "ostree-repo-finder-config.h"
35 #include "ostree-repo-finder.h"
36
37 /* Test fixture. Creates a temporary directory. */
38 typedef struct
39 {
40 OstreeRepo *parent_repo; /* owned */
41 GLnxTmpDir tmpdir; /* owned */
42 GFile *working_dir; /* owned */
43 } Fixture;
44
45 static void
46 setup (Fixture *fixture, gconstpointer test_data)
47 {
48 g_autoptr (GError) error = NULL;
49
50 (void)glnx_mkdtemp ("test-repo-finder-config-XXXXXX", 0700, &fixture->tmpdir, &error);
51 g_assert_no_error (error);
52
53 g_test_message ("Using temporary directory: %s", fixture->tmpdir.path);
54
55 glnx_shutil_mkdir_p_at (fixture->tmpdir.fd, "repo", 0700, NULL, &error);
56 g_assert_no_error (error);
57
58 fixture->working_dir = g_file_new_for_path (fixture->tmpdir.path);
59
60 fixture->parent_repo = ot_test_setup_repo (NULL, &error);
61 g_assert_no_error (error);
62 }
63
64 static void
65 teardown (Fixture *fixture, gconstpointer test_data)
66 {
67 /* Recursively remove the temporary directory. */
68 (void)glnx_tmpdir_delete (&fixture->tmpdir, NULL, NULL);
69
70 /* The repo also needs its source files to be removed. This is the inverse
71 * of setup_test_repository() in libtest.sh. */
72 int parent_repo_dfd = ostree_repo_get_dfd (fixture->parent_repo);
73 glnx_shutil_rm_rf_at (parent_repo_dfd, "../files", NULL, NULL);
74 glnx_shutil_rm_rf_at (parent_repo_dfd, "../repo", NULL, NULL);
75
76 g_clear_object (&fixture->working_dir);
77 g_clear_object (&fixture->parent_repo);
78 }
79
80 /* Test the object constructor works at a basic level. */
81 static void
82 test_repo_finder_config_init (void)
83 {
84 g_autoptr (OstreeRepoFinderConfig) finder = NULL;
85
86 /* Default everything. */
87 finder = ostree_repo_finder_config_new ();
88 }
89
90 static void
91 result_cb (GObject *source_object, GAsyncResult *result, gpointer user_data)
92 {
93 GAsyncResult **result_out = user_data;
94 *result_out = g_object_ref (result);
95 }
96
97 /* Test that no remotes are found if there are no config files in the refs
98 * directory. */
99 static void
100 test_repo_finder_config_no_configs (Fixture *fixture, gconstpointer test_data)
101 {
102 g_autoptr (OstreeRepoFinderConfig) finder = NULL;
103 g_autoptr (GMainContext) context = NULL;
104 g_autoptr (GAsyncResult) result = NULL;
105 g_autoptr (GPtrArray) results = NULL; /* (element-type OstreeRepoFinderResult) */
106 g_autoptr (GError) error = NULL;
107 const OstreeCollectionRef ref1 = { "org.example.Os", "exampleos/x86_64/standard" };
108 const OstreeCollectionRef ref2 = { "org.example.Os", "exampleos/x86_64/buildmain/standard" };
109 const OstreeCollectionRef *const refs[] = { &ref1, &ref2, NULL };
110
111 context = g_main_context_new ();
112 g_main_context_push_thread_default (context);
113
114 finder = ostree_repo_finder_config_new ();
115
116 ostree_repo_finder_resolve_async (OSTREE_REPO_FINDER (finder), refs, fixture->parent_repo, NULL,
117 result_cb, &result);
118
119 while (result == NULL)
120 g_main_context_iteration (context, TRUE);
121
122 results = ostree_repo_finder_resolve_finish (OSTREE_REPO_FINDER (finder), result, &error);
123 g_assert_no_error (error);
124 g_assert_nonnull (results);
125 g_assert_cmpuint (results->len, ==, 0);
126
127 g_main_context_pop_thread_default (context);
128 }
129
130 /* Add configuration for a remote named @remote_name, at @remote_uri, with a
131 * remote collection ID of @collection_id, to the given @repo. */
132 static void
133 assert_create_remote_config (OstreeRepo *repo, const gchar *remote_name, const gchar *remote_uri,
134 const gchar *collection_id)
135 {
136 g_autoptr (GError) error = NULL;
137 g_autoptr (GVariant) options = NULL;
138
139 if (collection_id != NULL)
140 options = g_variant_new_parsed ("@a{sv} { 'collection-id': <%s> }", collection_id);
141
142 ostree_repo_remote_add (repo, remote_name, remote_uri, options, NULL, &error);
143 g_assert_no_error (error);
144 }
145
146 static gchar *assert_create_remote (Fixture *fixture, const gchar *collection_id,
147 ...) G_GNUC_NULL_TERMINATED;
148
149 /* Create a new repository in a temporary directory with its collection ID set
150 * to @collection_id, and containing the refs given in @... (which must be
151 * %NULL-terminated). Return the `file://` URI of the new repository. */
152 static gchar *
153 assert_create_remote (Fixture *fixture, const gchar *collection_id, ...)
154 {
155 va_list args;
156 g_autoptr (GError) error = NULL;
157 const gchar *repo_name = (collection_id != NULL) ? collection_id : "no-collection";
158
159 glnx_shutil_mkdir_p_at (fixture->tmpdir.fd, repo_name, 0700, NULL, &error);
160 g_assert_no_error (error);
161
162 glnx_shutil_mkdir_p_at (fixture->tmpdir.fd, "empty", 0700, NULL, &error);
163 g_assert_no_error (error);
164
165 g_autoptr (GFile) repo_path = g_file_get_child (fixture->working_dir, repo_name);
166 g_autoptr (OstreeRepo) repo = ostree_repo_new (repo_path);
167 ostree_repo_set_collection_id (repo, collection_id, &error);
168 g_assert_no_error (error);
169 ostree_repo_create (repo, OSTREE_REPO_MODE_ARCHIVE, NULL, &error);
170 g_assert_no_error (error);
171
172 /* Set up the refs from @.... */
173 va_start (args, collection_id);
174
175 for (const gchar *ref_name = va_arg (args, const gchar *); ref_name != NULL;
176 ref_name = va_arg (args, const gchar *))
177 {
178 OstreeCollectionRef collection_ref = { (gchar *)collection_id, (gchar *)ref_name };
179 g_autofree gchar *checksum = NULL;
180 g_autoptr (OstreeMutableTree) mtree = NULL;
181 g_autoptr (OstreeRepoFile) repo_file = NULL;
182
183 mtree = ostree_mutable_tree_new ();
184 ostree_repo_write_dfd_to_mtree (repo, fixture->tmpdir.fd, "empty", mtree, NULL, NULL, &error);
185 g_assert_no_error (error);
186 ostree_repo_write_mtree (repo, mtree, (GFile **)&repo_file, NULL, &error);
187 g_assert_no_error (error);
188
189 ostree_repo_write_commit (repo, NULL /* no parent */, ref_name, ref_name,
190 NULL /* no metadata */, repo_file, &checksum, NULL, &error);
191 g_assert_no_error (error);
192
193 if (collection_id != NULL)
194 ostree_repo_set_collection_ref_immediate (repo, &collection_ref, checksum, NULL, &error);
195 else
196 ostree_repo_set_ref_immediate (repo, NULL, ref_name, checksum, NULL, &error);
197 g_assert_no_error (error);
198 }
199
200 va_end (args);
201
202 /* Update the summary. */
203 ostree_repo_regenerate_summary (repo, NULL /* no metadata */, NULL, &error);
204 g_assert_no_error (error);
205
206 return g_file_get_uri (repo_path);
207 }
208
209 /* Test resolving the refs against a collection of config files, which contain
210 * valid, invalid or duplicate repo information. */
211 static void
212 test_repo_finder_config_mixed_configs (Fixture *fixture, gconstpointer test_data)
213 {
214 g_autoptr (OstreeRepoFinderConfig) finder = NULL;
215 g_autoptr (GMainContext) context = NULL;
216 g_autoptr (GAsyncResult) async_result = NULL;
217 g_autoptr (GPtrArray) results = NULL; /* (element-type OstreeRepoFinderResult) */
218 g_autoptr (GError) error = NULL;
219 gsize i;
220 const OstreeCollectionRef ref0 = { "org.example.Collection0", "exampleos/x86_64/ref0" };
221 const OstreeCollectionRef ref1 = { "org.example.Collection0", "exampleos/x86_64/ref1" };
222 const OstreeCollectionRef ref2 = { "org.example.Collection1", "exampleos/x86_64/ref1" };
223 const OstreeCollectionRef ref3 = { "org.example.Collection1", "exampleos/x86_64/ref2" };
224 const OstreeCollectionRef ref4 = { "org.example.Collection2", "exampleos/x86_64/ref3" };
225 const OstreeCollectionRef *const refs[] = { &ref0, &ref1, &ref2, &ref3, &ref4, NULL };
226
227 context = g_main_context_new ();
228 g_main_context_push_thread_default (context);
229
230 /* Put together various ref configuration files. */
231 g_autofree gchar *collection0_uri = assert_create_remote (
232 fixture, "org.example.Collection0", "exampleos/x86_64/ref0", "exampleos/x86_64/ref1", NULL);
233 g_autofree gchar *collection1_uri
234 = assert_create_remote (fixture, "org.example.Collection1", "exampleos/x86_64/ref2", NULL);
235 g_autofree gchar *no_collection_uri
236 = assert_create_remote (fixture, NULL, "exampleos/x86_64/ref3", NULL);
237
238 assert_create_remote_config (fixture->parent_repo, "remote0", collection0_uri,
239 "org.example.Collection0");
240 assert_create_remote_config (fixture->parent_repo, "remote1", collection1_uri,
241 "org.example.Collection1");
242 assert_create_remote_config (fixture->parent_repo, "remote0-copy", collection0_uri,
243 "org.example.Collection0");
244 assert_create_remote_config (fixture->parent_repo, "remote1-bad-copy", collection1_uri,
245 "org.example.NotCollection1");
246 assert_create_remote_config (fixture->parent_repo, "remote2", no_collection_uri, NULL);
247
248 finder = ostree_repo_finder_config_new ();
249
250 /* Resolve the refs. */
251 ostree_repo_finder_resolve_async (OSTREE_REPO_FINDER (finder), refs, fixture->parent_repo, NULL,
252 result_cb, &async_result);
253
254 while (async_result == NULL)
255 g_main_context_iteration (context, TRUE);
256
257 results = ostree_repo_finder_resolve_finish (OSTREE_REPO_FINDER (finder), async_result, &error);
258 g_assert_no_error (error);
259 g_assert_nonnull (results);
260 g_assert_cmpuint (results->len, ==, 3);
261
262 /* Check that the results are correct: the invalid refs should have been
263 * ignored, and the valid results canonicalised and deduplicated. */
264 for (i = 0; i < results->len; i++)
265 {
266 const OstreeRepoFinderResult *result = g_ptr_array_index (results, i);
267
268 if (g_strcmp0 (ostree_remote_get_name (result->remote), "remote0") == 0
269 || g_strcmp0 (ostree_remote_get_name (result->remote), "remote0-copy") == 0)
270 {
271 g_assert_cmpuint (g_hash_table_size (result->ref_to_checksum), ==, 2);
272 g_assert_true (g_hash_table_contains (result->ref_to_checksum, &ref0));
273 g_assert_true (g_hash_table_contains (result->ref_to_checksum, &ref1));
274 g_assert_cmpstr (ostree_remote_get_url (result->remote), ==, collection0_uri);
275 }
276 else if (g_strcmp0 (ostree_remote_get_name (result->remote), "remote1") == 0)
277 {
278 g_assert_cmpuint (g_hash_table_size (result->ref_to_checksum), ==, 1);
279 g_assert_true (g_hash_table_contains (result->ref_to_checksum, &ref3));
280 g_assert_cmpstr (ostree_remote_get_url (result->remote), ==, collection1_uri);
281 }
282 else
283 {
284 g_assert_not_reached ();
285 }
286 }
287
288 g_main_context_pop_thread_default (context);
289 }
290
291 /* Test that using ostree_repo_find_remotes_async() works too.*/
292 static void
293 test_repo_finder_config_find_remotes (Fixture *fixture, gconstpointer test_data)
294 {
295 g_autoptr (GMainContext) context = NULL;
296 g_autoptr (GAsyncResult) result = NULL;
297 g_auto (OstreeRepoFinderResultv) results = NULL;
298 g_autoptr (GError) error = NULL;
299 gsize i;
300 const OstreeCollectionRef ref0 = { "org.example.Collection0", "exampleos/x86_64/ref0" };
301 const OstreeCollectionRef ref1 = { "org.example.Collection0", "exampleos/x86_64/ref1" };
302 const OstreeCollectionRef ref2 = { "org.example.Collection1", "exampleos/x86_64/ref1" };
303 const OstreeCollectionRef ref3 = { "org.example.Collection1", "exampleos/x86_64/ref2" };
304 const OstreeCollectionRef ref4 = { "org.example.Collection2", "exampleos/x86_64/ref3" };
305 const OstreeCollectionRef *const refs[] = { &ref0, &ref1, &ref2, &ref3, &ref4, NULL };
306 OstreeRepoFinder *finders[2] = {
307 NULL,
308 };
309
310 context = g_main_context_new ();
311 g_main_context_push_thread_default (context);
312
313 /* Put together various ref configuration files. */
314 g_autofree gchar *collection0_uri = assert_create_remote (
315 fixture, "org.example.Collection0", "exampleos/x86_64/ref0", "exampleos/x86_64/ref1", NULL);
316 g_autofree gchar *collection1_uri
317 = assert_create_remote (fixture, "org.example.Collection1", "exampleos/x86_64/ref2", NULL);
318 g_autofree gchar *no_collection_uri
319 = assert_create_remote (fixture, NULL, "exampleos/x86_64/ref3", NULL);
320
321 assert_create_remote_config (fixture->parent_repo, "remote0", collection0_uri,
322 "org.example.Collection0");
323 assert_create_remote_config (fixture->parent_repo, "remote1", collection1_uri,
324 "org.example.Collection1");
325 assert_create_remote_config (fixture->parent_repo, "remote0-copy", collection0_uri,
326 "org.example.Collection0");
327 assert_create_remote_config (fixture->parent_repo, "remote1-bad-copy", collection1_uri,
328 "org.example.NotCollection1");
329 assert_create_remote_config (fixture->parent_repo, "remote2", no_collection_uri, NULL);
330
331 finders[0] = OSTREE_REPO_FINDER (ostree_repo_finder_config_new ());
332
333 /* Resolve the refs. */
334 ostree_repo_find_remotes_async (fixture->parent_repo, refs, NULL, finders, NULL, NULL, result_cb,
335 &result);
336
337 while (result == NULL)
338 g_main_context_iteration (context, TRUE);
339
340 results = ostree_repo_find_remotes_finish (fixture->parent_repo, result, &error);
341 g_assert_no_error (error);
342 g_assert_nonnull (results);
343 g_assert_cmpuint (g_strv_length ((char **)results), ==, 3);
344
345 /* Check that the results are correct: the invalid refs should have been
346 * ignored, and the valid results canonicalised and deduplicated. */
347 for (i = 0; results[i] != NULL; i++)
348 {
349 const char *ref0_checksum, *ref1_checksum, *ref2_checksum, *ref3_checksum;
350 guint64 *ref0_timestamp, *ref1_timestamp, *ref2_timestamp, *ref3_timestamp;
351
352 if (g_strcmp0 (ostree_remote_get_name (results[i]->remote), "remote0") == 0
353 || g_strcmp0 (ostree_remote_get_name (results[i]->remote), "remote0-copy") == 0)
354 {
355 g_assert_cmpuint (g_hash_table_size (results[i]->ref_to_checksum), ==, 5);
356
357 ref0_checksum = g_hash_table_lookup (results[i]->ref_to_checksum, &ref0);
358 g_assert_true (ostree_validate_checksum_string (ref0_checksum, NULL));
359
360 ref1_checksum = g_hash_table_lookup (results[i]->ref_to_checksum, &ref1);
361 g_assert_true (ostree_validate_checksum_string (ref1_checksum, NULL));
362
363 ref2_checksum = g_hash_table_lookup (results[i]->ref_to_checksum, &ref2);
364 g_assert (ref2_checksum == NULL);
365
366 g_assert_cmpuint (g_hash_table_size (results[i]->ref_to_timestamp), ==, 5);
367
368 ref0_timestamp = g_hash_table_lookup (results[i]->ref_to_timestamp, &ref0);
369 *ref0_timestamp = GUINT64_FROM_BE (*ref0_timestamp);
370 g_assert_cmpuint (*ref0_timestamp, >, 0);
371
372 ref1_timestamp = g_hash_table_lookup (results[i]->ref_to_timestamp, &ref1);
373 *ref1_timestamp = GUINT64_FROM_BE (*ref1_timestamp);
374 g_assert_cmpuint (*ref1_timestamp, >, 0);
375
376 ref2_timestamp = g_hash_table_lookup (results[i]->ref_to_timestamp, &ref2);
377 *ref2_timestamp = GUINT64_FROM_BE (*ref2_timestamp);
378 g_assert_cmpuint (*ref2_timestamp, ==, 0);
379
380 g_assert_cmpstr (ostree_remote_get_url (results[i]->remote), ==, collection0_uri);
381 }
382 else if (g_strcmp0 (ostree_remote_get_name (results[i]->remote), "remote1") == 0)
383 {
384 g_assert_cmpuint (g_hash_table_size (results[i]->ref_to_checksum), ==, 5);
385
386 ref3_checksum = g_hash_table_lookup (results[i]->ref_to_checksum, &ref3);
387 g_assert_true (ostree_validate_checksum_string (ref3_checksum, NULL));
388
389 ref0_checksum = g_hash_table_lookup (results[i]->ref_to_checksum, &ref0);
390 g_assert (ref0_checksum == NULL);
391
392 g_assert_cmpuint (g_hash_table_size (results[i]->ref_to_timestamp), ==, 5);
393
394 ref3_timestamp = g_hash_table_lookup (results[i]->ref_to_timestamp, &ref3);
395 *ref3_timestamp = GUINT64_FROM_BE (*ref3_timestamp);
396 g_assert_cmpuint (*ref3_timestamp, >, 0);
397
398 ref0_timestamp = g_hash_table_lookup (results[i]->ref_to_timestamp, &ref0);
399 *ref0_timestamp = GUINT64_FROM_BE (*ref0_timestamp);
400 g_assert_cmpuint (*ref0_timestamp, ==, 0);
401
402 g_assert_cmpstr (ostree_remote_get_url (results[i]->remote), ==, collection1_uri);
403 }
404 else
405 {
406 g_assert_not_reached ();
407 }
408 }
409
410 g_main_context_pop_thread_default (context);
411 }
412
413 int
414 main (int argc, char **argv)
415 {
416 setlocale (LC_ALL, "");
417 g_test_init (&argc, &argv, NULL);
418
419 g_test_add_func ("/repo-finder-config/init", test_repo_finder_config_init);
420 g_test_add ("/repo-finder-config/no-configs", Fixture, NULL, setup,
421 test_repo_finder_config_no_configs, teardown);
422 g_test_add ("/repo-finder-config/mixed-configs", Fixture, NULL, setup,
423 test_repo_finder_config_mixed_configs, teardown);
424 g_test_add ("/repo-finder-config/find-remotes", Fixture, NULL, setup,
425 test_repo_finder_config_find_remotes, teardown);
426
427 return g_test_run ();
428 }