Home | History | Annotate | Download | only in runtime
      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  */
     16 
     17 #include "utils.h"
     18 
     19 #include "common_runtime_test.h"
     20 #include "mirror/array.h"
     21 #include "mirror/array-inl.h"
     22 #include "mirror/object-inl.h"
     23 #include "mirror/object_array-inl.h"
     24 #include "mirror/string.h"
     25 #include "scoped_thread_state_change.h"
     26 #include "handle_scope-inl.h"
     27 
     28 #include <valgrind.h>
     29 
     30 namespace art {
     31 
     32 std::string PrettyArguments(const char* signature);
     33 std::string PrettyReturnType(const char* signature);
     34 
     35 class UtilsTest : public CommonRuntimeTest {};
     36 
     37 TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
     38   EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
     39   EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
     40 }
     41 
     42 TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
     43   EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
     44   EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
     45 }
     46 
     47 TEST_F(UtilsTest, PrettyDescriptor_Primitive) {
     48   EXPECT_EQ("boolean", PrettyDescriptor(Primitive::kPrimBoolean));
     49   EXPECT_EQ("byte", PrettyDescriptor(Primitive::kPrimByte));
     50   EXPECT_EQ("char", PrettyDescriptor(Primitive::kPrimChar));
     51   EXPECT_EQ("short", PrettyDescriptor(Primitive::kPrimShort));
     52   EXPECT_EQ("int", PrettyDescriptor(Primitive::kPrimInt));
     53   EXPECT_EQ("float", PrettyDescriptor(Primitive::kPrimFloat));
     54   EXPECT_EQ("long", PrettyDescriptor(Primitive::kPrimLong));
     55   EXPECT_EQ("double", PrettyDescriptor(Primitive::kPrimDouble));
     56   EXPECT_EQ("void", PrettyDescriptor(Primitive::kPrimVoid));
     57 }
     58 
     59 TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
     60   EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
     61   EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
     62   EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
     63   EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
     64   EXPECT_EQ("char[]", PrettyDescriptor("[C"));
     65   EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
     66   EXPECT_EQ("double[]", PrettyDescriptor("[D"));
     67   EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
     68   EXPECT_EQ("float[]", PrettyDescriptor("[F"));
     69   EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
     70   EXPECT_EQ("int[]", PrettyDescriptor("[I"));
     71   EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
     72   EXPECT_EQ("long[]", PrettyDescriptor("[J"));
     73   EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
     74   EXPECT_EQ("short[]", PrettyDescriptor("[S"));
     75   EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
     76 }
     77 
     78 TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
     79   EXPECT_EQ("boolean", PrettyDescriptor("Z"));
     80   EXPECT_EQ("byte", PrettyDescriptor("B"));
     81   EXPECT_EQ("char", PrettyDescriptor("C"));
     82   EXPECT_EQ("double", PrettyDescriptor("D"));
     83   EXPECT_EQ("float", PrettyDescriptor("F"));
     84   EXPECT_EQ("int", PrettyDescriptor("I"));
     85   EXPECT_EQ("long", PrettyDescriptor("J"));
     86   EXPECT_EQ("short", PrettyDescriptor("S"));
     87 }
     88 
     89 TEST_F(UtilsTest, PrettyArguments) {
     90   EXPECT_EQ("()", PrettyArguments("()V"));
     91   EXPECT_EQ("(int)", PrettyArguments("(I)V"));
     92   EXPECT_EQ("(int, int)", PrettyArguments("(II)V"));
     93   EXPECT_EQ("(int, int, int[][])", PrettyArguments("(II[[I)V"));
     94   EXPECT_EQ("(int, int, int[][], java.lang.Poop)", PrettyArguments("(II[[ILjava/lang/Poop;)V"));
     95   EXPECT_EQ("(int, int, int[][], java.lang.Poop, java.lang.Poop[][])", PrettyArguments("(II[[ILjava/lang/Poop;[[Ljava/lang/Poop;)V"));
     96 }
     97 
     98 TEST_F(UtilsTest, PrettyReturnType) {
     99   EXPECT_EQ("void", PrettyReturnType("()V"));
    100   EXPECT_EQ("int", PrettyReturnType("()I"));
    101   EXPECT_EQ("int[][]", PrettyReturnType("()[[I"));
    102   EXPECT_EQ("java.lang.Poop", PrettyReturnType("()Ljava/lang/Poop;"));
    103   EXPECT_EQ("java.lang.Poop[][]", PrettyReturnType("()[[Ljava/lang/Poop;"));
    104 }
    105 
    106 TEST_F(UtilsTest, PrettyTypeOf) {
    107   ScopedObjectAccess soa(Thread::Current());
    108   EXPECT_EQ("null", PrettyTypeOf(NULL));
    109 
    110   StackHandleScope<2> hs(soa.Self());
    111   Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "")));
    112   EXPECT_EQ("java.lang.String", PrettyTypeOf(s.Get()));
    113 
    114   Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2)));
    115   EXPECT_EQ("short[]", PrettyTypeOf(a.Get()));
    116 
    117   mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
    118   ASSERT_TRUE(c != NULL);
    119   mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
    120   EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o));
    121   EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass()));
    122 }
    123 
    124 TEST_F(UtilsTest, PrettyClass) {
    125   ScopedObjectAccess soa(Thread::Current());
    126   EXPECT_EQ("null", PrettyClass(NULL));
    127   mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
    128   ASSERT_TRUE(c != NULL);
    129   mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
    130   EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass()));
    131 }
    132 
    133 TEST_F(UtilsTest, PrettyClassAndClassLoader) {
    134   ScopedObjectAccess soa(Thread::Current());
    135   EXPECT_EQ("null", PrettyClassAndClassLoader(NULL));
    136   mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
    137   ASSERT_TRUE(c != NULL);
    138   mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
    139   EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass()));
    140 }
    141 
    142 TEST_F(UtilsTest, PrettyField) {
    143   ScopedObjectAccess soa(Thread::Current());
    144   EXPECT_EQ("null", PrettyField(NULL));
    145 
    146   mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(),
    147                                                                    "Ljava/lang/String;");
    148 
    149   mirror::ArtField* f;
    150   f = java_lang_String->FindDeclaredInstanceField("count", "I");
    151   EXPECT_EQ("int java.lang.String.count", PrettyField(f));
    152   EXPECT_EQ("java.lang.String.count", PrettyField(f, false));
    153   f = java_lang_String->FindDeclaredInstanceField("value", "[C");
    154   EXPECT_EQ("char[] java.lang.String.value", PrettyField(f));
    155   EXPECT_EQ("java.lang.String.value", PrettyField(f, false));
    156 }
    157 
    158 TEST_F(UtilsTest, PrettySize) {
    159   EXPECT_EQ("1GB", PrettySize(1 * GB));
    160   EXPECT_EQ("2GB", PrettySize(2 * GB));
    161   if (sizeof(size_t) > sizeof(uint32_t)) {
    162     EXPECT_EQ("100GB", PrettySize(100 * GB));
    163   }
    164   EXPECT_EQ("1024KB", PrettySize(1 * MB));
    165   EXPECT_EQ("10MB", PrettySize(10 * MB));
    166   EXPECT_EQ("100MB", PrettySize(100 * MB));
    167   EXPECT_EQ("1024B", PrettySize(1 * KB));
    168   EXPECT_EQ("10KB", PrettySize(10 * KB));
    169   EXPECT_EQ("100KB", PrettySize(100 * KB));
    170   EXPECT_EQ("0B", PrettySize(0));
    171   EXPECT_EQ("1B", PrettySize(1));
    172   EXPECT_EQ("10B", PrettySize(10));
    173   EXPECT_EQ("100B", PrettySize(100));
    174   EXPECT_EQ("512B", PrettySize(512));
    175 }
    176 
    177 TEST_F(UtilsTest, PrettyDuration) {
    178   const uint64_t one_sec = 1000000000;
    179   const uint64_t one_ms  = 1000000;
    180   const uint64_t one_us  = 1000;
    181 
    182   EXPECT_EQ("1s", PrettyDuration(1 * one_sec));
    183   EXPECT_EQ("10s", PrettyDuration(10 * one_sec));
    184   EXPECT_EQ("100s", PrettyDuration(100 * one_sec));
    185   EXPECT_EQ("1.001s", PrettyDuration(1 * one_sec + one_ms));
    186   EXPECT_EQ("1.000001s", PrettyDuration(1 * one_sec + one_us, 6));
    187   EXPECT_EQ("1.000000001s", PrettyDuration(1 * one_sec + 1, 9));
    188   EXPECT_EQ("1.000s", PrettyDuration(1 * one_sec + one_us, 3));
    189 
    190   EXPECT_EQ("1ms", PrettyDuration(1 * one_ms));
    191   EXPECT_EQ("10ms", PrettyDuration(10 * one_ms));
    192   EXPECT_EQ("100ms", PrettyDuration(100 * one_ms));
    193   EXPECT_EQ("1.001ms", PrettyDuration(1 * one_ms + one_us));
    194   EXPECT_EQ("1.000001ms", PrettyDuration(1 * one_ms + 1, 6));
    195 
    196   EXPECT_EQ("1us", PrettyDuration(1 * one_us));
    197   EXPECT_EQ("10us", PrettyDuration(10 * one_us));
    198   EXPECT_EQ("100us", PrettyDuration(100 * one_us));
    199   EXPECT_EQ("1.001us", PrettyDuration(1 * one_us + 1));
    200 
    201   EXPECT_EQ("1ns", PrettyDuration(1));
    202   EXPECT_EQ("10ns", PrettyDuration(10));
    203   EXPECT_EQ("100ns", PrettyDuration(100));
    204 }
    205 
    206 TEST_F(UtilsTest, MangleForJni) {
    207   ScopedObjectAccess soa(Thread::Current());
    208   EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
    209   EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
    210   EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
    211   EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
    212   EXPECT_EQ("_3C", MangleForJni("[C"));
    213 }
    214 
    215 TEST_F(UtilsTest, JniShortName_JniLongName) {
    216   ScopedObjectAccess soa(Thread::Current());
    217   mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
    218   ASSERT_TRUE(c != NULL);
    219   mirror::ArtMethod* m;
    220 
    221   m = c->FindVirtualMethod("charAt", "(I)C");
    222   ASSERT_TRUE(m != NULL);
    223   EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m));
    224   EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m));
    225 
    226   m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I");
    227   ASSERT_TRUE(m != NULL);
    228   EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m));
    229   EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m));
    230 
    231   m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;");
    232   ASSERT_TRUE(m != NULL);
    233   EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m));
    234   EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m));
    235 }
    236 
    237 TEST_F(UtilsTest, Split) {
    238   std::vector<std::string> actual;
    239   std::vector<std::string> expected;
    240 
    241   expected.clear();
    242 
    243   actual.clear();
    244   Split("", ':', actual);
    245   EXPECT_EQ(expected, actual);
    246 
    247   actual.clear();
    248   Split(":", ':', actual);
    249   EXPECT_EQ(expected, actual);
    250 
    251   expected.clear();
    252   expected.push_back("foo");
    253 
    254   actual.clear();
    255   Split(":foo", ':', actual);
    256   EXPECT_EQ(expected, actual);
    257 
    258   actual.clear();
    259   Split("foo:", ':', actual);
    260   EXPECT_EQ(expected, actual);
    261 
    262   actual.clear();
    263   Split(":foo:", ':', actual);
    264   EXPECT_EQ(expected, actual);
    265 
    266   expected.push_back("bar");
    267 
    268   actual.clear();
    269   Split("foo:bar", ':', actual);
    270   EXPECT_EQ(expected, actual);
    271 
    272   actual.clear();
    273   Split(":foo:bar", ':', actual);
    274   EXPECT_EQ(expected, actual);
    275 
    276   actual.clear();
    277   Split("foo:bar:", ':', actual);
    278   EXPECT_EQ(expected, actual);
    279 
    280   actual.clear();
    281   Split(":foo:bar:", ':', actual);
    282   EXPECT_EQ(expected, actual);
    283 
    284   expected.push_back("baz");
    285 
    286   actual.clear();
    287   Split("foo:bar:baz", ':', actual);
    288   EXPECT_EQ(expected, actual);
    289 
    290   actual.clear();
    291   Split(":foo:bar:baz", ':', actual);
    292   EXPECT_EQ(expected, actual);
    293 
    294   actual.clear();
    295   Split("foo:bar:baz:", ':', actual);
    296   EXPECT_EQ(expected, actual);
    297 
    298   actual.clear();
    299   Split(":foo:bar:baz:", ':', actual);
    300   EXPECT_EQ(expected, actual);
    301 }
    302 
    303 TEST_F(UtilsTest, Join) {
    304   std::vector<std::string> strings;
    305 
    306   strings.clear();
    307   EXPECT_EQ("", Join(strings, ':'));
    308 
    309   strings.clear();
    310   strings.push_back("foo");
    311   EXPECT_EQ("foo", Join(strings, ':'));
    312 
    313   strings.clear();
    314   strings.push_back("");
    315   strings.push_back("foo");
    316   EXPECT_EQ(":foo", Join(strings, ':'));
    317 
    318   strings.clear();
    319   strings.push_back("foo");
    320   strings.push_back("");
    321   EXPECT_EQ("foo:", Join(strings, ':'));
    322 
    323   strings.clear();
    324   strings.push_back("");
    325   strings.push_back("foo");
    326   strings.push_back("");
    327   EXPECT_EQ(":foo:", Join(strings, ':'));
    328 
    329   strings.clear();
    330   strings.push_back("foo");
    331   strings.push_back("bar");
    332   EXPECT_EQ("foo:bar", Join(strings, ':'));
    333 
    334   strings.clear();
    335   strings.push_back("foo");
    336   strings.push_back("bar");
    337   strings.push_back("baz");
    338   EXPECT_EQ("foo:bar:baz", Join(strings, ':'));
    339 }
    340 
    341 TEST_F(UtilsTest, StartsWith) {
    342   EXPECT_FALSE(StartsWith("foo", "bar"));
    343   EXPECT_TRUE(StartsWith("foo", "foo"));
    344   EXPECT_TRUE(StartsWith("food", "foo"));
    345   EXPECT_FALSE(StartsWith("fo", "foo"));
    346 }
    347 
    348 TEST_F(UtilsTest, EndsWith) {
    349   EXPECT_FALSE(EndsWith("foo", "bar"));
    350   EXPECT_TRUE(EndsWith("foo", "foo"));
    351   EXPECT_TRUE(EndsWith("foofoo", "foo"));
    352   EXPECT_FALSE(EndsWith("oo", "foo"));
    353 }
    354 
    355 TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) {
    356   EXPECT_STREQ("/foo/system@app (at) Foo.apk@classes.dex",
    357                GetDalvikCacheFilenameOrDie("/system/app/Foo.apk", "/foo").c_str());
    358 
    359   EXPECT_STREQ("/foo/data@app (at) foo-1.apk@classes.dex",
    360                GetDalvikCacheFilenameOrDie("/data/app/foo-1.apk", "/foo").c_str());
    361   EXPECT_STREQ("/foo/system@framework (at) core.jar@classes.dex",
    362                GetDalvikCacheFilenameOrDie("/system/framework/core.jar", "/foo").c_str());
    363   EXPECT_STREQ("/foo/system@framework (at) boot.art",
    364                GetDalvikCacheFilenameOrDie("/system/framework/boot.art", "/foo").c_str());
    365   EXPECT_STREQ("/foo/system@framework (at) boot.oat",
    366                GetDalvikCacheFilenameOrDie("/system/framework/boot.oat", "/foo").c_str());
    367 }
    368 
    369 TEST_F(UtilsTest, GetSystemImageFilename) {
    370   EXPECT_STREQ("/system/framework/arm/boot.art",
    371                GetSystemImageFilename("/system/framework/boot.art", kArm).c_str());
    372 }
    373 
    374 TEST_F(UtilsTest, DexFilenameToOdexFilename) {
    375   EXPECT_STREQ("/foo/bar/arm/baz.odex",
    376                DexFilenameToOdexFilename("/foo/bar/baz.jar", kArm).c_str());
    377 }
    378 
    379 TEST_F(UtilsTest, ExecSuccess) {
    380   std::vector<std::string> command;
    381   if (kIsTargetBuild) {
    382     command.push_back("/system/bin/id");
    383   } else {
    384     command.push_back("/usr/bin/id");
    385   }
    386   std::string error_msg;
    387   if (RUNNING_ON_VALGRIND == 0) {
    388     // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
    389     EXPECT_TRUE(Exec(command, &error_msg));
    390   }
    391   EXPECT_EQ(0U, error_msg.size()) << error_msg;
    392 }
    393 
    394 TEST_F(UtilsTest, ExecError) {
    395   std::vector<std::string> command;
    396   command.push_back("bogus");
    397   std::string error_msg;
    398   if (RUNNING_ON_VALGRIND == 0) {
    399     // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
    400     EXPECT_FALSE(Exec(command, &error_msg));
    401     EXPECT_NE(0U, error_msg.size());
    402   }
    403 }
    404 
    405 TEST_F(UtilsTest, RoundUpToPowerOfTwo) {
    406   // Tests the constexpr variant since all the parameters are constexpr
    407   EXPECT_EQ(0, RoundUpToPowerOfTwo(0));
    408   EXPECT_EQ(1, RoundUpToPowerOfTwo(1));
    409   EXPECT_EQ(2, RoundUpToPowerOfTwo(2));
    410   EXPECT_EQ(4, RoundUpToPowerOfTwo(3));
    411   EXPECT_EQ(8, RoundUpToPowerOfTwo(7));
    412 
    413   EXPECT_EQ(0b10000L, RoundUpToPowerOfTwo(0b01101L));
    414   EXPECT_EQ(1ULL << 63, RoundUpToPowerOfTwo(1ULL << 62 | 1ULL));
    415 }
    416 
    417 TEST_F(UtilsTest, MostSignificantBit) {
    418   EXPECT_EQ(-1, MostSignificantBit(0));
    419   EXPECT_EQ(0, MostSignificantBit(1));
    420   EXPECT_EQ(31, MostSignificantBit(~static_cast<uint32_t>(0)));
    421   EXPECT_EQ(2, MostSignificantBit(0b110));
    422   EXPECT_EQ(2, MostSignificantBit(0b100));
    423 }
    424 
    425 TEST_F(UtilsTest, MinimumBitsToStore) {
    426   EXPECT_EQ(0u, MinimumBitsToStore(0));
    427   EXPECT_EQ(1u, MinimumBitsToStore(1));
    428   EXPECT_EQ(2u, MinimumBitsToStore(0b10));
    429   EXPECT_EQ(2u, MinimumBitsToStore(0b11));
    430   EXPECT_EQ(3u, MinimumBitsToStore(0b100));
    431   EXPECT_EQ(3u, MinimumBitsToStore(0b110));
    432   EXPECT_EQ(3u, MinimumBitsToStore(0b101));
    433   EXPECT_EQ(8u, MinimumBitsToStore(0xFF));
    434   EXPECT_EQ(32u, MinimumBitsToStore(~static_cast<uint32_t>(0)));
    435 }
    436 
    437 }  // namespace art
    438