Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2015 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 "test_dex_file_builder.h"
     18 
     19 #include "dex_file-inl.h"
     20 #include "gtest/gtest.h"
     21 #include "utils.h"
     22 
     23 namespace art {
     24 
     25 TEST(TestDexFileBuilderTest, SimpleTest) {
     26   TestDexFileBuilder builder;
     27   builder.AddString("Arbitrary string");
     28   builder.AddType("Ljava/lang/Class;");
     29   builder.AddField("LTestClass;", "[I", "intField");
     30   builder.AddMethod("LTestClass;", "()I", "foo");
     31   builder.AddMethod("LTestClass;", "(Ljava/lang/Object;[Ljava/lang/Object;)LTestClass;", "bar");
     32   const char* dex_location = "TestDexFileBuilder/SimpleTest";
     33   std::unique_ptr<const DexFile> dex_file(builder.Build(dex_location));
     34   ASSERT_TRUE(dex_file != nullptr);
     35   EXPECT_STREQ(dex_location, dex_file->GetLocation().c_str());
     36 
     37   static const char* const expected_strings[] = {
     38       "Arbitrary string",
     39       "I",
     40       "LLL",  // shorty
     41       "LTestClass;",
     42       "Ljava/lang/Class;",
     43       "Ljava/lang/Object;",
     44       "[I",
     45       "[Ljava/lang/Object;",
     46       "bar",
     47       "foo",
     48       "intField",
     49   };
     50   ASSERT_EQ(arraysize(expected_strings), dex_file->NumStringIds());
     51   for (size_t i = 0; i != arraysize(expected_strings); ++i) {
     52     EXPECT_STREQ(expected_strings[i],
     53                  dex_file->GetStringData(dex_file->GetStringId(dex::StringIndex(i)))) << i;
     54   }
     55 
     56   static const char* const expected_types[] = {
     57       "I",
     58       "LTestClass;",
     59       "Ljava/lang/Class;",
     60       "Ljava/lang/Object;",
     61       "[I",
     62       "[Ljava/lang/Object;",
     63   };
     64   ASSERT_EQ(arraysize(expected_types), dex_file->NumTypeIds());
     65   for (size_t i = 0; i != arraysize(expected_types); ++i) {
     66     EXPECT_STREQ(expected_types[i],
     67                  dex_file->GetTypeDescriptor(dex_file->GetTypeId(dex::TypeIndex(i)))) << i;
     68   }
     69 
     70   ASSERT_EQ(1u, dex_file->NumFieldIds());
     71   EXPECT_STREQ("[I TestClass.intField", dex_file->PrettyField(0u).c_str());
     72 
     73   ASSERT_EQ(2u, dex_file->NumProtoIds());
     74   ASSERT_EQ(2u, dex_file->NumMethodIds());
     75   EXPECT_STREQ("TestClass TestClass.bar(java.lang.Object, java.lang.Object[])",
     76                dex_file->PrettyMethod(0u).c_str());
     77   EXPECT_STREQ("int TestClass.foo()",
     78                dex_file->PrettyMethod(1u).c_str());
     79 
     80   EXPECT_EQ(0u, builder.GetStringIdx("Arbitrary string"));
     81   EXPECT_EQ(2u, builder.GetTypeIdx("Ljava/lang/Class;"));
     82   EXPECT_EQ(0u, builder.GetFieldIdx("LTestClass;", "[I", "intField"));
     83   EXPECT_EQ(1u, builder.GetMethodIdx("LTestClass;", "()I", "foo"));
     84   EXPECT_EQ(0u, builder.GetMethodIdx("LTestClass;", "(Ljava/lang/Object;[Ljava/lang/Object;)LTestClass;", "bar"));
     85 }
     86 
     87 }  // namespace art
     88