intl/uconv/tests/nsTestUConv.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <stdio.h>
michael@0 7 #include <string.h>
michael@0 8 #include "nsXPCOM.h"
michael@0 9 #include "nsIComponentManager.h"
michael@0 10 #include "nsIServiceManager.h"
michael@0 11 #include "nsISupports.h"
michael@0 12 #include "nsICharsetConverterManager.h"
michael@0 13 #include "nsIPlatformCharset.h"
michael@0 14 #include "nsReadableUtils.h"
michael@0 15
michael@0 16
michael@0 17 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
michael@0 18 static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID);
michael@0 19
michael@0 20 /**
michael@0 21 * Test program for the Unicode Converters.
michael@0 22 *
michael@0 23 * Error messages format inside of a test.
michael@0 24 *
michael@0 25 * - silent while all is OK.
michael@0 26 *
michael@0 27 * - "ERROR at T001.easyConversion.Convert() code=0xfffd.\n"
michael@0 28 * - "ERROR at T001.easyConversion.ConvResLen expected=0x02 result=0x04.\n"
michael@0 29 *
michael@0 30 * - "Test Passed.\n" for a successful end.
michael@0 31 *
michael@0 32 * @created 01/Dec/1998
michael@0 33 * @author Catalin Rotaru [CATA]
michael@0 34 */
michael@0 35
michael@0 36 //----------------------------------------------------------------------
michael@0 37 // Global variables and macros
michael@0 38
michael@0 39 #define GENERAL_BUFFER 20000 // general purpose buffer; for Unicode divide by 2
michael@0 40
michael@0 41 #define ARRAY_SIZE(_array) \
michael@0 42 (sizeof(_array) / sizeof(_array[0]))
michael@0 43
michael@0 44 nsICharsetConverterManager * ccMan = nullptr;
michael@0 45
michael@0 46 /**
michael@0 47 * Test data for Latin1 charset.
michael@0 48 */
michael@0 49
michael@0 50 char bLatin1_d0[] = {
michael@0 51 "\x00\x0d\x7f\x80\xff"
michael@0 52 };
michael@0 53
michael@0 54 char16_t cLatin1_d0[] = {
michael@0 55 0x0000,0x000d,0x007f,0x20ac,0x00ff
michael@0 56 };
michael@0 57
michael@0 58 int32_t bLatin1_s0 = ARRAY_SIZE(bLatin1_d0)-1;
michael@0 59 int32_t cLatin1_s0 = ARRAY_SIZE(cLatin1_d0);
michael@0 60
michael@0 61 //----------------------------------------------------------------------
michael@0 62 // Converter Manager test code
michael@0 63
michael@0 64 nsresult testCharsetConverterManager()
michael@0 65 {
michael@0 66 printf("\n[T001] CharsetConverterManager\n");
michael@0 67
michael@0 68 return NS_OK;
michael@0 69 }
michael@0 70
michael@0 71 //----------------------------------------------------------------------
michael@0 72 // Helper functions and macros for testing decoders and encoders
michael@0 73
michael@0 74 #define CREATE_DECODER(_charset) \
michael@0 75 nsIUnicodeDecoder * dec; \
michael@0 76 nsAutoString str;str.AssignASCII(_charset); \
michael@0 77 nsresult res = ccMan->GetUnicodeDecoder(&str,&dec); \
michael@0 78 if (NS_FAILED(res)) { \
michael@0 79 printf("ERROR at GetUnicodeDecoder() code=0x%x.\n",res); \
michael@0 80 return res; \
michael@0 81 }
michael@0 82
michael@0 83 #define CREATE_ENCODER(_charset) \
michael@0 84 nsIUnicodeEncoder * enc; \
michael@0 85 nsAutoString str; str.AssignASCII(_charset); \
michael@0 86 nsresult res = ccMan->GetUnicodeEncoder(&str,&enc); \
michael@0 87 if (NS_FAILED(res)) { \
michael@0 88 printf("ERROR at GetUnicodeEncoder() code=0x%x.\n",res); \
michael@0 89 return res; \
michael@0 90 }
michael@0 91
michael@0 92 /**
michael@0 93 * Decoder test.
michael@0 94 *
michael@0 95 * This method will test the conversion only.
michael@0 96 */
michael@0 97 nsresult testDecoder(nsIUnicodeDecoder * aDec,
michael@0 98 const char * aSrc, int32_t aSrcLength,
michael@0 99 const char16_t * aRes, int32_t aResLength,
michael@0 100 const char * aTestName)
michael@0 101 {
michael@0 102 nsresult res;
michael@0 103
michael@0 104 // prepare for conversion
michael@0 105 int32_t srcLen = aSrcLength;
michael@0 106 char16_t dest[GENERAL_BUFFER/2];
michael@0 107 int32_t destLen = GENERAL_BUFFER/2;
michael@0 108
michael@0 109 // conversion
michael@0 110 res = aDec->Convert(aSrc, &srcLen, dest, &destLen);
michael@0 111 // we want a perfect result here - the test data should be complete!
michael@0 112 if (res != NS_OK) {
michael@0 113 printf("ERROR at %s.easy.Decode() code=0x%x.\n",aTestName,res);
michael@0 114 return NS_ERROR_UNEXPECTED;
michael@0 115 }
michael@0 116
michael@0 117 // compare results
michael@0 118 if (aResLength != destLen) {
michael@0 119 printf("ERROR at %s.easy.DecResLen expected=0x%x result=0x%x.\n",
michael@0 120 aTestName, aResLength, destLen);
michael@0 121 return NS_ERROR_UNEXPECTED;
michael@0 122 }
michael@0 123 for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
michael@0 124 printf("ERROR at %s.easy.DecResChar[%d] expected=0x%x result=0x%x.\n",
michael@0 125 aTestName, i, aRes[i], dest[i]);
michael@0 126 return NS_ERROR_UNEXPECTED;
michael@0 127 }
michael@0 128
michael@0 129 return NS_OK;
michael@0 130 }
michael@0 131
michael@0 132 /**
michael@0 133 * Encoder test.
michael@0 134 *
michael@0 135 * This method will test the conversion only.
michael@0 136 */
michael@0 137 nsresult testEncoder(nsIUnicodeEncoder * aEnc,
michael@0 138 const char16_t * aSrc, int32_t aSrcLength,
michael@0 139 const char * aRes, int32_t aResLength,
michael@0 140 const char * aTestName)
michael@0 141 {
michael@0 142 nsresult res;
michael@0 143
michael@0 144 // prepare for conversion
michael@0 145 int32_t srcLen = 0;
michael@0 146 char dest[GENERAL_BUFFER];
michael@0 147 int32_t destLen = 0;
michael@0 148 int32_t bcr, bcw;
michael@0 149
michael@0 150 // conversion
michael@0 151 bcr = aSrcLength;
michael@0 152 bcw = GENERAL_BUFFER;
michael@0 153 res = aEnc->Convert(aSrc, &bcr, dest, &bcw);
michael@0 154 srcLen += bcr;
michael@0 155 destLen += bcw;
michael@0 156 // we want a perfect result here - the test data should be complete!
michael@0 157 if (res != NS_OK) {
michael@0 158 printf("ERROR at %s.easy.Encode() code=0x%x.\n",aTestName,res);
michael@0 159 return NS_ERROR_UNEXPECTED;
michael@0 160 }
michael@0 161
michael@0 162 // finish
michael@0 163 bcw = GENERAL_BUFFER - destLen;
michael@0 164 res = aEnc->Finish(dest + destLen, &bcw);
michael@0 165 destLen += bcw;
michael@0 166 // we want a perfect result here - the test data should be complete!
michael@0 167 if (res != NS_OK) {
michael@0 168 printf("ERROR at %s.easy.Finish() code=0x%x.\n",aTestName,res);
michael@0 169 return NS_ERROR_UNEXPECTED;
michael@0 170 }
michael@0 171
michael@0 172 // compare results
michael@0 173 if (aResLength != destLen) {
michael@0 174 printf("ERROR at %s.easy.EncResLen expected=0x%x result=0x%x.\n",
michael@0 175 aTestName, aResLength, destLen);
michael@0 176 return NS_ERROR_UNEXPECTED;
michael@0 177 }
michael@0 178 for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
michael@0 179 printf("ERROR at %s.easy.EncResChar[%d] expected=0x%x result=0x%x.\n",
michael@0 180 aTestName, i, aRes[i], dest[i]);
michael@0 181 return NS_ERROR_UNEXPECTED;
michael@0 182 }
michael@0 183
michael@0 184 return NS_OK;
michael@0 185 }
michael@0 186
michael@0 187 /**
michael@0 188 * Decoder test.
michael@0 189 *
michael@0 190 * This method will test a given converter under a given set of data and some
michael@0 191 * very stressful conditions.
michael@0 192 */
michael@0 193 nsresult testStressDecoder(nsIUnicodeDecoder * aDec,
michael@0 194 const char * aSrc, int32_t aSrcLength,
michael@0 195 const char16_t * aRes, int32_t aResLength,
michael@0 196 const char * aTestName)
michael@0 197 {
michael@0 198 nsresult res;
michael@0 199
michael@0 200 // get estimated length
michael@0 201 int32_t estimatedLength;
michael@0 202 res = aDec->GetMaxLength(aSrc, aSrcLength, &estimatedLength);
michael@0 203 if (NS_FAILED(res)) {
michael@0 204 printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res);
michael@0 205 return res;
michael@0 206 }
michael@0 207 bool exactLength = (res == NS_EXACT_LENGTH);
michael@0 208
michael@0 209 // prepare for conversion
michael@0 210 int32_t srcLen = 0;
michael@0 211 int32_t srcOff = 0;
michael@0 212 char16_t dest[1024];
michael@0 213 int32_t destLen = 0;
michael@0 214 int32_t destOff = 0;
michael@0 215
michael@0 216 // controlled conversion
michael@0 217 for (;srcOff < aSrcLength;) {
michael@0 218 res = aDec->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen);
michael@0 219 if (NS_FAILED(res)) {
michael@0 220 printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res);
michael@0 221 return res;
michael@0 222 }
michael@0 223
michael@0 224 srcOff+=srcLen;
michael@0 225 destOff+=destLen;
michael@0 226
michael@0 227 // give a little input each time; it'll be consumed if enough output space
michael@0 228 srcLen = 1;
michael@0 229 // give output space only when requested: sadic!
michael@0 230 if (res == NS_PARTIAL_MORE_OUTPUT) {
michael@0 231 destLen = 1;
michael@0 232 } else {
michael@0 233 destLen = 0;
michael@0 234 }
michael@0 235 }
michael@0 236
michael@0 237 // we want perfect result here - the test data should be complete!
michael@0 238 if (res != NS_OK) {
michael@0 239 printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res);
michael@0 240 return NS_ERROR_UNEXPECTED;
michael@0 241 }
michael@0 242
michael@0 243 // compare lengths
michael@0 244 if (exactLength) {
michael@0 245 if (destOff != estimatedLength) {
michael@0 246 printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
michael@0 247 aTestName, estimatedLength, destOff);
michael@0 248 return NS_ERROR_UNEXPECTED;
michael@0 249 }
michael@0 250 } else {
michael@0 251 if (destOff > estimatedLength) {
michael@0 252 printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
michael@0 253 aTestName, estimatedLength, destOff);
michael@0 254 return NS_ERROR_UNEXPECTED;
michael@0 255 }
michael@0 256 }
michael@0 257
michael@0 258 // compare results
michael@0 259 if (aResLength != destOff) {
michael@0 260 printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n",
michael@0 261 aTestName, aResLength, destOff);
michael@0 262 return NS_ERROR_UNEXPECTED;
michael@0 263 }
michael@0 264 for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
michael@0 265 printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n",
michael@0 266 aTestName, i, aRes[i], dest[i]);
michael@0 267 return NS_ERROR_UNEXPECTED;
michael@0 268 }
michael@0 269
michael@0 270 return NS_OK;
michael@0 271 }
michael@0 272
michael@0 273 /**
michael@0 274 * Encoder test.
michael@0 275 *
michael@0 276 * This method will test a given converter under a given set of data and some
michael@0 277 * very stressful conditions.
michael@0 278 */
michael@0 279 nsresult testStressEncoder(nsIUnicodeEncoder * aEnc,
michael@0 280 const char16_t * aSrc, int32_t aSrcLength,
michael@0 281 const char * aRes, int32_t aResLength,
michael@0 282 const char * aTestName)
michael@0 283 {
michael@0 284 nsresult res;
michael@0 285
michael@0 286 // get estimated length
michael@0 287 int32_t estimatedLength;
michael@0 288 res = aEnc->GetMaxLength(aSrc, aSrcLength, &estimatedLength);
michael@0 289 if (NS_FAILED(res)) {
michael@0 290 printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res);
michael@0 291 return res;
michael@0 292 }
michael@0 293 bool exactLength = (res == NS_OK_UENC_EXACTLENGTH);
michael@0 294
michael@0 295 // prepare for conversion
michael@0 296 int32_t srcLen = 0;
michael@0 297 int32_t srcOff = 0;
michael@0 298 char dest[GENERAL_BUFFER];
michael@0 299 int32_t destLen = 0;
michael@0 300 int32_t destOff = 0;
michael@0 301
michael@0 302 // controlled conversion
michael@0 303 for (;srcOff < aSrcLength;) {
michael@0 304 res = aEnc->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen);
michael@0 305 if (NS_FAILED(res)) {
michael@0 306 printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res);
michael@0 307 return res;
michael@0 308 }
michael@0 309
michael@0 310 srcOff+=srcLen;
michael@0 311 destOff+=destLen;
michael@0 312
michael@0 313 // give a little input each time; it'll be consumed if enough output space
michael@0 314 srcLen = 1;
michael@0 315 // give output space only when requested: sadic!
michael@0 316 if (res == NS_OK_UENC_MOREOUTPUT) {
michael@0 317 destLen = 1;
michael@0 318 } else {
michael@0 319 destLen = 0;
michael@0 320 }
michael@0 321 }
michael@0 322
michael@0 323 if (res != NS_OK) if (res != NS_OK_UENC_MOREOUTPUT) {
michael@0 324 printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res);
michael@0 325 return NS_ERROR_UNEXPECTED;
michael@0 326 }
michael@0 327
michael@0 328 for (;;) {
michael@0 329 res = aEnc->Finish(dest + destOff, &destLen);
michael@0 330 if (NS_FAILED(res)) {
michael@0 331 printf("ERROR at %s.stress.Finish() code=0x%x.\n",aTestName,res);
michael@0 332 return res;
michael@0 333 }
michael@0 334
michael@0 335 destOff+=destLen;
michael@0 336
michael@0 337 // give output space only when requested: sadic!
michael@0 338 if (res == NS_OK_UENC_MOREOUTPUT) {
michael@0 339 destLen = 1;
michael@0 340 } else break;
michael@0 341 }
michael@0 342
michael@0 343 // compare lengths
michael@0 344 if (exactLength) {
michael@0 345 if (destOff != estimatedLength) {
michael@0 346 printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
michael@0 347 aTestName, estimatedLength, destOff);
michael@0 348 return NS_ERROR_UNEXPECTED;
michael@0 349 }
michael@0 350 } else {
michael@0 351 if (destOff > estimatedLength) {
michael@0 352 printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
michael@0 353 aTestName, estimatedLength, destOff);
michael@0 354 return NS_ERROR_UNEXPECTED;
michael@0 355 }
michael@0 356 }
michael@0 357
michael@0 358 // compare results
michael@0 359 if (aResLength != destOff) {
michael@0 360 printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n",
michael@0 361 aTestName, aResLength, destOff);
michael@0 362 return NS_ERROR_UNEXPECTED;
michael@0 363 }
michael@0 364 for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
michael@0 365 printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n",
michael@0 366 aTestName, i, aRes[i], dest[i]);
michael@0 367 return NS_ERROR_UNEXPECTED;
michael@0 368 }
michael@0 369
michael@0 370 return NS_OK;
michael@0 371 }
michael@0 372
michael@0 373 /**
michael@0 374 * Reset decoder.
michael@0 375 */
michael@0 376 nsresult resetDecoder(nsIUnicodeDecoder * aDec, const char * aTestName)
michael@0 377 {
michael@0 378 nsresult res = aDec->Reset();
michael@0 379
michael@0 380 if (NS_FAILED(res)) {
michael@0 381 printf("ERROR at %s.dec.Reset() code=0x%x.\n",aTestName,res);
michael@0 382 return res;
michael@0 383 }
michael@0 384
michael@0 385 return res;
michael@0 386 }
michael@0 387
michael@0 388 /**
michael@0 389 * Reset encoder.
michael@0 390 */
michael@0 391 nsresult resetEncoder(nsIUnicodeEncoder * aEnc, const char * aTestName)
michael@0 392 {
michael@0 393 nsresult res = aEnc->Reset();
michael@0 394
michael@0 395 if (NS_FAILED(res)) {
michael@0 396 printf("ERROR at %s.enc.Reset() code=0x%x.\n",aTestName,res);
michael@0 397 return res;
michael@0 398 }
michael@0 399
michael@0 400 return res;
michael@0 401 }
michael@0 402
michael@0 403 /**
michael@0 404 * A standard decoder test.
michael@0 405 */
michael@0 406 nsresult standardDecoderTest(char * aTestName, char * aCharset, char * aSrc,
michael@0 407 int32_t aSrcLen, char16_t * aRes, int32_t aResLen)
michael@0 408 {
michael@0 409 printf("\n[%s] Unicode <- %s\n", aTestName, aCharset);
michael@0 410
michael@0 411 // create converter
michael@0 412 CREATE_DECODER(aCharset);
michael@0 413
michael@0 414 // test converter - easy test
michael@0 415 res = testDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName);
michael@0 416
michael@0 417 // reset converter
michael@0 418 if (NS_SUCCEEDED(res)) res = resetDecoder(dec, aTestName);
michael@0 419
michael@0 420 // test converter - stress test
michael@0 421 if (NS_SUCCEEDED(res))
michael@0 422 res = testStressDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName);
michael@0 423
michael@0 424 // release converter
michael@0 425 NS_RELEASE(dec);
michael@0 426
michael@0 427 if (NS_FAILED(res)) {
michael@0 428 return res;
michael@0 429 } else {
michael@0 430 printf("Test Passed.\n");
michael@0 431 return NS_OK;
michael@0 432 }
michael@0 433 }
michael@0 434
michael@0 435 nsresult loadBinaryFile(char * aFile, char * aBuff, int32_t * aBuffLen)
michael@0 436 {
michael@0 437 FILE * f = fopen(aFile, "rb");
michael@0 438 if (!f) {
michael@0 439 printf("ERROR at opening file: \"%s\".\n", aFile);
michael@0 440 return NS_ERROR_UNEXPECTED;
michael@0 441 }
michael@0 442
michael@0 443 int32_t n = fread(aBuff, 1, *aBuffLen, f);
michael@0 444 if (n >= *aBuffLen) {
michael@0 445 printf("ERROR at reading from file \"%s\": too much input data.\n", aFile);
michael@0 446 return NS_ERROR_UNEXPECTED;
michael@0 447 }
michael@0 448
michael@0 449 *aBuffLen = n;
michael@0 450 fclose(f);
michael@0 451 return NS_OK;
michael@0 452 }
michael@0 453
michael@0 454 nsresult loadUnicodeFile(char * aFile, char16_t * aBuff, int32_t * aBuffLen)
michael@0 455 {
michael@0 456 int32_t buffLen = 2*(*aBuffLen);
michael@0 457
michael@0 458 nsresult res = loadBinaryFile(aFile, (char *)aBuff, &buffLen);
michael@0 459 if (NS_FAILED(res)) return res;
michael@0 460
michael@0 461 *aBuffLen = buffLen/2;
michael@0 462 return NS_OK;
michael@0 463 }
michael@0 464
michael@0 465 nsresult testDecoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile)
michael@0 466 {
michael@0 467 // create converter
michael@0 468 CREATE_DECODER(aCharset);
michael@0 469
michael@0 470 int32_t srcLen = GENERAL_BUFFER;
michael@0 471 char src[GENERAL_BUFFER];
michael@0 472 int32_t expLen = GENERAL_BUFFER/2;
michael@0 473 char16_t exp[GENERAL_BUFFER/2];
michael@0 474
michael@0 475 res = loadBinaryFile(aSrcFile, src, &srcLen);
michael@0 476 if (NS_FAILED(res)) return res;
michael@0 477
michael@0 478 res = loadUnicodeFile(aResultFile, exp, &expLen);
michael@0 479 if (NS_FAILED(res)) return res;
michael@0 480
michael@0 481 // test converter - easy test
michael@0 482 res = testDecoder(dec, src, srcLen, exp, expLen, "dec");
michael@0 483
michael@0 484 // release converter
michael@0 485 NS_RELEASE(dec);
michael@0 486
michael@0 487 if (NS_FAILED(res)) {
michael@0 488 return res;
michael@0 489 } else {
michael@0 490 printf("Test Passed.\n");
michael@0 491 return NS_OK;
michael@0 492 }
michael@0 493
michael@0 494 return NS_OK;
michael@0 495 }
michael@0 496
michael@0 497 nsresult testEncoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile)
michael@0 498 {
michael@0 499 // XXX write me
michael@0 500 return NS_OK;
michael@0 501 }
michael@0 502
michael@0 503 //----------------------------------------------------------------------
michael@0 504 // Decoders testing functions
michael@0 505
michael@0 506 /**
michael@0 507 * Test the ISO2022JP decoder.
michael@0 508 */
michael@0 509 nsresult testISO2022JPDecoder()
michael@0 510 {
michael@0 511 char * testName = "T102";
michael@0 512 printf("\n[%s] Unicode <- ISO2022JP\n", testName);
michael@0 513
michael@0 514 // create converter
michael@0 515 CREATE_DECODER("iso-2022-jp");
michael@0 516
michael@0 517 // test data
michael@0 518 char src[] = {"\x0d\x7f\xdd" "\x1b(J\xaa\xdc\x41" "\x1b$B\x21\x21" "\x1b$@\x32\x37" "\x1b(J\x1b(B\xcc"};
michael@0 519 char16_t exp[] = {0x000d,0x007f,0xfffd, 0xff6a,0xFF9C,0x0041, 0x3000, 0x5378, 0xfffd};
michael@0 520
michael@0 521 // test converter - normal operation
michael@0 522 res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 523
michael@0 524 // reset converter
michael@0 525 if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
michael@0 526
michael@0 527 // test converter - stress test
michael@0 528 if (NS_SUCCEEDED(res))
michael@0 529 res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 530
michael@0 531 // release converter
michael@0 532 NS_RELEASE(dec);
michael@0 533
michael@0 534 if (NS_FAILED(res)) {
michael@0 535 return res;
michael@0 536 } else {
michael@0 537 printf("Test Passed.\n");
michael@0 538 return NS_OK;
michael@0 539 }
michael@0 540 }
michael@0 541
michael@0 542 /**
michael@0 543 * Test the EUCJP decoder.
michael@0 544 */
michael@0 545 nsresult testEUCJPDecoder()
michael@0 546 {
michael@0 547 char * testName = "T103";
michael@0 548 printf("\n[%s] Unicode <- EUCJP\n", testName);
michael@0 549
michael@0 550 // create converter
michael@0 551 CREATE_DECODER("euc-jp");
michael@0 552
michael@0 553 // test data
michael@0 554 char src[] = {"\x45"};
michael@0 555 char16_t exp[] = {0x0045};
michael@0 556
michael@0 557 // test converter - normal operation
michael@0 558 res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 559
michael@0 560 // reset converter
michael@0 561 if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
michael@0 562
michael@0 563 // test converter - stress test
michael@0 564 if (NS_SUCCEEDED(res))
michael@0 565 res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 566
michael@0 567 // release converter
michael@0 568 NS_RELEASE(dec);
michael@0 569
michael@0 570 if (NS_FAILED(res)) {
michael@0 571 return res;
michael@0 572 } else {
michael@0 573 printf("Test Passed.\n");
michael@0 574 return NS_OK;
michael@0 575 }
michael@0 576 }
michael@0 577
michael@0 578 /**
michael@0 579 * Test the ISO88597 decoder.
michael@0 580 */
michael@0 581 nsresult testISO88597Decoder()
michael@0 582 {
michael@0 583 char * testName = "T104";
michael@0 584 printf("\n[%s] Unicode <- ISO88597\n", testName);
michael@0 585
michael@0 586 // create converter
michael@0 587 CREATE_DECODER("iso-8859-7");
michael@0 588
michael@0 589 // test data
michael@0 590 char src[] = {
michael@0 591 "\x09\x0d\x20\x40"
michael@0 592 "\x80\x98\xa3\xaf"
michael@0 593 "\xa7\xb1\xb3\xc9"
michael@0 594 "\xd9\xe3\xf4\xff"
michael@0 595 };
michael@0 596 char16_t exp[] = {
michael@0 597 0x0009, 0x000d, 0x0020, 0x0040,
michael@0 598 0xfffd, 0xfffd, 0x00a3, 0x2015,
michael@0 599 0x00a7, 0x00b1, 0x00b3, 0x0399,
michael@0 600 0x03a9, 0x03b3, 0x03c4, 0xfffd
michael@0 601 };
michael@0 602
michael@0 603 // test converter - normal operation
michael@0 604 res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 605
michael@0 606 // reset converter
michael@0 607 if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
michael@0 608
michael@0 609 // test converter - stress test
michael@0 610 if (NS_SUCCEEDED(res))
michael@0 611 res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 612
michael@0 613 // release converter
michael@0 614 NS_RELEASE(dec);
michael@0 615
michael@0 616 if (NS_FAILED(res)) {
michael@0 617 return res;
michael@0 618 } else {
michael@0 619 printf("Test Passed.\n");
michael@0 620 return NS_OK;
michael@0 621 }
michael@0 622 }
michael@0 623
michael@0 624 /**
michael@0 625 * Test the SJIS decoder.
michael@0 626 */
michael@0 627 nsresult testSJISDecoder()
michael@0 628 {
michael@0 629 char * testName = "T105";
michael@0 630 printf("\n[%s] Unicode <- SJIS\n", testName);
michael@0 631
michael@0 632 // create converter
michael@0 633 CREATE_DECODER("Shift_JIS");
michael@0 634
michael@0 635 // test data
michael@0 636 char src[] = {
michael@0 637 "Japanese" /* English */
michael@0 638 "\x8a\xbf\x8e\x9a" /* Kanji */
michael@0 639 "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */
michael@0 640 "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */
michael@0 641 "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */
michael@0 642 };
michael@0 643 char16_t exp[] = {
michael@0 644 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
michael@0 645 0x6f22, 0x5b57,
michael@0 646 0x30ab, 0x30bf, 0x30ab, 0x30ca,
michael@0 647 0x3072, 0x3089, 0x304c, 0x306a,
michael@0 648 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
michael@0 649 };
michael@0 650
michael@0 651 // test converter - normal operation
michael@0 652 res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 653
michael@0 654 // reset converter
michael@0 655 if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
michael@0 656
michael@0 657 // test converter - stress test
michael@0 658 if (NS_SUCCEEDED(res))
michael@0 659 res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 660
michael@0 661 // release converter
michael@0 662 NS_RELEASE(dec);
michael@0 663
michael@0 664 if (NS_FAILED(res)) {
michael@0 665 return res;
michael@0 666 } else {
michael@0 667 printf("Test Passed.\n");
michael@0 668 return NS_OK;
michael@0 669 }
michael@0 670 }
michael@0 671
michael@0 672 /**
michael@0 673 * Test the UTF8 decoder.
michael@0 674 */
michael@0 675 nsresult testUTF8Decoder()
michael@0 676 {
michael@0 677 char * testName = "T106";
michael@0 678 printf("\n[%s] Unicode <- UTF8\n", testName);
michael@0 679
michael@0 680 // create converter
michael@0 681 CREATE_DECODER("utf-8");
michael@0 682
michael@0 683 #ifdef NOPE // XXX decomment this when I have test data
michael@0 684 // test data
michael@0 685 char src[] = {};
michael@0 686 char16_t exp[] = {};
michael@0 687
michael@0 688 // test converter - normal operation
michael@0 689 res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 690
michael@0 691 // reset converter
michael@0 692 if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
michael@0 693
michael@0 694 // test converter - stress test
michael@0 695 if (NS_SUCCEEDED(res))
michael@0 696 res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
michael@0 697 #endif
michael@0 698
michael@0 699 // release converter
michael@0 700 NS_RELEASE(dec);
michael@0 701
michael@0 702 if (NS_FAILED(res)) {
michael@0 703 return res;
michael@0 704 } else {
michael@0 705 printf("Test Passed.\n");
michael@0 706 return NS_OK;
michael@0 707 }
michael@0 708 }
michael@0 709
michael@0 710 //----------------------------------------------------------------------
michael@0 711 // Encoders testing functions
michael@0 712
michael@0 713 /**
michael@0 714 * Test the Latin1 encoder.
michael@0 715 */
michael@0 716 nsresult testLatin1Encoder()
michael@0 717 {
michael@0 718 char * testName = "T201";
michael@0 719 printf("\n[%s] Unicode -> Latin1\n", testName);
michael@0 720
michael@0 721 // create converter
michael@0 722 CREATE_ENCODER("iso-8859-1");
michael@0 723 enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc);
michael@0 724
michael@0 725 // test data
michael@0 726 char16_t src[] = {0x0001,0x0002,0xffff,0x00e3};
michael@0 727 char exp[] = {"\x01\x02\xcc\xe3"};
michael@0 728
michael@0 729 // test converter - easy test
michael@0 730 res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
michael@0 731
michael@0 732 // reset converter
michael@0 733 if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
michael@0 734
michael@0 735 // test converter - stress test
michael@0 736 if (NS_SUCCEEDED(res))
michael@0 737 res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
michael@0 738
michael@0 739 // release converter
michael@0 740 NS_RELEASE(enc);
michael@0 741
michael@0 742 if (NS_FAILED(res)) {
michael@0 743 return res;
michael@0 744 } else {
michael@0 745 printf("Test Passed.\n");
michael@0 746 return NS_OK;
michael@0 747 }
michael@0 748 }
michael@0 749
michael@0 750 /**
michael@0 751 * Test the Shift-JIS encoder.
michael@0 752 */
michael@0 753 nsresult testSJISEncoder()
michael@0 754 {
michael@0 755 char * testName = "T202";
michael@0 756 printf("\n[%s] Unicode -> SJIS\n", testName);
michael@0 757
michael@0 758 // create converter
michael@0 759 CREATE_ENCODER("Shift_JIS");
michael@0 760 enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc);
michael@0 761
michael@0 762 // test data
michael@0 763 char16_t src[] = {
michael@0 764 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
michael@0 765 0x6f22, 0x5b57,
michael@0 766 0x30ab, 0x30bf, 0x30ab, 0x30ca,
michael@0 767 0x3072, 0x3089, 0x304c, 0x306a,
michael@0 768 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
michael@0 769 };
michael@0 770 char exp[] = {
michael@0 771 "Japanese" /* English */
michael@0 772 "\x8a\xbf\x8e\x9a" /* Kanji */
michael@0 773 "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */
michael@0 774 "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */
michael@0 775 "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */
michael@0 776 };
michael@0 777
michael@0 778 // test converter - easy test
michael@0 779 res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
michael@0 780
michael@0 781 // reset converter
michael@0 782 if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
michael@0 783
michael@0 784 // test converter - stress test
michael@0 785 if (NS_SUCCEEDED(res))
michael@0 786 res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
michael@0 787
michael@0 788 // release converter
michael@0 789 NS_RELEASE(enc);
michael@0 790
michael@0 791 if (NS_FAILED(res)) {
michael@0 792 return res;
michael@0 793 } else {
michael@0 794 printf("Test Passed.\n");
michael@0 795 return NS_OK;
michael@0 796 }
michael@0 797 }
michael@0 798
michael@0 799 /**
michael@0 800 * Test the EUC-JP encoder.
michael@0 801 */
michael@0 802 nsresult testEUCJPEncoder()
michael@0 803 {
michael@0 804 char * testName = "T203";
michael@0 805 printf("\n[%s] Unicode -> EUCJP\n", testName);
michael@0 806
michael@0 807 // create converter
michael@0 808 CREATE_ENCODER("euc-jp");
michael@0 809 enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc);
michael@0 810
michael@0 811 // test data
michael@0 812 char16_t src[] = {0x0045, 0x0054};
michael@0 813 char exp[] = {"\x45\x54"};
michael@0 814
michael@0 815 // test converter - easy test
michael@0 816 res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
michael@0 817
michael@0 818 // reset converter
michael@0 819 if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
michael@0 820
michael@0 821 // test converter - stress test
michael@0 822 if (NS_SUCCEEDED(res))
michael@0 823 res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
michael@0 824
michael@0 825 // release converter
michael@0 826 NS_RELEASE(enc);
michael@0 827
michael@0 828 if (NS_FAILED(res)) {
michael@0 829 return res;
michael@0 830 } else {
michael@0 831 printf("Test Passed.\n");
michael@0 832 return NS_OK;
michael@0 833 }
michael@0 834 }
michael@0 835
michael@0 836 /**
michael@0 837 * Test the ISO-2022-JP encoder.
michael@0 838 */
michael@0 839 nsresult testISO2022JPEncoder()
michael@0 840 {
michael@0 841 char * testName = "T204";
michael@0 842 printf("\n[%s] Unicode -> ISO2022JP\n", testName);
michael@0 843
michael@0 844 // create converter
michael@0 845 CREATE_ENCODER("iso-2022-jp");
michael@0 846 enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc);
michael@0 847
michael@0 848 // test data
michael@0 849 char16_t src[] = {0x000d,0x007f, 0xff6a,0xFF9C, 0x3000, 0x5378};
michael@0 850 char exp[] = {"\x0d\x7f" "\x1b(J\xaa\xdc" "\x1b$@\x21\x21\x32\x37\x1b(B"};
michael@0 851
michael@0 852 // test converter - easy test
michael@0 853 res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
michael@0 854
michael@0 855 // reset converter
michael@0 856 if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
michael@0 857
michael@0 858 // test converter - stress test
michael@0 859 if (NS_SUCCEEDED(res))
michael@0 860 res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
michael@0 861
michael@0 862 // release converter
michael@0 863 NS_RELEASE(enc);
michael@0 864
michael@0 865 if (NS_FAILED(res)) {
michael@0 866 return res;
michael@0 867 } else {
michael@0 868 printf("Test Passed.\n");
michael@0 869 return NS_OK;
michael@0 870 }
michael@0 871 }
michael@0 872
michael@0 873 nsresult testPlatformCharset()
michael@0 874 {
michael@0 875 nsIPlatformCharset *cinfo;
michael@0 876 nsresult res = CallGetService(kPlatformCharsetCID, &cinfo);
michael@0 877 if (NS_FAILED(res)) {
michael@0 878 printf("ERROR at GetService() code=0x%x.\n",res);
michael@0 879 return res;
michael@0 880 }
michael@0 881
michael@0 882 nsString value;
michael@0 883 res = cinfo->GetCharset(kPlatformCharsetSel_PlainTextInClipboard , value);
michael@0 884 printf("Clipboard plain text encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get());
michael@0 885
michael@0 886 res = cinfo->GetCharset(kPlatformCharsetSel_FileName , value);
michael@0 887 printf("File Name encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get());
michael@0 888
michael@0 889 res = cinfo->GetCharset(kPlatformCharsetSel_Menu , value);
michael@0 890 printf("Menu encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get());
michael@0 891
michael@0 892 cinfo->Release();
michael@0 893 return res;
michael@0 894
michael@0 895 }
michael@0 896
michael@0 897 //----------------------------------------------------------------------
michael@0 898 // Testing functions
michael@0 899
michael@0 900 nsresult testAll()
michael@0 901 {
michael@0 902 nsresult res;
michael@0 903
michael@0 904 // test the manager(s)
michael@0 905 res = testCharsetConverterManager();
michael@0 906 if (NS_FAILED(res)) return res;
michael@0 907
michael@0 908 testPlatformCharset();
michael@0 909
michael@0 910 // test decoders
michael@0 911 standardDecoderTest("T101", "ISO-8859-1", bLatin1_d0, bLatin1_s0, cLatin1_d0, cLatin1_s0);
michael@0 912 testISO2022JPDecoder();
michael@0 913 testEUCJPDecoder();
michael@0 914 testISO88597Decoder();
michael@0 915 testSJISDecoder();
michael@0 916 testUTF8Decoder();
michael@0 917 testMUTF7Decoder();
michael@0 918 testUTF7Decoder();
michael@0 919
michael@0 920 // test encoders
michael@0 921 testLatin1Encoder();
michael@0 922 testSJISEncoder();
michael@0 923 testEUCJPEncoder();
michael@0 924 testISO2022JPEncoder();
michael@0 925 testMUTF7Encoder();
michael@0 926 testUTF7Encoder();
michael@0 927
michael@0 928 // return
michael@0 929 return NS_OK;
michael@0 930 }
michael@0 931
michael@0 932 nsresult testFromArgs(int argc, char **argv)
michael@0 933 {
michael@0 934 nsresult res = NS_OK;
michael@0 935 if ((argc == 5) && (!strcmp(argv[1], "-tdec"))) {
michael@0 936 res = testDecoderFromFiles(argv[2], argv[3], argv[4]);
michael@0 937 } else if ((argc == 5) && (!strcmp(argv[1], "-tenc"))) {
michael@0 938 res = testEncoderFromFiles(argv[2], argv[3], argv[4]);
michael@0 939 } else {
michael@0 940 printf("Usage:\n");
michael@0 941 printf(" TestUConv.exe\n");
michael@0 942 printf(" TestUConv.exe -tdec encoding inputEncodedFile expectedResultUnicodeFile\n");
michael@0 943 printf(" TestUConv.exe -tenc encoding inputUnicodeFile expectedResultEncodedFile\n");
michael@0 944 }
michael@0 945
michael@0 946 return res;
michael@0 947 }
michael@0 948
michael@0 949 //----------------------------------------------------------------------
michael@0 950 // Main program functions
michael@0 951
michael@0 952 nsresult init()
michael@0 953 {
michael@0 954 nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
michael@0 955 if (NS_FAILED(rv))
michael@0 956 return rv;
michael@0 957 return CallGetService(kCharsetConverterManagerCID, &ccMan);
michael@0 958 }
michael@0 959
michael@0 960 nsresult done()
michael@0 961 {
michael@0 962 NS_RELEASE(ccMan);
michael@0 963 return NS_OK;
michael@0 964 }
michael@0 965
michael@0 966 int main(int argc, char **argv)
michael@0 967 {
michael@0 968 nsresult res;
michael@0 969
michael@0 970 res = init();
michael@0 971 if (NS_FAILED(res)) return -1;
michael@0 972
michael@0 973 if (argc <= 1) {
michael@0 974 printf("*** Unicode Converters Test ***\n");
michael@0 975 res = testAll();
michael@0 976 printf("\n***--------- Done --------***\n");
michael@0 977 } else {
michael@0 978 res = testFromArgs(argc, argv);
michael@0 979 }
michael@0 980
michael@0 981 done();
michael@0 982
michael@0 983 if (NS_FAILED(res)) return -1;
michael@0 984 else return 0;
michael@0 985 }

mercurial