mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
testServer is now testPsyc and it can handle file input as well
This commit is contained in:
parent
a1547d399b
commit
620a61514e
5 changed files with 70 additions and 40 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -9,7 +9,7 @@ src/match
|
|||
test/testMatch
|
||||
test/testParser
|
||||
test/testRender
|
||||
test/testServerPsyc
|
||||
test/testPsyc
|
||||
test/testText
|
||||
test/isRoutingVar
|
||||
test/getVarType
|
||||
|
|
|
@ -4,8 +4,8 @@ CFLAGS = -I../include -I../src -Wall -std=c99 ${OPT}
|
|||
LDFLAGS = -L../lib
|
||||
LOADLIBES = -lpsyc -lm
|
||||
LOADLIBES_NET = ${LOADLIBES}
|
||||
TARGETS = testServerPsyc testParser testMatch testRender testText isRoutingVar getVarType
|
||||
O = testServer.o
|
||||
TARGETS = testPsyc testParser testMatch testRender testText isRoutingVar getVarType
|
||||
O = test.o
|
||||
WRAPPER =
|
||||
DIET = diet
|
||||
PORT = 4440
|
||||
|
@ -20,8 +20,8 @@ endif
|
|||
all: ${TARGETS}
|
||||
it: all
|
||||
|
||||
testServerPsyc: LOADLIBES := ${LOADLIBES_NET}
|
||||
testServerPsyc: testServer.o
|
||||
testPsyc: LOADLIBES := ${LOADLIBES_NET}
|
||||
testPsyc: test.o
|
||||
|
||||
diet: WRAPPER = ${DIET}
|
||||
diet: all
|
||||
|
@ -39,8 +39,8 @@ test: ${TARGETS}
|
|||
./testText
|
||||
./isRoutingVar
|
||||
./getVarType
|
||||
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./testParser $$f; x=$$((x+$$?)); done; exit $$x
|
||||
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./testParser $$f -r; x=$$((x+$$?)); done; exit $$x
|
||||
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./testPsyc $$f -f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
|
||||
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./testPsyc $$f -fr | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
|
||||
|
||||
.NOTPARALLEL: nettestrun
|
||||
|
||||
|
@ -49,12 +49,12 @@ nettest: nettestfull nettestsplit
|
|||
nettestrun: srvstart pkt srvkill
|
||||
|
||||
nettestfull:
|
||||
${MAKE} nettestrun; x=$$?; pkill -x testServerPsyc; exit $$x
|
||||
${MAKE} nettestrun srv_args=r; x=$$?; pkill -x testServerPsyc; exit $$x
|
||||
${MAKE} nettestrun; x=$$?; pkill -x testPsyc; exit $$x
|
||||
${MAKE} nettestrun srv_args=r; x=$$?; pkill -x testPsyc; exit $$x
|
||||
|
||||
split_max = 10
|
||||
nettestsplit:
|
||||
x=0; for n in `seq 1 ${split_max}`; do ${MAKE} nettestrun srv_recvbuf=$$n && ${MAKE} nettestrun srv_args=r srv_recvbuf=$$n || break; done; x=$$?; pkill -x testServerPsyc; exit $$x
|
||||
x=0; for n in `seq 1 ${split_max}`; do ${MAKE} nettestrun srv_recvbuf=$$n && ${MAKE} nettestrun srv_args=r srv_recvbuf=$$n || break; done; x=$$?; pkill -x testPsyc; exit $$x
|
||||
|
||||
pkt:
|
||||
x=0; for f in packets/[0-9]*; do echo ">> $$f"; cat $$f | nc localhost ${PORT} | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
|
||||
|
@ -66,8 +66,8 @@ pkterr:
|
|||
for f in packets/err-*; do echo ">> $$f"; cat $$f | nc localhost ${PORT}; done
|
||||
|
||||
srvstart:
|
||||
pkill -x testServerPsyc; exit 0
|
||||
./testServerPsyc ${PORT} -${srv_args} ${srv_recvbuf} &
|
||||
pkill -x testPsyc; exit 0
|
||||
./testPsyc ${PORT} -${srv_args} ${srv_recvbuf} &
|
||||
|
||||
srvkill:
|
||||
pkill -x testServerPsyc
|
||||
pkill -x testPsyc
|
||||
|
|
|
@ -9,16 +9,19 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#define __USE_POSIX
|
||||
#include <netdb.h>
|
||||
|
||||
#include "testServer.h"
|
||||
#include "test.h"
|
||||
|
||||
// cmd line args
|
||||
extern uint8_t verbose, stats;
|
||||
|
@ -31,6 +34,22 @@ void *get_in_addr (struct sockaddr *sa) {
|
|||
return &(((struct sockaddr_in6*)sa)->sin6_addr);
|
||||
}
|
||||
|
||||
void test_file(const char* filename) {
|
||||
char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
|
||||
char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
|
||||
size_t nbytes;
|
||||
|
||||
int fd = open(filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
test_init(0);
|
||||
while ((nbytes = read(fd, (void*)recvbuf, RECV_BUF_SIZE)))
|
||||
test_input(0, recvbuf, nbytes);
|
||||
}
|
||||
|
||||
void test_server(const char* port, 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: ^^^^
|
||||
|
@ -150,10 +169,12 @@ void test_server(const char* port, size_t recv_buf_size) {
|
|||
printf("%ld ms\n", (end[i].tv_sec * 1000000 + end[i].tv_usec - start[i].tv_sec * 1000000 - start[i].tv_usec) / 1000);
|
||||
|
||||
// got error or connection closed by client
|
||||
if (nbytes == 0) // connection closed
|
||||
printf("# Socket %d hung up\n", i);
|
||||
else
|
||||
if (nbytes == 0) { // connection closed
|
||||
if (verbose)
|
||||
printf("# Socket %d hung up\n", i);
|
||||
} else {
|
||||
perror("recv");
|
||||
}
|
||||
|
||||
close(i); // bye!
|
||||
FD_CLR(i, &master); // remove from master set
|
|
@ -1,5 +1,5 @@
|
|||
#ifndef TESTSERVER_H
|
||||
# define TESTSERVER_H
|
||||
#ifndef TEST_H
|
||||
# define TEST_H
|
||||
|
||||
# define RECV_BUF_SIZE 8 * 1024
|
||||
# define CONT_BUF_SIZE 8 * 1024
|
||||
|
@ -8,6 +8,8 @@
|
|||
|
||||
void test_init(int i);
|
||||
int test_input(int i, char *recvbuf, size_t nbytes);
|
||||
|
||||
void test_file(const char* filename);
|
||||
void test_server(const char* port, size_t recv_buf_size);
|
||||
|
||||
#endif
|
|
@ -9,30 +9,22 @@
|
|||
#include <psyc/render.h>
|
||||
#include <psyc/syntax.h>
|
||||
|
||||
#include "testServer.h"
|
||||
//#include "testServer.c"
|
||||
#include "test.h"
|
||||
|
||||
// max size for routing & entity header
|
||||
#define ROUTING_LINES 16
|
||||
#define ENTITY_LINES 32
|
||||
|
||||
// cmd line args
|
||||
uint8_t verbose, stats;
|
||||
uint8_t verbose, stats, file;
|
||||
uint8_t routing_only, parse_multiple, no_render, progress;
|
||||
|
||||
psycParseState parsers[NUM_PARSERS];
|
||||
psycPacket packets[NUM_PARSERS];
|
||||
psycModifier routing[NUM_PARSERS][ROUTING_LINES];
|
||||
psycModifier entity[NUM_PARSERS][ENTITY_LINES];
|
||||
psycModifier *mod = NULL;
|
||||
|
||||
uint8_t routing_only, parse_multiple, no_render, progress;
|
||||
|
||||
int ret, retl, contbytes = 0;
|
||||
char oper;
|
||||
psycString name, value, elem;
|
||||
psycString *pname = NULL, *pvalue = NULL;
|
||||
psycParseListState listState;
|
||||
size_t len;
|
||||
int contbytes = 0, last_ret = 0;
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
char *port = argc > 1 ? argv[1] : "4440";
|
||||
|
@ -47,10 +39,15 @@ int main (int argc, char **argv) {
|
|||
parse_multiple = opts && memchr(opts, (int)'m', strlen(opts));
|
||||
no_render = opts && memchr(opts, (int)'n', strlen(opts));
|
||||
progress = opts && memchr(opts, (int)'p', strlen(opts));
|
||||
file = opts && memchr(opts, (int)'f', strlen(opts));
|
||||
size_t recv_buf_size = argc > 3 ? atoi(argv[3]) : 0;
|
||||
|
||||
test_server(port, recv_buf_size);
|
||||
return 0;
|
||||
if (file)
|
||||
test_file(argv[1]);
|
||||
else
|
||||
test_server(port, recv_buf_size);
|
||||
|
||||
return last_ret;
|
||||
}
|
||||
|
||||
static inline
|
||||
|
@ -77,10 +74,17 @@ void test_init (int i) {
|
|||
}
|
||||
|
||||
int test_input (int i, char *recvbuf, size_t nbytes) {
|
||||
int j;
|
||||
int j, ret, retl, r;
|
||||
char *parsebuf = recvbuf - contbytes;
|
||||
char sendbuf[SEND_BUF_SIZE];
|
||||
|
||||
char oper;
|
||||
psycString name, value, elem;
|
||||
psycString *pname = NULL, *pvalue = NULL;
|
||||
psycModifier *mod = NULL;
|
||||
psycParseListState listState;
|
||||
size_t len;
|
||||
|
||||
psyc_setParseBuffer2(&parsers[i], parsebuf, contbytes + nbytes);
|
||||
contbytes = 0;
|
||||
oper = 0;
|
||||
|
@ -88,7 +92,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
value.length = 0;
|
||||
|
||||
do {
|
||||
ret = psyc_parse(&parsers[i], &oper, &name, &value);
|
||||
ret = last_ret = psyc_parse(&parsers[i], &oper, &name, &value);
|
||||
if (verbose >= 2)
|
||||
printf("# ret = %d\n", ret);
|
||||
|
||||
|
@ -130,7 +134,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
if (verbose)
|
||||
printf("# Done parsing.\n");
|
||||
else if (progress)
|
||||
write(1, ".", 1);
|
||||
r = write(1, ".", 1);
|
||||
if (!parse_multiple) // parse multiple packets?
|
||||
ret = -1;
|
||||
|
||||
|
@ -146,12 +150,15 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
psyc_setPacketLength(&packets[i]);
|
||||
|
||||
if (psyc_render(&packets[i], sendbuf, SEND_BUF_SIZE) == PSYC_RENDER_SUCCESS) {
|
||||
if (send(i, sendbuf, packets[i].length, 0) == -1) {
|
||||
perror("send error");
|
||||
if (file && write(1, sendbuf, packets[i].length) == -1) {
|
||||
perror("write");
|
||||
ret = -1;
|
||||
} else if (!file && send(i, sendbuf, packets[i].length, 0) == -1) {
|
||||
perror("send");
|
||||
ret = -1;
|
||||
}
|
||||
} else {
|
||||
perror("render error");
|
||||
printf("# Render error");
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
|
@ -299,7 +306,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
while (ret > 0);
|
||||
|
||||
if (progress)
|
||||
write(1, " ", 1);
|
||||
r = write(1, " ", 1);
|
||||
|
||||
return ret;
|
||||
}
|
Loading…
Reference in a new issue