Exemple #1
0
static void
write_file(char *filename, const char *name, const struct lys_module *module, LYS_OUTFORMAT format)
{
    FILE *f;

    strcpy(filename, name);
    strcat(filename, "_");
    strcat(filename, module->name);
    f = fopen(filename, "w");
    if (!f) {
        fprintf(stderr, "unable to open \"%s\" file.\n", filename);
        fail();
    }
    lys_print_file(f, module, format, NULL);
    fclose(f);
}
Exemple #2
0
API int
lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, const char *target_node,
               int line_length, int options)
{
    FILE *f;
    int ret;

    if (!path || !module) {
        LOGARG;
        return EXIT_FAILURE;
    }

    f = fopen(path, "w");
    if (!f) {
        LOGERR(module->ctx, LY_ESYS, "Failed to open file \"%s\" (%s).", path, strerror(errno));
        return EXIT_FAILURE;
    }

    ret = lys_print_file(f, module, format, target_node, line_length, options);
    fclose(f);
    return ret;
}
static void
test_lys_print_file_jsons(void **state)
{
    (void) state; /* unused */
    const struct lys_module *module;
    LYS_INFORMAT yang_format = LYS_IN_YIN;
    struct stat sb;
    char *target = "grouping/gg";
    char file_name1[20];
    char file_name2[20];
    char *result;
    FILE *f1 = NULL, *f2 = NULL;
    int rc;
    int fd1 = -1, fd2 = -1;

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

    memset(file_name1, 0, sizeof(file_name1));
    memset(file_name2, 0, sizeof(file_name2));
    strncpy(file_name1, TMP_TEMPLATE, sizeof(file_name1));
    strncpy(file_name2, TMP_TEMPLATE, sizeof(file_name2));

    fd1 = mkstemp(file_name1);
    fd2 = mkstemp(file_name2);
    if (fd1 < 1 || fd2 < 1) {
        goto error;
    }
    close(fd1);
    close(fd2);

    f1 = (fopen(file_name1,"r+"));
    f2 = (fopen(file_name2,"r+"));
    if (!f1 || !f2) {
        goto error;
    }

    /* module */
    rc = lys_print_file(f1, module, LYS_OUT_JSON, NULL, 0, 0);
    if (rc) {
        goto error;
    }

    fclose(f1); f1 = NULL;

    fd1 = open(file_name1, O_RDONLY);
    if (fstat(fd1, &sb) == -1 || !S_ISREG(sb.st_mode)) {
        goto error;
    }

    result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
    assert_string_equal(result_jsons, result);

    /* grouping */
    rc = lys_print_file(f2, module, LYS_OUT_JSON, target, 0, 0);
    if (rc) {
        goto error;
    }

    fclose(f2); f2 = NULL;

    fd2 = open(file_name2, O_RDONLY);
    if (fstat(fd2, &sb) == -1 || !S_ISREG(sb.st_mode)) {
        goto error;
    }

    result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
    assert_string_equal(result_jsons_grouping, result);

    close(fd1);
    close(fd2);
    unlink(file_name1);
    unlink(file_name2);

    return;
error:
    if (f1)
        fclose(f1);
    if (fd1 > 0) {
        unlink(file_name1);
        close(fd1);
    }
    if (f2)
        fclose(f2);
    if (fd2 > 0) {
        unlink(file_name2);
        close(fd2);
    }
    fail();
}
static void
test_lys_print_file_info(void **state)
{
    (void) state; /* unused */
    const struct lys_module *module;
    LYS_INFORMAT yang_format = LYS_IN_YIN;
    struct stat sb;
    char *target = "feature/foo";
    char file_name[20];
    char *result;
    FILE *f = NULL;
    int rc;
    int fd = -1;

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

    memset(file_name, 0, sizeof(file_name));
    strncpy(file_name, TMP_TEMPLATE, sizeof(file_name));

    fd = mkstemp(file_name);
    if (fd < 1) {
        goto error;
    }
    close(fd);

    f = (fopen(file_name,"r+"));
    if (f == NULL) {
        goto error;
    }

    rc = lys_print_file(f, module, LYS_OUT_INFO, target, 0, 0);
    if (rc) {
        goto error;
    }

    fclose(f);

    fd = open(file_name, O_RDONLY);
    if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
        goto error;
    }

    result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);

    assert_string_equal(result_info, result);

    close(fd);
    unlink(file_name);

    return;
error:
    if (f)
        fclose(f);
    if (fd > 0) {
        unlink(file_name);
        close(fd);
    }
    fail();
}