]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-xml.c
Merge pull request #7386 from keszybz/spdx
[thirdparty/systemd.git] / src / test / test-xml.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <stdarg.h>
22
23 #include "alloc-util.h"
24 #include "string-util.h"
25 #include "util.h"
26 #include "xml.h"
27
28 static void test_one(const char *data, ...) {
29 void *state = NULL;
30 va_list ap;
31
32 va_start(ap, data);
33
34 for (;;) {
35 _cleanup_free_ char *name = NULL;
36 int t, tt;
37 const char *nn;
38
39 t = xml_tokenize(&data, &name, &state, NULL);
40 assert_se(t >= 0);
41
42 tt = va_arg(ap, int);
43 assert_se(tt >= 0);
44
45 assert_se(t == tt);
46 if (t == XML_END)
47 break;
48
49 nn = va_arg(ap, const char *);
50 assert_se(streq_ptr(nn, name));
51 }
52
53 va_end(ap);
54 }
55
56 int main(int argc, char *argv[]) {
57
58 test_one("", XML_END);
59
60 test_one("<foo></foo>",
61 XML_TAG_OPEN, "foo",
62 XML_TAG_CLOSE, "foo",
63 XML_END);
64
65 test_one("<foo waldo=piep meh=\"huhu\"/>",
66 XML_TAG_OPEN, "foo",
67 XML_ATTRIBUTE_NAME, "waldo",
68 XML_ATTRIBUTE_VALUE, "piep",
69 XML_ATTRIBUTE_NAME, "meh",
70 XML_ATTRIBUTE_VALUE, "huhu",
71 XML_TAG_CLOSE_EMPTY, NULL,
72 XML_END);
73
74 test_one("xxxx\n"
75 "<foo><?xml foo?> <!-- zzzz --> </foo>",
76 XML_TEXT, "xxxx\n",
77 XML_TAG_OPEN, "foo",
78 XML_TEXT, " ",
79 XML_TEXT, " ",
80 XML_TAG_CLOSE, "foo",
81 XML_END);
82
83 return 0;
84 }