2004-11-06 10:48:58 +00:00
//
2005-03-18 21:33:11 +00:00
// VMime library (http://www.vmime.org)
2018-09-05 21:54:48 +00:00
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
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
2009-09-06 12:02:10 +00:00
// published by the Free Software Foundation; either version 3 of
2004-11-06 10:48:58 +00:00
// 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
2014-01-15 23:27:51 +00:00
# include <locale>
# include <clocale>
2004-11-06 10:48:58 +00:00
2013-03-08 07:19:50 +00:00
VMIME_TEST_SUITE_BEGIN ( textTest )
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 )
2007-11-20 21:45:54 +00:00
VMIME_TEST ( testWordGenerateSpace )
2008-04-28 19:49:48 +00:00
VMIME_TEST ( testWordGenerateSpace2 )
2006-10-02 13:44:00 +00:00
VMIME_TEST ( testWordGenerateMultiBytes )
2010-05-21 07:41:15 +00:00
VMIME_TEST ( testWordGenerateQuote )
VMIME_TEST ( testWordGenerateSpecialCharsets )
2010-10-12 09:45:16 +00:00
VMIME_TEST ( testWordGenerateSpecials )
2010-10-12 20:01:34 +00:00
VMIME_TEST ( testWhitespace )
VMIME_TEST ( testWhitespaceMBox )
2010-10-18 14:20:34 +00:00
VMIME_TEST ( testFoldingAscii )
2011-01-21 15:28:06 +00:00
VMIME_TEST ( testForcedNonEncoding )
2011-06-26 12:47:25 +00:00
VMIME_TEST ( testBugFix20110511 )
2013-02-24 15:28:13 +00:00
VMIME_TEST ( testInternationalizedEmail_specialChars )
VMIME_TEST ( testInternationalizedEmail_UTF8 )
VMIME_TEST ( testInternationalizedEmail_nonUTF8 )
VMIME_TEST ( testInternationalizedEmail_folding )
2020-06-16 17:47:33 +00:00
VMIME_TEST ( testInternationalizedEmail_whitespace )
2015-01-14 18:35:28 +00:00
VMIME_TEST ( testWronglyPaddedB64Words )
2015-02-16 17:43:03 +00:00
VMIME_TEST ( testFixBrokenWords )
2016-11-05 12:31:54 +00:00
VMIME_TEST ( testUnknownCharset )
2005-08-25 21:25:45 +00:00
VMIME_TEST_LIST_END
2018-09-05 21:54:48 +00:00
static const vmime : : string getDisplayText ( const vmime : : text & t ) {
2008-04-28 19:49:48 +00:00
return t . getWholeBuffer ( ) ;
2006-10-02 13:44:00 +00:00
}
2018-09-05 21:54:48 +00:00
static const vmime : : string cleanGeneratedWords ( const std : : string & str ) {
2006-10-02 13:44:00 +00:00
std : : istringstream iss ( str ) ;
std : : string res ;
std : : string x ;
2018-09-05 21:54:48 +00:00
while ( std : : getline ( iss , x ) ) {
2006-10-02 13:44:00 +00:00
res + = vmime : : utility : : stringUtils : : trim ( x ) ;
2018-09-05 21:54:48 +00:00
}
2006-10-02 13:44:00 +00:00
return res ;
}
2018-09-05 21:54:48 +00:00
void setUp ( ) {
2014-01-15 23:27:51 +00:00
// Set the global C and C++ locale to the user-configured locale.
// The locale should use UTF-8 encoding for these tests to run successfully.
2018-09-05 21:54:48 +00:00
try {
2014-01-15 23:27:51 +00:00
std : : locale : : global ( std : : locale ( " " ) ) ;
2018-09-05 21:54:48 +00:00
} catch ( std : : exception & ) {
2014-01-15 23:27:51 +00:00
std : : setlocale ( LC_ALL , " " ) ;
}
}
2018-09-05 21:54:48 +00:00
void tearDown ( ) {
2014-01-15 23:27:51 +00:00
// Restore default locale
std : : locale : : global ( std : : locale ( " C " ) ) ;
}
2018-09-05 21:54:48 +00:00
void testConstructors ( ) {
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 ( ) ) ;
2013-02-25 12:10:15 +00:00
VASSERT_EQ ( " 2.3 " , vmime : : charset : : getLocalCharset ( ) , 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 ) ) ;
2013-11-21 21:16:57 +00:00
t4 . appendWord ( vmime : : make_shared < 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
2018-09-05 21:54:48 +00:00
void testCopy ( ) {
2005-08-25 21:25:45 +00:00
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
2018-09-05 21:54:48 +00:00
void testNewFromString ( ) {
2005-08-25 21:25:45 +00:00
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 ( ) ) ;
2010-10-12 20:01:34 +00:00
VASSERT_EQ ( " 2.2 " , " some ASCII characters and special chars: " , t2 . getWordAt ( 0 ) - > getBuffer ( ) ) ;
2005-08-25 21:25:45 +00:00
VASSERT_EQ ( " 2.3 " , vmime : : charset ( vmime : : charsets : : US_ASCII ) , t2 . getWordAt ( 0 ) - > getCharset ( ) ) ;
2011-06-26 12:47:25 +00:00
VASSERT_EQ ( " 2.4 " , " \xf1 \xf2 \xf3 \xf4 " , t2 . getWordAt ( 1 ) - > getBuffer ( ) ) ;
2005-08-25 21:25:45 +00:00
VASSERT_EQ ( " 2.5 " , c2 , t2 . getWordAt ( 1 ) - > getCharset ( ) ) ;
2008-04-28 19:49:48 +00:00
VASSERT_EQ ( " 2.6 " , " and then more ASCII chars. " , t2 . getWordAt ( 2 ) - > getBuffer ( ) ) ;
2005-08-25 21:25:45 +00:00
VASSERT_EQ ( " 2.7 " , vmime : : charset ( vmime : : charsets : : US_ASCII ) , t2 . getWordAt ( 2 ) - > getCharset ( ) ) ;
}
2018-09-05 21:54:48 +00:00
static const vmime : : string parseText ( const vmime : : string & buffer ) {
2005-08-25 21:25:45 +00:00
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
2018-09-05 21:54:48 +00:00
return oss . str ( ) ;
2005-08-25 21:25:45 +00:00
}
2004-11-06 10:48:58 +00:00
2018-09-05 21:54:48 +00:00
void testParse ( ) {
2004-11-06 10:48:58 +00:00
2018-09-05 21:54:48 +00:00
// From RFC-2047
VASSERT_EQ (
" 1 " ,
" [text: [[word: charset=US-ASCII, buffer=Keith Moore]]] " ,
parseText ( " =?US-ASCII?Q?Keith_Moore?= " )
) ;
VASSERT_EQ (
" 2 " ,
" [text: [[word: charset=ISO-8859-1, buffer=Keld J \xf8 rn Simonsen]]] " ,
parseText ( " =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= " )
) ;
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 " )
) ;
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').
2018-09-05 21:54:48 +00:00
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
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 6 " ,
" [text: [[word: charset=iso-8859-1, buffer=Know wh \xe4 t? It works!]]] " ,
parseText ( " =?iso-8859-1?Q?Know_wh=E4t?_It_works!?= " )
) ;
2005-03-15 10:32:52 +00:00
2014-06-30 20:48:42 +00:00
// With language specifier
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 7 " ,
" [text: [[word: charset=US-ASCII, buffer=Keith Moore, lang=EN]]] " ,
parseText ( " =?US-ASCII*EN?Q?Keith_Moore?= " )
) ;
2005-08-25 21:25:45 +00:00
}
2005-03-15 10:32:52 +00:00
2018-09-05 21:54:48 +00:00
void testGenerate ( ) {
2005-08-25 21:25:45 +00:00
// TODO
2014-06-30 20:48:42 +00:00
// With language specifier
vmime : : word wlang1 ( " Émeline " , vmime : : charset ( " UTF-8 " ) , " FR " ) ;
VASSERT_EQ ( " lang1 " , " =?UTF-8*FR?Q?=C3=89meline?= " , wlang1 . generate ( ) ) ;
vmime : : word wlang2 ( " Keith Moore " , vmime : : charset ( " US-ASCII " ) , " EN " ) ;
VASSERT_EQ ( " lang2 " , " =?US-ASCII*EN?Q?Keith_Moore?= " , wlang2 . generate ( ) ) ;
2005-08-25 21:25:45 +00:00
}
2005-03-15 10:32:52 +00:00
2018-09-05 21:54:48 +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
2008-04-28 19:49:48 +00:00
VASSERT_EQ ( " 11 " , " a b c " , DISPLAY_FORM ( " a =?ISO-8859-1?Q?b?= c " ) ) ;
VASSERT_EQ ( " 12 " , " a b c " , DISPLAY_FORM ( " a =?ISO-8859-1?Q?b?= c " ) ) ;
VASSERT_EQ ( " 13 " , " a b c " , DISPLAY_FORM ( " a =?ISO-8859-1?Q?b?= c " ) ) ;
VASSERT_EQ ( " 14 " , " a b c d " , DISPLAY_FORM ( " a =?ISO-8859-1?Q?b?= c =?ISO-8859-1?Q?d?= " ) ) ;
VASSERT_EQ ( " 15 " , " a b c d e " , DISPLAY_FORM ( " a =?ISO-8859-1?Q?b?= c =?ISO-8859-1?Q?d?= e " ) ) ;
// Whitespaces and multiline
VASSERT_EQ ( " 16 " , " a b c d e " , DISPLAY_FORM ( " =?ISO-8859-1?Q?a_b_?=c \n \t =?ISO-8859-1?Q?d_?=e " ) ) ;
2017-01-02 20:40:38 +00:00
// Ignored newlines
VASSERT_EQ ( " 17 " , " ab " , DISPLAY_FORM ( " =?ISO-8859-1?Q?a?= \r \n b " ) ) ;
VASSERT_EQ ( " 18 " , " a b " , DISPLAY_FORM ( " =?ISO-8859-1?Q?a?= \r \n b " ) ) ;
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
2018-09-05 21:54:48 +00:00
void testWordConstructors ( ) {
2013-02-25 12:10:15 +00:00
VASSERT_EQ ( " 1.1 " , vmime : : charset : : getLocalCharset ( ) , vmime : : word ( ) . getCharset ( ) ) ;
2005-08-25 21:25:45 +00:00
VASSERT_EQ ( " 1.2 " , " " , vmime : : word ( ) . getBuffer ( ) ) ;
2005-03-15 10:32:52 +00:00
2013-02-25 12:10:15 +00:00
VASSERT_EQ ( " 2.1 " , vmime : : charset : : getLocalCharset ( ) , vmime : : word ( " foo " ) . getCharset ( ) ) ;
2005-08-25 21:25:45 +00:00
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
2018-09-05 21:54:48 +00:00
void testWordParse ( ) {
2005-08-25 21:25:45 +00:00
// 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 \t baz... " ) ;
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 \t baz... " , 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
2018-09-05 21:54:48 +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
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 2 " ,
" =?foo?B?8fLz9PU=?= " ,
vmime : : word ( " \xf1 \xf2 \xf3 \xf4 \xf5 " , vmime : : charset ( " foo " ) ) . generate ( )
) ;
2005-08-25 21:25:45 +00:00
}
2004-11-06 10:48:58 +00:00
2018-09-05 21:54:48 +00:00
void testWordGenerateSpace ( ) {
2007-11-20 21:45:54 +00:00
// No white-space between an unencoded word and a encoded one
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " ,
" Bonjour =?utf-8?Q?Fran=C3=A7ois?= " ,
vmime : : text : : newFromString (
" Bonjour Fran \xc3 \xa7 ois " ,
vmime : : charset ( " utf-8 " )
) - > generate ( )
) ;
2007-11-20 21:45:54 +00:00
// White-space between two encoded words
vmime : : text txt ;
2013-11-21 21:16:57 +00:00
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " \xc3 \x89 t \xc3 \xa9 " , " utf-8 " ) ) ;
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " Fran \xc3 \xa7 ois " , " utf-8 " ) ) ;
2007-11-20 21:45:54 +00:00
const vmime : : string decoded = " \xc3 \x89 t \xc3 \xa9 " " Fran \xc3 \xa7 ois " ;
const vmime : : string encoded = " =?utf-8?B?w4l0w6k=?= =?utf-8?Q?Fran=C3=A7ois?= " ;
// -- test encoding
VASSERT_EQ ( " 2 " , encoded , txt . generate ( ) ) ;
// -- ensure no space is added when decoding
vmime : : text txt2 ;
txt2 . parse ( encoded , 0 , encoded . length ( ) ) ;
VASSERT_EQ ( " 3 " , decoded , txt2 . getWholeBuffer ( ) ) ;
2008-04-28 19:49:48 +00:00
// -- test rencoding
VASSERT_EQ ( " 4 " , encoded , txt2 . generate ( ) ) ;
}
2018-09-05 21:54:48 +00:00
void testWordGenerateSpace2 ( ) {
2008-04-28 19:49:48 +00:00
// White-space between two encoded words (#2)
vmime : : text txt ;
2013-11-21 21:16:57 +00:00
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " Facture " , " utf-8 " ) ) ;
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " \xc3 \xa0 " , " utf-8 " ) ) ;
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " envoyer " , " utf-8 " ) ) ;
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " \xc3 \xa0 " , " utf-8 " ) ) ;
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " Martine " , " utf-8 " ) ) ;
2008-04-28 19:49:48 +00:00
const vmime : : string decoded = " Facture " " \xc3 \xa0 " " envoyer " " \xc3 \xa0 " " Martine " ;
const vmime : : string encoded = " Facture =?utf-8?B?w6A=?= envoyer =?utf-8?B?w6A=?= Martine " ;
// -- test encoding
VASSERT_EQ ( " 1 " , encoded , txt . generate ( ) ) ;
// -- ensure no space is added when decoding
vmime : : text txt2 ;
txt2 . parse ( encoded , 0 , encoded . length ( ) ) ;
VASSERT_EQ ( " 2 " , decoded , txt2 . getWholeBuffer ( ) ) ;
// -- test rencoding
VASSERT_EQ ( " 3 " , encoded , txt2 . generate ( ) ) ;
2007-11-20 21:45:54 +00:00
}
2018-09-05 21:54:48 +00:00
void testWordGenerateMultiBytes ( ) {
2006-10-02 13:44:00 +00:00
// Ensure we don't encode a non-integral number of characters
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " ,
" =?utf-8?Q?aaa?==?utf-8?Q?=C3=A9?==?utf-8?Q?zzz?= " ,
cleanGeneratedWords (
vmime : : word ( " aaa \xc3 \xa9 zzz " , vmime : : charset ( " utf-8 " ) ) . generate ( 16 )
)
) ;
2006-10-02 13:44:00 +00:00
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 2 " ,
" =?utf-8?Q?aaa=C3=A9?==?utf-8?Q?zzz?= " ,
cleanGeneratedWords (
vmime : : word ( " aaa \xc3 \xa9 zzz " , vmime : : charset ( " utf-8 " ) ) . generate ( 17 )
)
) ;
2006-10-02 13:44:00 +00:00
}
2018-09-05 21:54:48 +00:00
void testWordGenerateQuote ( ) {
2010-05-21 07:41:15 +00:00
std : : string str ;
vmime : : utility : : outputStreamStringAdapter os ( str ) ;
2013-02-24 15:28:13 +00:00
vmime : : generationContext ctx ;
ctx . setMaxLineLength ( 1000 ) ;
2010-05-21 07:41:15 +00:00
// ASCII-only text is quotable
str . clear ( ) ;
2018-09-05 21:54:48 +00:00
vmime : : word ( " Quoted text " )
. generate ( ctx , os , 0 , NULL , vmime : : text : : QUOTE_IF_POSSIBLE , NULL ) ;
2010-05-21 07:41:15 +00:00
VASSERT_EQ ( " 1 " , " \" Quoted text \" " , cleanGeneratedWords ( str ) ) ;
// Text with CR/LF is not quotable
str . clear ( ) ;
2018-09-05 21:54:48 +00:00
vmime : : word ( " Non-quotable \n text " , " us-ascii " )
. generate ( ctx , os , 0 , NULL , vmime : : text : : QUOTE_IF_POSSIBLE , NULL ) ;
2010-05-21 07:41:15 +00:00
VASSERT_EQ ( " 2 " , " =?us-ascii?Q?Non-quotable=0Atext?= " , cleanGeneratedWords ( str ) ) ;
// Text with non-ASCII chars is not quotable
str . clear ( ) ;
2018-09-05 21:54:48 +00:00
vmime : : word ( " Non-quotable text \xc3 \xa9 " )
. generate ( ctx , os , 0 , NULL , vmime : : text : : QUOTE_IF_POSSIBLE , NULL ) ;
2010-05-21 07:41:15 +00:00
VASSERT_EQ ( " 3 " , " =?UTF-8?Q?Non-quotable_text_=C3=A9?= " , cleanGeneratedWords ( str ) ) ;
}
2018-09-05 21:54:48 +00:00
void testWordGenerateSpecialCharsets ( ) {
2010-05-21 07:41:15 +00:00
// ISO-2022-JP only uses 7-bit chars but should be encoded in Base64
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " ,
" =?iso-2022-jp?B?XlskQiVRITwlPSVKJWshJiU9JVUlSCUmJSclIl5bKEI=?= " ,
cleanGeneratedWords (
vmime : : word (
" ^[$B%Q!<%=%J%k!&%=%U%H%&%'% \" ^[(B " ,
vmime : : charset ( " iso-2022-jp " )
) . generate ( 100 )
)
) ;
2010-05-21 07:41:15 +00:00
}
2018-09-05 21:54:48 +00:00
void testWordGenerateSpecials ( ) {
2010-10-12 09:45:16 +00:00
// In RFC-2047, quotation marks (ASCII 22h) should be encoded
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " ,
" =?UTF-8?Q?=22=C3=9Cml=C3=A4ute=22?= " ,
vmime : : word (
" \x22 \xC3 \x9C ml \xC3 \xA4 ute \x22 " ,
vmime : : charset ( " UTF-8 " )
) . generate ( )
) ;
2010-10-12 09:45:16 +00:00
}
2018-09-05 21:54:48 +00:00
void testWhitespace ( ) {
2010-10-12 20:01:34 +00:00
// Create
vmime : : text text ;
text . createFromString ( " Achim Br \xc3 \xa4 ndt " , vmime : : charsets : : UTF_8 ) ;
VASSERT_EQ ( " 1 " , 2 , text . getWordCount ( ) ) ;
VASSERT_EQ ( " 2 " , " Achim " , text . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 3 " , " us-ascii " , text . getWordAt ( 0 ) - > getCharset ( ) ) ;
VASSERT_EQ ( " 4 " , " Br \xc3 \xa4 ndt " , text . getWordAt ( 1 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 5 " , " utf-8 " , text . getWordAt ( 1 ) - > getCharset ( ) ) ;
// Generate
VASSERT_EQ ( " 6 " , " Achim =?utf-8?Q?Br=C3=A4ndt?= " , text . generate ( ) ) ;
// Parse
text . parse ( " =?us-ascii?Q?Achim_?= =?utf-8?Q?Br=C3=A4ndt?= " ) ;
VASSERT_EQ ( " 7 " , 2 , text . getWordCount ( ) ) ;
VASSERT_EQ ( " 8 " , " Achim " , text . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 9 " , " us-ascii " , text . getWordAt ( 0 ) - > getCharset ( ) ) ;
VASSERT_EQ ( " 10 " , " Br \xc3 \xa4 ndt " , text . getWordAt ( 1 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 11 " , " utf-8 " , text . getWordAt ( 1 ) - > getCharset ( ) ) ;
}
2018-09-05 21:54:48 +00:00
void testWhitespaceMBox ( ) {
2010-10-12 20:01:34 +00:00
// Space MUST be encoded inside a word
vmime : : mailbox mbox ( vmime : : text ( " Achim Br \xc3 \xa4 ndt " , vmime : : charsets : : UTF_8 ) , " me@vmime.org " ) ;
VASSERT_EQ ( " generate1 " , " =?us-ascii?Q?Achim_?= =?utf-8?Q?Br=C3=A4ndt?= <me@vmime.org> " , mbox . generate ( ) ) ;
vmime : : text txt ;
2013-11-21 21:16:57 +00:00
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " Achim " , " us-ascii " ) ) ;
txt . appendWord ( vmime : : make_shared < vmime : : word > ( " Br \xc3 \xa4 ndt " , " utf-8 " ) ) ;
2010-10-12 20:01:34 +00:00
mbox = vmime : : mailbox ( txt , " me@vmime.org " ) ;
VASSERT_EQ ( " generate2 " , " =?us-ascii?Q?Achim_?= =?utf-8?Q?Br=C3=A4ndt?= <me@vmime.org> " , mbox . generate ( ) ) ;
mbox . parse ( " =?us-ascii?Q?Achim?= =?utf-8?Q?Br=C3=A4ndt?= <me@vmime.org> " ) ;
VASSERT_EQ ( " parse.name.count " , 2 , mbox . getName ( ) . getWordCount ( ) ) ;
VASSERT_EQ ( " parse.name.word1.buffer " , " Achim " , mbox . getName ( ) . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " parse.name.word1.charset " , " us-ascii " , mbox . getName ( ) . getWordAt ( 0 ) - > getCharset ( ) ) ;
VASSERT_EQ ( " parse.name.word2.buffer " , " Br \xc3 \xa4 ndt " , mbox . getName ( ) . getWordAt ( 1 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " parse.name.word2.charset " , " utf-8 " , mbox . getName ( ) . getWordAt ( 1 ) - > getCharset ( ) ) ;
VASSERT_EQ ( " parse.email " , " me@vmime.org " , mbox . getEmail ( ) ) ;
}
2018-09-05 21:54:48 +00:00
void testFoldingAscii ( ) {
2010-10-18 14:20:34 +00:00
// In this test, no encoding is needed, but line should be folded anyway
vmime : : word w ( " 01234567890123456789012345678901234567890123456789 "
" 01234567890123456789012345678901234567890123456789 " , vmime : : charset ( " us-ascii " ) ) ;
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" fold.ascii " ,
2010-10-18 14:20:34 +00:00
" =?us-ascii?Q?01234567890123456789012345678901234?= \r \n "
" =?us-ascii?Q?5678901234567890123456789012345678?= \r \n "
2018-09-05 21:54:48 +00:00
" =?us-ascii?Q?9012345678901234567890123456789?= " , w . generate ( 50 )
) ;
2010-10-18 14:20:34 +00:00
}
2018-09-05 21:54:48 +00:00
void testForcedNonEncoding ( ) {
2011-01-21 15:28:06 +00:00
// Testing long unbreakable and unencodable header
vmime : : relay r ;
2018-09-05 21:54:48 +00:00
r . parse (
" from User (Ee9GMqZQ8t7IQwftfAFHd2KyScCYRrFSJ50tKEoXv2bVCG4HcPU80GGWiFabAvG77FekpGgF1h@[127.0.0.1]) by servername.hostname.com \n \t "
" with esmtp id 1NGTS9-2C0sqG0; Fri, 4 Dec 2009 09:23:49 +0100 "
) ;
2011-01-21 15:28:06 +00:00
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" received.long " ,
" from User \r \n (Ee9GMqZQ8t7IQwftfAFHd2KyScCYRrFSJ50tKEoXv2bVCG4HcPU80GGWiFabAvG77FekpGgF1h@[127.0.0.1]) \r \n by servername.hostname.com with esmtp id 1NGTS9-2C0sqG0; Fri, 4 Dec 2009 \r \n 09:23:49 +0100 " ,
r . generate ( 78 )
) ;
2011-01-21 15:28:06 +00:00
}
2018-09-05 21:54:48 +00:00
void testBugFix20110511 ( ) {
2011-06-26 12:47:25 +00:00
/*
Using the latest version of vmime ( 0.9 .1 ) , encoding the following string : Jean
Gwenaël Dutourd will result in :
Jean = ? utf - 8 ? Q ? Gwena = C3 = ABl_ ? = Dutourd
However , decoding this will result in Jean Gwenaël Dutourd ( notice two spaces
between the last 2 words ) . The encoder adds a _ after the second word , but
since the last word is not encoded , the space between them is not ignored , and
is decoded into an additional space .
See : http : //sourceforge.net/projects/vmime/forums/forum/237357/topic/4531365
*/
const std : : string DECODED_TEXT = " Jean Gwenaël Dutourd " ;
const std : : string ENCODED_TEXT = " Jean =?utf-8?Q?Gwena=C3=ABl?= Dutourd " ;
// Encode
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" encode " ,
ENCODED_TEXT ,
vmime : : text : : newFromString ( DECODED_TEXT , vmime : : charset ( " utf-8 " ) ) - > generate ( )
) ;
2011-06-26 12:47:25 +00:00
// Decode
vmime : : text t ;
t . parse ( ENCODED_TEXT ) ;
// -- words
std : : ostringstream oss ; oss < < t ;
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" decode1 " ,
2011-06-26 12:47:25 +00:00
" [text: [[word: charset=us-ascii, buffer=Jean ], "
" [word: charset=utf-8, buffer=Gwenaël], "
2018-09-05 21:54:48 +00:00
" [word: charset=us-ascii, buffer= Dutourd]]] " ,
oss . str ( )
) ;
2011-06-26 12:47:25 +00:00
// -- getWholeBuffer
VASSERT_EQ ( " decode2 " , DECODED_TEXT , t . getWholeBuffer ( ) ) ;
}
2018-09-05 21:54:48 +00:00
void testInternationalizedEmail_specialChars ( ) {
2013-02-24 15:28:13 +00:00
vmime : : generationContext ctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
ctx . setInternationalizedEmailSupport ( true ) ;
vmime : : generationContext : : switcher < vmime : : generationContext > contextSwitcher ( ctx ) ;
// Special sequence/chars should still be encoded
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " ,
" =?us-ascii?Q?Test=3D=3Frfc2047_sequence?= " ,
vmime : : word ( " Test=?rfc2047 sequence " , vmime : : charset ( " us-ascii " ) ) . generate ( )
) ;
2013-02-24 15:28:13 +00:00
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 2 " ,
" =?us-ascii?Q?Line_One=0ALine_Two?= " ,
vmime : : word ( " Line One \n Line Two " , vmime : : charset ( " us-ascii " ) ) . generate ( )
) ;
2013-02-24 15:28:13 +00:00
}
2018-09-05 21:54:48 +00:00
void testInternationalizedEmail_UTF8 ( ) {
2013-02-24 15:28:13 +00:00
vmime : : generationContext ctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
ctx . setInternationalizedEmailSupport ( true ) ;
vmime : : generationContext : : switcher < vmime : : generationContext > contextSwitcher ( ctx ) ;
// Already UTF-8 encoded text should be left as is
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " , " Achim Br \xc3 \xa4 ndt " ,
vmime : : word ( " Achim Br \xc3 \xa4 ndt " , vmime : : charset ( " utf-8 " ) ) . generate ( )
) ;
2013-02-24 15:28:13 +00:00
}
2018-09-05 21:54:48 +00:00
void testInternationalizedEmail_nonUTF8 ( ) {
2013-02-24 15:28:13 +00:00
vmime : : generationContext ctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
ctx . setInternationalizedEmailSupport ( true ) ;
vmime : : generationContext : : switcher < vmime : : generationContext > contextSwitcher ( ctx ) ;
// Non UTF-8 encoded text should first be converted to UTF-8
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " , " Achim Br \xc3 \xa4 ndt " ,
vmime : : word ( " Achim Br \xe4 ndt " , vmime : : charset ( " iso-8859-1 " ) ) . generate ( )
) ;
2013-02-24 15:28:13 +00:00
}
2018-09-05 21:54:48 +00:00
void testInternationalizedEmail_folding ( ) {
2013-02-24 15:28:13 +00:00
vmime : : generationContext ctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
ctx . setInternationalizedEmailSupport ( true ) ;
vmime : : generationContext : : switcher < vmime : : generationContext > contextSwitcher ( ctx ) ;
// RFC-2047 encoding must be performed, as line folding is needed
vmime : : word w1 ( " 01234567890123456789 \xc3 \xa0 x012345678901234567890123456789 "
" 01234567890123456789 \xc3 \xa0 x012345678901234567890123456789 " , vmime : : charset ( " utf-8 " ) ) ;
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " ,
2013-02-24 15:28:13 +00:00
" =?utf-8?Q?01234567890123456789=C3=A0x01234567890?= \r \n "
" =?utf-8?Q?1234567890123456789012345678901234567?= \r \n "
" =?utf-8?Q?89=C3=A0x0123456789012345678901234567?= \r \n "
2018-09-05 21:54:48 +00:00
" =?utf-8?Q?89?= " ,
w1 . generate ( 50 )
) ;
2013-02-24 15:28:13 +00:00
// RFC-2047 encoding will not be forced, as words can be wrapped in a new line
vmime : : word w2 ( " bla bla bla This is some ' \xc3 \xa0 \xc3 \xa7 ' UTF-8 encoded text " , vmime : : charset ( " utf-8 " ) ) ;
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 2 " ,
2013-02-24 15:28:13 +00:00
" bla bla bla This is \r \n "
" some ' \xc3 \xa0 \xc3 \xa7 ' UTF-8 \r \n "
2018-09-05 21:54:48 +00:00
" encoded text " ,
w2 . generate ( 20 )
) ;
2013-02-24 15:28:13 +00:00
}
2020-06-16 17:47:33 +00:00
void testInternationalizedEmail_whitespace ( ) {
// Sanity checks for running this test
{
vmime : : text t ;
t . parse ( " =?utf-8?Q?Adquisi=C3=B3n?= de Laptop y celular " ) ;
VASSERT_EQ ( " parse " , " Adquisión de Laptop y celular " , t . getWholeBuffer ( ) ) ;
}
{
vmime : : text t ( " Adquisi \xc3 \xb3 n de Laptop y celular " , vmime : : charset ( " UTF-8 " ) ) ;
VASSERT_EQ ( " generate " , " =?UTF-8?Q?Adquisi=C3=B3n?= de Laptop y celular " , t . generate ( ) ) ;
}
// Ensure a whitespace is added between encoded words in intl email support enabled
{
vmime : : text t ;
t . parse ( " =?utf-8?Q?Adquisi=C3=B3n?= de Laptop y celular " ) ;
std : : ostringstream oss ;
vmime : : utility : : outputStreamAdapter ossAdapter ( oss ) ;
vmime : : generationContext gctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
gctx . setInternationalizedEmailSupport ( true ) ;
t . generate ( gctx , ossAdapter ) ;
VASSERT_EQ ( " generate " , " Adquisi \xc3 \xb3 n de Laptop y celular " , oss . str ( ) ) ;
}
{
vmime : : text t ;
t . parse ( " =?utf-8?Q?Adquisi=C3=B3n?= de Laptop =?utf-8?Q?y?= celular " ) ;
std : : ostringstream oss ;
vmime : : utility : : outputStreamAdapter ossAdapter ( oss ) ;
vmime : : generationContext gctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
gctx . setInternationalizedEmailSupport ( true ) ;
t . generate ( gctx , ossAdapter ) ;
VASSERT_EQ ( " generate " , " Adquisi \xc3 \xb3 n de Laptop y celular " , oss . str ( ) ) ;
}
{
vmime : : text t ;
t . parse ( " =?utf-8?Q?Adquisi=C3=B3n?= de Laptop =?utf-8?Q?y_celular?= " ) ;
std : : ostringstream oss ;
vmime : : utility : : outputStreamAdapter ossAdapter ( oss ) ;
vmime : : generationContext gctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
gctx . setInternationalizedEmailSupport ( true ) ;
t . generate ( gctx , ossAdapter ) ;
VASSERT_EQ ( " generate " , " Adquisi \xc3 \xb3 n de Laptop y celular " , oss . str ( ) ) ;
}
// Ensure no whitespace is added with non-encoded words
{
vmime : : text t ;
t . parse ( " Laptop y celular " ) ;
std : : ostringstream oss ;
vmime : : utility : : outputStreamAdapter ossAdapter ( oss ) ;
vmime : : generationContext gctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
gctx . setInternationalizedEmailSupport ( true ) ;
t . generate ( gctx , ossAdapter ) ;
VASSERT_EQ ( " generate " , " Laptop y celular " , oss . str ( ) ) ;
}
{
vmime : : text t ;
t . parse ( " =?utf-8?Q?Laptop_y_celular?= " ) ;
std : : ostringstream oss ;
vmime : : utility : : outputStreamAdapter ossAdapter ( oss ) ;
vmime : : generationContext gctx ( vmime : : generationContext : : getDefaultContext ( ) ) ;
gctx . setInternationalizedEmailSupport ( true ) ;
t . generate ( gctx , ossAdapter ) ;
VASSERT_EQ ( " generate " , " Laptop y celular " , oss . str ( ) ) ;
}
}
2018-09-05 21:54:48 +00:00
void testWronglyPaddedB64Words ( ) {
2015-01-14 18:35:28 +00:00
vmime : : text outText ;
vmime : : text : : decodeAndUnfold ( " =?utf-8?B?5Lit5?= \n =?utf-8?B?paH?= " , & outText ) ;
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 1 " ,
" \xe4 \xb8 \xad \xe6 \x96 \x87 " ,
outText . getConvertedText ( vmime : : charset ( " utf-8 " ) )
) ;
2015-01-14 18:35:28 +00:00
vmime : : text : : decodeAndUnfold ( " =?utf-8?B?5Lit5p?= \n =?utf-8?B?aH?= " , & outText ) ;
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 2 " ,
" \xe4 \xb8 \xad \xe6 \x96 \x87 " ,
outText . getConvertedText ( vmime : : charset ( " utf-8 " ) )
) ;
2015-01-14 18:35:28 +00:00
vmime : : text : : decodeAndUnfold ( " =?utf-8?B?5Lit5pa?= \n =?utf-8?B?H?= " , & outText ) ;
2018-09-05 21:54:48 +00:00
VASSERT_EQ (
" 3 " ,
" \xe4 \xb8 \xad \xe6 \x96 \x87 " ,
outText . getConvertedText ( vmime : : charset ( " utf-8 " ) )
) ;
2015-01-14 18:35:28 +00:00
}
2015-02-16 17:43:03 +00:00
// Ensure that words which encode a non-integral number of characters
// are correctly decoded.
2018-09-05 21:54:48 +00:00
void testFixBrokenWords ( ) {
2015-02-16 17:43:03 +00:00
vmime : : text outText ;
vmime : : charsetConverterOptions opts ;
opts . silentlyReplaceInvalidSequences = false ; // just to be sure that broken words are actually fixed
// Test case 1
2018-09-05 21:54:48 +00:00
vmime : : text : : decodeAndUnfold (
" =?utf-8?Q?Gwena=C3?= "
" =?utf-8?Q?=ABl?= " ,
& outText
) ;
2015-02-16 17:43:03 +00:00
2016-11-05 12:31:54 +00:00
VASSERT_EQ ( " 1.1 " , 1 , outText . getWordCount ( ) ) ;
VASSERT_EQ ( " 1.2 " , " Gwena \xc3 \xab l " , outText . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 1.3 " , vmime : : charset ( " utf-8 " ) , outText . getWordAt ( 0 ) - > getCharset ( ) ) ;
2015-02-16 17:43:03 +00:00
// Test case 2
2018-09-05 21:54:48 +00:00
vmime : : text : : decodeAndUnfold (
" =?utf-8?B?5Lit6Yu85qmf5qKw6JGj5LqL5pyDMTAz5bm056ysMDXlsYbn?= "
" =?utf-8?B?rKwwN+asoeitsOeoiw==?= " ,
& outText
) ;
2015-02-16 17:43:03 +00:00
2016-11-05 12:31:54 +00:00
VASSERT_EQ ( " 2.1 " , 1 , outText . getWordCount ( ) ) ;
VASSERT_EQ ( " 2.2 " , " \xe4 \xb8 \xad \xe9 \x8b \xbc \xe6 \xa9 \x9f \xe6 \xa2 \xb0 "
2015-02-16 17:43:03 +00:00
" \xe8 \x91 \xa3 \xe4 \xba \x8b \xe6 \x9c \x83 \x31 \x30 \x33 \xe5 \xb9 \xb4 "
" \xe7 \xac \xac \x30 \x35 \xe5 \xb1 \x86 \xe7 \xac \xac \x30 \x37 \xe6 \xac "
2016-11-05 12:31:54 +00:00
" \xa1 \xe8 \xad \xb0 \xe7 \xa8 \x8b " , outText . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 2.3 " , vmime : : charset ( " utf-8 " ) , outText . getWordAt ( 0 ) - > getCharset ( ) ) ;
2015-02-16 17:43:03 +00:00
// Test case 3 (a character spanning over 3 words: 'を' = E3 82 92)
2018-09-05 21:54:48 +00:00
vmime : : text : : decodeAndUnfold (
" =?utf-8?Q?abc=E3?= "
" =?utf-8?Q?=82?= "
" =?utf-8?Q?=92xyz?= " ,
& outText
) ;
2015-02-16 17:43:03 +00:00
2016-11-05 12:31:54 +00:00
VASSERT_EQ ( " 3.1 " , 1 , outText . getWordCount ( ) ) ;
VASSERT_EQ ( " 3.2 " , " abc \xe3 \x82 \x92 xyz " , outText . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 3.3 " , vmime : : charset ( " utf-8 " ) , outText . getWordAt ( 0 ) - > getCharset ( ) ) ;
// Test case 4 (remains invalid)
2018-09-05 21:54:48 +00:00
vmime : : text : : decodeAndUnfold (
" =?utf-8?Q?abc=E3?= "
" =?utf-8?Q?=82?= "
" =?utf-8?Q?xy?= "
" =?utf-8?Q?z?= " ,
& outText
) ;
2016-11-05 12:31:54 +00:00
VASSERT_EQ ( " 4.1 " , 2 , outText . getWordCount ( ) ) ;
VASSERT_EQ ( " 4.2 " , " abc " , outText . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 4.3 " , vmime : : charset ( " utf-8 " ) , outText . getWordAt ( 0 ) - > getCharset ( ) ) ;
VASSERT_EQ ( " 4.4 " , " \xe3 \x82 xyz " , outText . getWordAt ( 1 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 4.5 " , vmime : : charset ( " utf-8 " ) , outText . getWordAt ( 1 ) - > getCharset ( ) ) ;
// Test case 5 (remains partially invalid)
2018-09-05 21:54:48 +00:00
vmime : : text : : decodeAndUnfold (
" =?utf-8?Q?abc=E3?= "
" =?utf-8?Q?=82?= "
" =?utf-8?Q? \x92 xy?= "
" =?utf-8?Q?z \xc3 ?= " ,
& outText
) ;
2016-11-05 12:31:54 +00:00
VASSERT_EQ ( " 5.1 " , 2 , outText . getWordCount ( ) ) ;
VASSERT_EQ ( " 5.2 " , " abc \xe3 \x82 \x92 xyz " , outText . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 5.3 " , vmime : : charset ( " utf-8 " ) , outText . getWordAt ( 0 ) - > getCharset ( ) ) ;
VASSERT_EQ ( " 5.4 " , " \xc3 " , outText . getWordAt ( 1 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 5.5 " , vmime : : charset ( " utf-8 " ) , outText . getWordAt ( 1 ) - > getCharset ( ) ) ;
}
2018-09-05 21:54:48 +00:00
void testUnknownCharset ( ) {
2016-11-05 12:31:54 +00:00
vmime : : text t ;
vmime : : text : : decodeAndUnfold ( " =?gb2312?B?wdaRY8PA?= " , & t ) ;
VASSERT_EQ ( " 1.1 " , 1 , t . getWordCount ( ) ) ;
VASSERT_EQ ( " 1.2 " , " \xc1 \xd6 \x91 \x63 \xc3 \xc0 " , t . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 1.3 " , vmime : : charset ( " gb2312 " ) , t . getWordAt ( 0 ) - > getCharset ( ) ) ;
vmime : : parsingContext ctx ;
const vmime : : string hfieldBuffer = " From: '=?gb2312?B?wdaRY8PA?=' <a.b@c.de> " ;
vmime : : shared_ptr < vmime : : headerField > hfield =
vmime : : headerField : : parseNext ( ctx , hfieldBuffer , 0 , hfieldBuffer . size ( ) ) ;
vmime : : shared_ptr < vmime : : mailbox > hvalue =
hfield - > getValue < vmime : : mailbox > ( ) ;
2015-02-16 17:43:03 +00:00
2016-11-05 12:31:54 +00:00
VASSERT_EQ ( " 2.1 " , 3 , hvalue - > getName ( ) . getWordCount ( ) ) ;
VASSERT_EQ ( " 2.2 " , " ' " , hvalue - > getName ( ) . getWordAt ( 0 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 2.3 " , vmime : : charset ( " us-ascii " ) , hvalue - > getName ( ) . getWordAt ( 0 ) - > getCharset ( ) ) ;
VASSERT_EQ ( " 2.4 " , " \xc1 \xd6 \x91 \x63 \xc3 \xc0 " , hvalue - > getName ( ) . getWordAt ( 1 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 2.5 " , vmime : : charset ( " gb2312 " ) , hvalue - > getName ( ) . getWordAt ( 1 ) - > getCharset ( ) ) ;
VASSERT_EQ ( " 2.6 " , " ' " , hvalue - > getName ( ) . getWordAt ( 2 ) - > getBuffer ( ) ) ;
VASSERT_EQ ( " 2.7 " , vmime : : charset ( " us-ascii " ) , hvalue - > getName ( ) . getWordAt ( 2 ) - > getCharset ( ) ) ;
2015-02-16 17:43:03 +00:00
}
2005-08-25 21:25:45 +00:00
VMIME_TEST_SUITE_END