2015-12-19 20:31:37 +01:00
|
|
|
/*
|
|
|
|
* check_rules.c, SJ
|
|
|
|
*/
|
|
|
|
|
2018-11-03 15:48:37 +01:00
|
|
|
#include "test.h"
|
2015-12-19 20:31:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct rule_test {
|
|
|
|
char filename[SMALLBUFSIZE];
|
|
|
|
char *expected_result;
|
|
|
|
int days;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct rule_query {
|
|
|
|
char query[SMALLBUFSIZE];
|
|
|
|
uint64 id;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct rule_query rules[] = {
|
|
|
|
{"insert into archiving_rule (`from`, `days`) values('hirlevel@jatekokbirodalma.hu', 101)", 0},
|
|
|
|
{"insert into archiving_rule (`to`, `days`) values('xxx@acts.hu', 102)", 0},
|
|
|
|
{"insert into archiving_rule (`to`, `subject`, `days`) values('sj@acts.hu', 'ultra slim', 103)", 0},
|
|
|
|
{"insert into archiving_rule (`body`, `days`) values('quality and price is very good', 104)", 0},
|
|
|
|
{"insert into archiving_rule (`attachment_type`, _attachment_size, attachment_size, `days`) values('image', '>', 2000000, 105)", 0},
|
|
|
|
{"insert into archiving_rule (`attachment_type`, _attachment_size, attachment_size, `days`) values('image', '>', 20000, 106)", 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
static void fill_rule_table(struct config *cfg){
|
2016-04-05 21:10:09 +02:00
|
|
|
unsigned int i;
|
2015-12-19 20:31:37 +01:00
|
|
|
struct session_data sdata;
|
|
|
|
|
|
|
|
if(open_database(&sdata, cfg) == ERR){
|
|
|
|
printf("cannot open database\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("adding testing rules...\n");
|
|
|
|
|
|
|
|
for(i=0; i<sizeof(rules)/sizeof(struct rule_query); i++){
|
2016-01-02 08:16:38 +01:00
|
|
|
p_query(&sdata, rules[i].query);
|
2020-12-12 21:43:16 +01:00
|
|
|
rules[i].id = mysql_insert_id(&(sdata.mysql));
|
2015-12-19 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
close_database(&sdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
static void restore_rule_table(struct config *cfg){
|
2016-04-05 21:10:09 +02:00
|
|
|
unsigned int i;
|
2015-12-19 20:31:37 +01:00
|
|
|
char buf[SMALLBUFSIZE];
|
|
|
|
struct session_data sdata;
|
|
|
|
|
|
|
|
if(open_database(&sdata, cfg) == ERR){
|
|
|
|
printf("cannot open database\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("removing testing rules...\n");
|
|
|
|
|
|
|
|
for(i=0; i<sizeof(rules)/sizeof(struct rule_query); i++){
|
|
|
|
snprintf(buf, sizeof(buf)-1, "delete from archiving_rule where id=%llu", rules[i].id);
|
2016-01-02 08:16:38 +01:00
|
|
|
p_query(&sdata, buf);
|
2015-12-19 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
close_database(&sdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
static void test_archiving_rule(struct config *cfg){
|
2016-04-05 21:10:09 +02:00
|
|
|
unsigned int i;
|
|
|
|
int j;
|
2015-12-19 20:31:37 +01:00
|
|
|
char *rule;
|
|
|
|
struct session_data sdata;
|
|
|
|
struct parser_state state;
|
2017-08-08 15:34:45 +02:00
|
|
|
struct data data;
|
2015-12-19 20:31:37 +01:00
|
|
|
struct stat st;
|
|
|
|
struct rule_test rule_test[] = {
|
|
|
|
{"1.eml", "domain=,from=hirlevel@jatekokbirodalma.hu,to=,subject=,body=,size0,att.name=,att.type=,att.size0,spam=-1", 0},
|
|
|
|
{"2.eml", "domain=,from=,to=sj@acts.hu,subject=ultra slim,body=,size0,att.name=,att.type=,att.size0,spam=-1", 0},
|
|
|
|
{"3.eml", NULL, 0},
|
|
|
|
{"4.eml", "domain=,from=,to=,subject=,body=quality and price is very good,size0,att.name=,att.type=,att.size0,spam=-1", 0},
|
|
|
|
{"5.eml", "domain=,from=,to=,subject=,body=,size0,att.name=,att.type=image,att.size>20000,spam=-1", 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if(open_database(&sdata, cfg) == ERR){
|
|
|
|
printf("cannot open database\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
inithash(data.mydomains);
|
|
|
|
load_mydomains(&sdata, &data, cfg);
|
|
|
|
|
|
|
|
initrules(data.archiving_rules);
|
|
|
|
|
2018-09-29 08:54:22 +02:00
|
|
|
load_rules(&sdata, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE);
|
2015-12-19 20:31:37 +01:00
|
|
|
|
|
|
|
for(i=0; i<sizeof(rule_test)/sizeof(struct rule_test); i++){
|
|
|
|
|
|
|
|
if(stat(rule_test[i].filename, &st) != 0){
|
|
|
|
fprintf(stderr, "%s is not found, skipping\n", rule_test[i].filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_session_data(&sdata, cfg);
|
2020-12-12 21:43:16 +01:00
|
|
|
|
2015-12-19 20:31:37 +01:00
|
|
|
sdata.delivered = 0;
|
|
|
|
sdata.tot_len = st.st_size;
|
|
|
|
|
|
|
|
snprintf(sdata.ttmpfile, SMALLBUFSIZE-1, "%s", rule_test[i].filename);
|
|
|
|
snprintf(sdata.filename, SMALLBUFSIZE-1, "%s", rule_test[i].filename);
|
|
|
|
snprintf(sdata.tmpframe, SMALLBUFSIZE-1, "%s.m", rule_test[i].filename);
|
|
|
|
|
|
|
|
state = parse_message(&sdata, 1, &data, cfg);
|
2020-12-13 08:36:12 +01:00
|
|
|
post_parse(&sdata, &state, cfg);
|
2015-12-19 20:31:37 +01:00
|
|
|
|
2019-01-13 16:22:27 +01:00
|
|
|
rule = check_against_ruleset(data.archiving_rules, &state, st.st_size, sdata.spam_message);
|
2015-12-19 20:31:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
for(j=1; j<=state.n_attachments; j++){
|
|
|
|
unlink(state.attachments[j].internalname);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink(sdata.tmpframe);
|
|
|
|
|
2015-12-28 14:50:37 +01:00
|
|
|
//printf("%s, '%s'\n", rule_test[i].filename, rule);
|
2015-12-19 20:31:37 +01:00
|
|
|
|
2015-12-29 15:16:31 +01:00
|
|
|
if(rule){
|
|
|
|
assert(strcmp(rule, rule_test[i].expected_result) == 0 && "test_archiving_rule()");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(rule_test[i].expected_result == NULL && "test_archiving_rule()");
|
|
|
|
}
|
2015-12-19 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
clearrules(data.archiving_rules);
|
|
|
|
|
|
|
|
clearhash(data.mydomains);
|
|
|
|
|
|
|
|
close_database(&sdata);
|
|
|
|
|
|
|
|
printf("test_archiving_rule() OK\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(){
|
2017-08-08 15:34:45 +02:00
|
|
|
struct config cfg;
|
2015-12-19 20:31:37 +01:00
|
|
|
|
2017-05-14 17:21:46 +02:00
|
|
|
if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!");
|
2015-12-19 20:31:37 +01:00
|
|
|
|
|
|
|
(void) openlog("rule_test", LOG_PID, LOG_MAIL);
|
|
|
|
|
|
|
|
cfg = read_config("test.conf");
|
|
|
|
|
|
|
|
setlocale(LC_MESSAGES, cfg.locale);
|
|
|
|
setlocale(LC_CTYPE, cfg.locale);
|
|
|
|
|
|
|
|
fill_rule_table(&cfg);
|
|
|
|
|
|
|
|
|
|
|
|
test_archiving_rule(&cfg);
|
|
|
|
|
|
|
|
restore_rule_table(&cfg);
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|