diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-05-02 14:26:21 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-06-12 18:42:22 +0200 |
commit | 309a747fa6cfeac0a0165543f23a924866727c9b (patch) | |
tree | a51242aa3ac87243c0614bf93d27e9083182e485 /src/test/test-varlink.c | |
parent | json: merge json_dispatch_path() + json_dispatch_absolute_path() (diff) | |
download | systemd-309a747fa6cfeac0a0165543f23a924866727c9b.tar.xz systemd-309a747fa6cfeac0a0165543f23a924866727c9b.zip |
libsystemd: turn json.[ch] into a public API
This is preparation for making our Varlink API a public API. Since our
Varlink API is built on top of our JSON API we need to make that public
first (it's a nice API, but JSON APIs there are already enough, this is
purely about the Varlink angle).
I made most of the json.h APIs public, and just placed them in
sd-json.h. Sometimes I wasn't so sure however, since the underlying data
structures would have to be made public too. If in doubt I didn#t risk
it, and moved the relevant API to src/libsystemd/sd-json/json-util.h
instead (without any sd_* symbol prefixes).
This is mostly a giant search/replace patch.
Diffstat (limited to 'src/test/test-varlink.c')
-rw-r--r-- | src/test/test-varlink.c | 95 |
1 files changed, 48 insertions, 47 deletions
diff --git a/src/test/test-varlink.c b/src/test/test-varlink.c index 8ad5757b9d..48fc4a7e7f 100644 --- a/src/test/test-varlink.c +++ b/src/test/test-varlink.c @@ -5,10 +5,11 @@ #include <pthread.h> #include "sd-event.h" +#include "sd-json.h" #include "data-fd-util.h" #include "fd-util.h" -#include "json.h" +#include "json-util.h" #include "rm-rf.h" #include "strv.h" #include "tests.h" @@ -25,33 +26,33 @@ static int n_done = 0; static int block_write_fd = -EBADF; -static int method_something(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *ret = NULL; - JsonVariant *a, *b; +static int method_something(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; + sd_json_variant *a, *b; int64_t x, y; int r; - a = json_variant_by_key(parameters, "a"); + a = sd_json_variant_by_key(parameters, "a"); if (!a) return varlink_error(link, "io.test.BadParameters", NULL); - x = json_variant_integer(a); + x = sd_json_variant_integer(a); - b = json_variant_by_key(parameters, "b"); + b = sd_json_variant_by_key(parameters, "b"); if (!b) return varlink_error(link, "io.test.BadParameters", NULL); - y = json_variant_integer(b); + y = sd_json_variant_integer(b); - r = json_build(&ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("sum", JSON_BUILD_INTEGER(x + y)))); + r = sd_json_build(&ret, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("sum", SD_JSON_BUILD_INTEGER(x + y)))); if (r < 0) return r; return varlink_reply(link, ret); } -static int method_something_more(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *ret = NULL; +static int method_something_more(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; int r; struct Something { @@ -59,9 +60,9 @@ static int method_something_more(Varlink *link, JsonVariant *parameters, Varlink int y; }; - static const JsonDispatch dispatch_table[] = { - { "a", JSON_VARIANT_INTEGER, json_dispatch_int, offsetof(struct Something, x), JSON_MANDATORY }, - { "b", JSON_VARIANT_INTEGER, json_dispatch_int, offsetof(struct Something, y), JSON_MANDATORY}, + static const sd_json_dispatch_field dispatch_table[] = { + { "a", SD_JSON_VARIANT_INTEGER, sd_json_dispatch_int, offsetof(struct Something, x), SD_JSON_MANDATORY }, + { "b", SD_JSON_VARIANT_INTEGER, sd_json_dispatch_int, offsetof(struct Something, y), SD_JSON_MANDATORY}, {} }; struct Something s = {}; @@ -71,9 +72,9 @@ static int method_something_more(Varlink *link, JsonVariant *parameters, Varlink return r; for (int i = 0; i < 5; i++) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; - r = json_build(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("sum", JSON_BUILD_INTEGER(s.x + (s.y * i))))); + r = sd_json_build(&w, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("sum", SD_JSON_BUILD_INTEGER(s.x + (s.y * i))))); if (r < 0) return r; @@ -82,7 +83,7 @@ static int method_something_more(Varlink *link, JsonVariant *parameters, Varlink return r; } - r = json_build(&ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("sum", JSON_BUILD_INTEGER(s.x + (s.y * 5))))); + r = sd_json_build(&ret, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("sum", SD_JSON_BUILD_INTEGER(s.x + (s.y * 5))))); if (r < 0) return r; @@ -98,16 +99,16 @@ static void test_fd(int fd, const void *buf, size_t n) { assert_se(memcmp_nn(buf, n, rbuf, m) == 0); } -static int method_passfd(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *ret = NULL; - JsonVariant *a; +static int method_passfd(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; + sd_json_variant *a; int r; - a = json_variant_by_key(parameters, "fd"); + a = sd_json_variant_by_key(parameters, "fd"); if (!a) return varlink_error(link, "io.test.BadParameters", NULL); - ASSERT_STREQ(json_variant_string(a), "whoop"); + ASSERT_STREQ(sd_json_variant_string(a), "whoop"); int xx = varlink_peek_fd(link, 0), yy = varlink_peek_fd(link, 1), @@ -129,7 +130,7 @@ static int method_passfd(Varlink *link, JsonVariant *parameters, VarlinkMethodFl assert_se(vv >= 0); assert_se(ww >= 0); - r = json_build(&ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("yo", JSON_BUILD_INTEGER(88)))); + r = sd_json_build(&ret, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("yo", SD_JSON_BUILD_INTEGER(88)))); if (r < 0) return r; @@ -142,7 +143,7 @@ static int method_passfd(Varlink *link, JsonVariant *parameters, VarlinkMethodFl return varlink_reply(link, ret); } -static int method_done(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int method_done(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { if (++n_done == 2) sd_event_exit(varlink_get_event(link), EXIT_FAILURE); @@ -150,12 +151,12 @@ static int method_done(Varlink *link, JsonVariant *parameters, VarlinkMethodFlag return 0; } -static int reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { - JsonVariant *sum; +static int reply(Varlink *link, sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { + sd_json_variant *sum; - sum = json_variant_by_key(parameters, "sum"); + sum = sd_json_variant_by_key(parameters, "sum"); - assert_se(json_variant_integer(sum) == 7+22); + assert_se(sd_json_variant_integer(sum) == 7+22); if (++n_done == 2) sd_event_exit(varlink_get_event(link), EXIT_FAILURE); @@ -177,7 +178,7 @@ static int on_connect(VarlinkServer *s, Varlink *link, void *userdata) { return 0; } -static int overload_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { +static int overload_reply(Varlink *link, sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { /* This method call reply should always be called with a disconnection, since the method call should * be talking to an overloaded server */ @@ -213,7 +214,7 @@ static void flood_test(const char *address) { assert_se(asprintf(&t, "flood-%zu", k) >= 0); assert_se(varlink_set_description(connections[k], t) >= 0); assert_se(varlink_attach_event(connections[k], e, k) >= 0); - assert_se(varlink_sendb(connections[k], "io.test.Rubbish", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("id", JSON_BUILD_INTEGER(k)))) >= 0); + assert_se(varlink_sendb(connections[k], "io.test.Rubbish", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("id", SD_JSON_BUILD_INTEGER(k)))) >= 0); } /* Then, create one more, which should fail */ @@ -222,7 +223,7 @@ static void flood_test(const char *address) { assert_se(varlink_set_description(c, "overload-client") >= 0); assert_se(varlink_attach_event(c, e, k) >= 0); assert_se(varlink_bind_reply(c, overload_reply) >= 0); - assert_se(varlink_invokeb(c, "io.test.Overload", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("foo", JSON_BUILD_CONST_STRING("bar")))) >= 0); + assert_se(varlink_invokeb(c, "io.test.Overload", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("foo", JSON_BUILD_CONST_STRING("bar")))) >= 0); /* Unblock it */ log_debug("Unblocking server..."); @@ -238,15 +239,15 @@ static void flood_test(const char *address) { static void *thread(void *arg) { _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *i = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *wrong = NULL; - JsonVariant *o = NULL, *k = NULL, *j = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *i = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *wrong = NULL; + sd_json_variant *o = NULL, *k = NULL, *j = NULL; const char *error_id; const char *e; int x = 0; - assert_se(json_build(&i, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(88)), - JSON_BUILD_PAIR("b", JSON_BUILD_INTEGER(99)))) >= 0); + assert_se(sd_json_build(&i, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_INTEGER(88)), + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_INTEGER(99)))) >= 0); assert_se(varlink_connect_address(&c, arg) >= 0); assert_se(varlink_set_description(c, "thread-client") >= 0); @@ -254,8 +255,8 @@ static void *thread(void *arg) { assert_se(varlink_set_allow_fd_passing_output(c, true) >= 0); /* Test that client is able to perform two sequential varlink_collect calls if first resulted in an error */ - assert_se(json_build(&wrong, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(88)), - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(99)))) >= 0); + assert_se(sd_json_build(&wrong, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_INTEGER(88)), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(99)))) >= 0); assert_se(varlink_collect(c, "io.test.DoSomethingMore", wrong, &j, &error_id) >= 0); assert_se(strcmp_ptr(error_id, "org.varlink.service.InvalidParameter") == 0); @@ -263,16 +264,16 @@ static void *thread(void *arg) { assert_se(varlink_collect(c, "io.test.DoSomethingMore", i, &j, &error_id) >= 0); assert_se(!error_id); - assert_se(json_variant_is_array(j) && !json_variant_is_blank_array(j)); + assert_se(sd_json_variant_is_array(j) && !sd_json_variant_is_blank_array(j)); JSON_VARIANT_ARRAY_FOREACH(k, j) { - assert_se(json_variant_integer(json_variant_by_key(k, "sum")) == 88 + (99 * x)); + assert_se(sd_json_variant_integer(sd_json_variant_by_key(k, "sum")) == 88 + (99 * x)); x++; } assert_se(x == 6); assert_se(varlink_call(c, "io.test.DoSomething", i, &o, &e) >= 0); - assert_se(json_variant_integer(json_variant_by_key(o, "sum")) == 88 + 99); + assert_se(sd_json_variant_integer(sd_json_variant_by_key(o, "sum")) == 88 + 99); assert_se(!e); int fd1 = acquire_data_fd("foo"); @@ -287,7 +288,7 @@ static void *thread(void *arg) { assert_se(varlink_push_fd(c, fd2) == 1); assert_se(varlink_push_fd(c, fd3) == 2); - assert_se(varlink_callb(c, "io.test.PassFD", &o, &e, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("fd", JSON_BUILD_STRING("whoop")))) >= 0); + assert_se(varlink_callb(c, "io.test.PassFD", &o, &e, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("fd", SD_JSON_BUILD_STRING("whoop")))) >= 0); int fd4 = varlink_peek_fd(c, 0); int fd5 = varlink_peek_fd(c, 1); @@ -298,8 +299,8 @@ static void *thread(void *arg) { test_fd(fd4, "miau", 4); test_fd(fd5, "wuff", 4); - assert_se(varlink_callb(c, "io.test.IDontExist", &o, &e, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("x", JSON_BUILD_REAL(5.5)))) >= 0); - ASSERT_STREQ(json_variant_string(json_variant_by_key(o, "method")), "io.test.IDontExist"); + assert_se(varlink_callb(c, "io.test.IDontExist", &o, &e, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("x", SD_JSON_BUILD_REAL(5.5)))) >= 0); + ASSERT_STREQ(sd_json_variant_string(sd_json_variant_by_key(o, "method")), "io.test.IDontExist"); ASSERT_STREQ(e, VARLINK_ERROR_METHOD_NOT_FOUND); flood_test(arg); @@ -331,7 +332,7 @@ int main(int argc, char *argv[]) { _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL; _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL; _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(sd_event_unrefp) sd_event *e = NULL; _cleanup_close_pair_ int block_fds[2] = EBADF_PAIR; pthread_t t; @@ -361,8 +362,8 @@ int main(int argc, char *argv[]) { assert_se(varlink_server_attach_event(s, e, 0) >= 0); assert_se(varlink_server_set_connections_max(s, OVERLOAD_CONNECTIONS) >= 0); - assert_se(json_build(&v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(7)), - JSON_BUILD_PAIR("b", JSON_BUILD_INTEGER(22)))) >= 0); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_INTEGER(7)), + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_INTEGER(22)))) >= 0); assert_se(varlink_connect_address(&c, sp) >= 0); assert_se(varlink_set_description(c, "main-client") >= 0); |