| 1 | // |
| 2 | // StreamConverterTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "StreamConverterTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/StreamConverter.h" |
| 15 | #include "Poco/ASCIIEncoding.h" |
| 16 | #include "Poco/Latin1Encoding.h" |
| 17 | #include "Poco/UTF8Encoding.h" |
| 18 | #include "Poco/StreamCopier.h" |
| 19 | #include <sstream> |
| 20 | |
| 21 | |
| 22 | using Poco::InputStreamConverter; |
| 23 | using Poco::OutputStreamConverter; |
| 24 | using Poco::Latin1Encoding; |
| 25 | using Poco::UTF8Encoding; |
| 26 | using Poco::ASCIIEncoding; |
| 27 | using Poco::StreamCopier; |
| 28 | |
| 29 | |
| 30 | StreamConverterTest::StreamConverterTest(const std::string& rName): CppUnit::TestCase(rName) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | |
| 35 | StreamConverterTest::~StreamConverterTest() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | |
| 40 | void StreamConverterTest::testIdentityASCIIIn() |
| 41 | { |
| 42 | ASCIIEncoding encoding; |
| 43 | |
| 44 | std::istringstream istr1("" ); |
| 45 | std::ostringstream ostr1; |
| 46 | InputStreamConverter converter1(istr1, encoding, encoding); |
| 47 | StreamCopier::copyStream(converter1, ostr1); |
| 48 | assertTrue (ostr1.str() == "" ); |
| 49 | assertTrue (converter1.errors() == 0); |
| 50 | |
| 51 | std::istringstream istr2("foo bar" ); |
| 52 | std::ostringstream ostr2; |
| 53 | InputStreamConverter converter2(istr2, encoding, encoding); |
| 54 | StreamCopier::copyStream(converter2, ostr2); |
| 55 | assertTrue (ostr2.str() == "foo bar" ); |
| 56 | assertTrue (converter2.errors() == 0); |
| 57 | |
| 58 | std::istringstream istr3("x" ); |
| 59 | std::ostringstream ostr3; |
| 60 | InputStreamConverter converter3(istr3, encoding, encoding); |
| 61 | StreamCopier::copyStream(converter3, ostr3); |
| 62 | assertTrue (ostr3.str() == "x" ); |
| 63 | assertTrue (converter3.errors() == 0); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void StreamConverterTest::testIdentityASCIIOut() |
| 68 | { |
| 69 | ASCIIEncoding encoding; |
| 70 | |
| 71 | std::ostringstream ostr1; |
| 72 | OutputStreamConverter converter1(ostr1, encoding, encoding); |
| 73 | converter1 << "" ; |
| 74 | assertTrue (ostr1.str() == "" ); |
| 75 | assertTrue (converter1.errors() == 0); |
| 76 | |
| 77 | std::ostringstream ostr2; |
| 78 | OutputStreamConverter converter2(ostr2, encoding, encoding); |
| 79 | converter2 << "foo bar" ; |
| 80 | assertTrue (ostr2.str() == "foo bar" ); |
| 81 | assertTrue (converter2.errors() == 0); |
| 82 | |
| 83 | std::ostringstream ostr3; |
| 84 | OutputStreamConverter converter3(ostr3, encoding, encoding); |
| 85 | converter3 << "x" ; |
| 86 | assertTrue (ostr3.str() == "x" ); |
| 87 | assertTrue (converter3.errors() == 0); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void StreamConverterTest::testIdentityUTF8In() |
| 92 | { |
| 93 | UTF8Encoding encoding; |
| 94 | |
| 95 | std::istringstream istr1("" ); |
| 96 | std::ostringstream ostr1; |
| 97 | InputStreamConverter converter1(istr1, encoding, encoding); |
| 98 | StreamCopier::copyStream(converter1, ostr1); |
| 99 | assertTrue (ostr1.str() == "" ); |
| 100 | assertTrue (converter1.errors() == 0); |
| 101 | |
| 102 | std::istringstream istr2("foo bar" ); |
| 103 | std::ostringstream ostr2; |
| 104 | InputStreamConverter converter2(istr2, encoding, encoding); |
| 105 | StreamCopier::copyStream(converter2, ostr2); |
| 106 | assertTrue (ostr2.str() == "foo bar" ); |
| 107 | assertTrue (converter2.errors() == 0); |
| 108 | |
| 109 | std::istringstream istr3("x" ); |
| 110 | std::ostringstream ostr3; |
| 111 | InputStreamConverter converter3(istr3, encoding, encoding); |
| 112 | StreamCopier::copyStream(converter3, ostr3); |
| 113 | assertTrue (ostr3.str() == "x" ); |
| 114 | assertTrue (converter3.errors() == 0); |
| 115 | |
| 116 | const unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0x20, 0x00}; |
| 117 | std::string text((const char*) greek); |
| 118 | |
| 119 | std::istringstream istr4(text); |
| 120 | std::ostringstream ostr4; |
| 121 | InputStreamConverter converter4(istr4, encoding, encoding); |
| 122 | StreamCopier::copyStream(converter4, ostr4); |
| 123 | assertTrue (ostr4.str() == text); |
| 124 | assertTrue (converter4.errors() == 0); |
| 125 | |
| 126 | const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00}; |
| 127 | std::string text2((const char*) supp); |
| 128 | |
| 129 | std::istringstream istr5(text2); |
| 130 | std::ostringstream ostr5; |
| 131 | InputStreamConverter converter5(istr5, encoding, encoding); |
| 132 | StreamCopier::copyStream(converter5, ostr5); |
| 133 | assertTrue (ostr5.str() == text2); |
| 134 | assertTrue (converter5.errors() == 0); |
| 135 | |
| 136 | |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void StreamConverterTest::testIdentityUTF8Out() |
| 141 | { |
| 142 | UTF8Encoding encoding; |
| 143 | |
| 144 | std::ostringstream ostr1; |
| 145 | OutputStreamConverter converter1(ostr1, encoding, encoding); |
| 146 | converter1 << "" ; |
| 147 | assertTrue (ostr1.str() == "" ); |
| 148 | assertTrue (converter1.errors() == 0); |
| 149 | |
| 150 | std::ostringstream ostr2; |
| 151 | OutputStreamConverter converter2(ostr2, encoding, encoding); |
| 152 | converter2 << "foo bar" ; |
| 153 | assertTrue (ostr2.str() == "foo bar" ); |
| 154 | assertTrue (converter2.errors() == 0); |
| 155 | |
| 156 | std::ostringstream ostr3; |
| 157 | OutputStreamConverter converter3(ostr3, encoding, encoding); |
| 158 | converter3 << "x" ; |
| 159 | assertTrue (ostr3.str() == "x" ); |
| 160 | assertTrue (converter3.errors() == 0); |
| 161 | |
| 162 | const unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0x20, 0x00}; |
| 163 | std::string text((const char*) greek); |
| 164 | |
| 165 | std::ostringstream ostr4; |
| 166 | OutputStreamConverter converter4(ostr4, encoding, encoding); |
| 167 | converter4 << text; |
| 168 | assertTrue (ostr4.str() == text); |
| 169 | assertTrue (converter4.errors() == 0); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | void StreamConverterTest::testUTF8toASCIIIn() |
| 174 | { |
| 175 | UTF8Encoding utf8Encoding; |
| 176 | ASCIIEncoding asciiEncoding; |
| 177 | |
| 178 | const unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0x20, 0x41, 0x42, 0x00}; |
| 179 | std::string text((const char*) greek); |
| 180 | |
| 181 | std::istringstream istr1(text); |
| 182 | std::ostringstream ostr1; |
| 183 | InputStreamConverter converter1(istr1, utf8Encoding, asciiEncoding); |
| 184 | StreamCopier::copyStream(converter1, ostr1); |
| 185 | assertTrue (ostr1.str() == " ????? AB" ); |
| 186 | assertTrue (converter1.errors() == 0); |
| 187 | |
| 188 | std::istringstream istr2("abcde" ); |
| 189 | std::ostringstream ostr2; |
| 190 | InputStreamConverter converter2(istr2, utf8Encoding, asciiEncoding); |
| 191 | StreamCopier::copyStream(converter2, ostr2); |
| 192 | assertTrue (ostr2.str() == "abcde" ); |
| 193 | assertTrue (converter2.errors() == 0); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void StreamConverterTest::testUTF8toASCIIOut() |
| 198 | { |
| 199 | UTF8Encoding utf8Encoding; |
| 200 | ASCIIEncoding asciiEncoding; |
| 201 | |
| 202 | const unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0x20, 0x41, 0x42, 0x00}; |
| 203 | std::string text((const char*) greek); |
| 204 | |
| 205 | std::ostringstream ostr1; |
| 206 | OutputStreamConverter converter1(ostr1, utf8Encoding, asciiEncoding); |
| 207 | converter1 << text; |
| 208 | assertTrue (ostr1.str() == " ????? AB" ); |
| 209 | assertTrue (converter1.errors() == 0); |
| 210 | |
| 211 | std::ostringstream ostr2; |
| 212 | OutputStreamConverter converter2(ostr2, utf8Encoding, asciiEncoding); |
| 213 | converter2 << "abcde" ; |
| 214 | assertTrue (ostr2.str() == "abcde" ); |
| 215 | assertTrue (converter2.errors() == 0); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void StreamConverterTest::testLatin1toUTF8In() |
| 220 | { |
| 221 | UTF8Encoding utf8Encoding; |
| 222 | Latin1Encoding latin1Encoding; |
| 223 | |
| 224 | const unsigned char latin1Chars[] = {'g', 252, 'n', 't', 'e', 'r', 0}; |
| 225 | const unsigned char utf8Chars[] = {'g', 195, 188, 'n', 't', 'e', 'r', 0}; |
| 226 | std::string latin1Text((const char*) latin1Chars); |
| 227 | std::string utf8Text((const char*) utf8Chars); |
| 228 | |
| 229 | std::istringstream istr1(latin1Text); |
| 230 | std::ostringstream ostr1; |
| 231 | InputStreamConverter converter1(istr1, latin1Encoding, utf8Encoding); |
| 232 | StreamCopier::copyStream(converter1, ostr1); |
| 233 | assertTrue (ostr1.str() == utf8Text); |
| 234 | assertTrue (converter1.errors() == 0); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | void StreamConverterTest::testLatin1toUTF8Out() |
| 239 | { |
| 240 | UTF8Encoding utf8Encoding; |
| 241 | Latin1Encoding latin1Encoding; |
| 242 | |
| 243 | const unsigned char latin1Chars[] = {'g', 252, 'n', 't', 'e', 'r', 0}; |
| 244 | const unsigned char utf8Chars[] = {'g', 195, 188, 'n', 't', 'e', 'r', 0}; |
| 245 | std::string latin1Text((const char*) latin1Chars); |
| 246 | std::string utf8Text((const char*) utf8Chars); |
| 247 | |
| 248 | std::ostringstream ostr1; |
| 249 | OutputStreamConverter converter1(ostr1, latin1Encoding, utf8Encoding); |
| 250 | converter1 << latin1Text; |
| 251 | assertTrue (ostr1.str() == utf8Text); |
| 252 | assertTrue (converter1.errors() == 0); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | void StreamConverterTest::testErrorsIn() |
| 257 | { |
| 258 | UTF8Encoding utf8Encoding; |
| 259 | Latin1Encoding latin1Encoding; |
| 260 | |
| 261 | const unsigned char badChars[] = {'a', 'b', 255, 'c', 254, 0}; |
| 262 | std::string badText((const char*) badChars); |
| 263 | |
| 264 | std::istringstream istr1(badText); |
| 265 | std::ostringstream ostr1; |
| 266 | InputStreamConverter converter1(istr1, utf8Encoding, latin1Encoding); |
| 267 | StreamCopier::copyStream(converter1, ostr1); |
| 268 | assertTrue (converter1.errors() == 2); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | void StreamConverterTest::testErrorsOut() |
| 273 | { |
| 274 | UTF8Encoding utf8Encoding; |
| 275 | Latin1Encoding latin1Encoding; |
| 276 | |
| 277 | const unsigned char badChars[] = {'a', 'b', 255, 'c', 254, 0}; |
| 278 | std::string badText((const char*) badChars); |
| 279 | |
| 280 | std::ostringstream ostr1; |
| 281 | OutputStreamConverter converter1(ostr1, utf8Encoding, latin1Encoding); |
| 282 | converter1 << badText; |
| 283 | assertTrue (converter1.errors() == 1); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void StreamConverterTest::setUp() |
| 288 | { |
| 289 | } |
| 290 | |
| 291 | |
| 292 | void StreamConverterTest::tearDown() |
| 293 | { |
| 294 | } |
| 295 | |
| 296 | |
| 297 | CppUnit::Test* StreamConverterTest::suite() |
| 298 | { |
| 299 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("StreamConverterTest" ); |
| 300 | |
| 301 | CppUnit_addTest(pSuite, StreamConverterTest, testIdentityASCIIIn); |
| 302 | CppUnit_addTest(pSuite, StreamConverterTest, testIdentityASCIIOut); |
| 303 | CppUnit_addTest(pSuite, StreamConverterTest, testIdentityUTF8In); |
| 304 | CppUnit_addTest(pSuite, StreamConverterTest, testIdentityUTF8Out); |
| 305 | CppUnit_addTest(pSuite, StreamConverterTest, testUTF8toASCIIIn); |
| 306 | CppUnit_addTest(pSuite, StreamConverterTest, testUTF8toASCIIOut); |
| 307 | CppUnit_addTest(pSuite, StreamConverterTest, testLatin1toUTF8In); |
| 308 | CppUnit_addTest(pSuite, StreamConverterTest, testLatin1toUTF8Out); |
| 309 | CppUnit_addTest(pSuite, StreamConverterTest, testErrorsIn); |
| 310 | CppUnit_addTest(pSuite, StreamConverterTest, testErrorsOut); |
| 311 | |
| 312 | return pSuite; |
| 313 | } |
| 314 | |