1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <rtl/ustring.hxx>
21 #include <cppunit/TestAssert.h>
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
25 #include <tools/inetmime.hxx>
30 class Test
: public CppUnit::TestFixture
32 bool testDecode(char const * input
, char const * expected
);
34 void test_decodeHeaderFieldBody();
36 void test_scanContentType_basic();
37 void test_scanContentType_rfc2231();
39 CPPUNIT_TEST_SUITE(Test
);
40 CPPUNIT_TEST(test_decodeHeaderFieldBody
);
41 CPPUNIT_TEST(test_scanContentType_basic
);
42 CPPUNIT_TEST(test_scanContentType_rfc2231
);
43 CPPUNIT_TEST_SUITE_END();
46 bool Test::testDecode(char const * input
, char const * expected
)
48 OUString result
= INetMIME::decodeHeaderFieldBody(input
);
49 return result
.equalsAscii(expected
);
52 void Test::test_decodeHeaderFieldBody()
54 CPPUNIT_ASSERT(testDecode("=?iso-8859-1?B?QQ==?=", "A"));
55 CPPUNIT_ASSERT(testDecode("=?iso-8859-1?B?QUI=?=", "AB"));
56 CPPUNIT_ASSERT(testDecode("=?iso-8859-1?B?QUJD?=", "ABC"));
59 void Test::test_scanContentType_basic()
62 = u
"TEST/subTST; parm1=Value1; Parm2=\"unpacked value; %20\""_ustr
;
63 // Just scan input for valid string:
64 auto end
= INetMIME::scanContentType(input
);
65 CPPUNIT_ASSERT(end
!= nullptr);
66 CPPUNIT_ASSERT_EQUAL(OUString(), OUString(end
));
67 // Scan input and parse type, subType and parameters:
70 INetContentTypeParameterList parameters
;
71 end
= INetMIME::scanContentType(input
,
72 &type
, &subType
, ¶meters
);
73 CPPUNIT_ASSERT(end
!= nullptr);
74 CPPUNIT_ASSERT_EQUAL(OUString(), OUString(end
));
75 CPPUNIT_ASSERT_EQUAL(u
"test"_ustr
, type
);
76 CPPUNIT_ASSERT_EQUAL(u
"subtst"_ustr
, subType
);
78 INetContentTypeParameterList::size_type(2), parameters
.size());
79 auto i
= parameters
.find("parm1"_ostr
);
80 CPPUNIT_ASSERT(i
!= parameters
.end());
81 CPPUNIT_ASSERT_EQUAL(OString(), i
->second
.m_sCharset
);
82 CPPUNIT_ASSERT_EQUAL(OString(), i
->second
.m_sLanguage
);
83 CPPUNIT_ASSERT_EQUAL(u
"Value1"_ustr
, i
->second
.m_sValue
);
84 CPPUNIT_ASSERT(i
->second
.m_bConverted
);
85 i
= parameters
.find("parm2"_ostr
);
86 CPPUNIT_ASSERT(i
!= parameters
.end());
87 CPPUNIT_ASSERT_EQUAL(OString(), i
->second
.m_sCharset
);
88 CPPUNIT_ASSERT_EQUAL(OString(), i
->second
.m_sLanguage
);
89 CPPUNIT_ASSERT_EQUAL(u
"unpacked value; %20"_ustr
, i
->second
.m_sValue
);
90 CPPUNIT_ASSERT(i
->second
.m_bConverted
);
93 void Test::test_scanContentType_rfc2231()
95 // Test extended parameter with value split in 3 sections:
98 "parm1*0*=US-ASCII'En'5%25%20; "
100 "parm1*2*=%20%3d%200.5"_ustr
;
101 // Just scan input for valid string:
102 auto end
= INetMIME::scanContentType(input
);
103 CPPUNIT_ASSERT(end
!= nullptr);
104 CPPUNIT_ASSERT_EQUAL(OUString(), OUString(end
));
105 // Scan input and parse type, subType and parameters:
108 INetContentTypeParameterList parameters
;
109 end
= INetMIME::scanContentType(input
,
110 &type
, &subType
, ¶meters
);
111 CPPUNIT_ASSERT(end
!= nullptr);
112 CPPUNIT_ASSERT_EQUAL(OUString(), OUString(end
));
113 CPPUNIT_ASSERT_EQUAL(u
"test"_ustr
, type
);
114 CPPUNIT_ASSERT_EQUAL(u
"subtst"_ustr
, subType
);
115 CPPUNIT_ASSERT_EQUAL(
116 INetContentTypeParameterList::size_type(1), parameters
.size());
117 auto i
= parameters
.find("parm1"_ostr
);
118 CPPUNIT_ASSERT(i
!= parameters
.end());
119 CPPUNIT_ASSERT_EQUAL("us-ascii"_ostr
, i
->second
.m_sCharset
);
120 CPPUNIT_ASSERT_EQUAL("en"_ostr
, i
->second
.m_sLanguage
);
121 CPPUNIT_ASSERT_EQUAL(u
"5% of 10 = 0.5"_ustr
, i
->second
.m_sValue
);
122 CPPUNIT_ASSERT(i
->second
.m_bConverted
);
124 // Test extended parameters with different value charsets:
125 input
= "TEST/subTST;"
126 "parm1*0*=us-ascii'en'value;PARM1*1*=1;"
127 "parm2*0*=WINDOWS-1250'en-GB'value2%20%80;"
128 "parm3*0*=UNKNOWN'EN'value3;"
129 "parm1*1*=2"; // this parameter is a duplicate,
130 // the scan should end before this parameter
131 // Just scan input for valid string:
132 end
= INetMIME::scanContentType(input
);
133 CPPUNIT_ASSERT(end
!= nullptr);
134 CPPUNIT_ASSERT_EQUAL(u
";parm1*1*=2"_ustr
, OUString(end
)); // the invalid end of input
135 // Scan input and parse type, subType and parameters:
136 end
= INetMIME::scanContentType(input
,
137 &type
, &subType
, ¶meters
);
138 CPPUNIT_ASSERT(end
!= nullptr);
139 CPPUNIT_ASSERT_EQUAL(u
";parm1*1*=2"_ustr
, OUString(end
)); // the invalid end of input
140 CPPUNIT_ASSERT_EQUAL(u
"test"_ustr
, type
);
141 CPPUNIT_ASSERT_EQUAL(u
"subtst"_ustr
, subType
);
142 CPPUNIT_ASSERT_EQUAL(
143 INetContentTypeParameterList::size_type(3), parameters
.size());
144 i
= parameters
.find("parm1"_ostr
);
145 CPPUNIT_ASSERT(i
!= parameters
.end());
146 CPPUNIT_ASSERT_EQUAL("us-ascii"_ostr
, i
->second
.m_sCharset
);
147 CPPUNIT_ASSERT_EQUAL("en"_ostr
, i
->second
.m_sLanguage
);
148 CPPUNIT_ASSERT_EQUAL(u
"value1"_ustr
, i
->second
.m_sValue
);
149 CPPUNIT_ASSERT(i
->second
.m_bConverted
);
150 i
= parameters
.find("parm2"_ostr
);
151 CPPUNIT_ASSERT(i
!= parameters
.end());
152 CPPUNIT_ASSERT_EQUAL("windows-1250"_ostr
, i
->second
.m_sCharset
);
153 CPPUNIT_ASSERT_EQUAL("en-gb"_ostr
, i
->second
.m_sLanguage
);
154 // Euro currency sign, windows-1250 x80 is converted to unicode u20AC:
155 CPPUNIT_ASSERT_EQUAL(u
"value2 \u20AC"_ustr
, i
->second
.m_sValue
);
156 CPPUNIT_ASSERT(i
->second
.m_bConverted
);
157 i
= parameters
.find("parm3"_ostr
);
158 CPPUNIT_ASSERT(i
!= parameters
.end());
159 CPPUNIT_ASSERT_EQUAL("unknown"_ostr
, i
->second
.m_sCharset
);
160 CPPUNIT_ASSERT_EQUAL("en"_ostr
, i
->second
.m_sLanguage
);
161 // Conversion fails for unknown charsets:
162 CPPUNIT_ASSERT(!i
->second
.m_bConverted
);
165 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */