|
| 1 | +/** |
| 2 | + * @file test_sec7_1.c |
| 3 | + * @author Pavol Vican |
| 4 | + * @brief Cmocka test for RFC 6020 section 7.1 (without 7.1.3) conformance. |
| 5 | + * |
| 6 | + * Copyright (c) 2016 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 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <setjmp.h> |
| 18 | +#include <errno.h> |
| 19 | +#include <unistd.h> |
| 20 | +#include <stdarg.h> |
| 21 | +#include <cmocka.h> |
| 22 | +#include <string.h> |
| 23 | +#include <sys/wait.h> |
| 24 | + |
| 25 | +#include "tests/config.h" |
| 26 | +#include "libyang.h" |
| 27 | + |
| 28 | +#define TEST_DIR "sec7_15_1" |
| 29 | +#define TEST_NAME test_sec7_15_1 |
| 30 | +#define TEST_SCHEMA_COUNT 2 |
| 31 | +#define TEST_SCHEMA_LOAD_FAIL 1,0 |
| 32 | +#define TEST_DATA_FILE_COUNT 0 |
| 33 | +#define TEST_DATA_FILE_LOAD_FAIL 0 |
| 34 | + |
| 35 | +struct state { |
| 36 | + struct ly_ctx *ctx; |
| 37 | + struct lyd_node *node; |
| 38 | +}; |
| 39 | + |
| 40 | +static int |
| 41 | +setup_f(void **state) |
| 42 | +{ |
| 43 | + struct state *st; |
| 44 | + |
| 45 | + (*state) = st = calloc(1, sizeof *st); |
| 46 | + if (!st) { |
| 47 | + fprintf(stderr, "Memory allocation error"); |
| 48 | + return -1; |
| 49 | + } |
| 50 | + |
| 51 | + /* libyang context */ |
| 52 | + st->ctx = ly_ctx_new(TESTS_DIR "/conformance/" TEST_DIR, 0); |
| 53 | + if (!st->ctx) { |
| 54 | + fprintf(stderr, "Failed to create context.\n"); |
| 55 | + return -1; |
| 56 | + } |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static int |
| 62 | +teardown_f(void **state) |
| 63 | +{ |
| 64 | + struct state *st = (*state); |
| 65 | + |
| 66 | + lyd_free(st->node); |
| 67 | + ly_ctx_destroy(st->ctx, NULL); |
| 68 | + free(st); |
| 69 | + (*state) = NULL; |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +static void |
| 76 | +TEST_ACTION(void **state) |
| 77 | +{ |
| 78 | + struct state *st = (*state); |
| 79 | + const int schemas_fail[] = {TEST_SCHEMA_LOAD_FAIL}; |
| 80 | + const int data_files_fail[] = {TEST_DATA_FILE_LOAD_FAIL}; |
| 81 | + char buf[1024]; |
| 82 | + LYS_INFORMAT schema_format = LYS_IN_YANG; |
| 83 | + const struct lys_module *mod; |
| 84 | + int i, j, ret; |
| 85 | + |
| 86 | + for (i = 0; i < 2; ++i) { |
| 87 | + for (j = 0; j < TEST_SCHEMA_COUNT; ++j) { |
| 88 | + sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.%s", j + 1, (schema_format == LYS_IN_YANG ? "yang" : "yin")); |
| 89 | + mod = lys_parse_path(st->ctx, buf, schema_format); |
| 90 | + if (schemas_fail[j]) { |
| 91 | + assert_ptr_equal(mod, NULL); |
| 92 | + } else { |
| 93 | + assert_ptr_not_equal(mod, NULL); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + for (j = 0; j < TEST_DATA_FILE_COUNT; ++j) { |
| 98 | + sprintf(buf, TESTS_DIR "/conformance/data%d.xml", j + 1); |
| 99 | + st->node = lyd_parse_path(st->ctx, buf, LYD_XML, LYD_OPT_CONFIG); |
| 100 | + if (data_files_fail[j]) { |
| 101 | + assert_ptr_not_equal(st->node, NULL); |
| 102 | + } else { |
| 103 | + assert_ptr_equal(st->node, NULL); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (schema_format == LYS_IN_YANG) { |
| 108 | + /* convert the modules */ |
| 109 | + for (j = 0; j < TEST_SCHEMA_COUNT; ++j) { |
| 110 | + sprintf(buf, BUILD_DIR "/yang2yin " |
| 111 | + TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yang " |
| 112 | + TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1, j + 1); |
| 113 | + ret = system(buf); |
| 114 | + if (ret == -1) { |
| 115 | + fprintf(stderr, "system() failed (%s).\n", strerror(errno)); |
| 116 | + fail(); |
| 117 | + } else if (WEXITSTATUS(ret) != 0) { |
| 118 | + fprintf(stderr, "Executing command \"%s\" finished with %d.\n", buf, WEXITSTATUS(ret)); |
| 119 | + fail(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + schema_format = LYS_IN_YIN; |
| 124 | + } else { |
| 125 | + /* remove the modules */ |
| 126 | + for (j = 0; j < TEST_SCHEMA_COUNT; ++j) { |
| 127 | + sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1); |
| 128 | + if (unlink(buf)) { |
| 129 | + fprintf(stderr, "unlink() on \"%s\" failed (%s).\n", buf, strerror(errno)); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +int |
| 137 | +main(void) |
| 138 | +{ |
| 139 | + const struct CMUnitTest tests[] = { |
| 140 | + cmocka_unit_test_setup_teardown(TEST_ACTION, setup_f, teardown_f), |
| 141 | + }; |
| 142 | + |
| 143 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 144 | +} |
0 commit comments