xref: /freebsd/contrib/expat/tests/structdata.c (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
1 /*
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 2017      Rhodri James <rhodri@wildebeest.org.uk>
10    Copyright (c) 2017-2023 Sebastian Pipping <sebastian@pipping.org>
11    Copyright (c) 2022      Sean McBride <sean@rogue-research.com>
12    Licensed under the MIT license:
13 
14    Permission is  hereby granted,  free of charge,  to any  person obtaining
15    a  copy  of  this  software   and  associated  documentation  files  (the
16    "Software"),  to  deal in  the  Software  without restriction,  including
17    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
18    distribute, sublicense, and/or sell copies of the Software, and to permit
19    persons  to whom  the Software  is  furnished to  do so,  subject to  the
20    following conditions:
21 
22    The above copyright  notice and this permission notice  shall be included
23    in all copies or substantial portions of the Software.
24 
25    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
26    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
27    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
28    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
29    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
30    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
31    USE OR OTHER DEALINGS IN THE SOFTWARE.
32 */
33 
34 #if defined(NDEBUG)
35 #  undef NDEBUG /* because test suite relies on assert(...) at the moment */
36 #endif
37 
38 #include "expat_config.h"
39 
40 #include <assert.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 
45 #include "structdata.h"
46 #include "minicheck.h"
47 
48 #define STRUCT_EXTENSION_COUNT 8
49 
50 #ifdef XML_UNICODE_WCHAR_T
51 #  include <wchar.h>
52 #  define XML_FMT_STR "ls"
53 #  define xcstrlen(s) wcslen(s)
54 #  define xcstrcmp(s, t) wcscmp((s), (t))
55 #else
56 #  define XML_FMT_STR "s"
57 #  define xcstrlen(s) strlen(s)
58 #  define xcstrcmp(s, t) strcmp((s), (t))
59 #endif
60 
61 static XML_Char *
62 xmlstrdup(const XML_Char *s) {
63   size_t byte_count = (xcstrlen(s) + 1) * sizeof(XML_Char);
64   XML_Char *const dup = (XML_Char *)malloc(byte_count);
65 
66   assert(dup != NULL);
67   memcpy(dup, s, byte_count);
68   return dup;
69 }
70 
71 void
72 StructData_Init(StructData *storage) {
73   assert(storage != NULL);
74   storage->count = 0;
75   storage->max_count = 0;
76   storage->entries = NULL;
77 }
78 
79 void
80 StructData_AddItem(StructData *storage, const XML_Char *s, int data0, int data1,
81                    int data2) {
82   StructDataEntry *entry;
83 
84   assert(storage != NULL);
85   assert(s != NULL);
86   if (storage->count == storage->max_count) {
87     StructDataEntry *new_entries;
88 
89     storage->max_count += STRUCT_EXTENSION_COUNT;
90     new_entries = (StructDataEntry *)realloc(
91         storage->entries, storage->max_count * sizeof(StructDataEntry));
92     assert(new_entries != NULL);
93     storage->entries = new_entries;
94   }
95 
96   entry = &storage->entries[storage->count];
97   entry->str = xmlstrdup(s);
98   entry->data0 = data0;
99   entry->data1 = data1;
100   entry->data2 = data2;
101   storage->count++;
102 }
103 
104 /* 'fail()' aborts the function via a longjmp, so there is no point
105  * in returning a value from this function.
106  */
107 void
108 StructData_CheckItems(StructData *storage, const StructDataEntry *expected,
109                       int count) {
110   char buffer[1024];
111 
112   assert(storage != NULL);
113   assert(expected != NULL);
114   if (count != storage->count) {
115     snprintf(buffer, sizeof(buffer),
116              "wrong number of entries: got %d, expected %d", storage->count,
117              count);
118     StructData_Dispose(storage);
119     fail(buffer);
120   } else {
121     for (int i = 0; i < count; i++) {
122       const StructDataEntry *got = &storage->entries[i];
123       const StructDataEntry *want = &expected[i];
124 
125       assert(got != NULL);
126       assert(want != NULL);
127 
128       if (xcstrcmp(got->str, want->str) != 0) {
129         StructData_Dispose(storage);
130         fail("structure got bad string");
131       } else {
132         if (got->data0 != want->data0 || got->data1 != want->data1
133             || got->data2 != want->data2) {
134           snprintf(buffer, sizeof(buffer),
135                    "struct '%" XML_FMT_STR
136                    "' expected (%d,%d,%d), got (%d,%d,%d)",
137                    got->str, want->data0, want->data1, want->data2, got->data0,
138                    got->data1, got->data2);
139           StructData_Dispose(storage);
140           fail(buffer);
141         }
142       }
143     }
144   }
145 }
146 
147 void
148 StructData_Dispose(StructData *storage) {
149   int i;
150 
151   assert(storage != NULL);
152   for (i = 0; i < storage->count; i++)
153     free((void *)storage->entries[i].str);
154   free(storage->entries);
155 
156   storage->count = 0;
157   storage->entries = NULL;
158 }
159