Home | History | Annotate | Line # | Download | only in tests
      1 /* Commonly used functions for the Expat test suite
      2                             __  __            _
      3                          ___\ \/ /_ __   __ _| |_
      4                         / _ \\  /| '_ \ / _` | __|
      5                        |  __//  \| |_) | (_| | |_
      6                         \___/_/\_\ .__/ \__,_|\__|
      7                                  |_| XML parser
      8 
      9    Copyright (c) 2001-2006 Fred L. Drake, Jr. <fdrake (at) users.sourceforge.net>
     10    Copyright (c) 2003      Greg Stein <gstein (at) users.sourceforge.net>
     11    Copyright (c) 2005-2007 Steven Solie <steven (at) solie.ca>
     12    Copyright (c) 2005-2012 Karl Waclawek <karl (at) waclawek.net>
     13    Copyright (c) 2016-2025 Sebastian Pipping <sebastian (at) pipping.org>
     14    Copyright (c) 2017-2022 Rhodri James <rhodri (at) wildebeest.org.uk>
     15    Copyright (c) 2017      Joe Orton <jorton (at) redhat.com>
     16    Copyright (c) 2017      Jos Gutirrez de la Concha <jose (at) zeroc.com>
     17    Copyright (c) 2018      Marco Maggi <marco.maggi-ipsu (at) poste.it>
     18    Copyright (c) 2019      David Loffredo <loffredo (at) steptools.com>
     19    Copyright (c) 2020      Tim Gates <tim.gates (at) iress.com>
     20    Copyright (c) 2021      Donghee Na <donghee.na (at) python.org>
     21    Copyright (c) 2023      Sony Corporation / Snild Dolkow <snild (at) sony.com>
     22    Licensed under the MIT license:
     23 
     24    Permission is  hereby granted,  free of charge,  to any  person obtaining
     25    a  copy  of  this  software   and  associated  documentation  files  (the
     26    "Software"),  to  deal in  the  Software  without restriction,  including
     27    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
     28    distribute, sublicense, and/or sell copies of the Software, and to permit
     29    persons  to whom  the Software  is  furnished to  do so,  subject to  the
     30    following conditions:
     31 
     32    The above copyright  notice and this permission notice  shall be included
     33    in all copies or substantial portions of the Software.
     34 
     35    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
     36    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
     37    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
     38    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
     39    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
     40    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     41    USE OR OTHER DEALINGS IN THE SOFTWARE.
     42 */
     43 
     44 #ifdef __cplusplus
     45 extern "C" {
     46 #endif
     47 
     48 #ifndef XML_COMMON_H
     49 #  define XML_COMMON_H
     50 
     51 #  include "expat_config.h"
     52 #  include "minicheck.h"
     53 #  include "chardata.h"
     54 
     55 #  ifdef XML_LARGE_SIZE
     56 #    define XML_FMT_INT_MOD "ll"
     57 #  else
     58 #    define XML_FMT_INT_MOD "l"
     59 #  endif
     60 
     61 #  ifdef XML_UNICODE_WCHAR_T
     62 #    define XML_FMT_STR "ls"
     63 #    include <wchar.h>
     64 #    define xcstrlen(s) wcslen(s)
     65 #    define xcstrcmp(s, t) wcscmp((s), (t))
     66 #    define xcstrncmp(s, t, n) wcsncmp((s), (t), (n))
     67 #    define XCS(s) _XCS(s)
     68 #    define _XCS(s) L##s
     69 #  else
     70 #    ifdef XML_UNICODE
     71 #      error "No support for UTF-16 character without wchar_t in tests"
     72 #    else
     73 #      define XML_FMT_STR "s"
     74 #      define xcstrlen(s) strlen(s)
     75 #      define xcstrcmp(s, t) strcmp((s), (t))
     76 #      define xcstrncmp(s, t, n) strncmp((s), (t), (n))
     77 #      define XCS(s) s
     78 #    endif /* XML_UNICODE */
     79 #  endif   /* XML_UNICODE_WCHAR_T */
     80 
     81 extern XML_Parser g_parser;
     82 
     83 extern XML_Bool g_resumable;
     84 extern XML_Bool g_abortable;
     85 
     86 extern int g_chunkSize;
     87 
     88 extern const char *long_character_data_text;
     89 extern const char *long_cdata_text;
     90 extern const char *get_buffer_test_text;
     91 
     92 extern void tcase_add_test__ifdef_xml_dtd(TCase *tc, tcase_test_function test);
     93 extern void tcase_add_test__if_xml_ge(TCase *tc, tcase_test_function test);
     94 
     95 extern void basic_teardown(void);
     96 
     97 extern void _xml_failure(XML_Parser parser, const char *file, int line);
     98 
     99 #  define xml_failure(parser) _xml_failure((parser), __FILE__, __LINE__)
    100 
    101 extern enum XML_Status _XML_Parse_SINGLE_BYTES(XML_Parser parser, const char *s,
    102                                                int len, int isFinal);
    103 
    104 extern void _expect_failure(const char *text, enum XML_Error errorCode,
    105                             const char *errorMessage, const char *file,
    106                             int lineno);
    107 
    108 #  define expect_failure(text, errorCode, errorMessage)                        \
    109     _expect_failure((text), (errorCode), (errorMessage), __FILE__, __LINE__)
    110 
    111 /* Support functions for handlers to collect up character and attribute data.
    112  */
    113 
    114 extern void _run_character_check(const char *text, const XML_Char *expected,
    115                                  const char *file, int line);
    116 
    117 #  define run_character_check(text, expected)                                  \
    118     _run_character_check(text, expected, __FILE__, __LINE__)
    119 
    120 extern void _run_attribute_check(const char *text, const XML_Char *expected,
    121                                  const char *file, int line);
    122 
    123 #  define run_attribute_check(text, expected)                                  \
    124     _run_attribute_check(text, expected, __FILE__, __LINE__)
    125 
    126 typedef struct ExtTest {
    127   const char *parse_text;
    128   const XML_Char *encoding;
    129   CharData *storage;
    130 } ExtTest;
    131 
    132 extern void _run_ext_character_check(const char *text, ExtTest *test_data,
    133                                      const XML_Char *expected, const char *file,
    134                                      int line);
    135 
    136 #  define run_ext_character_check(text, test_data, expected)                   \
    137     _run_ext_character_check(text, test_data, expected, __FILE__, __LINE__)
    138 
    139 #  define ALLOC_ALWAYS_SUCCEED (-1)
    140 #  define REALLOC_ALWAYS_SUCCEED (-1)
    141 
    142 extern int g_allocation_count;
    143 extern int g_reallocation_count;
    144 
    145 extern void *duff_allocator(size_t size);
    146 
    147 extern void *duff_reallocator(void *ptr, size_t size);
    148 
    149 extern char *portable_strndup(const char *s, size_t n);
    150 
    151 #endif /* XML_COMMON_H */
    152 
    153 #ifdef __cplusplus
    154 }
    155 #endif
    156