vmime/tests/net/smtp/SMTPTransportTest.cpp

325 lines
11 KiB
C++
Raw Normal View History

2006-05-05 20:50:26 +00:00
//
// VMime library (http://www.vmime.org)
2018-09-05 21:54:48 +00:00
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
2006-05-05 20:50:26 +00:00
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
2006-05-05 20:50:26 +00:00
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library. Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//
#include "tests/testUtils.hpp"
#include "vmime/net/smtp/SMTPTransport.hpp"
#include "vmime/net/smtp/SMTPChunkingOutputStreamAdapter.hpp"
2013-09-08 18:49:51 +00:00
#include "vmime/net/smtp/SMTPExceptions.hpp"
#include "SMTPTransportTestUtils.hpp"
2006-05-05 20:50:26 +00:00
2013-03-08 07:19:50 +00:00
VMIME_TEST_SUITE_BEGIN(SMTPTransportTest)
2006-05-05 20:50:26 +00:00
VMIME_TEST_LIST_BEGIN
2013-08-16 09:41:55 +00:00
VMIME_TEST(testConnectToInvalidServer)
2006-05-05 20:50:26 +00:00
VMIME_TEST(testGreetingError)
VMIME_TEST(testMAILandRCPT)
VMIME_TEST(testChunking)
VMIME_TEST(testSize_Chunking)
VMIME_TEST(testSize_NoChunking)
VMIME_TEST(testSMTPUTF8_available)
VMIME_TEST(testSMTPUTF8_notAvailable)
2006-05-05 20:50:26 +00:00
VMIME_TEST_LIST_END
2018-09-05 21:54:48 +00:00
void testConnectToInvalidServer() {
vmime::shared_ptr <vmime::net::session> sess = vmime::net::session::create();
2013-08-16 09:41:55 +00:00
vmime::utility::url url("smtp://invalid-smtp-server");
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::transport> store = sess->getTransport(url);
2013-08-16 09:41:55 +00:00
VASSERT_THROW("connect", store->connect(), vmime::exceptions::connection_error);
}
2018-09-05 21:54:48 +00:00
void testGreetingError() {
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2006-05-05 20:50:26 +00:00
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
2006-05-05 20:50:26 +00:00
2013-11-21 21:16:57 +00:00
tr->setSocketFactory(vmime::make_shared <testSocketFactory <greetingErrorSMTPTestSocket> >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
2006-05-05 20:50:26 +00:00
2018-09-05 21:54:48 +00:00
VASSERT_THROW(
"Connection",
tr->connect(),
vmime::exceptions::connection_greeting_error
);
2006-05-05 20:50:26 +00:00
}
2018-09-05 21:54:48 +00:00
void testMAILandRCPT() {
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2006-05-05 20:50:26 +00:00
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
2006-05-05 20:50:26 +00:00
2013-11-21 21:16:57 +00:00
tr->setSocketFactory(vmime::make_shared <testSocketFactory <MAILandRCPTSMTPTestSocket> >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
2006-05-05 20:50:26 +00:00
VASSERT_NO_THROW("Connection", tr->connect());
vmime::mailbox exp("expeditor@test.vmime.org");
vmime::mailboxList recips;
2013-11-21 21:16:57 +00:00
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient1@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient2@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient3@test.vmime.org"));
2006-05-05 20:50:26 +00:00
vmime::string data("Message data");
vmime::utility::inputStreamStringAdapter is(data);
tr->send(exp, recips, is, 0);
}
2018-09-05 21:54:48 +00:00
void testChunking() {
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
2013-11-21 21:16:57 +00:00
tr->setSocketFactory(vmime::make_shared <testSocketFactory <chunkingSMTPTestSocket> >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
tr->connect();
2018-09-05 21:54:48 +00:00
VASSERT(
"Test server should report it supports the CHUNKING extension!",
vmime::dynamicCast <vmime::net::smtp::SMTPTransport>(tr)->getConnection()->hasExtension("CHUNKING")
);
vmime::mailbox exp("expeditor@test.vmime.org");
vmime::mailboxList recips;
2013-11-21 21:16:57 +00:00
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient@test.vmime.org"));
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::message> msg = vmime::make_shared <SMTPTestMessage>();
tr->send(msg, exp, recips);
}
2018-09-05 21:54:48 +00:00
void testSize_Chunking() {
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
2013-11-21 21:16:57 +00:00
tr->setSocketFactory(vmime::make_shared <testSocketFactory <bigMessageSMTPTestSocket <true> > >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
tr->connect();
2018-09-05 21:54:48 +00:00
VASSERT(
"Test server should report it supports the SIZE extension!",
vmime::dynamicCast <vmime::net::smtp::SMTPTransport>(tr)->getConnection()->hasExtension("SIZE")
);
vmime::mailbox exp("expeditor@test.vmime.org");
vmime::mailboxList recips;
2013-11-21 21:16:57 +00:00
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient@test.vmime.org"));
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::message> msg = vmime::make_shared <SMTPBigTestMessage4MB>();
2018-09-05 21:54:48 +00:00
VASSERT_THROW(
"Max size limit exception",
tr->send(msg, exp, recips),
vmime::net::smtp::SMTPMessageSizeExceedsMaxLimitsException
);
}
2018-09-05 21:54:48 +00:00
void testSize_NoChunking() {
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
2013-11-21 21:16:57 +00:00
tr->setSocketFactory(vmime::make_shared <testSocketFactory <bigMessageSMTPTestSocket <false> > >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
tr->connect();
2018-09-05 21:54:48 +00:00
VASSERT(
"Test server should report it supports the SIZE extension!",
vmime::dynamicCast <vmime::net::smtp::SMTPTransport>(tr)->getConnection()->hasExtension("SIZE")
);
vmime::mailbox exp("expeditor@test.vmime.org");
vmime::mailboxList recips;
2013-11-21 21:16:57 +00:00
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient@test.vmime.org"));
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::message> msg = vmime::make_shared <SMTPBigTestMessage4MB>();
2018-09-05 21:54:48 +00:00
VASSERT_THROW(
"Max size limit exception",
tr->send(msg, exp, recips),
vmime::net::smtp::SMTPMessageSizeExceedsMaxLimitsException
);
}
2006-05-05 20:50:26 +00:00
2018-09-05 21:54:48 +00:00
void testSMTPUTF8_available() {
// Test with UTF8 sender
{
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
tr->setSocketFactory(vmime::make_shared <testSocketFactory <UTF8SMTPTestSocket <true> > >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
VASSERT_NO_THROW("Connection", tr->connect());
vmime::mailbox exp(
vmime::emailAddress(
vmime::word("expéditeur", vmime::charsets::UTF_8),
vmime::word("test.vmime.org")
)
);
vmime::mailboxList recips;
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient1@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient2@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>(
vmime::emailAddress(
vmime::word("récepteur", vmime::charsets::UTF_8),
vmime::word("test.vmime.org")
)
));
vmime::string data("Message data");
vmime::utility::inputStreamStringAdapter is(data);
tr->send(exp, recips, is, 0);
}
// Test with UTF8 recipient only
{
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
tr->setSocketFactory(vmime::make_shared <testSocketFactory <UTF8SMTPTestSocket <true> > >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
VASSERT_NO_THROW("Connection", tr->connect());
vmime::mailbox exp("expediteur@test.vmime.org");
vmime::mailboxList recips;
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient1@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient2@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>(
vmime::emailAddress(
vmime::word("récepteur", vmime::charsets::UTF_8),
vmime::word("test.vmime.org")
)
));
vmime::string data("Message data");
vmime::utility::inputStreamStringAdapter is(data);
tr->send(exp, recips, is, 0);
}
}
2018-09-05 21:54:48 +00:00
void testSMTPUTF8_notAvailable() {
// Test with UTF8 sender
{
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
tr->setSocketFactory(vmime::make_shared <testSocketFactory <UTF8SMTPTestSocket <false> > >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
VASSERT_NO_THROW("Connection", tr->connect());
vmime::mailbox exp(
vmime::emailAddress(
vmime::word("expéditeur", vmime::charsets::UTF_8),
vmime::word("test.vmime.org")
)
);
vmime::mailboxList recips;
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient1@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient2@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>(
vmime::emailAddress(
vmime::word("récepteur", vmime::charsets::UTF_8),
vmime::word("test.vmime.org")
)
));
vmime::string data("Message data");
vmime::utility::inputStreamStringAdapter is(data);
tr->send(exp, recips, is, 0);
}
// Test with UTF8 recipient only
{
vmime::shared_ptr <vmime::net::session> session = vmime::net::session::create();
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::transport> tr =
session->getTransport(vmime::utility::url("smtp://localhost"));
tr->setSocketFactory(vmime::make_shared <testSocketFactory <UTF8SMTPTestSocket <false> > >());
tr->setTimeoutHandlerFactory(vmime::make_shared <testTimeoutHandlerFactory>());
VASSERT_NO_THROW("Connection", tr->connect());
vmime::mailbox exp("expediteur@test.vmime.org");
vmime::mailboxList recips;
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient1@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>("recipient2@test.vmime.org"));
recips.appendMailbox(vmime::make_shared <vmime::mailbox>(
vmime::emailAddress(
vmime::word("récepteur", vmime::charsets::UTF_8),
vmime::word("test.vmime.org")
)
));
vmime::string data("Message data");
vmime::utility::inputStreamStringAdapter is(data);
tr->send(exp, recips, is, 0);
}
}
VMIME_TEST_SUITE_END