1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

test: opts, help

This commit is contained in:
Gabor Adam Toth 2011-05-15 21:12:17 +02:00
parent 31042f3fae
commit 8d86238305
3 changed files with 56 additions and 24 deletions

View file

@ -26,6 +26,18 @@
// cmd line args
extern uint8_t verbose, stats;
void check_range(char c, const char *s, int min, int max) {
int n = atoi(s);
if (n < min) {
printf("-%c: error, should be >= %d\n", c, min);
exit(-1);
}
if (max > min && n > max) {
printf("-%c: error, should be <= %d\n", c, max);
exit(-1);
}
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr (struct sockaddr *sa) {
if (sa->sa_family == AF_INET)
@ -34,10 +46,10 @@ void *get_in_addr (struct sockaddr *sa) {
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
void test_file(const char* filename, size_t recv_buf_size) {
void test_file(const char* filename, size_t count, size_t recv_buf_size) {
char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
size_t nbytes;
size_t i, nbytes;
struct timeval start, end;
int fd = open(filename, O_RDONLY);
@ -46,14 +58,14 @@ void test_file(const char* filename, size_t recv_buf_size) {
exit(1);
}
test_init(0);
if (stats)
gettimeofday(&start, NULL);
while ((nbytes = read(fd, (void*)recvbuf, recv_buf_size)))
test_input(0, recvbuf, nbytes);
for (i = 0; i < count; i++)
test_input(0, recvbuf, nbytes);
if (stats) {
gettimeofday(&end, NULL);
@ -61,7 +73,7 @@ void test_file(const char* filename, size_t recv_buf_size) {
}
}
void test_server(const char* port, size_t recv_buf_size) {
void test_server(const char* port, size_t count, size_t recv_buf_size) {
char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
@ -126,6 +138,8 @@ void test_server(const char* port, size_t recv_buf_size) {
exit(3);
}
printf("# Listening on TCP port %s\n", port);
// add the listener to the master set
FD_SET(listener, &master);

View file

@ -9,7 +9,8 @@
void test_init(int i);
int test_input(int i, char *recvbuf, size_t nbytes);
void test_file(const char* filename, size_t recv_buf_size);
void test_server(const char* port, size_t recv_buf_size);
void test_file(const char* filename, size_t count, size_t recv_buf_size);
void test_server(const char* port, size_t count, size_t recv_buf_size);
void check_range(char c, const char *s, int min, int max);
#endif

View file

@ -19,47 +19,64 @@
#define ENTITY_LINES 32
// cmd line args
uint8_t verbose, stats;
uint8_t routing_only, parse_multiple, no_render, quiet, progress;
char *filename, *port = "4440";
size_t recv_buf_size = RECV_BUF_SIZE;
uint8_t verbose, stats;
uint8_t multiple, single, routing_only, no_render, quiet, progress;
size_t count = 1, recv_buf_size = RECV_BUF_SIZE;
psycParseState parsers[NUM_PARSERS];
psycPacket packets[NUM_PARSERS];
psycModifier routing[NUM_PARSERS][ROUTING_LINES];
psycModifier entity[NUM_PARSERS][ENTITY_LINES];
int contbytes, last_ret;
int contbytes, exit_code;
int main (int argc, char **argv) {
int c;
while ((c = getopt (argc, argv, "f:p:b:mnqrsvP")) != -1) {
while ((c = getopt (argc, argv, "f:p:b:c:nmqrsvPSh")) != -1) {
switch (c) {
case 'f': filename = optarg; break;
case 'p': port = optarg;
if (atoi(optarg) <= 0) { printf("-%c: error, should be > 0\n", c); exit(-1); }
break;
case 'b': recv_buf_size = atoi(optarg);
if (atoi(optarg) <= 0) { printf("-%c: error, should be > 0\n", c); exit(-1); }
break;
case 'm': parse_multiple = 1; break;
case 'p': port = optarg; check_range(c, optarg, 1, 0); break;
case 'b': recv_buf_size = atoi(optarg); check_range(c, optarg, 1, RECV_BUF_SIZE); break;
case 'c': count = atoi(optarg); check_range(c, optarg, 1, 0); break;
case 'n': no_render = 1; break;
case 'm': multiple = 1; break;
case 'q': quiet = 1; break;
case 'r': routing_only = 1; break;
case 's': stats = 1; break;
case 'v': verbose++; break;
case 'P': progress = 1; break;
case 'S': single = 1; break;
case 'h':
printf(
"testPsyc -f <filename> [-b <read_buf_size>] [-c <count>] [-mnqrSsvP]\n"
"testPsyc [-p <port>] [-b <recv_buf_size>] [-nqrsvP]\n"
" -f <filename>\tInput file name\n"
" -p <port>\t\tListen on TCP port, default is %s\n"
" -b <buf_size>\tRead/receive buffer size, default is %d\n"
" -c <count>\t\tParse data from file <count> times\n"
" -m\t\t\tParse multiple packets from file\n"
" -n\t\t\tNo rendering, only parsing\n"
" -r\t\t\tParse routing header only\n"
" -q\t\t\tQuiet mode, don't output rendered string\n"
" -S\t\t\tSingle packet mode, close connection after parsing one packet\n"
" -s\t\t\tShow statistics at the end\n"
" -v\t\t\tVerbose, can be specified multiple times for more verbosity\n"
" -P\t\t\tShow progress\n"
" -h\t\t\tShow this help\n",
port, RECV_BUF_SIZE);
exit(0);
case '?': exit(-1);
default: abort();
}
}
if (filename)
test_file(filename, recv_buf_size);
test_file(filename, count, recv_buf_size);
else
test_server(port, recv_buf_size);
test_server(port, count, recv_buf_size);
return last_ret;
return exit_code;
}
static inline
@ -104,7 +121,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
value.length = 0;
do {
ret = last_ret = psyc_parse(&parsers[i], &oper, &name, &value);
ret = exit_code = psyc_parse(&parsers[i], &oper, &name, &value);
if (verbose >= 2)
printf("# ret = %d\n", ret);
@ -147,7 +164,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
printf("# Done parsing.\n");
else if (progress)
r = write(1, ".", 1);
if (!parse_multiple) // parse multiple packets?
if ((filename && !multiple) || (!filename && single))
ret = -1;
if (!no_render) {