blob: d2100d16d4a6f5141615dcc97edc42d86df6c882 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "utils.h"
18
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010020#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080021#include "common_runtime_test.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/array.h"
23#include "mirror/array-inl.h"
24#include "mirror/object-inl.h"
25#include "mirror/object_array-inl.h"
26#include "mirror/string.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028#include "handle_scope-inl.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070029
Evgenii Stepanov1e133742015-05-20 12:30:59 -070030#include "base/memory_tool.h"
Mathieu Chartier661974a2014-01-09 11:23:53 -080031
Elliott Hughes11e45072011-08-16 17:40:46 -070032namespace art {
33
Elliott Hughes9058f2b2012-03-22 18:06:48 -070034std::string PrettyArguments(const char* signature);
35std::string PrettyReturnType(const char* signature);
36
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080037class UtilsTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070038
Elliott Hughes5174fe62011-08-23 15:12:35 -070039TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070040 EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
41 EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070042}
43
Elliott Hughes5174fe62011-08-23 15:12:35 -070044TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070045 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
46 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070047}
48
Mathieu Chartier76433272014-09-26 14:32:37 -070049TEST_F(UtilsTest, PrettyDescriptor_Primitive) {
50 EXPECT_EQ("boolean", PrettyDescriptor(Primitive::kPrimBoolean));
51 EXPECT_EQ("byte", PrettyDescriptor(Primitive::kPrimByte));
52 EXPECT_EQ("char", PrettyDescriptor(Primitive::kPrimChar));
53 EXPECT_EQ("short", PrettyDescriptor(Primitive::kPrimShort));
54 EXPECT_EQ("int", PrettyDescriptor(Primitive::kPrimInt));
55 EXPECT_EQ("float", PrettyDescriptor(Primitive::kPrimFloat));
56 EXPECT_EQ("long", PrettyDescriptor(Primitive::kPrimLong));
57 EXPECT_EQ("double", PrettyDescriptor(Primitive::kPrimDouble));
58 EXPECT_EQ("void", PrettyDescriptor(Primitive::kPrimVoid));
59}
60
Elliott Hughes5174fe62011-08-23 15:12:35 -070061TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070062 EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
63 EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
64 EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
65 EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
66 EXPECT_EQ("char[]", PrettyDescriptor("[C"));
67 EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
68 EXPECT_EQ("double[]", PrettyDescriptor("[D"));
69 EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
70 EXPECT_EQ("float[]", PrettyDescriptor("[F"));
71 EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
72 EXPECT_EQ("int[]", PrettyDescriptor("[I"));
73 EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
74 EXPECT_EQ("long[]", PrettyDescriptor("[J"));
75 EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
76 EXPECT_EQ("short[]", PrettyDescriptor("[S"));
77 EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
Elliott Hughes11e45072011-08-16 17:40:46 -070078}
79
Elliott Hughes5174fe62011-08-23 15:12:35 -070080TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070081 EXPECT_EQ("boolean", PrettyDescriptor("Z"));
82 EXPECT_EQ("byte", PrettyDescriptor("B"));
83 EXPECT_EQ("char", PrettyDescriptor("C"));
84 EXPECT_EQ("double", PrettyDescriptor("D"));
85 EXPECT_EQ("float", PrettyDescriptor("F"));
86 EXPECT_EQ("int", PrettyDescriptor("I"));
87 EXPECT_EQ("long", PrettyDescriptor("J"));
88 EXPECT_EQ("short", PrettyDescriptor("S"));
89}
90
91TEST_F(UtilsTest, PrettyArguments) {
92 EXPECT_EQ("()", PrettyArguments("()V"));
93 EXPECT_EQ("(int)", PrettyArguments("(I)V"));
94 EXPECT_EQ("(int, int)", PrettyArguments("(II)V"));
95 EXPECT_EQ("(int, int, int[][])", PrettyArguments("(II[[I)V"));
96 EXPECT_EQ("(int, int, int[][], java.lang.Poop)", PrettyArguments("(II[[ILjava/lang/Poop;)V"));
97 EXPECT_EQ("(int, int, int[][], java.lang.Poop, java.lang.Poop[][])", PrettyArguments("(II[[ILjava/lang/Poop;[[Ljava/lang/Poop;)V"));
98}
99
100TEST_F(UtilsTest, PrettyReturnType) {
101 EXPECT_EQ("void", PrettyReturnType("()V"));
102 EXPECT_EQ("int", PrettyReturnType("()I"));
103 EXPECT_EQ("int[][]", PrettyReturnType("()[[I"));
104 EXPECT_EQ("java.lang.Poop", PrettyReturnType("()Ljava/lang/Poop;"));
105 EXPECT_EQ("java.lang.Poop[][]", PrettyReturnType("()[[Ljava/lang/Poop;"));
Elliott Hughes11e45072011-08-16 17:40:46 -0700106}
107
Elliott Hughes54e7df12011-09-16 11:47:04 -0700108TEST_F(UtilsTest, PrettyTypeOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700109 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700110 EXPECT_EQ("null", PrettyTypeOf(nullptr));
Elliott Hughes11e45072011-08-16 17:40:46 -0700111
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700112 StackHandleScope<2> hs(soa.Self());
113 Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "")));
114 EXPECT_EQ("java.lang.String", PrettyTypeOf(s.Get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700115
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700116 Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2)));
117 EXPECT_EQ("short[]", PrettyTypeOf(a.Get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700118
Ian Rogers98379392014-02-24 16:53:16 -0800119 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700120 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700122 EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o));
123 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass()));
124}
125
126TEST_F(UtilsTest, PrettyClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 EXPECT_EQ("null", PrettyClass(nullptr));
Ian Rogers98379392014-02-24 16:53:16 -0800129 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700132 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass()));
133}
134
Ian Rogersd81871c2011-10-03 13:57:23 -0700135TEST_F(UtilsTest, PrettyClassAndClassLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 EXPECT_EQ("null", PrettyClassAndClassLoader(nullptr));
Ian Rogers98379392014-02-24 16:53:16 -0800138 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -0700141 EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass()));
142}
143
Elliott Hughes54e7df12011-09-16 11:47:04 -0700144TEST_F(UtilsTest, PrettyField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700146 EXPECT_EQ("null", PrettyField(nullptr));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700147
Ian Rogers98379392014-02-24 16:53:16 -0800148 mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(),
149 "Ljava/lang/String;");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700150
Mathieu Chartierc7853442015-03-27 14:35:38 -0700151 ArtField* f;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700152 f = java_lang_String->FindDeclaredInstanceField("count", "I");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700153 EXPECT_EQ("int java.lang.String.count", PrettyField(f));
154 EXPECT_EQ("java.lang.String.count", PrettyField(f, false));
Elliott Hughes11e45072011-08-16 17:40:46 -0700155}
156
Ian Rogers3bb17a62012-01-27 23:56:44 -0800157TEST_F(UtilsTest, PrettySize) {
158 EXPECT_EQ("1GB", PrettySize(1 * GB));
159 EXPECT_EQ("2GB", PrettySize(2 * GB));
160 if (sizeof(size_t) > sizeof(uint32_t)) {
161 EXPECT_EQ("100GB", PrettySize(100 * GB));
162 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700163 EXPECT_EQ("1024KB", PrettySize(1 * MB));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800164 EXPECT_EQ("10MB", PrettySize(10 * MB));
165 EXPECT_EQ("100MB", PrettySize(100 * MB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700166 EXPECT_EQ("1024B", PrettySize(1 * KB));
Elliott Hughes409d2732012-04-03 13:34:44 -0700167 EXPECT_EQ("10KB", PrettySize(10 * KB));
168 EXPECT_EQ("100KB", PrettySize(100 * KB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700169 EXPECT_EQ("0B", PrettySize(0));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800170 EXPECT_EQ("1B", PrettySize(1));
171 EXPECT_EQ("10B", PrettySize(10));
172 EXPECT_EQ("100B", PrettySize(100));
Elliott Hughesc967f782012-04-16 10:23:15 -0700173 EXPECT_EQ("512B", PrettySize(512));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800174}
175
Elliott Hughes79082e32011-08-25 12:07:32 -0700176TEST_F(UtilsTest, MangleForJni) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes79082e32011-08-25 12:07:32 -0700178 EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
179 EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
180 EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
181 EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
182 EXPECT_EQ("_3C", MangleForJni("[C"));
183}
184
185TEST_F(UtilsTest, JniShortName_JniLongName) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700186 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800187 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700188 ASSERT_TRUE(c != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 ArtMethod* m;
Elliott Hughes79082e32011-08-25 12:07:32 -0700190
Andreas Gampe542451c2016-07-26 09:02:02 -0700191 m = c->FindVirtualMethod("charAt", "(I)C", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700192 ASSERT_TRUE(m != nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -0700193 EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m));
194 EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m));
195
Andreas Gampe542451c2016-07-26 09:02:02 -0700196 m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700197 ASSERT_TRUE(m != nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -0700198 EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m));
199 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m));
200
Andreas Gampe542451c2016-07-26 09:02:02 -0700201 m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700202 ASSERT_TRUE(m != nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -0700203 EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m));
204 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m));
205}
206
Elliott Hughes34023802011-08-30 12:06:17 -0700207TEST_F(UtilsTest, Split) {
208 std::vector<std::string> actual;
209 std::vector<std::string> expected;
210
211 expected.clear();
212
213 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700214 Split("", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700215 EXPECT_EQ(expected, actual);
216
217 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700218 Split(":", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700219 EXPECT_EQ(expected, actual);
220
221 expected.clear();
222 expected.push_back("foo");
223
224 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700225 Split(":foo", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700226 EXPECT_EQ(expected, actual);
227
228 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700229 Split("foo:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700230 EXPECT_EQ(expected, actual);
231
232 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700233 Split(":foo:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700234 EXPECT_EQ(expected, actual);
235
236 expected.push_back("bar");
237
238 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700239 Split("foo:bar", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700240 EXPECT_EQ(expected, actual);
241
242 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700243 Split(":foo:bar", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700244 EXPECT_EQ(expected, actual);
245
246 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700247 Split("foo:bar:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700248 EXPECT_EQ(expected, actual);
249
250 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700251 Split(":foo:bar:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700252 EXPECT_EQ(expected, actual);
253
254 expected.push_back("baz");
255
256 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700257 Split("foo:bar:baz", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700258 EXPECT_EQ(expected, actual);
259
260 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700261 Split(":foo:bar:baz", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700262 EXPECT_EQ(expected, actual);
263
264 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700265 Split("foo:bar:baz:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700266 EXPECT_EQ(expected, actual);
267
268 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700269 Split(":foo:bar:baz:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700270 EXPECT_EQ(expected, actual);
271}
272
Elliott Hughes48436bb2012-02-07 15:23:28 -0800273TEST_F(UtilsTest, Join) {
274 std::vector<std::string> strings;
275
276 strings.clear();
277 EXPECT_EQ("", Join(strings, ':'));
278
279 strings.clear();
280 strings.push_back("foo");
281 EXPECT_EQ("foo", Join(strings, ':'));
282
283 strings.clear();
284 strings.push_back("");
285 strings.push_back("foo");
286 EXPECT_EQ(":foo", Join(strings, ':'));
287
288 strings.clear();
289 strings.push_back("foo");
290 strings.push_back("");
291 EXPECT_EQ("foo:", Join(strings, ':'));
292
293 strings.clear();
294 strings.push_back("");
295 strings.push_back("foo");
296 strings.push_back("");
297 EXPECT_EQ(":foo:", Join(strings, ':'));
298
299 strings.clear();
300 strings.push_back("foo");
301 strings.push_back("bar");
302 EXPECT_EQ("foo:bar", Join(strings, ':'));
303
304 strings.clear();
305 strings.push_back("foo");
306 strings.push_back("bar");
307 strings.push_back("baz");
308 EXPECT_EQ("foo:bar:baz", Join(strings, ':'));
309}
310
Brian Carlstrom7a967b32012-03-28 15:23:10 -0700311TEST_F(UtilsTest, StartsWith) {
312 EXPECT_FALSE(StartsWith("foo", "bar"));
313 EXPECT_TRUE(StartsWith("foo", "foo"));
314 EXPECT_TRUE(StartsWith("food", "foo"));
315 EXPECT_FALSE(StartsWith("fo", "foo"));
316}
317
318TEST_F(UtilsTest, EndsWith) {
319 EXPECT_FALSE(EndsWith("foo", "bar"));
320 EXPECT_TRUE(EndsWith("foo", "foo"));
321 EXPECT_TRUE(EndsWith("foofoo", "foo"));
322 EXPECT_FALSE(EndsWith("oo", "foo"));
323}
324
Brian Carlstrom19a08362013-10-16 14:37:22 -0700325TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100326 EXPECT_STREQ("/foo/system@app@Foo.apk@classes.dex",
327 GetDalvikCacheFilenameOrDie("/system/app/Foo.apk", "/foo").c_str());
328
329 EXPECT_STREQ("/foo/data@app@foo-1.apk@classes.dex",
330 GetDalvikCacheFilenameOrDie("/data/app/foo-1.apk", "/foo").c_str());
331 EXPECT_STREQ("/foo/system@framework@core.jar@classes.dex",
332 GetDalvikCacheFilenameOrDie("/system/framework/core.jar", "/foo").c_str());
333 EXPECT_STREQ("/foo/system@framework@boot.art",
334 GetDalvikCacheFilenameOrDie("/system/framework/boot.art", "/foo").c_str());
Alex Light6e183f22014-07-18 14:57:04 -0700335 EXPECT_STREQ("/foo/system@framework@boot.oat",
336 GetDalvikCacheFilenameOrDie("/system/framework/boot.oat", "/foo").c_str());
Brian Carlstrom19a08362013-10-16 14:37:22 -0700337}
338
Andreas Gampe40da2862015-02-27 12:49:04 -0800339TEST_F(UtilsTest, GetDalvikCache) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700340 EXPECT_STREQ("", GetDalvikCache("should-not-exist123").c_str());
Andreas Gampe40da2862015-02-27 12:49:04 -0800341
Richard Uhler55b58b62016-08-12 09:05:13 -0700342 EXPECT_STREQ((android_data_ + "/dalvik-cache/.").c_str(), GetDalvikCache(".").c_str());
Andreas Gampe40da2862015-02-27 12:49:04 -0800343}
344
345
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700346TEST_F(UtilsTest, GetSystemImageFilename) {
347 EXPECT_STREQ("/system/framework/arm/boot.art",
348 GetSystemImageFilename("/system/framework/boot.art", kArm).c_str());
349}
350
Brian Carlstrom6449c622014-02-10 23:48:36 -0800351TEST_F(UtilsTest, ExecSuccess) {
352 std::vector<std::string> command;
353 if (kIsTargetBuild) {
Nicolas Geoffray4f7fdd22015-04-24 11:57:37 +0100354 std::string android_root(GetAndroidRoot());
355 command.push_back(android_root + "/bin/id");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800356 } else {
357 command.push_back("/usr/bin/id");
358 }
359 std::string error_msg;
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700360 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800361 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
362 EXPECT_TRUE(Exec(command, &error_msg));
363 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800364 EXPECT_EQ(0U, error_msg.size()) << error_msg;
365}
366
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800367TEST_F(UtilsTest, ExecError) {
Andreas Gampe369810a2015-01-14 19:53:31 -0800368 // This will lead to error messages in the log.
369 ScopedLogSeverity sls(LogSeverity::FATAL);
370
Brian Carlstrom6449c622014-02-10 23:48:36 -0800371 std::vector<std::string> command;
372 command.push_back("bogus");
373 std::string error_msg;
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700374 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800375 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
376 EXPECT_FALSE(Exec(command, &error_msg));
377 EXPECT_NE(0U, error_msg.size());
378 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800379}
380
Narayan Kamath8508e372015-05-06 14:55:43 +0100381TEST_F(UtilsTest, IsValidDescriptor) {
382 std::vector<uint8_t> descriptor(
383 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, ';', 0x00 });
384 EXPECT_TRUE(IsValidDescriptor(reinterpret_cast<char*>(&descriptor[0])));
385
386 std::vector<uint8_t> unpaired_surrogate(
387 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, ';', 0x00 });
388 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate[0])));
389
390 std::vector<uint8_t> unpaired_surrogate_at_end(
391 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0x00 });
392 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_at_end[0])));
393
394 std::vector<uint8_t> invalid_surrogate(
395 { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, ';', 0x00 });
396 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&invalid_surrogate[0])));
397
398 std::vector<uint8_t> unpaired_surrogate_with_multibyte_sequence(
399 { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, 0xf0, 0x9f, 0x8f, 0xa0, ';', 0x00 });
400 EXPECT_FALSE(
401 IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_with_multibyte_sequence[0])));
402}
403
Elliott Hughes11e45072011-08-16 17:40:46 -0700404} // namespace art