Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file xml.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from xml.c |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #define _BSD_SOURCE |
| 16 | #define _DEFAULT_SOURCE |
| 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <cmocka.h> |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "libyang.h" |
| 26 | #include "../../src/xml.c" |
| 27 | |
| 28 | #define BUFSIZE 1024 |
| 29 | char logbuf[BUFSIZE] = {0}; |
| 30 | |
| 31 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 32 | #define ENABLE_LOGGER_CHECKING 1 |
| 33 | |
| 34 | static void |
| 35 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 36 | { |
| 37 | (void) level; /* unused */ |
| 38 | |
| 39 | if (path) { |
| 40 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 41 | } else { |
| 42 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static int |
| 47 | logger_setup(void **state) |
| 48 | { |
| 49 | (void) state; /* unused */ |
| 50 | #if ENABLE_LOGGER_CHECKING |
| 51 | ly_set_log_clb(logger, 1); |
| 52 | #endif |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | logbuf_clean(void) |
| 58 | { |
| 59 | logbuf[0] = '\0'; |
| 60 | } |
| 61 | |
| 62 | #if ENABLE_LOGGER_CHECKING |
| 63 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 64 | #else |
| 65 | # define logbuf_assert(str) |
| 66 | #endif |
| 67 | |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 68 | |
| 69 | static void |
| 70 | test_utf8(void **state) |
| 71 | { |
| 72 | (void) state; /* unused */ |
| 73 | |
| 74 | char buf[5] = {0}; |
| 75 | const char *str = buf; |
| 76 | unsigned int c; |
| 77 | size_t len; |
| 78 | |
| 79 | /* test invalid UTF-8 characters in lyxml_getutf8 |
| 80 | * - https://en.wikipedia.org/wiki/UTF-8 */ |
Radek Krejci | de362c0 | 2018-09-21 16:27:37 +0200 | [diff] [blame] | 81 | buf[0] = 0x04; |
| 82 | assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len)); |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 83 | buf[0] = 0x80; |
| 84 | assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len)); |
| 85 | |
Radek Krejci | 234ace0 | 2018-09-21 17:01:12 +0200 | [diff] [blame^] | 86 | buf[0] = 0xc0; |
Radek Krejci | de362c0 | 2018-09-21 16:27:37 +0200 | [diff] [blame] | 87 | buf[1] = 0x00; |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 88 | assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len)); |
| 89 | buf[1] = 0x80; |
| 90 | assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len)); |
| 91 | |
Radek Krejci | 234ace0 | 2018-09-21 17:01:12 +0200 | [diff] [blame^] | 92 | buf[0] = 0xe0; |
| 93 | buf[1] = 0x00; |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 94 | buf[2] = 0x80; |
| 95 | assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len)); |
Radek Krejci | 234ace0 | 2018-09-21 17:01:12 +0200 | [diff] [blame^] | 96 | buf[1] = 0x80; |
| 97 | assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len)); |
| 98 | |
| 99 | buf[0] = 0xf0; |
| 100 | buf[1] = 0x00; |
| 101 | buf[2] = 0x80; |
| 102 | buf[3] = 0x80; |
| 103 | assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len)); |
| 104 | buf[1] = 0x80; |
| 105 | assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len)); |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 108 | static void |
| 109 | test_element(void **state) |
| 110 | { |
| 111 | (void) state; /* unused */ |
| 112 | |
| 113 | size_t name_len, prefix_len; |
| 114 | const char *name, *prefix; |
| 115 | const char *str, *p; |
| 116 | |
| 117 | struct lyxml_context ctx; |
| 118 | memset(&ctx, 0, sizeof ctx); |
| 119 | ctx.line = 1; |
| 120 | |
| 121 | /* empty */ |
| 122 | str = ""; |
| 123 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 124 | assert_null(name); |
| 125 | assert_true(str[0] == '\0'); |
| 126 | |
| 127 | /* no element */ |
| 128 | logbuf_clean(); |
| 129 | str = p = "no data present"; |
| 130 | assert_int_equal(LY_EINVAL, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 131 | assert_null(name); |
| 132 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 133 | logbuf_assert(""); |
| 134 | |
| 135 | /* not supported DOCTYPE */ |
| 136 | str = p = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>"; |
| 137 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 138 | assert_null(name); |
| 139 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 140 | logbuf_assert("Document Type Declaration not supported. Line number 1."); |
| 141 | |
| 142 | /* unqualified element */ |
| 143 | str = " < element/>"; |
| 144 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 145 | assert_null(prefix); |
| 146 | assert_false(strncmp("element", name, name_len)); |
| 147 | assert_int_equal(7, name_len); |
| 148 | assert_string_equal("/>", str); |
| 149 | |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 150 | str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>"; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 151 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 152 | assert_null(prefix); |
| 153 | assert_false(strncmp("element", name, name_len)); |
| 154 | assert_int_equal(7, name_len); |
| 155 | assert_string_equal("/>", str); |
| 156 | |
| 157 | str = "<element xmlns=\"urn\"></element>"; |
| 158 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 159 | assert_null(prefix); |
| 160 | assert_false(strncmp("element", name, name_len)); |
| 161 | assert_int_equal(7, name_len); |
| 162 | assert_string_equal(" xmlns=\"urn\"></element>", str); |
| 163 | |
| 164 | /* qualified element */ |
| 165 | str = " < yin:element/>"; |
| 166 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 167 | assert_false(strncmp("yin", prefix, prefix_len)); |
| 168 | assert_false(strncmp("element", name, name_len)); |
| 169 | assert_int_equal(3, prefix_len); |
| 170 | assert_int_equal(7, name_len); |
| 171 | assert_string_equal("/>", str); |
| 172 | |
| 173 | str = "<yin:element xmlns=\"urn\"></element>"; |
| 174 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 175 | assert_false(strncmp("yin", prefix, prefix_len)); |
| 176 | assert_false(strncmp("element", name, name_len)); |
| 177 | assert_int_equal(3, prefix_len); |
| 178 | assert_int_equal(7, name_len); |
| 179 | assert_string_equal(" xmlns=\"urn\"></element>", str); |
| 180 | |
| 181 | /* UTF8 characters */ |
| 182 | str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>"; |
| 183 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 184 | assert_false(strncmp("𠜎€𠜎Øn", prefix, prefix_len)); |
| 185 | assert_false(strncmp("𠜎€𠜎Øn", name, name_len)); |
| 186 | assert_int_equal(14, prefix_len); |
| 187 | assert_int_equal(14, name_len); |
| 188 | assert_string_equal("/>", str); |
| 189 | |
| 190 | /* invalid UTF-8 character */ |
| 191 | str = "<¢:element>"; |
| 192 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 193 | logbuf_assert("Identifier \"¢:element>\" starts with invalid character. Line number 1."); |
| 194 | str = "<yin:c⁐element>"; |
| 195 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len)); |
| 196 | logbuf_assert("Invalid character 0x9081e2. Line number 1."); |
| 197 | } |
| 198 | |
| 199 | int main(void) |
| 200 | { |
| 201 | const struct CMUnitTest tests[] = { |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 202 | cmocka_unit_test_setup(test_utf8, logger_setup), |
| 203 | cmocka_unit_test(test_element), |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 207 | } |