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