1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 
17 package android.util;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import androidx.test.runner.AndroidJUnit4;
22 
23 import com.android.internal.util.HexDump;
24 
25 import dalvik.system.VMRuntime;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 @RunWith(AndroidJUnit4.class)
32 public class CharsetUtilsTest {
33     private byte[] dest;
34     private long destPtr;
35 
36     @Before
setUp()37     public void setUp() {
38         dest = (byte[]) VMRuntime.getRuntime().newNonMovableArray(byte.class, 8);
39         destPtr = VMRuntime.getRuntime().addressOf(dest);
40     }
41 
42     @Test
testModifiedUtf8_Empty()43     public void testModifiedUtf8_Empty() {
44         assertEquals(0, CharsetUtils.toModifiedUtf8Bytes("", destPtr, 0, dest.length));
45         assertEquals("0000000000000000", HexDump.toHexString(dest));
46         assertEquals("", CharsetUtils.fromModifiedUtf8Bytes(destPtr, 0, 0));
47     }
48 
49     @Test
testModifiedUtf8_Null()50     public void testModifiedUtf8_Null() {
51         assertEquals(4, CharsetUtils.toModifiedUtf8Bytes("!\0!", destPtr, 0, dest.length));
52         assertEquals("21C0802100000000", HexDump.toHexString(dest));
53         assertEquals("!\0!", CharsetUtils.fromModifiedUtf8Bytes(destPtr, 0, 4));
54     }
55 
56     @Test
testModifiedUtf8_Simple()57     public void testModifiedUtf8_Simple() {
58         assertEquals(7, CharsetUtils.toModifiedUtf8Bytes("example", destPtr, 0, dest.length));
59         assertEquals("6578616D706C6500", HexDump.toHexString(dest));
60         assertEquals("example", CharsetUtils.fromModifiedUtf8Bytes(destPtr, 0, 7));
61     }
62 
63     @Test
testModifiedUtf8_Complex()64     public void testModifiedUtf8_Complex() {
65         assertEquals(3, CharsetUtils.toModifiedUtf8Bytes("☃", destPtr, 4, dest.length));
66         assertEquals("00000000E2988300", HexDump.toHexString(dest));
67         assertEquals("☃", CharsetUtils.fromModifiedUtf8Bytes(destPtr, 4, 3));
68     }
69 
70     @Test
testModifiedUtf8_Bounds()71     public void testModifiedUtf8_Bounds() {
72         assertEquals(-3, CharsetUtils.toModifiedUtf8Bytes("foo", destPtr, 0, 0));
73         assertEquals(-3, CharsetUtils.toModifiedUtf8Bytes("foo", destPtr, 0, 2));
74         assertEquals(-3, CharsetUtils.toModifiedUtf8Bytes("foo", destPtr, -2, 8));
75         assertEquals(-3, CharsetUtils.toModifiedUtf8Bytes("foo", destPtr, 6, 8));
76         assertEquals(-3, CharsetUtils.toModifiedUtf8Bytes("foo", destPtr, 10, 8));
77     }
78 
79     @Test
testModifiedUtf8_Overwrite()80     public void testModifiedUtf8_Overwrite() {
81         assertEquals(5, CharsetUtils.toModifiedUtf8Bytes("!!!!!", destPtr, 0, dest.length));
82         assertEquals(3, CharsetUtils.toModifiedUtf8Bytes("...", destPtr, 0, dest.length));
83         assertEquals(1, CharsetUtils.toModifiedUtf8Bytes("?", destPtr, 0, dest.length));
84         assertEquals("3F002E0021000000", HexDump.toHexString(dest));
85     }
86 }
87