Path: blob/main/contrib/libucl/tests/test_schema.c
39586 views
/* Copyright (c) 2014, Vsevolod Stakhov1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10*11* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY12* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED13* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE14* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY15* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES16* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;17* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND18* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT19* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS20* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.21*/2223#include <stdio.h>24#include <errno.h>25#include <unistd.h>26#include "ucl.h"2728static int29read_stdin (char **buf)30{31int size = BUFSIZ, remain, ret;32char *p;3334*buf = malloc (size);35if (*buf == NULL) {36return -1;37}3839p = *buf;40remain = size;4142while ((ret = read (STDIN_FILENO, p, remain - 1)) > 0) {43remain -= ret;44p += ret;4546if (remain <= 1) {47*buf = realloc (*buf, size * 2);48if (*buf == NULL) {49return -1;50}5152p = *buf + size - 1;53remain = size + 1;54size *= 2;55}56}5758*p = '\0';5960return ret;61}6263static bool64perform_test (const ucl_object_t *schema, const ucl_object_t *obj,65struct ucl_schema_error *err)66{67const ucl_object_t *valid, *data, *description;68bool match;6970data = ucl_object_lookup (obj, "data");71description = ucl_object_lookup (obj, "description");72valid = ucl_object_lookup (obj, "valid");7374if (data == NULL || description == NULL || valid == NULL) {75fprintf (stdout, "Bad test case\n");76return false;77}7879match = ucl_object_validate (schema, data, err);80if (match != ucl_object_toboolean (valid)) {81fprintf (stdout, "Test case '%s' failed (expected %s): '%s'\n",82ucl_object_tostring (description),83ucl_object_toboolean (valid) ? "valid" : "invalid",84err->msg);85fprintf (stdout, "%s\n", ucl_object_emit (data, UCL_EMIT_CONFIG));86fprintf (stdout, "%s\n", ucl_object_emit (schema, UCL_EMIT_CONFIG));87return false;88}8990return true;91}9293static int94perform_tests (const ucl_object_t *obj)95{96struct ucl_schema_error err;97ucl_object_iter_t iter = NULL;98const ucl_object_t *schema, *tests, *description, *test;99100if (obj->type != UCL_OBJECT) {101fprintf (stdout, "Bad test case\n");102return EXIT_FAILURE;103}104105schema = ucl_object_lookup (obj, "schema");106tests = ucl_object_lookup (obj, "tests");107description = ucl_object_lookup (obj, "description");108109if (schema == NULL || tests == NULL || description == NULL) {110fprintf (stdout, "Bad test case\n");111return EXIT_FAILURE;112}113114memset (&err, 0, sizeof (err));115116while ((test = ucl_object_iterate (tests, &iter, true)) != NULL) {117if (!perform_test (schema, test, &err)) {118fprintf (stdout, "Test suite '%s' failed\n",119ucl_object_tostring (description));120return EXIT_FAILURE;121}122}123124return 0;125}126127int128main (int argc, char **argv)129{130char *buf = NULL;131struct ucl_parser *parser;132ucl_object_t *obj = NULL;133const ucl_object_t *elt;134ucl_object_iter_t iter = NULL;135int ret = 0;136137if (read_stdin (&buf) == -1) {138exit (EXIT_FAILURE);139}140141parser = ucl_parser_new (0);142143ucl_parser_add_string (parser, buf, 0);144145if (ucl_parser_get_error (parser) != NULL) {146fprintf (stdout, "Error occurred: %s\n", ucl_parser_get_error (parser));147ret = 1;148return EXIT_FAILURE;149}150obj = ucl_parser_get_object (parser);151ucl_parser_free (parser);152153while ((elt = ucl_object_iterate (obj, &iter, true)) != NULL) {154ret = perform_tests (elt);155if (ret != 0) {156break;157}158}159160ucl_object_unref (obj);161162return ret;163}164165166