added unit test for rules

This commit is contained in:
SJ 2015-12-19 20:31:37 +01:00
parent c691a4e37e
commit 36332b9af4
8 changed files with 17044 additions and 4 deletions

8358
unit_tests/1.eml Normal file

File diff suppressed because it is too large Load Diff

37
unit_tests/2.eml Normal file
View File

@ -0,0 +1,37 @@
Return-Path: <hwtwi@mcqw.com>
X-Original-To: sj@acts.hu
X-Envelope-To: sj@acts.hu
Received: from mcqw.com (unknown [111.177.117.51])
by lithium.acts.hu (Postfix) with ESMTP id 12AC620EBC9
for <sj@acts.hu>; Sun, 1 Nov 2015 15:34:10 +0100 (CET)
Received: from cloudvm-1e7a8fc ([127.0.0.1]) by localhost via TCP with ESMTPA; Sun, 01 Nov 2015 22:26:34 +0800
MIME-Version: 1.0
From: "JML lighting" <huixinsoft67@foxmail.com>
Sender: "JML lighting" <hwtwi@mcqw.com>
To: sj@acts.hu
Reply-To: "JML lighting" <huixinsoft67@foxmail.com>
Date: 1 Nov 2015 22:26:34 +0800
Subject: =?utf-8?B?TmV3IGRlc2lnbiB1bHRyYSBzbGltIGxlZCAgcGFuZWwgbGlnaHQ=?=
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
Message-Id: <20151101142653.111156815AF6D@acts.hu>
PGh0bWw+PGJvZHk+PFA+RGVhciBTaXIuPC9QPg0KPFA+SGF2ZSBhIGdvb2QgZGF5ICE8L1A+
DQo8UD5XZSBhcmUgYSBUVVYgYW5kIFNHUyBjZXJ0aWZpZWQgbGVkIGxpZ2h0aW5nIG1hbnVm
YWN0dXJlci4mbmJzcDsgPC9QPg0KPFA+R2xhZCB0byBzaGFyZSBvdXIgdWx0cmEgc2xpbSAm
YW1wOyBzY3Jld2xlc3MgbGVkIHBhbmVsIGxpZ2h0IGFzIGJlbG93PC9QPg0KPFA+Ny41bW0g
dWx0cmEgc2xpbSwgbm8gc2NyZXdzIGV4cG9zZWQ8QlI+MTAwbG0vdywgUmEmZ3Q7ODAsPEJS
PjEwMC0yNDBWQUMsIDMgeWVhcnMgd2FycmFudHk8L1A+DQo8UD5vdmVyIDEwLDAwMHBjcy9t
b250aCBwcm9kdWN0aW9uIGNhcGFjaXR5PEJSPlByb2Zlc3Npb25hbCBsZWQgcGFuZWwgbGln
aHQgc3VwcGxpZXIgZm9yIG92ZXIgNSB5ZWFycy48L1A+DQo8UD5RdWFsaXR5IGZpcnN0LCBk
ZWxpdmVyeSBpbiB0aW1lLHByb2Zlc3Npb25hbCBzZXJ2aWNlLCBxdWljayByZXNwb25zZS48
L1A+DQo8UD48QlI+QmVzdCBSZWdhcmRzPC9QPg0KPFA+Sk1MIGxpZ2h0aW5nIENvLiwgTHRk
LjwvUD4NCjxQPjQvRiwgRmVuZ2xpbiBCdWlsZGluZ++8jEppblhpbmcgUm9hZCA1NTEjLCBG
dXNoYW4gSW5kdXN0cmlhbCBQYXJrICxMaWFvQnUsIERvbmdHdWFuLCBHdWFuZ0RvbmcsIENo
aW5hLiA8QlI+UG9zdCBDb2RlIDogNTIzNDAxPC9QPg0KPFA+VGVsLjorODYtMDc2OS04OTEy
OTc5OTxCUj5GYXg6Kzg2LTA3NjktODkxMjk3NTU8L1A+DQo8UD4mbmJzcDs8L1A+PC9ib2R5
PjwvaHRtbD4=

40
unit_tests/3.eml Normal file
View File

@ -0,0 +1,40 @@
Return-Path: <dqyzyjy@dsmart.com.tr>
X-Original-To: sj@acts.hu
X-Envelope-To: sj@acts.hu
Received: from dsmart.com.tr (unknown [94.120.132.51])
by lithium.acts.hu (Postfix) with ESMTP id A90CC20EBC9
for <sj@acts.hu>; Sun, 1 Nov 2015 19:10:09 +0100 (CET)
From: Christabel Q. <dqyzyjy@dsmart.com.tr>
To: <sj@acts.hu>
Subject: You'll never sleep alone!
Date: Sun, 1 Nov 2015 20:02:50 +0200
MIME-Version: 1.0
Content-Type: text/html; charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable
Message-Id: <20151101180250.C328C6815AF6D@acts.hu>
<p style=3D"color:#5F979A; =
font-size:21px">&#66;&#782;o&#110;j&#111;&#858;ur =
l&#868;o&#118;&#1467;&#101;&#108;&#121; &#112;&#864;e&#1613;ck&#101;r =
:-}<br/>
i &#100;on&#39;&#116; &#119;&#97;n&#116; a =
&#114;&#101;&#788;l&#97;&#1613;ti&#111;ns&#104;&#105;p&#46; =
&#106;&#117;&#866;&#115;&#116; wa&#110;&#116; &#97; ma&#110; =
&#116;&#111; f$&#99;&#107; m&#101;&#46;&#46; i&#39;&#109; =
&#51;&#48;&#47;&#102; w&#105;th a &#114;&#111;&#1616;u&#110;d =
&#98;&#48;0&#1440;t&#121; &#97;&#794;&#110;d &#115;of&#116; =
&#116;&#64;ts &#46; tu&#114;&#110; &#117; o&#110;? :-S<br/>
&#77;y u&#878;&#115;&#101;&#114;&#1465;&#110;a&#109;&#818;&#101; =
&#105;&#115; =
<b>C&#1612;&#104;&#1628;&#114;&#105;&#787;&#115;&#116;&#877;&#97;b&#852;&=
#101;&#1452;&#108;</b><br/>
&#77;&#1621;y p&#114;&#1624;&#111;&#1620;fi&#108;&#101; is =
&#104;er&#101;&#877;: <a =
href=3D"&#104;t&#116;&#112;://C&#104;r&#105;&#115;&#116;&#97;be&#108;&#46=
;Ins&#116;&#97;&#68;a&#116;&#101;R&#101;&#113;u&#101;&#115;&#116;.&#114;u=
">&#104;tt&#112;:/&#47;C&#104;&#114;&#105;&#115;&#116;a&#98;&#101;&#108;.=
I&#110;st&#97;Date&#82;e&#113;uest.ru</a><br/>
T&#867;&#65;L&#1616;&#75; S0&#48;N&#33;</p>

67
unit_tests/4.eml Normal file
View File

@ -0,0 +1,67 @@
Return-Path: <dglehwb@njhq.com>
X-Original-To: sj@acts.hu
X-Envelope-To: sj@acts.hu
Received: from njhq.com (unknown [180.97.194.14])
by lithium.acts.hu (Postfix) with ESMTP id B0EFB20EBC9
for <sj@acts.hu>; Mon, 2 Nov 2015 13:48:44 +0100 (CET)
Received: from cho2015073100343 ([127.0.0.1]) by localhost via TCP with ESMTPA; Mon, 02 Nov 2015 20:40:42 +0800
MIME-Version: 1.0
From: "QH Lighting" <sdf32dfvs@163.com>
Sender: "QH Lighting" <dglehwb@njhq.com>
To: sj@acts.hu
Reply-To: "QH Lighting" <sdf32dfvs@163.com>
Date: 2 Nov 2015 20:40:42 +0800
Subject: =?utf-8?B?UkU6IDYuNVcgMzYwwrA3MjAgTEVEIEJ1bGI=?=
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
Message-Id: <20151102124131.F19436822FDA1@acts.hu>
PGh0bWw+PGJvZHk+PFAgc3R5bGU9IlRFWFQtVFJBTlNGT1JNOiBub25lOyBCQUNLR1JPVU5E
LUNPTE9SOiByZ2IoMjU1LDI1NSwyNTUpOyBURVhULUlOREVOVDogMHB4OyBGT05UOiAxMnB0
ICdUaW1lcyBOZXcgUm9tYW4nLCBzZXJpZjsgV0hJVEUtU1BBQ0U6IG5vcm1hbDsgTEVUVEVS
LVNQQUNJTkc6IG5vcm1hbDsgQ09MT1I6IHJnYigwLDAsMCk7IE1BUkdJTi1MRUZUOiAwY207
IE1BUkdJTi1SSUdIVDogMGNtOyBXT1JELVNQQUNJTkc6IDBweDsgLXdlYmtpdC10ZXh0LXN0
cm9rZS13aWR0aDogMHB4Ij5IZWxsbyw8P3htbDpuYW1lc3BhY2UgcHJlZml4ID0gbyAvPjxv
OnA+PC9vOnA+PC9QPg0KPFAgc3R5bGU9IlRFWFQtVFJBTlNGT1JNOiBub25lOyBCQUNLR1JP
VU5ELUNPTE9SOiByZ2IoMjU1LDI1NSwyNTUpOyBURVhULUlOREVOVDogMHB4OyBGT05UOiAx
MnB0ICdUaW1lcyBOZXcgUm9tYW4nLCBzZXJpZjsgV0hJVEUtU1BBQ0U6IG5vcm1hbDsgTEVU
VEVSLVNQQUNJTkc6IG5vcm1hbDsgQ09MT1I6IHJnYigwLDAsMCk7IE1BUkdJTi1MRUZUOiAw
Y207IE1BUkdJTi1SSUdIVDogMGNtOyBXT1JELVNQQUNJTkc6IDBweDsgLXdlYmtpdC10ZXh0
LXN0c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1RpbWVzIE5ldyBSb21hbicsIHNlcmlmOyBXSElURS1TUEFDRTogbm9y
bWFsOyBMRVRURVItU1BBQ0lORzogbm9ybWFsOyBDT0xPUjogcmdiKDAsMCwwKTsgTUFSR0lO
LUxFRlQ6IDBjbTsgTUFSR0lOLVJJR0hUOiAwY207IFdPUkQtU1BBQ0lORzogMHB4OyAtd2Vi
a2l0LXRleHQtc3Ryb2tlLXdpZHRoOiAwcHgiPlFIIGxpZ2h0aW5nIExURDwvUD48L2JvZHk+
PC9odG1sPg==

8358
unit_tests/5.eml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -24,11 +24,14 @@ RUNNING_GROUP = `@id_bin@ -gn $(RUNNING_USER)`
INSTALL = @INSTALL@
all: check_parser_utils smtp
all: check_parser_utils check_rules smtp
check_parser_utils: check_parser_utils.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR)
check_rules: check_rules.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR)
smtp: smtp.c ../src/libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR)
@ -36,7 +39,7 @@ install:
@echo
clean:
rm -f check_parser_utils smtp
rm -f check_parser_utils check_rules smtp
distclean: clean
rm -f Makefile

177
unit_tests/check_rules.c Normal file
View File

@ -0,0 +1,177 @@
/*
* check_rules.c, SJ
*/
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include <stdbool.h>
#include <assert.h>
#include "../src/piler.h"
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}
};
static void fill_rule_table(struct __config *cfg){
int i;
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++){
p_query(&sdata, rules[i].query, cfg);
rules[i].id = mysql_insert_id(&(sdata.mysql));
}
close_database(&sdata);
}
static void restore_rule_table(struct __config *cfg){
int i;
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);
p_query(&sdata, buf, cfg);
}
close_database(&sdata);
}
static void test_archiving_rule(struct __config *cfg){
int i, j;
char *rule;
struct session_data sdata;
struct parser_state state;
struct __data data;
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);
load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE, cfg);
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);
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);
post_parse(&sdata, &state, cfg);
rule = check_againt_ruleset(data.archiving_rules, &state, st.st_size, sdata.spam_message);
for(j=1; j<=state.n_attachments; j++){
unlink(state.attachments[j].internalname);
}
unlink(sdata.tmpframe);
printf("%s, '%s'\n", rule_test[i].filename, rule);
if(rule == NULL && rule == rule_test[i].expected_result) continue;
assert(strcmp(rule, rule_test[i].expected_result) == 0 && "test_archiving_rule()");
}
clearrules(data.archiving_rules);
clearhash(data.mydomains);
close_database(&sdata);
printf("test_archiving_rule() OK\n");
}
int main(){
struct __config cfg;
if(!can_i_write_current_directory()) __fatal("cannot write current directory!");
(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;
}

View File

@ -22,8 +22,8 @@ min_word_len=1
mmap_dedup_test=0
mysql_connect_timeout=2
mysqldb=piler
mysqlpwd=verystrongpassword
mysqlsocket=/var/run/mysqld/mysqld.sock
mysqlpwd=piler123
mysqlsocket=/var/run/mysql/mysql.sock
mysqluser=piler
number_of_worker_processes=10
pemfile=/usr/local/etc/piler.pem