HardenedBSD src tree
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.6KB

  1. /*-
  2. * Copyright (c) 2019 Martin Matuska
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "test.h"
  26. __FBSDID("$FreeBSD");
  27. #include <locale.h>
  28. static void
  29. test_read_format_lha_filename_UTF16_UTF8(const char *refname)
  30. {
  31. struct archive *a;
  32. struct archive_entry *ae;
  33. /*
  34. * Read LHA filename in en_US.UTF-8.
  35. */
  36. if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
  37. skipping("en_US.UTF-8 locale not available on this system.");
  38. return;
  39. }
  40. /*
  41. * Create a read object only for a test that platform support
  42. * a character-set conversion because we can read a character-set
  43. * of filenames from the header of an lha archive file and so we
  44. * want to test that it works well.
  45. */
  46. assert((a = archive_read_new()) != NULL);
  47. assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
  48. if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP932")) {
  49. assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  50. skipping("This system cannot convert character-set"
  51. " from CP932 to UTF-8.");
  52. return;
  53. }
  54. if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=UTF-16")) {
  55. assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  56. skipping("This system cannot convert character-set"
  57. " from UTF-16 to UTF-8.");
  58. return;
  59. }
  60. assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  61. assert((a = archive_read_new()) != NULL);
  62. assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
  63. assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
  64. assertEqualIntA(a, ARCHIVE_OK,
  65. archive_read_open_filename(a, refname, 10240));
  66. /* Note that usual Japanese filenames are tested in other cases */
  67. #if defined(__APPLE__)
  68. /* NFD normalization */
  69. /* U:O:A:u:o:a: */
  70. #define UMLAUT_DIRNAME "\x55\xcc\x88\x4f\xcc\x88\x41\xcc\x88\x75\xcc\x88\x6f"\
  71. "\xcc\x88\x61\xcc\x88/"
  72. /* a:o:u:A:O:U:.txt */
  73. #define UMLAUT_FNAME "\x61\xcc\x88\x6f\xcc\x88\x75\xcc\x88\x41\xcc\x88"\
  74. "\x4f\xcc\x88\x55\xcc\x88.txt"
  75. #else
  76. /* NFC normalization */
  77. /* U:O:A:u:o:a: */
  78. #define UMLAUT_DIRNAME "\xc3\x9c\xc3\x96\xc3\x84\xc3\xbc\xc3\xb6\xc3\xa4/"
  79. /* a:o:u:A:O:U:.txt */
  80. #define UMLAUT_FNAME "\xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c.txt"
  81. #endif
  82. /* "Test" in Japanese Katakana */
  83. #define KATAKANA_FNAME "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.txt"
  84. #define KATAKANA_DIRNAME "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88/"
  85. /* Verify regular file. U:O:A:u:o:a:/a:o:u:A:O:U:.txt */
  86. assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
  87. assertEqualString(UMLAUT_DIRNAME UMLAUT_FNAME, archive_entry_pathname(ae));
  88. assertEqualInt(12, archive_entry_size(ae));
  89. /* Verify directory. U:O:A:u:o:a:/ */
  90. assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
  91. assertEqualString(UMLAUT_DIRNAME, archive_entry_pathname(ae));
  92. assertEqualInt(0, archive_entry_size(ae));
  93. /* Verify regular file. U:O:A:u:o:a:/("Test" in Japanese).txt */
  94. assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
  95. assertEqualString(UMLAUT_DIRNAME KATAKANA_FNAME,
  96. archive_entry_pathname(ae));
  97. assertEqualInt(25, archive_entry_size(ae));
  98. /* Verify regular file. ("Test" in Japanese)/a:o:u:A:O:U:.txt */
  99. assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
  100. assertEqualString(KATAKANA_DIRNAME UMLAUT_FNAME,
  101. archive_entry_pathname(ae));
  102. assertEqualInt(12, archive_entry_size(ae));
  103. /* Verify directory. ("Test" in Japanese)/ */
  104. assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
  105. assertEqualString(KATAKANA_DIRNAME, archive_entry_pathname(ae));
  106. assertEqualInt(0, archive_entry_size(ae));
  107. /* Verify regular file. a:o:u:A:O:U:.txt */
  108. assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
  109. assertEqualString(UMLAUT_FNAME, archive_entry_pathname(ae));
  110. assertEqualInt(12, archive_entry_size(ae));
  111. /* End of archive. */
  112. assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
  113. /* Verify archive format. */
  114. assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
  115. assertEqualIntA(a, ARCHIVE_FORMAT_LHA, archive_format(a));
  116. /* Close the archive. */
  117. assertEqualInt(ARCHIVE_OK, archive_read_close(a));
  118. assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  119. }
  120. DEFINE_TEST(test_read_format_lha_filename_UTF16)
  121. {
  122. /* A sample file was created with Unlha32.dll. */
  123. const char *refname = "test_read_format_lha_filename_utf16.lzh";
  124. extract_reference_file(refname);
  125. test_read_format_lha_filename_UTF16_UTF8(refname);
  126. }