From: Ivan Kruglov Date: Mon, 26 May 2025 10:02:59 +0000 (-0700) Subject: json: add unit tests for json_dispatch_const_unit_name() X-Git-Tag: v258-rc1~479^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2af33c3d1e89a06fdaa57052ad397beb432642ef;p=thirdparty%2Fsystemd.git json: add unit tests for json_dispatch_const_unit_name() --- diff --git a/src/test/test-json.c b/src/test/test-json.c index 482e053e7ac..065e84dad77 100644 --- a/src/test/test-json.c +++ b/src/test/test-json.c @@ -1422,4 +1422,57 @@ TEST(fd_info) { pidref_done(&pidref); } +TEST(unit_name) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + ASSERT_OK(sd_json_buildo(&v, + SD_JSON_BUILD_PAIR_STRING("plain", "myservice.service"), + SD_JSON_BUILD_PAIR_STRING("instance", "myservice@instance1.service"), + SD_JSON_BUILD_PAIR_STRING("template", "myservice@.service"))); + + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); + + struct { + const char *plain, *instance, *template; + } data = {}; + + ASSERT_OK(sd_json_dispatch( + v, + (const sd_json_dispatch_field[]) { + { "plain", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, plain), SD_JSON_STRICT }, + { "instance", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, instance), 0 }, + { "template", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, template), SD_JSON_RELAX }, + {}, + }, + /* flags= */ 0, + &data)); + + ASSERT_STREQ(data.plain, "myservice.service"); + ASSERT_STREQ(data.instance, "myservice@instance1.service"); + ASSERT_STREQ(data.template, "myservice@.service"); + + ASSERT_ERROR(sd_json_dispatch( + v, + (const sd_json_dispatch_field[]) { + { "plain", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, plain), SD_JSON_RELAX }, + /* instance value is not allowed with SD_JSON_STRICT */ + { "instance", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, instance), SD_JSON_STRICT }, + { "template", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, template), SD_JSON_RELAX }, + {}, + }, + /* flags= */ 0, + &data), EINVAL); + + ASSERT_ERROR(sd_json_dispatch( + v, + (const sd_json_dispatch_field[]) { + { "plain", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, plain), SD_JSON_RELAX }, + { "instance", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, instance), SD_JSON_RELAX }, + /* template value is not allowed by default */ + { "template", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, voffsetof(data, template), 0 }, + {}, + }, + /* flags= */ 0, + &data), EINVAL); +} + DEFINE_TEST_MAIN(LOG_DEBUG);