blob: e846c983afa3f686889353d2896226f9d568ac65 [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 Gampe3f093892017-09-14 15:11:01 -070019#include <libgen.h>
David Sehrd106d9f2016-08-16 19:22:57 -070020#include <stdlib.h>
21
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Andreas Gampe3f093892017-09-14 15:11:01 -070023#include "base/stl_util.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010024#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_runtime_test.h"
David Sehr97c381e2017-02-01 15:09:58 -080026#include "exec_utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/array-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "mirror/array.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
32#include "mirror/string.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_thread_state_change-inl.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070034
Evgenii Stepanov1e133742015-05-20 12:30:59 -070035#include "base/memory_tool.h"
Mathieu Chartier661974a2014-01-09 11:23:53 -080036
Elliott Hughes11e45072011-08-16 17:40:46 -070037namespace art {
38
Elliott Hughes9058f2b2012-03-22 18:06:48 -070039std::string PrettyArguments(const char* signature);
40std::string PrettyReturnType(const char* signature);
41
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080042class UtilsTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070043
Elliott Hughes5174fe62011-08-23 15:12:35 -070044TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070045 EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
46 EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070047}
48
Elliott Hughes5174fe62011-08-23 15:12:35 -070049TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070050 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
51 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070052}
53
Mathieu Chartier76433272014-09-26 14:32:37 -070054TEST_F(UtilsTest, PrettyDescriptor_Primitive) {
55 EXPECT_EQ("boolean", PrettyDescriptor(Primitive::kPrimBoolean));
56 EXPECT_EQ("byte", PrettyDescriptor(Primitive::kPrimByte));
57 EXPECT_EQ("char", PrettyDescriptor(Primitive::kPrimChar));
58 EXPECT_EQ("short", PrettyDescriptor(Primitive::kPrimShort));
59 EXPECT_EQ("int", PrettyDescriptor(Primitive::kPrimInt));
60 EXPECT_EQ("float", PrettyDescriptor(Primitive::kPrimFloat));
61 EXPECT_EQ("long", PrettyDescriptor(Primitive::kPrimLong));
62 EXPECT_EQ("double", PrettyDescriptor(Primitive::kPrimDouble));
63 EXPECT_EQ("void", PrettyDescriptor(Primitive::kPrimVoid));
64}
65
Elliott Hughes5174fe62011-08-23 15:12:35 -070066TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070067 EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
68 EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
69 EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
70 EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
71 EXPECT_EQ("char[]", PrettyDescriptor("[C"));
72 EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
73 EXPECT_EQ("double[]", PrettyDescriptor("[D"));
74 EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
75 EXPECT_EQ("float[]", PrettyDescriptor("[F"));
76 EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
77 EXPECT_EQ("int[]", PrettyDescriptor("[I"));
78 EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
79 EXPECT_EQ("long[]", PrettyDescriptor("[J"));
80 EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
81 EXPECT_EQ("short[]", PrettyDescriptor("[S"));
82 EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
Elliott Hughes11e45072011-08-16 17:40:46 -070083}
84
Elliott Hughes5174fe62011-08-23 15:12:35 -070085TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070086 EXPECT_EQ("boolean", PrettyDescriptor("Z"));
87 EXPECT_EQ("byte", PrettyDescriptor("B"));
88 EXPECT_EQ("char", PrettyDescriptor("C"));
89 EXPECT_EQ("double", PrettyDescriptor("D"));
90 EXPECT_EQ("float", PrettyDescriptor("F"));
91 EXPECT_EQ("int", PrettyDescriptor("I"));
92 EXPECT_EQ("long", PrettyDescriptor("J"));
93 EXPECT_EQ("short", PrettyDescriptor("S"));
94}
95
Elliott Hughes54e7df12011-09-16 11:47:04 -070096TEST_F(UtilsTest, PrettyTypeOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 ScopedObjectAccess soa(Thread::Current());
David Sehr709b0702016-10-13 09:12:37 -070098 EXPECT_EQ("null", mirror::Object::PrettyTypeOf(nullptr));
Elliott Hughes11e45072011-08-16 17:40:46 -070099
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 StackHandleScope<2> hs(soa.Self());
101 Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "")));
David Sehr709b0702016-10-13 09:12:37 -0700102 EXPECT_EQ("java.lang.String", mirror::Object::PrettyTypeOf(s.Get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700103
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700104 Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2)));
David Sehr709b0702016-10-13 09:12:37 -0700105 EXPECT_EQ("short[]", mirror::Object::PrettyTypeOf(a.Get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700106
Ian Rogers98379392014-02-24 16:53:16 -0800107 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700108 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehr709b0702016-10-13 09:12:37 -0700110 EXPECT_EQ("java.lang.String[]", mirror::Object::PrettyTypeOf(o));
111 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Object::PrettyTypeOf(o->GetClass()));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700112}
113
114TEST_F(UtilsTest, PrettyClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 ScopedObjectAccess soa(Thread::Current());
David Sehr709b0702016-10-13 09:12:37 -0700116 EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
Ian Rogers98379392014-02-24 16:53:16 -0800117 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehr709b0702016-10-13 09:12:37 -0700120 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700121}
122
Ian Rogersd81871c2011-10-03 13:57:23 -0700123TEST_F(UtilsTest, PrettyClassAndClassLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 ScopedObjectAccess soa(Thread::Current());
David Sehr709b0702016-10-13 09:12:37 -0700125 EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
Ian Rogers98379392014-02-24 16:53:16 -0800126 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700127 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehr709b0702016-10-13 09:12:37 -0700129 EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
130 mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700131}
132
Elliott Hughes54e7df12011-09-16 11:47:04 -0700133TEST_F(UtilsTest, PrettyField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 ScopedObjectAccess soa(Thread::Current());
David Sehr709b0702016-10-13 09:12:37 -0700135 EXPECT_EQ("null", ArtField::PrettyField(nullptr));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700136
Ian Rogers98379392014-02-24 16:53:16 -0800137 mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(),
138 "Ljava/lang/String;");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700139
Mathieu Chartierc7853442015-03-27 14:35:38 -0700140 ArtField* f;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700141 f = java_lang_String->FindDeclaredInstanceField("count", "I");
David Sehr709b0702016-10-13 09:12:37 -0700142 EXPECT_EQ("int java.lang.String.count", f->PrettyField());
143 EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
Elliott Hughes11e45072011-08-16 17:40:46 -0700144}
145
Ian Rogers3bb17a62012-01-27 23:56:44 -0800146TEST_F(UtilsTest, PrettySize) {
147 EXPECT_EQ("1GB", PrettySize(1 * GB));
148 EXPECT_EQ("2GB", PrettySize(2 * GB));
149 if (sizeof(size_t) > sizeof(uint32_t)) {
150 EXPECT_EQ("100GB", PrettySize(100 * GB));
151 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700152 EXPECT_EQ("1024KB", PrettySize(1 * MB));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800153 EXPECT_EQ("10MB", PrettySize(10 * MB));
154 EXPECT_EQ("100MB", PrettySize(100 * MB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700155 EXPECT_EQ("1024B", PrettySize(1 * KB));
Elliott Hughes409d2732012-04-03 13:34:44 -0700156 EXPECT_EQ("10KB", PrettySize(10 * KB));
157 EXPECT_EQ("100KB", PrettySize(100 * KB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700158 EXPECT_EQ("0B", PrettySize(0));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800159 EXPECT_EQ("1B", PrettySize(1));
160 EXPECT_EQ("10B", PrettySize(10));
161 EXPECT_EQ("100B", PrettySize(100));
Elliott Hughesc967f782012-04-16 10:23:15 -0700162 EXPECT_EQ("512B", PrettySize(512));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800163}
164
Elliott Hughes79082e32011-08-25 12:07:32 -0700165TEST_F(UtilsTest, MangleForJni) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes79082e32011-08-25 12:07:32 -0700167 EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
168 EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
169 EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
170 EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
171 EXPECT_EQ("_3C", MangleForJni("[C"));
172}
173
174TEST_F(UtilsTest, JniShortName_JniLongName) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800176 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700177 ASSERT_TRUE(c != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178 ArtMethod* m;
Elliott Hughes79082e32011-08-25 12:07:32 -0700179
Vladimir Markoba118822017-06-12 15:41:56 +0100180 m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700181 ASSERT_TRUE(m != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100182 ASSERT_FALSE(m->IsDirect());
David Sehr709b0702016-10-13 09:12:37 -0700183 EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
184 EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700185
Vladimir Markoba118822017-06-12 15:41:56 +0100186 m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700187 ASSERT_TRUE(m != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100188 ASSERT_FALSE(m->IsDirect());
David Sehr709b0702016-10-13 09:12:37 -0700189 EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
190 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700191
Vladimir Markoba118822017-06-12 15:41:56 +0100192 m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700193 ASSERT_TRUE(m != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100194 ASSERT_TRUE(m->IsStatic());
David Sehr709b0702016-10-13 09:12:37 -0700195 EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
196 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700197}
198
Elliott Hughes34023802011-08-30 12:06:17 -0700199TEST_F(UtilsTest, Split) {
200 std::vector<std::string> actual;
201 std::vector<std::string> expected;
202
203 expected.clear();
204
205 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700206 Split("", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700207 EXPECT_EQ(expected, actual);
208
209 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700210 Split(":", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700211 EXPECT_EQ(expected, actual);
212
213 expected.clear();
214 expected.push_back("foo");
215
216 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700217 Split(":foo", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700218 EXPECT_EQ(expected, actual);
219
220 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700221 Split("foo:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700222 EXPECT_EQ(expected, actual);
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 expected.push_back("bar");
229
230 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700231 Split("foo:bar", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700232 EXPECT_EQ(expected, actual);
233
234 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700235 Split(":foo:bar", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700236 EXPECT_EQ(expected, actual);
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 expected.push_back("baz");
247
248 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700249 Split("foo:bar:baz", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700250 EXPECT_EQ(expected, actual);
251
252 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700253 Split(":foo:bar:baz", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700254 EXPECT_EQ(expected, actual);
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
Richard Uhler458ab532016-08-15 16:12:57 -0700265TEST_F(UtilsTest, GetDalvikCacheFilename) {
266 std::string name;
267 std::string error;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100268
Richard Uhler458ab532016-08-15 16:12:57 -0700269 EXPECT_TRUE(GetDalvikCacheFilename("/system/app/Foo.apk", "/foo", &name, &error)) << error;
270 EXPECT_EQ("/foo/system@app@Foo.apk@classes.dex", name);
271
272 EXPECT_TRUE(GetDalvikCacheFilename("/data/app/foo-1.apk", "/foo", &name, &error)) << error;
273 EXPECT_EQ("/foo/data@app@foo-1.apk@classes.dex", name);
274
275 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/core.jar", "/foo", &name, &error)) << error;
276 EXPECT_EQ("/foo/system@framework@core.jar@classes.dex", name);
277
278 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.art", "/foo", &name, &error)) << error;
279 EXPECT_EQ("/foo/system@framework@boot.art", name);
280
281 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.oat", "/foo", &name, &error)) << error;
282 EXPECT_EQ("/foo/system@framework@boot.oat", name);
Brian Carlstrom19a08362013-10-16 14:37:22 -0700283}
284
Andreas Gampe40da2862015-02-27 12:49:04 -0800285TEST_F(UtilsTest, GetDalvikCache) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700286 EXPECT_STREQ("", GetDalvikCache("should-not-exist123").c_str());
Andreas Gampe40da2862015-02-27 12:49:04 -0800287
Richard Uhler55b58b62016-08-12 09:05:13 -0700288 EXPECT_STREQ((android_data_ + "/dalvik-cache/.").c_str(), GetDalvikCache(".").c_str());
Andreas Gampe40da2862015-02-27 12:49:04 -0800289}
290
291
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700292TEST_F(UtilsTest, GetSystemImageFilename) {
293 EXPECT_STREQ("/system/framework/arm/boot.art",
294 GetSystemImageFilename("/system/framework/boot.art", kArm).c_str());
295}
296
Brian Carlstrom6449c622014-02-10 23:48:36 -0800297TEST_F(UtilsTest, ExecSuccess) {
298 std::vector<std::string> command;
299 if (kIsTargetBuild) {
Nicolas Geoffray4f7fdd22015-04-24 11:57:37 +0100300 std::string android_root(GetAndroidRoot());
301 command.push_back(android_root + "/bin/id");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800302 } else {
303 command.push_back("/usr/bin/id");
304 }
305 std::string error_msg;
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700306 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800307 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
308 EXPECT_TRUE(Exec(command, &error_msg));
309 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800310 EXPECT_EQ(0U, error_msg.size()) << error_msg;
311}
312
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800313TEST_F(UtilsTest, ExecError) {
Andreas Gampe369810a2015-01-14 19:53:31 -0800314 // This will lead to error messages in the log.
315 ScopedLogSeverity sls(LogSeverity::FATAL);
316
Brian Carlstrom6449c622014-02-10 23:48:36 -0800317 std::vector<std::string> command;
318 command.push_back("bogus");
319 std::string error_msg;
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700320 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800321 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
322 EXPECT_FALSE(Exec(command, &error_msg));
David Sehrd106d9f2016-08-16 19:22:57 -0700323 EXPECT_FALSE(error_msg.empty());
Mathieu Chartier661974a2014-01-09 11:23:53 -0800324 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800325}
326
David Sehrd106d9f2016-08-16 19:22:57 -0700327TEST_F(UtilsTest, EnvSnapshotAdditionsAreNotVisible) {
328 static constexpr const char* kModifiedVariable = "EXEC_SHOULD_NOT_EXPORT_THIS";
329 static constexpr int kOverwrite = 1;
330 // Set an variable in the current environment.
331 EXPECT_EQ(setenv(kModifiedVariable, "NEVER", kOverwrite), 0);
332 // Test that it is not exported.
333 std::vector<std::string> command;
334 if (kIsTargetBuild) {
335 std::string android_root(GetAndroidRoot());
336 command.push_back(android_root + "/bin/printenv");
337 } else {
338 command.push_back("/usr/bin/printenv");
339 }
340 command.push_back(kModifiedVariable);
341 std::string error_msg;
342 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
343 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
344 EXPECT_FALSE(Exec(command, &error_msg));
345 EXPECT_NE(0U, error_msg.size()) << error_msg;
346 }
347}
348
349TEST_F(UtilsTest, EnvSnapshotDeletionsAreNotVisible) {
350 static constexpr const char* kDeletedVariable = "PATH";
351 static constexpr int kOverwrite = 1;
352 // Save the variable's value.
353 const char* save_value = getenv(kDeletedVariable);
354 EXPECT_NE(save_value, nullptr);
355 // Delete the variable.
356 EXPECT_EQ(unsetenv(kDeletedVariable), 0);
357 // Test that it is not exported.
358 std::vector<std::string> command;
359 if (kIsTargetBuild) {
360 std::string android_root(GetAndroidRoot());
361 command.push_back(android_root + "/bin/printenv");
362 } else {
363 command.push_back("/usr/bin/printenv");
364 }
365 command.push_back(kDeletedVariable);
366 std::string error_msg;
367 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
368 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
369 EXPECT_TRUE(Exec(command, &error_msg));
370 EXPECT_EQ(0U, error_msg.size()) << error_msg;
371 }
372 // Restore the variable's value.
373 EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0);
374}
375
Narayan Kamath8508e372015-05-06 14:55:43 +0100376TEST_F(UtilsTest, IsValidDescriptor) {
377 std::vector<uint8_t> descriptor(
378 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, ';', 0x00 });
379 EXPECT_TRUE(IsValidDescriptor(reinterpret_cast<char*>(&descriptor[0])));
380
381 std::vector<uint8_t> unpaired_surrogate(
382 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, ';', 0x00 });
383 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate[0])));
384
385 std::vector<uint8_t> unpaired_surrogate_at_end(
386 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0x00 });
387 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_at_end[0])));
388
389 std::vector<uint8_t> invalid_surrogate(
390 { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, ';', 0x00 });
391 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&invalid_surrogate[0])));
392
393 std::vector<uint8_t> unpaired_surrogate_with_multibyte_sequence(
394 { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, 0xf0, 0x9f, 0x8f, 0xa0, ';', 0x00 });
395 EXPECT_FALSE(
396 IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_with_multibyte_sequence[0])));
397}
398
Orion Hodsonc069a302017-01-18 09:23:12 +0000399TEST_F(UtilsTest, ArrayCount) {
400 int i[64];
401 EXPECT_EQ(ArrayCount(i), 64u);
402 char c[7];
403 EXPECT_EQ(ArrayCount(c), 7u);
404}
405
406TEST_F(UtilsTest, BoundsCheckedCast) {
407 char buffer[64];
408 const char* buffer_end = buffer + ArrayCount(buffer);
409 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(nullptr, buffer, buffer_end), nullptr);
410 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer, buffer, buffer_end),
411 reinterpret_cast<const uint64_t*>(buffer));
412 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer + 56, buffer, buffer_end),
413 reinterpret_cast<const uint64_t*>(buffer + 56));
414 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer - 1, buffer, buffer_end), nullptr);
415 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer + 57, buffer, buffer_end), nullptr);
416}
417
Andreas Gampe3f093892017-09-14 15:11:01 -0700418TEST_F(UtilsTest, GetAndroidRootSafe) {
419 std::string error_msg;
420
421 // We don't expect null returns for most cases, so don't check and let std::string crash.
422
423 // CommonRuntimeTest sets ANDROID_ROOT, so expect this to be the same.
424 std::string android_root = GetAndroidRootSafe(&error_msg);
425 std::string android_root_env = getenv("ANDROID_ROOT");
426 EXPECT_EQ(android_root, android_root_env);
427
428 // Set ANDROID_ROOT to something else (but the directory must exist). So use dirname.
429 char* root_dup = strdup(android_root_env.c_str());
430 char* dir = dirname(root_dup);
431 ASSERT_EQ(0, setenv("ANDROID_ROOT", dir, 1 /* overwrite */));
432 std::string android_root2 = GetAndroidRootSafe(&error_msg);
433 EXPECT_STREQ(dir, android_root2.c_str());
434 free(root_dup);
435
436 // Set a bogus value for ANDROID_ROOT. This should be an error.
437 ASSERT_EQ(0, setenv("ANDROID_ROOT", "/this/is/obviously/bogus", 1 /* overwrite */));
438 EXPECT_TRUE(GetAndroidRootSafe(&error_msg) == nullptr);
439
440 // Unset ANDROID_ROOT and see that it still returns something (as libart code is running).
441 ASSERT_EQ(0, unsetenv("ANDROID_ROOT"));
442 std::string android_root3 = GetAndroidRootSafe(&error_msg);
443 // This should be the same as the other root (modulo realpath), otherwise the test setup is
444 // broken.
445 UniqueCPtr<char> real_root(realpath(android_root.c_str(), nullptr));
446 UniqueCPtr<char> real_root3(realpath(android_root3.c_str(), nullptr));
447 EXPECT_STREQ(real_root.get(), real_root3.get());
448
449
450 // Reset ANDROID_ROOT, as other things may depend on it.
451 ASSERT_EQ(0, setenv("ANDROID_ROOT", android_root_env.c_str(), 1 /* overwrite */));
452}
453
Elliott Hughes11e45072011-08-16 17:40:46 -0700454} // namespace art