/* test generated deallocation on an object whose construction was prematurely
 * terminated due to an error */
static void test_dealloc_partial(void)
{
    static const char text[] = "don't leak me";

    UserDefTwo *ud2 = NULL;
    Error *err = NULL;

    /* create partial object */
    {
        QDict *ud2_dict;
        QmpInputVisitor *qiv;

        ud2_dict = qdict_new();
        qdict_put_obj(ud2_dict, "string0", QOBJECT(qstring_from_str(text)));

        qiv = qmp_input_visitor_new(QOBJECT(ud2_dict));
        visit_type_UserDefTwo(qmp_input_get_visitor(qiv), &ud2, NULL, &err);
        qmp_input_visitor_cleanup(qiv);
        QDECREF(ud2_dict);
    }

    /* verify partial success */
    assert(ud2 != NULL);
    assert(ud2->string0 != NULL);
    assert(strcmp(ud2->string0, text) == 0);
    assert(ud2->dict1 == NULL);

    /* confirm & release construction error */
    assert(err != NULL);
    error_free(err);

    /* tear down partial object */
    qapi_free_UserDefTwo(ud2);
}
Beispiel #2
0
void qapi_copy_SocketAddress(SocketAddress **p_dest,
                             SocketAddress *src)
{
    QmpOutputVisitor *qov;
    QmpInputVisitor *qiv;
    Visitor *ov, *iv;
    QObject *obj;

    *p_dest = NULL;

    qov = qmp_output_visitor_new();
    ov = qmp_output_get_visitor(qov);
    visit_type_SocketAddress(ov, NULL, &src, &error_abort);
    obj = qmp_output_get_qobject(qov);
    qmp_output_visitor_cleanup(qov);
    if (!obj) {
        return;
    }

    qiv = qmp_input_visitor_new(obj);
    iv = qmp_input_get_visitor(qiv);
    visit_type_SocketAddress(iv, NULL, p_dest, &error_abort);
    qmp_input_visitor_cleanup(qiv);
    qobject_decref(obj);
}
Beispiel #3
0
static void qnull_visit_test(void)
{
    QObject *obj;
    QmpOutputVisitor *qov;
    QmpInputVisitor *qiv;

    /*
     * Most tests of interactions between QObject and visitors are in
     * test-qmp-*-visitor; but these tests live here because they
     * depend on layering violations to check qnull_ refcnt.
     */

    g_assert(qnull_.refcnt == 1);
    obj = qnull();
    qiv = qmp_input_visitor_new(obj, true);
    qobject_decref(obj);
    visit_type_null(qmp_input_get_visitor(qiv), NULL, &error_abort);
    qmp_input_visitor_cleanup(qiv);

    qov = qmp_output_visitor_new();
    visit_type_null(qmp_output_get_visitor(qov), NULL, &error_abort);
    obj = qmp_output_get_qobject(qov);
    g_assert(obj == &qnull_);
    qobject_decref(obj);
    qmp_output_visitor_cleanup(qov);

    g_assert(qnull_.refcnt == 1);
}
Beispiel #4
0
int qmp_marshal_input_cpu(Monitor *mon, const QDict *qdict, QObject **ret)
{
    Error *local_err = NULL;
    Error **errp = &local_err;
    QDict *args = (QDict *)qdict;
    QmpInputVisitor *mi;
    QapiDeallocVisitor *md;
    Visitor *v;
    int64_t index;

    mi = qmp_input_visitor_new(QOBJECT(args));
    v = qmp_input_get_visitor(mi);
    visit_type_int(v, &index, "index", errp);
    qmp_input_visitor_cleanup(mi);

    if (error_is_set(errp)) {
        goto out;
    }
    qmp_cpu(index, errp);

out:
    md = qapi_dealloc_visitor_new();
    v = qapi_dealloc_get_visitor(md);
    visit_type_int(v, &index, "index", errp);
    qapi_dealloc_visitor_cleanup(md);

    if (local_err) {
        qerror_report_err(local_err);
        error_free(local_err);
        return -1;
    }
    return 0;
}
Beispiel #5
0
QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj)
{
    QmpInputVisitor *v;

    v = qmp_input_visitor_new(obj);
    v->strict = true;

    return v;
}
Beispiel #6
0
void object_property_set_qobject(struct uc_struct *uc, Object *obj, QObject *value,
                                 const char *name, Error **errp)
{
    QmpInputVisitor *mi;
    mi = qmp_input_visitor_new(value);
    object_property_set(uc, obj, qmp_input_get_visitor(mi), name, errp);    // qq

    qmp_input_visitor_cleanup(mi);
}
/* The various test_init functions are provided instead of a test setup
   function so that the JSON string used by the tests are kept in the test
   functions (and not in main()). */
static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data,
                                                 const char *json_string,
                                                 va_list *ap)
{
    visitor_input_teardown(data, NULL);

    data->obj = qobject_from_jsonv(json_string, ap);
    g_assert(data->obj);

    data->qiv = qmp_input_visitor_new(data->obj, false);
    g_assert(data->qiv);
    return data->qiv;
}
Beispiel #8
0
/* test enum values nested in schema-defined structs */
static void test_nested_enums(void)
{
    QmpOutputVisitor *mo;
    QmpInputVisitor *mi;
    Visitor *v;
    NestedEnumsOne *nested_enums, *nested_enums_cpy = NULL;
    Error *err = NULL;
    QObject *obj;
    QString *str;

    nested_enums = g_malloc0(sizeof(NestedEnumsOne));
    nested_enums->enum1 = ENUM_ONE_VALUE1;
    nested_enums->enum2 = ENUM_ONE_VALUE2;
    nested_enums->enum3 = ENUM_ONE_VALUE3;
    nested_enums->enum4 = ENUM_ONE_VALUE3;
    nested_enums->has_enum2 = false;
    nested_enums->has_enum4 = true;

    /* C type -> QObject */
    mo = qmp_output_visitor_new();
    v = qmp_output_get_visitor(mo);
    visit_type_NestedEnumsOne(v, &nested_enums, NULL, &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }
    obj = qmp_output_get_qobject(mo);
    g_assert(obj);
    str = qobject_to_json_pretty(obj);
    g_print("%s\n", qstring_get_str(str));
    QDECREF(str);

    /* QObject -> C type */
    mi = qmp_input_visitor_new(obj);
    v = qmp_input_get_visitor(mi);
    visit_type_NestedEnumsOne(v, &nested_enums_cpy, NULL, &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }
    g_assert(nested_enums_cpy);
    g_assert(nested_enums_cpy->enum1 == nested_enums->enum1);
    g_assert(nested_enums_cpy->enum3 == nested_enums->enum3);
    g_assert(nested_enums_cpy->enum4 == nested_enums->enum4);
    g_assert(nested_enums_cpy->has_enum2 == false);
    g_assert(nested_enums_cpy->has_enum4 == true);

    qmp_output_visitor_cleanup(mo);
    qmp_input_visitor_cleanup(mi);
    qapi_free_NestedEnumsOne(nested_enums);
    qapi_free_NestedEnumsOne(nested_enums_cpy);
}
/* similar to visitor_input_test_init(), but does not expect a string
 * literal/format json_string argument and so can be used for
 * programatically generated strings (and we can't pass in programatically
 * generated strings via %s format parameters since qobject_from_jsonv()
 * will wrap those in double-quotes and treat the entire object as a
 * string)
 */
static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
                                            const char *json_string)
{
    Visitor *v;

    data->obj = qobject_from_json(json_string);

    g_assert(data->obj != NULL);

    data->qiv = qmp_input_visitor_new(data->obj);
    g_assert(data->qiv != NULL);

    v = qmp_input_get_visitor(data->qiv);
    g_assert(v != NULL);

    return v;
}
/* The various test_init functions are provided instead of a test setup
   function so that the JSON string used by the tests are kept in the test
   functions (and not in main()). */
static Visitor *validate_test_init_internal(TestInputVisitorData *data,
                                            const char *json_string,
                                            va_list *ap)
{
    Visitor *v;

    validate_teardown(data, NULL);

    data->obj = qobject_from_jsonv(json_string, ap);
    g_assert(data->obj);

    data->qiv = qmp_input_visitor_new(data->obj, true);
    g_assert(data->qiv);

    v = qmp_input_get_visitor(data->qiv);
    g_assert(v);

    return v;
}
/* This is provided instead of a test setup function so that the JSON
   string used by the tests are kept in the test functions (and not
   int main()) */
static Visitor *visitor_input_test_init(TestInputVisitorData *data,
                                        const char *json_string, ...)
{
    Visitor *v;
    va_list ap;

    va_start(ap, json_string);
    data->obj = qobject_from_jsonv(json_string, &ap);
    va_end(ap);

    g_assert(data->obj != NULL);

    data->qiv = qmp_input_visitor_new(data->obj);
    g_assert(data->qiv != NULL);

    v = qmp_input_get_visitor(data->qiv);
    g_assert(v != NULL);

    return v;
}
Beispiel #12
0
/* test enum values */
static void test_enums(void)
{
    QmpOutputVisitor *mo;
    QmpInputVisitor *mi;
    Visitor *v;
    EnumOne enum1 = ENUM_ONE_VALUE2, enum1_cpy = ENUM_ONE_VALUE1;
    Error *err = NULL;
    QObject *obj;
    QString *str;

    /* C type -> QObject */
    mo = qmp_output_visitor_new();
    v = qmp_output_get_visitor(mo);
    visit_type_EnumOne(v, &enum1, "unused", &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }
    obj = qmp_output_get_qobject(mo);
    g_assert(obj);
    str = qobject_to_json_pretty(obj);
    g_print("%s\n", qstring_get_str(str));
    QDECREF(str);
    g_assert(g_strcmp0(qstring_get_str(qobject_to_qstring(obj)), "value2") == 0);

    /* QObject -> C type */
    mi = qmp_input_visitor_new(obj);
    v = qmp_input_get_visitor(mi);
    visit_type_EnumOne(v, &enum1_cpy, "unused", &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }
    g_debug("enum1_cpy, enum1: %d, %d", enum1_cpy, enum1);
    g_assert(enum1_cpy == enum1);

    qobject_decref(obj);
}
Beispiel #13
0
/* test core visitor methods */
static void test_visitor_core(void)
{
    QmpOutputVisitor *mo;
    QmpInputVisitor *mi;
    Visitor *v;
    TestStruct ts = { 42, 82 };
    TestStruct *pts = &ts;
    TestStructList *lts = NULL;
    Error *err = NULL;
    QObject *obj;
    QList *qlist;
    QDict *qdict;
    QString *str;
    int64_t value = 0;

    mo = qmp_output_visitor_new();
    v = qmp_output_get_visitor(mo);

    visit_type_TestStruct(v, &pts, NULL, &err);

    obj = qmp_output_get_qobject(mo);

    str = qobject_to_json(obj);

    printf("%s\n", qstring_get_str(str));

    QDECREF(str);

    obj = QOBJECT(qint_from_int(0x42));

    mi = qmp_input_visitor_new(obj);
    v = qmp_input_get_visitor(mi);

    visit_type_int(v, &value, NULL, &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }

    g_assert(value == 0x42);

    qobject_decref(obj);

    obj = qobject_from_json("{'x': 42, 'y': 84}");
    mi = qmp_input_visitor_new(obj);
    v = qmp_input_get_visitor(mi);

    pts = NULL;

    visit_type_TestStruct(v, &pts, NULL, &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }

    g_assert(pts != NULL);
    g_assert(pts->x == 42);
    g_assert(pts->y == 84);

    qobject_decref(obj);
    g_free(pts);

    /* test list input visitor */
    obj = qobject_from_json("[{'x': 42, 'y': 84}, {'x': 12, 'y': 24}]");
    mi = qmp_input_visitor_new(obj);
    v = qmp_input_get_visitor(mi);

    visit_type_TestStructList(v, &lts, NULL, &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }

    g_assert(lts != NULL);
    g_assert(lts->value->x == 42);
    g_assert(lts->value->y == 84);

    g_assert(lts->next != NULL);
    g_assert(lts->next->value->x == 12);
    g_assert(lts->next->value->y == 24);
    g_assert(lts->next->next == NULL);

    qobject_decref(obj);

    /* test list output visitor */
    mo = qmp_output_visitor_new();
    v = qmp_output_get_visitor(mo);
    visit_type_TestStructList(v, &lts, NULL, &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }
    obj = qmp_output_get_qobject(mo);
    g_print("obj: %s\n", qstring_get_str(qobject_to_json(obj)));

    qlist = qobject_to_qlist(obj);
    assert(qlist);
    obj = qlist_pop(qlist);
    qdict = qobject_to_qdict(obj);
    assert(qdict);
    assert(qdict_get_int(qdict, "x") == 42);
    assert(qdict_get_int(qdict, "y") == 84);
    qobject_decref(obj);

    obj = qlist_pop(qlist);
    qdict = qobject_to_qdict(obj);
    assert(qdict);
    assert(qdict_get_int(qdict, "x") == 12);
    assert(qdict_get_int(qdict, "y") == 24);
    qobject_decref(obj);

    qmp_output_visitor_cleanup(mo);
    QDECREF(qlist);
}
Beispiel #14
0
/* test deep nesting with refs to other user-defined types */
static void test_nested_structs(void)
{
    QmpOutputVisitor *mo;
    QmpInputVisitor *mi;
    Visitor *v;
    UserDefOne ud1;
    UserDefOne *ud1_p = &ud1, *ud1c_p = NULL;
    UserDefTwo ud2;
    UserDefTwo *ud2_p = &ud2, *ud2c_p = NULL;
    Error *err = NULL;
    QObject *obj;
    QString *str;

    ud1.integer = 42;
    ud1.string = strdup("fourty two");

    /* sanity check */
    mo = qmp_output_visitor_new();
    v = qmp_output_get_visitor(mo);
    visit_type_UserDefOne(v, &ud1_p, "o_O", &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }
    obj = qmp_output_get_qobject(mo);
    g_assert(obj);
    qobject_decref(obj);

    ud2.string = strdup("fourty three");
    ud2.dict.string = strdup("fourty four");
    ud2.dict.dict.userdef = ud1_p;
    ud2.dict.dict.string = strdup("fourty five");
    ud2.dict.has_dict2 = true;
    ud2.dict.dict2.userdef = ud1_p;
    ud2.dict.dict2.string = strdup("fourty six");

    /* c type -> qobject */
    mo = qmp_output_visitor_new();
    v = qmp_output_get_visitor(mo);
    visit_type_UserDefTwo(v, &ud2_p, "unused", &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }
    obj = qmp_output_get_qobject(mo);
    g_assert(obj);
    str = qobject_to_json_pretty(obj);
    g_print("%s\n", qstring_get_str(str));
    QDECREF(str);

    /* qobject -> c type, should match original struct */
    mi = qmp_input_visitor_new(obj);
    v = qmp_input_get_visitor(mi);
    visit_type_UserDefTwo(v, &ud2c_p, NULL, &err);
    if (err) {
        g_error("%s", error_get_pretty(err));
    }

    g_assert(!g_strcmp0(ud2c_p->string, ud2.string));
    g_assert(!g_strcmp0(ud2c_p->dict.string, ud2.dict.string));

    ud1c_p = ud2c_p->dict.dict.userdef;
    g_assert(ud1c_p->integer == ud1_p->integer);
    g_assert(!g_strcmp0(ud1c_p->string, ud1_p->string));

    g_assert(!g_strcmp0(ud2c_p->dict.dict.string, ud2.dict.dict.string));

    ud1c_p = ud2c_p->dict.dict2.userdef;
    g_assert(ud1c_p->integer == ud1_p->integer);
    g_assert(!g_strcmp0(ud1c_p->string, ud1_p->string));

    g_assert(!g_strcmp0(ud2c_p->dict.dict2.string, ud2.dict.dict2.string));
    g_free(ud1.string);
    g_free(ud2.string);
    g_free(ud2.dict.string);
    g_free(ud2.dict.dict.string);
    g_free(ud2.dict.dict2.string);

    qapi_free_UserDefTwo(ud2c_p);

    qobject_decref(obj);
}