From: Baptiste Daroussin Date: Tue, 4 Jan 2022 16:13:58 +0000 (+0100) Subject: tests: Add tests about check_smtpreply X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ea4a4e244868dfb73bc8258d7fa81e949d8513e;p=thirdparty%2Fmlmmj.git tests: Add tests about check_smtpreply --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 3b52f97b..0f00f3a9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -7,7 +7,7 @@ mlmmj_SOURCES = mlmmj.c $(top_srcdir)/src/prepstdreply.c $(top_srcdir)/include/p $(top_srcdir)/src/strgen.c $(top_srcdir)/src/random-int.c $(top_srcdir)/src/readn.c \ $(top_srcdir)/src/log_error.c $(top_srcdir)/src/chomp.c $(top_srcdir)/src/mygetline.c $(top_srcdir)/src/unistr.c \ $(top_srcdir)/src/mlmmj.c $(top_srcdir)/src/mail-functions.c \ - $(top_srcdir)/src/subscriberfuncs.c + $(top_srcdir)/src/subscriberfuncs.c $(top_srcdir)/src/init_sockfd.c $(top_srcdir)/src/checkwait_smtpreply.c mlmmj_CFLAGS = @ATF_CFLAGS@ -g -Wall -pedantic -Wsign-compare -DDEFAULTTEXTDIR='"@textlibdir@"' -I$(top_srcdir)/include -DSRCDIR='"@abs_top_srcdir@"' mlmmj_LDADD = @ATF_LIBS@ diff --git a/tests/mlmmj.c b/tests/mlmmj.c index 8224a8d1..d01743ce 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -20,6 +20,10 @@ * IN THE SOFTWARE. */ +#include +#include +#include + #include #include #include @@ -42,6 +46,8 @@ #include "mail-functions.h" #include "mygetline.h" #include "subscriberfuncs.h" +#include "init_sockfd.h" +#include "checkwait_smtpreply.h" ATF_TC(random_int); ATF_TC(statctrl); @@ -66,6 +72,8 @@ ATF_TC(unsubscribe); ATF_TC(lowercase); ATF_TC(subbed); ATF_TC(controls); +ATF_TC(init_sock); +ATF_TC(smtp); ATF_TC_HEAD(random_int, tc) { } ATF_TC_HEAD(statctrl, tc) { } @@ -90,6 +98,8 @@ ATF_TC_HEAD(unsubscribe, tc) {} ATF_TC_HEAD(lowercase, tc) {} ATF_TC_HEAD(subbed, tc) {} ATF_TC_HEAD(controls, tc) {} +ATF_TC_HEAD(init_sock, tc) {} +ATF_TC_HEAD(smtp, tc) {} #ifndef NELEM #define NELEM(array) (sizeof(array) / sizeof((array)[0])) @@ -623,6 +633,131 @@ ATF_TC_BODY(controls, tc) close(fd); } +ATF_TC_BODY(init_sock, tc) +{ + pid_t p = atf_utils_fork(); + if (p == 0) { + int s, c; + struct sockaddr_in me, cl; + socklen_t clsize; + s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) + exit(1); + if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &(int) { 1 }, sizeof(int)) != 0) + exit(2); + memset(&me, 0, sizeof(me)); + me.sin_family = AF_INET; + me.sin_addr.s_addr = inet_addr("127.0.0.1"); + /* specific interface */ + me.sin_port = htons(25678); + if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) + exit(3); + if (listen(s, 1) == -1) + exit(4); + + /* Now we can accept incoming connections one + * at a time using accept(2). */ + + c = accept(s, (struct sockaddr *) &cl, + &clsize); + if (c == -1) + exit(5); + exit(0); + } + sleep(1); + int sock; + do { + init_sockfd(&sock, "127.0.0.1", 25678); + if (sock == -1 && errno != EPERM) + break; + if (sock != -1) + break; + } while (1); + if (sock == -1) + atf_tc_fail(strerror(errno)); + atf_utils_wait(p, 0, "", ""); +} + +ATF_TC_BODY(smtp, tc) +{ + int smtppipe[2]; + char *reply; + ATF_REQUIRE(pipe(smtppipe) >= 0); + pid_t p = atf_utils_fork(); + if (p == 0) { + dprintf(smtppipe[0], "220 me fake smtp\n"); + /* EHLO */ + reply = mygetline(smtppipe[0]); + dprintf(smtppipe[0], + "250-hostname.net\n" + "250-PIPELINING\n" + "250-SIZE 20480000\n" + "250-ETRN\n" + "250-STARTTLS\n" + "250-ENHANCEDSTATUSCODES\n" + "250-8BITMIME\n" + "250-DSN\n" + "250-SMTPUTF8\n" + "250 CHUNKING\n"); + /* HELO */ + reply = mygetline(smtppipe[0]); + dprintf(smtppipe[0], + "250-hostname.net\n"); + /* MAIL FROM */ + reply = mygetline(smtppipe[0]); + dprintf(smtppipe[0], + "250 2.1.0 Ok\n"); + /* RCPT TO */ + reply = mygetline(smtppipe[0]); + dprintf(smtppipe[0], + "250 2.1.0 Ok\n"); + /* DATA */ + reply = mygetline(smtppipe[0]); + dprintf(smtppipe[0], + "354 Send message content; end with .\n"); + /* DATA */ + reply = mygetline(smtppipe[0]); + dprintf(smtppipe[0], + "250 2.1.0 Ok\n"); + /* QUIT */ + reply = mygetline(smtppipe[0]); + dprintf(smtppipe[0], + "221 2.0.0 Bye\n"); + /* RSET */ + reply = mygetline(smtppipe[0]); + dprintf(smtppipe[0], + "250 2.0.0 Ok\n"); + exit (0); + } + close(smtppipe[0]); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_CONNECT); + ATF_REQUIRE_EQ(reply, NULL); + write_ehlo(smtppipe[1], "plop"); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_EHLO); + ATF_REQUIRE_EQ(reply, NULL); + write_helo(smtppipe[1], "plop"); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_HELO); + ATF_REQUIRE_EQ(reply, NULL); + write_mail_from(smtppipe[1], "plop", NULL); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_FROM); + ATF_REQUIRE_EQ(reply, NULL); + write_rcpt_to(smtppipe[1], "plop"); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_RCPTTO); + ATF_REQUIRE_EQ(reply, NULL); + write_data(smtppipe[1]); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_DATA); + ATF_REQUIRE_EQ(reply, NULL); + write_dot(smtppipe[1]); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_DOT); + ATF_REQUIRE_EQ(reply, NULL); + write_quit(smtppipe[1]); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_QUIT); + ATF_REQUIRE_EQ(reply, NULL); + write_rset(smtppipe[1]); + reply = checkwait_smtpreply(smtppipe[1], MLMMJ_RSET); + ATF_REQUIRE_EQ(reply, NULL); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, random_int); @@ -648,6 +783,8 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, lowercase); ATF_TP_ADD_TC(tp, subbed); ATF_TP_ADD_TC(tp, controls); + ATF_TP_ADD_TC(tp, init_sock); + ATF_TP_ADD_TC(tp, smtp); return (atf_no_error()); }