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