xref: /openbmc/qemu/tests/unit/test-qapi-util.c (revision f101c9fe)
1 /*
2  * Unit tests for QAPI utility functions
3  *
4  * Copyright (C) 2017 Red Hat Inc.
5  *
6  * Authors:
7  *  Markus Armbruster <armbru@redhat.com>,
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 
16 static void test_qapi_enum_parse(void)
17 {
18     Error *err = NULL;
19     int ret;
20 
21     ret = qapi_enum_parse(&QType_lookup, NULL, QTYPE_NONE, &error_abort);
22     g_assert_cmpint(ret, ==, QTYPE_NONE);
23 
24     ret = qapi_enum_parse(&QType_lookup, "junk", -1, NULL);
25     g_assert_cmpint(ret, ==, -1);
26 
27     ret = qapi_enum_parse(&QType_lookup, "junk", -1, &err);
28     error_free_or_abort(&err);
29 
30     ret = qapi_enum_parse(&QType_lookup, "none", -1, &error_abort);
31     g_assert_cmpint(ret, ==, QTYPE_NONE);
32 
33     ret = qapi_enum_parse(&QType_lookup, QType_str(QTYPE__MAX - 1),
34                           QTYPE__MAX - 1, &error_abort);
35     g_assert_cmpint(ret, ==, QTYPE__MAX - 1);
36 }
37 
38 static void test_parse_qapi_name(void)
39 {
40     int ret;
41 
42     /* Must start with a letter */
43     ret = parse_qapi_name("a", true);
44     g_assert(ret == 1);
45     ret = parse_qapi_name("a$", false);
46     g_assert(ret == 1);
47     ret = parse_qapi_name("", false);
48     g_assert(ret == -1);
49     ret = parse_qapi_name("1", false);
50     g_assert(ret == -1);
51 
52     /* Only letters, digits, hyphen, underscore */
53     ret = parse_qapi_name("A-Za-z0-9_", true);
54     g_assert(ret == 10);
55     ret = parse_qapi_name("A-Za-z0-9_$", false);
56     g_assert(ret == 10);
57     ret = parse_qapi_name("A-Za-z0-9_$", true);
58     g_assert(ret == -1);
59 
60     /* __RFQDN_ */
61     ret = parse_qapi_name("__com.redhat_supports", true);
62     g_assert(ret == 21);
63     ret = parse_qapi_name("_com.example_", false);
64     g_assert(ret == -1);
65     ret = parse_qapi_name("__com.example", false);
66     g_assert(ret == -1);
67     ret = parse_qapi_name("__com.example_", false);
68     g_assert(ret == -1);
69 }
70 
71 int main(int argc, char *argv[])
72 {
73     g_test_init(&argc, &argv, NULL);
74     g_test_add_func("/qapi/util/qapi_enum_parse", test_qapi_enum_parse);
75     g_test_add_func("/qapi/util/parse_qapi_name", test_parse_qapi_name);
76     g_test_run();
77     return 0;
78 }
79