From 908fc8266ef81f80964be710dd5a15dbdb86500f Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Thu, 22 Oct 2015 03:06:13 -0200 Subject: content-parser: handle \r\n line endings properly --- tests/check_content_parser.c | 502 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 500 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/check_content_parser.c b/tests/check_content_parser.c index 37c1761..994e900 100644 --- a/tests/check_content_parser.c +++ b/tests/check_content_parser.c @@ -144,6 +144,84 @@ test_content_parse(void **state) } +static void +test_content_parse_crlf(void **state) +{ + size_t l = 0; + char *html = blogc_content_parse( + "# um\r\n" + "## dois\r\n" + "### tres\r\n" + "#### quatro\r\n" + "##### cinco\r\n" + "###### seis\r\n" + "\r\n" + "bola\r\n" + "chunda\r\n" + "\r\n" + "> bola \r\n" + "> guda\r\n" + "> buga\r\n" + "> \r\n" + "> asd\r\n" + "\r\n" + " bola\r\n" + " asd\r\n" + " qwewer\r\n" + "\r\n" + "+++\r\n" + "1. chunda\r\n" + "3. fuuuu\r\n" + "\r\n" + "+ chunda2\r\n" + "+ fuuuu2\r\n" + "\r\n" + "\r\n" + "\r\n" + "guda\r\n" + "yay\r\n" + "\r\n" + "**bola**\r\n", &l); + assert_non_null(html); + assert_int_equal(l, 0); + assert_string_equal(html, + "

um

\r\n" + "

dois

\r\n" + "

tres

\r\n" + "

quatro

\r\n" + "
cinco
\r\n" + "
seis
\r\n" + "

bola\r\n" + "chunda

\r\n" + "

bola
\r\n" + "guda\r\n" + "buga

\r\n" + "
asd
\r\n" + "
\r\n" + "
bola\r\n"
+        " asd\r\n"
+        "qwewer
\r\n" + "
\r\n" + "
    \r\n" + "
  1. chunda
  2. \r\n" + "
  3. fuuuu
  4. \r\n" + "
\r\n" + "\r\n" + "\r\n" + "

guda\r\n" + "yay

\r\n" + "

bola

\r\n"); + free(html); +} + + static void test_content_parse_with_excerpt(void **state) { @@ -186,6 +264,48 @@ test_content_parse_with_excerpt(void **state) } +static void +test_content_parse_with_excerpt_crlf(void **state) +{ + size_t l = 0; + char *html = blogc_content_parse( + "# test\r\n" + "\r\n" + "chunda\r\n" + "\r\n" + "..\r\n" + "\r\n" + "guda\r\n" + "lol", &l); + assert_non_null(html); + assert_int_equal(l, 40); + assert_string_equal(html, + "

test

\r\n" + "

chunda

\r\n" + "

guda\r\n" + "lol

\r\n"); + free(html); + l = 0; + html = blogc_content_parse( + "# test\r\n" + "\r\n" + "chunda\r\n" + "\r\n" + "...\r\n" + "\r\n" + "guda\r\n" + "lol", &l); + assert_non_null(html); + assert_int_equal(l, 40); + assert_string_equal(html, + "

test

\r\n" + "

chunda

\r\n" + "

guda\r\n" + "lol

\r\n"); + free(html); +} + + static void test_content_parse_header(void **state) { @@ -212,6 +332,32 @@ test_content_parse_header(void **state) } +static void +test_content_parse_header_crlf(void **state) +{ + char *html = blogc_content_parse("## bola", NULL); + assert_non_null(html); + assert_string_equal(html, "

bola

\n"); + free(html); + html = blogc_content_parse("## bola\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, "

bola

\r\n"); + free(html); + html = blogc_content_parse( + "bola\r\n" + "\r\n" + "## bola\r\n" + "\r\n" + "guda\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola

\r\n" + "

bola

\r\n" + "

guda

\r\n"); + free(html); +} + + static void test_content_parse_html(void **state) { @@ -239,6 +385,33 @@ test_content_parse_html(void **state) } +static void +test_content_parse_html_crlf(void **state) +{ + char *html = blogc_content_parse("
\r\n
", NULL); + assert_non_null(html); + assert_string_equal(html, "
\r\n
\r\n"); + free(html); + html = blogc_content_parse("
\r\n
\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, "
\r\n
\r\n"); + free(html); + html = blogc_content_parse( + "bola\r\n" + "\r\n" + "
\r\n" + "
\r\n" + "\r\n" + "chunda\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola

\r\n" + "
\r\n
\r\n" + "

chunda

\r\n"); + free(html); +} + + static void test_content_parse_blockquote(void **state) { @@ -274,6 +447,41 @@ test_content_parse_blockquote(void **state) } +static void +test_content_parse_blockquote_crlf(void **state) +{ + char *html = blogc_content_parse("> bola\r\n> guda", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola\r\n" + "guda

\r\n" + "
\r\n"); + free(html); + html = blogc_content_parse("> bola\r\n> guda\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola\r\n" + "guda

\r\n" + "
\r\n"); + free(html); + html = blogc_content_parse( + "bola\r\n" + "\r\n" + "> bola\r\n" + "> guda\r\n" + "\r\n" + "chunda\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola

\r\n" + "

bola\r\n" + "guda

\r\n" + "
\r\n" + "

chunda

\r\n"); + free(html); +} + + static void test_content_parse_code(void **state) { @@ -306,6 +514,38 @@ test_content_parse_code(void **state) } +static void +test_content_parse_code_crlf(void **state) +{ + char *html = blogc_content_parse(" bola\r\n guda", NULL); + assert_non_null(html); + assert_string_equal(html, + "
bola\r\n"
+        "guda
\r\n"); + free(html); + html = blogc_content_parse(" bola\r\n guda\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "
bola\r\n"
+        "guda
\r\n"); + free(html); + html = blogc_content_parse( + "bola\r\n" + "\r\n" + " bola\r\n" + " guda\r\n" + "\r\n" + "chunda\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola

\r\n" + "
bola\r\n"
+        "guda
\r\n" + "

chunda

\r\n"); + free(html); +} + + static void test_content_parse_horizontal_rule(void **state) { @@ -364,6 +604,64 @@ test_content_parse_horizontal_rule(void **state) } +static void +test_content_parse_horizontal_rule_crlf(void **state) +{ + char *html = blogc_content_parse("bola\r\nguda\r\n\r\n**", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola\r\n" + "guda

\r\n" + "
\r\n"); + free(html); + html = blogc_content_parse("bola\r\nguda\r\n\r\n++++", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola\r\n" + "guda

\r\n" + "
\r\n"); + free(html); + html = blogc_content_parse("bola\r\nguda\r\n\r\n--\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola\r\n" + "guda

\r\n" + "
\r\n"); + free(html); + html = blogc_content_parse("bola\r\nguda\r\n\r\n****\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola\r\n" + "guda

\r\n" + "
\r\n"); + free(html); + html = blogc_content_parse( + "bola\r\n" + "\r\n" + "**\r\n" + "\r\n" + "chunda\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola

\r\n" + "
\r\n" + "

chunda

\r\n"); + free(html); + html = blogc_content_parse( + "bola\r\n" + "\r\n" + "----\r\n" + "\r\n" + "chunda\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

bola

\r\n" + "
\r\n" + "

chunda

\r\n"); + free(html); +} + + static void test_content_parse_unordered_list(void **state) { @@ -452,6 +750,94 @@ test_content_parse_unordered_list(void **state) } +static void +test_content_parse_unordered_list_crlf(void **state) +{ + char *html = blogc_content_parse( + "lol\r\n" + "\r\n" + "* asd\r\n" + "* qwe\r\n" + "* zxc", NULL); + assert_non_null(html); + assert_string_equal(html, + "

lol

\r\n" + "\r\n"); + free(html); + html = blogc_content_parse( + "lol\r\n" + "\r\n" + "* asd\r\n" + "* qwe\r\n" + "* zxc\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

lol

\r\n" + "\r\n"); + free(html); + html = blogc_content_parse( + "lol\r\n" + "\r\n" + "* asd\r\n" + "* qwe\r\n" + "* zxc\r\n" + "\r\n" + "fuuuu\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

lol

\r\n" + "\r\n" + "

fuuuu

\r\n"); + free(html); + html = blogc_content_parse( + "lol\r\n" + "\r\n" + "* asd\r\n" + " cvb\r\n" + "* qwe\r\n" + "* zxc\r\n" + " 1234\r\n" + "\r\n" + "fuuuu\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

lol

\r\n" + "\r\n" + "

fuuuu

\r\n"); + free(html); + html = blogc_content_parse( + "* asd\r\n" + "* qwe\r\n" + "* zxc", NULL); + assert_non_null(html); + assert_string_equal(html, + "\r\n"); + free(html); +} + + static void test_content_parse_ordered_list(void **state) { @@ -540,6 +926,94 @@ test_content_parse_ordered_list(void **state) } +static void +test_content_parse_ordered_list_crlf(void **state) +{ + char *html = blogc_content_parse( + "lol\r\n" + "\r\n" + "1. asd\r\n" + "2. qwe\r\n" + "3. zxc", NULL); + assert_non_null(html); + assert_string_equal(html, + "

lol

\r\n" + "
    \r\n" + "
  1. asd
  2. \r\n" + "
  3. qwe
  4. \r\n" + "
  5. zxc
  6. \r\n" + "
\r\n"); + free(html); + html = blogc_content_parse( + "lol\r\n" + "\r\n" + "1. asd\r\n" + "2. qwe\r\n" + "3. zxc\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

lol

\r\n" + "
    \r\n" + "
  1. asd
  2. \r\n" + "
  3. qwe
  4. \r\n" + "
  5. zxc
  6. \r\n" + "
\r\n"); + free(html); + html = blogc_content_parse( + "lol\r\n" + "\r\n" + "1. asd\r\n" + "2. qwe\r\n" + "3. zxc\r\n" + "\r\n" + "fuuuu\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

lol

\r\n" + "
    \r\n" + "
  1. asd
  2. \r\n" + "
  3. qwe
  4. \r\n" + "
  5. zxc
  6. \r\n" + "
\r\n" + "

fuuuu

\r\n"); + free(html); + html = blogc_content_parse( + "lol\r\n" + "\r\n" + "1. asd\r\n" + " cvb\r\n" + "2. qwe\r\n" + "3. zxc\r\n" + " 1234\r\n" + "\r\n" + "fuuuu\r\n", NULL); + assert_non_null(html); + assert_string_equal(html, + "

lol

\r\n" + "
    \r\n" + "
  1. asd\r\n" + "cvb
  2. \r\n" + "
  3. qwe
  4. \r\n" + "
  5. zxc\r\n" + "1234
  6. \r\n" + "
\r\n" + "

fuuuu

\r\n"); + free(html); + html = blogc_content_parse( + "1. asd\r\n" + "2. qwe\r\n" + "3. zxc", NULL); + assert_non_null(html); + assert_string_equal(html, + "
    \r\n" + "
  1. asd
  2. \r\n" + "
  3. qwe
  4. \r\n" + "
  5. zxc
  6. \r\n" + "
\r\n"); + free(html); +} + + static void test_content_parse_invalid_excerpt(void **state) { @@ -1126,11 +1600,11 @@ test_content_parse_inline_line_break(void **state) free(html); html = blogc_content_parse_inline("asd "); assert_non_null(html); - assert_string_equal(html, "asd
\n"); + assert_string_equal(html, "asd
"); free(html); html = blogc_content_parse_inline("asd "); assert_non_null(html); - assert_string_equal(html, "asd
\n"); + assert_string_equal(html, "asd
"); free(html); // invalid html = blogc_content_parse_inline("asd "); @@ -1144,6 +1618,20 @@ test_content_parse_inline_line_break(void **state) } +static void +test_content_parse_inline_line_break_crlf(void **state) +{ + char *html = blogc_content_parse_inline("asd \r\n"); + assert_non_null(html); + assert_string_equal(html, "asd
\r\n"); + free(html); + html = blogc_content_parse_inline("asd \r\n"); + assert_non_null(html); + assert_string_equal(html, "asd \r\n"); + free(html); +} + + int main(void) { @@ -1151,14 +1639,23 @@ main(void) unit_test(test_slugify), unit_test(test_is_ordered_list_item), unit_test(test_content_parse), + unit_test(test_content_parse_crlf), unit_test(test_content_parse_with_excerpt), + unit_test(test_content_parse_with_excerpt_crlf), unit_test(test_content_parse_header), + unit_test(test_content_parse_header_crlf), unit_test(test_content_parse_html), + unit_test(test_content_parse_html_crlf), unit_test(test_content_parse_blockquote), + unit_test(test_content_parse_blockquote_crlf), unit_test(test_content_parse_code), + unit_test(test_content_parse_code_crlf), unit_test(test_content_parse_horizontal_rule), + unit_test(test_content_parse_horizontal_rule_crlf), unit_test(test_content_parse_unordered_list), + unit_test(test_content_parse_unordered_list_crlf), unit_test(test_content_parse_ordered_list), + unit_test(test_content_parse_ordered_list_crlf), unit_test(test_content_parse_invalid_excerpt), unit_test(test_content_parse_invalid_header), unit_test(test_content_parse_invalid_header_empty), @@ -1175,6 +1672,7 @@ main(void) unit_test(test_content_parse_inline_link_auto), unit_test(test_content_parse_inline_image), unit_test(test_content_parse_inline_line_break), + unit_test(test_content_parse_inline_line_break_crlf), }; return run_tests(tests); } -- cgit v1.2.3-18-g5258