From 6a21d0ca1071fc225ef11b4cee57be3c2a62264b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Thu, 25 Nov 2021 12:29:29 +0000 Subject: osinfo: honour tree architecture when matching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tree matching code currently ignores the tree architecture which is generally ok, since osinfo_tree_create_from_location will leave it set to NULL. None the less, if an architecture is provided for the unknown tree, we should only return results that match this architecture. Signed-off-by: Daniel P. Berrangé --- osinfo/osinfo_tree.c | 14 ++++++++---- tests/dbdata/os/libosinfo.org/test-tree-arm.xml | 29 ++++++++++++++++++++++++ tests/test-db.c | 30 ++++++++++++++++++++----- tests/test-tree.c | 8 +++++++ 4 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 tests/dbdata/os/libosinfo.org/test-tree-arm.xml diff --git a/osinfo/osinfo_tree.c b/osinfo/osinfo_tree.c index cedeadc..60deb52 100644 --- a/osinfo/osinfo_tree.c +++ b/osinfo/osinfo_tree.c @@ -463,9 +463,10 @@ OsinfoTree *osinfo_tree_new(const gchar *id, "id", id, NULL); - osinfo_entity_set_param(OSINFO_ENTITY(tree), - OSINFO_TREE_PROP_ARCHITECTURE, - architecture); + if (architecture) + osinfo_entity_set_param(OSINFO_ENTITY(tree), + OSINFO_TREE_PROP_ARCHITECTURE, + architecture); return tree; } @@ -1203,11 +1204,13 @@ OsinfoTree *osinfo_tree_create_from_treeinfo(const gchar *treeinfo, */ gboolean osinfo_tree_matches(OsinfoTree *tree, OsinfoTree *reference) { + const gchar *tree_arch = osinfo_tree_get_architecture(tree); const gchar *tree_treeinfo_family = osinfo_tree_get_treeinfo_family(tree); const gchar *tree_treeinfo_variant = osinfo_tree_get_treeinfo_variant(tree); const gchar *tree_treeinfo_version = osinfo_tree_get_treeinfo_version(tree); const gchar *tree_treeinfo_arch = osinfo_tree_get_treeinfo_arch(tree); + const gchar *reference_arch = osinfo_tree_get_architecture(reference); const gchar *reference_treeinfo_family = osinfo_tree_get_treeinfo_family(reference); const gchar *reference_treeinfo_variant = osinfo_tree_get_treeinfo_variant(reference); const gchar *reference_treeinfo_version = osinfo_tree_get_treeinfo_version(reference); @@ -1216,7 +1219,10 @@ gboolean osinfo_tree_matches(OsinfoTree *tree, OsinfoTree *reference) if (!osinfo_tree_has_treeinfo(reference)) return FALSE; - if (match_regex(reference_treeinfo_family, tree_treeinfo_family) && + if ((!tree_arch || + g_str_equal(reference_arch, tree_arch) || + g_str_equal(reference_arch, "all")) && + match_regex(reference_treeinfo_family, tree_treeinfo_family) && match_regex(reference_treeinfo_variant, tree_treeinfo_variant) && match_regex(reference_treeinfo_version, tree_treeinfo_version) && match_regex(reference_treeinfo_arch, tree_treeinfo_arch)) diff --git a/tests/dbdata/os/libosinfo.org/test-tree-arm.xml b/tests/dbdata/os/libosinfo.org/test-tree-arm.xml new file mode 100644 index 0000000..93162a8 --- /dev/null +++ b/tests/dbdata/os/libosinfo.org/test-tree-arm.xml @@ -0,0 +1,29 @@ + + + + tree-arm + TreeArm + unknown + libosinfo.org + test + + + http://libosinfo.org/tree/v7 + + Tree + unknown + arm + + + + + http://libosinfo.org/tree/v6 + + Tree + unknown + arm + + + + + diff --git a/tests/test-db.c b/tests/test-db.c index ff645c7..32c2835 100644 --- a/tests/test-db.c +++ b/tests/test-db.c @@ -530,7 +530,7 @@ test_identify_media(void) static OsinfoTree * -create_tree(const gchar *arch, gboolean set_treeinfo_arch) +create_tree(const gchar *arch, const gchar *treeinfo_arch) { OsinfoTree *tree; @@ -541,10 +541,10 @@ create_tree(const gchar *arch, gboolean set_treeinfo_arch) osinfo_entity_set_param(OSINFO_ENTITY(tree), OSINFO_TREE_PROP_TREEINFO_VERSION, "unknown"); - if (set_treeinfo_arch) + if (treeinfo_arch) osinfo_entity_set_param(OSINFO_ENTITY(tree), - OSINFO_TREE_PROP_TREEINFO_ARCH, - arch); + OSINFO_TREE_PROP_TREEINFO_ARCH, + treeinfo_arch); return tree; } @@ -564,17 +564,35 @@ test_identify_tree(void) db = osinfo_loader_get_db(loader); /* Matching against an "all" architecture" */ - tree = create_tree("x86_64", FALSE); + tree = create_tree("x86_64", NULL); g_assert_true(osinfo_db_identify_tree(db, tree)); g_assert_cmpstr(osinfo_tree_get_architecture(tree), ==, "all"); g_object_unref(tree); /* Matching against a known architecture, which has to have precedence */ - tree = create_tree("i686", TRUE); + tree = create_tree("i686", "i686"); g_assert_true(osinfo_db_identify_tree(db, tree)); g_assert_cmpstr(osinfo_tree_get_architecture(tree), ==, "i686"); g_object_unref(tree); + /* Matching against a known architecture, which has to have precedence */ + tree = create_tree(NULL, "i686"); + g_assert_true(osinfo_db_identify_tree(db, tree)); + g_assert_cmpstr(osinfo_tree_get_architecture(tree), ==, "i686"); + g_object_unref(tree); + + /* Should not match a tree tagged with different arch, even + * if treeinfo matches */ + tree = create_tree("armv7hl", "arm"); + g_assert_true(osinfo_db_identify_tree(db, tree)); + g_assert_cmpstr(osinfo_tree_get_url(tree), ==, "http://libosinfo.org/tree/v7"); + g_object_unref(tree); + + tree = create_tree("armv6", "arm"); + g_assert_true(osinfo_db_identify_tree(db, tree)); + g_assert_cmpstr(osinfo_tree_get_url(tree), ==, "http://libosinfo.org/tree/v6"); + g_object_unref(tree); + g_object_unref(loader); } diff --git a/tests/test-tree.c b/tests/test-tree.c index 88adccd..79d6e74 100644 --- a/tests/test-tree.c +++ b/tests/test-tree.c @@ -172,9 +172,17 @@ test_matching(void) "(Server|Workstation)", "3[0-9]", NULL); + /* Mis-match on arch */ + OsinfoTree *reference4 = test_create_tree("https://fedoraproject.org/fedora/35/tree1", + "i686", + "Fedora", + NULL, + NULL, + NULL); g_assert(osinfo_tree_matches(unknown, reference1)); g_assert(!osinfo_tree_matches(unknown, reference2)); g_assert(osinfo_tree_matches(unknown, reference3)); + g_assert(!osinfo_tree_matches(unknown, reference4)); } int -- cgit v1.2.1