1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.archivers.zip;
21
22 import static java.nio.charset.StandardCharsets.ISO_8859_1;
23 import static java.nio.charset.StandardCharsets.US_ASCII;
24 import static java.nio.charset.StandardCharsets.UTF_16BE;
25 import static java.nio.charset.StandardCharsets.UTF_8;
26 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28
29 import java.nio.ByteBuffer;
30 import java.util.Arrays;
31
32 import org.junit.jupiter.api.Test;
33
34 public class NioZipEncodingTest {
35
36 private static final String UMLAUTS = "\u00e4\u00f6\u00fc";
37
38 private static final String RAINBOW_EMOJI = "\ud83c\udf08";
39
40 @Test
41 public void testPartialSurrogatePair() {
42 final NioZipEncoding e = new NioZipEncoding(US_ASCII, false);
43 final ByteBuffer bb = e.encode("\ud83c");
44 final int off = bb.arrayOffset();
45 final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
46 assertEquals(0, result.length);
47 }
48
49 @Test
50 public void testRainbowEmojiToSurrogatePairUTF16() {
51 final NioZipEncoding e = new NioZipEncoding(UTF_16BE, false);
52 final ByteBuffer bb = e.encode(RAINBOW_EMOJI);
53 final int off = bb.arrayOffset();
54 final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
55 assertArrayEquals(RAINBOW_EMOJI.getBytes(UTF_16BE), result);
56 }
57
58 @Test
59 public void testUmlautToISO88591() {
60 final NioZipEncoding e = new NioZipEncoding(ISO_8859_1, true);
61 final ByteBuffer bb = e.encode("\u00e4\u00f6\u00fc");
62 final int off = bb.arrayOffset();
63 final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
64 assertArrayEquals(UMLAUTS.getBytes(ISO_8859_1), result);
65 }
66
67 @Test
68 public void testUmlautToUTF16BE() {
69 final NioZipEncoding e = new NioZipEncoding(UTF_16BE, false);
70 final ByteBuffer bb = e.encode(UMLAUTS);
71 final int off = bb.arrayOffset();
72 final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
73 assertArrayEquals(UMLAUTS.getBytes(UTF_16BE), result);
74 }
75
76 @Test
77 public void testUmlautToUTF8() {
78 final NioZipEncoding e = new NioZipEncoding(UTF_8, true);
79 final ByteBuffer bb = e.encode("\u00e4\u00f6\u00fc");
80 final int off = bb.arrayOffset();
81 final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
82 assertArrayEquals(UMLAUTS.getBytes(UTF_8), result);
83 }
84
85 @Test
86 public void testUnmappableRainbowEmoji() {
87 final NioZipEncoding e = new NioZipEncoding(US_ASCII, false);
88 final ByteBuffer bb = e.encode(RAINBOW_EMOJI);
89 final int off = bb.arrayOffset();
90 final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
91 assertEquals("%UD83C%UDF08", new String(result, US_ASCII));
92 }
93
94 @Test
95 public void testUnmappableUmlauts() {
96 final NioZipEncoding e = new NioZipEncoding(US_ASCII, false);
97 final ByteBuffer bb = e.encode("\u00e4\u00f6\u00fc");
98 final int off = bb.arrayOffset();
99 final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
100 assertEquals("%U00E4%U00F6%U00FC", new String(result, US_ASCII));
101 }
102 }