Markus Armbruster | 6c873d1 | 2017-02-28 22:27:03 +0100 | [diff] [blame] | 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 | #include "qapi/util.h" |
| 16 | #include "test-qapi-types.h" |
| 17 | |
| 18 | static void test_qapi_enum_parse(void) |
| 19 | { |
| 20 | Error *err = NULL; |
| 21 | int ret; |
| 22 | |
| 23 | ret = qapi_enum_parse(QType_lookup, NULL, QTYPE__MAX, QTYPE_NONE, |
| 24 | &error_abort); |
| 25 | g_assert_cmpint(ret, ==, QTYPE_NONE); |
| 26 | |
| 27 | ret = qapi_enum_parse(QType_lookup, "junk", QTYPE__MAX, -1, |
| 28 | NULL); |
| 29 | g_assert_cmpint(ret, ==, -1); |
| 30 | |
| 31 | ret = qapi_enum_parse(QType_lookup, "junk", QTYPE__MAX, -1, |
| 32 | &err); |
| 33 | error_free_or_abort(&err); |
| 34 | |
| 35 | ret = qapi_enum_parse(QType_lookup, "none", QTYPE__MAX, -1, |
| 36 | &error_abort); |
| 37 | g_assert_cmpint(ret, ==, QTYPE_NONE); |
| 38 | |
| 39 | ret = qapi_enum_parse(QType_lookup, QType_lookup[QTYPE__MAX - 1], |
| 40 | QTYPE__MAX, QTYPE__MAX - 1, |
| 41 | &error_abort); |
| 42 | g_assert_cmpint(ret, ==, QTYPE__MAX - 1); |
| 43 | } |
| 44 | |
Markus Armbruster | 069b64e | 2017-02-28 22:27:04 +0100 | [diff] [blame] | 45 | static void test_parse_qapi_name(void) |
| 46 | { |
| 47 | int ret; |
| 48 | |
| 49 | /* Must start with a letter */ |
| 50 | ret = parse_qapi_name("a", true); |
| 51 | g_assert(ret == 1); |
| 52 | ret = parse_qapi_name("a$", false); |
| 53 | g_assert(ret == 1); |
| 54 | ret = parse_qapi_name("", false); |
| 55 | g_assert(ret == -1); |
| 56 | ret = parse_qapi_name("1", false); |
| 57 | g_assert(ret == -1); |
| 58 | |
| 59 | /* Only letters, digits, hyphen, underscore */ |
| 60 | ret = parse_qapi_name("A-Za-z0-9_", true); |
| 61 | g_assert(ret == 10); |
| 62 | ret = parse_qapi_name("A-Za-z0-9_$", false); |
| 63 | g_assert(ret == 10); |
| 64 | ret = parse_qapi_name("A-Za-z0-9_$", true); |
| 65 | g_assert(ret == -1); |
| 66 | |
| 67 | /* __RFQDN_ */ |
| 68 | ret = parse_qapi_name("__com.redhat_supports", true); |
| 69 | g_assert(ret == 21); |
| 70 | ret = parse_qapi_name("_com.example_", false); |
| 71 | g_assert(ret == -1); |
| 72 | ret = parse_qapi_name("__com.example", false); |
| 73 | g_assert(ret == -1); |
| 74 | ret = parse_qapi_name("__com.example_", false); |
| 75 | g_assert(ret == -1); |
| 76 | } |
| 77 | |
Markus Armbruster | 6c873d1 | 2017-02-28 22:27:03 +0100 | [diff] [blame] | 78 | int main(int argc, char *argv[]) |
| 79 | { |
| 80 | g_test_init(&argc, &argv, NULL); |
| 81 | g_test_add_func("/qapi/util/qapi_enum_parse", test_qapi_enum_parse); |
Markus Armbruster | 069b64e | 2017-02-28 22:27:04 +0100 | [diff] [blame] | 82 | g_test_add_func("/qapi/util/parse_qapi_name", test_parse_qapi_name); |
Markus Armbruster | 6c873d1 | 2017-02-28 22:27:03 +0100 | [diff] [blame] | 83 | g_test_run(); |
| 84 | return 0; |
| 85 | } |