static void
test_lys_print_mem_jsons(void **state)
{
    (void) state; /* unused */
    const struct lys_module *module;
    LYS_INFORMAT yang_format = LYS_IN_YIN;
    const char *target = "grouping/gg";
    char *result = NULL;
    int rc;

    module = lys_parse_mem(ctx, lys_module_a, yang_format);
    if (!module) {
        fail();
    }

    rc = lys_print_mem(&result, module, LYS_OUT_JSON, NULL, 0, 0);
    if (rc) {
        fail();
    }

    assert_string_equal(result_jsons, result);
    free(result);

    rc = lys_print_mem(&result, module, LYS_OUT_JSON, target, 0, 0);
    if (rc) {
        fail();
    }

    assert_string_equal(result_jsons_grouping, result);
    free(result);
}
void
test_parse_print_yang(void **state)
{
    struct state *st = (*state);
    struct stat s;
    int fd;

    *state = st = calloc(1, sizeof *st);
    assert_ptr_not_equal(st, NULL);

    st->ctx = ly_ctx_new(TESTS_DIR"/data/files");
    assert_ptr_not_equal(st->ctx, NULL);

    st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/all.yang", LYS_IN_YANG);
    assert_ptr_not_equal(st->mod, NULL);

    st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/all-dev.yang", LYS_IN_YANG);
    assert_ptr_not_equal(st->mod, NULL);

    fd = open(TESTS_DIR"/data/files/all-dev.yang", O_RDONLY);
    fstat(fd, &s);
    st->str1 = malloc(s.st_size + 1);
    assert_ptr_not_equal(st->str1, NULL);
    assert_int_equal(read(fd, st->str1, s.st_size), s.st_size);
    st->str1[s.st_size] = '\0';

    lys_print_mem(&(st->str2), st->mod, LYS_OUT_YANG, NULL);

    assert_string_equal(st->str1, st->str2);

    close(fd);
    fd = -1;
    free(st->str1);
    st->str1 = NULL;
    free(st->str2);
    st->str2 = NULL;

    st->mod = ly_ctx_get_module(st->ctx, "all", NULL);
    assert_ptr_not_equal(st->mod, NULL);

    fd = open(TESTS_DIR"/data/files/all.yang", O_RDONLY);
    fstat(fd, &s);
    st->str1 = malloc(s.st_size + 1);
    assert_ptr_not_equal(st->str1, NULL);
    assert_int_equal(read(fd, st->str1, s.st_size), s.st_size);
    st->str1[s.st_size] = '\0';

    lys_print_mem(&(st->str2), st->mod, LYS_OUT_YANG, NULL);

    assert_string_equal(st->str1, st->str2);
}
static void
test_lys_print_mem_yin(void **state)
{
    (void) state; /* unused */
    const struct lys_module *module;
    LYS_INFORMAT yang_format = LYS_IN_YIN;
    char *result = NULL;
    int rc;

    module = lys_parse_mem(ctx, lys_module_a, yang_format);
    if (!module) {
        fail();
    }

    rc = lys_print_mem(&result, module, LYS_OUT_YIN, NULL, 0, 0);
    if (rc) {
        fail();
    }

    assert_string_equal(result_yin, result);
    free(result);
}
Exemple #4
0
static void
test_fullset(void **state)
{
    struct ly_ctx *ctx = *state;
    const struct lys_module *mod;
    char *buf = NULL;

    const char *tree_alldisabled = "module: features\n"
"   +--rw lst* [id] {not a}?\n"
"   |  +--rw id    string\n"
"   +--rw (ch)? {not (a and b)}?\n"
"   |  +--:(ch3)\n"
"   |     +--rw ch3?   string\n"
"   +--rw axml?   anyxml {not (a or b)}?\n";

    const char *tree_a = "module: features\n"
"   +--rw grp?    string\n"
"   +--rw cont! {a}?\n"
"   +--rw ll*     string {a or b}?\n"
"   +--rw (ch)? {not (a and b)}?\n"
"      +--:(ch1) {a}?\n"
"      |  +--rw ch1?   string\n"
"      +--:(ch3)\n"
"         +--rw ch3?   string\n";

    const char *tree_ab = "module: features\n"
"   +--rw grp?    string\n"
"   +--rw cont! {a}?\n"
"   +--rw lf?     string {a and b}?\n"
"   +--rw ll*     string {a or b}?\n";

    const char *tree_abaa = "module: features\n"
"   +--rw grp?    string\n"
"   +--rw cont! {a}?\n"
"   |  +--rw aug?   string\n"
"   +--rw lf?     string {a and b}?\n"
"   +--rw ll*     string {a or b}?\n"
"rpcs:\n"
"   +---x rpc1 {aa}?\n"
"notifications:\n"
"   +---n notif1 {aa}?\n";

    const char *tree_b = "module: features\n"
"   +--rw ll*     string {a or b}?\n"
"   +--rw lst* [id] {not a}?\n"
"   |  +--rw id    string\n"
"   +--rw (ch)? {not (a and b)}?\n"
"      +--:(ch2) {b}?\n"
"      |  +--rw ch2?   string\n"
"      +--:(ch3)\n"
"         +--rw ch3?   string\n";

    mod = ly_ctx_load_module(ctx, "features", NULL);
    assert_non_null(mod);

    lys_print_mem(&buf, mod, LYS_OUT_TREE, NULL);
    assert_non_null(buf);
    assert_string_equal(buf, tree_alldisabled);
    free(buf); buf = NULL;

    lys_features_enable(mod, "a");
    lys_print_mem(&buf, mod, LYS_OUT_TREE, NULL);
    assert_non_null(buf);
    assert_string_equal(buf, tree_a);
    free(buf); buf = NULL;

    lys_features_enable(mod, "b");
    lys_print_mem(&buf, mod, LYS_OUT_TREE, NULL);
    assert_non_null(buf);
    assert_string_equal(buf, tree_ab);
    free(buf); buf = NULL;

    lys_features_enable(mod, "aa");
    lys_print_mem(&buf, mod, LYS_OUT_TREE, NULL);
    assert_non_null(buf);
    assert_string_equal(buf, tree_abaa);
    free(buf); buf = NULL;

    lys_features_disable(mod, "a"); /* aa is also disabled by disabling a */
    lys_print_mem(&buf, mod, LYS_OUT_TREE, NULL);
    assert_non_null(buf);
    assert_string_equal(buf, tree_b);
    free(buf); buf = NULL;
}