[systemd-devel] [PATCH 1/3] tests: add tests to test-unit-name
Tom Gundersen
teg at jklm.no
Mon Jun 23 17:43:42 PDT 2014
Applied all three. Thanks!
Tom
On Mon, Jun 23, 2014 at 9:28 PM, Ronny Chevalier
<chevalier.ronny at gmail.com> wrote:
> add tests for:
> - unit_instance_is_valid
> - unit_prefix_is_valid
> - unit_name_change_suffix
> - unit_name_build
> - unit_name_is_instance
> - build_subslice
> - unit_name_to_instance
> - unit_name_escape
> ---
> src/test/test-unit-name.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 127 insertions(+)
>
> diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c
> index fff3e16..9179081 100644
> --- a/src/test/test-unit-name.c
> +++ b/src/test/test-unit-name.c
> @@ -5,6 +5,7 @@
>
> Copyright 2012 Lennart Poettering
> Copyright 2013 Zbigniew Jędrzejewski-Szmek
> + Copyright 2014 Ronny Chevalier
>
> systemd is free software; you can redistribute it and/or modify it
> under the terms of the GNU Lesser General Public License as published by
> @@ -193,9 +194,135 @@ static int test_unit_printf(void) {
> return 0;
> }
>
> +static void test_unit_instance_is_valid(void) {
> + assert_se(unit_instance_is_valid("fooBar"));
> + assert_se(unit_instance_is_valid("foo-bar"));
> + assert_se(unit_instance_is_valid("foo.stUff"));
> + assert_se(unit_instance_is_valid("fOo123.stuff"));
> + assert_se(unit_instance_is_valid("@f_oo123.Stuff"));
> +
> + assert_se(!unit_instance_is_valid("$¢£"));
> + assert_se(!unit_instance_is_valid(""));
> + assert_se(!unit_instance_is_valid("foo bar"));
> + assert_se(!unit_instance_is_valid("foo/bar"));
> +}
> +
> +static void test_unit_prefix_is_valid(void) {
> + assert_se(unit_prefix_is_valid("fooBar"));
> + assert_se(unit_prefix_is_valid("foo-bar"));
> + assert_se(unit_prefix_is_valid("foo.stUff"));
> + assert_se(unit_prefix_is_valid("fOo123.stuff"));
> + assert_se(unit_prefix_is_valid("foo123.Stuff"));
> +
> + assert_se(!unit_prefix_is_valid("$¢£"));
> + assert_se(!unit_prefix_is_valid(""));
> + assert_se(!unit_prefix_is_valid("foo bar"));
> + assert_se(!unit_prefix_is_valid("foo/bar"));
> + assert_se(!unit_prefix_is_valid("@foo-bar"));
> +}
> +
> +static void test_unit_name_change_suffix(void) {
> + char *r;
> +
> + r = unit_name_change_suffix("foo.bar", ".service");
> + assert_se(r);
> + assert_se(streq(r, "foo.service"));
> + free(r);
> +
> + r = unit_name_change_suffix("foo at stuff.bar", ".boo");
> + assert_se(r);
> + assert_se(streq(r, "foo at stuff.boo"));
> + free(r);
> +}
> +
> +static void test_unit_name_build(void) {
> + char *r;
> +
> + r = unit_name_build("foo", "bar", ".service");
> + assert_se(r);
> + assert_se(streq(r, "foo at bar.service"));
> + free(r);
> +
> + r = unit_name_build("fo0-stUff_b", "bar", ".mount");
> + assert_se(r);
> + assert_se(streq(r, "fo0-stUff_b at bar.mount"));
> + free(r);
> +
> + r = unit_name_build("foo", NULL, ".service");
> + assert_se(r);
> + assert_se(streq(r, "foo.service"));
> + free(r);
> +}
> +
> +static void test_unit_name_is_instance(void) {
> + assert_se(unit_name_is_instance("a at b.service"));
> + assert_se(unit_name_is_instance("a-c_c01Aj at b05Dii_-oioi.service"));
> +
> + assert_se(!unit_name_is_instance("a.service"));
> + assert_se(!unit_name_is_instance("junk"));
> + assert_se(!unit_name_is_instance(""));
> +}
> +
> +static void test_build_subslice(void) {
> + char *a;
> + char *b;
> +
> + assert_se(build_subslice("-.slice", "foo", &a) >= 0);
> + assert_se(build_subslice(a, "bar", &b) >= 0);
> + free(a);
> + assert_se(build_subslice(b, "barfoo", &a) >= 0);
> + free(b);
> + assert_se(build_subslice(a, "foobar", &b) >= 0);
> + free(a);
> + assert_se(streq(b, "foo-bar-barfoo-foobar.slice"));
> + free(b);
> +
> + assert_se(build_subslice("foo.service", "bar", &a) < 0);
> + assert_se(build_subslice("foo", "bar", &a) < 0);
> +}
> +
> +static void test_unit_name_to_instance(void) {
> + char *instance;
> + int r;
> +
> + r = unit_name_to_instance("foo at bar.service", &instance);
> + assert_se(r >= 0);
> + assert_se(streq(instance, "bar"));
> + free(instance);
> +
> + r = unit_name_to_instance("fo0-stUff_b at b.e", &instance);
> + assert_se(r >= 0);
> + assert_se(streq(instance, "b"));
> + free(instance);
> +
> + r = unit_name_to_instance("foo.bar", &instance);
> + assert_se(r >= 0);
> + assert_se(!instance);
> +
> + r = unit_name_to_instance("fooj at unk", &instance);
> + assert_se(r < 0);
> +}
> +
> +static void test_unit_name_escape(void) {
> + _cleanup_free_ char *r;
> +
> + r = unit_name_escape("ab+-c.a/bc at foo.service");
> + assert_se(r);
> + assert_se(streq(r, "ab\\x2b\\x2dc.a-bc\\x40foo.service"));
> +}
> +
> int main(int argc, char* argv[]) {
> int rc = 0;
> test_replacements();
> TEST_REQ_RUNNING_SYSTEMD(rc = test_unit_printf());
> + test_unit_instance_is_valid();
> + test_unit_prefix_is_valid();
> + test_unit_name_change_suffix();
> + test_unit_name_build();
> + test_unit_name_is_instance();
> + test_build_subslice();
> + test_unit_name_to_instance();
> + test_unit_name_escape();
> +
> return rc;
> }
> --
> 2.0.0
>
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
More information about the systemd-devel
mailing list