encoding-target: Ensure target names and categories are valid

This commit is contained in:
Edward Hervey 2010-12-21 16:33:50 +01:00
parent 6ffabccf04
commit 4b3e1403a1
2 changed files with 135 additions and 0 deletions

View file

@ -19,6 +19,7 @@
*/
#include <locale.h>
#include <string.h>
#include "encoding-target.h"
/*
@ -150,6 +151,40 @@ gst_encoding_target_get_profiles (GstEncodingTarget * target)
return target->profiles;
}
static inline gboolean
validate_name (const gchar * name)
{
guint i, len;
len = strlen (name);
if (len == 0)
return FALSE;
/* First character can only be a lower case ASCII character */
if (!g_ascii_isalpha (name[0]) || !g_ascii_islower (name[0]))
return FALSE;
/* All following characters can only by:
* either a lower case ASCII character
* or an hyphen
* or a numeric */
for (i = 1; i < len; i++) {
/* if uppercase ASCII letter, return */
if (g_ascii_isupper (name[i]))
return FALSE;
/* if a digit, continue */
if (g_ascii_isdigit (name[i]))
continue;
/* if an hyphen, continue */
if (name[i] == '-')
continue;
/* remaining should only be ascii letters */
if (!g_ascii_isalpha (name[i]))
return FALSE;
}
return TRUE;
}
/**
* gst_encoding_target_new:
@ -160,6 +195,10 @@ gst_encoding_target_get_profiles (GstEncodingTarget * target)
*
* Creates a new #GstEncodingTarget.
*
* The name and category can only consist of lowercase ASCII letters for the
* first character, followed by either lowercase ASCII letters, digits or
* hyphens ('-').
*
* Since: 0.10.32
*
* Returns: The newly created #GstEncodingTarget or %NULL if there was an
@ -176,6 +215,12 @@ gst_encoding_target_new (const gchar * name, const gchar * category,
g_return_val_if_fail (category != NULL, NULL);
g_return_val_if_fail (description != NULL, NULL);
/* Validate name */
if (!validate_name (name))
goto invalid_name;
if (!validate_name (category))
goto invalid_category;
res = (GstEncodingTarget *) gst_mini_object_new (GST_TYPE_ENCODING_TARGET);
res->name = g_strdup (name);
res->category = g_strdup (category);
@ -190,6 +235,18 @@ gst_encoding_target_new (const gchar * name, const gchar * category,
}
return res;
invalid_name:
{
GST_ERROR ("Invalid name for encoding category : '%s'", name);
return NULL;
}
invalid_category:
{
GST_ERROR ("Invalid category for encoding category : '%s'", category);
return NULL;
}
}
/**

View file

@ -132,6 +132,83 @@ GST_START_TEST (test_profile_output_caps)
GST_END_TEST;
GST_START_TEST (test_target_naming)
{
GstEncodingTarget *target;
/* NULL values */
ASSERT_CRITICAL (target = gst_encoding_target_new (NULL, NULL, NULL, NULL));
fail_if (target != NULL);
ASSERT_CRITICAL (target =
gst_encoding_target_new ("donkey", NULL, NULL, NULL));
fail_if (target != NULL);
ASSERT_CRITICAL (target =
gst_encoding_target_new (NULL, "donkey", NULL, NULL));
fail_if (target != NULL);
ASSERT_CRITICAL (target =
gst_encoding_target_new (NULL, NULL, "donkey", NULL));
fail_if (target != NULL);
/* Name and Category validation */
/* empty non-NULL strings */
fail_if (gst_encoding_target_new ("", "valid", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("valid", "", "description", NULL) != NULL);
/* don't start with a lower case ASCII character */
fail_if (gst_encoding_target_new ("A", "valid", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("3", "valid", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("-", "valid", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("!", "valid", "description", NULL) != NULL);
fail_if (gst_encoding_target_new (" ", "valid", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("valid", "A", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("valid", "3", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("valid", "-", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("valid", "!", "description", NULL) != NULL);
fail_if (gst_encoding_target_new ("valid", " ", "description", NULL) != NULL);
/* Starting with anything else is valid */
target = gst_encoding_target_new ("a", "valid", "description", NULL);
fail_if (target == NULL);
gst_encoding_target_unref (target);
target = gst_encoding_target_new ("z", "valid", "description", NULL);
fail_if (target == NULL);
gst_encoding_target_unref (target);
target = gst_encoding_target_new ("valid", "a", "description", NULL);
fail_if (target == NULL);
gst_encoding_target_unref (target);
target = gst_encoding_target_new ("valid", "z", "description", NULL);
fail_if (target == NULL);
gst_encoding_target_unref (target);
/* only inner valid characters are lower-case ASCII letters *OR* digits *OR* hyphens */
fail_if (gst_encoding_target_new ("aA", "valid", "description",
NULL) != NULL);
fail_if (gst_encoding_target_new ("a!", "valid", "description",
NULL) != NULL);
fail_if (gst_encoding_target_new ("space donkeys", "valid", "description",
NULL) != NULL);
fail_if (gst_encoding_target_new ("howaboutùnicode", "valid", "description",
NULL) != NULL);
fail_if (gst_encoding_target_new ("valid", "aA", "description",
NULL) != NULL);
fail_if (gst_encoding_target_new ("valid", "a!", "description",
NULL) != NULL);
target =
gst_encoding_target_new ("donkey-4-ever", "valid", "description", NULL);
fail_if (target == NULL);
gst_encoding_target_unref (target);
target =
gst_encoding_target_new ("valid", "donkey-4-ever", "description", NULL);
fail_if (target == NULL);
gst_encoding_target_unref (target);
}
GST_END_TEST;
static GstEncodingTarget *
create_saveload_target (void)
{
@ -395,6 +472,7 @@ profile_suite (void)
tcase_add_test (tc_chain, test_profile_creation);
tcase_add_test (tc_chain, test_profile_output_caps);
tcase_add_test (tc_chain, test_target_naming);
if (can_write) {
tcase_add_test (tc_chain, test_loading_profile);
tcase_add_test (tc_chain, test_saving_profile);