From 72c07e67e284a7b125dd5d9823be8d129ba548bc Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 25 Jan 2024 17:51:45 +0900 Subject: [PATCH] Add more display string tests --- sfparse_test.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/sfparse_test.c b/sfparse_test.c index 8e4cc6e..0de2140 100644 --- a/sfparse_test.c +++ b/sfparse_test.c @@ -1554,9 +1554,10 @@ void test_sf_parser_dispstring(void) { sf_parser sfp; sf_value val; sf_vec decoded; - uint8_t buf[64]; + uint8_t buf[128]; - /* https://github.com/httpwg/structured-field-tests/blob/main/display-string.json */ + /* https://github.com/httpwg/structured-field-tests/blob/main/display-string.json + */ { /* basic display string (ascii content) */ @@ -1576,6 +1577,33 @@ void test_sf_parser_dispstring(void) { sf_parser_bytes_free(); } + { + /* all printable ascii */ + sf_parser_bytes_init(&sfp, "%\" " + "!%22#$%25&'()*+,-./" + "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]" + "^_`abcdefghijklmnopqrstuvwxyz{|}~\""); + + CU_ASSERT(0 == sf_parser_item(&sfp, &val)); + CU_ASSERT(SF_TYPE_DISPSTRING == val.type); + CU_ASSERT(str_sf_vec_eq(" !%22#$%25&'()*+,-./" + "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]" + "^_`abcdefghijklmnopqrstuvwxyz{|}~", + &val.vec)); + + decoded.base = buf; + sf_pctdecode(&decoded, &val.vec); + + CU_ASSERT(str_sf_vec_eq(" !\"#$%&'()*+,-./" + "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" + "abcdefghijklmnopqrstuvwxyz{|}~", + &decoded)); + + CU_ASSERT(SF_ERR_EOF == sf_parser_item(&sfp, NULL)); + + sf_parser_bytes_free(); + } + { /* non-ascii display string (uppercase escaping) */ sf_parser_bytes_init(&sfp, "%\"f%C3%BC%C3%BC\""); @@ -1630,6 +1658,24 @@ void test_sf_parser_dispstring(void) { sf_parser_bytes_free(); } + { + /* unquoted display string */ + sf_parser_bytes_init(&sfp, "%foo"); + + CU_ASSERT(SF_ERR_PARSE_ERROR == sf_parser_item(&sfp, &val)); + + sf_parser_bytes_free(); + } + + { + /* display string missing initial quote */ + sf_parser_bytes_init(&sfp, "%foo\""); + + CU_ASSERT(SF_ERR_PARSE_ERROR == sf_parser_item(&sfp, &val)); + + sf_parser_bytes_free(); + } + { /* unbalanced display string */ sf_parser_bytes_init(&sfp, "%\"foo"); @@ -1702,6 +1748,24 @@ void test_sf_parser_dispstring(void) { sf_parser_bytes_free(); } + { + /* BOM in display string */ + sf_parser_bytes_init(&sfp, "%\"BOM: %ef%bb%bf\""); + + CU_ASSERT(0 == sf_parser_item(&sfp, &val)); + CU_ASSERT(SF_TYPE_DISPSTRING == val.type); + CU_ASSERT(str_sf_vec_eq("BOM: %ef%bb%bf", &val.vec)); + + decoded.base = buf; + sf_pctdecode(&decoded, &val.vec); + + CU_ASSERT(str_sf_vec_eq("BOM: \xef\xbb\xbf", &decoded)); + + CU_ASSERT(SF_ERR_EOF == sf_parser_item(&sfp, NULL)); + + sf_parser_bytes_free(); + } + /* Additional tests */ {