summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2014-08-11 10:08:41 +0100
committerRichard Hughes <richard@hughsie.com>2014-08-11 10:08:55 +0100
commit415325d3342576b220a9a50ba681ded72e1c7925 (patch)
tree523f1cb902ac7a26c78ea6e3ee1c55d42648dee1
parent65ce58938ccc2364fd84573b55db55855f850d63 (diff)
downloadappstream-glib-415325d3342576b220a9a50ba681ded72e1c7925.tar.gz
Metadata licenses like 'CC0 and CC-BY-3.0' are valid content licenses
To validate this, tokenize the string (which automatically converts the legacy ID's) and check each string.
-rw-r--r--data/tests/success.appdata.xml2
-rw-r--r--libappstream-glib/as-app-validate.c39
-rw-r--r--libappstream-glib/as-app.c42
-rw-r--r--libappstream-glib/as-self-test.c2
4 files changed, 35 insertions, 50 deletions
diff --git a/data/tests/success.appdata.xml b/data/tests/success.appdata.xml
index 5deaf83..7794428 100644
--- a/data/tests/success.appdata.xml
+++ b/data/tests/success.appdata.xml
@@ -2,7 +2,7 @@
<!-- Copyright 2013 Richard Hughes <richard@hughsie.com> -->
<application>
<id type="desktop">gnome-power-statistics.desktop</id>
- <metadata_license>CC0</metadata_license>
+ <metadata_license>CC0 and CC-BY-3.0</metadata_license>
<project_license>GPL-2.0+ and GFDL-1.3</project_license>
<name>0 A.D.</name>
<summary>Observe power management</summary>
diff --git a/libappstream-glib/as-app-validate.c b/libappstream-glib/as-app-validate.c
index 8f4b940..6f54255 100644
--- a/libappstream-glib/as-app-validate.c
+++ b/libappstream-glib/as-app-validate.c
@@ -870,6 +870,31 @@ as_app_validate_license (const gchar *license_text, GError **error)
}
/**
+ * as_app_validate_is_content_license:
+ **/
+static gboolean
+as_app_validate_is_content_license (const gchar *license)
+{
+ guint i;
+ _cleanup_strv_free_ gchar **tokens = NULL;
+ tokens = as_utils_spdx_license_tokenize (license);
+ for (i = 0; tokens[i] != NULL; i++) {
+ if (g_strcmp0 (tokens[i], "CC0-1.0") == 0)
+ continue;
+ if (g_strcmp0 (tokens[i], "CC-BY-3.0") == 0)
+ continue;
+ if (g_strcmp0 (tokens[i], "CC-BY-SA-3.0") == 0)
+ continue;
+ if (g_strcmp0 (tokens[i], "GFDL-1.3") == 0)
+ continue;
+ if (g_strcmp0 (tokens[i], "# and ") == 0)
+ continue;
+ return FALSE;
+ }
+ return TRUE;
+}
+
+/**
* as_app_validate:
* @app: a #AsApp instance.
* @flags: the #AsAppValidateFlags to use, e.g. %AS_APP_VALIDATE_FLAG_NONE
@@ -999,15 +1024,11 @@ as_app_validate (AsApp *app, AsAppValidateFlags flags, GError **error)
/* metadata_license */
license = as_app_get_metadata_license (app);
if (license != NULL) {
- if (require_content_license) {
- if (g_strcmp0 (license, "CC0-1.0") != 0 &&
- g_strcmp0 (license, "CC-BY-3.0") != 0 &&
- g_strcmp0 (license, "CC-BY-SA-3.0") != 0 &&
- g_strcmp0 (license, "GFDL-1.3") != 0) {
- ai_app_validate_add (probs,
- AS_PROBLEM_KIND_TAG_INVALID,
- "<metadata_license> is not valid");
- }
+ if (require_content_license &&
+ !as_app_validate_is_content_license (license)) {
+ ai_app_validate_add (probs,
+ AS_PROBLEM_KIND_TAG_INVALID,
+ "<metadata_license> is not valid");
} else if (validate_license) {
ret = as_app_validate_license (license, &error_local);
if (!ret) {
diff --git a/libappstream-glib/as-app.c b/libappstream-glib/as-app.c
index 2b5c3e2..64268cc 100644
--- a/libappstream-glib/as-app.c
+++ b/libappstream-glib/as-app.c
@@ -1420,22 +1420,6 @@ as_app_set_project_license (AsApp *app,
}
/**
- * as_strncmp:
- **/
-static gint
-as_strncmp (const gchar *s1, const gchar *s2, gssize n)
-{
- if (n < 0) return g_strcmp0 (s1, s2);
- if (s1 == NULL && s2 == NULL)
- return 0;
- if (s1 != NULL && s2 == NULL)
- return 1;
- if (s1 == NULL && s2 != NULL)
- return -1;
- return strncmp (s1, s2, n);
-}
-
-/**
* as_app_set_metadata_license:
* @app: a #AsApp instance.
* @metadata_license: the project license string.
@@ -1451,18 +1435,7 @@ as_app_set_metadata_license (AsApp *app,
gssize metadata_license_len)
{
AsAppPrivate *priv = GET_PRIVATE (app);
- guint i;
- struct {
- const gchar *old;
- const gchar *new;
- } licenses[] = {
- { "CC0", "CC0-1.0" },
- { "CC-BY", "CC-BY-3.0" },
- { "CC-BY-SA", "CC-BY-SA-3.0" },
- { "GFDL", "GFDL-1.3" },
- { "GPL-2", "GPL-2.0" },
- { "GPL-3", "GPL-3.0" },
- { NULL, NULL } };
+ _cleanup_strv_free_ gchar **tokens = NULL;
/* handle untrusted */
if ((priv->trust_flags & AS_APP_TRUST_FLAG_CHECK_VALID_UTF8) > 0 &&
@@ -1472,18 +1445,9 @@ as_app_set_metadata_license (AsApp *app,
}
/* automatically replace deprecated license names */
- for (i = 0; licenses[i].old != NULL; i++) {
- if (as_strncmp (metadata_license,
- licenses[i].old,
- metadata_license_len) == 0) {
- metadata_license = licenses[i].new;
- metadata_license_len = -1;
- break;
- }
- }
-
g_free (priv->metadata_license);
- priv->metadata_license = as_strndup (metadata_license, metadata_license_len);
+ tokens = as_utils_spdx_license_tokenize (metadata_license);
+ priv->metadata_license = as_utils_spdx_license_detokenize (tokens);
}
/**
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c
index 948397a..c965907 100644
--- a/libappstream-glib/as-self-test.c
+++ b/libappstream-glib/as-self-test.c
@@ -676,7 +676,7 @@ as_test_app_validate_appdata_good_func (void)
g_assert_cmpstr (as_app_get_id_full (app), ==, "gnome-power-statistics.desktop");
g_assert_cmpstr (as_app_get_name (app, "C"), ==, "0 A.D.");
g_assert_cmpstr (as_app_get_comment (app, "C"), ==, "Observe power management");
- g_assert_cmpstr (as_app_get_metadata_license (app), ==, "CC0-1.0");
+ g_assert_cmpstr (as_app_get_metadata_license (app), ==, "CC0-1.0 and CC-BY-3.0");
g_assert_cmpstr (as_app_get_update_contact (app), ==, "richard@hughsie.com");
g_assert_cmpstr (as_app_get_project_group (app), ==, "GNOME");
g_assert_cmpstr (as_app_get_url_item (app, AS_URL_KIND_HOMEPAGE), ==,