Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "utils.h" |
| 18 | |
Andreas Gampe | 3f09389 | 2017-09-14 15:11:01 -0700 | [diff] [blame] | 19 | #include <libgen.h> |
David Sehr | d106d9f | 2016-08-16 19:22:57 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 23 | #include "base/file_utils.h" |
Andreas Gampe | 3f09389 | 2017-09-14 15:11:01 -0700 | [diff] [blame] | 24 | #include "base/stl_util.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 25 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 26 | #include "common_runtime_test.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 27 | #include "exec_utils.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/array-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 30 | #include "mirror/array.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "mirror/object-inl.h" |
| 32 | #include "mirror/object_array-inl.h" |
| 33 | #include "mirror/string.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 34 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 35 | |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 36 | #include "base/memory_tool.h" |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 37 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 38 | namespace art { |
| 39 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 40 | std::string PrettyArguments(const char* signature); |
| 41 | std::string PrettyReturnType(const char* signature); |
| 42 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 43 | class UtilsTest : public CommonRuntimeTest {}; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 45 | TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 46 | EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;")); |
| 47 | EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;")); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 50 | TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 51 | EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;")); |
| 52 | EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;")); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 55 | TEST_F(UtilsTest, PrettyDescriptor_Primitive) { |
| 56 | EXPECT_EQ("boolean", PrettyDescriptor(Primitive::kPrimBoolean)); |
| 57 | EXPECT_EQ("byte", PrettyDescriptor(Primitive::kPrimByte)); |
| 58 | EXPECT_EQ("char", PrettyDescriptor(Primitive::kPrimChar)); |
| 59 | EXPECT_EQ("short", PrettyDescriptor(Primitive::kPrimShort)); |
| 60 | EXPECT_EQ("int", PrettyDescriptor(Primitive::kPrimInt)); |
| 61 | EXPECT_EQ("float", PrettyDescriptor(Primitive::kPrimFloat)); |
| 62 | EXPECT_EQ("long", PrettyDescriptor(Primitive::kPrimLong)); |
| 63 | EXPECT_EQ("double", PrettyDescriptor(Primitive::kPrimDouble)); |
| 64 | EXPECT_EQ("void", PrettyDescriptor(Primitive::kPrimVoid)); |
| 65 | } |
| 66 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 67 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 68 | EXPECT_EQ("boolean[]", PrettyDescriptor("[Z")); |
| 69 | EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z")); |
| 70 | EXPECT_EQ("byte[]", PrettyDescriptor("[B")); |
| 71 | EXPECT_EQ("byte[][]", PrettyDescriptor("[[B")); |
| 72 | EXPECT_EQ("char[]", PrettyDescriptor("[C")); |
| 73 | EXPECT_EQ("char[][]", PrettyDescriptor("[[C")); |
| 74 | EXPECT_EQ("double[]", PrettyDescriptor("[D")); |
| 75 | EXPECT_EQ("double[][]", PrettyDescriptor("[[D")); |
| 76 | EXPECT_EQ("float[]", PrettyDescriptor("[F")); |
| 77 | EXPECT_EQ("float[][]", PrettyDescriptor("[[F")); |
| 78 | EXPECT_EQ("int[]", PrettyDescriptor("[I")); |
| 79 | EXPECT_EQ("int[][]", PrettyDescriptor("[[I")); |
| 80 | EXPECT_EQ("long[]", PrettyDescriptor("[J")); |
| 81 | EXPECT_EQ("long[][]", PrettyDescriptor("[[J")); |
| 82 | EXPECT_EQ("short[]", PrettyDescriptor("[S")); |
| 83 | EXPECT_EQ("short[][]", PrettyDescriptor("[[S")); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 86 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 87 | EXPECT_EQ("boolean", PrettyDescriptor("Z")); |
| 88 | EXPECT_EQ("byte", PrettyDescriptor("B")); |
| 89 | EXPECT_EQ("char", PrettyDescriptor("C")); |
| 90 | EXPECT_EQ("double", PrettyDescriptor("D")); |
| 91 | EXPECT_EQ("float", PrettyDescriptor("F")); |
| 92 | EXPECT_EQ("int", PrettyDescriptor("I")); |
| 93 | EXPECT_EQ("long", PrettyDescriptor("J")); |
| 94 | EXPECT_EQ("short", PrettyDescriptor("S")); |
| 95 | } |
| 96 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 97 | TEST_F(UtilsTest, PrettyTypeOf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 98 | ScopedObjectAccess soa(Thread::Current()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 99 | EXPECT_EQ("null", mirror::Object::PrettyTypeOf(nullptr)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 100 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 101 | StackHandleScope<2> hs(soa.Self()); |
| 102 | Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), ""))); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 103 | EXPECT_EQ("java.lang.String", mirror::Object::PrettyTypeOf(s.Get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 104 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 105 | Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2))); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 106 | EXPECT_EQ("short[]", mirror::Object::PrettyTypeOf(a.Get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 107 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 108 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(c != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 110 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 111 | EXPECT_EQ("java.lang.String[]", mirror::Object::PrettyTypeOf(o)); |
| 112 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Object::PrettyTypeOf(o->GetClass())); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST_F(UtilsTest, PrettyClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 116 | ScopedObjectAccess soa(Thread::Current()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 117 | EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 118 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 119 | ASSERT_TRUE(c != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 121 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass())); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 124 | TEST_F(UtilsTest, PrettyClassAndClassLoader) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 125 | ScopedObjectAccess soa(Thread::Current()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 126 | EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 127 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 128 | ASSERT_TRUE(c != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 130 | EXPECT_EQ("java.lang.Class<java.lang.String[],null>", |
| 131 | mirror::Class::PrettyClassAndClassLoader(o->GetClass())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 134 | TEST_F(UtilsTest, PrettyField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 135 | ScopedObjectAccess soa(Thread::Current()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 136 | EXPECT_EQ("null", ArtField::PrettyField(nullptr)); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 137 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 138 | mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(), |
| 139 | "Ljava/lang/String;"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 140 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 141 | ArtField* f; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 142 | f = java_lang_String->FindDeclaredInstanceField("count", "I"); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 143 | EXPECT_EQ("int java.lang.String.count", f->PrettyField()); |
| 144 | EXPECT_EQ("java.lang.String.count", f->PrettyField(false)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 147 | TEST_F(UtilsTest, PrettySize) { |
| 148 | EXPECT_EQ("1GB", PrettySize(1 * GB)); |
| 149 | EXPECT_EQ("2GB", PrettySize(2 * GB)); |
| 150 | if (sizeof(size_t) > sizeof(uint32_t)) { |
| 151 | EXPECT_EQ("100GB", PrettySize(100 * GB)); |
| 152 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 153 | EXPECT_EQ("1024KB", PrettySize(1 * MB)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 154 | EXPECT_EQ("10MB", PrettySize(10 * MB)); |
| 155 | EXPECT_EQ("100MB", PrettySize(100 * MB)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 156 | EXPECT_EQ("1024B", PrettySize(1 * KB)); |
Elliott Hughes | 409d273 | 2012-04-03 13:34:44 -0700 | [diff] [blame] | 157 | EXPECT_EQ("10KB", PrettySize(10 * KB)); |
| 158 | EXPECT_EQ("100KB", PrettySize(100 * KB)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 159 | EXPECT_EQ("0B", PrettySize(0)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 160 | EXPECT_EQ("1B", PrettySize(1)); |
| 161 | EXPECT_EQ("10B", PrettySize(10)); |
| 162 | EXPECT_EQ("100B", PrettySize(100)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 163 | EXPECT_EQ("512B", PrettySize(512)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 166 | TEST_F(UtilsTest, MangleForJni) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 167 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 168 | EXPECT_EQ("hello_00024world", MangleForJni("hello$world")); |
| 169 | EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world")); |
| 170 | EXPECT_EQ("hello_1world", MangleForJni("hello_world")); |
| 171 | EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;")); |
| 172 | EXPECT_EQ("_3C", MangleForJni("[C")); |
| 173 | } |
| 174 | |
| 175 | TEST_F(UtilsTest, JniShortName_JniLongName) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 176 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 177 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 178 | ASSERT_TRUE(c != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 179 | ArtMethod* m; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 180 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 181 | m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 182 | ASSERT_TRUE(m != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 183 | ASSERT_FALSE(m->IsDirect()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 184 | EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName()); |
| 185 | EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 186 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 187 | m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 188 | ASSERT_TRUE(m != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 189 | ASSERT_FALSE(m->IsDirect()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 190 | EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName()); |
| 191 | EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 192 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 193 | m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 194 | ASSERT_TRUE(m != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 195 | ASSERT_TRUE(m->IsStatic()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 196 | EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName()); |
| 197 | EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 200 | TEST_F(UtilsTest, Split) { |
| 201 | std::vector<std::string> actual; |
| 202 | std::vector<std::string> expected; |
| 203 | |
| 204 | expected.clear(); |
| 205 | |
| 206 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 207 | Split("", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 208 | EXPECT_EQ(expected, actual); |
| 209 | |
| 210 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 211 | Split(":", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 212 | EXPECT_EQ(expected, actual); |
| 213 | |
| 214 | expected.clear(); |
| 215 | expected.push_back("foo"); |
| 216 | |
| 217 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 218 | Split(":foo", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 219 | EXPECT_EQ(expected, actual); |
| 220 | |
| 221 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 222 | Split("foo:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 223 | EXPECT_EQ(expected, actual); |
| 224 | |
| 225 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 226 | Split(":foo:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 227 | EXPECT_EQ(expected, actual); |
| 228 | |
| 229 | expected.push_back("bar"); |
| 230 | |
| 231 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 232 | Split("foo:bar", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 233 | EXPECT_EQ(expected, actual); |
| 234 | |
| 235 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 236 | Split(":foo:bar", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 237 | EXPECT_EQ(expected, actual); |
| 238 | |
| 239 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 240 | Split("foo:bar:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 241 | EXPECT_EQ(expected, actual); |
| 242 | |
| 243 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 244 | Split(":foo:bar:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 245 | EXPECT_EQ(expected, actual); |
| 246 | |
| 247 | expected.push_back("baz"); |
| 248 | |
| 249 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 250 | Split("foo:bar:baz", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 251 | EXPECT_EQ(expected, actual); |
| 252 | |
| 253 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 254 | Split(":foo:bar:baz", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 255 | EXPECT_EQ(expected, actual); |
| 256 | |
| 257 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 258 | Split("foo:bar:baz:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 259 | EXPECT_EQ(expected, actual); |
| 260 | |
| 261 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 262 | Split(":foo:bar:baz:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 263 | EXPECT_EQ(expected, actual); |
| 264 | } |
| 265 | |
Richard Uhler | 458ab53 | 2016-08-15 16:12:57 -0700 | [diff] [blame] | 266 | TEST_F(UtilsTest, GetDalvikCacheFilename) { |
| 267 | std::string name; |
| 268 | std::string error; |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 269 | |
Richard Uhler | 458ab53 | 2016-08-15 16:12:57 -0700 | [diff] [blame] | 270 | EXPECT_TRUE(GetDalvikCacheFilename("/system/app/Foo.apk", "/foo", &name, &error)) << error; |
| 271 | EXPECT_EQ("/foo/system@app@Foo.apk@classes.dex", name); |
| 272 | |
| 273 | EXPECT_TRUE(GetDalvikCacheFilename("/data/app/foo-1.apk", "/foo", &name, &error)) << error; |
| 274 | EXPECT_EQ("/foo/data@app@foo-1.apk@classes.dex", name); |
| 275 | |
| 276 | EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/core.jar", "/foo", &name, &error)) << error; |
| 277 | EXPECT_EQ("/foo/system@framework@core.jar@classes.dex", name); |
| 278 | |
| 279 | EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.art", "/foo", &name, &error)) << error; |
| 280 | EXPECT_EQ("/foo/system@framework@boot.art", name); |
| 281 | |
| 282 | EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.oat", "/foo", &name, &error)) << error; |
| 283 | EXPECT_EQ("/foo/system@framework@boot.oat", name); |
Brian Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 286 | TEST_F(UtilsTest, GetDalvikCache) { |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 287 | EXPECT_STREQ("", GetDalvikCache("should-not-exist123").c_str()); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 288 | |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 289 | EXPECT_STREQ((android_data_ + "/dalvik-cache/.").c_str(), GetDalvikCache(".").c_str()); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 293 | TEST_F(UtilsTest, GetSystemImageFilename) { |
| 294 | EXPECT_STREQ("/system/framework/arm/boot.art", |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 295 | GetSystemImageFilename("/system/framework/boot.art", InstructionSet::kArm).c_str()); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 298 | TEST_F(UtilsTest, ExecSuccess) { |
| 299 | std::vector<std::string> command; |
| 300 | if (kIsTargetBuild) { |
Nicolas Geoffray | 4f7fdd2 | 2015-04-24 11:57:37 +0100 | [diff] [blame] | 301 | std::string android_root(GetAndroidRoot()); |
| 302 | command.push_back(android_root + "/bin/id"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 303 | } else { |
| 304 | command.push_back("/usr/bin/id"); |
| 305 | } |
| 306 | std::string error_msg; |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 307 | if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 308 | // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks. |
| 309 | EXPECT_TRUE(Exec(command, &error_msg)); |
| 310 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 311 | EXPECT_EQ(0U, error_msg.size()) << error_msg; |
| 312 | } |
| 313 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 314 | TEST_F(UtilsTest, ExecError) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 315 | // This will lead to error messages in the log. |
| 316 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 317 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 318 | std::vector<std::string> command; |
| 319 | command.push_back("bogus"); |
| 320 | std::string error_msg; |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 321 | if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 322 | // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks. |
| 323 | EXPECT_FALSE(Exec(command, &error_msg)); |
David Sehr | d106d9f | 2016-08-16 19:22:57 -0700 | [diff] [blame] | 324 | EXPECT_FALSE(error_msg.empty()); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 325 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 326 | } |
| 327 | |
David Sehr | d106d9f | 2016-08-16 19:22:57 -0700 | [diff] [blame] | 328 | TEST_F(UtilsTest, EnvSnapshotAdditionsAreNotVisible) { |
| 329 | static constexpr const char* kModifiedVariable = "EXEC_SHOULD_NOT_EXPORT_THIS"; |
| 330 | static constexpr int kOverwrite = 1; |
| 331 | // Set an variable in the current environment. |
| 332 | EXPECT_EQ(setenv(kModifiedVariable, "NEVER", kOverwrite), 0); |
| 333 | // Test that it is not exported. |
| 334 | std::vector<std::string> command; |
| 335 | if (kIsTargetBuild) { |
| 336 | std::string android_root(GetAndroidRoot()); |
| 337 | command.push_back(android_root + "/bin/printenv"); |
| 338 | } else { |
| 339 | command.push_back("/usr/bin/printenv"); |
| 340 | } |
| 341 | command.push_back(kModifiedVariable); |
| 342 | std::string error_msg; |
| 343 | if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
| 344 | // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks. |
| 345 | EXPECT_FALSE(Exec(command, &error_msg)); |
| 346 | EXPECT_NE(0U, error_msg.size()) << error_msg; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | TEST_F(UtilsTest, EnvSnapshotDeletionsAreNotVisible) { |
| 351 | static constexpr const char* kDeletedVariable = "PATH"; |
| 352 | static constexpr int kOverwrite = 1; |
| 353 | // Save the variable's value. |
| 354 | const char* save_value = getenv(kDeletedVariable); |
| 355 | EXPECT_NE(save_value, nullptr); |
| 356 | // Delete the variable. |
| 357 | EXPECT_EQ(unsetenv(kDeletedVariable), 0); |
| 358 | // Test that it is not exported. |
| 359 | std::vector<std::string> command; |
| 360 | if (kIsTargetBuild) { |
| 361 | std::string android_root(GetAndroidRoot()); |
| 362 | command.push_back(android_root + "/bin/printenv"); |
| 363 | } else { |
| 364 | command.push_back("/usr/bin/printenv"); |
| 365 | } |
| 366 | command.push_back(kDeletedVariable); |
| 367 | std::string error_msg; |
| 368 | if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
| 369 | // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks. |
| 370 | EXPECT_TRUE(Exec(command, &error_msg)); |
| 371 | EXPECT_EQ(0U, error_msg.size()) << error_msg; |
| 372 | } |
| 373 | // Restore the variable's value. |
| 374 | EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0); |
| 375 | } |
| 376 | |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 377 | TEST_F(UtilsTest, IsValidDescriptor) { |
| 378 | std::vector<uint8_t> descriptor( |
| 379 | { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, ';', 0x00 }); |
| 380 | EXPECT_TRUE(IsValidDescriptor(reinterpret_cast<char*>(&descriptor[0]))); |
| 381 | |
| 382 | std::vector<uint8_t> unpaired_surrogate( |
| 383 | { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, ';', 0x00 }); |
| 384 | EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate[0]))); |
| 385 | |
| 386 | std::vector<uint8_t> unpaired_surrogate_at_end( |
| 387 | { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0x00 }); |
| 388 | EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_at_end[0]))); |
| 389 | |
| 390 | std::vector<uint8_t> invalid_surrogate( |
| 391 | { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, ';', 0x00 }); |
| 392 | EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&invalid_surrogate[0]))); |
| 393 | |
| 394 | std::vector<uint8_t> unpaired_surrogate_with_multibyte_sequence( |
| 395 | { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, 0xf0, 0x9f, 0x8f, 0xa0, ';', 0x00 }); |
| 396 | EXPECT_FALSE( |
| 397 | IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_with_multibyte_sequence[0]))); |
| 398 | } |
| 399 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 400 | TEST_F(UtilsTest, ArrayCount) { |
| 401 | int i[64]; |
| 402 | EXPECT_EQ(ArrayCount(i), 64u); |
| 403 | char c[7]; |
| 404 | EXPECT_EQ(ArrayCount(c), 7u); |
| 405 | } |
| 406 | |
| 407 | TEST_F(UtilsTest, BoundsCheckedCast) { |
| 408 | char buffer[64]; |
| 409 | const char* buffer_end = buffer + ArrayCount(buffer); |
| 410 | EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(nullptr, buffer, buffer_end), nullptr); |
| 411 | EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer, buffer, buffer_end), |
| 412 | reinterpret_cast<const uint64_t*>(buffer)); |
| 413 | EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer + 56, buffer, buffer_end), |
| 414 | reinterpret_cast<const uint64_t*>(buffer + 56)); |
| 415 | EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer - 1, buffer, buffer_end), nullptr); |
| 416 | EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer + 57, buffer, buffer_end), nullptr); |
| 417 | } |
| 418 | |
Andreas Gampe | 3f09389 | 2017-09-14 15:11:01 -0700 | [diff] [blame] | 419 | TEST_F(UtilsTest, GetAndroidRootSafe) { |
| 420 | std::string error_msg; |
| 421 | |
| 422 | // We don't expect null returns for most cases, so don't check and let std::string crash. |
| 423 | |
| 424 | // CommonRuntimeTest sets ANDROID_ROOT, so expect this to be the same. |
| 425 | std::string android_root = GetAndroidRootSafe(&error_msg); |
| 426 | std::string android_root_env = getenv("ANDROID_ROOT"); |
| 427 | EXPECT_EQ(android_root, android_root_env); |
| 428 | |
| 429 | // Set ANDROID_ROOT to something else (but the directory must exist). So use dirname. |
| 430 | char* root_dup = strdup(android_root_env.c_str()); |
| 431 | char* dir = dirname(root_dup); |
| 432 | ASSERT_EQ(0, setenv("ANDROID_ROOT", dir, 1 /* overwrite */)); |
| 433 | std::string android_root2 = GetAndroidRootSafe(&error_msg); |
| 434 | EXPECT_STREQ(dir, android_root2.c_str()); |
| 435 | free(root_dup); |
| 436 | |
| 437 | // Set a bogus value for ANDROID_ROOT. This should be an error. |
| 438 | ASSERT_EQ(0, setenv("ANDROID_ROOT", "/this/is/obviously/bogus", 1 /* overwrite */)); |
| 439 | EXPECT_TRUE(GetAndroidRootSafe(&error_msg) == nullptr); |
| 440 | |
| 441 | // Unset ANDROID_ROOT and see that it still returns something (as libart code is running). |
| 442 | ASSERT_EQ(0, unsetenv("ANDROID_ROOT")); |
| 443 | std::string android_root3 = GetAndroidRootSafe(&error_msg); |
| 444 | // This should be the same as the other root (modulo realpath), otherwise the test setup is |
| 445 | // broken. |
| 446 | UniqueCPtr<char> real_root(realpath(android_root.c_str(), nullptr)); |
| 447 | UniqueCPtr<char> real_root3(realpath(android_root3.c_str(), nullptr)); |
| 448 | EXPECT_STREQ(real_root.get(), real_root3.get()); |
| 449 | |
| 450 | |
| 451 | // Reset ANDROID_ROOT, as other things may depend on it. |
| 452 | ASSERT_EQ(0, setenv("ANDROID_ROOT", android_root_env.c_str(), 1 /* overwrite */)); |
| 453 | } |
| 454 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 455 | } // namespace art |