test-xml.c revision bcf3295d2b0d87caefad2e73d221aac080d0c11e
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering/***
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering This file is part of systemd.
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering Copyright 2013 Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering systemd is free software; you can redistribute it and/or modify it
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering under the terms of the GNU Lesser General Public License as published by
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering (at your option) any later version.
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering systemd is distributed in the hope that it will be useful, but
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering Lesser General Public License for more details.
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering You should have received a copy of the GNU Lesser General Public License
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering***/
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <stdarg.h>
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "xml.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "util.h"
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poetteringstatic void test_one(const char *data, ...) {
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering void *state = NULL;
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering va_list ap;
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering va_start(ap, data);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering for (;;) {
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering _cleanup_free_ char *name = NULL;
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering int t, tt;
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering const char *nn;
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering t = xml_tokenize(&data, &name, &state, NULL);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering assert_se(t >= 0);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering tt = va_arg(ap, int);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering assert_se(tt >= 0);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering assert_se(t == tt);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering if (t == XML_END)
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering break;
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering nn = va_arg(ap, const char *);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering assert_se(streq_ptr(nn, name));
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering }
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering va_end(ap);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering}
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poetteringint main(int argc, char *argv[]) {
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering test_one("", XML_END);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering test_one("<foo></foo>",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TAG_OPEN, "foo",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TAG_CLOSE, "foo",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_END);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering test_one("<foo waldo=piep meh=\"huhu\"/>",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TAG_OPEN, "foo",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_ATTRIBUTE_NAME, "waldo",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_ATTRIBUTE_VALUE, "piep",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_ATTRIBUTE_NAME, "meh",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_ATTRIBUTE_VALUE, "huhu",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TAG_CLOSE_EMPTY, NULL,
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_END);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering test_one("xxxx\n"
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering "<foo><?xml foo?> <!-- zzzz --> </foo>",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TEXT, "xxxx\n",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TAG_OPEN, "foo",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TEXT, " ",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TEXT, " ",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_TAG_CLOSE, "foo",
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering XML_END);
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering return 0;
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering}
72648326ea6d3e68cdb0b5890df737047d031a41Lennart Poettering