Blame tests/data/test_leafref_remove.c

Packit 8fb591
/**
Packit 8fb591
 * @file test_leafref_remove.c
Packit 8fb591
 * @author Radek Krejci <rkrejci@cesnet.cz>
Packit 8fb591
 * @brief Cmoka tests for correct removing leafref nodes from data tree.
Packit 8fb591
 *
Packit 8fb591
 * Copyright (c) 2015 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 <stdarg.h>
Packit 8fb591
#include <cmocka.h>
Packit 8fb591
Packit 8fb591
#include "tests/config.h"
Packit 8fb591
#include "libyang.h"
Packit 8fb591
Packit 8fb591
struct state {
Packit 8fb591
    struct ly_ctx *ctx;
Packit 8fb591
    struct lyd_node *data;
Packit 8fb591
};
Packit 8fb591
Packit 8fb591
static int
Packit 8fb591
setup_f(void **state)
Packit 8fb591
{
Packit 8fb591
    struct state *st;
Packit 8fb591
    const char *schemafile = TESTS_DIR"/data/files/leafrefs.yin";
Packit 8fb591
    const char *datafile = TESTS_DIR"/data/files/leafrefs.xml";
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(NULL, 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
    /* schema */
Packit 8fb591
    if (!lys_parse_path(st->ctx, schemafile, LYS_IN_YIN)) {
Packit 8fb591
        fprintf(stderr, "Failed to load data model \"%s\".\n", schemafile);
Packit 8fb591
        return -1;
Packit 8fb591
    }
Packit 8fb591
Packit 8fb591
    /* data */
Packit 8fb591
    st->data = lyd_parse_path(st->ctx, datafile, LYD_XML, LYD_OPT_CONFIG);
Packit 8fb591
    if (!st->data) {
Packit 8fb591
        fprintf(stderr, "Failed to load initial data file.\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(st->data);
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_leafref_free(void **state)
Packit 8fb591
{
Packit 8fb591
    struct state *st = (*state);
Packit 8fb591
    int r;
Packit 8fb591
Packit 8fb591
    lyd_free(st->data->child->child->prev);
Packit 8fb591
    r = lyd_validate(&(st->data), LYD_OPT_CONFIG, NULL);
Packit 8fb591
    assert_int_not_equal(r, 0);
Packit 8fb591
Packit 8fb591
    lyd_new_leaf(st->data->child, NULL, "name", "jedna");
Packit 8fb591
    r = lyd_validate(&(st->data), LYD_OPT_CONFIG, NULL);
Packit 8fb591
    assert_int_equal(r, 0);
Packit 8fb591
}
Packit 8fb591
Packit 8fb591
static void
Packit 8fb591
test_leafref_unlink(void **state)
Packit 8fb591
{
Packit 8fb591
    struct state *st = (*state);
Packit 8fb591
    struct lyd_node *node;
Packit 8fb591
    int r;
Packit 8fb591
Packit 8fb591
    node = st->data->child->child->next;
Packit 8fb591
    lyd_unlink(node);
Packit 8fb591
    r = lyd_validate(&(st->data), LYD_OPT_CONFIG, NULL);
Packit 8fb591
    assert_int_not_equal(r, 0);
Packit 8fb591
Packit 8fb591
    lyd_insert(st->data->child, node);
Packit 8fb591
    r = lyd_validate(&(st->data), LYD_OPT_CONFIG, NULL);
Packit 8fb591
    assert_int_equal(r, 0);
Packit 8fb591
}
Packit 8fb591
Packit 8fb591
static void
Packit 8fb591
test_leafref_unlink2(void **state)
Packit 8fb591
{
Packit 8fb591
    struct state *st = (*state);
Packit 8fb591
    struct lyd_node *node;
Packit 8fb591
    int r;
Packit 8fb591
Packit 8fb591
    node = st->data->child;
Packit 8fb591
    lyd_unlink(node);
Packit 8fb591
    r = lyd_validate(&(st->data), LYD_OPT_CONFIG, NULL);
Packit 8fb591
    assert_int_not_equal(r, 0);
Packit 8fb591
Packit 8fb591
    lyd_insert(st->data, node);
Packit 8fb591
    r = lyd_validate(&(st->data), LYD_OPT_CONFIG, NULL);
Packit 8fb591
    assert_int_equal(r, 0);
Packit 8fb591
}
Packit 8fb591
Packit 8fb591
int main(void)
Packit 8fb591
{
Packit 8fb591
    const struct CMUnitTest tests[] = {
Packit 8fb591
                    cmocka_unit_test_setup_teardown(test_leafref_free, setup_f, teardown_f),
Packit 8fb591
                    cmocka_unit_test_setup_teardown(test_leafref_unlink, setup_f, teardown_f),
Packit 8fb591
                    cmocka_unit_test_setup_teardown(test_leafref_unlink2, setup_f, teardown_f), };
Packit 8fb591
Packit 8fb591
    return cmocka_run_group_tests(tests, NULL, NULL);
Packit 8fb591
}