[libsoup] Fix parsing of <double> in new XMLRPC code
- From: Dan Winship <danw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libsoup] Fix parsing of <double> in new XMLRPC code
- Date: Thu, 16 Jun 2016 12:13:35 +0000 (UTC)
commit 5ec000ba17a1d5b808a71ef5909265713415bd45
Author: Dan Winship <danw gnome org>
Date: Thu Jun 16 08:12:21 2016 -0400
Fix parsing of <double> in new XMLRPC code
The new XMLRPC code didn't parse <double>s correctly, which we didn't
notice because the test cases don't test <double> either. So fix the
code and the tests.
https://bugzilla.gnome.org/show_bug.cgi?id=767707
libsoup/soup-xmlrpc.c | 9 ++++-----
tests/xmlrpc-old-server-test.c | 9 +++++----
tests/xmlrpc-old-test.c | 25 +++++++++++++------------
tests/xmlrpc-server-test.c | 8 ++++----
tests/xmlrpc-server.php | 2 +-
tests/xmlrpc-test.c | 23 ++++++++++++-----------
6 files changed, 39 insertions(+), 37 deletions(-)
---
diff --git a/libsoup/soup-xmlrpc.c b/libsoup/soup-xmlrpc.c
index b1673d9..42dcda9 100644
--- a/libsoup/soup-xmlrpc.c
+++ b/libsoup/soup-xmlrpc.c
@@ -1007,21 +1007,20 @@ parse_value (xmlNode *node, const char **signature, GError **error)
else
variant = parse_number (typenode, class, error);
} else if (g_str_equal (typename, "double")) {
- if (class == G_VARIANT_CLASS_VARIANT || class == G_VARIANT_CLASS_DOUBLE) {
+ if (class != G_VARIANT_CLASS_VARIANT && class != G_VARIANT_CLASS_DOUBLE) {
g_set_error (error, SOUP_XMLRPC_ERROR, SOUP_XMLRPC_ERROR_ARGUMENTS,
"<double> node does not match signature");
goto fail;
}
variant = parse_double (typenode, error);
} else if (g_str_equal (typename, "string")) {
- content = xmlNodeGetContent (typenode);
- if (class == G_VARIANT_CLASS_VARIANT || class == G_VARIANT_CLASS_STRING)
- variant = g_variant_new_string ((const char *)content);
- else {
+ if (class != G_VARIANT_CLASS_VARIANT && class != G_VARIANT_CLASS_STRING) {
g_set_error (error, SOUP_XMLRPC_ERROR, SOUP_XMLRPC_ERROR_ARGUMENTS,
"<string> node does not match signature");
goto fail;
}
+ content = xmlNodeGetContent (typenode);
+ variant = g_variant_new_string ((const char *)content);
} else if (g_str_equal (typename, "base64")) {
if (class != G_VARIANT_CLASS_VARIANT) {
if (!g_str_has_prefix (*signature, "ay")) {
diff --git a/tests/xmlrpc-old-server-test.c b/tests/xmlrpc-old-server-test.c
index 3d3ba99..a7076b5 100644
--- a/tests/xmlrpc-old-server-test.c
+++ b/tests/xmlrpc-old-server-test.c
@@ -33,7 +33,8 @@ args_error (SoupMessage *msg, GValueArray *params, int expected)
static void
do_sum (SoupMessage *msg, GValueArray *params)
{
- int sum = 0, i, val;
+ int i;
+ double sum = 0.0, val;
GValueArray *nums;
if (params->n_values != 1) {
@@ -46,14 +47,14 @@ do_sum (SoupMessage *msg, GValueArray *params)
}
for (i = 0; i < nums->n_values; i++) {
- if (!soup_value_array_get_nth (nums, i, G_TYPE_INT, &val)) {
- type_error (msg, G_TYPE_INT, nums, i);
+ if (!soup_value_array_get_nth (nums, i, G_TYPE_DOUBLE, &val)) {
+ type_error (msg, G_TYPE_DOUBLE, nums, i);
return;
}
sum += val;
}
- soup_xmlrpc_set_response (msg, G_TYPE_INT, sum);
+ soup_xmlrpc_set_response (msg, G_TYPE_DOUBLE, sum);
}
diff --git a/tests/xmlrpc-old-test.c b/tests/xmlrpc-old-test.c
index d6ecd05..3867b69 100644
--- a/tests/xmlrpc-old-test.c
+++ b/tests/xmlrpc-old-test.c
@@ -110,35 +110,36 @@ check_xmlrpc (GValue *value, GType type, ...)
static void
test_sum (void)
{
- GValueArray *ints;
- int i, val, sum, result;
+ GValueArray *dbls;
+ int i;
+ double val, sum, result;
GValue retval;
gboolean ok;
SOUP_TEST_SKIP_IF_NO_XMLRPC_SERVER;
- debug_printf (2, "sum (array of int -> int): ");
+ debug_printf (2, "sum (array of double -> double): ");
- ints = g_value_array_new (10);
+ dbls = g_value_array_new (10);
for (i = sum = 0; i < 10; i++) {
- val = g_random_int_range (0, 100);
- debug_printf (2, "%s%d", i == 0 ? "[" : ", ", val);
- soup_value_array_append (ints, G_TYPE_INT, val);
+ val = g_random_int_range (0, 400) / 4.0;
+ debug_printf (2, "%s%.2f", i == 0 ? "[" : ", ", val);
+ soup_value_array_append (dbls, G_TYPE_DOUBLE, val);
sum += val;
}
debug_printf (2, "] -> ");
ok = (do_xmlrpc ("sum", &retval,
- G_TYPE_VALUE_ARRAY, ints,
+ G_TYPE_VALUE_ARRAY, dbls,
G_TYPE_INVALID) &&
- check_xmlrpc (&retval, G_TYPE_INT, &result));
- g_value_array_free (ints);
+ check_xmlrpc (&retval, G_TYPE_DOUBLE, &result));
+ g_value_array_free (dbls);
if (!ok)
return;
- debug_printf (2, "%d\n", result);
- g_assert_cmpint (result, ==, sum);
+ debug_printf (2, "%.2f\n", result);
+ g_assert_cmpfloat (result, ==, sum);
}
static void
diff --git a/tests/xmlrpc-server-test.c b/tests/xmlrpc-server-test.c
index db390cc..80f04ea 100644
--- a/tests/xmlrpc-server-test.c
+++ b/tests/xmlrpc-server-test.c
@@ -31,18 +31,18 @@ do_sum (SoupMessage *msg, SoupXMLRPCParams *params)
GVariant *args;
GVariant *child;
GVariantIter iter;
- int sum = 0, val;
+ double sum = 0.0, val;
- if (!(args = parse_params (msg, params, "(ai)")))
+ if (!(args = parse_params (msg, params, "(ad)")))
return;
child = g_variant_get_child_value (args, 0);
g_variant_iter_init (&iter, child);
- while (g_variant_iter_loop (&iter, "i", &val))
+ while (g_variant_iter_loop (&iter, "d", &val))
sum += val;
- soup_xmlrpc_message_set_response (msg, g_variant_new_int32 (sum), NULL);
+ soup_xmlrpc_message_set_response (msg, g_variant_new_double (sum), NULL);
g_variant_unref (args);
g_variant_unref (child);
diff --git a/tests/xmlrpc-server.php b/tests/xmlrpc-server.php
index 66cb2be..4025f95 100644
--- a/tests/xmlrpc-server.php
+++ b/tests/xmlrpc-server.php
@@ -18,7 +18,7 @@ function sum ($method_name, $params, $app_data)
$sum = 0;
foreach ($params[0] as $val) {
- if (xmlrpc_get_type ($val) != "int")
+ if (xmlrpc_get_type ($val) != "double")
return paramfault();
$sum = $sum + $val;
diff --git a/tests/xmlrpc-test.c b/tests/xmlrpc-test.c
index c50995a..1643a25 100644
--- a/tests/xmlrpc-test.c
+++ b/tests/xmlrpc-test.c
@@ -86,33 +86,34 @@ static void
test_sum (void)
{
GVariantBuilder builder;
- int i, val, sum, result;
+ int i;
+ double val, sum, result;
GVariant *retval;
gboolean ok;
SOUP_TEST_SKIP_IF_NO_XMLRPC_SERVER;
- debug_printf (2, "sum (array of int -> int): ");
+ debug_printf (2, "sum (array of double -> double): ");
- g_variant_builder_init (&builder, G_VARIANT_TYPE ("ai"));
+ g_variant_builder_init (&builder, G_VARIANT_TYPE ("ad"));
for (i = sum = 0; i < 10; i++) {
- val = g_random_int_range (0, 100);
- debug_printf (2, "%s%d", i == 0 ? "[" : ", ", val);
- g_variant_builder_add (&builder, "i", val);
+ val = g_random_int_range (0, 400) / 4.0;
+ debug_printf (2, "%s%.2f", i == 0 ? "[" : ", ", val);
+ g_variant_builder_add (&builder, "d", val);
sum += val;
}
debug_printf (2, "] -> ");
ok = do_xmlrpc ("sum",
- g_variant_new ("(@ai)", g_variant_builder_end (&builder)),
- "i", &retval);
+ g_variant_new ("(@ad)", g_variant_builder_end (&builder)),
+ "d", &retval);
if (!ok)
return;
- result = g_variant_get_int32 (retval);
- debug_printf (2, "%d\n", result);
- g_assert_cmpint (result, ==, sum);
+ result = g_variant_get_double (retval);
+ debug_printf (2, "%.2f\n", result);
+ g_assert_cmpfloat (result, ==, sum);
g_variant_unref (retval);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]