blob: 66e38b1c13bca1a23afbcfa1f35f31548e8478b8 [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
Vladimir Marko3481ba22015-04-13 12:22:36 +010019#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080020#include "common_runtime_test.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027#include "handle_scope-inl.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070028
Mathieu Chartier661974a2014-01-09 11:23:53 -080029#include <valgrind.h>
30
Elliott Hughes11e45072011-08-16 17:40:46 -070031namespace art {
32
Elliott Hughes9058f2b2012-03-22 18:06:48 -070033std::string PrettyArguments(const char* signature);
34std::string PrettyReturnType(const char* signature);
35
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080036class UtilsTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070037
Elliott Hughes5174fe62011-08-23 15:12:35 -070038TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070039 EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
40 EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070041}
42
Elliott Hughes5174fe62011-08-23 15:12:35 -070043TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070044 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
45 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070046}
47
Mathieu Chartier76433272014-09-26 14:32:37 -070048TEST_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
Elliott Hughes5174fe62011-08-23 15:12:35 -070060TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070061 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"));
Elliott Hughes11e45072011-08-16 17:40:46 -070077}
78
Elliott Hughes5174fe62011-08-23 15:12:35 -070079TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070080 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
90TEST_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
99TEST_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;"));
Elliott Hughes11e45072011-08-16 17:40:46 -0700105}
106
Elliott Hughes54e7df12011-09-16 11:47:04 -0700107TEST_F(UtilsTest, PrettyTypeOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 EXPECT_EQ("null", PrettyTypeOf(nullptr));
Elliott Hughes11e45072011-08-16 17:40:46 -0700110
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 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()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700114
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2)));
116 EXPECT_EQ("short[]", PrettyTypeOf(a.Get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700117
Ian Rogers98379392014-02-24 16:53:16 -0800118 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700119 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700121 EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o));
122 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass()));
123}
124
125TEST_F(UtilsTest, PrettyClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700127 EXPECT_EQ("null", PrettyClass(nullptr));
Ian Rogers98379392014-02-24 16:53:16 -0800128 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700131 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass()));
132}
133
Ian Rogersd81871c2011-10-03 13:57:23 -0700134TEST_F(UtilsTest, PrettyClassAndClassLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700136 EXPECT_EQ("null", PrettyClassAndClassLoader(nullptr));
Ian Rogers98379392014-02-24 16:53:16 -0800137 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700138 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -0700140 EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass()));
141}
142
Elliott Hughes54e7df12011-09-16 11:47:04 -0700143TEST_F(UtilsTest, PrettyField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700145 EXPECT_EQ("null", PrettyField(nullptr));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700146
Ian Rogers98379392014-02-24 16:53:16 -0800147 mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(),
148 "Ljava/lang/String;");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700149
Mathieu Chartierc7853442015-03-27 14:35:38 -0700150 ArtField* f;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700151 f = java_lang_String->FindDeclaredInstanceField("count", "I");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700152 EXPECT_EQ("int java.lang.String.count", PrettyField(f));
153 EXPECT_EQ("java.lang.String.count", PrettyField(f, false));
Elliott Hughes11e45072011-08-16 17:40:46 -0700154}
155
Ian Rogers3bb17a62012-01-27 23:56:44 -0800156TEST_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 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700162 EXPECT_EQ("1024KB", PrettySize(1 * MB));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800163 EXPECT_EQ("10MB", PrettySize(10 * MB));
164 EXPECT_EQ("100MB", PrettySize(100 * MB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700165 EXPECT_EQ("1024B", PrettySize(1 * KB));
Elliott Hughes409d2732012-04-03 13:34:44 -0700166 EXPECT_EQ("10KB", PrettySize(10 * KB));
167 EXPECT_EQ("100KB", PrettySize(100 * KB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700168 EXPECT_EQ("0B", PrettySize(0));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800169 EXPECT_EQ("1B", PrettySize(1));
170 EXPECT_EQ("10B", PrettySize(10));
171 EXPECT_EQ("100B", PrettySize(100));
Elliott Hughesc967f782012-04-16 10:23:15 -0700172 EXPECT_EQ("512B", PrettySize(512));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800173}
174
Elliott Hughes79082e32011-08-25 12:07:32 -0700175TEST_F(UtilsTest, MangleForJni) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes79082e32011-08-25 12:07:32 -0700177 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
184TEST_F(UtilsTest, JniShortName_JniLongName) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700185 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800186 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700187 ASSERT_TRUE(c != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 ArtMethod* m;
Elliott Hughes79082e32011-08-25 12:07:32 -0700189
Mathieu Chartiere401d142015-04-22 13:56:20 -0700190 m = c->FindVirtualMethod("charAt", "(I)C", sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700191 ASSERT_TRUE(m != nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -0700192 EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m));
193 EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m));
194
Mathieu Chartiere401d142015-04-22 13:56:20 -0700195 m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I", sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700196 ASSERT_TRUE(m != nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -0700197 EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m));
198 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m));
199
Mathieu Chartiere401d142015-04-22 13:56:20 -0700200 m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;", sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700201 ASSERT_TRUE(m != nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -0700202 EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m));
203 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m));
204}
205
Elliott Hughes34023802011-08-30 12:06:17 -0700206TEST_F(UtilsTest, Split) {
207 std::vector<std::string> actual;
208 std::vector<std::string> expected;
209
210 expected.clear();
211
212 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700213 Split("", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700214 EXPECT_EQ(expected, actual);
215
216 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700217 Split(":", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700218 EXPECT_EQ(expected, actual);
219
220 expected.clear();
221 expected.push_back("foo");
222
223 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700224 Split(":foo", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700225 EXPECT_EQ(expected, actual);
226
227 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700228 Split("foo:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700229 EXPECT_EQ(expected, actual);
230
231 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700232 Split(":foo:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700233 EXPECT_EQ(expected, actual);
234
235 expected.push_back("bar");
236
237 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700238 Split("foo:bar", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700239 EXPECT_EQ(expected, actual);
240
241 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700242 Split(":foo:bar", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700243 EXPECT_EQ(expected, actual);
244
245 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700246 Split("foo:bar:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700247 EXPECT_EQ(expected, actual);
248
249 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700250 Split(":foo:bar:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700251 EXPECT_EQ(expected, actual);
252
253 expected.push_back("baz");
254
255 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700256 Split("foo:bar:baz", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700257 EXPECT_EQ(expected, actual);
258
259 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700260 Split(":foo:bar:baz", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700261 EXPECT_EQ(expected, actual);
262
263 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700264 Split("foo:bar:baz:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700265 EXPECT_EQ(expected, actual);
266
267 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700268 Split(":foo:bar:baz:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700269 EXPECT_EQ(expected, actual);
270}
271
Elliott Hughes48436bb2012-02-07 15:23:28 -0800272TEST_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
Brian Carlstrom7a967b32012-03-28 15:23:10 -0700310TEST_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
317TEST_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
Brian Carlstrom19a08362013-10-16 14:37:22 -0700324TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100325 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());
Alex Light6e183f22014-07-18 14:57:04 -0700334 EXPECT_STREQ("/foo/system@framework@boot.oat",
335 GetDalvikCacheFilenameOrDie("/system/framework/boot.oat", "/foo").c_str());
Brian Carlstrom19a08362013-10-16 14:37:22 -0700336}
337
Andreas Gampe40da2862015-02-27 12:49:04 -0800338TEST_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
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700347TEST_F(UtilsTest, GetSystemImageFilename) {
348 EXPECT_STREQ("/system/framework/arm/boot.art",
349 GetSystemImageFilename("/system/framework/boot.art", kArm).c_str());
350}
351
Brian Carlstrom6449c622014-02-10 23:48:36 -0800352TEST_F(UtilsTest, ExecSuccess) {
353 std::vector<std::string> command;
354 if (kIsTargetBuild) {
Nicolas Geoffray4f7fdd22015-04-24 11:57:37 +0100355 std::string android_root(GetAndroidRoot());
356 command.push_back(android_root + "/bin/id");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800357 } else {
358 command.push_back("/usr/bin/id");
359 }
360 std::string error_msg;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800361 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 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800365 EXPECT_EQ(0U, error_msg.size()) << error_msg;
366}
367
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800368TEST_F(UtilsTest, ExecError) {
Andreas Gampe369810a2015-01-14 19:53:31 -0800369 // This will lead to error messages in the log.
370 ScopedLogSeverity sls(LogSeverity::FATAL);
371
Brian Carlstrom6449c622014-02-10 23:48:36 -0800372 std::vector<std::string> command;
373 command.push_back("bogus");
374 std::string error_msg;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800375 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 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800380}
381
Narayan Kamath8508e372015-05-06 14:55:43 +0100382TEST_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
Elliott Hughes11e45072011-08-16 17:40:46 -0700405} // namespace art