[libmediaart] cache: Improve _strip_invalid_entites() API



commit 4d3a06eb14b302c827b3af97b40c869aba0650be
Author: Martyn Russell <martyn lanedo com>
Date:   Fri Feb 7 16:11:28 2014 +0000

    cache: Improve _strip_invalid_entites() API
    
    Add more information to documentation.
    Add a check on the input string to make sure we avoid crashes.
    Add test case

 libmediaart/cache.c  |   10 +++++++++-
 tests/mediaarttest.c |   33 +++++++++++++++++++++++++++------
 2 files changed, 36 insertions(+), 7 deletions(-)
---
diff --git a/libmediaart/cache.c b/libmediaart/cache.c
index 774e2cf..2e29bae 100644
--- a/libmediaart/cache.c
+++ b/libmediaart/cache.c
@@ -94,7 +94,13 @@ media_art_strip_find_next_block (const gchar    *original,
  * This functions is used internally by media_art_get_file() and
  * media_art_get_path(). You will not normally need to call it yourself.
  *
- * Returns: copy of original but then stripped
+ * This function provides the following features:
+ * 1. Invalid characters include: ()[]<>{}_! #$^&*+=|\/"'?~;
+ * 2. Text inside brackets of (), {}, [] and <> pairs are removed.
+ * 3. Multiples of space characters are removed.
+ *
+ * Returns: @original stripped of invalid characters which must be
+ * freed. On error or if @original is empty, %NULL is returned.
  *
  * Since: 0.2.0
  */
@@ -118,6 +124,8 @@ media_art_strip_invalid_entities (const gchar *original)
                {  0,   0  }
        };
 
+       g_return_val_if_fail (original != NULL, NULL);
+
        str_no_blocks = g_string_new ("");
 
        p = original;
diff --git a/tests/mediaarttest.c b/tests/mediaarttest.c
index 967c911..77a5a42 100644
--- a/tests/mediaarttest.c
+++ b/tests/mediaarttest.c
@@ -99,10 +99,29 @@ test_mediaart_stripping (void)
 static void
 test_mediaart_stripping_null (void)
 {
-        // FIXME: Decide what is the expected behaviour here...
-        //   a. Return NULL
-        //   b. Return ""
-        //g_assert (!mediaart_strip_invalid_entities (NULL));
+}
+
+static void
+test_mediaart_stripping_failures_subprocess (void)
+{
+       g_assert (!media_art_strip_invalid_entities (NULL));
+}
+
+static void
+test_mediaart_stripping_failures (void)
+{
+       gchar *stripped = NULL;
+
+       /* a. Return NULL for NULL (subprocess)
+        * b. Return NULL for ""
+        */
+        stripped = media_art_strip_invalid_entities ("");
+        g_assert (stripped);
+        g_assert_cmpstr (stripped, ==, "");
+
+       g_test_trap_subprocess ("/mediaart/stripping_failures/subprocess", 0, 0);
+       g_test_trap_assert_failed ();
+       g_test_trap_assert_stderr ("*assertion 'original != NULL' failed*");
 }
 
 static void
@@ -283,8 +302,10 @@ main (int argc, char **argv)
                          test_mediaart_init);
         g_test_add_func ("/mediaart/stripping",
                          test_mediaart_stripping);
-        g_test_add_func ("/mediaart/stripping_null",
-                         test_mediaart_stripping_null);
+        g_test_add_func ("/mediaart/stripping_failures",
+                         test_mediaart_stripping_failures);
+        g_test_add_func ("/mediaart/stripping_failures/subprocess",
+                         test_mediaart_stripping_failures_subprocess);
         g_test_add_func ("/mediaart/location",
                          test_mediaart_location);
         g_test_add_func ("/mediaart/location_null",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]