| 1 |
|
| 2 |
. |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 38 |
typedef struct |
| 39 |
{ |
| 40 |
OstreeRepo *parent_repo; |
| 41 |
GLnxTmpDir tmpdir; |
| 42 |
GFile *working_dir; |
| 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 |
|
| 68 |
(void)glnx_tmpdir_delete (&fixture->tmpdir, NULL, NULL); |
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 81 |
static void |
| 82 |
test_repo_finder_config_init (void) |
| 83 |
{ |
| 84 |
g_autoptr (OstreeRepoFinderConfig) finder = NULL; |
| 85 |
|
| 86 |
|
| 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 |
|
| 98 |
|
| 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; |
| 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 |
|
| 131 |
|
| 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 |
|
| 150 |
|
| 151 |
|
| 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 |
|
| 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 , ref_name, ref_name, |
| 190 |
NULL , 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 |
|
| 203 |
ostree_repo_regenerate_summary (repo, NULL , NULL, &error); |
| 204 |
g_assert_no_error (error); |
| 205 |
|
| 206 |
return g_file_get_uri (repo_path); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 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; |
| 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 |
|
| 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 |
|
| 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 |
|
| 263 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 346 |
|
| 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 |
} |