static void test_strbuf(void) {
        struct strbuf *sb;
        _cleanup_strv_free_ char **l;
        ssize_t a, b, c, d, e, f, g;

        sb = strbuf_new();

        a = add_string(sb, "waldo");
        b = add_string(sb, "foo");
        c = add_string(sb, "bar");
        d = add_string(sb, "waldo");   /* duplicate */
        e = add_string(sb, "aldo");    /* duplicate */
        f = add_string(sb, "do");      /* duplicate */
        g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */

        /* check the content of the buffer directly */
        l = strv_parse_nulstr(sb->buf, sb->len);

        assert(streq(l[0], "")); /* root*/
        assert(streq(l[1], "waldo"));
        assert(streq(l[2], "foo"));
        assert(streq(l[3], "bar"));
        assert(streq(l[4], "waldorf"));

        assert(sb->nodes_count == 5); /* root + 4 non-duplicates */
        assert(sb->dedup_count == 3);
        assert(sb->in_count == 7);

        assert(sb->in_len == 29);    /* length of all strings added */
        assert(sb->dedup_len == 11); /* length of all strings duplicated */
        assert(sb->len == 23);       /* buffer length: in - dedup + \0 for each node */

        /* check the returned offsets and the respective content in the buffer */
        assert(a == 1);
        assert(b == 7);
        assert(c == 11);
        assert(d == 1);
        assert(e == 2);
        assert(f == 4);
        assert(g == 15);

        assert(streq(sb->buf + a, "waldo"));
        assert(streq(sb->buf + b, "foo"));
        assert(streq(sb->buf + c, "bar"));
        assert(streq(sb->buf + d, "waldo"));
        assert(streq(sb->buf + e, "aldo"));
        assert(streq(sb->buf + f, "do"));
        assert(streq(sb->buf + g, "waldorf"));

        strbuf_complete(sb);
        assert(sb->root == NULL);

        strbuf_cleanup(sb);
}
Exemple #2
0
_public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) {
        assert_return(c, -EINVAL);

        if (!(c->mask & SD_BUS_CREDS_CMDLINE))
                return -ENODATA;

        if (!c->cmdline)
                return -ENXIO;

        if (!c->cmdline_array) {
                c->cmdline_array = strv_parse_nulstr(c->cmdline, c->cmdline_size);
                if (!c->cmdline_array)
                        return -ENOMEM;
        }

        *cmdline = c->cmdline_array;
        return 0;
}