qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 08/58] check-qjson: Cover escaped characters more thoroughly, part 1
Date: Fri, 24 Aug 2018 21:31:16 +0200	[thread overview]
Message-ID: <20180824193206.25475-9-armbru@redhat.com> (raw)
In-Reply-To: <20180824193206.25475-1-armbru@redhat.com>

escaped_string() first tests double quoted strings, then repeats a few
tests with single quotes.  Repeat all of them: store the strings to
test without quotes, and wrap them in either kind of quote for
testing.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-9-armbru@redhat.com>
---
 tests/check-qjson.c | 96 +++++++++++++++++++++++++++------------------
 1 file changed, 57 insertions(+), 39 deletions(-)

diff --git a/tests/check-qjson.c b/tests/check-qjson.c
index 188f683317..008b6e95e4 100644
--- a/tests/check-qjson.c
+++ b/tests/check-qjson.c
@@ -22,55 +22,73 @@
 #include "qapi/qmp/qstring.h"
 #include "qemu-common.h"
 
+static QString *from_json_str(const char *jstr, bool single, Error **errp)
+{
+    char quote = single ? '\'' : '"';
+    char *qjstr = g_strdup_printf("%c%s%c", quote, jstr, quote);
+    QString *ret = qobject_to(QString, qobject_from_json(qjstr, errp));
+
+    g_free(qjstr);
+    return ret;
+}
+
+static char *to_json_str(QString *str)
+{
+    QString *json = qobject_to_json(QOBJECT(str));
+    char *jstr;
+
+    if (!json) {
+        return NULL;
+    }
+    /* peel off double quotes */
+    jstr = g_strndup(qstring_get_str(json) + 1,
+                     qstring_get_length(json) - 2);
+    qobject_unref(json);
+    return jstr;
+}
+
 static void escaped_string(void)
 {
-    int i;
     struct {
-        const char *encoded;
-        const char *decoded;
+        /* Content of JSON string to parse with qobject_from_json() */
+        const char *json_in;
+        /* Expected parse output; to unparse with qobject_to_json() */
+        const char *utf8_out;
         int skip;
     } test_cases[] = {
-        { "\"\\b\"", "\b" },
-        { "\"\\f\"", "\f" },
-        { "\"\\n\"", "\n" },
-        { "\"\\r\"", "\r" },
-        { "\"\\t\"", "\t" },
-        { "\"/\"", "/" },
-        { "\"\\/\"", "/", .skip = 1 },
-        { "\"\\\\\"", "\\" },
-        { "\"\\\"\"", "\"" },
-        { "\"hello world \\\"embedded string\\\"\"",
+        { "\\b", "\b" },
+        { "\\f", "\f" },
+        { "\\n", "\n" },
+        { "\\r", "\r" },
+        { "\\t", "\t" },
+        { "/", "/" },
+        { "\\/", "/", .skip = 1 },
+        { "\\\\", "\\" },
+        { "\\\"", "\"" },
+        { "hello world \\\"embedded string\\\"",
           "hello world \"embedded string\"" },
-        { "\"hello world\\nwith new line\"", "hello world\nwith new line" },
-        { "\"single byte utf-8 \\u0020\"", "single byte utf-8  ", .skip = 1 },
-        { "\"double byte utf-8 \\u00A2\"", "double byte utf-8 \xc2\xa2" },
-        { "\"triple byte utf-8 \\u20AC\"", "triple byte utf-8 \xe2\x82\xac" },
-        { "'\\b'", "\b", .skip = 1 },
-        { "'\\f'", "\f", .skip = 1 },
-        { "'\\n'", "\n", .skip = 1 },
-        { "'\\r'", "\r", .skip = 1 },
-        { "'\\t'", "\t", .skip = 1 },
-        { "'\\/'", "/", .skip = 1 },
-        { "'\\\\'", "\\", .skip = 1 },
+        { "hello world\\nwith new line", "hello world\nwith new line" },
+        { "single byte utf-8 \\u0020", "single byte utf-8  ", .skip = 1 },
+        { "double byte utf-8 \\u00A2", "double byte utf-8 \xc2\xa2" },
+        { "triple byte utf-8 \\u20AC", "triple byte utf-8 \xe2\x82\xac" },
         {}
     };
+    int i, j;
+    QString *cstr;
+    char *jstr;
 
-    for (i = 0; test_cases[i].encoded; i++) {
-        QObject *obj;
-        QString *str;
-
-        obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        str = qobject_to(QString, obj);
-        g_assert(str);
-        g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
-
-        if (test_cases[i].skip == 0) {
-            str = qobject_to_json(obj);
-            g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);
-            qobject_unref(obj);
+    for (i = 0; test_cases[i].json_in; i++) {
+        for (j = 0; j < 2; j++) {
+            cstr = from_json_str(test_cases[i].json_in, j, &error_abort);
+            g_assert_cmpstr(qstring_get_try_str(cstr),
+                            ==, test_cases[i].utf8_out);
+            if (test_cases[i].skip == 0) {
+                jstr = to_json_str(cstr);
+                g_assert_cmpstr(jstr, ==, test_cases[i].json_in);
+                g_free(jstr);
+            }
+            qobject_unref(cstr);
         }
-
-        qobject_unref(str);
     }
 }
 
-- 
2.17.1

  parent reply	other threads:[~2018-08-24 19:32 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-24 19:31 [Qemu-devel] [PULL 00/58] QObject patches for 2018-08-24 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 01/58] docs/interop/qmp-spec: How to force known good parser state Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 02/58] check-qjson: Cover multiple JSON objects in same string Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 03/58] check-qjson: Cover blank and lexically erroneous input Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 04/58] check-qjson: Cover whitespace more thoroughly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 05/58] qmp-cmd-test: Split off qmp-test Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 06/58] qmp-test: Cover syntax and lexical errors Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 07/58] test-qga: Clean up how we test QGA synchronization Markus Armbruster
2018-08-24 19:31 ` Markus Armbruster [this message]
2018-08-24 19:31 ` [Qemu-devel] [PULL 09/58] check-qjson: Streamline escaped_string()'s test strings Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 10/58] check-qjson: Cover escaped characters more thoroughly, part 2 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 11/58] check-qjson: Consolidate partly redundant string tests Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 12/58] check-qjson: Cover UTF-8 in single quoted strings Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 13/58] check-qjson: Simplify utf8_string() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 14/58] check-qjson: Fix utf8_string() to test all invalid sequences Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 15/58] check-qjson qmp-test: Cover control characters more thoroughly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 16/58] check-qjson: Cover interpolation " Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 17/58] json: Fix lexer to include the bad character in JSON_ERROR token Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 18/58] json: Reject unescaped control characters Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 19/58] json: Revamp lexer documentation Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 20/58] json: Tighten and simplify qstring_from_escaped_str()'s loop Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 21/58] check-qjson: Document we expect invalid UTF-8 to be rejected Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 22/58] json: Reject invalid UTF-8 sequences Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 23/58] json: Report first rather than last parse error Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 24/58] json: Leave rejecting invalid UTF-8 to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 25/58] json: Accept overlong \xC0\x80 as U+0000 ("modified UTF-8") Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 26/58] json: Leave rejecting invalid escape sequences to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 27/58] json: Simplify parse_string() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 28/58] json: Reject invalid \uXXXX, fix \u0000 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 29/58] json: Fix \uXXXX for surrogate pairs Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 30/58] check-qjson: Fix and enable utf8_string()'s disabled part Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 31/58] json: remove useless return value from lexer/parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 32/58] json-parser: simplify and avoid JSONParserContext allocation Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 33/58] json: Have lexer call streamer directly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 34/58] json: Redesign the callback to consume JSON values Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 35/58] json: Don't pass null @tokens to json_parser_parse() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 36/58] json: Don't create JSON_ERROR tokens that won't be used Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 37/58] json: Rename token JSON_ESCAPE & friends to JSON_INTERP Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 38/58] json: Treat unwanted interpolation as lexical error Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 39/58] json: Pass lexical errors and limit violations to callback Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 40/58] json: Leave rejecting invalid interpolation to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 41/58] json: Replace %I64d, %I64u by %PRId64, %PRIu64 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 42/58] json: Improve names of lexer states related to numbers Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 43/58] qjson: Fix qobject_from_json() & friends for multiple values Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 44/58] json: Fix latent parser aborts at end of input Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 45/58] json: Fix streamer not to ignore trailing unterminated structures Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 46/58] json: Assert json_parser_parse() consumes all tokens on success Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 47/58] qjson: Have qobject_from_json() & friends reject empty and blank Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 48/58] json: Enforce token count and size limits more tightly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 49/58] json: Streamline json_message_process_token() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 50/58] json: Unbox tokens queue in JSONMessageParser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 51/58] json: Make JSONToken opaque outside json-parser.c Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 52/58] qobject: Drop superfluous includes of qemu-common.h Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 53/58] json: Clean up headers Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 54/58] tests/drive_del-test: Fix harmless JSON interpolation bug Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 55/58] json: Keep interpolation state in JSONParserContext Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 56/58] json: Improve safety of qobject_from_jsonf_nofail() & friends Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 57/58] json: Support %% in JSON strings when interpolating Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 58/58] json: Update references to RFC 7159 to RFC 8259 Markus Armbruster
2018-08-25  9:58 ` [Qemu-devel] [PULL 00/58] QObject patches for 2018-08-24 Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180824193206.25475-9-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).