message: move tests to test-mu-message.cc

This commit is contained in:
Dirk-Jan C. Binnema
2022-04-09 01:26:43 +03:00
parent 4e65cac631
commit 00c7fc897d
3 changed files with 337 additions and 233 deletions

View File

@ -558,234 +558,3 @@ Message::parts() const
return priv_->parts;
}
/*
* tests
*/
#ifdef BUILD_TESTS
/*
* test message 1
*/
static void
test_message_mailing_list()
{
constexpr const char *test_message_1 =
R"(Return-Path: <sqlite-dev-bounces@sqlite.org>
X-Original-To: xxxx@localhost
Delivered-To: xxxx@localhost
Received: from mindcrime (localhost [127.0.0.1])
by mail.xxxxsoftware.nl (Postfix) with ESMTP id 32F276963F
for <xxxx@localhost>; Mon, 4 Aug 2008 21:49:34 +0300 (EEST)
Message-Id: <83B5AF40-DBFA-4578-A043-04C80276E195@sqlabs.net>
From: anon@example.com
To: sqlite-dev@sqlite.org
Mime-Version: 1.0 (Apple Message framework v926)
Date: Mon, 4 Aug 2008 11:40:49 +0200
X-Mailer: Apple Mail (2.926)
Subject: [sqlite-dev] VM optimization inside sqlite3VdbeExec
Precedence: list
Reply-To: sqlite-dev@sqlite.org
List-Id: <sqlite-dev.sqlite.org>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Sender: sqlite-dev-bounces@sqlite.org
Content-Length: 639
Inside sqlite3VdbeExec there is a very big switch statement.
In order to increase performance with few modifications to the
original code, why not use this technique ?
http://docs.freebsd.org/info/gcc/gcc.info.Labels_as_Values.html
With a properly defined "instructions" array, instead of the switch
statement you can use something like:
goto * instructions[pOp->opcode];
)";
auto message{Message::make_from_string(test_message_1)};
g_assert_true(!!message);
g_assert_true(message->bcc().empty());
g_assert_true(!message->body_html());
assert_equal(message->body_text().value_or(""),
R"(Inside sqlite3VdbeExec there is a very big switch statement.
In order to increase performance with few modifications to the
original code, why not use this technique ?
http://docs.freebsd.org/info/gcc/gcc.info.Labels_as_Values.html
With a properly defined "instructions" array, instead of the switch
statement you can use something like:
goto * instructions[pOp->opcode];
)");
g_assert_true(message->cc().empty());
g_assert_cmpuint(message->date(), ==, 1217842849);
g_assert_true(message->flags() == (Flags::MailingList | Flags::Unread));
const auto from{message->from()};
g_assert_cmpuint(from.size(),==,1);
assert_equal(from.at(0).name, "");
assert_equal(from.at(0).email, "anon@example.com");
assert_equal(message->mailing_list(), "sqlite-dev.sqlite.org");
assert_equal(message->message_id(),
"83B5AF40-DBFA-4578-A043-04C80276E195@sqlabs.net");
g_assert_true(message->path().empty());
g_assert_true(message->priority() == Priority::Low);
g_assert_cmpuint(message->size(),==,::strlen(test_message_1));
g_assert_true(message->references().empty());
assert_equal(message->subject(),
"[sqlite-dev] VM optimization inside sqlite3VdbeExec");
const auto to{message->to()};
g_assert_cmpuint(to.size(),==,1);
assert_equal(to.at(0).name, "");
assert_equal(to.at(0).email, "sqlite-dev@sqlite.org");
assert_equal(message->header("X-Mailer").value_or(""), "Apple Mail (2.926)");
auto all_contacts{message->all_contacts()};
g_assert_cmpuint(all_contacts.size(), ==, 4);
seq_sort(all_contacts, [](auto&& c1, auto&& c2){return c1.email < c2.email; });
assert_equal(all_contacts[0].email, "anon@example.com");
assert_equal(all_contacts[1].email, "sqlite-dev-bounces@sqlite.org");
assert_equal(all_contacts[2].email, "sqlite-dev@sqlite.org");
assert_equal(all_contacts[3].email, "sqlite-dev@sqlite.org");
}
static void
test_message_attachments(void)
{
constexpr const char* msg_text =
R"(Return-Path: <foo@example.com>
Received: from pop.gmail.com [256.85.129.309]
by evergrey with POP3 (fetchmail-6.4.29)
for <djcb@localhost> (single-drop); Thu, 24 Mar 2022 20:12:40 +0200 (EET)
Sender: "Foo, Example" <foo@example.com>
User-agent: mu4e 1.7.11; emacs 29.0.50
From: "Foo Example" <foo@example.com>
To: bar@example.com
Subject: =?utf-8?B?w6R0dMOkY2htZcOxdHM=?=
Date: Thu, 24 Mar 2022 20:04:39 +0200
Organization: ACME Inc.
Message-ID: <87a6dfw7bg.fsf@example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
--=-=-=
Content-Type: text/plain
Hello,
--=-=-=
Content-Type: image/jpeg
Content-Disposition: attachment; filename=file-01.bin
Content-Transfer-Encoding: base64
AAECAw==
--=-=-=
Content-Type: audio/ogg
Content-Disposition: inline; filename=/tmp/file-02.bin
Content-Transfer-Encoding: base64
BAUGBw==
--=-=-=
Content-Type: message/rfc822
Content-Disposition: attachment;
filename="message.eml"
From: "Fnorb" <fnorb@example.com>
To: Bob <bob@example.com>
Subject: news for you
Date: Mon, 28 Mar 2022 22:53:26 +0300
Attached message!
--=-=-=
Content-Type: text/plain
World!
--=-=-=--
)";
auto message{Message::make_from_string(msg_text)};
g_assert_true(!!message);
g_assert_true(message->bcc().empty());
g_assert_true(!message->body_html());
assert_equal(message->body_text().value_or(""), R"(Hello,World!)");
g_assert_true(message->cc().empty());
g_assert_cmpuint(message->date(), ==, 1648145079);
g_assert_true(message->flags() == (Flags::HasAttachment|Flags::Unread));
const auto from{message->from()};
g_assert_cmpuint(from.size(),==,1);
assert_equal(from.at(0).name, "Foo Example");
assert_equal(from.at(0).email, "foo@example.com");
g_assert_true(message->path().empty());
g_assert_true(message->priority() == Priority::Normal);
g_assert_cmpuint(message->size(),==,::strlen(msg_text));
assert_equal(message->subject(), "ättächmeñts");
g_assert_cmpuint(message->parts().size(),==,5);
{
auto&& part{message->parts().at(0)};
g_assert_false(!!part.raw_filename());
assert_equal(part.mime_type().value(), "text/plain");
assert_equal(part.to_string().value(), "Hello,");
}
{
auto&& part{message->parts().at(1)};
assert_equal(part.raw_filename().value(), "file-01.bin");
assert_equal(part.mime_type().value(), "image/jpeg");
// file consists of 4 bytes 0...3
g_assert_cmpuint(part.to_string()->at(0), ==, 0);
g_assert_cmpuint(part.to_string()->at(1), ==, 1);
g_assert_cmpuint(part.to_string()->at(2), ==, 2);
g_assert_cmpuint(part.to_string()->at(3), ==, 3);
}
{
auto&& part{message->parts().at(2)};
assert_equal(part.raw_filename().value(), "/tmp/file-02.bin");
assert_equal(part.cooked_filename().value(), "tmp-file-02.bin");
assert_equal(part.mime_type().value(), "audio/ogg");
// file consistso of 4 bytes 4..7
assert_equal(part.to_string().value(), "\004\005\006\007");
}
{
auto&& part{message->parts().at(3)};
g_assert_true(part.mime_type() == "message/rfc822");
}
{
auto&& part{message->parts().at(4)};
g_assert_false(!!part.raw_filename());
g_assert_true(!!part.mime_type());
assert_equal(part.mime_type().value(), "text/plain");
assert_equal(part.to_string().value(), "World!");
}
}
int
main(int argc, char* argv[])
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/message/message/mailing-list",
test_message_mailing_list);
g_test_add_func("/message/message/attachments",
test_message_attachments);
return g_test_run();
}
#endif /*BUILD_TESTS*/