2004-11-06 10:48:58 +00:00
|
|
|
//
|
2005-03-18 21:33:11 +00:00
|
|
|
// VMime library (http://www.vmime.org)
|
2005-01-03 12:26:48 +00:00
|
|
|
// Copyright (C) 2002-2005 Vincent Richard <vincent@vincent-richard.net>
|
2004-11-06 10:48:58 +00:00
|
|
|
//
|
|
|
|
// 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 2 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.
|
|
|
|
//
|
2005-09-17 10:10:29 +00:00
|
|
|
// 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.
|
2004-11-06 10:48:58 +00:00
|
|
|
//
|
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
#include "tests/testUtils.hpp"
|
2004-11-06 10:48:58 +00:00
|
|
|
|
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
#define VMIME_TEST_SUITE textTest
|
|
|
|
#define VMIME_TEST_SUITE_MODULE "Parser"
|
2004-11-06 10:48:58 +00:00
|
|
|
|
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VMIME_TEST_SUITE_BEGIN
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VMIME_TEST_LIST_BEGIN
|
|
|
|
VMIME_TEST(testConstructors)
|
|
|
|
VMIME_TEST(testCopy)
|
|
|
|
VMIME_TEST(testNewFromString)
|
|
|
|
VMIME_TEST(testDisplayForm)
|
|
|
|
VMIME_TEST(testParse)
|
|
|
|
VMIME_TEST(testGenerate)
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VMIME_TEST(testWordConstructors)
|
|
|
|
VMIME_TEST(testWordParse)
|
|
|
|
VMIME_TEST(testWordGenerate)
|
|
|
|
VMIME_TEST_LIST_END
|
|
|
|
|
|
|
|
|
|
|
|
void testConstructors()
|
2004-11-06 10:48:58 +00:00
|
|
|
{
|
2005-08-25 21:25:45 +00:00
|
|
|
vmime::text t1;
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("1.1", 0, t1.getWordCount());
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
vmime::text t2("Test\xa9\xc3");
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("2.1", 1, t2.getWordCount());
|
|
|
|
VASSERT_EQ("2.2", "Test\xa9\xc3", t2.getWordAt(0)->getBuffer());
|
|
|
|
VASSERT_EQ("2.3", vmime::charset::getLocaleCharset(), t2.getWordAt(0)->getCharset());
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
vmime::text t3("Test\xa9\xc3", vmime::charset(vmime::charsets::ISO8859_13));
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("3.1", 1, t3.getWordCount());
|
|
|
|
VASSERT_EQ("3.2", "Test\xa9\xc3", t3.getWordAt(0)->getBuffer());
|
|
|
|
VASSERT_EQ("3.3", vmime::charset(vmime::charsets::ISO8859_13), t3.getWordAt(0)->getCharset());
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
vmime::word w1("Test", vmime::charset(vmime::charsets::UTF_8));
|
|
|
|
vmime::text t4(w1);
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("4.1", 1, t4.getWordCount());
|
|
|
|
VASSERT_EQ("4.2", w1.getBuffer(), t4.getWordAt(0)->getBuffer());
|
|
|
|
VASSERT_EQ("4.3", w1.getCharset(), t4.getWordAt(0)->getCharset());
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
vmime::word w2("Other", vmime::charset(vmime::charsets::US_ASCII));
|
|
|
|
t4.appendWord(vmime::create <vmime::word>(w2));
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
vmime::text t5(t4);
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("5.1", 2, t5.getWordCount());
|
|
|
|
VASSERT_EQ("5.2", w1.getBuffer(), t5.getWordAt(0)->getBuffer());
|
|
|
|
VASSERT_EQ("5.3", w1.getCharset(), t5.getWordAt(0)->getCharset());
|
|
|
|
VASSERT_EQ("5.4", w2.getBuffer(), t5.getWordAt(1)->getBuffer());
|
|
|
|
VASSERT_EQ("5.5", w2.getCharset(), t5.getWordAt(1)->getCharset());
|
|
|
|
}
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
void testCopy()
|
|
|
|
{
|
|
|
|
vmime::text t1("Test: \xa9\xc3");
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT("operator==", t1 == t1);
|
|
|
|
VASSERT("clone", *vmime::clone(t1) == t1);
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
vmime::text t2;
|
|
|
|
t2.copyFrom(t1);
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT("copyFrom", t1 == t2);
|
|
|
|
}
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
void testNewFromString()
|
|
|
|
{
|
|
|
|
vmime::string s1 = "only ASCII characters";
|
|
|
|
vmime::charset c1("test");
|
|
|
|
vmime::text t1;
|
|
|
|
|
|
|
|
t1.createFromString(s1, c1);
|
|
|
|
|
|
|
|
VASSERT_EQ("1.1", 1, t1.getWordCount());
|
|
|
|
VASSERT_EQ("1.2", s1, t1.getWordAt(0)->getBuffer());
|
|
|
|
VASSERT_EQ("1.3", vmime::charset(vmime::charsets::US_ASCII), t1.getWordAt(0)->getCharset());
|
|
|
|
|
|
|
|
vmime::string s2_1 = "some ASCII characters and special chars: ";
|
|
|
|
vmime::string s2_2 = "\xf1\xf2\xf3\xf4 ";
|
|
|
|
vmime::string s2_3 = "and then more ASCII chars.";
|
|
|
|
vmime::string s2 = s2_1 + s2_2 + s2_3;
|
|
|
|
vmime::charset c2("test");
|
|
|
|
vmime::text t2;
|
|
|
|
|
|
|
|
t2.createFromString(s2, c2);
|
|
|
|
|
|
|
|
VASSERT_EQ("2.1", 3, t2.getWordCount());
|
|
|
|
VASSERT_EQ("2.2", s2_1, t2.getWordAt(0)->getBuffer());
|
|
|
|
VASSERT_EQ("2.3", vmime::charset(vmime::charsets::US_ASCII), t2.getWordAt(0)->getCharset());
|
|
|
|
VASSERT_EQ("2.4", s2_2, t2.getWordAt(1)->getBuffer());
|
|
|
|
VASSERT_EQ("2.5", c2, t2.getWordAt(1)->getCharset());
|
|
|
|
VASSERT_EQ("2.6", s2_3, t2.getWordAt(2)->getBuffer());
|
|
|
|
VASSERT_EQ("2.7", vmime::charset(vmime::charsets::US_ASCII), t2.getWordAt(2)->getCharset());
|
|
|
|
}
|
|
|
|
|
|
|
|
static const vmime::string parseText(const vmime::string& buffer)
|
|
|
|
{
|
|
|
|
vmime::text t;
|
|
|
|
t.parse(buffer);
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << t;
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
return (oss.str());
|
|
|
|
}
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
void testParse()
|
|
|
|
{
|
|
|
|
// From RFC-2047
|
|
|
|
VASSERT_EQ("1", "[text: [[word: charset=US-ASCII, buffer=Keith Moore]]]",
|
|
|
|
parseText("=?US-ASCII?Q?Keith_Moore?="));
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("2", "[text: [[word: charset=ISO-8859-1, buffer=Keld J\xf8rn Simonsen]]]",
|
|
|
|
parseText("=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?="));
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("3", "[text: [[word: charset=ISO-8859-1, buffer=Andr\xe9]," \
|
|
|
|
"[word: charset=us-ascii, buffer= Pirard]]]",
|
|
|
|
parseText("=?ISO-8859-1?Q?Andr=E9?= Pirard"));
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("4", "[text: [[word: charset=ISO-8859-1, buffer=If you can read this yo]," \
|
|
|
|
"[word: charset=ISO-8859-2, buffer=u understand the example.]]]",
|
|
|
|
parseText("=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\r\n " \
|
|
|
|
"=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?="));
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
// Bugfix: in "=?charset?q?=XX=YY?=", the "?=" finish
|
|
|
|
// sequence was not correctly found (should be the one
|
|
|
|
// after '=YY' and not the one after '?q').
|
|
|
|
VASSERT_EQ("5", "[text: [[word: charset=abc, buffer=\xe9\xe9]]]",
|
|
|
|
parseText("=?abc?q?=E9=E9?="));
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
// Question marks (?) in the middle of the string
|
|
|
|
VASSERT_EQ("6", "[text: [[word: charset=iso-8859-1, buffer=Know wh\xe4t? It works!]]]",
|
|
|
|
parseText("=?iso-8859-1?Q?Know_wh=E4t?_It_works!?="));
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
// TODO: add more
|
|
|
|
}
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
void testGenerate()
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
static const vmime::string getDisplayText(const vmime::text& t)
|
|
|
|
{
|
|
|
|
vmime::string res;
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
for (int i = 0 ; i < t.getWordCount() ; ++i)
|
|
|
|
res += t.getWordAt(i)->getBuffer();
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
return res;
|
|
|
|
}
|
2005-03-14 21:36:38 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
void testDisplayForm()
|
|
|
|
{
|
2005-03-14 21:36:38 +00:00
|
|
|
#define DISPLAY_FORM(x) getDisplayText(*vmime::text::decodeAndUnfold(x))
|
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
// From RFC-2047
|
|
|
|
VASSERT_EQ("1", "a", DISPLAY_FORM("=?ISO-8859-1?Q?a?="));
|
|
|
|
VASSERT_EQ("2", "a b", DISPLAY_FORM("=?ISO-8859-1?Q?a?= b"));
|
|
|
|
VASSERT_EQ("3", "ab", DISPLAY_FORM("=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="));
|
|
|
|
VASSERT_EQ("4", "ab", DISPLAY_FORM("=?ISO-8859-1?Q?a?= \t =?ISO-8859-1?Q?b?="));
|
|
|
|
VASSERT_EQ("5", "ab", DISPLAY_FORM("=?ISO-8859-1?Q?a?= \r\n \t =?ISO-8859-1?Q?b?="));
|
|
|
|
VASSERT_EQ("6", "a b", DISPLAY_FORM("=?ISO-8859-1?Q?a_b?="));
|
|
|
|
VASSERT_EQ("7", "a b", DISPLAY_FORM("=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?="));
|
2005-03-14 21:36:38 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
// Some more tests...
|
|
|
|
VASSERT_EQ("8", "a b", DISPLAY_FORM(" a =?ISO-8859-1?Q?b?= "));
|
|
|
|
VASSERT_EQ("9", "a b ", DISPLAY_FORM(" \t =?ISO-8859-1?Q?a?= b "));
|
|
|
|
VASSERT_EQ("10", "a b", DISPLAY_FORM(" a\r\n\t b"));
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-03-14 21:36:38 +00:00
|
|
|
#undef DISPLAY_FORM
|
2005-08-25 21:25:45 +00:00
|
|
|
}
|
2005-03-14 21:36:38 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
void testWordConstructors()
|
|
|
|
{
|
|
|
|
VASSERT_EQ("1.1", vmime::charset::getLocaleCharset(), vmime::word().getCharset());
|
|
|
|
VASSERT_EQ("1.2", "", vmime::word().getBuffer());
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("2.1", vmime::charset::getLocaleCharset(), vmime::word("foo").getCharset());
|
|
|
|
VASSERT_EQ("2.2", "foo", vmime::word("foo").getBuffer());
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("3.1", "bar", vmime::word("foo", vmime::charset("bar")).getCharset().getName());
|
|
|
|
VASSERT_EQ("3.2", "foo", vmime::word("foo", vmime::charset("bar")).getBuffer());
|
|
|
|
}
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
void testWordParse()
|
|
|
|
{
|
|
|
|
// Simple encoded word
|
|
|
|
vmime::word w1;
|
|
|
|
w1.parse("=?foo?q?bar=E9 baz?=");
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("1.1", "foo", w1.getCharset().getName());
|
|
|
|
VASSERT_EQ("1.2", "bar\xe9 baz", w1.getBuffer());
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
// Unencoded text
|
|
|
|
vmime::word w2;
|
|
|
|
w2.parse(" foo bar \tbaz...");
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("2.1", vmime::charset(vmime::charsets::US_ASCII), w2.getCharset());
|
|
|
|
VASSERT_EQ("2.2", " foo bar \tbaz...", w2.getBuffer());
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
// Malformed word
|
|
|
|
vmime::word w3;
|
|
|
|
w3.parse("=?foo bar");
|
2004-11-07 10:33:01 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("3.1", vmime::charset(vmime::charsets::US_ASCII), w3.getCharset());
|
|
|
|
VASSERT_EQ("3.2", "=?foo bar", w3.getBuffer());
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
// Unknown encoding
|
|
|
|
vmime::word w4;
|
|
|
|
w4.parse("=?whatever?not_q_or_b?whatever?=");
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("4.1", vmime::charset(vmime::charsets::US_ASCII), w4.getCharset());
|
|
|
|
VASSERT_EQ("4.2", "=?whatever?not_q_or_b?whatever?=", w4.getBuffer());
|
|
|
|
}
|
2005-03-15 10:32:52 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
void testWordGenerate()
|
|
|
|
{
|
|
|
|
VASSERT_EQ("1", "=?foo?Q?bar=E9_baz?=",
|
|
|
|
vmime::word("bar\xe9 baz", vmime::charset("foo")).generate());
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VASSERT_EQ("2", "=?foo?B?8fLz9PU=?=",
|
|
|
|
vmime::word("\xf1\xf2\xf3\xf4\xf5", vmime::charset("foo")).generate());
|
|
|
|
}
|
2004-11-06 10:48:58 +00:00
|
|
|
|
2005-08-25 21:25:45 +00:00
|
|
|
VMIME_TEST_SUITE_END
|
2004-11-06 10:48:58 +00:00
|
|
|
|