Blame tests/conformance/test_sec9_4_4.c

Packit 8fb591
/**
Packit 8fb591
 * @file test_sec9_4_4.c
Packit 8fb591
 * @author Pavol Vican
Packit 8fb591
 * @brief Cmocka test for RFC 6020 section 9.4.4 (also 9.4.4.1) conformance.
Packit 8fb591
 *
Packit 8fb591
 * Copyright (c) 2016 CESNET, z.s.p.o.
Packit 8fb591
 *
Packit 8fb591
 * This source code is licensed under BSD 3-Clause License (the "License").
Packit 8fb591
 * You may not use this file except in compliance with the License.
Packit 8fb591
 * You may obtain a copy of the License at
Packit 8fb591
 *
Packit 8fb591
 *     https://opensource.org/licenses/BSD-3-Clause
Packit 8fb591
 */
Packit 8fb591
Packit 8fb591
#include <stdio.h>
Packit 8fb591
#include <stdlib.h>
Packit 8fb591
#include <setjmp.h>
Packit 8fb591
#include <errno.h>
Packit 8fb591
#include <unistd.h>
Packit 8fb591
#include <stdarg.h>
Packit 8fb591
#include <cmocka.h>
Packit 8fb591
#include <string.h>
Packit 8fb591
#include <sys/wait.h>
Packit 8fb591
Packit 8fb591
#include "tests/config.h"
Packit 8fb591
#include "libyang.h"
Packit 8fb591
Packit 8fb591
#define TEST_DIR "sec9_4_4"
Packit 8fb591
#define TEST_NAME test_sec7_9_4_4
Packit 8fb591
#define TEST_SCHEMA_COUNT 10
Packit 8fb591
#define TEST_SCHEMA_LOAD_FAIL 1,1,1,1,1,1,1,1,1,0
Packit 8fb591
#define TEST_DATA_FILE_COUNT 5
Packit 8fb591
#define TEST_DATA_FILE_LOAD_FAIL 0,1,1,1,0
Packit 8fb591
Packit 8fb591
struct state {
Packit 8fb591
    struct ly_ctx *ctx;
Packit 8fb591
    struct lyd_node *node;
Packit 8fb591
};
Packit 8fb591
Packit 8fb591
static int
Packit 8fb591
setup_f(void **state)
Packit 8fb591
{
Packit 8fb591
    struct state *st;
Packit 8fb591
Packit 8fb591
    (*state) = st = calloc(1, sizeof *st);
Packit 8fb591
    if (!st) {
Packit 8fb591
        fprintf(stderr, "Memory allocation error");
Packit 8fb591
        return -1;
Packit 8fb591
    }
Packit 8fb591
Packit 8fb591
    /* libyang context */
Packit 8fb591
    st->ctx = ly_ctx_new(TESTS_DIR "/conformance/" TEST_DIR, 0);
Packit 8fb591
    if (!st->ctx) {
Packit 8fb591
        fprintf(stderr, "Failed to create context.\n");
Packit 8fb591
        return -1;
Packit 8fb591
    }
Packit 8fb591
Packit 8fb591
    return 0;
Packit 8fb591
}
Packit 8fb591
Packit 8fb591
static int
Packit 8fb591
teardown_f(void **state)
Packit 8fb591
{
Packit 8fb591
    struct state *st = (*state);
Packit 8fb591
Packit 8fb591
    lyd_free_withsiblings(st->node);
Packit 8fb591
    ly_ctx_destroy(st->ctx, NULL);
Packit 8fb591
    free(st);
Packit 8fb591
    (*state) = NULL;
Packit 8fb591
Packit 8fb591
    return 0;
Packit 8fb591
}
Packit 8fb591
Packit 8fb591
static void
Packit 8fb591
TEST_STRING_LENGTH(void **state)
Packit 8fb591
{
Packit 8fb591
    struct state *st = (*state);
Packit 8fb591
    const int schemas_fail[] = {TEST_SCHEMA_LOAD_FAIL};
Packit 8fb591
    const int data_files_fail[] = {TEST_DATA_FILE_LOAD_FAIL};
Packit 8fb591
    char buf[1024];
Packit 8fb591
    LYS_INFORMAT schema_format = LYS_IN_YANG;
Packit 8fb591
    const struct lys_module *mod;
Packit 8fb591
    int i, j, ret;
Packit 8fb591
Packit 8fb591
    for (i = 0; i < 2; ++i) {
Packit 8fb591
        for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
Packit 8fb591
            sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.%s", j + 1, (schema_format == LYS_IN_YANG ? "yang" : "yin"));
Packit 8fb591
            mod = lys_parse_path(st->ctx, buf, schema_format);
Packit 8fb591
            if (schemas_fail[j]) {
Packit 8fb591
                assert_ptr_equal(mod, NULL);
Packit 8fb591
            } else {
Packit 8fb591
                assert_ptr_not_equal(mod, NULL);
Packit 8fb591
            }
Packit 8fb591
        }
Packit 8fb591
Packit 8fb591
        for (j = 0; j < TEST_DATA_FILE_COUNT; ++j) {
Packit 8fb591
            sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/data%d.xml", j + 1);
Packit 8fb591
            st->node = lyd_parse_path(st->ctx, buf, LYD_XML, LYD_OPT_CONFIG);
Packit 8fb591
            if (data_files_fail[j]) {
Packit 8fb591
                assert_ptr_equal(st->node, NULL);
Packit 8fb591
            } else {
Packit 8fb591
                assert_ptr_not_equal(st->node, NULL);
Packit 8fb591
            }
Packit 8fb591
            lyd_free_withsiblings(st->node);
Packit 8fb591
            st->node = NULL;
Packit 8fb591
        }
Packit 8fb591
Packit 8fb591
        if (schema_format == LYS_IN_YANG) {
Packit 8fb591
            /* convert the modules */
Packit 8fb591
            for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
Packit 8fb591
                sprintf(buf, BUILD_DIR "/yang2yin "
Packit 8fb591
                             TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yang "
Packit 8fb591
                             TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1, j + 1);
Packit 8fb591
                ret = system(buf);
Packit 8fb591
                if (ret == -1) {
Packit 8fb591
                    fprintf(stderr, "system() failed (%s).\n", strerror(errno));
Packit 8fb591
                    fail();
Packit 8fb591
                } else if (WEXITSTATUS(ret) != 0) {
Packit 8fb591
                    fprintf(stderr, "Executing command \"%s\" finished with %d.\n", buf, WEXITSTATUS(ret));
Packit 8fb591
                    fail();
Packit 8fb591
                }
Packit 8fb591
            }
Packit 8fb591
Packit 8fb591
            schema_format = LYS_IN_YIN;
Packit 8fb591
        } else {
Packit 8fb591
            /* remove the modules */
Packit 8fb591
            for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
Packit 8fb591
                sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1);
Packit 8fb591
                if (unlink(buf)) {
Packit 8fb591
                    fprintf(stderr, "unlink() on \"%s\" failed (%s).\n", buf, strerror(errno));
Packit 8fb591
                }
Packit 8fb591
            }
Packit 8fb591
        }
Packit 8fb591
    }
Packit 8fb591
}
Packit 8fb591
Packit 8fb591
int
Packit 8fb591
main(void)
Packit 8fb591
{
Packit 8fb591
    const struct CMUnitTest tests[] = {
Packit 8fb591
        cmocka_unit_test_setup_teardown(TEST_STRING_LENGTH, setup_f, teardown_f),
Packit 8fb591
    };
Packit 8fb591
Packit 8fb591
    return cmocka_run_group_tests(tests, NULL, NULL);
Packit 8fb591
}