Annotation of embedaddon/confuse/tests/keyval.c, revision 1.1.1.1
1.1 misho 1: /* Test key=value pairs, e.g. env vars. a program should set to children */
2:
3: #include "config.h"
4: #include <string.h>
5: #include <stdlib.h>
6: #include "check_confuse.h"
7:
8: static void check_byname(cfg_t *cfg, const char *key, const char *val)
9: {
10: char *value;
11:
12: printf("Checking for key:%s, expected value %s\n", key, val);
13:
14: value = cfg_getstr(cfg, key);
15: printf("Found value %s\n", value);
16:
17: fail_unless(value != NULL);
18: fail_unless(!strcmp(value, val));
19: }
20:
21: static void check_keyval(cfg_t *cfg, unsigned int index, const char *key, const char *val)
22: {
23: cfg_opt_t *opt;
24: int rc;
25:
26: printf("Checking index %u for key:%s val:%s\n", index, key, val);
27:
28: opt = cfg_getnopt(cfg, index);
29: fail_unless(opt != NULL);
30:
31: printf("Found key:%s val:%s\n", cfg_opt_name(opt), cfg_opt_getstr(opt));
32:
33: rc =strcmp(cfg_opt_name(opt), key);
34: fail_unless(rc == 0);
35:
36: rc =strcmp(cfg_opt_getstr(opt), val);
37: fail_unless(rc == 0);
38: }
39:
40: int main(void)
41: {
42: cfg_opt_t opts[] = {
43: CFG_SEC("env", NULL, CFGF_KEYSTRVAL),
44: CFG_END()
45: };
46: unsigned int num;
47: cfg_t *cfg, *sec;
48: cfg_opt_t *opt;
49: char *key;
50: int rc;
51:
52: cfg = cfg_init(opts, CFGF_NONE);
53: fail_unless(cfg != NULL);
54:
55: rc = cfg_parse_buf(cfg, "env {\n"
56: " foo=bar\n"
57: " bar=for"
58: " baz=qux\n"
59: " bar=xyzzy\n" /* Should replace previous 'bar' */
60: "}");
61: fail_unless(rc == CFG_SUCCESS);
62:
63: sec = cfg_getsec(cfg, "env");
64: fail_unless(sec != NULL);
65:
66: /* Fuzz internals a bit, check for non-existing key */
67: cfg_getstr(sec, "some-key-not-in-the-config-file");
68:
69: num = cfg_num(sec);
70: fail_unless(num == 3); /* { foo, bar, baz } */
71:
72: check_keyval(sec, 0, "foo", "bar");
73: check_keyval(sec, 1, "bar", "xyzzy");
74: check_keyval(sec, 2, "baz", "qux");
75:
76: check_byname(sec, "foo", "bar");
77: check_byname(sec, "baz", "qux");
78: check_byname(sec, "bar", "xyzzy");
79:
80: printf("PASS\n");
81: cfg_free(cfg);
82:
83: return 0;
84: }
85:
86: /**
87: * Local Variables:
88: * indent-tabs-mode: t
89: * c-file-style: "linux"
90: * End:
91: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>