diff options
author | Hoà V. DINH <dinh.viet.hoa@gmail.com> | 2016-02-09 07:31:25 -0800 |
---|---|---|
committer | Hoà V. DINH <dinh.viet.hoa@gmail.com> | 2016-02-09 07:31:25 -0800 |
commit | 20ae2fd57a9a9edd4c539fa170433ee8d59904b1 (patch) | |
tree | 59699d28c313dc3348d36da8f4d80437deb4ded6 /tests | |
parent | 5b694c89ebe98f388af25ad8f176f42523594358 (diff) | |
parent | e554610cb5b5820eae81217066d594c6f1999089 (diff) |
Merge pull request #1355 from disaykin/reduce-memory-on-send-message
Add low-memory interface for building, sending and appending messages
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-all.cpp | 95 |
1 files changed, 93 insertions, 2 deletions
diff --git a/tests/test-all.cpp b/tests/test-all.cpp index 02a4081c..c3e25ada 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; } @@ -200,6 +216,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; @@ -291,6 +350,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) { @@ -411,12 +500,14 @@ void testAll() //mailcore::Data * data = testMessageBuilder(); //testMessageParser(data); //testSMTP(data); + //testSendingMessageFromFileViaSMTP(data); //testIMAP(); //testIMAPMove(); //testIMAPCapability(); //testPOP(); //testNNTP(); //testAsyncSMTP(data); + //testAsyncSendMessageFromFileViaSMTP(data); //testAsyncIMAP(); //testAsyncPOP(); //testAddresses(); |