| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib.res.decoder; |
| 18 |
|
| 19 |
import org.junit.Test; |
| 20 |
|
| 21 |
import java.nio.charset.StandardCharsets; |
| 22 |
|
| 23 |
import static org.junit.Assert.assertEquals; |
| 24 |
|
| 25 |
public class StringBlockWithSurrogatePairInUtf8Test { |
| 26 |
@Test |
| 27 |
public void decodeSingleOctet() { |
| 28 |
final String actual = new StringBlock("abcDEF123".getBytes(StandardCharsets.UTF_8), true).decodeString(0, 9); |
| 29 |
assertEquals("Incorrect decoding", "abcDEF123", actual); |
| 30 |
} |
| 31 |
|
| 32 |
@Test |
| 33 |
public void decodeTwoOctets() { |
| 34 |
final String actual0 = new StringBlock(new byte[] { (byte) 0xC2, (byte) 0x80}, true).decodeString(0, 2); |
| 35 |
assertEquals("Incorrect decoding", "\u0080", actual0); |
| 36 |
|
| 37 |
final String actual1 = new StringBlock(new byte[] { (byte) 0xDF, (byte) 0xBF}, true).decodeString(0, 2); |
| 38 |
assertEquals("Incorrect decoding", "\u07FF", actual1); |
| 39 |
} |
| 40 |
|
| 41 |
@Test |
| 42 |
public void decodeThreeOctets() { |
| 43 |
final String actual0 = new StringBlock(new byte[] { (byte) 0xE0, (byte) 0xA0, (byte) 0x80}, true).decodeString(0, 3); |
| 44 |
assertEquals("Incorrect decoding", "\u0800", actual0); |
| 45 |
|
| 46 |
final String actual1 = new StringBlock(new byte[] { (byte) 0xEF, (byte) 0xBF, (byte) 0xBF}, true).decodeString(0, 3); |
| 47 |
assertEquals("Incorrect decoding", "\uFFFF", actual1); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void decodeSurrogatePair_when_givesAsThreeOctetsFromInvalidRangeOfUtf8() { |
| 52 |
// See: https://github.com/iBotPeaches/Apktool/issues/2299 |
| 53 |
final String actual = new StringBlock(new byte[] { (byte) 0xED, (byte) 0xA0, (byte) 0xBD, (byte) 0xED, (byte) 0xB4, (byte) 0x86}, true).decodeString(0, 6); |
| 54 |
assertEquals("Incorrect decoding", "\uD83D\uDD06", actual); |
| 55 |
|
| 56 |
// See: https://github.com/iBotPeaches/Apktool/issues/2546 |
| 57 |
final byte[] bytesWithCharactersBeforeSurrogatePair = {'G', 'o', 'o', 'd', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g', '!', ' ', |
| 58 |
(byte) 0xED, (byte) 0xA0, (byte) 0xBD, (byte) 0xED, (byte) 0xB1, (byte) 0x8B, |
| 59 |
' ', 'S', 'u', 'n', ' ', |
| 60 |
(byte) 0xED, (byte) 0xA0, (byte) 0xBC, (byte) 0xED, (byte) 0xBC, (byte) 0x9E |
| 61 |
}; |
| 62 |
final String actual2 = new StringBlock(bytesWithCharactersBeforeSurrogatePair, true).decodeString(0, 31); |
| 63 |
|
| 64 |
// D83D -> 0xED 0xA0 0xBD |
| 65 |
// DC4B -> 0xED 0xB1 0x8B |
| 66 |
// D83C -> 0xED 0xA0 0xBC |
| 67 |
// DF1E -> 0xED 0xBC 0x9E |
| 68 |
assertEquals("Incorrect decoding when there are valid characters before the surrogate pair", |
| 69 |
"Good morning! \uD83D\uDC4B Sun \uD83C\uDF1E", actual2); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void decodeSurrogatePair_when_givesAsThreeOctetsFromTheValidRangeOfUtf8() { |
| 74 |
// \u10FFFF is encoded in UTF-8 as "0xDBFF 0xDFFF" (4-byte encoding), |
| 75 |
// but when used in Android resources which are encoded in UTF-8, 3-byte encoding is used, |
| 76 |
// so each of these is encoded as 3-bytes |
| 77 |
final String actual = new StringBlock(new byte[] { (byte) 0xED, (byte) 0xAF, (byte) 0xBF, (byte) 0xED, (byte) 0xBF, (byte) 0xBF}, true).decodeString(0, 6); |
| 78 |
assertEquals("Incorrect decoding", "\uDBFF\uDFFF", actual); |
| 79 |
} |
| 80 |
} |