//
// POP3ClientSessionTest.cpp
//
// $Id: POP3ClientSessionTest.cpp,v 1.1.1.3 2006/04/04 23:33:04 obiltschnig Exp $
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "POP3ClientSessionTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "DialogServer.h"
#include "Net/POP3ClientSession.h"
#include "Net/MailMessage.h"
#include "Net/NetException.h"
using Net::POP3ClientSession;
using Net::MessageHeader;
using Net::MailMessage;
using Net::POP3Exception;
POP3ClientSessionTest::POP3ClientSessionTest(const std::string& name): CppUnit::TestCase(name)
{
}
POP3ClientSessionTest::~POP3ClientSessionTest()
{
}
void POP3ClientSessionTest::testLogin()
{
DialogServer server;
server.addResponse("+OK POP3 Ready...");
server.addResponse("+OK USER");
server.addResponse("+OK PASS");
server.addResponse("+OK QUIT");
POP3ClientSession session("localhost", server.port());
session.login("user", "secret");
std::string cmd = server.popCommand();
assert (cmd == "USER user");
cmd = server.popCommand();
assert (cmd == "PASS secret");
session.close();
cmd = server.popCommand();
assert (cmd == "QUIT");
}
void POP3ClientSessionTest::testLoginFail()
{
DialogServer server;
server.addResponse("+OK POP3 Ready...");
server.addResponse("+OK USER");
server.addResponse("-ERR PASS");
server.addResponse("+OK QUIT");
POP3ClientSession session("localhost", server.port());
try
{
session.login("user", "secret");
fail("login failed - must throw");
}
catch (POP3Exception&)
{
}
session.close();
}
void POP3ClientSessionTest::testMessageCount()
{
DialogServer server;
server.addResponse("+OK POP3 Ready...");
server.addResponse("+OK USER");
server.addResponse("+OK PASS");
server.addResponse("+OK 42 12345");
server.addResponse("+OK QUIT");
POP3ClientSession session("localhost", server.port());
session.login("user", "secret");
server.clearCommands();
int n = session.messageCount();
std::string cmd = server.popCommand();
assert (cmd == "STAT");
assert (n == 42);
session.close();
}
void POP3ClientSessionTest::testList()
{
DialogServer server;
server.addResponse("+OK POP3 Ready...");
server.addResponse("+OK USER");
server.addResponse("+OK PASS");
server.addResponse(
"+OK Here comes da list\r\n"
"1 1234\r\n"
"2 5678\r\n"
"3 987\r\n"
".\r\n"
);
server.addResponse("+OK QUIT");
POP3ClientSession session("localhost", server.port());
session.login("user", "secret");
server.clearCommands();
std::vector<POP3ClientSession::MessageInfo> infos;
session.listMessages(infos);
std::string cmd = server.popCommand();
assert (cmd == "LIST");
assert (infos.size() == 3);
assert (infos[0].id == 1);
assert (infos[0].size == 1234);
assert (infos[1].id == 2);
assert (infos[1].size == 5678);
assert (infos[2].id == 3);
assert (infos[2].size == 987);
session.close();
}
void POP3ClientSessionTest::testRetrieveMessage()
{
DialogServer server;
server.addResponse("+OK POP3 Ready...");
server.addResponse("+OK USER");
server.addResponse("+OK PASS");
server.addResponse(
"+OK Here comes the message\r\n"
"From: john.doe@no.where\r\n"
"To: jane.doe@no.where\r\n"
"Subject: test\r\n"
"\r\n"
"Hello Jane,\r\n"
"\r\n"
"blah blah blah...\r\n"
"....\r\n"
"\r\n"
"Yours, John\r\n"
".\r\n"
);
server.addResponse("+OK QUIT");
POP3ClientSession session("localhost", server.port());
session.login("user", "secret");
server.clearCommands();
MailMessage message;
session.retrieveMessage(1, message);
std::string cmd = server.popCommand();
assert (cmd == "RETR 1");
assert (message.getContent() ==
"Hello Jane,\r\n"
"\r\n"
"blah blah blah...\r\n"
"...\r\n"
"\r\n"
"Yours, John\r\n"
);
session.close();
}
void POP3ClientSessionTest::testRetrieveHeader()
{
DialogServer server;
server.addResponse("+OK POP3 Ready...");
server.addResponse("+OK USER");
server.addResponse("+OK PASS");
server.addResponse(
"+OK Here comes the message\r\n"
"From: john.doe@no.where\r\n"
"To: jane.doe@no.where\r\n"
"Subject: test\r\n"
"\r\n"
"."
);
server.addResponse("+OK QUIT");
POP3ClientSession session("localhost", server.port());
session.login("user", "secret");
server.clearCommands();
MessageHeader header;
session.retrieveHeader(1, header);
std::string cmd = server.popCommand();
assert (cmd == "TOP 1 0");
assert (header.get("From") == "john.doe@no.where");
assert (header.get("To") == "jane.doe@no.where");
assert (header.get("Subject") == "test");
session.close();
}
void POP3ClientSessionTest::testRetrieveMessages()
{
DialogServer server;
server.addResponse("+OK POP3 Ready...");
server.addResponse("+OK USER");
server.addResponse("+OK PASS");
server.addResponse(
"+OK Here comes the message\r\n"
"From: john.doe@no.where\r\n"
"To: jane.doe@no.where\r\n"
"Subject: test\r\n"
"\r\n"
"."
);
server.addResponse(
"+OK Here comes the message\r\n"
"From: john.doe@no.where\r\n"
"To: jane.doe@no.where\r\n"
"Subject: test\r\n"
"\r\n"
"Hello Jane,\r\n"
"\r\n"
"blah blah blah...\r\n"
"....\r\n"
"\r\n"
"Yours, John\r\n"
"."
);
server.addResponse("+OK QUIT");
POP3ClientSession session("localhost", server.port());
session.login("user", "secret");
server.clearCommands();
MessageHeader header;
session.retrieveHeader(1, header);
std::string cmd = server.popCommand();
assert (cmd == "TOP 1 0");
assert (header.get("From") == "john.doe@no.where");
assert (header.get("To") == "jane.doe@no.where");
assert (header.get("Subject") == "test");
MailMessage message;
session.retrieveMessage(2, message);
cmd = server.popCommand();
assert (cmd == "RETR 2");
poco_recv_mail_test.zip
需积分: 10 185 浏览量
2012-06-29
10:19:55
上传
评论
收藏 5KB ZIP 举报
ldliude1571
- 粉丝: 2
- 资源: 28