Annotation of embedaddon/confuse/tests/suite_validate.c, revision 1.1
1.1 ! misho 1: #include "check_confuse.h"
! 2: #include <string.h>
! 3: #include <sys/types.h>
! 4: #include <sys/socket.h>
! 5: #include <netinet/in.h>
! 6: #include <arpa/inet.h>
! 7:
! 8: static cfg_t *cfg = 0;
! 9:
! 10: #define ACTION_NONE 0
! 11: #define ACTION_RUN 1
! 12: #define ACTION_WALK 2
! 13: #define ACTION_CRAWL 3
! 14: #define ACTION_JUMP 4
! 15:
! 16: int parse_action(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
! 17: {
! 18: if(strcmp(value, "run") == 0)
! 19: *(int *)result = ACTION_RUN;
! 20: else if(strcmp(value, "walk") == 0)
! 21: *(int *)result = ACTION_WALK;
! 22: else if(strcmp(value, "crawl") == 0)
! 23: *(int *)result = ACTION_CRAWL;
! 24: else if(strcmp(value, "jump") == 0)
! 25: *(int *)result = ACTION_JUMP;
! 26: else
! 27: {
! 28: /* cfg_error(cfg, "Invalid action value '%s'", value); */
! 29: return -1;
! 30: }
! 31: return 0;
! 32: }
! 33:
! 34: int validate_speed(cfg_t *cfg, cfg_opt_t *opt)
! 35: {
! 36: int i;
! 37:
! 38: for(i = 0; i < cfg_opt_size(opt); i++)
! 39: {
! 40: if(cfg_opt_getnint(opt, i) <= 0)
! 41: {
! 42: /* cfg_error(cfg, "speed must be positive in section %s", cfg->name); */
! 43: return 1;
! 44: }
! 45: }
! 46: return 0;
! 47: }
! 48:
! 49: int validate_ip(cfg_t *cfg, cfg_opt_t *opt)
! 50: {
! 51: int i;
! 52:
! 53: for(i = 0; i < cfg_opt_size(opt); i++)
! 54: {
! 55: struct in_addr addr;
! 56: char *ip = cfg_opt_getnstr(opt, i);
! 57: if(inet_aton(ip, &addr) == 0)
! 58: {
! 59: /* cfg_error(cfg, "invalid IP address %s in section %s", ip, cfg->name); */
! 60: return 1;
! 61: }
! 62: }
! 63: return 0;
! 64: }
! 65:
! 66: int validate_action(cfg_t *cfg, cfg_opt_t *opt)
! 67: {
! 68: cfg_opt_t *name_opt;
! 69: cfg_t *action_sec = cfg_opt_getnsec(opt, 0);
! 70:
! 71: fail_unless(action_sec != 0);
! 72:
! 73: name_opt = cfg_getopt(action_sec, "name");
! 74:
! 75: fail_unless(name_opt != 0);
! 76: fail_unless(cfg_opt_size(name_opt) == 1);
! 77:
! 78: if(cfg_opt_getnstr(name_opt, 0) == NULL)
! 79: {
! 80: /* cfg_error(cfg, "missing required option 'name' in section %s", opt->name); */
! 81: return 1;
! 82: }
! 83: return 0;
! 84: }
! 85:
! 86: void validate_setup(void)
! 87: {
! 88: cfg_opt_t *opt = 0;
! 89:
! 90: cfg_opt_t action_opts[] =
! 91: {
! 92: CFG_INT("speed", 0, CFGF_NONE),
! 93: CFG_STR("name", 0, CFGF_NONE),
! 94: CFG_INT("xspeed", 0, CFGF_NONE),
! 95: CFG_END()
! 96: };
! 97:
! 98: cfg_opt_t multi_opts[] =
! 99: {
! 100: CFG_INT_LIST("speeds", 0, CFGF_NONE),
! 101: //{"speeds",CFGT_INT,0,0,CFGF_LIST,0,{0,0,cfg_false,0,0},0,0,0,validate_speed,0},
! 102: CFG_SEC("options", action_opts, CFGF_NONE),
! 103: CFG_END()
! 104: };
! 105:
! 106: cfg_opt_t opts[] =
! 107: {
! 108: CFG_STR_LIST("ip-address", 0, CFGF_NONE),
! 109: CFG_INT_CB("action", ACTION_NONE, CFGF_NONE, parse_action),
! 110: CFG_SEC("options", action_opts, CFGF_NONE),
! 111: CFG_SEC("multi_options", multi_opts, CFGF_MULTI),
! 112: CFG_END()
! 113: };
! 114:
! 115: cfg = cfg_init(opts, 0);
! 116:
! 117: cfg_set_validate_func(cfg, "ip-address", validate_ip);
! 118: fail_unless(cfg_set_validate_func(cfg, "ip-address", validate_ip) == validate_ip);
! 119: opt = cfg_getopt(cfg, "ip-address");
! 120: fail_unless(opt != 0);
! 121: fail_unless(opt->validcb == validate_ip);
! 122:
! 123: cfg_set_validate_func(cfg, "options", validate_action);
! 124: fail_unless(cfg_set_validate_func(cfg, "options", validate_action) == validate_action);
! 125: opt = cfg_getopt(cfg, "options");
! 126: fail_unless(opt != 0);
! 127: fail_unless(opt->validcb == validate_action);
! 128:
! 129: cfg_set_validate_func(cfg, "options|speed", validate_speed);
! 130: fail_unless(cfg_set_validate_func(cfg, "options|speed", validate_speed) == validate_speed);
! 131: opt = cfg_getopt(cfg, "options|speed");
! 132: fail_unless(opt != 0);
! 133: fail_unless(opt->validcb == validate_speed);
! 134:
! 135: cfg_set_validate_func(cfg, "multi_options|speeds", validate_speed);
! 136: fail_unless(cfg_set_validate_func(cfg, "multi_options|speeds", validate_speed) == validate_speed);
! 137:
! 138: cfg_set_validate_func(cfg, "multi_options|options|xspeed", validate_speed);
! 139: fail_unless(cfg_set_validate_func(cfg, "multi_options|options|xspeed", validate_speed) == validate_speed);
! 140: }
! 141:
! 142: void validate_teardown(void)
! 143: {
! 144: cfg_free(cfg);
! 145: }
! 146:
! 147: void validate_test(void)
! 148: {
! 149: char *buf;
! 150: int i;
! 151:
! 152: buf = "action = wlak";
! 153: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 154:
! 155: buf = "action = walk";
! 156: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 157:
! 158: buf = "action = run"
! 159: " options { speed = 6 }";
! 160: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 161:
! 162: buf = "action = jump"
! 163: " options { speed = 2 name = 'Joe' }";
! 164: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 165:
! 166: buf = "action = crawl"
! 167: " options { speed = -2 name = 'Smith' }";
! 168: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 169:
! 170: buf = "ip-address = { 0.0.0.0 , 1.2.3.4 , 192.168.0.254 , 10.0.0.255 , 20.30.40.256}";
! 171: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 172: buf = "ip-address = { 0.0.0.0 , 1.2.3.4 , 192.168.0.254 , 10.0.0.255 , 20.30.40.250}";
! 173: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 174: buf = "ip-address = { 1.2.3. }";
! 175: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 176:
! 177: buf = "action = run"
! 178: " multi_options { speeds = {1, 2, 3, 4, 5} }";
! 179: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 180: for(i = 0; i < cfg_size(cfg, "multi_options"); i++)
! 181: {
! 182: cfg_t *multisec = cfg_getnsec(cfg, "multi_options", i);
! 183: cfg_opt_t *speeds_opt = cfg_getopt(multisec, "speeds");
! 184: fail_unless(speeds_opt != 0);
! 185: fail_unless(speeds_opt->validcb == validate_speed);
! 186: }
! 187:
! 188: buf = "action = run"
! 189: " multi_options { speeds = {1, 2, 3, -4, 5} }";
! 190: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 191:
! 192: buf = "action = run"
! 193: " multi_options { speeds = {1, 2, 3, 4, 0} }";
! 194: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 195:
! 196: buf = "action = run"
! 197: " multi_options { options { xspeed = 3 } }";
! 198: fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
! 199:
! 200: buf = "action = run"
! 201: " multi_options { options { xspeed = -3 } }";
! 202: fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
! 203: }
! 204:
! 205: int
! 206: main(void)
! 207: {
! 208: validate_setup();
! 209: validate_test();
! 210: validate_teardown();
! 211:
! 212: return 0;
! 213: }
! 214:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>