Refactored unit tests

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2018-11-03 15:48:37 +01:00
parent 78dd67613b
commit 4a5e57816c
8 changed files with 51 additions and 89 deletions

View File

@ -19,12 +19,13 @@ DEFS = @defs@
INCDIR = -I. -I.. -I../.. -I../../src -I../src @INCDIR@ @sql_includes@ INCDIR = -I. -I.. -I../.. -I../../src -I../src @INCDIR@ @sql_includes@
LIBDIR = -L. @LIBDIR@ @LDFLAGS@ -L../src LIBDIR = -L. @LIBDIR@ @LDFLAGS@ -L../src
LIBS = @LIBS@ @sql_libs@ LIBS = @LIBS@ @sql_libs@
OBJS = common.o
RUNNING_USER = @RUNNING_USER@ RUNNING_USER = @RUNNING_USER@
RUNNING_GROUP = `@id_bin@ -gn $(RUNNING_USER)` RUNNING_GROUP = `@id_bin@ -gn $(RUNNING_USER)`
INSTALL = @INSTALL@ INSTALL = @INSTALL@
all: check_parser_utils check_rules check_digest check_mydomains check_parser check_decoder check_hash check_misc check_attachments smtp all: $(OBJS) check_parser_utils check_rules check_digest check_mydomains check_parser check_decoder check_hash check_misc check_attachments smtp
check_parser_utils: check_parser_utils.c ../src/libpiler.a check_parser_utils: check_parser_utils.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR)
@ -33,13 +34,13 @@ check_rules: check_rules.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR)
check_digest: check_digest.c ../src/libpiler.a check_digest: check_digest.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< $(OBJS) -lpiler $(LIBS) $(LIBDIR)
check_mydomains: check_mydomains.c ../src/libpiler.a check_mydomains: check_mydomains.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR)
check_parser: check_parser.c ../src/libpiler.a check_parser: check_parser.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< $(OBJS) -lpiler $(LIBS) $(LIBDIR)
check_decoder: check_decoder.c ../src/libpiler.a check_decoder: check_decoder.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR)
@ -56,11 +57,14 @@ check_attachments: check_attachments.c ../src/libpiler.a
smtp: smtp.c ../src/libpiler.a smtp: smtp.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR)
%.o: $(srcdir)/%.c
$(CC) $(CFLAGS) -fPIC $(INCDIR) $(DEFS) -c $< -o $@
install: install:
@echo @echo
clean: clean:
rm -f check_parser_utils check_rules check_digest check_mydomains check_parser check_decoder check_hash check_misc check_attachments smtp rm -f check_parser_utils check_rules check_digest check_mydomains check_parser check_decoder check_hash check_misc check_attachments smtp *.o
distclean: clean distclean: clean
rm -f Makefile rm -f Makefile

View File

@ -2,21 +2,7 @@
* check_rules.c, SJ * check_rules.c, SJ
*/ */
#include <stdio.h> #include "test.h"
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include <stdbool.h>
#include <assert.h>
#include "../src/piler.h"
struct digest_test {
char s[SMALLBUFSIZE];
char *digest1;
char *digest2;
};
struct digest_test tests[] = { struct digest_test tests[] = {
@ -70,7 +56,6 @@ static void test_make_digests(struct config *cfg){
struct session_data sdata; struct session_data sdata;
struct parser_state state; struct parser_state state;
struct data data; struct data data;
struct stat st;
if(open_database(&sdata, cfg) == ERR){ if(open_database(&sdata, cfg) == ERR){
@ -80,24 +65,10 @@ static void test_make_digests(struct config *cfg){
for(i=0; i<sizeof(tests)/sizeof(struct digest_test); i++){ for(i=0; i<sizeof(tests)/sizeof(struct digest_test); i++){
if(setup_and_parse_message(&sdata, &state, &data, tests[i].s, cfg) == 1){
if(stat(tests[i].s, &st) != 0){
fprintf(stderr, "%s is not found, skipping\n", tests[i].s);
continue; continue;
} }
init_session_data(&sdata, cfg);
sdata.delivered = 0;
sdata.tot_len = st.st_size;
snprintf(sdata.ttmpfile, SMALLBUFSIZE-1, "%s", tests[i].s);
snprintf(sdata.filename, SMALLBUFSIZE-1, "%s", tests[i].s);
snprintf(sdata.tmpframe, SMALLBUFSIZE-1, "%s.m", tests[i].s);
state = parse_message(&sdata, 1, &data, cfg);
post_parse(&sdata, &state, cfg);
make_digests(&sdata, cfg); make_digests(&sdata, cfg);
//printf("%s => body digest: %s, digest: %s\n", tests[i].s, sdata.bodydigest, sdata.digest); //printf("%s => body digest: %s, digest: %s\n", tests[i].s, sdata.bodydigest, sdata.digest);
@ -137,4 +108,3 @@ int main(){
return 0; return 0;
} }

View File

@ -2,13 +2,7 @@
* check_mydomains.c, SJ * check_mydomains.c, SJ
*/ */
#include <stdio.h> #include "test.h"
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include <stdbool.h>
#include <assert.h>
#include "../src/piler.h"
char *domains[] = { char *domains[] = {
@ -133,4 +127,3 @@ int main(){
return 0; return 0;
} }

View File

@ -21,7 +21,6 @@ struct parser_test {
static void test_parser(struct config *cfg){ static void test_parser(struct config *cfg){
unsigned int i; unsigned int i;
int j; int j;
struct stat st;
struct session_data sdata; struct session_data sdata;
struct parser_state state; struct parser_state state;
struct data data; struct data data;
@ -55,24 +54,10 @@ static void test_parser(struct config *cfg){
for(i=0; i<sizeof(tests)/sizeof(struct parser_test); i++){ for(i=0; i<sizeof(tests)/sizeof(struct parser_test); i++){
if(setup_and_parse_message(&sdata, &state, &data, tests[i].s, cfg) == 1){
if(stat(tests[i].s, &st) != 0){
fprintf(stderr, "%s is not found, skipping\n", tests[i].s);
continue; continue;
} }
init_session_data(&sdata, cfg);
sdata.delivered = 0;
sdata.tot_len = st.st_size;
snprintf(sdata.ttmpfile, SMALLBUFSIZE-1, "%s", tests[i].s);
snprintf(sdata.filename, SMALLBUFSIZE-1, "%s", tests[i].s);
snprintf(sdata.tmpframe, SMALLBUFSIZE-1, "%s.m", tests[i].s);
state = parse_message(&sdata, 1, &data, cfg);
post_parse(&sdata, &state, cfg);
for(j=1; j<=state.n_attachments; j++){ for(j=1; j<=state.n_attachments; j++){
unlink(state.attachments[j].internalname); unlink(state.attachments[j].internalname);
} }
@ -112,5 +97,3 @@ int main(){
return 0; return 0;
} }

View File

@ -2,14 +2,7 @@
* check_rules.c, SJ * check_rules.c, SJ
*/ */
#include <stdio.h> #include "test.h"
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include <stdbool.h>
#include <assert.h>
#include "../src/piler.h"
struct rule_test { struct rule_test {
@ -178,4 +171,3 @@ int main(){
return 0; return 0;
} }

36
unit_tests/common.c Normal file
View File

@ -0,0 +1,36 @@
/*
* common.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include <stdbool.h>
#include <assert.h>
#include "../src/piler.h"
int setup_and_parse_message(struct session_data *sdata, struct parser_state *state, struct data *data, char *filename, struct config *cfg){
struct stat st;
if(stat(filename, &st) != 0){
fprintf(stderr, "%s is not found, skipping\n", filename);
return 1;
}
init_session_data(sdata, cfg);
sdata->delivered = 0;
sdata->tot_len = st.st_size;
snprintf(sdata->ttmpfile, SMALLBUFSIZE-1, "%s", filename);
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename);
snprintf(sdata->tmpframe, SMALLBUFSIZE-1, "%s.m", filename);
*state = parse_message(sdata, 1, data, cfg);
post_parse(sdata, state, cfg);
return 0;
}

View File

@ -2,24 +2,7 @@
* smtp.c, SJ * smtp.c, SJ
*/ */
#include <stdio.h> #include "test.h"
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <locale.h>
#include <getopt.h>
#include <stdbool.h>
#include <assert.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "../src/piler.h"
extern char *optarg; extern char *optarg;

View File

@ -50,3 +50,4 @@ struct digest_test {
#define TEST_HEADER() printf("%s() ", __func__); #define TEST_HEADER() printf("%s() ", __func__);
#define TEST_FOOTER() printf(" OK\n"); #define TEST_FOOTER() printf(" OK\n");
int setup_and_parse_message(struct session_data *sdata, struct parser_state *state, struct data *data, char *filename, struct config *cfg);