From d1e587a9cd06a9cf5f619642ec17506d589c687b Mon Sep 17 00:00:00 2001 From: Dmitry Isaikin Date: Tue, 9 Feb 2016 13:48:03 +0300 Subject: Some tests for MessageBuilder::writeToFile --- tests/test-all.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test-all.cpp b/tests/test-all.cpp index 1858bef1..ba79a3ea 100644 --- a/tests/test-all.cpp +++ b/tests/test-all.cpp @@ -63,6 +63,16 @@ class TestCallback : public mailcore::Object, public mailcore::OperationCallback } }; +static mailcore::String * temporaryFilenameForTest() +{ + char tempfile[] = "/tmp/mailcore2-test-XXXXXX"; + char * result = mktemp(tempfile); + if (result == NULL) { + return NULL; + } + return mailcore::String::stringWithFileSystemRepresentation(tempfile); +} + static mailcore::Data * testMessageBuilder() { mailcore::Address * address = new mailcore::Address(); @@ -96,7 +106,12 @@ static mailcore::Data * testMessageBuilder() mailcore::Data * data = msg->data(); MCLog("%s", data->bytes()); - + + mailcore::String *filename = temporaryFilenameForTest(); + msg->writeToFile(filename); + mailcore::Data *fileData = mailcore::Data::dataWithContentsOfFile(filename); + MCAssert(data->isEqual(fileData)); + mailcore::MessageBuilder * msg2 = new mailcore::MessageBuilder(msg); mailcore::String *htmlBody = msg->htmlBody(); mailcore::String *htmlBody2 = msg2->htmlBody(); @@ -104,7 +119,8 @@ static mailcore::Data * testMessageBuilder() msg->release(); msg2->release(); - + unlink(filename->fileSystemRepresentation()); + return data; } -- cgit v1.2.3 From e554610cb5b5820eae81217066d594c6f1999089 Mon Sep 17 00:00:00 2001 From: Dmitry Isaikin Date: Tue, 9 Feb 2016 16:12:15 +0300 Subject: Add some tests for sending message from file via SMTP --- tests/test-all.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'tests') diff --git a/tests/test-all.cpp b/tests/test-all.cpp index ba79a3ea..de5c50a6 100644 --- a/tests/test-all.cpp +++ b/tests/test-all.cpp @@ -197,6 +197,49 @@ static void testSMTP(mailcore::Data * data) smtp->release(); } +static void parseAddressesFromRfc822(mailcore::String * filename, mailcore::Array ** pRecipients, mailcore::Address ** pFrom) +{ + mailcore::MessageParser * parser = mailcore::MessageParser::messageParserWithContentsOfFile(filename); + + mailcore::Array * recipients = mailcore::Array::array(); + if (parser->header()->to() != NULL) { + recipients->addObjectsFromArray(parser->header()->to()); + } + if (parser->header()->cc() != NULL) { + recipients->addObjectsFromArray(parser->header()->cc()); + } + if (parser->header()->bcc() != NULL) { + recipients->addObjectsFromArray(parser->header()->bcc()); + } + *pRecipients = recipients; + *pFrom = parser->header()->from(); +} + +static void testSendingMessageFromFileViaSMTP(mailcore::Data * data) +{ + mailcore::SMTPSession * smtp; + mailcore::ErrorCode error; + + mailcore::String * filename = temporaryFilenameForTest(); + data->writeToFile(filename); + + smtp = new mailcore::SMTPSession(); + + smtp->setHostname(MCSTR("smtp.gmail.com")); + smtp->setPort(25); + smtp->setUsername(email); + smtp->setPassword(password); + smtp->setConnectionType(mailcore::ConnectionTypeStartTLS); + + mailcore::Array * recipients; + mailcore::Address * from; + parseAddressesFromRfc822(filename, &recipients, &from); + + smtp->sendMessage(from, recipients, filename, NULL, &error); + + smtp->release(); +} + static void testPOP() { mailcore::POPSession * session; @@ -288,6 +331,36 @@ static void testAsyncSMTP(mailcore::Data * data) //smtp->release(); } +static void testAsyncSendMessageFromFileViaSMTP(mailcore::Data * data) +{ + mailcore::SMTPAsyncSession * smtp; + TestSMTPCallback * callback = new TestSMTPCallback(); + + mailcore::String * filename = temporaryFilenameForTest(); + data->writeToFile(filename); + + mailcore::Array * recipients; + mailcore::Address * from; + parseAddressesFromRfc822(filename, &recipients, &from); + + smtp = new mailcore::SMTPAsyncSession(); + + smtp->setHostname(MCSTR("smtp.gmail.com")); + smtp->setPort(25); + smtp->setUsername(email); + smtp->setPassword(password); + smtp->setConnectionType(mailcore::ConnectionTypeStartTLS); + + mailcore::SMTPOperation * op = smtp->sendMessageOperation(from, recipients, filename); + op->setSmtpCallback(callback); + op->setCallback(callback); + op->start(); + + mainLoop(); + + //smtp->release(); +} + class TestIMAPCallback : public mailcore::Object, public mailcore::OperationCallback, public mailcore::IMAPOperationCallback { virtual void operationFinished(mailcore::Operation * op) { @@ -408,11 +481,13 @@ void testAll() //mailcore::Data * data = testMessageBuilder(); //testMessageParser(data); //testSMTP(data); + //testSendingMessageFromFileViaSMTP(data); //testIMAP(); //testIMAPMove(); //testPOP(); //testNNTP(); //testAsyncSMTP(data); + //testAsyncSendMessageFromFileViaSMTP(data); //testAsyncIMAP(); //testAsyncPOP(); //testAddresses(); -- cgit v1.2.3