[gmime] Updated text/enriched filter and added unit tests



commit bb986e5d534b4a02cf16b82c21e8428e72b0a480
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Sat Dec 16 14:46:23 2017 -0500

    Updated text/enriched filter and added unit tests

 gmime/gmime-filter-enriched.c    |   51 ++++++++++++++++---------------------
 tests/data/filters/enriched.html |    1 +
 tests/data/filters/enriched.txt  |    7 +++++
 tests/test-filters.c             |   49 ++++++++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+), 29 deletions(-)
---
diff --git a/gmime/gmime-filter-enriched.c b/gmime/gmime-filter-enriched.c
index 5064ad4..d3cb3b3 100644
--- a/gmime/gmime-filter-enriched.c
+++ b/gmime/gmime-filter-enriched.c
@@ -45,6 +45,7 @@
 
 typedef char * (*EnrichedParamParser) (const char *inptr, size_t inlen);
 
+static char *param_parse_paraindent (const char *inptr, size_t inlen);
 static char *param_parse_colour (const char *inptr, size_t inlen);
 static char *param_parse_font (const char *inptr, size_t inlen);
 static char *param_parse_lang (const char *inptr, size_t inlen);
@@ -89,16 +90,17 @@ static struct {
        { "/lang",       "</span>",             FALSE, NULL               },
        
        /* don't handle this tag yet... */
-       { "paraindent",  "<!-- ",               /* TRUE */ FALSE, NULL    },
-       { "/paraindent", " -->",                FALSE, NULL               },
+       { "paraindent",  "<p style=\"%s\">",    TRUE,  param_parse_paraindent },
+       { "/paraindent", "</p>",                FALSE, NULL              },
        
        /* as soon as we support all the tags that can have a param
         * tag argument, these should be unnecessary, but we'll keep
         * them anyway just in case? */
-       { "param",       "<!-- ",               FALSE, NULL               },
+       { "param",       "<!-- param:",         FALSE, NULL               },
        { "/param",      " -->",                FALSE, NULL               },
 };
 
+#define PARAM_TAG_MIN_LEN  (sizeof ("<param>") + sizeof ("</param>") - 2)
 
 static void g_mime_filter_enriched_class_init (GMimeFilterEnrichedClass *klass);
 static void g_mime_filter_enriched_init       (GMimeFilterEnriched *filter, GMimeFilterEnrichedClass *klass);
@@ -191,54 +193,46 @@ enriched_tag_needs_param (const char *tag)
 }
 #endif
 
+static char *
+param_parse_paraindent (const char *inptr, size_t inlen)
+{
+       return g_strdup ("text-indent:40px");
+}
+
 static const char *valid_colours[] = {
        "red", "green", "blue", "yellow", "cyan", "magenta", "black", "white"
 };
 
-#define NUM_VALID_COLOURS  (sizeof (valid_colours) / sizeof (valid_colours[0]))
-
 static char *
-param_parse_colour (const char *inptr, size_t inlen)
+param_parse_colour (const char *in, size_t inlen)
 {
-       const char *inend, *end;
+       const char *inend = in + inlen;
+       const char *inptr = in;
+       const char *end;
        guint32 rgb = 0;
        guint v, i;
        
-       for (i = 0; i < NUM_VALID_COLOURS; i++) {
-               if (!g_ascii_strncasecmp (inptr, valid_colours[i], inlen))
+       for (i = 0; i < G_N_ELEMENTS (valid_colours); i++) {
+               size_t n = strlen (valid_colours[i]);
+               
+               if (inlen == n && !g_ascii_strncasecmp (inptr, valid_colours[i], n))
                        return g_strdup (valid_colours[i]);
        }
        
        /* check for numeric r/g/b in the format: ####,####,#### */
-       if (inptr[4] != ',' || inptr[9] != ',') {
-               /* okay, mailer must have used a string name that
-                * rfc1896 did not specify? do some simple scanning
-                * action, a colour name MUST be [a-zA-Z] */
-               end = inptr;
-               inend = inptr + inlen;
-               while (end < inend && ((*end >= 'a' && *end <= 'z') || (*end >= 'A' && *end <= 'Z')))
-                       end++;
-               
-               return g_strndup (inptr, (size_t) (end - inptr));
-       }
-       
        for (i = 0; i < 3; i++) {
                v = strtoul (inptr, (char **) &end, 16);
-               if (end != inptr + 4)
-                       goto invalid_format;
+               if (end != inptr + 4 || (i < 2 && *end != ',') || (i == 2 && end != inend))
+                       return g_strndup (in, inlen);
                
                v >>= 8;
+               
                rgb = (rgb << 8) | (v & 0xff);
                
                inptr += 5;
        }
        
        return g_strdup_printf ("#%.6X", rgb);
-       
- invalid_format:
-       
-       /* default colour? */
-       return g_strdup ("black");
 }
 
 static char *
@@ -454,7 +448,6 @@ enriched_to_html (GMimeFilter *filter, char *in, size_t inlen, size_t prespace,
                                                while (inptr < inend && *inptr != '<')
                                                        inptr++;
                                                
-#define PARAM_TAG_MIN_LEN  (sizeof ("<param>") + sizeof ("</param>") - 1)
                                                if (inptr == inend || (size_t) (inend - inptr) <= 
PARAM_TAG_MIN_LEN) {
                                                        inptr = tag - 1;
                                                        goto need_input;
diff --git a/tests/data/filters/enriched.html b/tests/data/filters/enriched.html
new file mode 100644
index 0000000..8fd8d68
--- /dev/null
+++ b/tests/data/filters/enriched.html
@@ -0,0 +1 @@
+<b>Hello, <i>world!</i></b><br><font color="red">Blood</font> is <b>thicker</b> than <font 
color="#0000FF">water</font>.<br><p style="text-indent:40px"><i>-- Well-known proverb </i></p>
\ No newline at end of file
diff --git a/tests/data/filters/enriched.txt b/tests/data/filters/enriched.txt
new file mode 100644
index 0000000..82114e7
--- /dev/null
+++ b/tests/data/filters/enriched.txt
@@ -0,0 +1,7 @@
+<bold>Hello, <italic>world!</italic></bold>
+
+<color><param>red</param>Blood</color> is <bold>thicker</bold> than
+<color><param>0000,0000,ffff</param>water</color>.
+
+<paraindent><param>left</param><italic>-- Well-known proverb
+</italic></paraindent>
diff --git a/tests/test-filters.c b/tests/test-filters.c
index 74b1fea..8423c69 100644
--- a/tests/test-filters.c
+++ b/tests/test-filters.c
@@ -154,6 +154,53 @@ error:
 }
 
 static void
+test_enriched (const char *datadir, const char *input, const char *output)
+{
+       const char *what = "GMimeFilterEnriched";
+       GByteArray *actual, *expected;
+       GMimeStream *stream;
+       GMimeFilter *filter;
+       char *path;
+       
+       testsuite_check ("%s (%s)", what, input);
+       
+       actual = g_byte_array_new ();
+       stream = g_mime_stream_mem_new_with_byte_array (actual);
+       g_mime_stream_mem_set_owner ((GMimeStreamMem *) stream, FALSE);
+       
+       filter = g_mime_filter_enriched_new (0);
+       
+       path = g_build_filename (datadir, input, NULL);
+       pump_data_through_filter (filter, path, stream, TRUE, TRUE);
+       g_mime_filter_reset (filter);
+       g_object_unref (stream);
+       g_object_unref (filter);
+       g_free (path);
+       
+       path = g_build_filename (datadir, output, NULL);
+       expected = read_all_bytes (path, TRUE);
+       g_free (path);
+       
+       if (actual->len != expected->len) {
+               testsuite_check_failed ("%s failed: stream lengths do not match: expected=%u; actual=%u",
+                                       what, expected->len, actual->len);
+               goto error;
+       }
+       
+       if (memcmp (actual->data, expected->data, actual->len) != 0) {
+               testsuite_check_failed ("%s failed: stream contents do not match", what);
+               goto error;
+       }
+       
+       testsuite_check_passed ();
+       
+error:
+       
+       g_byte_array_free (expected, TRUE);
+       g_byte_array_free (actual, TRUE);
+}
+
+static void
 test_gzip (const char *datadir, const char *filename)
 {
        char *name = g_strdup_printf ("%s.gz", filename);
@@ -352,6 +399,8 @@ int main (int argc, char **argv)
        test_charset_conversion (datadir, "japanese", "utf-8", "shift-jis");
        test_charset_conversion (datadir, "japanese", "shift-jis", "utf-8");
        
+       test_enriched (datadir, "enriched.txt", "enriched.html");
+       
        test_gzip (datadir, "lorem-ipsum.txt");
        test_gunzip (datadir, "lorem-ipsum.txt");
        


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