Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_parser_xml.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from parser_xml.c |
| 5 | * |
| 6 | * Copyright (c) 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame^] | 23 | #include "../../src/context.h" |
| 24 | #include "../../src/tree_data_internal.h" |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 25 | |
| 26 | #define BUFSIZE 1024 |
| 27 | char logbuf[BUFSIZE] = {0}; |
| 28 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 29 | |
| 30 | struct ly_ctx *ctx; /* context for tests */ |
| 31 | |
| 32 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 33 | #define ENABLE_LOGGER_CHECKING 1 |
| 34 | |
| 35 | #if ENABLE_LOGGER_CHECKING |
| 36 | static void |
| 37 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 38 | { |
| 39 | (void) level; /* unused */ |
| 40 | if (store) { |
| 41 | if (path && path[0]) { |
| 42 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 43 | } else { |
| 44 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 45 | } |
| 46 | if (store > 0) { |
| 47 | --store; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | static int |
| 54 | setup(void **state) |
| 55 | { |
| 56 | (void) state; /* unused */ |
| 57 | |
| 58 | const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;" |
| 59 | "leaf foo { type string;}}"; |
| 60 | |
| 61 | #if ENABLE_LOGGER_CHECKING |
| 62 | ly_set_log_clb(logger, 1); |
| 63 | #endif |
| 64 | |
| 65 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 66 | assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG)); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | teardown(void **state) |
| 73 | { |
| 74 | #if ENABLE_LOGGER_CHECKING |
| 75 | if (*state) { |
| 76 | fprintf(stderr, "%s\n", logbuf); |
| 77 | } |
| 78 | #else |
| 79 | (void) state; /* unused */ |
| 80 | #endif |
| 81 | |
| 82 | ly_ctx_destroy(ctx, NULL); |
| 83 | ctx = NULL; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | logbuf_clean(void) |
| 90 | { |
| 91 | logbuf[0] = '\0'; |
| 92 | } |
| 93 | |
| 94 | #if ENABLE_LOGGER_CHECKING |
| 95 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 96 | #else |
| 97 | # define logbuf_assert(str) |
| 98 | #endif |
| 99 | |
| 100 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 101 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 102 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 103 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 104 | CLEANUP |
| 105 | |
| 106 | static void |
| 107 | test_leaf(void **state) |
| 108 | { |
| 109 | *state = test_leaf; |
| 110 | |
| 111 | const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>"; |
| 112 | struct lyd_node *tree; |
| 113 | struct lyd_node_term *leaf; |
| 114 | |
| 115 | assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, 0, &tree)); |
| 116 | assert_non_null(tree); |
| 117 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 118 | assert_string_equal("foo", tree->schema->name); |
| 119 | leaf = (struct lyd_node_term*)tree; |
| 120 | assert_string_equal("foo value", leaf->value.canonized); |
| 121 | |
| 122 | lyd_free_all(tree); |
| 123 | *state = NULL; |
| 124 | } |
| 125 | |
| 126 | int main(void) |
| 127 | { |
| 128 | const struct CMUnitTest tests[] = { |
| 129 | cmocka_unit_test_setup_teardown(test_leaf, setup, teardown), |
| 130 | }; |
| 131 | |
| 132 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 133 | } |