vmime/tests/net/smtp/SMTPTransportTest.cpp

179 lines
6.2 KiB
C++
Raw Normal View History

2006-05-05 20:50:26 +00:00
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2013 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)
2006-05-05 20:50:26 +00:00
VMIME_TEST_LIST_END
2013-08-16 09:41:55 +00:00
void testConnectToInvalidServer()
{
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::session> sess
= vmime::make_shared <vmime::net::session>();
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);
}
2006-05-05 20:50:26 +00:00
void testGreetingError()
{
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::session> session =
vmime::make_shared <vmime::net::session>();
2006-05-05 20:50:26 +00:00
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::transport> tr = session->getTransport
2006-05-05 20:50:26 +00:00
(vmime::utility::url("smtp://localhost"));
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
VASSERT_THROW("Connection", tr->connect(),
vmime::exceptions::connection_greeting_error);
}
void testMAILandRCPT()
{
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::session> session =
vmime::make_shared <vmime::net::session>();
2006-05-05 20:50:26 +00:00
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::transport> tr = session->getTransport
2006-05-05 20:50:26 +00:00
(vmime::utility::url("smtp://localhost"));
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);
}
void testChunking()
{
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::session> session =
vmime::make_shared <vmime::net::session>();
2013-11-21 21:16:57 +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();
VASSERT("Test server should report it supports the CHUNKING extension!",
2013-11-21 21:16:57 +00:00
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);
}
void testSize_Chunking()
2006-05-05 20:50:26 +00:00
{
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::session> session =
vmime::make_shared <vmime::net::session>();
2013-11-21 21:16:57 +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();
VASSERT("Test server should report it supports the SIZE extension!",
2013-11-21 21:16:57 +00:00
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>();
VASSERT_THROW("Connection", tr->send(msg, exp, recips),
2013-09-08 18:49:51 +00:00
vmime::net::smtp::SMTPMessageSizeExceedsMaxLimitsException);
}
void testSize_NoChunking()
{
2013-11-21 21:16:57 +00:00
vmime::shared_ptr <vmime::net::session> session =
vmime::make_shared <vmime::net::session>();
2013-11-21 21:16:57 +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();
VASSERT("Test server should report it supports the SIZE extension!",
2013-11-21 21:16:57 +00:00
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>();
VASSERT_THROW("Connection", tr->send(msg, exp, recips),
2013-09-08 18:49:51 +00:00
vmime::net::smtp::SMTPMessageSizeExceedsMaxLimitsException);
}
2006-05-05 20:50:26 +00:00
VMIME_TEST_SUITE_END
2006-05-05 20:50:26 +00:00