示例#1
0
文件: test-run.c 项目: soymo/augeas
static int run_one_test(struct test *test) {
    int r;
    struct augeas *aug = NULL;
    struct memstream ms;
    int result = 0;

    aug = aug_init("/dev/null", NULL, AUG_NO_STDINC|AUG_NO_LOAD);
    fail(aug == NULL, "aug_init");
    fail(aug_error(aug) != AUG_NOERROR, "aug_init: errcode was %d",
         aug_error(aug));

    printf("%-30s ... ", test->name);

    r = init_memstream(&ms);
    fail(r < 0, "init_memstream");

    r = aug_srun(aug, ms.stream, test->cmd);
    fail(r != test->result, "return value: expected %d, actual %d",
         test->result, r);
    fail(aug_error(aug) != test->errcode, "errcode: expected %s, actual %s",
         errtokens[test->errcode], errtokens[aug_error(aug)]);

    r = close_memstream(&ms);
    fail(r < 0, "close_memstream");
    fail(ms.buf == NULL, "close_memstream left buf NULL");

    if (test->out != NULL) {
        fail(STRNEQ(ms.buf, test->out), "output: expected '%s', actual '%s'",
             test->out, ms.buf);
    } else if (test->out_present) {
        fail(strlen(ms.buf) == 0,
             "output: expected some output");
    } else {
        fail(strlen(ms.buf) > 0,
             "output: expected nothing, actual '%s'", ms.buf);
    }
    printf("PASS\n");

 done:
    free(ms.buf);
    aug_close(aug);
    return result;
 error:
    result = -1;
    goto done;
}
示例#2
0
static gchar * network_interfaces ()
{
        augeas *aug;
        gchar *value = NULL, **if_match, **option_match, *path, *result, *p, option[128];
        gint if_number, option_number, i, j;

        aug = aug_init (NULL, NULL, AUG_NONE | AUG_NO_ERR_CLOSE | AUG_NO_MODL_AUTOLOAD);
        aug_set (aug, "/augeas/load/Interfaces/lens", "Interfaces.lns");
        aug_set (aug, "/augeas/load/Interfaces/incl", "/etc/network/interfaces");
        aug_load (aug);
        aug_get (aug, "//files/etc/network/interfaces", (const gchar **)&value);
        if_number = aug_match (aug, "//files/etc/network/interfaces/iface[.!='lo']", &if_match);
        result = g_strdup ("[");
        for (i = 0; i < if_number; i++) {
                aug_get (aug, if_match[i], (const gchar **)&value);
                p = result;
                result = g_strdup_printf ("%s{\"name\": \"%s\",", p, value);
                g_free (p);
                p = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/*", value);
                option_number = aug_match (aug, p, &option_match);
                g_free (p);
                for (j = 0; j < option_number; j++) {
                        sscanf (option_match[j], "%*[^]]]/%[^/]", option);
                        if (g_str_has_prefix (option, "#comment")) {
                                continue;
                        }
                        aug_get (aug, option_match[j], (const gchar **)&value);
                        p = result;
                        result = g_strdup_printf ("%s\n\"%s\": \"%s\",", p, option, value);
                        g_free (p);
                        g_free (option_match[j]);
                }
                g_free (option_match);
                g_free (if_match[i]);
                result[strlen (result) - 1] = '}';
                p = result;
                result = g_strdup_printf ("%s,", p);
                g_free (p);
        }
        g_free (if_match);
        aug_close (aug);
        result[strlen (result) - 1] = ']';

        return result;
}
示例#3
0
文件: test-api.c 项目: flebel/augeas
static void testGet(CuTest *tc) {
    int r;
    const char *value;
    struct augeas *aug;

    aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD);
    CuAssertPtrNotNull(tc, aug);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* Make sure we're looking at the right thing */
    r = aug_match(aug, "/augeas/version/save/*", NULL);
    CuAssertTrue(tc, r > 1);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* aug_get returns 1 and the value if exactly one node matches */
    r = aug_get(aug, "/augeas/version/save/*[1]", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* aug_get returns 0 and no value when no node matches */
    r = aug_get(aug, "/augeas/version/save/*[ last() + 1 ]", &value);
    CuAssertIntEquals(tc, 0, r);
    CuAssertPtrEquals(tc, NULL, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* aug_get should return an error when multiple nodes match */
    r = aug_get(aug, "/augeas/version/save/*", &value);
    CuAssertIntEquals(tc, -1, r);
    CuAssertPtrEquals(tc, NULL, value);
    CuAssertIntEquals(tc, AUG_EMMATCH, aug_error(aug));

    /* augeas should prepend context if relative path given */
    r = aug_set(aug, "/augeas/context", "/augeas/version");
    r = aug_get(aug, "save/*[1]", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* augeas should still work with an empty context */
    r = aug_set(aug, "/augeas/context", "");
    r = aug_get(aug, "/augeas/version", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* augeas should ignore trailing slashes in context */
    r = aug_set(aug, "/augeas/context", "/augeas/version/");
    r = aug_get(aug, "save/*[1]", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* augeas should create non-existent context path */
    r = aug_set(aug, "/augeas/context", "/context/foo");
    r = aug_set(aug, "bar", "value");
    r = aug_get(aug, "/context/foo/bar", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    aug_close(aug);
}
示例#4
0
文件: augparse.c 项目: afcady/augeas
int main(int argc, char **argv) {
    int opt;
    struct augeas *aug;
    char *loadpath = NULL;
    size_t loadpathlen = 0;
    enum {
        VAL_NO_STDINC = CHAR_MAX + 1,
        VAL_NO_TYPECHECK = VAL_NO_STDINC + 1,
        VAL_VERSION = VAL_NO_TYPECHECK + 1
    };
    struct option options[] = {
        { "help",      0, 0, 'h' },
        { "include",   1, 0, 'I' },
        { "trace",     0, 0, 't' },
        { "nostdinc",  0, 0, VAL_NO_STDINC },
        { "notypecheck",  0, 0, VAL_NO_TYPECHECK },
        { "version",  0, 0, VAL_VERSION },
        { 0, 0, 0, 0}
    };
    int idx;
    unsigned int flags = AUG_TYPE_CHECK|AUG_NO_MODL_AUTOLOAD;
    progname = argv[0];

    setlocale(LC_ALL, "");
    while ((opt = getopt_long(argc, argv, "hI:t", options, &idx)) != -1) {
        switch(opt) {
        case 'I':
            argz_add(&loadpath, &loadpathlen, optarg);
            break;
        case 't':
            flags |= AUG_TRACE_MODULE_LOADING;
            break;
        case 'h':
            usage();
            break;
        case VAL_NO_STDINC:
            flags |= AUG_NO_STDINC;
            break;
        case VAL_NO_TYPECHECK:
            flags &= ~(AUG_TYPE_CHECK);
            break;
        case VAL_VERSION:
            print_version = true;
            break;
        default:
            usage();
            break;
        }
    }

    if (!print_version && optind >= argc) {
        fprintf(stderr, "Expected .aug file\n");
        usage();
    }

    argz_stringify(loadpath, loadpathlen, PATH_SEP_CHAR);
    aug = aug_init(NULL, loadpath, flags);
    if (aug == NULL) {
        fprintf(stderr, "Memory exhausted\n");
        return 2;
    }

    if (print_version) {
        print_version_info(aug);
        return EXIT_SUCCESS;
    }

    if (__aug_load_module_file(aug, argv[optind]) == -1) {
        fprintf(stderr, "%s\n", aug_error_message(aug));
        const char *s = aug_error_details(aug);
        if (s != NULL) {
            fprintf(stderr, "%s\n", s);
        }
        exit(EXIT_FAILURE);
    }

    aug_close(aug);
    free(loadpath);
}
示例#5
0
static void teardown(ATTRIBUTE_UNUSED CuTest *tc) {
    aug_close(aug);
    aug = NULL;
    free(root);
    root = NULL;
}
示例#6
0
static void augeas_free(augeas *aug) {
    if (aug != NULL)
        aug_close(aug);
}
示例#7
0
static gchar * set_network_interfaces (RequestData *request_data)
{
        gchar *interfaces, *result, *name, *path, *value;
        augeas *aug;
        JSON_Value *val;
        JSON_Array *array;
        JSON_Object *obj;
        gint if_count, i, ret;

        aug = aug_init (NULL, NULL, AUG_NONE | AUG_NO_ERR_CLOSE | AUG_NO_MODL_AUTOLOAD);
        aug_set (aug, "/augeas/load/Interfaces/lens", "Interfaces.lns");
        aug_set (aug, "/augeas/load/Interfaces/incl", "/etc/network/interfaces");
        aug_load (aug);
        interfaces = request_data->raw_request + request_data->header_size;
        val = json_parse_string (interfaces);
        if (val == NULL) {
                GST_ERROR ("parse json type interfaces failure");
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"invalid data\"\n}");
                aug_close (aug);
                return result;
        }
        array = json_value_get_array (val);
        if (array == NULL) {
                GST_ERROR ("get interfaces array failure");
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"not array type interfaces\"\n}");
                aug_close (aug);
                json_value_free (val);
                return result;
        }
        if_count = json_array_get_count (array);
        for (i = 0; i < if_count; i++) {
                obj = json_array_get_object (array, i);
                name = (gchar *)json_object_get_string (obj, "name");
                value = (gchar *)json_object_get_string (obj, "method");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/method", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "address");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/address", name);
                        ret = aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "netmask");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/netmask", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "network");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/network", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "broadcast");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/broadcast", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "gateway");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/gateway", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
        }
        if (aug_save (aug) == -1) {
                aug_get (aug, "/augeas//error", (const gchar **)&value);
                GST_ERROR ("set /etc/network/interface failure: %s", value);
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"%s\"\n}", value);

        } else {
                result = g_strdup ("{\n    \"result\": \"success\"\n}");
        }
        json_value_free (val);
        aug_close (aug);

        return result;
}