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 "utils.h"
18 
19 #include "class_linker-inl.h"
20 #include "common_runtime_test.h"
21 #include "mirror/array.h"
22 #include "mirror/array-inl.h"
23 #include "mirror/object-inl.h"
24 #include "mirror/object_array-inl.h"
25 #include "mirror/string.h"
26 #include "scoped_thread_state_change.h"
27 #include "handle_scope-inl.h"
28 
29 #include <valgrind.h>
30 
31 namespace art {
32 
33 std::string PrettyArguments(const char* signature);
34 std::string PrettyReturnType(const char* signature);
35 
36 class UtilsTest : public CommonRuntimeTest {};
37 
TEST_F(UtilsTest,PrettyDescriptor_ArrayReferences)38 TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
39   EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
40   EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
41 }
42 
TEST_F(UtilsTest,PrettyDescriptor_ScalarReferences)43 TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
44   EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
45   EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
46 }
47 
TEST_F(UtilsTest,PrettyDescriptor_Primitive)48 TEST_F(UtilsTest, PrettyDescriptor_Primitive) {
49   EXPECT_EQ("boolean", PrettyDescriptor(Primitive::kPrimBoolean));
50   EXPECT_EQ("byte", PrettyDescriptor(Primitive::kPrimByte));
51   EXPECT_EQ("char", PrettyDescriptor(Primitive::kPrimChar));
52   EXPECT_EQ("short", PrettyDescriptor(Primitive::kPrimShort));
53   EXPECT_EQ("int", PrettyDescriptor(Primitive::kPrimInt));
54   EXPECT_EQ("float", PrettyDescriptor(Primitive::kPrimFloat));
55   EXPECT_EQ("long", PrettyDescriptor(Primitive::kPrimLong));
56   EXPECT_EQ("double", PrettyDescriptor(Primitive::kPrimDouble));
57   EXPECT_EQ("void", PrettyDescriptor(Primitive::kPrimVoid));
58 }
59 
TEST_F(UtilsTest,PrettyDescriptor_PrimitiveArrays)60 TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
61   EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
62   EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
63   EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
64   EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
65   EXPECT_EQ("char[]", PrettyDescriptor("[C"));
66   EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
67   EXPECT_EQ("double[]", PrettyDescriptor("[D"));
68   EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
69   EXPECT_EQ("float[]", PrettyDescriptor("[F"));
70   EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
71   EXPECT_EQ("int[]", PrettyDescriptor("[I"));
72   EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
73   EXPECT_EQ("long[]", PrettyDescriptor("[J"));
74   EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
75   EXPECT_EQ("short[]", PrettyDescriptor("[S"));
76   EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
77 }
78 
TEST_F(UtilsTest,PrettyDescriptor_PrimitiveScalars)79 TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
80   EXPECT_EQ("boolean", PrettyDescriptor("Z"));
81   EXPECT_EQ("byte", PrettyDescriptor("B"));
82   EXPECT_EQ("char", PrettyDescriptor("C"));
83   EXPECT_EQ("double", PrettyDescriptor("D"));
84   EXPECT_EQ("float", PrettyDescriptor("F"));
85   EXPECT_EQ("int", PrettyDescriptor("I"));
86   EXPECT_EQ("long", PrettyDescriptor("J"));
87   EXPECT_EQ("short", PrettyDescriptor("S"));
88 }
89 
TEST_F(UtilsTest,PrettyArguments)90 TEST_F(UtilsTest, PrettyArguments) {
91   EXPECT_EQ("()", PrettyArguments("()V"));
92   EXPECT_EQ("(int)", PrettyArguments("(I)V"));
93   EXPECT_EQ("(int, int)", PrettyArguments("(II)V"));
94   EXPECT_EQ("(int, int, int[][])", PrettyArguments("(II[[I)V"));
95   EXPECT_EQ("(int, int, int[][], java.lang.Poop)", PrettyArguments("(II[[ILjava/lang/Poop;)V"));
96   EXPECT_EQ("(int, int, int[][], java.lang.Poop, java.lang.Poop[][])", PrettyArguments("(II[[ILjava/lang/Poop;[[Ljava/lang/Poop;)V"));
97 }
98 
TEST_F(UtilsTest,PrettyReturnType)99 TEST_F(UtilsTest, PrettyReturnType) {
100   EXPECT_EQ("void", PrettyReturnType("()V"));
101   EXPECT_EQ("int", PrettyReturnType("()I"));
102   EXPECT_EQ("int[][]", PrettyReturnType("()[[I"));
103   EXPECT_EQ("java.lang.Poop", PrettyReturnType("()Ljava/lang/Poop;"));
104   EXPECT_EQ("java.lang.Poop[][]", PrettyReturnType("()[[Ljava/lang/Poop;"));
105 }
106 
TEST_F(UtilsTest,PrettyTypeOf)107 TEST_F(UtilsTest, PrettyTypeOf) {
108   ScopedObjectAccess soa(Thread::Current());
109   EXPECT_EQ("null", PrettyTypeOf(nullptr));
110 
111   StackHandleScope<2> hs(soa.Self());
112   Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "")));
113   EXPECT_EQ("java.lang.String", PrettyTypeOf(s.Get()));
114 
115   Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2)));
116   EXPECT_EQ("short[]", PrettyTypeOf(a.Get()));
117 
118   mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
119   ASSERT_TRUE(c != nullptr);
120   mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
121   EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o));
122   EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass()));
123 }
124 
TEST_F(UtilsTest,PrettyClass)125 TEST_F(UtilsTest, PrettyClass) {
126   ScopedObjectAccess soa(Thread::Current());
127   EXPECT_EQ("null", PrettyClass(nullptr));
128   mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
129   ASSERT_TRUE(c != nullptr);
130   mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
131   EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass()));
132 }
133 
TEST_F(UtilsTest,PrettyClassAndClassLoader)134 TEST_F(UtilsTest, PrettyClassAndClassLoader) {
135   ScopedObjectAccess soa(Thread::Current());
136   EXPECT_EQ("null", PrettyClassAndClassLoader(nullptr));
137   mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
138   ASSERT_TRUE(c != nullptr);
139   mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
140   EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass()));
141 }
142 
TEST_F(UtilsTest,PrettyField)143 TEST_F(UtilsTest, PrettyField) {
144   ScopedObjectAccess soa(Thread::Current());
145   EXPECT_EQ("null", PrettyField(nullptr));
146 
147   mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(),
148                                                                    "Ljava/lang/String;");
149 
150   ArtField* f;
151   f = java_lang_String->FindDeclaredInstanceField("count", "I");
152   EXPECT_EQ("int java.lang.String.count", PrettyField(f));
153   EXPECT_EQ("java.lang.String.count", PrettyField(f, false));
154 }
155 
TEST_F(UtilsTest,PrettySize)156 TEST_F(UtilsTest, PrettySize) {
157   EXPECT_EQ("1GB", PrettySize(1 * GB));
158   EXPECT_EQ("2GB", PrettySize(2 * GB));
159   if (sizeof(size_t) > sizeof(uint32_t)) {
160     EXPECT_EQ("100GB", PrettySize(100 * GB));
161   }
162   EXPECT_EQ("1024KB", PrettySize(1 * MB));
163   EXPECT_EQ("10MB", PrettySize(10 * MB));
164   EXPECT_EQ("100MB", PrettySize(100 * MB));
165   EXPECT_EQ("1024B", PrettySize(1 * KB));
166   EXPECT_EQ("10KB", PrettySize(10 * KB));
167   EXPECT_EQ("100KB", PrettySize(100 * KB));
168   EXPECT_EQ("0B", PrettySize(0));
169   EXPECT_EQ("1B", PrettySize(1));
170   EXPECT_EQ("10B", PrettySize(10));
171   EXPECT_EQ("100B", PrettySize(100));
172   EXPECT_EQ("512B", PrettySize(512));
173 }
174 
TEST_F(UtilsTest,MangleForJni)175 TEST_F(UtilsTest, MangleForJni) {
176   ScopedObjectAccess soa(Thread::Current());
177   EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
178   EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
179   EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
180   EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
181   EXPECT_EQ("_3C", MangleForJni("[C"));
182 }
183 
TEST_F(UtilsTest,JniShortName_JniLongName)184 TEST_F(UtilsTest, JniShortName_JniLongName) {
185   ScopedObjectAccess soa(Thread::Current());
186   mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
187   ASSERT_TRUE(c != nullptr);
188   ArtMethod* m;
189 
190   m = c->FindVirtualMethod("charAt", "(I)C", sizeof(void*));
191   ASSERT_TRUE(m != nullptr);
192   EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m));
193   EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m));
194 
195   m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I", sizeof(void*));
196   ASSERT_TRUE(m != nullptr);
197   EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m));
198   EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m));
199 
200   m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;", sizeof(void*));
201   ASSERT_TRUE(m != nullptr);
202   EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m));
203   EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m));
204 }
205 
TEST_F(UtilsTest,Split)206 TEST_F(UtilsTest, Split) {
207   std::vector<std::string> actual;
208   std::vector<std::string> expected;
209 
210   expected.clear();
211 
212   actual.clear();
213   Split("", ':', &actual);
214   EXPECT_EQ(expected, actual);
215 
216   actual.clear();
217   Split(":", ':', &actual);
218   EXPECT_EQ(expected, actual);
219 
220   expected.clear();
221   expected.push_back("foo");
222 
223   actual.clear();
224   Split(":foo", ':', &actual);
225   EXPECT_EQ(expected, actual);
226 
227   actual.clear();
228   Split("foo:", ':', &actual);
229   EXPECT_EQ(expected, actual);
230 
231   actual.clear();
232   Split(":foo:", ':', &actual);
233   EXPECT_EQ(expected, actual);
234 
235   expected.push_back("bar");
236 
237   actual.clear();
238   Split("foo:bar", ':', &actual);
239   EXPECT_EQ(expected, actual);
240 
241   actual.clear();
242   Split(":foo:bar", ':', &actual);
243   EXPECT_EQ(expected, actual);
244 
245   actual.clear();
246   Split("foo:bar:", ':', &actual);
247   EXPECT_EQ(expected, actual);
248 
249   actual.clear();
250   Split(":foo:bar:", ':', &actual);
251   EXPECT_EQ(expected, actual);
252 
253   expected.push_back("baz");
254 
255   actual.clear();
256   Split("foo:bar:baz", ':', &actual);
257   EXPECT_EQ(expected, actual);
258 
259   actual.clear();
260   Split(":foo:bar:baz", ':', &actual);
261   EXPECT_EQ(expected, actual);
262 
263   actual.clear();
264   Split("foo:bar:baz:", ':', &actual);
265   EXPECT_EQ(expected, actual);
266 
267   actual.clear();
268   Split(":foo:bar:baz:", ':', &actual);
269   EXPECT_EQ(expected, actual);
270 }
271 
TEST_F(UtilsTest,Join)272 TEST_F(UtilsTest, Join) {
273   std::vector<std::string> strings;
274 
275   strings.clear();
276   EXPECT_EQ("", Join(strings, ':'));
277 
278   strings.clear();
279   strings.push_back("foo");
280   EXPECT_EQ("foo", Join(strings, ':'));
281 
282   strings.clear();
283   strings.push_back("");
284   strings.push_back("foo");
285   EXPECT_EQ(":foo", Join(strings, ':'));
286 
287   strings.clear();
288   strings.push_back("foo");
289   strings.push_back("");
290   EXPECT_EQ("foo:", Join(strings, ':'));
291 
292   strings.clear();
293   strings.push_back("");
294   strings.push_back("foo");
295   strings.push_back("");
296   EXPECT_EQ(":foo:", Join(strings, ':'));
297 
298   strings.clear();
299   strings.push_back("foo");
300   strings.push_back("bar");
301   EXPECT_EQ("foo:bar", Join(strings, ':'));
302 
303   strings.clear();
304   strings.push_back("foo");
305   strings.push_back("bar");
306   strings.push_back("baz");
307   EXPECT_EQ("foo:bar:baz", Join(strings, ':'));
308 }
309 
TEST_F(UtilsTest,StartsWith)310 TEST_F(UtilsTest, StartsWith) {
311   EXPECT_FALSE(StartsWith("foo", "bar"));
312   EXPECT_TRUE(StartsWith("foo", "foo"));
313   EXPECT_TRUE(StartsWith("food", "foo"));
314   EXPECT_FALSE(StartsWith("fo", "foo"));
315 }
316 
TEST_F(UtilsTest,EndsWith)317 TEST_F(UtilsTest, EndsWith) {
318   EXPECT_FALSE(EndsWith("foo", "bar"));
319   EXPECT_TRUE(EndsWith("foo", "foo"));
320   EXPECT_TRUE(EndsWith("foofoo", "foo"));
321   EXPECT_FALSE(EndsWith("oo", "foo"));
322 }
323 
TEST_F(UtilsTest,GetDalvikCacheFilenameOrDie)324 TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) {
325   EXPECT_STREQ("/foo/system@app@Foo.apk@classes.dex",
326                GetDalvikCacheFilenameOrDie("/system/app/Foo.apk", "/foo").c_str());
327 
328   EXPECT_STREQ("/foo/data@app@foo-1.apk@classes.dex",
329                GetDalvikCacheFilenameOrDie("/data/app/foo-1.apk", "/foo").c_str());
330   EXPECT_STREQ("/foo/system@framework@core.jar@classes.dex",
331                GetDalvikCacheFilenameOrDie("/system/framework/core.jar", "/foo").c_str());
332   EXPECT_STREQ("/foo/system@framework@boot.art",
333                GetDalvikCacheFilenameOrDie("/system/framework/boot.art", "/foo").c_str());
334   EXPECT_STREQ("/foo/system@framework@boot.oat",
335                GetDalvikCacheFilenameOrDie("/system/framework/boot.oat", "/foo").c_str());
336 }
337 
TEST_F(UtilsTest,GetDalvikCache)338 TEST_F(UtilsTest, GetDalvikCache) {
339   EXPECT_STREQ("", GetDalvikCache("should-not-exist123", false).c_str());
340 
341   EXPECT_STREQ((android_data_ + "/dalvik-cache/.").c_str(), GetDalvikCache(".", false).c_str());
342   EXPECT_STREQ((android_data_ + "/dalvik-cache/should-not-be-there").c_str(),
343                GetDalvikCache("should-not-be-there", true).c_str());
344 }
345 
346 
TEST_F(UtilsTest,GetSystemImageFilename)347 TEST_F(UtilsTest, GetSystemImageFilename) {
348   EXPECT_STREQ("/system/framework/arm/boot.art",
349                GetSystemImageFilename("/system/framework/boot.art", kArm).c_str());
350 }
351 
TEST_F(UtilsTest,ExecSuccess)352 TEST_F(UtilsTest, ExecSuccess) {
353   std::vector<std::string> command;
354   if (kIsTargetBuild) {
355     std::string android_root(GetAndroidRoot());
356     command.push_back(android_root + "/bin/id");
357   } else {
358     command.push_back("/usr/bin/id");
359   }
360   std::string error_msg;
361   if (RUNNING_ON_VALGRIND == 0) {
362     // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
363     EXPECT_TRUE(Exec(command, &error_msg));
364   }
365   EXPECT_EQ(0U, error_msg.size()) << error_msg;
366 }
367 
TEST_F(UtilsTest,ExecError)368 TEST_F(UtilsTest, ExecError) {
369   // This will lead to error messages in the log.
370   ScopedLogSeverity sls(LogSeverity::FATAL);
371 
372   std::vector<std::string> command;
373   command.push_back("bogus");
374   std::string error_msg;
375   if (RUNNING_ON_VALGRIND == 0) {
376     // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
377     EXPECT_FALSE(Exec(command, &error_msg));
378     EXPECT_NE(0U, error_msg.size());
379   }
380 }
381 
TEST_F(UtilsTest,IsValidDescriptor)382 TEST_F(UtilsTest, IsValidDescriptor) {
383   std::vector<uint8_t> descriptor(
384       { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, ';', 0x00 });
385   EXPECT_TRUE(IsValidDescriptor(reinterpret_cast<char*>(&descriptor[0])));
386 
387   std::vector<uint8_t> unpaired_surrogate(
388       { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, ';', 0x00 });
389   EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate[0])));
390 
391   std::vector<uint8_t> unpaired_surrogate_at_end(
392       { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0x00 });
393   EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_at_end[0])));
394 
395   std::vector<uint8_t> invalid_surrogate(
396       { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, ';', 0x00 });
397   EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&invalid_surrogate[0])));
398 
399   std::vector<uint8_t> unpaired_surrogate_with_multibyte_sequence(
400       { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, 0xf0, 0x9f, 0x8f, 0xa0, ';', 0x00 });
401   EXPECT_FALSE(
402       IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_with_multibyte_sequence[0])));
403 }
404 
405 }  // namespace art
406