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