2
0

testxlate.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "apr.h"
  19. #include "apr_errno.h"
  20. #include "apr_general.h"
  21. #include "apr_strings.h"
  22. #include "apr_xlate.h"
  23. static const char test_utf8[] = "Edelwei\xc3\x9f";
  24. static const char test_utf7[] = "Edelwei+AN8-";
  25. static const char test_latin1[] = "Edelwei\xdf";
  26. static const char test_latin2[] = "Edelwei\xdf";
  27. static int check_status (apr_status_t status, const char *msg)
  28. {
  29. if (status)
  30. {
  31. static char buf[1024];
  32. printf("ERROR: %s\n %s\n", msg,
  33. apr_strerror(status, buf, sizeof(buf)));
  34. return 1;
  35. }
  36. return 0;
  37. }
  38. static int test_conversion (apr_xlate_t *convset,
  39. const char *inbuf,
  40. const char *expected)
  41. {
  42. static char buf[1024];
  43. int retcode = 0;
  44. apr_size_t inbytes_left = strlen(inbuf);
  45. apr_size_t outbytes_left = sizeof(buf) - 1;
  46. apr_status_t status = apr_xlate_conv_buffer(convset,
  47. inbuf,
  48. &inbytes_left,
  49. buf,
  50. &outbytes_left);
  51. if (status == APR_SUCCESS) {
  52. status = apr_xlate_conv_buffer(convset, NULL, NULL,
  53. buf + sizeof(buf) - outbytes_left - 1,
  54. &outbytes_left);
  55. }
  56. buf[sizeof(buf) - outbytes_left - 1] = '\0';
  57. retcode |= check_status(status, "apr_xlate_conv_buffer");
  58. if ((!status || APR_STATUS_IS_INCOMPLETE(status))
  59. && strcmp(buf, expected))
  60. {
  61. printf("ERROR: expected: '%s'\n actual: '%s'"
  62. "\n inbytes_left: %"APR_SIZE_T_FMT"\n",
  63. expected, buf, inbytes_left);
  64. retcode |= 1;
  65. }
  66. return retcode;
  67. }
  68. static int one_test (const char *cs1, const char *cs2,
  69. const char *str1, const char *str2,
  70. apr_pool_t *pool)
  71. {
  72. apr_xlate_t *convset;
  73. const char *msg = apr_psprintf(pool, "apr_xlate_open(%s, %s)", cs2, cs1);
  74. int retcode = check_status(apr_xlate_open(&convset, cs2, cs1, pool), msg);
  75. if (!retcode)
  76. {
  77. retcode |= test_conversion(convset, str1, str2);
  78. retcode |= check_status(apr_xlate_close(convset), "apr_xlate_close");
  79. }
  80. printf("%s: %s -> %s\n", (retcode ? "FAIL" : "PASS"), cs1, cs2);
  81. return retcode;
  82. }
  83. int main (int argc, char **argv)
  84. {
  85. apr_pool_t *pool;
  86. int retcode = 0;
  87. #ifndef APR_HAS_XLATE
  88. puts("SKIP: apr_xlate not implemented");
  89. return 0;
  90. #endif
  91. apr_initialize();
  92. atexit(apr_terminate);
  93. apr_pool_create(&pool, NULL);
  94. /* 1. Identity transformation: UTF-8 -> UTF-8 */
  95. retcode |= one_test("UTF-8", "UTF-8", test_utf8, test_utf8, pool);
  96. /* 2. UTF-8 <-> ISO-8859-1 */
  97. retcode |= one_test("UTF-8", "ISO-8859-1", test_utf8, test_latin1, pool);
  98. retcode |= one_test("ISO-8859-1", "UTF-8", test_latin1, test_utf8, pool);
  99. /* 3. ISO-8859-1 <-> ISO-8859-2, identity */
  100. retcode |= one_test("ISO-8859-1", "ISO-8859-2",
  101. test_latin1, test_latin2, pool);
  102. retcode |= one_test("ISO-8859-2", "ISO-8859-1",
  103. test_latin2, test_latin1, pool);
  104. /* 4. Transformation using charset aliases */
  105. retcode |= one_test("UTF-8", "UTF-7", test_utf8, test_utf7, pool);
  106. retcode |= one_test("UTF-7", "UTF-8", test_utf7, test_utf8, pool);
  107. return retcode;
  108. }