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