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 23namespace art { 24 25TEST(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], dex_file->GetStringData(dex_file->GetStringId(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], dex_file->GetTypeDescriptor(dex_file->GetTypeId(i))) << i; 66 } 67 68 ASSERT_EQ(1u, dex_file->NumFieldIds()); 69 EXPECT_STREQ("[I TestClass.intField", PrettyField(0u, *dex_file).c_str()); 70 71 ASSERT_EQ(2u, dex_file->NumProtoIds()); 72 ASSERT_EQ(2u, dex_file->NumMethodIds()); 73 EXPECT_STREQ("TestClass TestClass.bar(java.lang.Object, java.lang.Object[])", 74 PrettyMethod(0u, *dex_file).c_str()); 75 EXPECT_STREQ("int TestClass.foo()", 76 PrettyMethod(1u, *dex_file).c_str()); 77 78 EXPECT_EQ(0u, builder.GetStringIdx("Arbitrary string")); 79 EXPECT_EQ(2u, builder.GetTypeIdx("Ljava/lang/Class;")); 80 EXPECT_EQ(0u, builder.GetFieldIdx("LTestClass;", "[I", "intField")); 81 EXPECT_EQ(1u, builder.GetMethodIdx("LTestClass;", "()I", "foo")); 82 EXPECT_EQ(0u, builder.GetMethodIdx("LTestClass;", "(Ljava/lang/Object;[Ljava/lang/Object;)LTestClass;", "bar")); 83} 84 85} // namespace art 86