1 /*
2 * Copyright (C) 2011 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 #include "common_test.h"
18 #include "mirror/array.h"
19 #include "mirror/array-inl.h"
20 #include "mirror/object-inl.h"
21 #include "mirror/object_array-inl.h"
22 #include "mirror/string.h"
23 #include "scoped_thread_state_change.h"
24 #include "sirt_ref.h"
25 #include "utils.h"
26
27 namespace art {
28
29 std::string PrettyArguments(const char* signature);
30 std::string PrettyReturnType(const char* signature);
31
32 class UtilsTest : public CommonTest {
33 };
34
TEST_F(UtilsTest,PrettyDescriptor_ArrayReferences)35 TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
36 EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
37 EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
38 }
39
TEST_F(UtilsTest,PrettyDescriptor_ScalarReferences)40 TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
41 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
42 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
43 }
44
TEST_F(UtilsTest,PrettyDescriptor_PrimitiveArrays)45 TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
46 EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
47 EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
48 EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
49 EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
50 EXPECT_EQ("char[]", PrettyDescriptor("[C"));
51 EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
52 EXPECT_EQ("double[]", PrettyDescriptor("[D"));
53 EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
54 EXPECT_EQ("float[]", PrettyDescriptor("[F"));
55 EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
56 EXPECT_EQ("int[]", PrettyDescriptor("[I"));
57 EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
58 EXPECT_EQ("long[]", PrettyDescriptor("[J"));
59 EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
60 EXPECT_EQ("short[]", PrettyDescriptor("[S"));
61 EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
62 }
63
TEST_F(UtilsTest,PrettyDescriptor_PrimitiveScalars)64 TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
65 EXPECT_EQ("boolean", PrettyDescriptor("Z"));
66 EXPECT_EQ("byte", PrettyDescriptor("B"));
67 EXPECT_EQ("char", PrettyDescriptor("C"));
68 EXPECT_EQ("double", PrettyDescriptor("D"));
69 EXPECT_EQ("float", PrettyDescriptor("F"));
70 EXPECT_EQ("int", PrettyDescriptor("I"));
71 EXPECT_EQ("long", PrettyDescriptor("J"));
72 EXPECT_EQ("short", PrettyDescriptor("S"));
73 }
74
TEST_F(UtilsTest,PrettyArguments)75 TEST_F(UtilsTest, PrettyArguments) {
76 EXPECT_EQ("()", PrettyArguments("()V"));
77 EXPECT_EQ("(int)", PrettyArguments("(I)V"));
78 EXPECT_EQ("(int, int)", PrettyArguments("(II)V"));
79 EXPECT_EQ("(int, int, int[][])", PrettyArguments("(II[[I)V"));
80 EXPECT_EQ("(int, int, int[][], java.lang.Poop)", PrettyArguments("(II[[ILjava/lang/Poop;)V"));
81 EXPECT_EQ("(int, int, int[][], java.lang.Poop, java.lang.Poop[][])", PrettyArguments("(II[[ILjava/lang/Poop;[[Ljava/lang/Poop;)V"));
82 }
83
TEST_F(UtilsTest,PrettyReturnType)84 TEST_F(UtilsTest, PrettyReturnType) {
85 EXPECT_EQ("void", PrettyReturnType("()V"));
86 EXPECT_EQ("int", PrettyReturnType("()I"));
87 EXPECT_EQ("int[][]", PrettyReturnType("()[[I"));
88 EXPECT_EQ("java.lang.Poop", PrettyReturnType("()Ljava/lang/Poop;"));
89 EXPECT_EQ("java.lang.Poop[][]", PrettyReturnType("()[[Ljava/lang/Poop;"));
90 }
91
TEST_F(UtilsTest,PrettyTypeOf)92 TEST_F(UtilsTest, PrettyTypeOf) {
93 ScopedObjectAccess soa(Thread::Current());
94 EXPECT_EQ("null", PrettyTypeOf(NULL));
95
96 SirtRef<mirror::String> s(soa.Self(), mirror::String::AllocFromModifiedUtf8(soa.Self(), ""));
97 EXPECT_EQ("java.lang.String", PrettyTypeOf(s.get()));
98
99 SirtRef<mirror::ShortArray> a(soa.Self(), mirror::ShortArray::Alloc(soa.Self(), 2));
100 EXPECT_EQ("short[]", PrettyTypeOf(a.get()));
101
102 mirror::Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;");
103 ASSERT_TRUE(c != NULL);
104 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
105 EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o));
106 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass()));
107 }
108
TEST_F(UtilsTest,PrettyClass)109 TEST_F(UtilsTest, PrettyClass) {
110 ScopedObjectAccess soa(Thread::Current());
111 EXPECT_EQ("null", PrettyClass(NULL));
112 mirror::Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;");
113 ASSERT_TRUE(c != NULL);
114 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
115 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass()));
116 }
117
TEST_F(UtilsTest,PrettyClassAndClassLoader)118 TEST_F(UtilsTest, PrettyClassAndClassLoader) {
119 ScopedObjectAccess soa(Thread::Current());
120 EXPECT_EQ("null", PrettyClassAndClassLoader(NULL));
121 mirror::Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;");
122 ASSERT_TRUE(c != NULL);
123 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
124 EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass()));
125 }
126
TEST_F(UtilsTest,PrettyField)127 TEST_F(UtilsTest, PrettyField) {
128 ScopedObjectAccess soa(Thread::Current());
129 EXPECT_EQ("null", PrettyField(NULL));
130
131 mirror::Class* java_lang_String = class_linker_->FindSystemClass("Ljava/lang/String;");
132
133 mirror::ArtField* f;
134 f = java_lang_String->FindDeclaredInstanceField("count", "I");
135 EXPECT_EQ("int java.lang.String.count", PrettyField(f));
136 EXPECT_EQ("java.lang.String.count", PrettyField(f, false));
137 f = java_lang_String->FindDeclaredInstanceField("value", "[C");
138 EXPECT_EQ("char[] java.lang.String.value", PrettyField(f));
139 EXPECT_EQ("java.lang.String.value", PrettyField(f, false));
140 }
141
TEST_F(UtilsTest,PrettySize)142 TEST_F(UtilsTest, PrettySize) {
143 EXPECT_EQ("1GB", PrettySize(1 * GB));
144 EXPECT_EQ("2GB", PrettySize(2 * GB));
145 if (sizeof(size_t) > sizeof(uint32_t)) {
146 EXPECT_EQ("100GB", PrettySize(100 * GB));
147 }
148 EXPECT_EQ("1024KB", PrettySize(1 * MB));
149 EXPECT_EQ("10MB", PrettySize(10 * MB));
150 EXPECT_EQ("100MB", PrettySize(100 * MB));
151 EXPECT_EQ("1024B", PrettySize(1 * KB));
152 EXPECT_EQ("10KB", PrettySize(10 * KB));
153 EXPECT_EQ("100KB", PrettySize(100 * KB));
154 EXPECT_EQ("0B", PrettySize(0));
155 EXPECT_EQ("1B", PrettySize(1));
156 EXPECT_EQ("10B", PrettySize(10));
157 EXPECT_EQ("100B", PrettySize(100));
158 EXPECT_EQ("512B", PrettySize(512));
159 }
160
TEST_F(UtilsTest,PrettyDuration)161 TEST_F(UtilsTest, PrettyDuration) {
162 const uint64_t one_sec = 1000000000;
163 const uint64_t one_ms = 1000000;
164 const uint64_t one_us = 1000;
165
166 EXPECT_EQ("1s", PrettyDuration(1 * one_sec));
167 EXPECT_EQ("10s", PrettyDuration(10 * one_sec));
168 EXPECT_EQ("100s", PrettyDuration(100 * one_sec));
169 EXPECT_EQ("1.001s", PrettyDuration(1 * one_sec + one_ms));
170 EXPECT_EQ("1.000001s", PrettyDuration(1 * one_sec + one_us));
171 EXPECT_EQ("1.000000001s", PrettyDuration(1 * one_sec + 1));
172
173 EXPECT_EQ("1ms", PrettyDuration(1 * one_ms));
174 EXPECT_EQ("10ms", PrettyDuration(10 * one_ms));
175 EXPECT_EQ("100ms", PrettyDuration(100 * one_ms));
176 EXPECT_EQ("1.001ms", PrettyDuration(1 * one_ms + one_us));
177 EXPECT_EQ("1.000001ms", PrettyDuration(1 * one_ms + 1));
178
179 EXPECT_EQ("1us", PrettyDuration(1 * one_us));
180 EXPECT_EQ("10us", PrettyDuration(10 * one_us));
181 EXPECT_EQ("100us", PrettyDuration(100 * one_us));
182 EXPECT_EQ("1.001us", PrettyDuration(1 * one_us + 1));
183
184 EXPECT_EQ("1ns", PrettyDuration(1));
185 EXPECT_EQ("10ns", PrettyDuration(10));
186 EXPECT_EQ("100ns", PrettyDuration(100));
187 }
188
TEST_F(UtilsTest,MangleForJni)189 TEST_F(UtilsTest, MangleForJni) {
190 ScopedObjectAccess soa(Thread::Current());
191 EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
192 EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
193 EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
194 EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
195 EXPECT_EQ("_3C", MangleForJni("[C"));
196 }
197
TEST_F(UtilsTest,JniShortName_JniLongName)198 TEST_F(UtilsTest, JniShortName_JniLongName) {
199 ScopedObjectAccess soa(Thread::Current());
200 mirror::Class* c = class_linker_->FindSystemClass("Ljava/lang/String;");
201 ASSERT_TRUE(c != NULL);
202 mirror::ArtMethod* m;
203
204 m = c->FindVirtualMethod("charAt", "(I)C");
205 ASSERT_TRUE(m != NULL);
206 EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m));
207 EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m));
208
209 m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I");
210 ASSERT_TRUE(m != NULL);
211 EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m));
212 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m));
213
214 m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;");
215 ASSERT_TRUE(m != NULL);
216 EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m));
217 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m));
218 }
219
TEST_F(UtilsTest,Split)220 TEST_F(UtilsTest, Split) {
221 std::vector<std::string> actual;
222 std::vector<std::string> expected;
223
224 expected.clear();
225
226 actual.clear();
227 Split("", ':', actual);
228 EXPECT_EQ(expected, actual);
229
230 actual.clear();
231 Split(":", ':', actual);
232 EXPECT_EQ(expected, actual);
233
234 expected.clear();
235 expected.push_back("foo");
236
237 actual.clear();
238 Split(":foo", ':', actual);
239 EXPECT_EQ(expected, actual);
240
241 actual.clear();
242 Split("foo:", ':', actual);
243 EXPECT_EQ(expected, actual);
244
245 actual.clear();
246 Split(":foo:", ':', actual);
247 EXPECT_EQ(expected, actual);
248
249 expected.push_back("bar");
250
251 actual.clear();
252 Split("foo:bar", ':', actual);
253 EXPECT_EQ(expected, actual);
254
255 actual.clear();
256 Split(":foo:bar", ':', actual);
257 EXPECT_EQ(expected, actual);
258
259 actual.clear();
260 Split("foo:bar:", ':', actual);
261 EXPECT_EQ(expected, actual);
262
263 actual.clear();
264 Split(":foo:bar:", ':', actual);
265 EXPECT_EQ(expected, actual);
266
267 expected.push_back("baz");
268
269 actual.clear();
270 Split("foo:bar:baz", ':', actual);
271 EXPECT_EQ(expected, actual);
272
273 actual.clear();
274 Split(":foo:bar:baz", ':', actual);
275 EXPECT_EQ(expected, actual);
276
277 actual.clear();
278 Split("foo:bar:baz:", ':', actual);
279 EXPECT_EQ(expected, actual);
280
281 actual.clear();
282 Split(":foo:bar:baz:", ':', actual);
283 EXPECT_EQ(expected, actual);
284 }
285
TEST_F(UtilsTest,Join)286 TEST_F(UtilsTest, Join) {
287 std::vector<std::string> strings;
288
289 strings.clear();
290 EXPECT_EQ("", Join(strings, ':'));
291
292 strings.clear();
293 strings.push_back("foo");
294 EXPECT_EQ("foo", Join(strings, ':'));
295
296 strings.clear();
297 strings.push_back("");
298 strings.push_back("foo");
299 EXPECT_EQ(":foo", Join(strings, ':'));
300
301 strings.clear();
302 strings.push_back("foo");
303 strings.push_back("");
304 EXPECT_EQ("foo:", Join(strings, ':'));
305
306 strings.clear();
307 strings.push_back("");
308 strings.push_back("foo");
309 strings.push_back("");
310 EXPECT_EQ(":foo:", Join(strings, ':'));
311
312 strings.clear();
313 strings.push_back("foo");
314 strings.push_back("bar");
315 EXPECT_EQ("foo:bar", Join(strings, ':'));
316
317 strings.clear();
318 strings.push_back("foo");
319 strings.push_back("bar");
320 strings.push_back("baz");
321 EXPECT_EQ("foo:bar:baz", Join(strings, ':'));
322 }
323
TEST_F(UtilsTest,StartsWith)324 TEST_F(UtilsTest, StartsWith) {
325 EXPECT_FALSE(StartsWith("foo", "bar"));
326 EXPECT_TRUE(StartsWith("foo", "foo"));
327 EXPECT_TRUE(StartsWith("food", "foo"));
328 EXPECT_FALSE(StartsWith("fo", "foo"));
329 }
330
TEST_F(UtilsTest,EndsWith)331 TEST_F(UtilsTest, EndsWith) {
332 EXPECT_FALSE(EndsWith("foo", "bar"));
333 EXPECT_TRUE(EndsWith("foo", "foo"));
334 EXPECT_TRUE(EndsWith("foofoo", "foo"));
335 EXPECT_FALSE(EndsWith("oo", "foo"));
336 }
337
CheckGetDalvikCacheFilenameOrDie(const char * in,const char * out)338 void CheckGetDalvikCacheFilenameOrDie(const char* in, const char* out) {
339 std::string expected(getenv("ANDROID_DATA"));
340 expected += "/dalvik-cache/";
341 expected += out;
342 EXPECT_STREQ(expected.c_str(), GetDalvikCacheFilenameOrDie(in).c_str());
343 }
344
TEST_F(UtilsTest,GetDalvikCacheFilenameOrDie)345 TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) {
346 CheckGetDalvikCacheFilenameOrDie("/system/app/Foo.apk", "system@app@Foo.apk@classes.dex");
347 CheckGetDalvikCacheFilenameOrDie("/data/app/foo-1.apk", "data@app@foo-1.apk@classes.dex");
348 CheckGetDalvikCacheFilenameOrDie("/system/framework/core.jar", "system@framework@core.jar@classes.dex");
349 CheckGetDalvikCacheFilenameOrDie("/system/framework/boot.art", "system@framework@boot.art");
350 }
351
352 } // namespace art
353