blob: ee8eb363f1b16e6cb4c52d24fcd7859e72282852 [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
David Sehrd106d9f2016-08-16 19:22:57 -070019#include <stdlib.h>
20
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010022#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080023#include "common_runtime_test.h"
David Sehr97c381e2017-02-01 15:09:58 -080024#include "exec_utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/array-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "mirror/array.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object-inl.h"
29#include "mirror/object_array-inl.h"
30#include "mirror/string.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070031#include "scoped_thread_state_change-inl.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070032
Evgenii Stepanov1e133742015-05-20 12:30:59 -070033#include "base/memory_tool.h"
Mathieu Chartier661974a2014-01-09 11:23:53 -080034
Elliott Hughes11e45072011-08-16 17:40:46 -070035namespace art {
36
Elliott Hughes9058f2b2012-03-22 18:06:48 -070037std::string PrettyArguments(const char* signature);
38std::string PrettyReturnType(const char* signature);
39
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080040class UtilsTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070041
Elliott Hughes5174fe62011-08-23 15:12:35 -070042TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070043 EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
44 EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070045}
46
Elliott Hughes5174fe62011-08-23 15:12:35 -070047TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070048 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
49 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070050}
51
Mathieu Chartier76433272014-09-26 14:32:37 -070052TEST_F(UtilsTest, PrettyDescriptor_Primitive) {
53 EXPECT_EQ("boolean", PrettyDescriptor(Primitive::kPrimBoolean));
54 EXPECT_EQ("byte", PrettyDescriptor(Primitive::kPrimByte));
55 EXPECT_EQ("char", PrettyDescriptor(Primitive::kPrimChar));
56 EXPECT_EQ("short", PrettyDescriptor(Primitive::kPrimShort));
57 EXPECT_EQ("int", PrettyDescriptor(Primitive::kPrimInt));
58 EXPECT_EQ("float", PrettyDescriptor(Primitive::kPrimFloat));
59 EXPECT_EQ("long", PrettyDescriptor(Primitive::kPrimLong));
60 EXPECT_EQ("double", PrettyDescriptor(Primitive::kPrimDouble));
61 EXPECT_EQ("void", PrettyDescriptor(Primitive::kPrimVoid));
62}
63
Elliott Hughes5174fe62011-08-23 15:12:35 -070064TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070065 EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
66 EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
67 EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
68 EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
69 EXPECT_EQ("char[]", PrettyDescriptor("[C"));
70 EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
71 EXPECT_EQ("double[]", PrettyDescriptor("[D"));
72 EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
73 EXPECT_EQ("float[]", PrettyDescriptor("[F"));
74 EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
75 EXPECT_EQ("int[]", PrettyDescriptor("[I"));
76 EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
77 EXPECT_EQ("long[]", PrettyDescriptor("[J"));
78 EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
79 EXPECT_EQ("short[]", PrettyDescriptor("[S"));
80 EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
Elliott Hughes11e45072011-08-16 17:40:46 -070081}
82
Elliott Hughes5174fe62011-08-23 15:12:35 -070083TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070084 EXPECT_EQ("boolean", PrettyDescriptor("Z"));
85 EXPECT_EQ("byte", PrettyDescriptor("B"));
86 EXPECT_EQ("char", PrettyDescriptor("C"));
87 EXPECT_EQ("double", PrettyDescriptor("D"));
88 EXPECT_EQ("float", PrettyDescriptor("F"));
89 EXPECT_EQ("int", PrettyDescriptor("I"));
90 EXPECT_EQ("long", PrettyDescriptor("J"));
91 EXPECT_EQ("short", PrettyDescriptor("S"));
92}
93
94TEST_F(UtilsTest, PrettyArguments) {
95 EXPECT_EQ("()", PrettyArguments("()V"));
96 EXPECT_EQ("(int)", PrettyArguments("(I)V"));
97 EXPECT_EQ("(int, int)", PrettyArguments("(II)V"));
98 EXPECT_EQ("(int, int, int[][])", PrettyArguments("(II[[I)V"));
99 EXPECT_EQ("(int, int, int[][], java.lang.Poop)", PrettyArguments("(II[[ILjava/lang/Poop;)V"));
100 EXPECT_EQ("(int, int, int[][], java.lang.Poop, java.lang.Poop[][])", PrettyArguments("(II[[ILjava/lang/Poop;[[Ljava/lang/Poop;)V"));
101}
102
103TEST_F(UtilsTest, PrettyReturnType) {
104 EXPECT_EQ("void", PrettyReturnType("()V"));
105 EXPECT_EQ("int", PrettyReturnType("()I"));
106 EXPECT_EQ("int[][]", PrettyReturnType("()[[I"));
107 EXPECT_EQ("java.lang.Poop", PrettyReturnType("()Ljava/lang/Poop;"));
108 EXPECT_EQ("java.lang.Poop[][]", PrettyReturnType("()[[Ljava/lang/Poop;"));
Elliott Hughes11e45072011-08-16 17:40:46 -0700109}
110
Elliott Hughes54e7df12011-09-16 11:47:04 -0700111TEST_F(UtilsTest, PrettyTypeOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 ScopedObjectAccess soa(Thread::Current());
David Sehr709b0702016-10-13 09:12:37 -0700113 EXPECT_EQ("null", mirror::Object::PrettyTypeOf(nullptr));
Elliott Hughes11e45072011-08-16 17:40:46 -0700114
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 StackHandleScope<2> hs(soa.Self());
116 Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "")));
David Sehr709b0702016-10-13 09:12:37 -0700117 EXPECT_EQ("java.lang.String", mirror::Object::PrettyTypeOf(s.Get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700118
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2)));
David Sehr709b0702016-10-13 09:12:37 -0700120 EXPECT_EQ("short[]", mirror::Object::PrettyTypeOf(a.Get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700121
Ian Rogers98379392014-02-24 16:53:16 -0800122 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700123 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehr709b0702016-10-13 09:12:37 -0700125 EXPECT_EQ("java.lang.String[]", mirror::Object::PrettyTypeOf(o));
126 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Object::PrettyTypeOf(o->GetClass()));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700127}
128
129TEST_F(UtilsTest, PrettyClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 ScopedObjectAccess soa(Thread::Current());
David Sehr709b0702016-10-13 09:12:37 -0700131 EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
Ian Rogers98379392014-02-24 16:53:16 -0800132 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700133 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehr709b0702016-10-13 09:12:37 -0700135 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700136}
137
Ian Rogersd81871c2011-10-03 13:57:23 -0700138TEST_F(UtilsTest, PrettyClassAndClassLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 ScopedObjectAccess soa(Thread::Current());
David Sehr709b0702016-10-13 09:12:37 -0700140 EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
Ian Rogers98379392014-02-24 16:53:16 -0800141 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700142 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehr709b0702016-10-13 09:12:37 -0700144 EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
145 mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700146}
147
Elliott Hughes54e7df12011-09-16 11:47:04 -0700148TEST_F(UtilsTest, PrettyField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 ScopedObjectAccess soa(Thread::Current());
David Sehr709b0702016-10-13 09:12:37 -0700150 EXPECT_EQ("null", ArtField::PrettyField(nullptr));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700151
Ian Rogers98379392014-02-24 16:53:16 -0800152 mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(),
153 "Ljava/lang/String;");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700154
Mathieu Chartierc7853442015-03-27 14:35:38 -0700155 ArtField* f;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700156 f = java_lang_String->FindDeclaredInstanceField("count", "I");
David Sehr709b0702016-10-13 09:12:37 -0700157 EXPECT_EQ("int java.lang.String.count", f->PrettyField());
158 EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
Elliott Hughes11e45072011-08-16 17:40:46 -0700159}
160
Ian Rogers3bb17a62012-01-27 23:56:44 -0800161TEST_F(UtilsTest, PrettySize) {
162 EXPECT_EQ("1GB", PrettySize(1 * GB));
163 EXPECT_EQ("2GB", PrettySize(2 * GB));
164 if (sizeof(size_t) > sizeof(uint32_t)) {
165 EXPECT_EQ("100GB", PrettySize(100 * GB));
166 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700167 EXPECT_EQ("1024KB", PrettySize(1 * MB));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800168 EXPECT_EQ("10MB", PrettySize(10 * MB));
169 EXPECT_EQ("100MB", PrettySize(100 * MB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700170 EXPECT_EQ("1024B", PrettySize(1 * KB));
Elliott Hughes409d2732012-04-03 13:34:44 -0700171 EXPECT_EQ("10KB", PrettySize(10 * KB));
172 EXPECT_EQ("100KB", PrettySize(100 * KB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700173 EXPECT_EQ("0B", PrettySize(0));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800174 EXPECT_EQ("1B", PrettySize(1));
175 EXPECT_EQ("10B", PrettySize(10));
176 EXPECT_EQ("100B", PrettySize(100));
Elliott Hughesc967f782012-04-16 10:23:15 -0700177 EXPECT_EQ("512B", PrettySize(512));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800178}
179
Elliott Hughes79082e32011-08-25 12:07:32 -0700180TEST_F(UtilsTest, MangleForJni) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes79082e32011-08-25 12:07:32 -0700182 EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
183 EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
184 EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
185 EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
186 EXPECT_EQ("_3C", MangleForJni("[C"));
187}
188
189TEST_F(UtilsTest, JniShortName_JniLongName) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800191 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700192 ASSERT_TRUE(c != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700193 ArtMethod* m;
Elliott Hughes79082e32011-08-25 12:07:32 -0700194
Vladimir Markoba118822017-06-12 15:41:56 +0100195 m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700196 ASSERT_TRUE(m != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100197 ASSERT_FALSE(m->IsDirect());
David Sehr709b0702016-10-13 09:12:37 -0700198 EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
199 EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700200
Vladimir Markoba118822017-06-12 15:41:56 +0100201 m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700202 ASSERT_TRUE(m != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100203 ASSERT_FALSE(m->IsDirect());
David Sehr709b0702016-10-13 09:12:37 -0700204 EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
205 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700206
Vladimir Markoba118822017-06-12 15:41:56 +0100207 m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700208 ASSERT_TRUE(m != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100209 ASSERT_TRUE(m->IsStatic());
David Sehr709b0702016-10-13 09:12:37 -0700210 EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
211 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700212}
213
Elliott Hughes34023802011-08-30 12:06:17 -0700214TEST_F(UtilsTest, Split) {
215 std::vector<std::string> actual;
216 std::vector<std::string> expected;
217
218 expected.clear();
219
220 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700221 Split("", ':', &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(":", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700226 EXPECT_EQ(expected, actual);
227
228 expected.clear();
229 expected.push_back("foo");
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 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700236 Split("foo:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700237 EXPECT_EQ(expected, actual);
238
239 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700240 Split(":foo:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700241 EXPECT_EQ(expected, actual);
242
243 expected.push_back("bar");
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 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700254 Split("foo:bar:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700255 EXPECT_EQ(expected, actual);
256
257 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700258 Split(":foo:bar:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700259 EXPECT_EQ(expected, actual);
260
261 expected.push_back("baz");
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 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700272 Split("foo:bar:baz:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700273 EXPECT_EQ(expected, actual);
274
275 actual.clear();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700276 Split(":foo:bar:baz:", ':', &actual);
Elliott Hughes34023802011-08-30 12:06:17 -0700277 EXPECT_EQ(expected, actual);
278}
279
Richard Uhler458ab532016-08-15 16:12:57 -0700280TEST_F(UtilsTest, GetDalvikCacheFilename) {
281 std::string name;
282 std::string error;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100283
Richard Uhler458ab532016-08-15 16:12:57 -0700284 EXPECT_TRUE(GetDalvikCacheFilename("/system/app/Foo.apk", "/foo", &name, &error)) << error;
285 EXPECT_EQ("/foo/system@app@Foo.apk@classes.dex", name);
286
287 EXPECT_TRUE(GetDalvikCacheFilename("/data/app/foo-1.apk", "/foo", &name, &error)) << error;
288 EXPECT_EQ("/foo/data@app@foo-1.apk@classes.dex", name);
289
290 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/core.jar", "/foo", &name, &error)) << error;
291 EXPECT_EQ("/foo/system@framework@core.jar@classes.dex", name);
292
293 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.art", "/foo", &name, &error)) << error;
294 EXPECT_EQ("/foo/system@framework@boot.art", name);
295
296 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.oat", "/foo", &name, &error)) << error;
297 EXPECT_EQ("/foo/system@framework@boot.oat", name);
Brian Carlstrom19a08362013-10-16 14:37:22 -0700298}
299
Andreas Gampe40da2862015-02-27 12:49:04 -0800300TEST_F(UtilsTest, GetDalvikCache) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700301 EXPECT_STREQ("", GetDalvikCache("should-not-exist123").c_str());
Andreas Gampe40da2862015-02-27 12:49:04 -0800302
Richard Uhler55b58b62016-08-12 09:05:13 -0700303 EXPECT_STREQ((android_data_ + "/dalvik-cache/.").c_str(), GetDalvikCache(".").c_str());
Andreas Gampe40da2862015-02-27 12:49:04 -0800304}
305
306
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700307TEST_F(UtilsTest, GetSystemImageFilename) {
308 EXPECT_STREQ("/system/framework/arm/boot.art",
309 GetSystemImageFilename("/system/framework/boot.art", kArm).c_str());
310}
311
Brian Carlstrom6449c622014-02-10 23:48:36 -0800312TEST_F(UtilsTest, ExecSuccess) {
313 std::vector<std::string> command;
314 if (kIsTargetBuild) {
Nicolas Geoffray4f7fdd22015-04-24 11:57:37 +0100315 std::string android_root(GetAndroidRoot());
316 command.push_back(android_root + "/bin/id");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800317 } else {
318 command.push_back("/usr/bin/id");
319 }
320 std::string error_msg;
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700321 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800322 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
323 EXPECT_TRUE(Exec(command, &error_msg));
324 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800325 EXPECT_EQ(0U, error_msg.size()) << error_msg;
326}
327
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800328TEST_F(UtilsTest, ExecError) {
Andreas Gampe369810a2015-01-14 19:53:31 -0800329 // This will lead to error messages in the log.
330 ScopedLogSeverity sls(LogSeverity::FATAL);
331
Brian Carlstrom6449c622014-02-10 23:48:36 -0800332 std::vector<std::string> command;
333 command.push_back("bogus");
334 std::string error_msg;
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700335 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800336 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
337 EXPECT_FALSE(Exec(command, &error_msg));
David Sehrd106d9f2016-08-16 19:22:57 -0700338 EXPECT_FALSE(error_msg.empty());
Mathieu Chartier661974a2014-01-09 11:23:53 -0800339 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800340}
341
David Sehrd106d9f2016-08-16 19:22:57 -0700342TEST_F(UtilsTest, EnvSnapshotAdditionsAreNotVisible) {
343 static constexpr const char* kModifiedVariable = "EXEC_SHOULD_NOT_EXPORT_THIS";
344 static constexpr int kOverwrite = 1;
345 // Set an variable in the current environment.
346 EXPECT_EQ(setenv(kModifiedVariable, "NEVER", kOverwrite), 0);
347 // Test that it is not exported.
348 std::vector<std::string> command;
349 if (kIsTargetBuild) {
350 std::string android_root(GetAndroidRoot());
351 command.push_back(android_root + "/bin/printenv");
352 } else {
353 command.push_back("/usr/bin/printenv");
354 }
355 command.push_back(kModifiedVariable);
356 std::string error_msg;
357 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
358 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
359 EXPECT_FALSE(Exec(command, &error_msg));
360 EXPECT_NE(0U, error_msg.size()) << error_msg;
361 }
362}
363
364TEST_F(UtilsTest, EnvSnapshotDeletionsAreNotVisible) {
365 static constexpr const char* kDeletedVariable = "PATH";
366 static constexpr int kOverwrite = 1;
367 // Save the variable's value.
368 const char* save_value = getenv(kDeletedVariable);
369 EXPECT_NE(save_value, nullptr);
370 // Delete the variable.
371 EXPECT_EQ(unsetenv(kDeletedVariable), 0);
372 // Test that it is not exported.
373 std::vector<std::string> command;
374 if (kIsTargetBuild) {
375 std::string android_root(GetAndroidRoot());
376 command.push_back(android_root + "/bin/printenv");
377 } else {
378 command.push_back("/usr/bin/printenv");
379 }
380 command.push_back(kDeletedVariable);
381 std::string error_msg;
382 if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
383 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
384 EXPECT_TRUE(Exec(command, &error_msg));
385 EXPECT_EQ(0U, error_msg.size()) << error_msg;
386 }
387 // Restore the variable's value.
388 EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0);
389}
390
Narayan Kamath8508e372015-05-06 14:55:43 +0100391TEST_F(UtilsTest, IsValidDescriptor) {
392 std::vector<uint8_t> descriptor(
393 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, ';', 0x00 });
394 EXPECT_TRUE(IsValidDescriptor(reinterpret_cast<char*>(&descriptor[0])));
395
396 std::vector<uint8_t> unpaired_surrogate(
397 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, ';', 0x00 });
398 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate[0])));
399
400 std::vector<uint8_t> unpaired_surrogate_at_end(
401 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0x00 });
402 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_at_end[0])));
403
404 std::vector<uint8_t> invalid_surrogate(
405 { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, ';', 0x00 });
406 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&invalid_surrogate[0])));
407
408 std::vector<uint8_t> unpaired_surrogate_with_multibyte_sequence(
409 { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, 0xf0, 0x9f, 0x8f, 0xa0, ';', 0x00 });
410 EXPECT_FALSE(
411 IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_with_multibyte_sequence[0])));
412}
413
Orion Hodsonc069a302017-01-18 09:23:12 +0000414TEST_F(UtilsTest, ArrayCount) {
415 int i[64];
416 EXPECT_EQ(ArrayCount(i), 64u);
417 char c[7];
418 EXPECT_EQ(ArrayCount(c), 7u);
419}
420
421TEST_F(UtilsTest, BoundsCheckedCast) {
422 char buffer[64];
423 const char* buffer_end = buffer + ArrayCount(buffer);
424 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(nullptr, buffer, buffer_end), nullptr);
425 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer, buffer, buffer_end),
426 reinterpret_cast<const uint64_t*>(buffer));
427 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer + 56, buffer, buffer_end),
428 reinterpret_cast<const uint64_t*>(buffer + 56));
429 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer - 1, buffer, buffer_end), nullptr);
430 EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer + 57, buffer, buffer_end), nullptr);
431}
432
Elliott Hughes11e45072011-08-16 17:40:46 -0700433} // namespace art