Annotation of embedaddon/confuse/tests/suite_single.c, revision 1.1.1.2
1.1.1.2 ! misho 1: #define _GNU_SOURCE
1.1 misho 2: #include "check_confuse.h"
3: #include <string.h>
4: #include <stdlib.h>
5: #include <sys/types.h>
1.1.1.2 ! misho 6: #include "../src/compat.h"
1.1 misho 7:
8: static cfg_t *cfg;
9:
10: int parse_ip_address(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
11: {
1.1.1.2 ! misho 12: int i;
! 13: unsigned int e[4];
! 14: unsigned char *addr;
! 15:
! 16: if (sscanf(value, "%u.%u.%u.%u", e + 0, e + 1, e + 2, e + 3) != 4) {
! 17: /*cfg_error(cfg, "invalid IP address %s in section %s", value, cfg->name); */
! 18: return 1;
! 19: }
! 20: addr = (unsigned char *)malloc(sizeof(4));
! 21: for (i = 0; i < 4; i++) {
! 22: if (e[i] <= 0xff) {
! 23: addr[i] = e[i];
! 24: } else {
! 25: free(addr);
! 26: return 1;
! 27: }
! 28: }
! 29: *(void **)result = (void *)addr;
! 30: return 0;
1.1 misho 31: }
32:
33: static unsigned char *my_ether_aton(const char *addr)
34: {
1.1.1.2 ! misho 35: int i;
! 36: static unsigned int e[6];
! 37: static unsigned char ec[6];
! 38:
! 39: if (sscanf(addr, "%x:%x:%x:%x:%x:%x", &e[0], &e[1], &e[2], &e[3], &e[4], &e[5]) != 6) {
! 40: return NULL;
! 41: }
! 42: for (i = 0; i < 6; i++) {
! 43: if (e[i] <= 0xff)
! 44: ec[i] = e[i];
! 45: else
! 46: return NULL;
! 47: }
! 48: return ec;
1.1 misho 49: }
50:
51: static char *my_ether_ntoa(unsigned char *addr)
52: {
1.1.1.2 ! misho 53: static char buf[18];
! 54:
! 55: sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
! 56:
! 57: return buf;
! 58: }
! 59:
! 60: static char *my_inet_ntoa(unsigned char *addr)
! 61: {
! 62: static char buf[16];
! 63:
! 64: sprintf(buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
! 65:
! 66: return buf;
1.1 misho 67: }
68:
69: int parse_ether_address(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
70: {
1.1.1.2 ! misho 71: unsigned char *tmp;
1.1 misho 72:
1.1.1.2 ! misho 73: tmp = my_ether_aton(value);
! 74: if (tmp == 0) {
! 75: /*cfg_error(cfg, "invalid Ethernet address %s in section %s", value, cfg->name); */
! 76: return 1;
! 77: }
! 78: *(void **)result = malloc(6);
! 79: memcpy(*(void **)result, tmp, 6);
! 80: return 0;
1.1 misho 81: }
82:
83: void single_setup(void)
84: {
1.1.1.2 ! misho 85: static cfg_opt_t subsec_opts[] = {
! 86: CFG_STR("subsubstring", "subsubdefault", CFGF_NONE),
! 87: CFG_INT("subsubinteger", -42, CFGF_NONE),
! 88: CFG_FLOAT("subsubfloat", 19923.1234, CFGF_NONE),
! 89: CFG_BOOL("subsubbool", cfg_false, CFGF_NONE),
! 90: CFG_END()
! 91: };
! 92:
! 93: static cfg_opt_t sec_opts[] = {
! 94: CFG_STR("substring", "subdefault", CFGF_NONE),
! 95: CFG_INT("subinteger", 17, CFGF_NONE),
! 96: CFG_FLOAT("subfloat", 8.37, CFGF_NONE),
! 97: CFG_BOOL("subbool", cfg_true, CFGF_NONE),
! 98: CFG_SEC("subsection", subsec_opts, CFGF_NONE),
! 99: CFG_END()
! 100: };
! 101:
! 102: static cfg_opt_t nodef_opts[] = {
! 103: CFG_STR("string", "defvalue", CFGF_NONE),
! 104: CFG_INT("int", -17, CFGF_NODEFAULT),
! 105: CFG_END()
! 106: };
! 107:
! 108: cfg_opt_t opts[] = {
! 109: CFG_STR("string", "default", CFGF_NONE),
! 110: CFG_INT("integer", 4711, CFGF_NONE),
! 111: CFG_FLOAT("float", 0.42, CFGF_NONE),
! 112: CFG_BOOL("bool", cfg_false, CFGF_NONE),
! 113: CFG_PTR_CB("ip-address", 0, CFGF_NONE, parse_ip_address, free),
! 114: CFG_PTR_CB("ethernet-address", 0, CFGF_NONE, parse_ether_address, free),
! 115: CFG_SEC("section", sec_opts, CFGF_NONE),
! 116: CFG_STR("nodefstring", "not used", CFGF_NODEFAULT),
! 117: CFG_SEC("nodefsec", nodef_opts, CFGF_NODEFAULT),
! 118: CFG_END()
! 119: };
! 120:
! 121: cfg = cfg_init(opts, 0);
! 122:
! 123: memset(opts, 0, sizeof(opts));
! 124: memset(sec_opts, 0, sizeof(sec_opts));
! 125: memset(subsec_opts, 0, sizeof(subsec_opts));
1.1 misho 126: }
127:
128: void single_teardown(void)
129: {
1.1.1.2 ! misho 130: cfg_free(cfg);
1.1 misho 131: }
132:
133: void single_string_test(void)
134: {
1.1.1.2 ! misho 135: char *buf;
1.1 misho 136:
1.1.1.2 ! misho 137: fail_unless(cfg_size(cfg, "string") == 1);
! 138: fail_unless(cfg_opt_size(cfg_getopt(cfg, "string")) == 1);
! 139: fail_unless(strcmp(cfg_getstr(cfg, "string"), "default") == 0);
! 140: fail_unless(cfg_getnstr(cfg, "string", 0) == cfg_getstr(cfg, "string"));
! 141: fail_unless(cfg_getnstr(cfg, "string", 1) == 0);
! 142: buf = "string = 'set'";
! 143: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 144: fail_unless(strcmp(cfg_getstr(cfg, "string"), "set") == 0);
! 145: cfg_setstr(cfg, "string", "manually set");
! 146: fail_unless(strcmp(cfg_getstr(cfg, "string"), "manually set") == 0);
! 147: buf = "multi set";
! 148: fail_unless(cfg_setmulti(cfg, "string", 1, &buf) == 0);
! 149: fail_unless(strcmp(cfg_getstr(cfg, "string"), "multi set") == 0);
1.1 misho 150: }
151:
152: void single_integer_test(void)
153: {
1.1.1.2 ! misho 154: char *buf;
1.1 misho 155:
1.1.1.2 ! misho 156: fail_unless(cfg_size(cfg, "integer") == 1);
! 157: fail_unless(cfg_opt_size(cfg_getopt(cfg, "integer")) == 1);
! 158: fail_unless(cfg_getint(cfg, "integer") == 4711);
! 159: fail_unless(cfg_getnint(cfg, "integer", 0) == cfg_getint(cfg, "integer"));
! 160: fail_unless(cfg_getnint(cfg, "integer", 1) == 0);
! 161: buf = "integer = -46";
! 162: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 163: fail_unless(cfg_getint(cfg, "integer") == -46);
! 164: cfg_setint(cfg, "integer", 999999);
! 165: fail_unless(cfg_getint(cfg, "integer") == 999999);
! 166: buf = "42";
! 167: fail_unless(cfg_setmulti(cfg, "integer", 1, &buf) == 0);
! 168: fail_unless(cfg_getint(cfg, "integer") == 42);
! 169: buf = "ouch";
! 170: fail_unless(cfg_setmulti(cfg, "integer", 1, &buf) == -1);
! 171: fail_unless(cfg_getint(cfg, "integer") == 42);
1.1 misho 172: }
173:
174: void single_float_test(void)
175: {
1.1.1.2 ! misho 176: char *buf;
1.1 misho 177:
1.1.1.2 ! misho 178: fail_unless(cfg_size(cfg, "float") == 1);
! 179: fail_unless(cfg_opt_size(cfg_getopt(cfg, "float")) == 1);
! 180: fail_unless(cfg_getfloat(cfg, "float") == 0.42);
! 181: fail_unless(cfg_getnfloat(cfg, "float", 0) == cfg_getfloat(cfg, "float"));
! 182: fail_unless(cfg_getnfloat(cfg, "float", 1) == 0);
! 183: buf = "float = -46.777";
! 184: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 185: fail_unless(cfg_getfloat(cfg, "float") == -46.777);
! 186: cfg_setfloat(cfg, "float", 5.1234e2);
! 187: fail_unless(cfg_getfloat(cfg, "float") == 5.1234e2);
! 188: buf = "4.2";
! 189: fail_unless(cfg_setmulti(cfg, "float", 1, &buf) == 0);
! 190: fail_unless(cfg_getfloat(cfg, "float") == 4.2);
! 191: buf = "ouch";
! 192: fail_unless(cfg_setmulti(cfg, "float", 1, &buf) == -1);
! 193: fail_unless(cfg_getfloat(cfg, "float") == 4.2);
1.1 misho 194: }
195:
196: void single_bool_test(void)
197: {
1.1.1.2 ! misho 198: char *buf;
1.1 misho 199:
1.1.1.2 ! misho 200: fail_unless(cfg_size(cfg, "bool") == 1);
! 201: fail_unless(cfg_opt_size(cfg_getopt(cfg, "bool")) == 1);
! 202: fail_unless(cfg_getbool(cfg, "bool") == cfg_false);
! 203:
! 204: buf = "bool = yes";
! 205: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 206: fail_unless(cfg_getbool(cfg, "bool") == cfg_true);
! 207: buf = "bool = no";
! 208: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 209: fail_unless(cfg_getbool(cfg, "bool") == cfg_false);
! 210: buf = "bool = true";
! 211: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 212: fail_unless(cfg_getbool(cfg, "bool") == cfg_true);
! 213: buf = "bool = false";
! 214: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 215: fail_unless(cfg_getbool(cfg, "bool") == cfg_false);
! 216: buf = "bool = on";
! 217: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 218: fail_unless(cfg_getbool(cfg, "bool") == cfg_true);
! 219: buf = "bool = off";
! 220: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 221: fail_unless(cfg_getbool(cfg, "bool") == cfg_false);
! 222:
! 223: cfg_setbool(cfg, "bool", cfg_true);
! 224: fail_unless(cfg_getbool(cfg, "bool") == cfg_true);
! 225: cfg_setbool(cfg, "bool", cfg_false);
! 226: fail_unless(cfg_getbool(cfg, "bool") == cfg_false);
! 227:
! 228: buf = "true";
! 229: fail_unless(cfg_setmulti(cfg, "bool", 1, &buf) == 0);
! 230: fail_unless(cfg_getbool(cfg, "bool") == cfg_true);
! 231: buf = "later";
! 232: fail_unless(cfg_setmulti(cfg, "bool", 1, &buf) == -1);
! 233: fail_unless(cfg_getbool(cfg, "bool") == cfg_true);
1.1 misho 234: }
235:
236: void single_section_test(void)
237: {
1.1.1.2 ! misho 238: char *buf;
! 239: cfg_t *sec, *subsec;
1.1 misho 240:
1.1.1.2 ! misho 241: fail_unless(cfg_size(cfg, "section") == 1);
! 242: fail_unless(cfg_opt_size(cfg_getopt(cfg, "section")) == 1);
1.1 misho 243:
1.1.1.2 ! misho 244: fail_unless(cfg_size(cfg, "section|subsection") == 1);
! 245: fail_unless(cfg_opt_size(cfg_getopt(cfg, "section|subsection")) == 1);
1.1 misho 246:
1.1.1.2 ! misho 247: fail_unless(cfg_size(cfg, "section|subsection|subsubstring") == 1);
! 248: fail_unless(cfg_opt_size(cfg_getopt(cfg, "section|subsection|subsubinteger")) == 1);
! 249:
! 250: fail_unless(cfg_title(cfg_getsec(cfg, "section")) == 0);
! 251: fail_unless(cfg_title(cfg_getsec(cfg, "section|subsection")) == 0);
! 252:
! 253: fail_unless(strcmp(cfg_getstr(cfg, "section|substring"), "subdefault") == 0);
! 254: sec = cfg_getsec(cfg, "section|subsection");
! 255: fail_unless(sec != 0);
! 256: fail_unless(cfg_getint(sec, "subsubinteger") == -42);
! 257:
! 258: fail_unless(cfg_getnsec(cfg, "section", 0) == cfg_getsec(cfg, "section"));
! 259: fail_unless(cfg_getnsec(cfg, "section", 1) == 0);
! 260:
! 261: sec = cfg_getsec(cfg, "section");
! 262: fail_unless(sec != 0);
! 263: subsec = cfg_getsec(sec, "subsection");
! 264: fail_unless(subsec != 0);
! 265: fail_unless(cfg_getfloat(subsec, "subsubfloat") == 19923.1234);
! 266:
! 267: buf = "section { substring = \"foo\" subsection { subsubstring = \"bar\"} }";
! 268: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
1.1 misho 269: }
270:
271: void single_ptr_test(void)
272: {
1.1.1.2 ! misho 273: char *buf;
! 274: char tmp[80];
! 275: char addr[] = { 0xC0, 0xA8, 0x00, 0x01, 0 }; /* 192.168.0.1 */
! 276: struct in_addr *ipaddr;
! 277: unsigned char *etheraddr, *cmpether;
! 278:
! 279: fail_unless(cfg_size(cfg, "ip-address") == 0);
! 280:
! 281: /* Test valid IPv4 address */
! 282: snprintf(tmp, sizeof(tmp), "ip-address = %s", my_inet_ntoa(addr));
! 283: fail_unless(cfg_parse_buf(cfg, tmp) == CFG_SUCCESS);
! 284: ipaddr = cfg_getptr(cfg, "ip-address");
! 285: fail_unless(ipaddr != 0);
! 286: fail_unless(memcmp(addr, ipaddr, 4) == 0);
! 287:
! 288: /* Test invalid IPv4 address */
! 289: buf = "ip-address = 192.168.0.325";
! 290: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 291:
! 292: buf = "ethernet-address = '00:03:93:d4:05:58'";
! 293: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 294: etheraddr = cfg_getptr(cfg, "ethernet-address");
! 295: fail_unless(etheraddr != 0);
! 296: fail_unless(my_ether_ntoa(etheraddr) != 0);
! 297: cmpether = my_ether_aton("00:03:93:d4:05:58");
! 298: fail_unless(memcmp(etheraddr, cmpether, 6) == 0);
1.1 misho 299:
1.1.1.2 ! misho 300: buf = "ethernet-address = '00:03:93:d4:05'";
! 301: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
1.1 misho 302: }
303:
304: void parse_buf_test(void)
305: {
1.1.1.2 ! misho 306: char *buf;
1.1 misho 307:
1.1.1.2 ! misho 308: fail_unless(cfg_parse_buf(cfg, 0) == CFG_SUCCESS);
! 309: fail_unless(cfg_parse_buf(cfg, "") == CFG_SUCCESS);
1.1 misho 310:
1.1.1.2 ! misho 311: buf = "bool = wrong";
! 312: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
1.1 misho 313:
1.1.1.2 ! misho 314: buf = "string = ";
! 315: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
1.1 misho 316:
1.1.1.2 ! misho 317: buf = "option = 'value'";
! 318: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
1.1 misho 319: }
320:
321: void nonexistent_option_test(void)
322: {
1.1.1.2 ! misho 323: fail_unless(cfg_getopt(cfg, "nonexistent") == 0);
! 324: fail_unless(cfg_getopt(cfg, "section|subnonexistent") == 0);
1.1 misho 325: }
326:
327: void nodefault_test(void)
328: {
1.1.1.2 ! misho 329: char *buf;
! 330: cfg_t *nodefsec;
1.1 misho 331:
1.1.1.2 ! misho 332: fail_unless(cfg_size(cfg, "nodefstring") == 0);
! 333: cfg_setstr(cfg, "nodefstring", "manually set");
! 334: fail_unless(cfg_size(cfg, "nodefstring") == 1);
! 335: fail_unless(cfg_size(cfg, "nodefsec") == 0);
! 336:
! 337: buf = "nodefsec {}";
! 338: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 339: nodefsec = cfg_getsec(cfg, "nodefsec");
! 340: fail_unless(nodefsec != 0);
! 341: fail_unless(cfg_size(nodefsec, "string") == 1);
! 342: fail_unless(cfg_size(nodefsec, "int") == 0);
! 343: }
! 344:
! 345: int main(void)
! 346: {
! 347: single_setup();
! 348:
! 349: single_string_test();
! 350: single_integer_test();
! 351: single_float_test();
! 352: single_bool_test();
! 353: single_section_test();
! 354: single_ptr_test();
! 355: parse_buf_test();
! 356: nonexistent_option_test();
! 357: nodefault_test();
1.1 misho 358:
1.1.1.2 ! misho 359: single_teardown();
1.1 misho 360:
1.1.1.2 ! misho 361: return 0;
! 362: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>