Skip to content

Commit

Permalink
lib-smtp: test-smtp-payload: Add SSL tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanbosch committed May 25, 2018
1 parent 92ee269 commit 0c746fb
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/lib-smtp/Makefile.am
Expand Up @@ -111,6 +111,11 @@ test_deps = \
../lib-test/libtest.la \
../lib/liblib.la

test_libs_ssl=
if BUILD_OPENSSL
test_libs_ssl += ../lib-ssl-iostream/libssl_iostream_openssl.la
endif

test_smtp_address_SOURCES = test-smtp-address.c
test_smtp_address_LDFLAGS = -export-dynamic
test_smtp_address_LDADD = $(test_libs)
Expand All @@ -133,7 +138,7 @@ test_smtp_command_parser_DEPENDENCIES = $(test_deps)

test_smtp_payload_SOURCES = test-smtp-payload.c
test_smtp_payload_LDFLAGS = -export-dynamic
test_smtp_payload_LDADD = $(test_libs)
test_smtp_payload_LDADD = $(test_libs) $(test_libs_ssl)
test_smtp_payload_DEPENDENCIES = $(test_deps)

test_smtp_submit_SOURCES = test-smtp-submit.c
Expand Down
88 changes: 84 additions & 4 deletions src/lib-smtp/test-smtp-payload.c
Expand Up @@ -12,6 +12,11 @@
#include "istream-base64.h"
#include "istream-crlf.h"
#include "iostream-temp.h"
#include "iostream-ssl.h"
#include "iostream-ssl-test.h"
#ifdef HAVE_OPENSSL
#include "iostream-openssl.h"
#endif
#include "connection.h"
#include "test-common.h"
#include "smtp-server.h"
Expand All @@ -31,8 +36,15 @@

static bool debug = FALSE;

enum test_ssl_mode {
TEST_SSL_MODE_NONE = 0,
TEST_SSL_MODE_IMMEDIATE,
TEST_SSL_MODE_STARTTLS
};

static unsigned int test_max_pending = 1;
static bool test_unknown_size = FALSE;
static enum test_ssl_mode test_ssl_mode = TEST_SSL_MODE_NONE;

static struct ip_addr bind_ip;
static in_port_t bind_port = 0;
Expand Down Expand Up @@ -390,7 +402,8 @@ static void client_init(int fd)
client->pool = pool;

client->smtp_conn = smtp_server_connection_create(smtp_server,
fd, fd, NULL, 0, FALSE, NULL, &server_callbacks, client);
fd, fd, NULL, 0, (test_ssl_mode == TEST_SSL_MODE_IMMEDIATE),
NULL, &server_callbacks, client);
smtp_server_connection_start(client->smtp_conn);
DLLIST_PREPEND(&clients, client);
}
Expand Down Expand Up @@ -658,6 +671,7 @@ static void test_client_continue(void *dummy ATTR_UNUSED)
struct istream *fstream, *payload;
const char *path = paths[client_files_last];
unsigned int r, rcpts;
enum smtp_client_connection_ssl_mode ssl_mode;

fstream = test_file_open(path);
if (fstream == NULL) {
Expand All @@ -681,9 +695,22 @@ static void test_client_continue(void *dummy ATTR_UNUSED)
tctrans = test_client_transaction_new();
tctrans->files_idx = client_files_last;

switch (test_ssl_mode) {
case TEST_SSL_MODE_NONE:
default:
ssl_mode = SMTP_CLIENT_SSL_MODE_NONE;
break;
case TEST_SSL_MODE_IMMEDIATE:
ssl_mode = SMTP_CLIENT_SSL_MODE_IMMEDIATE;
break;
case TEST_SSL_MODE_STARTTLS:
ssl_mode = SMTP_CLIENT_SSL_MODE_STARTTLS;
break;
}

tctrans->conn = smtp_client_connection_create(smtp_client,
client_protocol, net_ip2addr(&bind_ip), bind_port,
SMTP_CLIENT_SSL_MODE_NONE, NULL);
ssl_mode, NULL);

i_zero(&mail_params);
mail_params.envid = path;
Expand Down Expand Up @@ -778,13 +805,16 @@ static void test_server_kill(void)

static void test_run_client_server(
enum smtp_protocol protocol,
const struct smtp_client_settings *client_set,
const struct smtp_server_settings *server_set,
struct smtp_client_settings *client_set,
struct smtp_server_settings *server_set,
void (*client_init)(enum smtp_protocol protocol,
const struct smtp_client_settings *client_set))
{
struct ioloop *ioloop;

if (test_ssl_mode == TEST_SSL_MODE_STARTTLS)
server_set->capabilities |= SMTP_CAPABILITY_STARTTLS;

test_open_server_fd();

if ((server_pid = fork()) == (pid_t)-1)
Expand Down Expand Up @@ -825,6 +855,13 @@ static void test_run_scenarios(enum smtp_protocol protocol,
{
struct smtp_server_settings smtp_server_set;
struct smtp_client_settings smtp_client_set;
struct ssl_iostream_settings ssl_server_set, ssl_client_set;

/* ssl settings */
ssl_iostream_test_settings_server(&ssl_server_set);
ssl_server_set.verbose = debug;
ssl_iostream_test_settings_client(&ssl_client_set);
ssl_client_set.verbose = debug;

/* server settings */
i_zero(&smtp_server_set);
Expand All @@ -834,16 +871,19 @@ static void test_run_scenarios(enum smtp_protocol protocol,
smtp_server_set.max_client_idle_time_msecs = 10*000;
smtp_server_set.max_pipelined_commands = 1;
smtp_server_set.auth_optional = TRUE;
smtp_server_set.ssl = &ssl_server_set;
smtp_server_set.debug = debug;

/* client settings */
i_zero(&smtp_client_set);
smtp_client_set.my_hostname = "localhost";
smtp_client_set.temp_path_prefix = "/tmp";
smtp_client_set.ssl = &ssl_client_set;
smtp_client_set.debug = debug;

test_max_pending = 1;
test_unknown_size = FALSE;
test_ssl_mode = TEST_SSL_MODE_NONE;
test_files_init();
test_run_client_server(protocol,
&smtp_client_set, &smtp_server_set,
Expand All @@ -854,6 +894,7 @@ static void test_run_scenarios(enum smtp_protocol protocol,

test_max_pending = MAX_PARALLEL_PENDING;
test_unknown_size = FALSE;
test_ssl_mode = TEST_SSL_MODE_NONE;
test_files_init();
test_run_client_server(protocol,
&smtp_client_set, &smtp_server_set,
Expand All @@ -866,6 +907,7 @@ static void test_run_scenarios(enum smtp_protocol protocol,
smtp_server_set.capabilities |= SMTP_CAPABILITY_PIPELINING;
test_max_pending = MAX_PARALLEL_PENDING;
test_unknown_size = FALSE;
test_ssl_mode = TEST_SSL_MODE_NONE;
test_files_init();
test_run_client_server(protocol,
&smtp_client_set, &smtp_server_set,
Expand All @@ -878,13 +920,40 @@ static void test_run_scenarios(enum smtp_protocol protocol,
smtp_server_set.capabilities |= SMTP_CAPABILITY_PIPELINING;
test_max_pending = MAX_PARALLEL_PENDING;
test_unknown_size = TRUE;
test_ssl_mode = TEST_SSL_MODE_NONE;
test_files_init();
test_run_client_server(protocol,
&smtp_client_set, &smtp_server_set,
client_init);
test_files_deinit();

test_out("unknown payload size", TRUE);

smtp_server_set.max_pipelined_commands = 5;
smtp_server_set.capabilities |= SMTP_CAPABILITY_PIPELINING;
test_max_pending = MAX_PARALLEL_PENDING;
test_unknown_size = FALSE;
test_ssl_mode = TEST_SSL_MODE_IMMEDIATE;
test_files_init();
test_run_client_server(protocol,
&smtp_client_set, &smtp_server_set,
client_init);
test_files_deinit();

test_out("parallel pipelining ssl", TRUE);

smtp_server_set.max_pipelined_commands = 5;
smtp_server_set.capabilities |= SMTP_CAPABILITY_PIPELINING;
test_max_pending = MAX_PARALLEL_PENDING;
test_unknown_size = FALSE;
test_ssl_mode = TEST_SSL_MODE_STARTTLS;
test_files_init();
test_run_client_server(protocol,
&smtp_client_set, &smtp_server_set,
client_init);
test_files_deinit();

test_out("parallel pipelining startls", TRUE);
}

static void test_smtp_normal(void)
Expand Down Expand Up @@ -958,6 +1027,11 @@ int main(int argc, char *argv[])
{
int c;

lib_init();
#ifdef HAVE_OPENSSL
ssl_iostream_openssl_init();
#endif

atexit(test_atexit);
(void)signal(SIGCHLD, SIG_IGN);
(void)signal(SIGPIPE, SIG_IGN);
Expand All @@ -983,4 +1057,10 @@ int main(int argc, char *argv[])
bind_ip.u.ip4.s_addr = htonl(INADDR_LOOPBACK);

test_run(test_functions);

ssl_iostream_context_cache_free();
#ifdef HAVE_OPENSSL
ssl_iostream_openssl_deinit();
#endif
lib_deinit();
}

0 comments on commit 0c746fb

Please sign in to comment.