qemu/test-qmp-commands.c
<<
>>
Prefs
   1#include <glib.h>
   2#include "qemu-objects.h"
   3#include "test-qmp-commands.h"
   4#include "qapi/qmp-core.h"
   5#include "module.h"
   6
   7void qmp_user_def_cmd(Error **errp)
   8{
   9}
  10
  11void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
  12{
  13}
  14
  15UserDefTwo * qmp_user_def_cmd2(UserDefOne * ud1a, UserDefOne * ud1b, Error **errp)
  16{
  17    UserDefTwo *ret;
  18    UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
  19    UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
  20
  21    ud1c->string = strdup(ud1a->string);
  22    ud1c->integer = ud1a->integer;
  23    ud1d->string = strdup(ud1b->string);
  24    ud1d->integer = ud1b->integer;
  25
  26    ret = g_malloc0(sizeof(UserDefTwo));
  27    ret->string = strdup("blah1");
  28    ret->dict.string = strdup("blah2");
  29    ret->dict.dict.userdef = ud1c;
  30    ret->dict.dict.string = strdup("blah3");
  31    ret->dict.has_dict2 = true;
  32    ret->dict.dict2.userdef = ud1d;
  33    ret->dict.dict2.string = strdup("blah4");
  34
  35    return ret;
  36}
  37
  38/* test commands with no input and no return value */
  39static void test_dispatch_cmd(void)
  40{
  41    QDict *req = qdict_new();
  42    QObject *resp;
  43
  44    qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
  45
  46    resp = qmp_dispatch(QOBJECT(req));
  47    assert(resp != NULL);
  48    assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
  49    g_print("\nresp: %s\n", qstring_get_str(qobject_to_json(resp)));
  50
  51    qobject_decref(resp);
  52    QDECREF(req);
  53}
  54
  55/* test commands that return an error due to invalid parameters */
  56static void test_dispatch_cmd_error(void)
  57{
  58    QDict *req = qdict_new();
  59    QObject *resp;
  60
  61    qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
  62
  63    resp = qmp_dispatch(QOBJECT(req));
  64    assert(resp != NULL);
  65    assert(qdict_haskey(qobject_to_qdict(resp), "error"));
  66    g_print("\nresp: %s\n", qstring_get_str(qobject_to_json_pretty(resp)));
  67
  68    qobject_decref(resp);
  69    QDECREF(req);
  70}
  71
  72/* test commands that involve both input parameters and return values */
  73static void test_dispatch_cmd_io(void)
  74{
  75    QDict *req = qdict_new();
  76    QDict *args = qdict_new();
  77    QDict *ud1a = qdict_new();
  78    QDict *ud1b = qdict_new();
  79    QObject *resp;
  80
  81    qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
  82    qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
  83    qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
  84    qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
  85    qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
  86    qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
  87    qdict_put_obj(req, "arguments", QOBJECT(args));
  88
  89    qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
  90
  91    /* TODO: put in full payload and check for errors */
  92    resp = qmp_dispatch(QOBJECT(req));
  93    assert(resp != NULL);
  94    assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
  95    g_print("\nresp: %s\n", qstring_get_str(qobject_to_json_pretty(resp)));
  96
  97    qobject_decref(resp);
  98    QDECREF(req);
  99}
 100
 101/* test generated dealloc functions for generated types */
 102static void test_dealloc_types(void)
 103{
 104    UserDefOne *ud1test, *ud1a, *ud1b;
 105    UserDefOneList *ud1list;
 106
 107    ud1test = g_malloc0(sizeof(UserDefOne));
 108    ud1test->integer = 42;
 109    ud1test->string = g_strdup("hi there 42");
 110
 111    qapi_free_UserDefOne(ud1test);
 112
 113    ud1a = g_malloc0(sizeof(UserDefOne));
 114    ud1a->integer = 43;
 115    ud1a->string = g_strdup("hi there 43");
 116
 117    ud1b = g_malloc0(sizeof(UserDefOne));
 118    ud1b->integer = 44;
 119    ud1b->string = g_strdup("hi there 44");
 120
 121    ud1list = g_malloc0(sizeof(UserDefOneList));
 122    ud1list->value = ud1a;
 123    ud1list->next = g_malloc0(sizeof(UserDefOneList));
 124    ud1list->next->value = ud1b;
 125
 126    qapi_free_UserDefOneList(ud1list);
 127}
 128
 129int main(int argc, char **argv)
 130{
 131    g_test_init(&argc, &argv, NULL);
 132
 133    g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
 134    g_test_add_func("/0.15/dispatch_cmd_error", test_dispatch_cmd_error);
 135    g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
 136    g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
 137
 138    module_call_init(MODULE_INIT_QAPI);
 139    g_test_run();
 140
 141    return 0;
 142}
 143