Ejemplo n.º 1
0
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);
}
Ejemplo n.º 2
0
static int
setup_f(void **state)
{
    struct state *st;
    const char *schemafile = TESTS_DIR"/data/files/instance.yin";
    const char *datafile = TESTS_DIR"/data/files/instance.xml";

    (*state) = st = calloc(1, sizeof *st);
    if (!st) {
        fprintf(stderr, "Memory allocation error");
        return -1;
    }

    /* libyang context */
    st->ctx = ly_ctx_new(NULL);
    if (!st->ctx) {
        fprintf(stderr, "Failed to create context.\n");
        return -1;
    }

    /* schema */
    if (!lys_parse_path(st->ctx, schemafile, LYS_IN_YIN)) {
        fprintf(stderr, "Failed to load data model \"%s\".\n", schemafile);
        return -1;
    }

    /* data */
    st->data = lyd_parse_path(st->ctx, datafile, LYD_XML, 0);
    if (!st->data) {
        fprintf(stderr, "Failed to load initial data file.\n");
        return -1;
    }

    return 0;
}
Ejemplo n.º 3
0
static void
TEST_MODULE(void **state)
{
    struct state *st = (*state);
    const int schemas_fail[] = {TEST_SCHEMA_LOAD_FAIL};
    const int data_files_fail[] = {TEST_DATA_FILE_LOAD_FAIL};
    char buf[1024];
    LYS_INFORMAT schema_format = LYS_IN_YANG;
    const struct lys_module *mod;
    int i, j, ret;

    for (i = 0; i < 2; ++i) {
        for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
            sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.%s", j + 1, (schema_format == LYS_IN_YANG ? "yang" : "yin"));
            mod = lys_parse_path(st->ctx, buf, schema_format);
            if (schemas_fail[j]) {
                assert_ptr_equal(mod, NULL);
            } else {
                assert_ptr_not_equal(mod, NULL);
            }
        }

        for (j = 0; j < TEST_DATA_FILE_COUNT; ++j) {
            sprintf(buf, TESTS_DIR "/conformance/data%d.xml", j + 1);
            st->node = lyd_parse_path(st->ctx, buf, LYD_XML, LYD_OPT_CONFIG);
            if (data_files_fail[j]) {
                assert_ptr_not_equal(st->node, NULL);
            } else {
                assert_ptr_equal(st->node, NULL);
            }
        }

        if (schema_format == LYS_IN_YANG) {
            /* convert the modules */
            for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
                sprintf(buf, BUILD_DIR "/yang2yin "
                             TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yang "
                             TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1, j + 1);
                ret = system(buf);
                if (ret == -1) {
                    fprintf(stderr, "system() failed (%s).\n", strerror(errno));
                    fail();
                } else if (WEXITSTATUS(ret) != 0) {
                    fprintf(stderr, "Executing command \"%s\" finished with %d.\n", buf, WEXITSTATUS(ret));
                    fail();
                }
            }

            schema_format = LYS_IN_YIN;
        } else {
            /* remove the modules */
            for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
                sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1);
                if (unlink(buf)) {
                    fprintf(stderr, "unlink() on \"%s\" failed (%s).\n", buf, strerror(errno));
                }
            }
        }
    }
}
Ejemplo n.º 4
0
static int
setup_f(void **state)
{
    struct state *st;
    const char *schemafile = TESTS_DIR"/data/files/defaults.yin";

    (*state) = st = calloc(1, sizeof *st);
    if (!st) {
        fprintf(stderr, "Memory allocation error");
        return -1;
    }

    /* libyang context */
    st->ctx = ly_ctx_new(NULL, 0);
    if (!st->ctx) {
        fprintf(stderr, "Failed to create context.\n");
        goto error;
    }

    /* schemas */
    st->mod = lys_parse_path(st->ctx, schemafile, LYS_IN_YIN);
    if (!st->mod) {
        fprintf(stderr, "Failed to load data model \"%s\".\n", schemafile);
        goto error;
    }

    return 0;

error:
    ly_ctx_destroy(st->ctx, NULL);
    free(st);
    (*state) = NULL;

    return -1;
}
Ejemplo n.º 5
0
static void
test_dependency_circular(void **state)
{
    struct state *st = (struct state *)*state;
    struct lyd_node *node;

    /* schema */
    st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/when-circdepend.yin", LYS_IN_YIN);
    assert_ptr_not_equal(st->mod, NULL);

    st->dt = lyd_new_path(NULL, st->ctx, "/when-circdepend:top/a", "val_a", 0, 0);
    assert_ptr_not_equal(st->dt, NULL);
    node = lyd_new_path(st->dt, st->ctx, "/when-circdepend:top/b", "val_b", 0, 0);
    assert_ptr_not_equal(node, NULL);
    node = lyd_new_path(st->dt, st->ctx, "/when-circdepend:top/d", "1", 0, 0);
    assert_ptr_not_equal(node, NULL);
    node = lyd_new_path(st->dt, st->ctx, "/when-circdepend:top/d", "2", 0, 0);
    assert_ptr_not_equal(node, NULL);
    node = lyd_new_path(st->dt, st->ctx, "/when-circdepend:top/e", "val_e", 0, 0);
    assert_ptr_not_equal(node, NULL);

    assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 1);
    assert_int_equal(ly_errno, LY_EVALID);
    assert_int_equal(ly_vecode, LYVE_INWHEN);
}
Ejemplo n.º 6
0
static int
setup_f(void **state)
{
    struct state *st;
    const char *schema = TESTS_DIR"/data/files/all.yin";
    const char *schemadev = TESTS_DIR"/data/files/all-dev.yin";

    (*state) = st = calloc(1, sizeof *st);
    if (!st) {
        fprintf(stderr, "Memory allocation error");
        return -1;
    }

    /* libyang context */
    st->ctx = ly_ctx_new(TESTS_DIR"/data/files");
    if (!st->ctx) {
        fprintf(stderr, "Failed to create context.\n");
        goto error;
    }

    /* schema */
    st->mod = lys_parse_path(st->ctx, schema, LYS_IN_YIN);
    if (!st->mod) {
        fprintf(stderr, "Failed to load data model \"%s\".\n", schema);
        goto error;
    }
    lys_features_enable(st->mod, "feat2");
    lys_features_enable(st->mod, "*");

    st->mod = lys_parse_path(st->ctx, schemadev, LYS_IN_YIN);
    if (!st->mod) {
        fprintf(stderr, "Failed to load data model \"%s\".\n", schemadev);
        goto error;
    }

    return 0;

error:
    ly_ctx_destroy(st->ctx, NULL);
    free(st);
    (*state) = NULL;

    return -1;
}
Ejemplo n.º 7
0
static void
test_lys_parse_path(void **state)
{
    (void) state; /* unused */
    const struct lys_module *module;
    char *yang_folder = TESTS_DIR"/api/files";
    char *yin_file = TESTS_DIR"/api/files/a.yin";
    char *yang_file = TESTS_DIR"/api/files/b.yang";
    int fd = -1;

    fd = open(yin_file, O_RDONLY);
    if (fd == -1) {
        fail();
    }
    close(fd);

    fd = open(yang_file, O_RDONLY);
    if (fd == -1) {
        fail();
    }
    close(fd);

    ctx = ly_ctx_new(yang_folder, 0);
    module = lys_parse_path(ctx, yin_file, LYS_IN_YIN);
    if (!module) {
        fail();
    }

    assert_string_equal("a", module->name);

    module = lys_parse_path(ctx, yang_file, LYS_IN_YANG);
    if (!module) {
        fail();
    }

    assert_string_equal("b", module->name);

    ly_ctx_destroy(ctx, NULL);
    ctx = NULL;
}
Ejemplo n.º 8
0
int main(int argc, char *argv[])
{
	int fd, i;
	struct ly_ctx *ctx = NULL;
	char buf[30];
	struct lyd_node *data = NULL, *next;
	const struct lys_module *mod;

	/* libyang context */
        ctx = ly_ctx_new(NULL);
        if (!ctx) {
                fprintf(stderr, "Failed to create context.\n");
                return 1;
        }

        /* schema */
        if (!(mod = lys_parse_path(ctx, argv[1], LYS_IN_YIN))) {
                fprintf(stderr, "Failed to load data model.\n");
                goto cleanup;
        }

	/* data */
	data = NULL;
	fd = open("./addloop_result.xml", O_WRONLY | O_CREAT, 0666);
	data = NULL;
	for(i = 1; i <= 5000; i++) {
		next = lyd_new(NULL, mod, "ptest1");
		// if (i == 2091) {sprintf(buf, "%d", 1);} else {
		sprintf(buf, "%d", i);//}
		lyd_new_leaf(next, mod, "index", buf);
		lyd_new_leaf(next, mod, "p1", buf);
		if (!data) {
			data = next;
		} else {
			lyd_insert_after(data->prev, next);
		}
		if (lyd_validate(&data, LYD_OPT_CONFIG, NULL)) {
			goto cleanup;
		}
		//lyd_print_fd(fd, data, LYD_XML);
	}
	lyd_print_fd(fd, data, LYD_XML, LYP_WITHSIBLINGS | LYP_FORMAT);
	close(fd);

cleanup:
	lyd_free_withsiblings(data);
	ly_ctx_destroy(ctx, NULL);

	return 0;
}
Ejemplo n.º 9
0
static void
test_dummy(void **state)
{
    struct state *st = (struct state *)*state;

    /* schema */
    st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/when-dummy.yin", LYS_IN_YIN);
    assert_ptr_not_equal(st->mod, NULL);

    st->dt = lyd_new_path(NULL, st->ctx, "/when-dummy:c", "value", 0, 0);
    assert_ptr_not_equal(st->dt, NULL);

    assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 1);
    assert_int_equal(ly_errno, LY_EVALID);
    assert_int_equal(ly_vecode, LYVE_XPATH_DUMMY);
}
Ejemplo n.º 10
0
static void
test_modules(void **state)
{
    struct ly_ctx *ctx = *state;
    char *extension, path[PATH_MAX];
    const struct lys_module *module;

    char files[17][32] = { "iana-crypt-hash", "iana-if-type",
                           "ietf-inet-types", "ietf-interfaces", "ietf-ipfix-psamp", "ietf-ip",
                           "ietf-netconf-acm", "ietf-netconf-monitoring", "ietf-netconf-notifications",
                           "ietf-netconf-partial-lock", "ietf-netconf-with-defaults", "ietf-netconf",
                           "ietf-snmp", "ietf-system", "ietf-x509-cert-to-name", "ietf-yang-smiv2",
                           "ietf-yang-types"
                         };
    int i, format;


    if (!strcmp(ctx->models.search_path, realpath(SCHEMA_FOLDER_YIN, path))) {
        extension = ".yin";
        format = LYS_IN_YIN;
    }  else {
        extension = ".yang";
        format = LYS_IN_YANG;
    }

    if (chdir(ctx->models.search_path)) {
        fprintf(stderr, "unable to open \"%s\" folder.\n", ctx->models.search_path);
        fail();
    }

    for (i = 0; i < 17; i++) {
        strcat(files[i], extension);
        fprintf(stdout, "Loading \"%s\" module ... ", files[i]);
        if (!(module = lys_parse_path(ctx, files[i], format))) {
            fprintf(stdout, "failed\n");
            fail();
        }
        fprintf(stdout, "ok\n");
        if (format == LYS_IN_YIN) {
            write_file(yang_files[i], "tmp1", module, LYS_OUT_YANG);
            write_file(yin_files[i], "tmp3", module, LYS_OUT_YIN);
        } else {
            write_file(yang_files[i + 17], "tmp2", module, LYS_OUT_YANG);
            write_file(yin_files[i + 17], "tmp4", module, LYS_OUT_YIN);
        }
    }
}
Ejemplo n.º 11
0
static void
test_unlink_choice(void **state)
{
    struct state *st = (struct state *)*state;

    /* schema */
    st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/when-unlink.yin", LYS_IN_YIN);
    assert_ptr_not_equal(st->mod, NULL);

    st->dt = lyd_new_path(NULL, st->ctx, "/when-unlink:top/cas2", NULL, 0, 0);
    assert_ptr_not_equal(st->dt, NULL);

    assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0);

    lyd_print_mem(&(st->xml), st->dt, LYD_XML, LYP_WITHSIBLINGS);
    assert_string_equal(st->xml, "<top xmlns=\"urn:libyang:tests:when-unlink\"><cas2/></top>");
}
Ejemplo n.º 12
0
static void
test_modules(void **state)
{
    struct ly_ctx *ctx = *state;
    char *extension, path[PATH_MAX];
    const struct lys_module *module;
    int i, format;

    if (!strcmp(ctx->models.search_paths[0], realpath(SCHEMA_FOLDER_YIN, path))) {
        extension = ".yin";
        format = LYS_IN_YIN;
    }  else {
        extension = ".yang";
        format = LYS_IN_YANG;
    }

    if (chdir(ctx->models.search_paths[0])) {
        fprintf(stderr, "unable to open \"%s\" folder.\n", ctx->models.search_paths[0]);
        fail();
    }

    for (i = 0; i < SCHEMA_COUNT; i++) {
        sprintf(path, "%s%s", files[i], extension);
        fprintf(stdout, "Loading \"%s\" module ... ", path);
        if (!(module = lys_parse_path(ctx, path, format))) {
            fprintf(stdout, "failed\n");
            fail();
        }
        fprintf(stdout, "ok\n");
        if (format == LYS_IN_YIN) {
            write_file(yang_files[i], "tmp1", module, LYS_OUT_YANG);
            write_file(yin_files[i], "tmp3", module, LYS_OUT_YIN);
        } else {
            write_file(yang_files[i + SCHEMA_COUNT], "tmp2", module, LYS_OUT_YANG);
            write_file(yin_files[i + SCHEMA_COUNT], "tmp4", module, LYS_OUT_YIN);
        }
    }
}
Ejemplo n.º 13
0
static void
TEST_RPC_OUTPUT(void **state)
{
    struct state *st = (*state);
    const int schemas_fail[] = {TEST_SCHEMA_LOAD_FAIL};
    const int data_files_fail[] = {TEST_DATA_FILE_LOAD_FAIL};
    char buf[1024];
    LYS_INFORMAT schema_format = LYS_IN_YANG;
    const struct lys_module *mod;
    int i, j, ret;
    const int data_compare_fail[] = {TEST_DATA_COMPARE_FAIL};
    const char *data_compare_string[] = {TEST_DATA_COMPARE};
    const char *data_rpc_name[] = {TEST_RPC_NODE};
    struct lyd_node *rpc;

    for (i = 0; i < 2; ++i) {
        for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
            sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.%s", j + 1, (schema_format == LYS_IN_YANG ? "yang" : "yin"));
            mod = lys_parse_path(st->ctx, buf, schema_format);
            if (schemas_fail[j]) {
                assert_ptr_equal(mod, NULL);
            } else {
                assert_ptr_not_equal(mod, NULL);
            }
        }

        for (j = 0; j < TEST_DATA_FILE_COUNT; ++j) {
            sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/data%d.xml", j + 1);
            rpc = lyd_new(NULL, mod, data_rpc_name[j]);
            st->node = lyd_parse_path(st->ctx, buf, LYD_XML, LYD_OPT_RPCREPLY, rpc, NULL);
            lyd_free(rpc);
            if (data_files_fail[j]) {
                assert_ptr_equal(st->node, NULL);
            } else {
                assert_ptr_not_equal(st->node, NULL);
                lyd_print_mem(&st->s, st->node, LYD_XML, LYP_WITHSIBLINGS | LYP_FORMAT | LYP_WD_ALL);
                if (data_compare_fail[j]) {
                    assert_ptr_equal(strstr(st->s, data_compare_string[j]), NULL);
                } else {
                    assert_ptr_not_equal(strstr(st->s, data_compare_string[j]), NULL);
                }
                free(st->s);
                st->s = NULL;
            }
            lyd_free_withsiblings(st->node);
            st->node = NULL;
        }

        if (schema_format == LYS_IN_YANG) {
            /* convert the modules */
            for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
                sprintf(buf, BUILD_DIR "/yang2yin "
                             TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yang "
                             TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1, j + 1);
                ret = system(buf);
                if (ret == -1) {
                    fprintf(stderr, "system() failed (%s).\n", strerror(errno));
                    fail();
                } else if (WEXITSTATUS(ret) != 0) {
                    fprintf(stderr, "Executing command \"%s\" finished with %d.\n", buf, WEXITSTATUS(ret));
                    fail();
                }
            }

            schema_format = LYS_IN_YIN;
        } else {
            /* remove the modules */
            for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
                sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1);
                if (unlink(buf)) {
                    fprintf(stderr, "unlink() on \"%s\" failed (%s).\n", buf, strerror(errno));
                }
            }
        }
    }
}
Ejemplo n.º 14
0
/**
 * @brief Initializes libyang ctx with all schemas installed for specified module in sysrepo.
 */
static int
srcfg_ly_init(struct ly_ctx **ly_ctx, const char *module_name)
{
    DIR *dp = NULL;
    struct dirent *ep = NULL;
    char *delim = NULL;
    char schema_filename[PATH_MAX] = { 0, };
    const struct lys_module *module = NULL;

    CHECK_NULL_ARG2(ly_ctx, module_name);

    *ly_ctx = ly_ctx_new(srcfg_schema_search_dir);
    if (NULL == *ly_ctx) {
        SR_LOG_ERR("Unable to initialize libyang context: %s", ly_errmsg());
        return SR_ERR_INTERNAL;
    }
    ly_set_log_clb(srcfg_ly_log_cb, 1);

    /* iterate over all files in the directory with schemas */
    dp = opendir(srcfg_schema_search_dir);
    if (NULL == dp) {
        SR_LOG_ERR("Failed to open the schema directory: %s.", sr_strerror_safe(errno));
        return SR_ERR_INTERNAL;
    }
    while (NULL != (ep = readdir(dp))) {
        /* test file extension */
        LYS_INFORMAT fmt = LYS_IN_UNKNOWN;
        if (sr_str_ends_with(ep->d_name, SR_SCHEMA_YIN_FILE_EXT)) {
            fmt = LYS_IN_YIN;
        } else if (sr_str_ends_with(ep->d_name, SR_SCHEMA_YANG_FILE_EXT)) {
            fmt = LYS_IN_YANG;
        }
        if (fmt != LYS_IN_UNKNOWN) {
            /* strip extension and revision */
            strcpy(schema_filename, ep->d_name);
            delim = strrchr(schema_filename, '.');
            assert(delim);
            *delim = '\0';
            delim = strrchr(schema_filename, '@');
            if (delim) {
                *delim = '\0';
            }
            /* TODO install all revisions and dependencies of the specified module, but not more */
#if 0 /* XXX install all schemas until we can resolve all dependencies */
            if (strcmp(schema_filename, module_name) == 0) {
#endif
                /* construct full file path */
                snprintf(schema_filename, PATH_MAX, "%s%s", srcfg_schema_search_dir, ep->d_name);
                /* load the schema into the context */
                SR_LOG_DBG("Loading module schema: '%s'.", schema_filename);
                module = lys_parse_path(*ly_ctx, schema_filename, fmt);
                if (NULL == module) {
                    continue;
                }
                for (uint8_t i = 0; i < module->features_size; i++) {
                    lys_features_enable(module, module->features[i].name);
                }
#if 0
            }
#endif
        }
    }
    closedir(dp);

    return SR_ERR_OK;
}