示例#1
0
static void test_writer_compound(void) {
        const char *type = "(uaum(s)u)";
        unsigned int u1, u2, u3, u4, u5, u6;
        const char *s1;
        CVariant *cv;
        int r;

        /* allocate variant and write each entry sequentially */

        r = c_variant_new(&cv, type, strlen(type));
        assert(r >= 0);

        r = c_variant_begin(cv, "(");
        assert(r >= 0);

        r = c_variant_write(cv, "u", 0xffff);
        assert(r >= 0);

        r = c_variant_write(cv, "au", 4, 1, 2, 3, 4);
        assert(r >= 0);

        r = c_variant_write(cv, "m(s)", true, "foo");
        assert(r >= 0);

        r = c_variant_write(cv, "u", 0xffffffffU);
        assert(r >= 0);

        r = c_variant_end(cv, ")");
        assert(r >= 0);

        /* seal and verify */
        r = c_variant_seal(cv);
        assert(r >= 0);

        r = c_variant_read(cv, "(uaum(s)u)",
                           &u1,
                           4, &u2, &u3, &u4, &u5,
                           true, &s1,
                           &u6);
        assert(r >= 0);
        assert(u1 == 0xffff);
        assert(u2 == 1);
        assert(u3 == 2);
        assert(u4 == 3);
        assert(u5 == 4);
        assert(!strcmp(s1, "foo"));
        assert(u6 == 0xffffffffU);

        cv = c_variant_free(cv);
        assert(!cv);
}
示例#2
0
static void test_writer_basic(void) {
        const char *type;
        unsigned int u1;
        CVariant *cv;
        int r;

        /* simple 'u' type */
        type = "u";
        r = c_variant_new(&cv, type, strlen(type));
        assert(r >= 0);

        u1 = 0xf0f0;
        r = c_variant_write(cv, "u", u1);
        assert(r >= 0);

        r = c_variant_seal(cv);
        assert(r >= 0);

        u1 = 0;
        r = c_variant_read(cv, "u", &u1);
        assert(r >= 0);
        assert(u1 == 0xf0f0);

        cv = c_variant_free(cv);

        /* compound '(u)' type */
        type = "(u)";
        r = c_variant_new(&cv, type, strlen(type));
        assert(r >= 0);

        u1 = 0xf0f0;
        r = c_variant_write(cv, "(u)", u1);
        assert(r >= 0);

        r = c_variant_seal(cv);
        assert(r >= 0);

        u1 = 0;
        r = c_variant_read(cv, "(u)", &u1);
        assert(r >= 0);
        assert(u1 == 0xf0f0);

        cv = c_variant_free(cv);

        /* array 'au' type */
        type = "au";
        r = c_variant_new(&cv, type, strlen(type));
        assert(r >= 0);

        u1 = 0xf0f0;
        r = c_variant_write(cv, "au", 1, u1);
        assert(r >= 0);

        r = c_variant_seal(cv);
        assert(r >= 0);

        u1 = 0;
        r = c_variant_read(cv, "au", 1, &u1);
        assert(r >= 0);
        assert(u1 == 0xf0f0);

        cv = c_variant_free(cv);

        /* maybe 'mu' type */
        type = "mu";
        r = c_variant_new(&cv, type, strlen(type));
        assert(r >= 0);

        u1 = 0xf0f0;
        r = c_variant_write(cv, "mu", true, u1);
        assert(r >= 0);

        r = c_variant_seal(cv);
        assert(r >= 0);

        u1 = 0;
        r = c_variant_read(cv, "mu", true, &u1);
        assert(r >= 0);
        assert(u1 == 0xf0f0);

        cv = c_variant_free(cv);

        /* variant 'v', 'u' */
        type = "v";
        r = c_variant_new(&cv, type, strlen(type));
        assert(r >= 0);

        u1 = 0xf0f0;
        r = c_variant_write(cv, "v", "u", u1);
        assert(r >= 0);

        r = c_variant_seal(cv);
        assert(r >= 0);

        u1 = 0;
        r = c_variant_read(cv, "v", "u", &u1);
        assert(r >= 0);
        assert(u1 == 0xf0f0);

        cv = c_variant_free(cv);
}
示例#3
0
static void test_generate(const char *type, CVariant **cvp, GVariant **gvp) {
        GVariantBuilder *builder;
        CVariantVarg varg;
        CVariant *cv, *child;
        GVariant *gv, *gvc;
        uint64_t val_64;
        size_t n;
        char *s;
        int r, c;

        n = strlen(type);

        r = c_variant_new(&cv, type, n);
        assert(r >= 0);

        /* wrap as tuple as GVariantBuilder cannot deal with basic types */
        s = alloca(n + 2);
        s[0] = '(';
        memcpy(s + 1, type, n);
        s[n + 1] = ')';
        s[n + 2] = 0;
        builder = g_variant_builder_new(G_VARIANT_TYPE(s));

        for (c = c_variant_varg_init(&varg, type, strlen(type));
             c;
             c = c_variant_varg_next(&varg)) {
                val_64 = rand();
                val_64 <<= 32;
                val_64 |= rand();

                switch (c) {
                case -1:
                        c_variant_end(cv, NULL);
                        g_variant_builder_close(builder);
                        break;
                case C_VARIANT_VARIANT:
                        c_variant_new(&child, "u", 1);
                        c_variant_write(child, "u", (uint32_t)val_64);
                        c_variant_seal(child);
                        c_variant_write(cv, "v", child);
                        c_variant_free(child);
                        g_variant_builder_add(builder, "v",
                                              g_variant_new("u", (uint32_t)val_64));
                        break;
                case C_VARIANT_MAYBE:
                        c_variant_begin(cv, "m");
                        c_variant_varg_enter_bound(&varg, cv, val_64 & 1);

                        s = TEST_VARG_TYPE(&varg, "m", "");
                        g_variant_builder_open(builder, G_VARIANT_TYPE(s));
                        free(s);
                        break;
                case C_VARIANT_ARRAY:
                        c_variant_begin(cv, "a");
                        c_variant_varg_enter_bound(&varg, cv, val_64 & 0xf);

                        s = TEST_VARG_TYPE(&varg, "a", "");
                        g_variant_builder_open(builder, G_VARIANT_TYPE(s));
                        free(s);
                        break;
                case C_VARIANT_TUPLE_OPEN:
                        c_variant_begin(cv, "(");
                        c_variant_varg_enter_unbound(&varg, cv, ')');

                        s = TEST_VARG_TYPE(&varg, "(", ")");
                        g_variant_builder_open(builder, G_VARIANT_TYPE(s));
                        free(s);
                        break;
                case C_VARIANT_PAIR_OPEN:
                        c_variant_begin(cv, "{");
                        c_variant_varg_enter_unbound(&varg, cv, '}');

                        s = TEST_VARG_TYPE(&varg, "{", "}");
                        g_variant_builder_open(builder, G_VARIANT_TYPE(s));
                        free(s);
                        break;
                case C_VARIANT_INT64:
                        c_variant_write(cv, "x", val_64);
                        g_variant_builder_add(builder, "x", val_64);
                        break;
                case C_VARIANT_UINT64:
                        c_variant_write(cv, "t", val_64);
                        g_variant_builder_add(builder, "t", val_64);
                        break;
                case C_VARIANT_DOUBLE:
                        c_variant_write(cv, "d", *(double *)&val_64);
                        g_variant_builder_add(builder, "d", *(double *)&val_64);
                        break;
                case C_VARIANT_INT32:
                        c_variant_write(cv, "i", (uint32_t)val_64);
                        g_variant_builder_add(builder, "i", (uint32_t)val_64);
                        break;
                case C_VARIANT_UINT32:
                        c_variant_write(cv, "u", (uint32_t)val_64);
                        g_variant_builder_add(builder, "u", (uint32_t)val_64);
                        break;
                case C_VARIANT_HANDLE:
                        c_variant_write(cv, "h", (uint32_t)val_64);
                        g_variant_builder_add(builder, "h", (uint32_t)val_64);
                        break;
                case C_VARIANT_INT16:
                        c_variant_write(cv, "n", (int)val_64);
                        g_variant_builder_add(builder, "n", (int)val_64);
                        break;
                case C_VARIANT_UINT16:
                        c_variant_write(cv, "q", (int)val_64);
                        g_variant_builder_add(builder, "q", (int)val_64);
                        break;
                case C_VARIANT_BOOL:
                        c_variant_write(cv, "b", !!val_64);
                        g_variant_builder_add(builder, "b", !!val_64);
                        break;
                case C_VARIANT_BYTE:
                        c_variant_write(cv, "y", (int)val_64);
                        g_variant_builder_add(builder, "y", (int)val_64);
                        break;
                case C_VARIANT_STRING:
                        c_variant_write(cv, "s", "foobar");
                        g_variant_builder_add(builder, "s", "foobar");
                        break;
                case C_VARIANT_PATH:
                        c_variant_write(cv, "o", "/foo/bar");
                        g_variant_builder_add(builder, "o", "/foo/bar");
                        break;
                case C_VARIANT_SIGNATURE:
                        c_variant_write(cv, "g", "bison");
                        g_variant_builder_add(builder, "g", "bison");
                        break;
                default:
                        assert(0);
                        break;
                }
        }

        r = c_variant_seal(cv);
        assert(r >= 0);

        gv = g_variant_builder_end(builder);
        gvc = g_variant_get_child_value(gv, 0);
        g_variant_unref(gv);
        g_variant_builder_unref(builder);

        *cvp = cv;
        *gvp = gvc;
}