Unit tests for SMTPCommand and SMTPCommandSet. Fixed SMTPCommandSet::isFinished().

This commit is contained in:
Vincent Richard 2013-02-12 17:54:36 +01:00
parent 83c5ba96b9
commit feabba4470
5 changed files with 343 additions and 2 deletions

View File

@ -377,6 +377,8 @@ libvmimetest_sources = [
'tests/security/digest/sha1Test.cpp',
# =============================== Net ================================
'tests/net/smtp/SMTPTransportTest.cpp',
'tests/net/smtp/SMTPCommandTest.cpp',
'tests/net/smtp/SMTPCommandSetTest.cpp',
'tests/net/smtp/SMTPResponseTest.cpp',
'tests/net/maildir/maildirStoreTest.cpp'
]

View File

@ -126,7 +126,7 @@ const string SMTPCommandSet::getText() const
const bool SMTPCommandSet::isFinished() const
{
return (m_pipeline && m_started) || (m_commands.size() == 0);
return (m_pipeline && m_started) || (m_commands.size() == 0 && m_started);
}

View File

@ -0,0 +1,179 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
//
// 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
// 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/SMTPCommandSet.hpp"
#include "vmime/net/smtp/SMTPCommand.hpp"
using namespace vmime::net::smtp;
#define VMIME_TEST_SUITE SMTPCommandSetTest
#define VMIME_TEST_SUITE_MODULE "Net/SMTP"
VMIME_TEST_SUITE_BEGIN
VMIME_TEST_LIST_BEGIN
VMIME_TEST(testCreate)
VMIME_TEST(testCreatePipeline)
VMIME_TEST(testAddCommand)
VMIME_TEST(testAddCommandPipeline)
VMIME_TEST(testWriteToSocket)
VMIME_TEST(testWriteToSocketPipeline)
VMIME_TEST(testGetLastCommandSent)
VMIME_TEST(testGetLastCommandSentPipeline)
VMIME_TEST_LIST_END
void testCreate()
{
vmime::ref <SMTPCommandSet> cset = SMTPCommandSet::create(/* pipelining */ false);
VASSERT_NOT_NULL("Not null", cset);
VASSERT_FALSE("Finished", cset->isFinished());
}
void testCreatePipeline()
{
vmime::ref <SMTPCommandSet> cset = SMTPCommandSet::create(/* pipelining */ true);
VASSERT_NOT_NULL("Not null", cset);
VASSERT_FALSE("Finished", cset->isFinished());
}
void testAddCommand()
{
vmime::ref <SMTPCommandSet> cset = SMTPCommandSet::create(/* pipelining */ false);
VASSERT_NO_THROW("No throw 1", cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1")));
VASSERT_EQ("Text", "MY_COMMAND1\r\n", cset->getText());
VASSERT_NO_THROW("No throw 2", cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2")));
VASSERT_EQ("Text", "MY_COMMAND1\r\nMY_COMMAND2\r\n", cset->getText());
vmime::ref <testSocket> sok = vmime::create <testSocket>();
cset->writeToSocket(sok);
VASSERT_FALSE("Finished", cset->isFinished());
// Can't add commands when writing to socket has started
VASSERT_THROW("Throw", cset->addCommand(SMTPCommand::createCommand("MY_COMMAND3")), std::runtime_error);
cset->writeToSocket(sok);
VASSERT_TRUE("Finished", cset->isFinished());
}
void testAddCommandPipeline()
{
vmime::ref <SMTPCommandSet> cset = SMTPCommandSet::create(/* pipelining */ true);
VASSERT_NO_THROW("No throw 1", cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1")));
VASSERT_EQ("Text", "MY_COMMAND1\r\n", cset->getText());
VASSERT_NO_THROW("No throw 2", cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2")));
VASSERT_EQ("Text", "MY_COMMAND1\r\nMY_COMMAND2\r\n", cset->getText());
vmime::ref <testSocket> sok = vmime::create <testSocket>();
vmime::string response;
cset->writeToSocket(sok);
VASSERT_TRUE("Finished", cset->isFinished());
sok->localReceive(response);
VASSERT_EQ("Receive cmds", "MY_COMMAND1\r\nMY_COMMAND2\r\n", response);
// Can't add commands when writing to socket has started
VASSERT_THROW("Throw", cset->addCommand(SMTPCommand::createCommand("MY_COMMAND3")), std::runtime_error);
}
void testWriteToSocket()
{
vmime::ref <SMTPCommandSet> cset = SMTPCommandSet::create(/* pipelining */ false);
cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1"));
cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2"));
vmime::ref <testSocket> sok = vmime::create <testSocket>();
vmime::string response;
cset->writeToSocket(sok);
sok->localReceive(response);
VASSERT_EQ("Receive cmd 1", "MY_COMMAND1\r\n", response);
cset->writeToSocket(sok);
sok->localReceive(response);
VASSERT_EQ("Receive cmd 2", "MY_COMMAND2\r\n", response);
}
void testWriteToSocketPipeline()
{
vmime::ref <SMTPCommandSet> cset = SMTPCommandSet::create(/* pipelining */ true);
cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1"));
cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2"));
vmime::ref <testSocket> sok = vmime::create <testSocket>();
vmime::string response;
cset->writeToSocket(sok);
sok->localReceive(response);
VASSERT_EQ("Receive cmds", "MY_COMMAND1\r\nMY_COMMAND2\r\n", response);
}
void testGetLastCommandSent()
{
vmime::ref <SMTPCommandSet> cset = SMTPCommandSet::create(/* pipelining */ false);
cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1"));
cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2"));
vmime::ref <testSocket> sok = vmime::create <testSocket>();
cset->writeToSocket(sok);
VASSERT_EQ("Cmd 1", "MY_COMMAND1", cset->getLastCommandSent()->getText());
cset->writeToSocket(sok);
VASSERT_EQ("Cmd 2", "MY_COMMAND2", cset->getLastCommandSent()->getText());
}
void testGetLastCommandSentPipeline()
{
vmime::ref <SMTPCommandSet> cset = SMTPCommandSet::create(/* pipelining */ true);
cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1"));
cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2"));
vmime::ref <testSocket> sok = vmime::create <testSocket>();
cset->writeToSocket(sok);
VASSERT_EQ("Cmd 1", "MY_COMMAND1", cset->getLastCommandSent()->getText());
cset->writeToSocket(sok);
VASSERT_EQ("Cmd 2", "MY_COMMAND2", cset->getLastCommandSent()->getText());
}
VMIME_TEST_SUITE_END

View File

@ -0,0 +1,155 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
//
// 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
// 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/SMTPCommand.hpp"
using namespace vmime::net::smtp;
#define VMIME_TEST_SUITE SMTPCommandTest
#define VMIME_TEST_SUITE_MODULE "Net/SMTP"
VMIME_TEST_SUITE_BEGIN
VMIME_TEST_LIST_BEGIN
VMIME_TEST(testCreateCommand)
VMIME_TEST(testCreateCommandParams)
VMIME_TEST(testHELO)
VMIME_TEST(testEHLO)
VMIME_TEST(testAUTH)
VMIME_TEST(testSTARTTLS)
VMIME_TEST(testMAIL)
VMIME_TEST(testRCPT)
VMIME_TEST(testDATA)
VMIME_TEST(testNOOP)
VMIME_TEST(testQUIT)
VMIME_TEST(testWriteToSocket)
VMIME_TEST_LIST_END
void testCreateCommand()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::createCommand("MY_COMMAND");
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "MY_COMMAND", cmd->getText());
}
void testCreateCommandParams()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::createCommand("MY_COMMAND param1 param2");
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "MY_COMMAND param1 param2", cmd->getText());
}
void testHELO()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::HELO("hostname");
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "HELO hostname", cmd->getText());
}
void testEHLO()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::EHLO("hostname");
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "EHLO hostname", cmd->getText());
}
void testAUTH()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::AUTH("saslmechanism");
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "AUTH saslmechanism", cmd->getText());
}
void testSTARTTLS()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::STARTTLS();
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "STARTTLS", cmd->getText());
}
void testMAIL()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::MAIL(vmime::mailbox("me@vmime.org"));
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "MAIL FROM:<me@vmime.org>", cmd->getText());
}
void testRCPT()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::RCPT(vmime::mailbox("someone@vmime.org"));
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "RCPT TO:<someone@vmime.org>", cmd->getText());
}
void testDATA()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::DATA();
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "DATA", cmd->getText());
}
void testNOOP()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::NOOP();
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "NOOP", cmd->getText());
}
void testQUIT()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::QUIT();
VASSERT_NOT_NULL("Not null", cmd);
VASSERT_EQ("Text", "QUIT", cmd->getText());
}
void testWriteToSocket()
{
vmime::ref <SMTPCommand> cmd = SMTPCommand::createCommand("MY_COMMAND param1 param2");
vmime::ref <testSocket> sok = vmime::create <testSocket>();
cmd->writeToSocket(sok);
vmime::string response;
sok->localReceive(response);
VASSERT_EQ("Sent buffer", "MY_COMMAND param1 param2\r\n", response);
}
VMIME_TEST_SUITE_END

View File

@ -42,7 +42,12 @@
#define VASSERT_TRUE(msg, cond) \
VASSERT(msg, cond)
#define VASSERT_FALSE(msg, cond) \
VASSERT(!(msg, cond))
VASSERT(msg, !(cond))
#define VASSERT_NOT_NULL(msg, cond) \
VASSERT(msg, cond != NULL)
#define VASSERT_NULL(msg, cond) \
VASSERT(msg, cond == NULL)
#define VASSERT_EQ(msg, expected, actual) \
CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(msg), expected, actual)