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