diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-07-12 11:22:58 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-07-16 11:57:32 +0200 |
commit | 25ff515b398da61171a570baee641290374cec1a (patch) | |
tree | a41558cc31c2ed18c4b3262ed1812cdc5e0cc360 /src/test | |
parent | Merge pull request #33695 from DaanDeMeyer/epel (diff) | |
download | systemd-25ff515b398da61171a570baee641290374cec1a.tar.xz systemd-25ff515b398da61171a570baee641290374cec1a.zip |
sd-varlink: make our internal Varlink API public as sd-varlink.[ch]
It's time. sd-json was already done earlier in this cycle, let's now
make sd-varlink public too.
This is mostly just a search/replace job of epical proportions.
I left some functions internal (mostly IDL handling), and I turned some
static inline calls into regular calls.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-varlink-idl.c | 240 | ||||
-rw-r--r-- | src/test/test-varlink.c | 152 |
2 files changed, 197 insertions, 195 deletions
diff --git a/src/test/test-varlink-idl.c b/src/test/test-varlink-idl.c index c2d385eb56..4283ae7c01 100644 --- a/src/test/test-varlink-idl.c +++ b/src/test/test-varlink-idl.c @@ -2,11 +2,13 @@ #include <pthread.h> +#include "sd-varlink.h" +#include "sd-varlink-idl.h" + #include "fd-util.h" #include "pretty-print.h" #include "tests.h" -#include "varlink.h" -#include "varlink-idl.h" +#include "varlink-idl-util.h" #include "varlink-io.systemd.h" #include "varlink-io.systemd.BootControl.h" #include "varlink-io.systemd.Credentials.h" @@ -26,90 +28,90 @@ #include "varlink-io.systemd.sysext.h" #include "varlink-org.varlink.service.h" -static VARLINK_DEFINE_ENUM_TYPE( +static SD_VARLINK_DEFINE_ENUM_TYPE( EnumTest, - VARLINK_FIELD_COMMENT("piff paff"), - VARLINK_DEFINE_ENUM_VALUE(foo), - VARLINK_FIELD_COMMENT("waldo"), - VARLINK_DEFINE_ENUM_VALUE(bar), - VARLINK_FIELD_COMMENT("crux"), - VARLINK_DEFINE_ENUM_VALUE(baz)); - -static VARLINK_DEFINE_STRUCT_TYPE( + SD_VARLINK_FIELD_COMMENT("piff paff"), + SD_VARLINK_DEFINE_ENUM_VALUE(foo), + SD_VARLINK_FIELD_COMMENT("waldo"), + SD_VARLINK_DEFINE_ENUM_VALUE(bar), + SD_VARLINK_FIELD_COMMENT("crux"), + SD_VARLINK_DEFINE_ENUM_VALUE(baz)); + +static SD_VARLINK_DEFINE_STRUCT_TYPE( NestedStructTest, - VARLINK_FIELD_COMMENT("miepf"), - VARLINK_DEFINE_FIELD(x, VARLINK_INT, 0)); + SD_VARLINK_FIELD_COMMENT("miepf"), + SD_VARLINK_DEFINE_FIELD(x, SD_VARLINK_INT, 0)); -static VARLINK_DEFINE_STRUCT_TYPE( +static SD_VARLINK_DEFINE_STRUCT_TYPE( StructTest, - VARLINK_DEFINE_FIELD(bbb, VARLINK_BOOL, 0), - VARLINK_DEFINE_FIELD(bbbn, VARLINK_BOOL, VARLINK_NULLABLE), - VARLINK_DEFINE_FIELD(bbba, VARLINK_BOOL, VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(bbbna, VARLINK_BOOL, VARLINK_NULLABLE|VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(bbbm, VARLINK_BOOL, VARLINK_MAP), - VARLINK_DEFINE_FIELD(bbbnm, VARLINK_BOOL, VARLINK_NULLABLE|VARLINK_MAP), - - VARLINK_FIELD_COMMENT("more from here"), - - VARLINK_DEFINE_FIELD(iii, VARLINK_INT, 0), - VARLINK_DEFINE_FIELD(iiin, VARLINK_INT, VARLINK_NULLABLE), - VARLINK_DEFINE_FIELD(iiia, VARLINK_INT, VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(iiina, VARLINK_INT, VARLINK_NULLABLE|VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(iiim, VARLINK_INT, VARLINK_MAP), - VARLINK_DEFINE_FIELD(iiinm, VARLINK_INT, VARLINK_NULLABLE|VARLINK_MAP), - - VARLINK_DEFINE_FIELD(fff, VARLINK_FLOAT, 0), - VARLINK_DEFINE_FIELD(fffn, VARLINK_FLOAT, VARLINK_NULLABLE), - VARLINK_DEFINE_FIELD(fffa, VARLINK_FLOAT, VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(fffna, VARLINK_FLOAT, VARLINK_NULLABLE|VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(fffm, VARLINK_FLOAT, VARLINK_MAP), - VARLINK_DEFINE_FIELD(fffnm, VARLINK_FLOAT, VARLINK_NULLABLE|VARLINK_MAP), - - VARLINK_DEFINE_FIELD(sss, VARLINK_STRING, 0), - VARLINK_DEFINE_FIELD(sssn, VARLINK_STRING, VARLINK_NULLABLE), - VARLINK_DEFINE_FIELD(sssa, VARLINK_STRING, VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(sssna, VARLINK_STRING, VARLINK_NULLABLE|VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(sssm, VARLINK_STRING, VARLINK_MAP), - VARLINK_DEFINE_FIELD(sssnm, VARLINK_STRING, VARLINK_NULLABLE|VARLINK_MAP), - - VARLINK_DEFINE_FIELD(ooo, VARLINK_OBJECT, 0), - VARLINK_DEFINE_FIELD(ooon, VARLINK_OBJECT, VARLINK_NULLABLE), - VARLINK_DEFINE_FIELD(oooa, VARLINK_OBJECT, VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(ooona, VARLINK_OBJECT, VARLINK_NULLABLE|VARLINK_ARRAY), - VARLINK_DEFINE_FIELD(ooom, VARLINK_OBJECT, VARLINK_MAP), - VARLINK_DEFINE_FIELD(ooonm, VARLINK_OBJECT, VARLINK_NULLABLE|VARLINK_MAP), - - VARLINK_DEFINE_FIELD_BY_TYPE(eee, EnumTest, 0), - VARLINK_DEFINE_FIELD_BY_TYPE(eeen, EnumTest, VARLINK_NULLABLE), - VARLINK_DEFINE_FIELD_BY_TYPE(eeea, EnumTest, VARLINK_ARRAY), - VARLINK_DEFINE_FIELD_BY_TYPE(eeena, EnumTest, VARLINK_NULLABLE|VARLINK_ARRAY), - VARLINK_DEFINE_FIELD_BY_TYPE(eeem, EnumTest, VARLINK_MAP), - VARLINK_DEFINE_FIELD_BY_TYPE(eeenm, EnumTest, VARLINK_NULLABLE|VARLINK_MAP), - - VARLINK_DEFINE_FIELD_BY_TYPE(nnn, NestedStructTest, 0), - VARLINK_DEFINE_FIELD_BY_TYPE(nnnn, NestedStructTest, VARLINK_NULLABLE), - VARLINK_DEFINE_FIELD_BY_TYPE(nnna, NestedStructTest, VARLINK_ARRAY), - VARLINK_DEFINE_FIELD_BY_TYPE(nnnna, NestedStructTest, VARLINK_NULLABLE|VARLINK_ARRAY), - VARLINK_DEFINE_FIELD_BY_TYPE(nnnm, NestedStructTest, VARLINK_MAP), - VARLINK_DEFINE_FIELD_BY_TYPE(nnnnm, NestedStructTest, VARLINK_NULLABLE|VARLINK_MAP)); - -static VARLINK_DEFINE_METHOD( + SD_VARLINK_DEFINE_FIELD(bbb, SD_VARLINK_BOOL, 0), + SD_VARLINK_DEFINE_FIELD(bbbn, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE), + SD_VARLINK_DEFINE_FIELD(bbba, SD_VARLINK_BOOL, SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(bbbna, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(bbbm, SD_VARLINK_BOOL, SD_VARLINK_MAP), + SD_VARLINK_DEFINE_FIELD(bbbnm, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE|SD_VARLINK_MAP), + + SD_VARLINK_FIELD_COMMENT("more from here"), + + SD_VARLINK_DEFINE_FIELD(iii, SD_VARLINK_INT, 0), + SD_VARLINK_DEFINE_FIELD(iiin, SD_VARLINK_INT, SD_VARLINK_NULLABLE), + SD_VARLINK_DEFINE_FIELD(iiia, SD_VARLINK_INT, SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(iiina, SD_VARLINK_INT, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(iiim, SD_VARLINK_INT, SD_VARLINK_MAP), + SD_VARLINK_DEFINE_FIELD(iiinm, SD_VARLINK_INT, SD_VARLINK_NULLABLE|SD_VARLINK_MAP), + + SD_VARLINK_DEFINE_FIELD(fff, SD_VARLINK_FLOAT, 0), + SD_VARLINK_DEFINE_FIELD(fffn, SD_VARLINK_FLOAT, SD_VARLINK_NULLABLE), + SD_VARLINK_DEFINE_FIELD(fffa, SD_VARLINK_FLOAT, SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(fffna, SD_VARLINK_FLOAT, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(fffm, SD_VARLINK_FLOAT, SD_VARLINK_MAP), + SD_VARLINK_DEFINE_FIELD(fffnm, SD_VARLINK_FLOAT, SD_VARLINK_NULLABLE|SD_VARLINK_MAP), + + SD_VARLINK_DEFINE_FIELD(sss, SD_VARLINK_STRING, 0), + SD_VARLINK_DEFINE_FIELD(sssn, SD_VARLINK_STRING, SD_VARLINK_NULLABLE), + SD_VARLINK_DEFINE_FIELD(sssa, SD_VARLINK_STRING, SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(sssna, SD_VARLINK_STRING, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(sssm, SD_VARLINK_STRING, SD_VARLINK_MAP), + SD_VARLINK_DEFINE_FIELD(sssnm, SD_VARLINK_STRING, SD_VARLINK_NULLABLE|SD_VARLINK_MAP), + + SD_VARLINK_DEFINE_FIELD(ooo, SD_VARLINK_OBJECT, 0), + SD_VARLINK_DEFINE_FIELD(ooon, SD_VARLINK_OBJECT, SD_VARLINK_NULLABLE), + SD_VARLINK_DEFINE_FIELD(oooa, SD_VARLINK_OBJECT, SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(ooona, SD_VARLINK_OBJECT, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD(ooom, SD_VARLINK_OBJECT, SD_VARLINK_MAP), + SD_VARLINK_DEFINE_FIELD(ooonm, SD_VARLINK_OBJECT, SD_VARLINK_NULLABLE|SD_VARLINK_MAP), + + SD_VARLINK_DEFINE_FIELD_BY_TYPE(eee, EnumTest, 0), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(eeen, EnumTest, SD_VARLINK_NULLABLE), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(eeea, EnumTest, SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(eeena, EnumTest, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(eeem, EnumTest, SD_VARLINK_MAP), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(eeenm, EnumTest, SD_VARLINK_NULLABLE|SD_VARLINK_MAP), + + SD_VARLINK_DEFINE_FIELD_BY_TYPE(nnn, NestedStructTest, 0), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(nnnn, NestedStructTest, SD_VARLINK_NULLABLE), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(nnna, NestedStructTest, SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(nnnna, NestedStructTest, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(nnnm, NestedStructTest, SD_VARLINK_MAP), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(nnnnm, NestedStructTest, SD_VARLINK_NULLABLE|SD_VARLINK_MAP)); + +static SD_VARLINK_DEFINE_METHOD( MethodTest, - VARLINK_DEFINE_INPUT(x, VARLINK_BOOL, 0), - VARLINK_DEFINE_INPUT_BY_TYPE(y, EnumTest, 0), - VARLINK_DEFINE_INPUT_BY_TYPE(z, StructTest, 0), - VARLINK_DEFINE_OUTPUT(x, VARLINK_BOOL, 0), - VARLINK_DEFINE_OUTPUT_BY_TYPE(y, EnumTest, 0), - VARLINK_DEFINE_OUTPUT_BY_TYPE(z, StructTest, 0)); - -static VARLINK_DEFINE_ERROR( + SD_VARLINK_DEFINE_INPUT(x, SD_VARLINK_BOOL, 0), + SD_VARLINK_DEFINE_INPUT_BY_TYPE(y, EnumTest, 0), + SD_VARLINK_DEFINE_INPUT_BY_TYPE(z, StructTest, 0), + SD_VARLINK_DEFINE_OUTPUT(x, SD_VARLINK_BOOL, 0), + SD_VARLINK_DEFINE_OUTPUT_BY_TYPE(y, EnumTest, 0), + SD_VARLINK_DEFINE_OUTPUT_BY_TYPE(z, StructTest, 0)); + +static SD_VARLINK_DEFINE_ERROR( ErrorTest, - VARLINK_DEFINE_FIELD(x, VARLINK_BOOL, 0), - VARLINK_DEFINE_FIELD_BY_TYPE(y, EnumTest, 0), - VARLINK_DEFINE_FIELD_BY_TYPE(z, StructTest, 0)); + SD_VARLINK_DEFINE_FIELD(x, SD_VARLINK_BOOL, 0), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(y, EnumTest, 0), + SD_VARLINK_DEFINE_FIELD_BY_TYPE(z, StructTest, 0)); -static VARLINK_DEFINE_INTERFACE( +static SD_VARLINK_DEFINE_INTERFACE( xyz_test, "xyz.test", &vl_type_EnumTest, @@ -118,18 +120,18 @@ static VARLINK_DEFINE_INTERFACE( &vl_method_MethodTest, &vl_error_ErrorTest); -static void test_parse_format_one(const VarlinkInterface *iface) { - _cleanup_(varlink_interface_freep) VarlinkInterface *parsed = NULL; +static void test_parse_format_one(const sd_varlink_interface *iface) { + _cleanup_(varlink_interface_freep) sd_varlink_interface *parsed = NULL; _cleanup_free_ char *text = NULL, *text2 = NULL; assert_se(iface); - assert_se(varlink_idl_dump(stdout, /* use_colors=*/ true, /* cols= */ SIZE_MAX, iface) >= 0); + assert_se(sd_varlink_idl_dump(stdout, iface, SD_VARLINK_IDL_FORMAT_COLOR, /* cols= */ SIZE_MAX) >= 0); assert_se(varlink_idl_consistent(iface, LOG_ERR) >= 0); - assert_se(varlink_idl_format(iface, &text) >= 0); + assert_se(sd_varlink_idl_format(iface, &text) >= 0); assert_se(varlink_idl_parse(text, NULL, NULL, &parsed) >= 0); assert_se(varlink_idl_consistent(parsed, LOG_ERR) >= 0); - assert_se(varlink_idl_format(parsed, &text2) >= 0); + assert_se(sd_varlink_idl_format(parsed, &text2) >= 0); ASSERT_STREQ(text, text2); @@ -138,12 +140,12 @@ static void test_parse_format_one(const VarlinkInterface *iface) { parsed = varlink_interface_free(parsed); /* Do the same thing, but aggressively line break, and make sure this is roundtrippable as well */ - assert_se(varlink_idl_dump(stdout, /* use_colors=*/ true, 23, iface) >= 0); + assert_se(sd_varlink_idl_dump(stdout, iface, SD_VARLINK_IDL_FORMAT_COLOR, 23) >= 0); assert_se(varlink_idl_consistent(iface, LOG_ERR) >= 0); - assert_se(varlink_idl_format_full(iface, 23, &text) >= 0); + assert_se(sd_varlink_idl_format_full(iface, 0, 23, &text) >= 0); assert_se(varlink_idl_parse(text, NULL, NULL, &parsed) >= 0); assert_se(varlink_idl_consistent(parsed, LOG_ERR) >= 0); - assert_se(varlink_idl_format_full(parsed, 23, &text2) >= 0); + assert_se(sd_varlink_idl_format_full(parsed, 0, 23, &text2) >= 0); ASSERT_STREQ(text, text2); } @@ -189,7 +191,7 @@ TEST(parse_format) { } TEST(parse) { - _cleanup_(varlink_interface_freep) VarlinkInterface *parsed = NULL; + _cleanup_(varlink_interface_freep) sd_varlink_interface *parsed = NULL; /* This one has (nested) enonymous enums and structs */ static const char text[] = @@ -277,7 +279,7 @@ TEST(qualified_symbol_name_is_valid) { TEST(validate_json) { - _cleanup_(varlink_interface_freep) VarlinkInterface *parsed = NULL; + _cleanup_(varlink_interface_freep) sd_varlink_interface *parsed = NULL; /* This one has (nested) enonymous enums and structs */ static const char text[] = @@ -301,13 +303,13 @@ TEST(validate_json) { sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO, stdout, NULL); - const VarlinkSymbol* symbol = ASSERT_PTR(varlink_idl_find_symbol(parsed, VARLINK_METHOD, "Mymethod")); + const sd_varlink_symbol* symbol = ASSERT_PTR(varlink_idl_find_symbol(parsed, SD_VARLINK_METHOD, "Mymethod")); assert_se(varlink_idl_validate_method_call(symbol, v, NULL) >= 0); } static int test_recursive_one(unsigned depth) { - _cleanup_(varlink_interface_freep) VarlinkInterface *parsed = NULL; + _cleanup_(varlink_interface_freep) sd_varlink_interface *parsed = NULL; _cleanup_free_ char *pre = NULL, *post = NULL, *text = NULL; static const char header[] = "interface recursive.test\n" @@ -335,31 +337,31 @@ TEST(recursive) { assert_se(test_recursive_one(20000) < 0 ); } -static int test_method(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int test_method(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { sd_json_variant *foo = sd_json_variant_by_key(parameters, "foo"), *bar = sd_json_variant_by_key(parameters, "bar"); - return varlink_replyb(link, + return sd_varlink_replyb(link, SD_JSON_BUILD_OBJECT( SD_JSON_BUILD_PAIR_UNSIGNED("waldo", sd_json_variant_unsigned(foo) * sd_json_variant_unsigned(bar)), SD_JSON_BUILD_PAIR_UNSIGNED("quux", sd_json_variant_unsigned(foo) + sd_json_variant_unsigned(bar)))); } -static int done_method(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - assert_se(sd_event_exit(varlink_get_event(link), 0) >= 0); +static int done_method(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { + assert_se(sd_event_exit(sd_varlink_get_event(link), 0) >= 0); return 0; } -static VARLINK_DEFINE_METHOD( +static SD_VARLINK_DEFINE_METHOD( TestMethod, - VARLINK_DEFINE_INPUT(foo, VARLINK_INT, 0), - VARLINK_DEFINE_INPUT(bar, VARLINK_INT, 0), - VARLINK_DEFINE_INPUT(optional, VARLINK_STRING, VARLINK_NULLABLE), - VARLINK_DEFINE_OUTPUT(waldo, VARLINK_INT, 0), - VARLINK_DEFINE_OUTPUT(quux, VARLINK_INT, 0)); + SD_VARLINK_DEFINE_INPUT(foo, SD_VARLINK_INT, 0), + SD_VARLINK_DEFINE_INPUT(bar, SD_VARLINK_INT, 0), + SD_VARLINK_DEFINE_INPUT(optional, SD_VARLINK_STRING, SD_VARLINK_NULLABLE), + SD_VARLINK_DEFINE_OUTPUT(waldo, SD_VARLINK_INT, 0), + SD_VARLINK_DEFINE_OUTPUT(quux, SD_VARLINK_INT, 0)); -static VARLINK_DEFINE_METHOD(Done); +static SD_VARLINK_DEFINE_METHOD(Done); -static VARLINK_DEFINE_INTERFACE( +static SD_VARLINK_DEFINE_INTERFACE( xyz, "xyz", &vl_method_TestMethod, @@ -367,18 +369,18 @@ static VARLINK_DEFINE_INTERFACE( static void* server_thread(void *userdata) { - _cleanup_(varlink_server_unrefp) VarlinkServer *server = NULL; + _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *server = NULL; _cleanup_(sd_event_unrefp) sd_event *event = NULL; - assert_se(varlink_server_new(&server, 0) >= 0); - assert_se(varlink_server_add_interface(server, &vl_interface_xyz) >= 0); - assert_se(varlink_server_bind_method(server, "xyz.TestMethod", test_method) >= 0); - assert_se(varlink_server_bind_method(server, "xyz.Done", done_method) >= 0); + assert_se(sd_varlink_server_new(&server, 0) >= 0); + assert_se(sd_varlink_server_add_interface(server, &vl_interface_xyz) >= 0); + assert_se(sd_varlink_server_bind_method(server, "xyz.TestMethod", test_method) >= 0); + assert_se(sd_varlink_server_bind_method(server, "xyz.Done", done_method) >= 0); assert_se(sd_event_new(&event) >= 0); - assert_se(varlink_server_attach_event(server, event, 0) >= 0); + assert_se(sd_varlink_server_attach_event(server, event, 0) >= 0); - assert_se(varlink_server_add_connection(server, PTR_TO_FD(userdata), NULL) >= 0); + assert_se(sd_varlink_server_add_connection(server, PTR_TO_FD(userdata), NULL) >= 0); assert_se(sd_event_loop(event) >= 0); return NULL; @@ -386,16 +388,16 @@ static void* server_thread(void *userdata) { TEST(validate_method_call) { _cleanup_close_pair_ int fd[2] = EBADF_PAIR; - _cleanup_(varlink_unrefp) Varlink *v = NULL; + _cleanup_(sd_varlink_unrefp) sd_varlink *v = NULL; pthread_t t; assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0, fd) >= 0); assert_se(pthread_create(&t, NULL, server_thread, FD_TO_PTR(TAKE_FD(fd[1]))) == 0); - assert_se(varlink_connect_fd(&v, TAKE_FD(fd[0])) >= 0); + assert_se(sd_varlink_connect_fd(&v, TAKE_FD(fd[0])) >= 0); sd_json_variant *reply = NULL; const char *error_id = NULL; - assert_se(varlink_callb(v, "xyz.TestMethod", &reply, &error_id, + assert_se(sd_varlink_callb(v, "xyz.TestMethod", &reply, &error_id, SD_JSON_BUILD_OBJECT( SD_JSON_BUILD_PAIR_UNSIGNED("foo", 8), SD_JSON_BUILD_PAIR_UNSIGNED("bar", 9))) >= 0); @@ -412,7 +414,7 @@ TEST(validate_method_call) { sd_json_variant_dump(expected_reply, SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO, NULL, NULL); assert_se(sd_json_variant_equal(reply, expected_reply)); - assert_se(varlink_callb(v, "xyz.TestMethod", &reply, &error_id, + assert_se(sd_varlink_callb(v, "xyz.TestMethod", &reply, &error_id, SD_JSON_BUILD_OBJECT( SD_JSON_BUILD_PAIR_UNSIGNED("foo", 9), SD_JSON_BUILD_PAIR_UNSIGNED("bar", 8), @@ -421,21 +423,21 @@ TEST(validate_method_call) { assert_se(!error_id); assert_se(sd_json_variant_equal(reply, expected_reply)); - assert_se(varlink_callb(v, "xyz.TestMethod", &reply, &error_id, + assert_se(sd_varlink_callb(v, "xyz.TestMethod", &reply, &error_id, SD_JSON_BUILD_OBJECT( SD_JSON_BUILD_PAIR_UNSIGNED("foo", 8), SD_JSON_BUILD_PAIR_UNSIGNED("bar", 9), SD_JSON_BUILD_PAIR_STRING("zzz", "pfft"))) >= 0); - ASSERT_STREQ(error_id, VARLINK_ERROR_INVALID_PARAMETER); + ASSERT_STREQ(error_id, SD_VARLINK_ERROR_INVALID_PARAMETER); - assert_se(varlink_callb(v, "xyz.TestMethod", &reply, &error_id, + assert_se(sd_varlink_callb(v, "xyz.TestMethod", &reply, &error_id, SD_JSON_BUILD_OBJECT( SD_JSON_BUILD_PAIR_BOOLEAN("foo", true), SD_JSON_BUILD_PAIR_UNSIGNED("bar", 9))) >= 0); - ASSERT_STREQ(error_id, VARLINK_ERROR_INVALID_PARAMETER); + ASSERT_STREQ(error_id, SD_VARLINK_ERROR_INVALID_PARAMETER); - assert_se(varlink_send(v, "xyz.Done", NULL) >= 0); - assert_se(varlink_flush(v) >= 0); + assert_se(sd_varlink_send(v, "xyz.Done", NULL) >= 0); + assert_se(sd_varlink_flush(v) >= 0); assert_se(pthread_join(t, NULL) == 0); } diff --git a/src/test/test-varlink.c b/src/test/test-varlink.c index 48fc4a7e7f..a80344bcd1 100644 --- a/src/test/test-varlink.c +++ b/src/test/test-varlink.c @@ -6,6 +6,7 @@ #include "sd-event.h" #include "sd-json.h" +#include "sd-varlink.h" #include "data-fd-util.h" #include "fd-util.h" @@ -15,7 +16,6 @@ #include "tests.h" #include "tmpfile-util.h" #include "user-util.h" -#include "varlink.h" /* Let's pick some high value, that is higher than the largest listen() backlog, but leaves enough room below the typical RLIMIT_NOFILE value of 1024 so that we can process both sides of each socket in our @@ -26,7 +26,7 @@ static int n_done = 0; static int block_write_fd = -EBADF; -static int method_something(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int method_something(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; sd_json_variant *a, *b; int64_t x, y; @@ -34,13 +34,13 @@ static int method_something(Varlink *link, sd_json_variant *parameters, VarlinkM a = sd_json_variant_by_key(parameters, "a"); if (!a) - return varlink_error(link, "io.test.BadParameters", NULL); + return sd_varlink_error(link, "io.test.BadParameters", NULL); x = sd_json_variant_integer(a); b = sd_json_variant_by_key(parameters, "b"); if (!b) - return varlink_error(link, "io.test.BadParameters", NULL); + return sd_varlink_error(link, "io.test.BadParameters", NULL); y = sd_json_variant_integer(b); @@ -48,10 +48,10 @@ static int method_something(Varlink *link, sd_json_variant *parameters, VarlinkM if (r < 0) return r; - return varlink_reply(link, ret); + return sd_varlink_reply(link, ret); } -static int method_something_more(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int method_something_more(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; int r; @@ -67,7 +67,7 @@ static int method_something_more(Varlink *link, sd_json_variant *parameters, Var }; struct Something s = {}; - r = varlink_dispatch(link, parameters, dispatch_table, &s); + r = sd_varlink_dispatch(link, parameters, dispatch_table, &s); if (r != 0) return r; @@ -78,7 +78,7 @@ static int method_something_more(Varlink *link, sd_json_variant *parameters, Var if (r < 0) return r; - r = varlink_notify(link, w); + r = sd_varlink_notify(link, w); if (r < 0) return r; } @@ -87,7 +87,7 @@ static int method_something_more(Varlink *link, sd_json_variant *parameters, Var if (r < 0) return r; - return varlink_reply(link, ret); + return sd_varlink_reply(link, ret); } static void test_fd(int fd, const void *buf, size_t n) { @@ -99,20 +99,20 @@ 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, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int method_passfd(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; sd_json_variant *a; int r; a = sd_json_variant_by_key(parameters, "fd"); if (!a) - return varlink_error(link, "io.test.BadParameters", NULL); + return sd_varlink_error(link, "io.test.BadParameters", NULL); ASSERT_STREQ(sd_json_variant_string(a), "whoop"); - int xx = varlink_peek_fd(link, 0), - yy = varlink_peek_fd(link, 1), - zz = varlink_peek_fd(link, 2); + int xx = sd_varlink_peek_fd(link, 0), + yy = sd_varlink_peek_fd(link, 1), + zz = sd_varlink_peek_fd(link, 2); log_info("%i %i %i", xx, yy, zz); @@ -134,24 +134,24 @@ static int method_passfd(Varlink *link, sd_json_variant *parameters, VarlinkMeth if (r < 0) return r; - assert_se(varlink_push_fd(link, vv) == 0); - assert_se(varlink_push_fd(link, ww) == 1); + assert_se(sd_varlink_push_fd(link, vv) == 0); + assert_se(sd_varlink_push_fd(link, ww) == 1); TAKE_FD(vv); TAKE_FD(ww); - return varlink_reply(link, ret); + return sd_varlink_reply(link, ret); } -static int method_done(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int method_done(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { if (++n_done == 2) - sd_event_exit(varlink_get_event(link), EXIT_FAILURE); + sd_event_exit(sd_varlink_get_event(link), EXIT_FAILURE); return 0; } -static int reply(Varlink *link, sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { +static int reply(sd_varlink *link, sd_json_variant *parameters, const char *error_id, sd_varlink_reply_flags_t flags, void *userdata) { sd_json_variant *sum; sum = sd_json_variant_by_key(parameters, "sum"); @@ -159,41 +159,41 @@ static int reply(Varlink *link, sd_json_variant *parameters, const char *error_i assert_se(sd_json_variant_integer(sum) == 7+22); if (++n_done == 2) - sd_event_exit(varlink_get_event(link), EXIT_FAILURE); + sd_event_exit(sd_varlink_get_event(link), EXIT_FAILURE); return 0; } -static int on_connect(VarlinkServer *s, Varlink *link, void *userdata) { +static int on_connect(sd_varlink_server *s, sd_varlink *link, void *userdata) { uid_t uid = UID_INVALID; assert_se(s); assert_se(link); - assert_se(varlink_get_peer_uid(link, &uid) >= 0); + assert_se(sd_varlink_get_peer_uid(link, &uid) >= 0); assert_se(getuid() == uid); - assert_se(varlink_set_allow_fd_passing_input(link, true) >= 0); - assert_se(varlink_set_allow_fd_passing_output(link, true) >= 0); + assert_se(sd_varlink_set_allow_fd_passing_input(link, true) >= 0); + assert_se(sd_varlink_set_allow_fd_passing_output(link, true) >= 0); return 0; } -static int overload_reply(Varlink *link, sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { +static int overload_reply(sd_varlink *link, sd_json_variant *parameters, const char *error_id, sd_varlink_reply_flags_t 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 */ log_debug("Over reply triggered with error: %s", strna(error_id)); - ASSERT_STREQ(error_id, VARLINK_ERROR_DISCONNECTED); - sd_event_exit(varlink_get_event(link), 0); + ASSERT_STREQ(error_id, SD_VARLINK_ERROR_DISCONNECTED); + sd_event_exit(sd_varlink_get_event(link), 0); return 0; } static void flood_test(const char *address) { - _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL; + _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *c = NULL; _cleanup_(sd_event_unrefp) sd_event *e = NULL; - _cleanup_free_ Varlink **connections = NULL; + _cleanup_free_ sd_varlink **connections = NULL; size_t k; char x = 'x'; @@ -205,25 +205,25 @@ static void flood_test(const char *address) { assert_se(sd_event_default(&e) >= 0); /* Flood the server with connections */ - assert_se(connections = new0(Varlink*, OVERLOAD_CONNECTIONS)); + assert_se(connections = new0(sd_varlink*, OVERLOAD_CONNECTIONS)); for (k = 0; k < OVERLOAD_CONNECTIONS; k++) { _cleanup_free_ char *t = NULL; log_debug("connection %zu", k); - assert_se(varlink_connect_address(connections + k, address) >= 0); + assert_se(sd_varlink_connect_address(connections + k, address) >= 0); 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", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("id", SD_JSON_BUILD_INTEGER(k)))) >= 0); + assert_se(sd_varlink_set_description(connections[k], t) >= 0); + assert_se(sd_varlink_attach_event(connections[k], e, k) >= 0); + assert_se(sd_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 */ log_debug("Creating overload connection..."); - assert_se(varlink_connect_address(&c, address) >= 0); - 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", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("foo", JSON_BUILD_CONST_STRING("bar")))) >= 0); + assert_se(sd_varlink_connect_address(&c, address) >= 0); + assert_se(sd_varlink_set_description(c, "overload-client") >= 0); + assert_se(sd_varlink_attach_event(c, e, k) >= 0); + assert_se(sd_varlink_bind_reply(c, overload_reply) >= 0); + assert_se(sd_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..."); @@ -234,11 +234,11 @@ static void flood_test(const char *address) { /* And close all connections again */ for (k = 0; k < OVERLOAD_CONNECTIONS; k++) - connections[k] = varlink_unref(connections[k]); + connections[k] = sd_varlink_unref(connections[k]); } static void *thread(void *arg) { - _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL; + _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *c = 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; @@ -249,19 +249,19 @@ static void *thread(void *arg) { 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); - assert_se(varlink_set_allow_fd_passing_input(c, true) >= 0); - assert_se(varlink_set_allow_fd_passing_output(c, true) >= 0); + assert_se(sd_varlink_connect_address(&c, arg) >= 0); + assert_se(sd_varlink_set_description(c, "thread-client") >= 0); + assert_se(sd_varlink_set_allow_fd_passing_input(c, true) >= 0); + assert_se(sd_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 */ + /* Test that client is able to perform two sequential sd_varlink_collect calls if first resulted in an error */ 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(sd_varlink_collect(c, "io.test.DoSomethingMore", wrong, &j, &error_id) >= 0); assert_se(strcmp_ptr(error_id, "org.varlink.service.InvalidParameter") == 0); - assert_se(varlink_collect(c, "io.test.DoSomethingMore", i, &j, &error_id) >= 0); + assert_se(sd_varlink_collect(c, "io.test.DoSomethingMore", i, &j, &error_id) >= 0); assert_se(!error_id); assert_se(sd_json_variant_is_array(j) && !sd_json_variant_is_blank_array(j)); @@ -272,7 +272,7 @@ static void *thread(void *arg) { } assert_se(x == 6); - assert_se(varlink_call(c, "io.test.DoSomething", i, &o, &e) >= 0); + assert_se(sd_varlink_call(c, "io.test.DoSomething", i, &o, &e) >= 0); assert_se(sd_json_variant_integer(sd_json_variant_by_key(o, "sum")) == 88 + 99); assert_se(!e); @@ -284,14 +284,14 @@ static void *thread(void *arg) { assert_se(fd2 >= 0); assert_se(fd3 >= 0); - assert_se(varlink_push_fd(c, fd1) == 0); - assert_se(varlink_push_fd(c, fd2) == 1); - assert_se(varlink_push_fd(c, fd3) == 2); + assert_se(sd_varlink_push_fd(c, fd1) == 0); + assert_se(sd_varlink_push_fd(c, fd2) == 1); + assert_se(sd_varlink_push_fd(c, fd3) == 2); - 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); + assert_se(sd_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); + int fd4 = sd_varlink_peek_fd(c, 0); + int fd5 = sd_varlink_peek_fd(c, 1); assert_se(fd4 >= 0); assert_se(fd5 >= 0); @@ -299,13 +299,13 @@ 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, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("x", SD_JSON_BUILD_REAL(5.5)))) >= 0); + assert_se(sd_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); + ASSERT_STREQ(e, SD_VARLINK_ERROR_METHOD_NOT_FOUND); flood_test(arg); - assert_se(varlink_send(c, "io.test.Done", NULL) >= 0); + assert_se(sd_varlink_send(c, "io.test.Done", NULL) >= 0); return NULL; } @@ -329,8 +329,8 @@ static int block_fd_handler(sd_event_source *s, int fd, uint32_t revents, void * int main(int argc, char *argv[]) { _cleanup_(sd_event_source_unrefp) sd_event_source *block_event = NULL; - _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL; - _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL; + _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *s = NULL; + _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *c = NULL; _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL; _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(sd_event_unrefp) sd_event *e = NULL; @@ -350,28 +350,28 @@ int main(int argc, char *argv[]) { assert_se(sd_event_source_set_priority(block_event, SD_EVENT_PRIORITY_IMPORTANT) >= 0); block_write_fd = TAKE_FD(block_fds[1]); - assert_se(varlink_server_new(&s, VARLINK_SERVER_ACCOUNT_UID) >= 0); - assert_se(varlink_server_set_description(s, "our-server") >= 0); + assert_se(sd_varlink_server_new(&s, SD_VARLINK_SERVER_ACCOUNT_UID) >= 0); + assert_se(sd_varlink_server_set_description(s, "our-server") >= 0); - assert_se(varlink_server_bind_method(s, "io.test.PassFD", method_passfd) >= 0); - assert_se(varlink_server_bind_method(s, "io.test.DoSomething", method_something) >= 0); - assert_se(varlink_server_bind_method(s, "io.test.DoSomethingMore", method_something_more) >= 0); - assert_se(varlink_server_bind_method(s, "io.test.Done", method_done) >= 0); - assert_se(varlink_server_bind_connect(s, on_connect) >= 0); - assert_se(varlink_server_listen_address(s, sp, 0600) >= 0); - assert_se(varlink_server_attach_event(s, e, 0) >= 0); - assert_se(varlink_server_set_connections_max(s, OVERLOAD_CONNECTIONS) >= 0); + assert_se(sd_varlink_server_bind_method(s, "io.test.PassFD", method_passfd) >= 0); + assert_se(sd_varlink_server_bind_method(s, "io.test.DoSomething", method_something) >= 0); + assert_se(sd_varlink_server_bind_method(s, "io.test.DoSomethingMore", method_something_more) >= 0); + assert_se(sd_varlink_server_bind_method(s, "io.test.Done", method_done) >= 0); + assert_se(sd_varlink_server_bind_connect(s, on_connect) >= 0); + assert_se(sd_varlink_server_listen_address(s, sp, 0600) >= 0); + assert_se(sd_varlink_server_attach_event(s, e, 0) >= 0); + assert_se(sd_varlink_server_set_connections_max(s, OVERLOAD_CONNECTIONS) >= 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); - assert_se(varlink_bind_reply(c, reply) >= 0); + assert_se(sd_varlink_connect_address(&c, sp) >= 0); + assert_se(sd_varlink_set_description(c, "main-client") >= 0); + assert_se(sd_varlink_bind_reply(c, reply) >= 0); - assert_se(varlink_invoke(c, "io.test.DoSomething", v) >= 0); + assert_se(sd_varlink_invoke(c, "io.test.DoSomething", v) >= 0); - assert_se(varlink_attach_event(c, e, 0) >= 0); + assert_se(sd_varlink_attach_event(c, e, 0) >= 0); assert_se(pthread_create(&t, NULL, thread, (void*) sp) == 0); |