diff --git a/SConstruct b/SConstruct index e8891bd9..a2b7f8aa 100644 --- a/SConstruct +++ b/SConstruct @@ -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' ] diff --git a/src/net/smtp/SMTPCommandSet.cpp b/src/net/smtp/SMTPCommandSet.cpp index 8644ca48..0454e184 100644 --- a/src/net/smtp/SMTPCommandSet.cpp +++ b/src/net/smtp/SMTPCommandSet.cpp @@ -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); } diff --git a/tests/net/smtp/SMTPCommandSetTest.cpp b/tests/net/smtp/SMTPCommandSetTest.cpp new file mode 100644 index 00000000..a315d678 --- /dev/null +++ b/tests/net/smtp/SMTPCommandSetTest.cpp @@ -0,0 +1,179 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2013 Vincent Richard +// +// 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 cset = SMTPCommandSet::create(/* pipelining */ false); + + VASSERT_NOT_NULL("Not null", cset); + VASSERT_FALSE("Finished", cset->isFinished()); + } + + void testCreatePipeline() + { + vmime::ref cset = SMTPCommandSet::create(/* pipelining */ true); + + VASSERT_NOT_NULL("Not null", cset); + VASSERT_FALSE("Finished", cset->isFinished()); + } + + void testAddCommand() + { + vmime::ref 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 sok = vmime::create (); + + 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 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 sok = vmime::create (); + 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 cset = SMTPCommandSet::create(/* pipelining */ false); + + cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1")); + cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2")); + + vmime::ref sok = vmime::create (); + 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 cset = SMTPCommandSet::create(/* pipelining */ true); + + cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1")); + cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2")); + + vmime::ref sok = vmime::create (); + 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 cset = SMTPCommandSet::create(/* pipelining */ false); + + cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1")); + cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2")); + + vmime::ref sok = vmime::create (); + + 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 cset = SMTPCommandSet::create(/* pipelining */ true); + + cset->addCommand(SMTPCommand::createCommand("MY_COMMAND1")); + cset->addCommand(SMTPCommand::createCommand("MY_COMMAND2")); + + vmime::ref sok = vmime::create (); + + 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 diff --git a/tests/net/smtp/SMTPCommandTest.cpp b/tests/net/smtp/SMTPCommandTest.cpp new file mode 100644 index 00000000..93cdf7ae --- /dev/null +++ b/tests/net/smtp/SMTPCommandTest.cpp @@ -0,0 +1,155 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2013 Vincent Richard +// +// 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 cmd = SMTPCommand::createCommand("MY_COMMAND"); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "MY_COMMAND", cmd->getText()); + } + + void testCreateCommandParams() + { + vmime::ref 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 cmd = SMTPCommand::HELO("hostname"); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "HELO hostname", cmd->getText()); + } + + void testEHLO() + { + vmime::ref cmd = SMTPCommand::EHLO("hostname"); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "EHLO hostname", cmd->getText()); + } + + void testAUTH() + { + vmime::ref cmd = SMTPCommand::AUTH("saslmechanism"); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "AUTH saslmechanism", cmd->getText()); + } + + void testSTARTTLS() + { + vmime::ref cmd = SMTPCommand::STARTTLS(); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "STARTTLS", cmd->getText()); + } + + void testMAIL() + { + vmime::ref cmd = SMTPCommand::MAIL(vmime::mailbox("me@vmime.org")); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "MAIL FROM:", cmd->getText()); + } + + void testRCPT() + { + vmime::ref cmd = SMTPCommand::RCPT(vmime::mailbox("someone@vmime.org")); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "RCPT TO:", cmd->getText()); + } + + void testDATA() + { + vmime::ref cmd = SMTPCommand::DATA(); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "DATA", cmd->getText()); + } + + void testNOOP() + { + vmime::ref cmd = SMTPCommand::NOOP(); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "NOOP", cmd->getText()); + } + + void testQUIT() + { + vmime::ref cmd = SMTPCommand::QUIT(); + + VASSERT_NOT_NULL("Not null", cmd); + VASSERT_EQ("Text", "QUIT", cmd->getText()); + } + + void testWriteToSocket() + { + vmime::ref cmd = SMTPCommand::createCommand("MY_COMMAND param1 param2"); + + vmime::ref sok = vmime::create (); + cmd->writeToSocket(sok); + + vmime::string response; + sok->localReceive(response); + + VASSERT_EQ("Sent buffer", "MY_COMMAND param1 param2\r\n", response); + } + +VMIME_TEST_SUITE_END diff --git a/tests/testUtils.hpp b/tests/testUtils.hpp index d68dba8d..55c0424e 100644 --- a/tests/testUtils.hpp +++ b/tests/testUtils.hpp @@ -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)